Skip to content

Commit

Permalink
chore: improve RuntimeSupport integration test performance
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Nov 3, 2024
1 parent 6db06d2 commit 07de121
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
<PackageReference Include="AWSSDK.IdentityManagement" Version="3.7.402.7" />
<PackageReference Include="AWSSDK.Lambda" Version="3.7.402.3" />
<PackageReference Include="AWSSDK.S3" Version="3.7.103.34" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit" Version="2.9.2" />

<!-- This needs to be referenced to allow testing via AssumeRole credentials -->
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.400.13" />
Expand All @@ -39,24 +39,4 @@
<ProjectReference Include="..\..\..\src\Amazon.Lambda.APIGatewayEvents\Amazon.Lambda.APIGatewayEvents.csproj" />
</ItemGroup>

<Target Name="PackageTestFunction" BeforeTargets="BeforeBuild">
<Exec Command="dotnet tool install -g Amazon.Lambda.Tools" IgnoreExitCode="true" />

<Exec WorkingDirectory="..\CustomRuntimeFunctionTest" Command="dotnet restore" />
<Exec WorkingDirectory="..\CustomRuntimeFunctionTest" Condition="'$(Architecture)'=='' or '$(Architecture)'=='x86'" Command="dotnet lambda package -c Release --framework net6.0" />
<Exec WorkingDirectory="..\CustomRuntimeFunctionTest" Condition="'$(Architecture)'=='arm64'" Command="dotnet lambda package -c Release --framework net6.0 --function-architecture arm64" />

<Exec WorkingDirectory="..\CustomRuntimeFunctionTest" Command="dotnet restore" />
<Exec WorkingDirectory="..\CustomRuntimeFunctionTest" Condition="'$(Architecture)'=='' or '$(Architecture)'=='x86'" Command="dotnet lambda package -c Release --framework net8.0" />
<Exec WorkingDirectory="..\CustomRuntimeFunctionTest" Condition="'$(Architecture)'=='arm64'" Command="dotnet lambda package -c Release --framework net8.0 --function-architecture arm64" />

<Exec WorkingDirectory="..\CustomRuntimeAspNetCoreMinimalApiTest" Command="dotnet restore" />
<Exec WorkingDirectory="..\CustomRuntimeAspNetCoreMinimalApiTest" Condition="'$(Architecture)'=='' or '$(Architecture)'=='x86'" Command="dotnet lambda package -c Release --framework net6.0" />
<Exec WorkingDirectory="..\CustomRuntimeAspNetCoreMinimalApiTest" Condition="'$(Architecture)'=='arm64'" Command="dotnet lambda package -c Release --framework net6.0 --function-architecture arm64" />

<Exec WorkingDirectory="..\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest" Command="dotnet restore" />
<Exec WorkingDirectory="..\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest" Condition="'$(Architecture)'=='' or '$(Architecture)'=='x86'" Command="dotnet lambda package -c Release --framework net6.0" />
<Exec WorkingDirectory="..\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest" Condition="'$(Architecture)'=='arm64'" Command="dotnet lambda package -c Release --framework net6.0 --function-architecture arm64" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,34 @@
using Xunit;
using Amazon.Lambda.APIGatewayEvents;
using System.Text.Json;
using Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers;
using Xunit.Abstractions;


