-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve RuntimeSupport integration test performance
- Loading branch information
Showing
8 changed files
with
241 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...Support.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/Helpers/CommandLineWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers; | ||
|
||
public static class CommandLineWrapper | ||
{ | ||
public static void Run(string command, string arguments, string workingDirectory, ITestOutputHelper outputHelper) | ||
{ | ||
string tempOutputFile = Path.GetTempFileName(); | ||
var startInfo = new ProcessStartInfo | ||
{ | ||
FileName = GetSystemShell(), | ||
Arguments = | ||
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? | ||
$"/c {command} {arguments} > \"{tempOutputFile}\" 2>&1" : | ||
$"-c \"{command} {arguments} > '{tempOutputFile}' 2>&1\"", | ||
WorkingDirectory = workingDirectory, | ||
UseShellExecute = true, | ||
CreateNoWindow = true | ||
}; | ||
|
||
using (var process = Process.Start(startInfo)) | ||
{ | ||
if (process == null) | ||
throw new Exception($"Unable to start process: {command} {arguments}"); | ||
|
||
process.WaitForExit(); | ||
|
||
string output = File.ReadAllText(tempOutputFile); | ||
outputHelper.WriteLine(output); | ||
|
||
Assert.True(process.ExitCode == 0, $"Command '{command} {arguments}' failed."); | ||
} | ||
File.Delete(tempOutputFile); | ||
} | ||
|
||
private static string GetSystemShell() | ||
{ | ||
if (TryGetEnvironmentVariable("COMSPEC", out var comspec)) | ||
return comspec!; | ||
|
||
if (TryGetEnvironmentVariable("SHELL", out var shell)) | ||
return shell!; | ||
|
||
// fall back to defaults | ||
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "bash"; | ||
} | ||
|
||
private static bool TryGetEnvironmentVariable(string variable, out string value) | ||
{ | ||
value = Environment.GetEnvironmentVariable(variable); | ||
|
||
return !string.IsNullOrEmpty(value); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...eSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/Helpers/LambdaToolsHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Xunit.Abstractions; | ||
|
||
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers; | ||
|
||
public static class LambdaToolsHelper | ||
{ | ||
private static readonly string FunctionArchitecture = RuntimeInformation.OSArchitecture == System.Runtime.InteropServices.Architecture.Arm64 ? "arm64" : "x86_64"; | ||
|
||
public static string GetTempTestAppDirectory(string workingDirectory, string testAppPath) | ||
{ | ||
var customTestAppPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Directory.CreateDirectory(customTestAppPath); | ||
|
||
var currentDir = new DirectoryInfo(workingDirectory); | ||
CopyDirectory(currentDir, customTestAppPath); | ||
|
||
return Path.Combine(customTestAppPath, testAppPath); | ||
} | ||
|
||
public static string InstallLambdaTools(ITestOutputHelper output) | ||
{ | ||
var customToolPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Directory.CreateDirectory(customToolPath); | ||
CommandLineWrapper.Run( | ||
"dotnet", | ||
$"tool install Amazon.Lambda.Tools --tool-path {customToolPath}", | ||
Directory.GetCurrentDirectory(), | ||
output); | ||
return customToolPath; | ||
} | ||
|
||
public static void DotnetRestore(string workingDirectory, ITestOutputHelper output) | ||
{ | ||
CommandLineWrapper.Run( | ||
"dotnet", | ||
"restore", | ||
workingDirectory, | ||
output); | ||
} | ||
|
||
public static void LambdaPackage(string toolPath, string framework, string workingDirectory, ITestOutputHelper output) | ||
{ | ||
string lambdaToolPath = Path.Combine(toolPath, "dotnet-lambda"); | ||
CommandLineWrapper.Run( | ||
lambdaToolPath, | ||
$"package -c Release --framework {framework} --function-architecture {FunctionArchitecture}", | ||
workingDirectory, | ||
output); | ||
} | ||
|
||
public static void CleanUp(string toolPath) | ||
{ | ||
if (!string.IsNullOrEmpty(toolPath) && Directory.Exists(toolPath)) | ||
{ | ||
Directory.Delete(toolPath, true); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories"/> | ||
/// </summary> | ||
private static void CopyDirectory(DirectoryInfo dir, string destDirName) | ||
{ | ||
if (!dir.Exists) | ||
{ | ||
throw new DirectoryNotFoundException($"Source directory does not exist or could not be found: {dir.FullName}"); | ||
} | ||
|
||
var dirs = dir.GetDirectories(); | ||
|
||
Directory.CreateDirectory(destDirName); | ||
|
||
var files = dir.GetFiles(); | ||
foreach (var file in files) | ||
{ | ||
var tempPath = Path.Combine(destDirName, file.Name); | ||
file.CopyTo(tempPath, false); | ||
} | ||
|
||
foreach (var subdir in dirs) | ||
{ | ||
var tempPath = Path.Combine(destDirName, subdir.Name); | ||
var subDir = new DirectoryInfo(subdir.FullName); | ||
CopyDirectory(subDir, tempPath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters