Skip to content

Commit d60bb93

Browse files
committed
Add integ tests
1 parent bd0170e commit d60bb93

File tree

13 files changed

+282
-23
lines changed

13 files changed

+282
-23
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*.suo
55
*.user
66

7+
**/.kiro/
8+
79
####################
810
# Build/Test folders
911
####################

Libraries/Libraries.sln

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 17
4-
VisualStudioVersion = 17.0.31717.71
3+
# Visual Studio Version 18
4+
VisualStudioVersion = 18.3.11512.155 d18.3
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AAB54E74-20B1-42ED-BC3D-CE9F7BC7FD12}"
77
EndProject
@@ -151,6 +151,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCustomAuthorizerApp.Int
151151
EndProject
152152
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCustomAuthorizerApp", "test\TestCustomAuthorizerApp\TestCustomAuthorizerApp.csproj", "{3BFA4B73-BA61-4578-833B-C5B3A16EDA9E}"
153153
EndProject
154+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResponseStreamingFunctionHandlers", "test\Amazon.Lambda.RuntimeSupport.Tests\ResponseStreamingFunctionHandlers\ResponseStreamingFunctionHandlers.csproj", "{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}"
155+
EndProject
154156
Global
155157
GlobalSection(SolutionConfigurationPlatforms) = preSolution
156158
Debug|Any CPU = Debug|Any CPU
@@ -941,6 +943,18 @@ Global
941943
{3BFA4B73-BA61-4578-833B-C5B3A16EDA9E}.Release|x64.Build.0 = Release|Any CPU
942944
{3BFA4B73-BA61-4578-833B-C5B3A16EDA9E}.Release|x86.ActiveCfg = Release|Any CPU
943945
{3BFA4B73-BA61-4578-833B-C5B3A16EDA9E}.Release|x86.Build.0 = Release|Any CPU
946+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
947+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
948+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Debug|x64.ActiveCfg = Debug|Any CPU
949+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Debug|x64.Build.0 = Debug|Any CPU
950+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Debug|x86.ActiveCfg = Debug|Any CPU
951+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Debug|x86.Build.0 = Debug|Any CPU
952+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
953+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Release|Any CPU.Build.0 = Release|Any CPU
954+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Release|x64.ActiveCfg = Release|Any CPU
955+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Release|x64.Build.0 = Release|Any CPU
956+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Release|x86.ActiveCfg = Release|Any CPU
957+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9}.Release|x86.Build.0 = Release|Any CPU
944958
EndGlobalSection
945959
GlobalSection(SolutionProperties) = preSolution
946960
HideSolutionNode = FALSE
@@ -1015,6 +1029,7 @@ Global
10151029
{8D03BDF3-7078-4B46-A3F1-C73BE6D6CE0D} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
10161030
{8EEDD576-7FC4-4FAC-A5A2-F58562753A53} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
10171031
{3BFA4B73-BA61-4578-833B-C5B3A16EDA9E} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
1032+
{E404A7AC-812B-BC03-CA76-02C0BC2BA7F9} = {B5BD0336-7D08-492C-8489-42C987E29B39}
10181033
EndGlobalSection
10191034
GlobalSection(ExtensibilityGlobals) = postSolution
10201035
SolutionGuid = {503678A4-B8D1-4486-8915-405A3E9CF0EB}

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/Amazon.Lambda.RuntimeSupport.IntegrationTests.csproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
@@ -19,19 +19,19 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="AWSSDK.IdentityManagement" Version="3.7.402.7" />
23-
<PackageReference Include="AWSSDK.Lambda" Version="3.7.402.3" />
24-
<PackageReference Include="AWSSDK.S3" Version="3.7.103.34" />
25-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
26-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
27-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
22+
<PackageReference Include="AWSSDK.IdentityManagement" Version="4.0.9.9" />
23+
<PackageReference Include="AWSSDK.Lambda" Version="4.0.13.3" />
24+
<PackageReference Include="AWSSDK.S3" Version="4.0.18.8" />
25+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
26+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
27+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
2828
<PrivateAssets>all</PrivateAssets>
2929
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
3030
</PackageReference>
31-
<PackageReference Include="xunit" Version="2.9.2" />
31+
<PackageReference Include="xunit" Version="2.9.3" />
3232

3333
<!-- This needs to be referenced to allow testing via AssumeRole credentials -->
34-
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.400.13" />
34+
<PackageReference Include="AWSSDK.SecurityToken" Version="4.0.5.11" />
3535
</ItemGroup>
3636

