Skip to content

Commit

Permalink
Fixed an issue where delimited string and primitive values in payload…
Browse files Browse the repository at this point in the history
… were not parsed properly by Lambda Test Tool.
  • Loading branch information
ashishdhingra committed Oct 30, 2024
1 parent 5d11116 commit 69d7f0f
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Tools/LambdaTestTool/aws-lambda-test-tool-netcore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down
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.15.2</VersionPrefix>
<VersionPrefix>0.15.3</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.15.2</VersionPrefix>
<VersionPrefix>0.15.3</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.15.2</VersionPrefix>
<VersionPrefix>0.15.3</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 8.0 AWS Lambda functions locally.</Description>
<VersionPrefix>0.15.2</VersionPrefix>
<VersionPrefix>0.15.3</VersionPrefix>
<Product>AWS .NET Lambda Test Tool</Product>
<Copyright>Apache 2</Copyright>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
25 changes: 25 additions & 0 deletions Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;

namespace Amazon.Lambda.TestTool
{
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Amazon.Lambda.TestTool.Tests
{
public class NoUiStartupTests
{


[Fact]
public void DirectFunctionCallFromConfig()
{
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Function.cs
Original file line number Diff line number Diff line change
@@ -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
{

/// <summary>
/// A simple function that takes an integer and returns a string.
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
[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}";
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.1.3" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions Tools/LambdaTestTool/tests/LambdaFunctions/IntegerFunc/Readme.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42

0 comments on commit 69d7f0f

Please sign in to comment.