namespace Amazon.Lambda.RuntimeSupport.IntegrationTests
{
public class CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest : BaseCustomRuntimeTest
{
public CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest()
public CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest(
ITestOutputHelper output)
: base("CustomRuntimeMinimalApiCustomSerializerTest-" + DateTime.Now.Ticks, "CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest.zip", @"CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest\bin\Release\net6.0\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest.zip", "bootstrap")
{
string testAppPath = null;
string toolPath = null;
try
{
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory(
"../../../../../../..",
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest");
toolPath = LambdaToolsHelper.InstallLambdaTools(output);
LambdaToolsHelper.DotnetRestore(testAppPath, output);
LambdaToolsHelper.LambdaPackage(toolPath, "net6.0", testAppPath, output);
}
finally
{
LambdaToolsHelper.CleanUp(testAppPath);
LambdaToolsHelper.CleanUp(toolPath);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,34 @@
using Xunit;
using Amazon.Lambda.APIGatewayEvents;
using System.Text.Json;
using Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers;
using Xunit.Abstractions;


namespace Amazon.Lambda.RuntimeSupport.IntegrationTests
{
public class CustomRuntimeAspNetCoreMinimalApiTest : BaseCustomRuntimeTest
{
public CustomRuntimeAspNetCoreMinimalApiTest()
public CustomRuntimeAspNetCoreMinimalApiTest(
ITestOutputHelper output)
: base("CustomRuntimeAspNetCoreMinimalApiTest-" + DateTime.Now.Ticks, "CustomRuntimeAspNetCoreMinimalApiTest.zip", @"CustomRuntimeAspNetCoreMinimalApiTest\bin\Release\net6.0\CustomRuntimeAspNetCoreMinimalApiTest.zip", "bootstrap")
{
string testAppPath = null;
string toolPath = null;
try
{
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory(
"../../../../../../..",
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeAspNetCoreMinimalApiTest");
toolPath = LambdaToolsHelper.InstallLambdaTools(output);
LambdaToolsHelper.DotnetRestore(testAppPath, output);
LambdaToolsHelper.LambdaPackage(toolPath, "net6.0", testAppPath, output);
}
finally
{
LambdaToolsHelper.CleanUp(testAppPath);
LambdaToolsHelper.CleanUp(toolPath);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers;
using Xunit;
using Xunit.Abstractions;
using static Amazon.Lambda.RuntimeSupport.IntegrationTests.CustomRuntimeTests;

namespace Amazon.Lambda.RuntimeSupport.IntegrationTests
{
public class CustomRuntimeNET6Tests : CustomRuntimeTests
{
public CustomRuntimeNET6Tests()
: base("CustomRuntimeNET6FunctionTest-" + DateTime.Now.Ticks, "CustomRuntimeFunctionTest.zip", @"CustomRuntimeFunctionTest\bin\Release\net6.0\CustomRuntimeFunctionTest.zip", "CustomRuntimeFunctionTest", TargetFramework.NET6)
public CustomRuntimeNET6Tests(ITestOutputHelper output)
: base(
output,
"CustomRuntimeNET6FunctionTest-" + DateTime.Now.Ticks,
"CustomRuntimeFunctionTest.zip",
@"CustomRuntimeFunctionTest\bin\Release\net6.0\CustomRuntimeFunctionTest.zip",
"CustomRuntimeFunctionTest",
TargetFramework.NET6)
{
}

Expand All @@ -51,8 +59,14 @@ public async Task TestAllNET6HandlersAsync()

public class CustomRuntimeNET8Tests : CustomRuntimeTests
{
public CustomRuntimeNET8Tests()
: base("CustomRuntimeNET8FunctionTest-" + DateTime.Now.Ticks, "CustomRuntimeFunctionTest.zip", @"CustomRuntimeFunctionTest\bin\Release\net8.0\CustomRuntimeFunctionTest.zip", "CustomRuntimeFunctionTest", TargetFramework.NET8)
public CustomRuntimeNET8Tests(ITestOutputHelper output)
: base(
output,
"CustomRuntimeNET8FunctionTest-" + DateTime.Now.Ticks,
"CustomRuntimeFunctionTest.zip",
@"CustomRuntimeFunctionTest\bin\Release\net8.0\CustomRuntimeFunctionTest.zip",
"CustomRuntimeFunctionTest",
TargetFramework.NET8)
{
}

Expand All @@ -73,12 +87,34 @@ public enum TargetFramework { NET6, NET8}

private TargetFramework _targetFramework;

public CustomRuntimeTests(string functionName, string deploymentZipKey, string deploymentPackageZipRelativePath, string handler, TargetFramework targetFramework)
public CustomRuntimeTests(
ITestOutputHelper output,
string functionName,
string deploymentZipKey,
string deploymentPackageZipRelativePath,
string handler,
TargetFramework targetFramework)
: base(functionName, deploymentZipKey, deploymentPackageZipRelativePath, handler)
{
_targetFramework = targetFramework;
string testAppPath = null;
string toolPath = null;
try
{
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory(
"../../../../../../..",
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeFunctionTest");
toolPath = LambdaToolsHelper.InstallLambdaTools(output);
LambdaToolsHelper.DotnetRestore(testAppPath, output);
LambdaToolsHelper.LambdaPackage(toolPath, _targetFramework == TargetFramework.NET8 ? "net8.0" : "net6.0", testAppPath, output);
}
finally
{
LambdaToolsHelper.CleanUp(testAppPath);
LambdaToolsHelper.CleanUp(toolPath);
}
}

protected virtual async Task TestAllHandlersAsync()
{
// run all test cases in one test to ensure they run serially
Expand Down
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);
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<PackageReference Include="AWSSDK.S3" Version="3.7.4.1" />
<!-- AWSSDK.SecurityToken is needed at runtime for environments which uses assume-role operation for credentials -->
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.1.99" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
8 changes: 4 additions & 4 deletions buildtools/build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@
<Exec Command="dotnet msbuild -restore /p:Configuration=$(Configuration) /p:AssemblyOriginatorKeyFile=$(AssemblyOriginatorKeyFile) /p:SignAssembly=$(SignAssembly)" WorkingDirectory="..\Libraries\src\%(LibraryName.FileName)"/>
</Target>
<Target Name="run-unit-tests">
<Exec Command="dotnet test -c $(Configuration)" WorkingDirectory="..\Libraries\test\Amazon.Lambda.RuntimeSupport.Tests\Amazon.Lambda.RuntimeSupport.UnitTests"/>
<Exec Command="dotnet test -c $(Configuration)" WorkingDirectory="..\Libraries\test\Amazon.Lambda.Annotations.SourceGenerators.Tests"/>
<Exec Command="dotnet test -c $(Configuration) --logger &quot;console;verbosity=detailed&quot;" WorkingDirectory="..\Libraries\test\Amazon.Lambda.RuntimeSupport.Tests\Amazon.Lambda.RuntimeSupport.UnitTests"/>
<Exec Command="dotnet test -c $(Configuration) --logger &quot;console;verbosity=detailed&quot;" WorkingDirectory="..\Libraries\test\Amazon.Lambda.Annotations.SourceGenerators.Tests"/>
</Target>
<Target Name="run-integ-tests">
<Exec Command="dotnet test -c $(Configuration)" WorkingDirectory="..\Libraries\test\Amazon.Lambda.RuntimeSupport.Tests\Amazon.Lambda.RuntimeSupport.IntegrationTests"/>
<Exec Command="dotnet test -c $(Configuration)" WorkingDirectory="..\Libraries\test\TestServerlessApp.IntegrationTests"/>
<Exec Command="dotnet test -c $(Configuration) --logger &quot;console;verbosity=detailed&quot;" WorkingDirectory="..\Libraries\test\Amazon.Lambda.RuntimeSupport.Tests\Amazon.Lambda.RuntimeSupport.IntegrationTests"/>
<Exec Command="dotnet test -c $(Configuration) --logger &quot;console;verbosity=detailed&quot;" WorkingDirectory="..\Libraries\test\TestServerlessApp.IntegrationTests"/>
</Target>
<Target Name="create-nuget-packages-cicd" DependsOnTargets="build-project-packages">
<Exec Command="$(PackCommand)" WorkingDirectory="..\Libraries\src\%(LibraryName.FileName)"/>
Expand Down

0 comments on commit 07de121

Please sign in to comment.