Skip to content

Commit

Permalink
Added support for --disable-logs command line option in Lambda Test T…
Browse files Browse the repository at this point in the history
…ool in --no-ui mode.
  • Loading branch information
ashishdhingra committed Jul 28, 2023
1 parent 2e79888 commit 0e443c1
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<OutputType>Exe</OutputType>
<Description>A tool to help debug and test your .NET Core AWS Lambda functions locally.</Description>
<LangVersion>Latest</LangVersion>
<VersionPrefix>0.13.1</VersionPrefix>
<VersionPrefix>0.14.0</VersionPrefix>
<Product>AWS .NET Lambda Test Tool</Product>
<Copyright>Apache 2</Copyright>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<Description>A tool to help debug and test your .NET Core 3.1 AWS Lambda functions locally.</Description>
<VersionPrefix>0.13.1</VersionPrefix>
<VersionPrefix>0.14.0</VersionPrefix>
<Product>AWS .NET Lambda Test Tool</Product>
<Copyright>Apache 2</Copyright>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<Description>A tool to help debug and test your .NET 5.0 AWS Lambda functions locally.</Description>
<VersionPrefix>0.13.1</VersionPrefix>
<VersionPrefix>0.14.0</VersionPrefix>
<Product>AWS .NET Lambda Test Tool</Product>
<Copyright>Apache 2</Copyright>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<Description>A tool to help debug and test your .NET 6.0 AWS Lambda functions locally.</Description>
<VersionPrefix>0.13.1</VersionPrefix>
<VersionPrefix>0.14.0</VersionPrefix>
<Product>AWS .NET Lambda Test Tool</Product>
<Copyright>Apache 2</Copyright>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<Description>A tool to help debug and test your .NET 7.0 AWS Lambda functions locally.</Description>
<VersionPrefix>0.13.1</VersionPrefix>
<VersionPrefix>0.14.0</VersionPrefix>
<Product>AWS .NET Lambda Test Tool</Product>
<Copyright>Apache 2</Copyright>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class CommandLineOptions

public bool PauseExit { get; set; } = true;

public bool DisableLogs { get; set; } = false;

public static CommandLineOptions Parse(string[] args)
{
var options = new CommandLineOptions();
Expand Down Expand Up @@ -97,6 +99,13 @@ public static CommandLineOptions Parse(string[] args)
i++;
}
break;
case "--disable-logs":
options.DisableLogs = GetNextBoolValue(i, out skipAhead);
if (skipAhead)
{
i++;
}
break;
}
}

Expand Down Expand Up @@ -172,6 +181,7 @@ public static void PrintUsage()
Console.WriteLine("\t--pause-exit <true or false> If set to true the test tool will pause waiting for a key input before exiting. The is useful");
Console.WriteLine("\t when executing from an IDE so you can avoid having the output window immediately disappear after");
Console.WriteLine("\t executing the Lambda code. The default value is true.");
Console.WriteLine("\t--disable-logs Logs response only or any exceptions (switch is valid when using --no-ui).");
}
}

