From 0e443c1d4638164f0ceac50ec903d008bf52d997 Mon Sep 17 00:00:00 2001 From: Ashish Dhingra Date: Thu, 20 Jul 2023 14:01:51 -0700 Subject: [PATCH] Added support for --disable-logs command line option in Lambda Test Tool in --no-ui mode. --- ...Amazon.Lambda.TestTool.BlazorTester.csproj | 2 +- ...Lambda.TestTool.BlazorTester31-pack.csproj | 2 +- ...Lambda.TestTool.BlazorTester50-pack.csproj | 2 +- ...Lambda.TestTool.BlazorTester60-pack.csproj | 2 +- ...Lambda.TestTool.BlazorTester70-pack.csproj | 2 +- .../CommandLineOptions.cs | 10 +++ .../Amazon.Lambda.TestTool/TestToolStartup.cs | 78 ++++++++++++------- .../src/Amazon.Lambda.TestTool/Utils.cs | 15 ++-- .../CommandLineParserTests.cs | 3 +- .../NoUiStartupTests.cs | 12 ++- 10 files changed, 87 insertions(+), 41 deletions(-) diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester.csproj b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester.csproj index 4d469269b..80f7dc19e 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester.csproj +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester.csproj @@ -6,7 +6,7 @@ Exe A tool to help debug and test your .NET Core AWS Lambda functions locally. Latest - 0.13.1 + 0.14.0 AWS .NET Lambda Test Tool Apache 2 AWS;Amazon;Lambda diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester31-pack.csproj b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester31-pack.csproj index 293b1cf77..668a146bc 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester31-pack.csproj +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester31-pack.csproj @@ -5,7 +5,7 @@ Exe A tool to help debug and test your .NET Core 3.1 AWS Lambda functions locally. - 0.13.1 + 0.14.0 AWS .NET Lambda Test Tool Apache 2 AWS;Amazon;Lambda diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester50-pack.csproj b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester50-pack.csproj index 0c30f2056..68394639d 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester50-pack.csproj +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester50-pack.csproj @@ -5,7 +5,7 @@ Exe A tool to help debug and test your .NET 5.0 AWS Lambda functions locally. - 0.13.1 + 0.14.0 AWS .NET Lambda Test Tool Apache 2 AWS;Amazon;Lambda diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester60-pack.csproj b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester60-pack.csproj index 39e7a1a32..20a056920 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester60-pack.csproj +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester60-pack.csproj @@ -5,7 +5,7 @@ Exe A tool to help debug and test your .NET 6.0 AWS Lambda functions locally. - 0.13.1 + 0.14.0 AWS .NET Lambda Test Tool Apache 2 AWS;Amazon;Lambda diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester70-pack.csproj b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester70-pack.csproj index e5460c8aa..e47a1c244 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester70-pack.csproj +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester70-pack.csproj @@ -5,7 +5,7 @@ Exe A tool to help debug and test your .NET 7.0 AWS Lambda functions locally. - 0.13.1 + 0.14.0 AWS .NET Lambda Test Tool Apache 2 AWS;Amazon;Lambda diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/CommandLineOptions.cs b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/CommandLineOptions.cs index dba2ab350..bbedd0ed8 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/CommandLineOptions.cs +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/CommandLineOptions.cs @@ -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(); @@ -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; } } @@ -172,6 +181,7 @@ public static void PrintUsage() Console.WriteLine("\t--pause-exit 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)."); } } diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs index 6080f7b95..4b748fe5a 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs @@ -9,6 +9,8 @@ namespace Amazon.Lambda.TestTool { public class TestToolStartup { + private static bool shouldDisableLogs; + public class RunConfiguration { public enum RunMode { Normal, Test }; @@ -33,9 +35,11 @@ public static void Startup(string productName, Action { try { - Utils.PrintToolTitle(productName); - var commandOptions = CommandLineOptions.Parse(args); + shouldDisableLogs = Utils.ShouldDisableLogs(commandOptions); + + if (!shouldDisableLogs) Utils.PrintToolTitle(productName); + if (commandOptions.ShowHelp) { CommandLineOptions.PrintUsage(); @@ -79,7 +83,7 @@ public static void Startup(string productName, Action 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) { @@ -127,7 +131,7 @@ public static void Startup(string productName, Action 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); @@ -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. @@ -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(); } } @@ -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)) { @@ -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 @@ -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, @@ -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; } @@ -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; } @@ -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; @@ -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."); + } } } @@ -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); } diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Utils.cs b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Utils.cs index 434c5bf63..ebc5528b8 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Utils.cs +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Utils.cs @@ -123,7 +123,7 @@ public static string FindLambdaProjectDirectory(string lambdaAssemblyDirectory) return FindLambdaProjectDirectory(Directory.GetParent(lambdaAssemblyDirectory)?.FullName); } - public static IList SearchForConfigFiles(string lambdaFunctionDirectory) + public static IList SearchForConfigFiles(string lambdaFunctionDirectory, bool disableLogging = false) { var configFiles = new List(); @@ -143,7 +143,7 @@ public static IList 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))) @@ -151,14 +151,14 @@ public static IList SearchForConfigFiles(string lambdaFunctionDirectory) 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}"); } } @@ -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; } /// diff --git a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/CommandLineParserTests.cs b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/CommandLineParserTests.cs index cc48a11b5..1b0593e5b 100644 --- a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/CommandLineParserTests.cs +++ b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/CommandLineParserTests.cs @@ -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); @@ -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); } diff --git a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/NoUiStartupTests.cs b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/NoUiStartupTests.cs index 186082f0a..17c9fc651 100644 --- a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/NoUiStartupTests.cs +++ b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/NoUiStartupTests.cs @@ -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() - }; + }; } } }