Skip to content

Commit 0e443c1

Browse files
committed
Added support for --disable-logs command line option in Lambda Test Tool in --no-ui mode.
1 parent 2e79888 commit 0e443c1

10 files changed

+87
-41
lines changed

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<OutputType>Exe</OutputType>
77
<Description>A tool to help debug and test your .NET Core AWS Lambda functions locally.</Description>
88
<LangVersion>Latest</LangVersion>
9-
<VersionPrefix>0.13.1</VersionPrefix>
9+
<VersionPrefix>0.14.0</VersionPrefix>
1010
<Product>AWS .NET Lambda Test Tool</Product>
1111
<Copyright>Apache 2</Copyright>
1212
<PackageTags>AWS;Amazon;Lambda</PackageTags>

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester31-pack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
77
<Description>A tool to help debug and test your .NET Core 3.1 AWS Lambda functions locally.</Description>
8-
<VersionPrefix>0.13.1</VersionPrefix>
8+
<VersionPrefix>0.14.0</VersionPrefix>
99
<Product>AWS .NET Lambda Test Tool</Product>
1010
<Copyright>Apache 2</Copyright>
1111
<PackageTags>AWS;Amazon;Lambda</PackageTags>

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester50-pack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
77
<Description>A tool to help debug and test your .NET 5.0 AWS Lambda functions locally.</Description>
8-
<VersionPrefix>0.13.1</VersionPrefix>
8+
<VersionPrefix>0.14.0</VersionPrefix>
99
<Product>AWS .NET Lambda Test Tool</Product>
1010
<Copyright>Apache 2</Copyright>
1111
<PackageTags>AWS;Amazon;Lambda</PackageTags>

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester60-pack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
77
<Description>A tool to help debug and test your .NET 6.0 AWS Lambda functions locally.</Description>
8-
<VersionPrefix>0.13.1</VersionPrefix>
8+
<VersionPrefix>0.14.0</VersionPrefix>
99
<Product>AWS .NET Lambda Test Tool</Product>
1010
<Copyright>Apache 2</Copyright>
1111
<PackageTags>AWS;Amazon;Lambda</PackageTags>

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool.BlazorTester/Amazon.Lambda.TestTool.BlazorTester70-pack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
77
<Description>A tool to help debug and test your .NET 7.0 AWS Lambda functions locally.</Description>
8-
<VersionPrefix>0.13.1</VersionPrefix>
8+
<VersionPrefix>0.14.0</VersionPrefix>
99
<Product>AWS .NET Lambda Test Tool</Product>
1010
<Copyright>Apache 2</Copyright>
1111
<PackageTags>AWS;Amazon;Lambda</PackageTags>

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/CommandLineOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class CommandLineOptions
2828

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

31+
public bool DisableLogs { get; set; } = false;
32+
3133
public static CommandLineOptions Parse(string[] args)
3234
{
3335
var options = new CommandLineOptions();
@@ -97,6 +99,13 @@ public static CommandLineOptions Parse(string[] args)
9799
i++;
98100
}
99101
break;
102+
case "--disable-logs":
103+
options.DisableLogs = GetNextBoolValue(i, out skipAhead);
104+
if (skipAhead)
105+
{
106+
i++;
107+
}
108+
break;
100109
}
101110
}
102111

@@ -172,6 +181,7 @@ public static void PrintUsage()
172181
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");
173182
Console.WriteLine("\t when executing from an IDE so you can avoid having the output window immediately disappear after");
174183
Console.WriteLine("\t executing the Lambda code. The default value is true.");
184+
Console.WriteLine("\t--disable-logs Logs response only or any exceptions (switch is valid when using --no-ui).");
175185
}
176186
}
177187

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Amazon.Lambda.TestTool
99
{
1010
public class TestToolStartup
1111
{
12+
private static bool shouldDisableLogs;
13+
1214
public class RunConfiguration
1315
{
1416
public enum RunMode { Normal, Test };
@@ -33,9 +35,11 @@ public static void Startup(string productName, Action<LocalLambdaOptions, bool>
3335
{
3436
try
3537
{
36-
Utils.PrintToolTitle(productName);
37-
3838
var commandOptions = CommandLineOptions.Parse(args);
39+
shouldDisableLogs = Utils.ShouldDisableLogs(commandOptions);
40+
41+
if (!shouldDisableLogs) Utils.PrintToolTitle(productName);
42+
3943
if (commandOptions.ShowHelp)
4044
{
4145
CommandLineOptions.PrintUsage();
@@ -79,7 +83,7 @@ public static void Startup(string productName, Action<LocalLambdaOptions, bool>
7983
lambdaAssemblyDirectory = Utils.SearchLatestCompilationDirectory(lambdaAssemblyDirectory);
8084

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

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

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

133137
string configFile = DetermineConfigFile(commandOptions, lambdaAssemblyDirectory: lambdaAssemblyDirectory, lambdaProjectDirectory: lambdaProjectDirectory);
@@ -141,27 +145,27 @@ public static void ExecuteWithNoUi(LocalLambdaOptions localLambdaOptions, Comman
141145
{
142146
if (new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain().TryGetProfile(awsProfile, out _))
143147
{
144-
runConfiguration.OutputWriter.WriteLine($"... Setting AWS_PROFILE environment variable to {awsProfile}.");
148+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Setting AWS_PROFILE environment variable to {awsProfile}.");
145149
}
146150
else
147151
{
148-
runConfiguration.OutputWriter.WriteLine($"... Warning: Profile {awsProfile} not found in the aws credential store.");
152+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Warning: Profile {awsProfile} not found in the aws credential store.");
149153
awsProfile = null;
150154
}
151155
}
152156
else
153157
{
154-
runConfiguration.OutputWriter.WriteLine("... No profile choosen for AWS credentials. The --profile switch can be used to configure an AWS profile.");
158+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine("... No profile choosen for AWS credentials. The --profile switch can be used to configure an AWS profile.");
155159
}
156160

157161
var awsRegion = commandOptions.AWSRegion ?? configInfo.AWSRegion;
158162
if (!string.IsNullOrEmpty(awsRegion))
159163
{
160-
runConfiguration.OutputWriter.WriteLine($"... Setting AWS_REGION environment variable to {awsRegion}.");
164+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Setting AWS_REGION environment variable to {awsRegion}.");
161165
}
162166
else
163167
{
164-
runConfiguration.OutputWriter.WriteLine("... No default AWS region configured. The --region switch can be used to configure an AWS Region.");
168+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine("... No default AWS region configured. The --region switch can be used to configure an AWS Region.");
165169
}
166170

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

179183
if (runConfiguration.Mode == RunConfiguration.RunMode.Normal && commandOptions.PauseExit)
180184
{
181-
Console.WriteLine("Press any key to exit");
185+
if (!shouldDisableLogs) Console.WriteLine("Press any key to exit");
182186
Console.ReadKey();
183187
}
184188
}
@@ -188,7 +192,7 @@ private static string DetermineConfigFile(CommandLineOptions commandOptions, str
188192
string configFile = null;
189193
if (string.IsNullOrEmpty(commandOptions.ConfigFile))
190194
{
191-
configFile = Utils.SearchForConfigFiles(lambdaAssemblyDirectory).FirstOrDefault(x => string.Equals(Utils.DEFAULT_CONFIG_FILE, Path.GetFileName(x), StringComparison.OrdinalIgnoreCase));
195+
configFile = Utils.SearchForConfigFiles(lambdaAssemblyDirectory, shouldDisableLogs).FirstOrDefault(x => string.Equals(Utils.DEFAULT_CONFIG_FILE, Path.GetFileName(x), StringComparison.OrdinalIgnoreCase));
192196
}
193197
else if (Path.IsPathRooted(commandOptions.ConfigFile))
194198
{
@@ -211,7 +215,7 @@ private static LambdaConfigInfo LoadLambdaConfigInfo(string configFile, CommandL
211215
LambdaConfigInfo configInfo;
212216
if (configFile != null)
213217
{
214-
runConfiguration.OutputWriter.WriteLine($"... Using config file {configFile}");
218+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using config file {configFile}");
215219
configInfo = LambdaDefaultsConfigFileParser.LoadFromFile(configFile);
216220
}
217221
else
@@ -254,7 +258,7 @@ private static LambdaFunction LoadLambdaFunction(LambdaConfigInfo configInfo, Lo
254258
{
255259
// The user has explicitly set a function handler value that is not in the config file or CloudFormation template.
256260
// To support users testing add hoc methods create a temporary config object using explicit function handler value.
257-
runConfiguration.OutputWriter.WriteLine($"... Info: function handler {functionHandler} is not defined in config file.");
261+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Info: function handler {functionHandler} is not defined in config file.");
258262
var temporaryConfigInfo = LambdaDefaultsConfigFileParser.LoadFromFile(new LambdaConfigFile
259263
{
260264
FunctionHandler = functionHandler,
@@ -267,7 +271,7 @@ private static LambdaFunction LoadLambdaFunction(LambdaConfigInfo configInfo, Lo
267271
lambdaFunction = localLambdaOptions.LoadLambdaFuntion(configInfo, functionHandler);
268272
}
269273

270-
runConfiguration.OutputWriter.WriteLine($"... Using function handler {functionHandler}");
274+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using function handler {functionHandler}");
271275
return lambdaFunction;
272276
}
273277

@@ -280,7 +284,7 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co
280284
{
281285
if (Path.IsPathFullyQualified(payload) && File.Exists(payload))
282286
{
283-
runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {payload}");
287+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {payload}");
284288
payload = File.ReadAllText(payload);
285289
payloadFileFound = true;
286290
}
@@ -302,7 +306,7 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co
302306
{
303307
if (File.Exists(possiblePath))
304308
{
305-
runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {Path.GetFullPath(possiblePath)}");
309+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {Path.GetFullPath(possiblePath)}");
306310
payload = File.ReadAllText(possiblePath);
307311
payloadFileFound = true;
308312
break;
@@ -313,13 +317,16 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co
313317

314318
if (!payloadFileFound)
315319
{
316-
if (!string.IsNullOrEmpty(payload))
320+
if (!shouldDisableLogs)
317321
{
318-
runConfiguration.OutputWriter.WriteLine($"... Using payload with the value {payload}");
319-
}
320-
else
321-
{
322-
runConfiguration.OutputWriter.WriteLine("... No payload configured. If a payload is required set the --payload switch to a file path or a JSON document.");
322+
if (!string.IsNullOrEmpty(payload))
323+
{
324+
runConfiguration.OutputWriter.WriteLine($"... Using payload with the value {payload}");
325+
}
326+
else
327+
{
328+
runConfiguration.OutputWriter.WriteLine("... No payload configured. If a payload is required set the --payload switch to a file path or a JSON document.");
329+
}
323330
}
324331
}
325332

@@ -331,19 +338,32 @@ private static void ExecuteRequest(ExecutionRequest request, LocalLambdaOptions
331338
try
332339
{
333340
var response = localLambdaOptions.LambdaRuntime.ExecuteLambdaFunctionAsync(request).GetAwaiter().GetResult();
334-
335-
runConfiguration.OutputWriter.WriteLine("Captured Log information:");
336-
runConfiguration.OutputWriter.WriteLine(response.Logs);
341+
if (!shouldDisableLogs)
342+
{
343+
runConfiguration.OutputWriter.WriteLine("Captured Log information:");
344+
runConfiguration.OutputWriter.WriteLine(response.Logs);
345+
}
337346

338347
if (response.IsSuccess)
339348
{
340-
runConfiguration.OutputWriter.WriteLine("Request executed successfully");
341-
runConfiguration.OutputWriter.WriteLine("Response:");
342-
runConfiguration.OutputWriter.WriteLine(response.Response);
349+
if (!shouldDisableLogs)
350+
{
351+
runConfiguration.OutputWriter.WriteLine("Request executed successfully");
352+
runConfiguration.OutputWriter.WriteLine("Response:");
353+
}
354+
355+
if (!shouldDisableLogs)
356+
{
357+
runConfiguration.OutputWriter.WriteLine(response.Response);
358+
}
359+
else
360+
{
361+
runConfiguration.OutputWriter.Write(response.Response);
362+
}
343363
}
344364
else
345365
{
346-
runConfiguration.OutputWriter.WriteLine("Request failed to execute");
366+
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine("Request failed to execute");
347367
runConfiguration.OutputWriter.WriteLine($"Error:");
348368
runConfiguration.OutputWriter.WriteLine(response.Error);
349369
}

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Utils.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static string FindLambdaProjectDirectory(string lambdaAssemblyDirectory)
123123
return FindLambdaProjectDirectory(Directory.GetParent(lambdaAssemblyDirectory)?.FullName);
124124
}
125125

126-
public static IList<string> SearchForConfigFiles(string lambdaFunctionDirectory)
126+
public static IList<string> SearchForConfigFiles(string lambdaFunctionDirectory, bool disableLogging = false)
127127
{
128128
var configFiles = new List<string>();
129129

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

144144
if (!string.IsNullOrEmpty(configFile.DetermineHandler()))
145145
{
146-
Console.WriteLine($"Found Lambda config file {file}");
146+
if (!disableLogging) Console.WriteLine($"Found Lambda config file {file}");
147147
configFiles.Add(file);
148148
}
149149
else if (!string.IsNullOrEmpty(configFile.Template) && File.Exists(Path.Combine(lambdaFunctionDirectory, configFile.Template)))
150150
{
151151
var config = LambdaDefaultsConfigFileParser.LoadFromFile(configFile);
152152
if (config.FunctionInfos?.Count > 0)
153153
{
154-
Console.WriteLine($"Found Lambda config file {file}");
154+
if (!disableLogging) Console.WriteLine($"Found Lambda config file {file}");
155155
configFiles.Add(file);
156156
}
157157
}
158158
}
159159
catch
160160
{
161-
Console.WriteLine($"Error parsing JSON file: {file}");
161+
if (!disableLogging) Console.WriteLine($"Error parsing JSON file: {file}");
162162
}
163163
}
164164

@@ -240,7 +240,12 @@ public static string SearchLatestCompilationDirectory(string debugDirectory)
240240
if (depsFile.Count == 0)
241241
return debugDirectory;
242242

243-
return depsFile[0].Directory.FullName;
243+
return depsFile[0].Directory.FullName;
244+
}
245+
246+
public static bool ShouldDisableLogs(CommandLineOptions commandOptions)
247+
{
248+
return commandOptions != null && commandOptions.DisableLogs && commandOptions.NoUI;
244249
}
245250

246251
/// <summary>

Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/CommandLineParserTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public void AllValuesGetSet()
99
{
1010
var options = CommandLineOptions.Parse(new string[] {"--help", "--host", "example.com", "--port", "1111", "--no-launch-window",
1111
"--path", "./foo", "--profile", "test", "--region", "special-region",
12-
"--no-ui", "--config-file", "test-config.json", "--payload", "myfile.json", "--pause-exit", "false" });
12+
"--no-ui", "--config-file", "test-config.json", "--payload", "myfile.json", "--pause-exit", "false", "--disable-logs" });
1313

1414
Assert.True(options.ShowHelp);
1515
Assert.Equal("example.com", options.Host);
@@ -22,6 +22,7 @@ public void AllValuesGetSet()
2222
Assert.Equal("test-config.json", options.ConfigFile);
2323
Assert.Equal("myfile.json", options.Payload);
2424
Assert.False(options.PauseExit);
25+
Assert.True(options.DisableLogs);
2526
}
2627

2728

Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests.Shared/NoUiStartupTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,23 @@ public void NoProfileAndRegion()
9696
Assert.Contains("No default AWS region configured. The --region switch can be used to configure an AWS Region.", runConfiguration.OutputWriter.ToString());
9797
}
9898

99+
[Fact]
100+
public void DirectFunctionCallFromConfigWithDisableLogs()
101+
{
102+
var runConfiguration = CreateRunConfiguration();
103+
var buildPath = TestUtils.GetLambdaFunctionBuildPath("ToUpperFunc");
104+
105+
TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "\"hello WORLD\"", "--disable-logs" }, runConfiguration);
106+
Assert.Equal("\"HELLO WORLD\"", runConfiguration.OutputWriter.ToString());
107+
}
108+
99109
private TestToolStartup.RunConfiguration CreateRunConfiguration()
100110
{
101111
return new TestToolStartup.RunConfiguration
102112
{
103113
Mode = TestToolStartup.RunConfiguration.RunMode.Test,
104114
OutputWriter = new StringWriter()
105-
};
115+
};
106116
}
107117
}
108118
}

0 commit comments

Comments
 (0)