From 1630bb2a46598bb24347cf9593ce8a7420212d07 Mon Sep 17 00:00:00 2001 From: Phil Asmar Date: Tue, 25 Feb 2025 15:17:22 -0500 Subject: [PATCH] chore: Update test tool tests to use cmdline args instead of env vars --- ...on.Lambda.TestTool.IntegrationTests.csproj | 2 +- .../ApiGatewayEmulatorProcessTests.cs | 30 ++++++++----------- .../Amazon.Lambda.TestTool.UnitTests.csproj | 2 +- .../RuntimeApiTests.cs | 18 +++++------ 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/Amazon.Lambda.TestTool.IntegrationTests.csproj b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/Amazon.Lambda.TestTool.IntegrationTests.csproj index 00f962109..7c1381008 100644 --- a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/Amazon.Lambda.TestTool.IntegrationTests.csproj +++ b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/Amazon.Lambda.TestTool.IntegrationTests.csproj @@ -11,7 +11,7 @@ - + diff --git a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayEmulatorProcessTests.cs b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayEmulatorProcessTests.cs index 275d28f6a..2c6682e0f 100644 --- a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayEmulatorProcessTests.cs +++ b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayEmulatorProcessTests.cs @@ -42,8 +42,7 @@ public async Task TestLambdaToUpperV2() await WaitForGatewayHealthCheck(apiGatewayPort); var handler = (APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context) => { - var env = Environment.GetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API"); - testOutputHelper.WriteLine($"TestLambdaToUpperV2: {env}"); + testOutputHelper.WriteLine($"TestLambdaToUpperV2"); return new APIGatewayHttpApiV2ProxyResponse { StatusCode = 200, @@ -51,8 +50,8 @@ public async Task TestLambdaToUpperV2() }; }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"localhost:{lambdaPort}/testfunction"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"localhost:{lambdaPort}/testfunction") .Build() .RunAsync(_cancellationTokenSource.Token); @@ -83,8 +82,7 @@ public async Task TestLambdaToUpperRest() await WaitForGatewayHealthCheck(apiGatewayPort); var handler = (APIGatewayProxyRequest request, ILambdaContext context) => { - var env = Environment.GetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API"); - testOutputHelper.WriteLine($"TestLambdaToUpperRest: {env}"); + testOutputHelper.WriteLine($"TestLambdaToUpperRest"); return new APIGatewayProxyResponse() { StatusCode = 200, @@ -93,8 +91,8 @@ public async Task TestLambdaToUpperRest() }; }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"localhost:{lambdaPort}/testfunction"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"localhost:{lambdaPort}/testfunction") .Build() .RunAsync(_cancellationTokenSource.Token); @@ -125,8 +123,7 @@ public async Task TestLambdaToUpperV1() await WaitForGatewayHealthCheck(apiGatewayPort); var handler = (APIGatewayProxyRequest request, ILambdaContext context) => { - var env = Environment.GetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API"); - testOutputHelper.WriteLine($"TestLambdaToUpperV1: {env}"); + testOutputHelper.WriteLine($"TestLambdaToUpperV1"); return new APIGatewayProxyResponse() { StatusCode = 200, @@ -135,8 +132,8 @@ public async Task TestLambdaToUpperV1() }; }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"localhost:{lambdaPort}/testfunction"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"localhost:{lambdaPort}/testfunction") .Build() .RunAsync(_cancellationTokenSource.Token); @@ -167,8 +164,7 @@ public async Task TestLambdaBinaryResponse() await WaitForGatewayHealthCheck(apiGatewayPort); var handler = (APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context) => { - var env = Environment.GetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API"); - testOutputHelper.WriteLine($"TestLambdaBinaryResponse: {env}"); + testOutputHelper.WriteLine($"TestLambdaBinaryResponse"); // Create a simple binary pattern (for example, counting bytes from 0 to 255) byte[] binaryData = new byte[256]; for (int i = 0; i < 256; i++) @@ -188,8 +184,8 @@ public async Task TestLambdaBinaryResponse() }; }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"localhost:{lambdaPort}/binaryfunction"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"localhost:{lambdaPort}/binaryfunction") .Build() .RunAsync(_cancellationTokenSource.Token); @@ -226,13 +222,12 @@ public async Task TestLambdaReturnString() await WaitForGatewayHealthCheck(apiGatewayPort); var handler = (APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context) => { - var env = Environment.GetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API"); - testOutputHelper.WriteLine($"TestLambdaReturnString: {env}"); + testOutputHelper.WriteLine($"TestLambdaReturnString"); return request.Body.ToUpper(); }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"localhost:{lambdaPort}/stringfunction"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"localhost:{lambdaPort}/stringfunction") .Build() .RunAsync(_cancellationTokenSource.Token); @@ -263,8 +258,7 @@ public async Task TestLambdaWithNullEndpoint() await WaitForGatewayHealthCheck(apiGatewayPort); var handler = (APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context) => { - var env = Environment.GetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API"); - testOutputHelper.WriteLine($"TestLambdaWithNullEndpoint: {env}"); + testOutputHelper.WriteLine($"TestLambdaWithNullEndpoint"); return new APIGatewayHttpApiV2ProxyResponse { StatusCode = 200, @@ -272,8 +266,8 @@ public async Task TestLambdaWithNullEndpoint() }; }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"localhost:{lambdaPort}/testfunction"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"localhost:{lambdaPort}/testfunction") .Build() .RunAsync(_cancellationTokenSource.Token); diff --git a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Amazon.Lambda.TestTool.UnitTests.csproj b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Amazon.Lambda.TestTool.UnitTests.csproj index 94f9b7e07..81788cc9e 100644 --- a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Amazon.Lambda.TestTool.UnitTests.csproj +++ b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Amazon.Lambda.TestTool.UnitTests.csproj @@ -11,7 +11,7 @@ - + diff --git a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/RuntimeApiTests.cs b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/RuntimeApiTests.cs index 4194bde1f..2441824a4 100644 --- a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/RuntimeApiTests.cs +++ b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/RuntimeApiTests.cs @@ -19,11 +19,7 @@ namespace Amazon.Lambda.TestTool.UnitTests; public class RuntimeApiTests { -#if DEBUG - [Fact] -#else - [Fact(Skip = "Skipping this test as it is not working properly.")] -#endif + [RetryFact] public async Task AddEventToDataStore() { const string functionName = "FunctionFoo"; @@ -62,10 +58,10 @@ public async Task AddEventToDataStore() return input.ToUpper(); }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"{options.LambdaEmulatorHost}:{options.LambdaEmulatorPort}/{functionName}"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) - .Build() - .RunAsync(cancellationTokenSource.Token); + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"{options.LambdaEmulatorHost}:{options.LambdaEmulatorPort}/{functionName}") + .Build() + .RunAsync(cancellationTokenSource.Token); await Task.Delay(2_000, cancellationTokenSource.Token); Assert.True(handlerCalled); @@ -96,10 +92,10 @@ public async Task InvokeRequestResponse() return input.ToUpper(); }; - Environment.SetEnvironmentVariable("AWS_LAMBDA_RUNTIME_API", $"{options.LambdaEmulatorHost}:{options.LambdaEmulatorPort}/{functionName}"); _ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()) - .Build() - .RunAsync(cancellationTokenSource.Token); + .ConfigureOptions(x => x.RuntimeApiEndpoint = $"{options.LambdaEmulatorHost}:{options.LambdaEmulatorPort}/{functionName}") + .Build() + .RunAsync(cancellationTokenSource.Token); var lambdaClient = ConstructLambdaServiceClient(testToolProcess.ServiceUrl);