3737
<ItemGroup>

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/BaseCustomRuntimeTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class BaseCustomRuntimeTest
1717
{
1818
public const int FUNCTION_MEMORY_MB = 512;
1919

20-
protected static readonly RegionEndpoint TestRegion = RegionEndpoint.USWest2;
20+
public static readonly RegionEndpoint TestRegion = RegionEndpoint.USWest2;
2121
protected static readonly string LAMBDA_ASSUME_ROLE_POLICY =
2222
@"
2323
{
@@ -63,7 +63,7 @@ protected BaseCustomRuntimeTest(IntegrationTestFixture fixture, string functionN
6363
/// <param name="s3Client"></param>
6464
/// <param name="lambdaClient"></param>
6565
/// <returns></returns>
66-
protected async Task CleanUpTestResources(AmazonS3Client s3Client, AmazonLambdaClient lambdaClient,
66+
public async Task CleanUpTestResources(AmazonS3Client s3Client, AmazonLambdaClient lambdaClient,
6767
AmazonIdentityManagementServiceClient iamClient, bool roleAlreadyExisted)
6868
{
6969
await DeleteFunctionIfExistsAsync(lambdaClient);
@@ -109,7 +109,7 @@ await iamClient.DetachRolePolicyAsync(new DetachRolePolicyRequest
109109
}
110110
}
111111

112-
protected async Task<bool> PrepareTestResources(IAmazonS3 s3Client, IAmazonLambda lambdaClient,
112+
public async Task<bool> PrepareTestResources(IAmazonS3 s3Client, IAmazonLambda lambdaClient,
113113
AmazonIdentityManagementServiceClient iamClient)
114114
{
115115
var roleAlreadyExisted = await ValidateAndSetIamRoleArn(iamClient);
@@ -288,7 +288,7 @@ protected async Task CreateFunctionAsync(IAmazonLambda lambdaClient, string buck
288288
Handler = Handler,
289289
MemorySize = FUNCTION_MEMORY_MB,
290290
Timeout = 30,
291-
Runtime = Runtime.Dotnet6,
291+
Runtime = Runtime.Dotnet10,
292292
Role = ExecutionRoleArn
293293
};
294294

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/CustomRuntimeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task TestAllNET8HandlersAsync()
4848

4949
public class CustomRuntimeTests : BaseCustomRuntimeTest
5050
{
51-
public enum TargetFramework { NET6, NET8}
51+
public enum TargetFramework { NET8 }
5252

5353
private TargetFramework _targetFramework;
5454

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/Helpers/CommandLineWrapper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Text;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using Xunit;
@@ -31,6 +32,7 @@ public static async Task Run(string command, string arguments, string workingDir
3132
tcs.TrySetResult(true);
3233
};
3334

35+
var output = new StringBuilder();
3436
try
3537
{
3638
// Attach event handlers
@@ -39,6 +41,7 @@ public static async Task Run(string command, string arguments, string workingDir
3941
if (!string.IsNullOrEmpty(args.Data))
4042
{
4143
Console.WriteLine(args.Data);
44+
output.Append(args.Data);
4245
}
4346
};
4447

@@ -47,6 +50,7 @@ public static async Task Run(string command, string arguments, string workingDir
4750
if (!string.IsNullOrEmpty(args.Data))
4851
{
4952
Console.WriteLine(args.Data);
53+
output.Append(args.Data);
5054
}
5155
};
5256

@@ -87,4 +91,4 @@ public static async Task Run(string command, string arguments, string workingDir
8791
Assert.True(process.ExitCode == 0, $"Command '{command} {arguments}' failed.");
8892
}
8993
}
90-
}
94+
}

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/Helpers/LambdaToolsHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ public static class LambdaToolsHelper
1010

1111
public static string GetTempTestAppDirectory(string workingDirectory, string testAppPath)
1212
{
13+
#if DEBUG
14+
return Path.GetFullPath(Path.Combine(workingDirectory, testAppPath));
15+
#else
1316
var customTestAppPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
1417
Directory.CreateDirectory(customTestAppPath);
1518

1619
var currentDir = new DirectoryInfo(workingDirectory);
1720
CopyDirectory(currentDir, customTestAppPath);
1821

1922
return Path.Combine(customTestAppPath, testAppPath);
23+
#endif
2024
}
2125

2226
public static async Task<string> InstallLambdaTools()
@@ -78,4 +82,4 @@ private static void CopyDirectory(DirectoryInfo dir, string destDirName)
7882
CopyDirectory(subDir, tempPath);
7983
}
8084
}
81-
}
85+
}

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/IntegrationTestCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests;
44

55
[CollectionDefinition("Integration Tests")]
6-
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture>
6+
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture>, ICollectionFixture<ResponseStreamingTestsFixture>
77
{
88

9-
}
9+
}

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/IntegrationTestFixture.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,46 @@ public class IntegrationTestFixture : IAsyncLifetime
1414

