diff --git a/.autover/changes/72bf1fea-7d45-4ed0-a6df-0b71d4e5dd85.json b/.autover/changes/72bf1fea-7d45-4ed0-a6df-0b71d4e5dd85.json new file mode 100644 index 000000000..416f1c2f0 --- /dev/null +++ b/.autover/changes/72bf1fea-7d45-4ed0-a6df-0b71d4e5dd85.json @@ -0,0 +1,11 @@ +{ + "Projects": [ + { + "Name": "Amazon.Lambda.TestTool.BlazorTester", + "Type": "Patch", + "ChangelogMessages": [ + "Fixed an issue where delimited string and primitive values in payload were not parsed properly by Lambda Test Tool." + ] + } + ] +} \ No newline at end of file diff --git a/Tools/LambdaTestTool/aws-lambda-test-tool-netcore.sln b/Tools/LambdaTestTool/aws-lambda-test-tool-netcore.sln index dddbfa627..0229a8cd9 100644 --- a/Tools/LambdaTestTool/aws-lambda-test-tool-netcore.sln +++ b/Tools/LambdaTestTool/aws-lambda-test-tool-netcore.sln @@ -38,6 +38,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerlessTemplateYamlExamp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToUpperFunc", "tests\LambdaFunctions\ToUpperFunc\ToUpperFunc.csproj", "{7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegerFunc", "tests\LambdaFunctions\IntegerFunc\IntegerFunc.csproj", "{04026578-14B5-4EE0-8732-C4448F83356D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -92,6 +94,10 @@ Global {7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}.Debug|Any CPU.Build.0 = Debug|Any CPU {7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}.Release|Any CPU.ActiveCfg = Release|Any CPU {7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}.Release|Any CPU.Build.0 = Release|Any CPU + {04026578-14B5-4EE0-8732-C4448F83356D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {04026578-14B5-4EE0-8732-C4448F83356D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {04026578-14B5-4EE0-8732-C4448F83356D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {04026578-14B5-4EE0-8732-C4448F83356D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -110,6 +116,7 @@ Global {1055D389-9927-4EED-9876-4B497CB3B3C2} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1} {13B8F61E-5E34-46CD-B76F-A92D9DCF6946} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1} {7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1} + {04026578-14B5-4EE0-8732-C4448F83356D} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E6C77567-6F16-4EE3-8743-ADE6B68434FD} diff --git a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs index b130addb9..8f70f59d8 100644 --- a/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs +++ b/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Text.Json; namespace Amazon.Lambda.TestTool { @@ -318,6 +319,30 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co } } + try + { + var doc = JsonDocument.Parse(payload); + } + catch (JsonException) + { + try + { + if (!payload.StartsWith("[") && !payload.StartsWith("{") && !payload.StartsWith("\"")) + { + payload = "\"" + payload + "\""; + JsonDocument.Parse(payload); + } + else + { + throw; + } + } + catch (JsonException) + { + throw new ArgumentException("Provided payload for Lambda function is not a valid JSON document."); + } + } + return payload; } diff --git a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/Amazon.Lambda.TestTool.Tests.csproj b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/Amazon.Lambda.TestTool.Tests.csproj index d2974115a..9d51d2d4f 100644 --- a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/Amazon.Lambda.TestTool.Tests.csproj +++ b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/Amazon.Lambda.TestTool.Tests.csproj @@ -23,6 +23,7 @@ + diff --git a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/NoUiStartupTests.cs b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/NoUiStartupTests.cs index 17c9fc651..9c9710ba2 100644 --- a/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/NoUiStartupTests.cs +++ b/Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/NoUiStartupTests.cs @@ -10,8 +10,6 @@ namespace Amazon.Lambda.TestTool.Tests { public class NoUiStartupTests { - - [Fact] public void DirectFunctionCallFromConfig() { @@ -106,6 +104,36 @@ public void DirectFunctionCallFromConfigWithDisableLogs() Assert.Equal("\"HELLO WORLD\"", runConfiguration.OutputWriter.ToString()); } + [Fact] + public void DirectFunctionCallNonDelimitedPayloadStringFromConfig() + { + var runConfiguration = CreateRunConfiguration(); + var buildPath = TestUtils.GetLambdaFunctionBuildPath("ToUpperFunc"); + + TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "hello WORLD" }, runConfiguration); + Assert.Contains("HELLO WORLD", runConfiguration.OutputWriter.ToString()); + } + + [Fact] + public void DirectFunctionCallSingleQuoteDelimitedPayloadStringFromConfig() + { + var runConfiguration = CreateRunConfiguration(); + var buildPath = TestUtils.GetLambdaFunctionBuildPath("ToUpperFunc"); + + TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "'hello WORLD'" }, runConfiguration); + Assert.Contains("'HELLO WORLD'", runConfiguration.OutputWriter.ToString()); + } + + [Fact] + public void DirectFunctionCallIntegerPayloadFromConfig() + { + var runConfiguration = CreateRunConfiguration(); + var buildPath = TestUtils.GetLambdaFunctionBuildPath("IntegerFunc"); + + TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "42" }, runConfiguration); + Assert.Contains("Hello 42", runConfiguration.OutputWriter.ToString()); + } + private TestToolStartup.RunConfiguration CreateRunConfiguration() { return new TestToolStartup.RunConfiguration diff --git a/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Function.cs b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Function.cs new file mode 100644 index 000000000..ef3cf4489 --- /dev/null +++ b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Function.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +using Amazon.Lambda.Core; + + +namespace IntegerFunc +{ + public class Function + { + + /// + /// A simple function that takes an integer and returns a string. + /// + /// + /// + /// + [LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] + public static string FunctionHandler(int input, ILambdaContext context) + { + context.Logger.LogLine($"Executing function with input: {input}"); + Console.WriteLine("Testing Console Logging"); + + return $"Hello {input}"; + } + } +} + \ No newline at end of file diff --git a/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/IntegerFunc.csproj b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/IntegerFunc.csproj new file mode 100644 index 000000000..c7bbd56e4 --- /dev/null +++ b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/IntegerFunc.csproj @@ -0,0 +1,17 @@ + + + + net6.0;net8.0 + true + + + + + + + + + + + + diff --git a/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Readme.md b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Readme.md new file mode 100644 index 000000000..a877be198 --- /dev/null +++ b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Readme.md @@ -0,0 +1,45 @@ +# AWS Lambda Empty Function Project + +This starter project consists of: +* Function.cs - class file containing a class with a single function handler method +* aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS + +You may also have a test project depending on the options selected. + +The generated function handler is a simple method accepting an integer argument that returns a string. Replace the body of this method, and parameters, to suit your needs. + +## Here are some steps to follow from Visual Studio: + +To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*. + +To view your deployed function open its Function View window by double-clicking the function name shown beneath the AWS Lambda node in the AWS Explorer tree. + +To perform testing against your deployed function use the Test Invoke tab in the opened Function View window. + +To configure event sources for your deployed function, for example to have your function invoked when an object is created in an Amazon S3 bucket, use the Event Sources tab in the opened Function View window. + +To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window. + +To view execution logs of invocations of your function use the Logs tab in the opened Function View window. + +## Here are some steps to follow to get started from the command line: + +Once you have edited your function you can use the following command lines to build, test and deploy your function to AWS Lambda from the command line (these examples assume the project name is *EmptyFunction*): + +Restore dependencies +``` + cd "BlueprintBaseName" + dotnet restore +``` + +Execute unit tests +``` + cd "BlueprintBaseName/test/BlueprintBaseName.Tests" + dotnet test +``` + +Deploy function to AWS Lambda +``` + cd "BlueprintBaseName/src/BlueprintBaseName" + dotnet lambda deploy-function +``` diff --git a/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/aws-lambda-tools-defaults.json b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/aws-lambda-tools-defaults.json new file mode 100644 index 000000000..d71f5c86d --- /dev/null +++ b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/aws-lambda-tools-defaults.json @@ -0,0 +1,19 @@ +{ + "Information": [ + "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", + "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", + + "dotnet lambda help", + + "All the command line options for the Lambda command can be specified in this file." + ], + + "profile": "default", + "region": "us-west-2", + "configuration": "Release", + "framework": "netcoreapp3.1", + "function-runtime": "dotnetcore3.1", + "function-memory-size": 256, + "function-timeout": 30, + "function-handler": "IntegerFunc::IntegerFunc.Function::FunctionHandler" +} diff --git a/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/payload-sample.json b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/payload-sample.json new file mode 100644 index 000000000..f70d7bba4 --- /dev/null +++ b/Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/payload-sample.json @@ -0,0 +1 @@ +42 \ No newline at end of file