Expand Down
78 changes: 49 additions & 29 deletions Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Amazon.Lambda.TestTool
{
public class TestToolStartup
{
private static bool shouldDisableLogs;

public class RunConfiguration
{
public enum RunMode { Normal, Test };
Expand All @@ -33,9 +35,11 @@ public static void Startup(string productName, Action<LocalLambdaOptions, bool>
{
try
{
Utils.PrintToolTitle(productName);

var commandOptions = CommandLineOptions.Parse(args);
shouldDisableLogs = Utils.ShouldDisableLogs(commandOptions);

if (!shouldDisableLogs) Utils.PrintToolTitle(productName);

if (commandOptions.ShowHelp)
{
CommandLineOptions.PrintUsage();
Expand Down Expand Up @@ -79,7 +83,7 @@ public static void Startup(string productName, Action<LocalLambdaOptions, bool>
lambdaAssemblyDirectory = Utils.SearchLatestCompilationDirectory(lambdaAssemblyDirectory);

localLambdaOptions.LambdaRuntime = LocalLambdaRuntime.Initialize(lambdaAssemblyDirectory);
runConfiguration.OutputWriter.WriteLine($"Loaded local Lambda runtime from project output {lambdaAssemblyDirectory}");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"Loaded local Lambda runtime from project output {lambdaAssemblyDirectory}");

if (commandOptions.NoUI)
{
Expand Down Expand Up @@ -127,7 +131,7 @@ public static void Startup(string productName, Action<LocalLambdaOptions, bool>

public static void ExecuteWithNoUi(LocalLambdaOptions localLambdaOptions, CommandLineOptions commandOptions, string lambdaAssemblyDirectory, RunConfiguration runConfiguration)
{
runConfiguration.OutputWriter.WriteLine("Executing Lambda function without web interface");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine("Executing Lambda function without web interface");
var lambdaProjectDirectory = Utils.FindLambdaProjectDirectory(lambdaAssemblyDirectory);

string configFile = DetermineConfigFile(commandOptions, lambdaAssemblyDirectory: lambdaAssemblyDirectory, lambdaProjectDirectory: lambdaProjectDirectory);
Expand All @@ -141,27 +145,27 @@ public static void ExecuteWithNoUi(LocalLambdaOptions localLambdaOptions, Comman
{
if (new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain().TryGetProfile(awsProfile, out _))
{
runConfiguration.OutputWriter.WriteLine($"... Setting AWS_PROFILE environment variable to {awsProfile}.");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Setting AWS_PROFILE environment variable to {awsProfile}.");
}
else
{
runConfiguration.OutputWriter.WriteLine($"... Warning: Profile {awsProfile} not found in the aws credential store.");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Warning: Profile {awsProfile} not found in the aws credential store.");
awsProfile = null;
}
}
else
{
runConfiguration.OutputWriter.WriteLine("... No profile choosen for AWS credentials. The --profile switch can be used to configure an AWS profile.");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine("... No profile choosen for AWS credentials. The --profile switch can be used to configure an AWS profile.");
}

var awsRegion = commandOptions.AWSRegion ?? configInfo.AWSRegion;
if (!string.IsNullOrEmpty(awsRegion))
{
runConfiguration.OutputWriter.WriteLine($"... Setting AWS_REGION environment variable to {awsRegion}.");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Setting AWS_REGION environment variable to {awsRegion}.");
}
else
{
runConfiguration.OutputWriter.WriteLine("... No default AWS region configured. The --region switch can be used to configure an AWS Region.");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine("... No default AWS region configured. The --region switch can be used to configure an AWS Region.");
}

// Create the execution request that will be sent into the LocalLambdaRuntime.
Expand All @@ -178,7 +182,7 @@ public static void ExecuteWithNoUi(LocalLambdaOptions localLambdaOptions, Comman

if (runConfiguration.Mode == RunConfiguration.RunMode.Normal && commandOptions.PauseExit)
{
Console.WriteLine("Press any key to exit");
if (!shouldDisableLogs) Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
Expand All @@ -188,7 +192,7 @@ private static string DetermineConfigFile(CommandLineOptions commandOptions, str
string configFile = null;
if (string.IsNullOrEmpty(commandOptions.ConfigFile))
{
configFile = Utils.SearchForConfigFiles(lambdaAssemblyDirectory).FirstOrDefault(x => string.Equals(Utils.DEFAULT_CONFIG_FILE, Path.GetFileName(x), StringComparison.OrdinalIgnoreCase));
configFile = Utils.SearchForConfigFiles(lambdaAssemblyDirectory, shouldDisableLogs).FirstOrDefault(x => string.Equals(Utils.DEFAULT_CONFIG_FILE, Path.GetFileName(x), StringComparison.OrdinalIgnoreCase));
}
else if (Path.IsPathRooted(commandOptions.ConfigFile))
{
Expand All @@ -211,7 +215,7 @@ private static LambdaConfigInfo LoadLambdaConfigInfo(string configFile, CommandL
LambdaConfigInfo configInfo;
if (configFile != null)
{
runConfiguration.OutputWriter.WriteLine($"... Using config file {configFile}");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using config file {configFile}");
configInfo = LambdaDefaultsConfigFileParser.LoadFromFile(configFile);
}
else
Expand Down Expand Up @@ -254,7 +258,7 @@ private static LambdaFunction LoadLambdaFunction(LambdaConfigInfo configInfo, Lo
{
// The user has explicitly set a function handler value that is not in the config file or CloudFormation template.
// To support users testing add hoc methods create a temporary config object using explicit function handler value.
runConfiguration.OutputWriter.WriteLine($"... Info: function handler {functionHandler} is not defined in config file.");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Info: function handler {functionHandler} is not defined in config file.");
var temporaryConfigInfo = LambdaDefaultsConfigFileParser.LoadFromFile(new LambdaConfigFile
{
FunctionHandler = functionHandler,
Expand All @@ -267,7 +271,7 @@ private static LambdaFunction LoadLambdaFunction(LambdaConfigInfo configInfo, Lo
lambdaFunction = localLambdaOptions.LoadLambdaFuntion(configInfo, functionHandler);
}

runConfiguration.OutputWriter.WriteLine($"... Using function handler {functionHandler}");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using function handler {functionHandler}");
return lambdaFunction;
}

Expand All @@ -280,7 +284,7 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co
{
if (Path.IsPathFullyQualified(payload) && File.Exists(payload))
{
runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {payload}");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {payload}");
payload = File.ReadAllText(payload);
payloadFileFound = true;
}
Expand All @@ -302,7 +306,7 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co
{
if (File.Exists(possiblePath))
{
runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {Path.GetFullPath(possiblePath)}");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {Path.GetFullPath(possiblePath)}");
payload = File.ReadAllText(possiblePath);
payloadFileFound = true;
break;
Expand All @@ -313,13 +317,16 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co

if (!payloadFileFound)
{
if (!string.IsNullOrEmpty(payload))
if (!shouldDisableLogs)
{
runConfiguration.OutputWriter.WriteLine($"... Using payload with the value {payload}");
}
else
{
runConfiguration.OutputWriter.WriteLine("... No payload configured. If a payload is required set the --payload switch to a file path or a JSON document.");
if (!string.IsNullOrEmpty(payload))
{
runConfiguration.OutputWriter.WriteLine($"... Using payload with the value {payload}");
}
else
{
runConfiguration.OutputWriter.WriteLine("... No payload configured. If a payload is required set the --payload switch to a file path or a JSON document.");
}
}
}

Expand All @@ -331,19 +338,32 @@ private static void ExecuteRequest(ExecutionRequest request, LocalLambdaOptions
try
{
var response = localLambdaOptions.LambdaRuntime.ExecuteLambdaFunctionAsync(request).GetAwaiter().GetResult();

runConfiguration.OutputWriter.WriteLine("Captured Log information:");
runConfiguration.OutputWriter.WriteLine(response.Logs);
if (!shouldDisableLogs)
{
runConfiguration.OutputWriter.WriteLine("Captured Log information:");
runConfiguration.OutputWriter.WriteLine(response.Logs);
}

if (response.IsSuccess)
{
runConfiguration.OutputWriter.WriteLine("Request executed successfully");
runConfiguration.OutputWriter.WriteLine("Response:");
runConfiguration.OutputWriter.WriteLine(response.Response);
if (!shouldDisableLogs)
{
runConfiguration.OutputWriter.WriteLine("Request executed successfully");
runConfiguration.OutputWriter.WriteLine("Response:");
}

if (!shouldDisableLogs)
{
runConfiguration.OutputWriter.WriteLine(response.Response);
}
else
{
runConfiguration.OutputWriter.Write(response.Response);
}
}
else
{
runConfiguration.OutputWriter.WriteLine("Request failed to execute");
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine("Request failed to execute");
runConfiguration.OutputWriter.WriteLine($"Error:");
runConfiguration.OutputWriter.WriteLine(response.Error);
}
Expand Down
15 changes: 10 additions & 5 deletions Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static string FindLambdaProjectDirectory(string lambdaAssemblyDirectory)
return FindLambdaProjectDirectory(Directory.GetParent(lambdaAssemblyDirectory)?.FullName);
}

public static IList<string> SearchForConfigFiles(string lambdaFunctionDirectory)
public static IList<string> SearchForConfigFiles(string lambdaFunctionDirectory, bool disableLogging = false)
{
var configFiles = new List<string>();

Expand All @@ -143,22 +143,22 @@ public static IList<string> SearchForConfigFiles(string lambdaFunctionDirectory)

if (!string.IsNullOrEmpty(configFile.DetermineHandler()))
{
Console.WriteLine($"Found Lambda config file {file}");
if (!disableLogging) Console.WriteLine($"Found Lambda config file {file}");
configFiles.Add(file);
}
else if (!string.IsNullOrEmpty(configFile.Template) && File.Exists(Path.Combine(lambdaFunctionDirectory, configFile.Template)))
{
var config = LambdaDefaultsConfigFileParser.LoadFromFile(configFile);
if (config.FunctionInfos?.Count > 0)
{
Console.WriteLine($"Found Lambda config file {file}");
if (!disableLogging) Console.WriteLine($"Found Lambda config file {file}");
configFiles.Add(file);
}
}
}
catch
{
Console.WriteLine($"Error parsing JSON file: {file}");
if (!disableLogging) Console.WriteLine($"Error parsing JSON file: {file}");
}
}

Expand Down Expand Up @@ -240,7 +240,12 @@ public static string SearchLatestCompilationDirectory(string debugDirectory)
if (depsFile.Count == 0)
return debugDirectory;

return depsFile[0].Directory.FullName;
return depsFile[0].Directory.FullName;
}

public static bool ShouldDisableLogs(CommandLineOptions commandOptions)
{
return commandOptions != null && commandOptions.DisableLogs && commandOptions.NoUI;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void AllValuesGetSet()
{
var options = CommandLineOptions.Parse(new string[] {"--help", "--host", "example.com", "--port", "1111", "--no-launch-window",
"--path", "./foo", "--profile", "test", "--region", "special-region",
"--no-ui", "--config-file", "test-config.json", "--payload", "myfile.json", "--pause-exit", "false" });
"--no-ui", "--config-file", "test-config.json", "--payload", "myfile.json", "--pause-exit", "false", "--disable-logs" });

Assert.True(options.ShowHelp);
Assert.Equal("example.com", options.Host);
Expand All @@ -22,6 +22,7 @@ public void AllValuesGetSet()
Assert.Equal("test-config.json", options.ConfigFile);
Assert.Equal("myfile.json", options.Payload);
Assert.False(options.PauseExit);
Assert.True(options.DisableLogs);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,23 @@ public void NoProfileAndRegion()
Assert.Contains("No default AWS region configured. The --region switch can be used to configure an AWS Region.", runConfiguration.OutputWriter.ToString());
}

[Fact]
public void DirectFunctionCallFromConfigWithDisableLogs()
{
var runConfiguration = CreateRunConfiguration();
var buildPath = TestUtils.GetLambdaFunctionBuildPath("ToUpperFunc");

TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "\"hello WORLD\"", "--disable-logs" }, runConfiguration);
Assert.Equal("\"HELLO WORLD\"", runConfiguration.OutputWriter.ToString());
}

private TestToolStartup.RunConfiguration CreateRunConfiguration()
{
return new TestToolStartup.RunConfiguration
{
Mode = TestToolStartup.RunConfiguration.RunMode.Test,
OutputWriter = new StringWriter()
};
};
}
}
}

0 comments on commit 0e443c1

Please sign in to comment.