1515
public async Task InitializeAsync()
1616
{
17+
var toolPath = await LambdaToolsHelper.InstallLambdaTools();
18+
1719
var testAppPath = LambdaToolsHelper.GetTempTestAppDirectory(
1820
"../../../../../../..",
1921
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeFunctionTest");
20-
var toolPath = await LambdaToolsHelper.InstallLambdaTools();
2122
_tempPaths.AddRange([testAppPath, toolPath] );
2223
await LambdaToolsHelper.LambdaPackage(toolPath, "net8.0", testAppPath);
2324
TestAppPaths[@"CustomRuntimeFunctionTest\bin\Release\net8.0\CustomRuntimeFunctionTest.zip"] = Path.Combine(testAppPath, @"bin\Release\net8.0\CustomRuntimeFunctionTest.zip");
2425

2526
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory(
2627
"../../../../../../..",
2728
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeAspNetCoreMinimalApiTest");
28-
toolPath = await LambdaToolsHelper.InstallLambdaTools();
2929
_tempPaths.AddRange([testAppPath, toolPath] );
3030
await LambdaToolsHelper.LambdaPackage(toolPath, "net8.0", testAppPath);
3131
TestAppPaths[@"CustomRuntimeAspNetCoreMinimalApiTest\bin\Release\net8.0\CustomRuntimeAspNetCoreMinimalApiTest.zip"] = Path.Combine(testAppPath, @"bin\Release\net8.0\CustomRuntimeAspNetCoreMinimalApiTest.zip");
3232

3333
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory(
3434
"../../../../../../..",
3535
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest");
36-
toolPath = await LambdaToolsHelper.InstallLambdaTools();
3736
_tempPaths.AddRange([testAppPath, toolPath] );
3837
await LambdaToolsHelper.LambdaPackage(toolPath, "net8.0", testAppPath);
3938
TestAppPaths[@"CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest\bin\Release\net8.0\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest.zip"] = Path.Combine(testAppPath, @"bin\Release\net8.0\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest.zip");
39+
40+
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory(
41+
"../../../../../../..",
42+
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/ResponseStreamingFunctionHandlers");
43+
_tempPaths.AddRange([testAppPath, toolPath]);
44+
await LambdaToolsHelper.LambdaPackage(toolPath, "net10.0", testAppPath);
45+
TestAppPaths[@"ResponseStreamingFunctionHandlers\bin\Release\net10.0\ResponseStreamingFunctionHandlers.zip"] = Path.Combine(testAppPath, @"bin\Release\net10.0\ResponseStreamingFunctionHandlers.zip");
4046
}
4147

4248

4349
public Task DisposeAsync()
4450
{
51+
#if !DEBUG
4552
foreach (var tempPath in _tempPaths)
4653
{
4754
LambdaToolsHelper.CleanUp(tempPath);
4855
}
56+
#endif
4957

5058
return Task.CompletedTask;
5159
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Amazon.IdentityManagement;
11+
using Amazon.Lambda.Model;
12+
using Amazon.Runtime.EventStreams;
13+
using Amazon.S3;
14+
using Xunit;
15+
16+
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests
17+
{
18+
[Collection("Integration Tests")]
19+
public class ResponseStreamingTests : BaseCustomRuntimeTest
20+
{
21+
private readonly static string s_functionName = "IntegTestResponseStreamingFunctionHandlers" + DateTime.Now.Ticks;
22+
23+
private readonly ResponseStreamingTestsFixture _streamFixture;
24+
25+
public ResponseStreamingTests(IntegrationTestFixture fixture, ResponseStreamingTestsFixture streamFixture)
26+
: base(fixture, s_functionName, "ResponseStreamingFunctionHandlers.zip", @"ResponseStreamingFunctionHandlers\bin\Release\net10.0\ResponseStreamingFunctionHandlers.zip", "ResponseStreamingFunctionHandlers")
27+
{
28+
_streamFixture = streamFixture;
29+
}
30+
31+
[Fact]
32+
public async Task SimpleFunctionHandler()
33+
{
34+
await _streamFixture.EnsureResourcesDeployedAsync(this);
35+
36+
var evnts = await InvokeFunctionAsync(nameof(SimpleFunctionHandler));
37+
Assert.True(evnts.Any());
38+
39+
var content = GetCombinedStreamContent(evnts);
40+
Assert.Equal("Hello, World!", content);
41+
}
42+
43+
[Fact]
44+
public async Task StreamContentHandler()
45+
{
46+
await _streamFixture.EnsureResourcesDeployedAsync(this);
47+
48+
var evnts = await InvokeFunctionAsync(nameof(StreamContentHandler));
49+
Assert.True(evnts.Length > 5);
50+
51+
var content = GetCombinedStreamContent(evnts);
52+
Assert.Contains("Line 9999", content);
53+
Assert.EndsWith("Finish stream content\n", content);
54+
}
55+
56+
[Fact]
57+
public async Task UnhandledExceptionHandler()
58+
{
59+
await _streamFixture.EnsureResourcesDeployedAsync(this);
60+
61+
var evnts = await InvokeFunctionAsync(nameof(UnhandledExceptionHandler));
62+
Assert.True(evnts.Any());
63+
64+
var content = GetCombinedStreamContent(evnts);
65+
Assert.Contains("This method will fail", content);
66+
Assert.Contains("This is an unhandled exception", content);
67+
Assert.Contains("Lambda-Runtime-Function-Error-Type", content);
68+
Assert.Contains("InvalidOperationException", content);
69+
Assert.Contains("This is an unhandled exception", content);
70+
Assert.Contains("stackTrace", content);
71+
}
72+
73+
private async Task<IEventStreamEvent[]> InvokeFunctionAsync(string handlerScenario)
74+
{
75+
using var client = new AmazonLambdaClient(TestRegion);
76+
77+
var request = new InvokeWithResponseStreamRequest
78+
{
79+
FunctionName = base.FunctionName,
80+
Payload = new MemoryStream(System.Text.Encoding.UTF8.GetBytes($"\"{handlerScenario}\"")),
81+
InvocationType = ResponseStreamingInvocationType.RequestResponse
82+
};
83+
84+
var response = await client.InvokeWithResponseStreamAsync(request);
85+
var evnts = response.EventStream.AsEnumerable().ToArray();
86+
return evnts;
87+
}
88+
89+
private string GetCombinedStreamContent(IEventStreamEvent[] events)
90+
{
91+
var sb = new StringBuilder();
92+
foreach (var evnt in events)
93+
{
94+
if (evnt is InvokeResponseStreamUpdate chunk)
95+
{
96+
var text = System.Text.Encoding.UTF8.GetString(chunk.Payload.ToArray());
97+
sb.Append(text);
98+
}
99+
}
100+
return sb.ToString();
101+
}
102+
}
103+
104+
public class ResponseStreamingTestsFixture : IAsyncLifetime
105+
{
106+
private readonly AmazonLambdaClient _lambdaClient = new AmazonLambdaClient(BaseCustomRuntimeTest.TestRegion);
107+
private readonly AmazonS3Client _s3Client = new AmazonS3Client(BaseCustomRuntimeTest.TestRegion);
108+
private readonly AmazonIdentityManagementServiceClient _iamClient = new AmazonIdentityManagementServiceClient(BaseCustomRuntimeTest.TestRegion);
109+
bool _resourcesCreated;
110+
bool _roleAlreadyExisted;
111+
112+
ResponseStreamingTests _tests;
113+
114+
public async Task EnsureResourcesDeployedAsync(ResponseStreamingTests tests)
115+
{
116+
if (_resourcesCreated)
117+
return;
118+
119+
_tests = tests;
120+
_roleAlreadyExisted = await _tests.PrepareTestResources(_s3Client, _lambdaClient, _iamClient);
121+
122+
_resourcesCreated = true;
123+
}
124+
125+
public async Task DisposeAsync()
126+
{
127+
await _tests.CleanUpTestResources(_s3Client, _lambdaClient, _iamClient, _roleAlreadyExisted);
128+
129+
_lambdaClient.Dispose();
130+
_s3Client.Dispose();
131+
_iamClient.Dispose();
132+
}
133+
134+
public Task InitializeAsync() => Task.CompletedTask;
135+
}
136+
}

0 commit comments

Comments
 (0)