Skip to content

Aspire tweaks for test tool #1948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,44 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
{
EvaluateEnvironmentVariables(settings);

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

var testToolProcess = TestToolProcess.Startup(settings, cancellationTokenSource.Token);
tasks.Add(testToolProcess.RunningTask);
var tasks = new List<Task>();

if (!settings.NoLaunchWindow)
if (settings.LambdaEmulatorPort.HasValue)
{
try
var testToolProcess = TestToolProcess.Startup(settings, cancellationTokenSource.Token);
tasks.Add(testToolProcess.RunningTask);

if (!settings.NoLaunchWindow)
{
var info = new ProcessStartInfo
try
{
UseShellExecute = true,
FileName = testToolProcess.ServiceUrl
};
Process.Start(info);
}
catch (Exception e)
{
toolInteractiveService.WriteErrorLine($"Error launching browser: {e.Message}");
var info = new ProcessStartInfo
{
UseShellExecute = true,
FileName = testToolProcess.ServiceUrl
};
Process.Start(info);
}
catch (Exception e)
{
toolInteractiveService.WriteErrorLine($"Error launching browser: {e.Message}");
}
}
}

if (settings.ApiGatewayEmulatorMode is not null)
if (settings.ApiGatewayEmulatorPort.HasValue)
{
if (settings.ApiGatewayEmulatorMode is null)
{
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.");
}

var apiGatewayEmulatorProcess =
ApiGatewayEmulatorProcess.Startup(settings, cancellationTokenSource.Token);
tasks.Add(apiGatewayEmulatorProcess.RunningTask);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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

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

/// <summary>
/// Disable auto launching the test tool's web interface in a browser.
Expand Down Expand Up @@ -58,17 +57,16 @@ public sealed class RunCommandSettings : CommandSettings
/// and how API Gateway interprets the response from Lambda.
/// The available modes are: Rest, HttpV1, HttpV2.
/// </summary>
[CommandOption("--api-gateway-emulator <MODE>")]
[CommandOption("--api-gateway-emulator-mode <MODE>")]
[Description(
"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. " +
"The available modes are: Rest, HttpV1, HttpV2.")]
public ApiGatewayEmulatorMode? ApiGatewayEmulatorMode { get; set; }

/// <summary>
/// The port number used for the test tool's API Gateway emulator.
/// 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.
/// </summary>
[CommandOption("--api-gateway-emulator-port <PORT>")]
[Description("The port number used for the test tool's API Gateway emulator.")]
[DefaultValue(Constants.DefaultApiGatewayEmulatorPort)]
public int? ApiGatewayEmulatorPort { get; set; } = Constants.DefaultApiGatewayEmulatorPort;
public int? ApiGatewayEmulatorPort { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
using Amazon.Lambda.TestTool.Services.IO;
using BlazorMonaco.Editor;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.WebUtilities;

namespace Amazon.Lambda.TestTool.Components.Pages;

public partial class Home : ComponentBase, IDisposable
{
[Inject] public required NavigationManager NavManager { get; set; }
[Inject] public required ILogger<Home> Logger { get; set; }
[Inject] public required IHttpContextAccessor HttpContextAccessor { get; set; }
[Inject] public required IRuntimeApiDataStoreManager DataStoreManager { get; set; }
[Inject] public required IDirectoryManager DirectoryManager { get; set; }
Expand Down Expand Up @@ -87,10 +90,38 @@ public partial class Home : ComponentBase, IDisposable

protected override void OnInitialized()
{
DataStore = DataStoreManager.GetLambdaRuntimeDataStore(LambdaRuntimeApi.DefaultFunctionName);
var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
string initialFunction = string.Empty;
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("function", out var queryValue))
{
initialFunction = queryValue.ToString();
}

Logger.LogDebug("Query string variable for initial Lambda function set to: {initialFunction}", initialFunction);

_availableLambdaFunctions = DataStoreManager.GetListOfFunctionNames().ToList();
if (_availableLambdaFunctions.Count > 0)
SelectedFunctionName = _availableLambdaFunctions.First();
{
if (!string.IsNullOrEmpty(initialFunction) && _availableLambdaFunctions.Contains(initialFunction))
{
Logger.LogDebug("Query string function found in the list of available functions");
SelectedFunctionName = initialFunction;
}
else
{
Logger.LogDebug("Query string function not found in the list of available functions");
SelectedFunctionName = _availableLambdaFunctions.First();
}

DataStore = DataStoreManager.GetLambdaRuntimeDataStore(SelectedFunctionName);
}
else
{
Logger.LogDebug("No functions currently registered with the test tool so default to the default function datastore");
DataStore = DataStoreManager.GetLambdaRuntimeDataStore(LambdaRuntimeApi.DefaultFunctionName);
}


ThemeService.OnThemeChanged += HandleThemeChanged;
DataStoreManager.StateChange += DataStoreManagerOnStateChange;
SampleRequestManager = new SampleRequestManager(DirectoryManager.GetCurrentDirectory());
Expand Down
10 changes: 0 additions & 10 deletions Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ public abstract class Constants
/// </summary>
public const string ToolName = "dotnet-lambda-test-tool";

/// <summary>
/// The default port used by the Lambda Test Tool for the Lambda Runtime API and the Web Interface.
/// </summary>
public const int DefaultLambdaEmulatorPort = 5050;

/// <summary>
/// The default port used by the API Gateway Emulator.
/// </summary>
public const int DefaultApiGatewayEmulatorPort = 5051;

/// <summary>
/// The default hostname used for the Lambda Test Tool.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private void StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, TestCon
""HttpMethod"": ""{config.HttpMethod}"",
""Path"": ""/{config.RouteName}""
}}");
cancellationTokenSource.CancelAfter(5000);
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(60));
var settings = new RunCommandSettings { LambdaEmulatorPort = lambdaPort, NoLaunchWindow = true, ApiGatewayEmulatorMode = apiGatewayMode,ApiGatewayEmulatorPort = apiGatewayPort};
var command = new RunCommand(_mockInteractiveService.Object, _mockEnvironmentManager.Object);
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task ExecuteAsync_ApiGatewayEmulator_SuccessfulLaunch()
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
var cancellationSource = new CancellationTokenSource();
cancellationSource.CancelAfter(5000);
var settings = new RunCommandSettings { LambdaEmulatorPort = 9002, ApiGatewayEmulatorMode = ApiGatewayEmulatorMode.HttpV2, NoLaunchWindow = true};
var settings = new RunCommandSettings { LambdaEmulatorPort = 9002, ApiGatewayEmulatorPort = 9003, ApiGatewayEmulatorMode = ApiGatewayEmulatorMode.HttpV2, NoLaunchWindow = true};
var command = new RunCommand(_mockInteractiveService.Object, _mockEnvironmentManager.Object);
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);
var apiUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}/__lambda_test_tool_apigateway_health__";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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

[Fact]
public void DefaultPort_IsSetToConstantsDefaultPort()
{
// Arrange
var settings = new RunCommandSettings();

// Assert
Assert.Equal(Constants.DefaultLambdaEmulatorPort, settings.LambdaEmulatorPort);
}

[Fact]
public void ApiGatewayEmulatorPort_IsSetToConstantsDefaultApiGatewayEmulatorPort()
{
// Arrange
var settings = new RunCommandSettings();

// Assert
Assert.Equal(Constants.DefaultApiGatewayEmulatorPort, settings.ApiGatewayEmulatorPort);
}

[Fact]
public void NoLaunchWindow_DefaultsToFalse()
{
Expand Down
Loading