Skip to content

Commit 7c44dac

Browse files
authored
Aspire tweaks for test tool (#1948)
1 parent 6b857ec commit 7c44dac

File tree

7 files changed

+71
-58
lines changed

7 files changed

+71
-58
lines changed

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/RunCommand.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,44 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
3030
{
3131
EvaluateEnvironmentVariables(settings);
3232

33-
var tasks = new List<Task>();
33+
if (!settings.LambdaEmulatorPort.HasValue && !settings.ApiGatewayEmulatorPort.HasValue)
34+
{
35+
throw new ArgumentException("At least one of the following parameters must be set: " +
36+
"--lambda-emulator-port or --api-gateway-emulator-port");
37+
}
3438

35-
var testToolProcess = TestToolProcess.Startup(settings, cancellationTokenSource.Token);
36-
tasks.Add(testToolProcess.RunningTask);
39+
var tasks = new List<Task>();
3740

38-
if (!settings.NoLaunchWindow)
41+
if (settings.LambdaEmulatorPort.HasValue)
3942
{
40-
try
43+
var testToolProcess = TestToolProcess.Startup(settings, cancellationTokenSource.Token);
44+
tasks.Add(testToolProcess.RunningTask);
45+
46+
if (!settings.NoLaunchWindow)
4147
{
42-
var info = new ProcessStartInfo
48+
try
4349
{
44-
UseShellExecute = true,
45-
FileName = testToolProcess.ServiceUrl
46-
};
47-
Process.Start(info);
48-
}
49-
catch (Exception e)
50-
{
51-
toolInteractiveService.WriteErrorLine($"Error launching browser: {e.Message}");
50+
var info = new ProcessStartInfo
51+
{
52+
UseShellExecute = true,
53+
FileName = testToolProcess.ServiceUrl
54+
};
55+
Process.Start(info);
56+
}
57+
catch (Exception e)
58+
{
59+
toolInteractiveService.WriteErrorLine($"Error launching browser: {e.Message}");
60+
}
5261
}
5362
}
5463

55-
if (settings.ApiGatewayEmulatorMode is not null)
64+
if (settings.ApiGatewayEmulatorPort.HasValue)
5665
{
66+
if (settings.ApiGatewayEmulatorMode is null)
67+
{
68+
throw new ArgumentException("When --api-gateway-emulator-port is set the --api-gateway-mode must be set to configure the mode for the API Gateway emulator.");
69+
}
70+
5771
var apiGatewayEmulatorProcess =
5872
ApiGatewayEmulatorProcess.Startup(settings, cancellationTokenSource.Token);
5973
tasks.Add(apiGatewayEmulatorProcess.RunningTask);

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/Settings/RunCommandSettings.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

44
using Amazon.Lambda.TestTool.Models;
@@ -23,12 +23,11 @@ public sealed class RunCommandSettings : CommandSettings
2323
public string LambdaEmulatorHost { get; set; } = Constants.DefaultLambdaEmulatorHost;
2424

2525
/// <summary>
26-
/// The port number used for the test tool's web interface.
26+
/// The port number used for the test tool's web interface. If a port is specified the Lambda emulator will be started.
2727
/// </summary>
2828
[CommandOption("-p|--lambda-emulator-port <PORT>")]
2929
[Description("The port number used for the test tool's web interface.")]
30-
[DefaultValue(Constants.DefaultLambdaEmulatorPort)]
31-
public int LambdaEmulatorPort { get; set; } = Constants.DefaultLambdaEmulatorPort;
30+
public int? LambdaEmulatorPort { get; set; }
3231

3332
/// <summary>
3433
/// Disable auto launching the test tool's web interface in a browser.
@@ -58,17 +57,16 @@ public sealed class RunCommandSettings : CommandSettings
5857
/// and how API Gateway interprets the response from Lambda.
5958
/// The available modes are: Rest, HttpV1, HttpV2.
6059
/// </summary>
61-
[CommandOption("--api-gateway-emulator <MODE>")]
60+
[CommandOption("--api-gateway-emulator-mode <MODE>")]
6261
[Description(
6362
"The API Gateway Emulator Mode specifies the format of the event that API Gateway sends to a Lambda integration, and how API Gateway interprets the response from Lambda. " +
6463
"The available modes are: Rest, HttpV1, HttpV2.")]
6564
public ApiGatewayEmulatorMode? ApiGatewayEmulatorMode { get; set; }
6665

6766
/// <summary>
68-
/// The port number used for the test tool's API Gateway emulator.
67+
/// The port number used for the test tool's API Gateway emulator. If a port is specified the API Gateway emulator will be started. The --api-gateway-mode muse also be set when setting the API Gateway emulator port.
6968
/// </summary>
7069
[CommandOption("--api-gateway-emulator-port <PORT>")]
7170
[Description("The port number used for the test tool's API Gateway emulator.")]
72-
[DefaultValue(Constants.DefaultApiGatewayEmulatorPort)]
73-
public int? ApiGatewayEmulatorPort { get; set; } = Constants.DefaultApiGatewayEmulatorPort;
71+
public int? ApiGatewayEmulatorPort { get; set; }
7472
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Components/Pages/Home.razor.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
using Amazon.Lambda.TestTool.Services.IO;
99
using BlazorMonaco.Editor;
1010
using Microsoft.JSInterop;
11+
using Microsoft.AspNetCore.WebUtilities;
1112

1213
namespace Amazon.Lambda.TestTool.Components.Pages;
1314

1415
public partial class Home : ComponentBase, IDisposable
1516
{
17+
[Inject] public required NavigationManager NavManager { get; set; }
18+
[Inject] public required ILogger<Home> Logger { get; set; }
1619
[Inject] public required IHttpContextAccessor HttpContextAccessor { get; set; }
1720
[Inject] public required IRuntimeApiDataStoreManager DataStoreManager { get; set; }
1821
[Inject] public required IDirectoryManager DirectoryManager { get; set; }
@@ -87,10 +90,38 @@ public partial class Home : ComponentBase, IDisposable
8790

8891
protected override void OnInitialized()
8992
{
90-
DataStore = DataStoreManager.GetLambdaRuntimeDataStore(LambdaRuntimeApi.DefaultFunctionName);
93+
var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
94+
string initialFunction = string.Empty;
95+
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("function", out var queryValue))
96+
{
97+
initialFunction = queryValue.ToString();
98+
}
99+
100+
Logger.LogDebug("Query string variable for initial Lambda function set to: {initialFunction}", initialFunction);
101+
91102
_availableLambdaFunctions = DataStoreManager.GetListOfFunctionNames().ToList();
92103
if (_availableLambdaFunctions.Count > 0)
93-
SelectedFunctionName = _availableLambdaFunctions.First();
104+
{
105+
if (!string.IsNullOrEmpty(initialFunction) && _availableLambdaFunctions.Contains(initialFunction))
106+
{
107+
Logger.LogDebug("Query string function found in the list of available functions");
108+
SelectedFunctionName = initialFunction;
109+
}
110+
else
111+
{
112+
Logger.LogDebug("Query string function not found in the list of available functions");
113+
SelectedFunctionName = _availableLambdaFunctions.First();
114+
}
115+
116+
DataStore = DataStoreManager.GetLambdaRuntimeDataStore(SelectedFunctionName);
117+
}
118+
else
119+
{
120+
Logger.LogDebug("No functions currently registered with the test tool so default to the default function datastore");
121+
DataStore = DataStoreManager.GetLambdaRuntimeDataStore(LambdaRuntimeApi.DefaultFunctionName);
122+
}
123+
124+
94125
ThemeService.OnThemeChanged += HandleThemeChanged;
95126
DataStoreManager.StateChange += DataStoreManagerOnStateChange;
96127
SampleRequestManager = new SampleRequestManager(DirectoryManager.GetCurrentDirectory());

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Constants.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ public abstract class Constants
1515
/// </summary>
1616
public const string ToolName = "dotnet-lambda-test-tool";
1717

18-
/// <summary>
19-
/// The default port used by the Lambda Test Tool for the Lambda Runtime API and the Web Interface.
20-
/// </summary>
21-
public const int DefaultLambdaEmulatorPort = 5050;
22-
23-
/// <summary>
24-
/// The default port used by the API Gateway Emulator.
25-
/// </summary>
26-
public const int DefaultApiGatewayEmulatorPort = 5051;
27-
2818
/// <summary>
2919
/// The default hostname used for the Lambda Test Tool.
3020
/// </summary>

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayEmulatorProcessTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private void StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, TestCon
291291
""HttpMethod"": ""{config.HttpMethod}"",
292292
""Path"": ""/{config.RouteName}""
293293
}}");
294-
cancellationTokenSource.CancelAfter(5000);
294+
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(60));
295295
var settings = new RunCommandSettings { LambdaEmulatorPort = lambdaPort, NoLaunchWindow = true, ApiGatewayEmulatorMode = apiGatewayMode,ApiGatewayEmulatorPort = apiGatewayPort};
296296
var command = new RunCommand(_mockInteractiveService.Object, _mockEnvironmentManager.Object);
297297
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Commands/RunCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task ExecuteAsync_ApiGatewayEmulator_SuccessfulLaunch()
5050
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
5151
var cancellationSource = new CancellationTokenSource();
5252
cancellationSource.CancelAfter(5000);
53-
var settings = new RunCommandSettings { LambdaEmulatorPort = 9002, ApiGatewayEmulatorMode = ApiGatewayEmulatorMode.HttpV2, NoLaunchWindow = true};
53+
var settings = new RunCommandSettings { LambdaEmulatorPort = 9002, ApiGatewayEmulatorPort = 9003, ApiGatewayEmulatorMode = ApiGatewayEmulatorMode.HttpV2, NoLaunchWindow = true};
5454
var command = new RunCommand(_mockInteractiveService.Object, _mockEnvironmentManager.Object);
5555
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);
5656
var apiUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}/__lambda_test_tool_apigateway_health__";

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Commands/Settings/RunCommandSettingsTests.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

44
using Amazon.Lambda.TestTool.Commands.Settings;
@@ -18,26 +18,6 @@ public void DefaultHost_IsSetToConstantsDefaultHost()
1818
Assert.Equal(Constants.DefaultLambdaEmulatorHost, settings.LambdaEmulatorHost);
1919
}
2020

21-
[Fact]
22-
public void DefaultPort_IsSetToConstantsDefaultPort()
23-
{
24-
// Arrange
25-
var settings = new RunCommandSettings();
26-
27-
// Assert
28-
Assert.Equal(Constants.DefaultLambdaEmulatorPort, settings.LambdaEmulatorPort);
29-
}
30-
31-
[Fact]
32-
public void ApiGatewayEmulatorPort_IsSetToConstantsDefaultApiGatewayEmulatorPort()
33-
{
34-
// Arrange
35-
var settings = new RunCommandSettings();
36-
37-
// Assert
38-
Assert.Equal(Constants.DefaultApiGatewayEmulatorPort, settings.ApiGatewayEmulatorPort);
39-
}
40-
4121
[Fact]
4222
public void NoLaunchWindow_DefaultsToFalse()
4323
{

0 commit comments

Comments
 (0)