Skip to content

Commit aa60d30

Browse files
authored
Merge pull request aws#1467 from aws/normj/testtool-env-variable-support
Add support for setting environment variables with test tool
2 parents 59ec9c1 + 3964e81 commit aa60d30

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class LambdaConfigFile
1919
[JsonPropertyName("image-command")]
2020
public string ImageCommand { get; set; }
2121

22+
[JsonPropertyName("environment-variables")]
23+
public string EnvironmentVariables { get; set; }
24+
2225
public string ConfigFileLocation { get; set; }
2326

2427
public string DetermineHandler()

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Runtime/LambdaDefaultsConfigFileParser.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
using YamlDotNet.RepresentationModel;
88

9-
109
namespace Amazon.Lambda.TestTool.Runtime
1110
{
1211
/// <summary>
@@ -69,6 +68,19 @@ public static LambdaConfigInfo LoadFromFile(LambdaConfigFile configFile)
6968
{
7069
info.Name = functionHandler;
7170
}
71+
72+
if(configFile.EnvironmentVariables != null)
73+
{
74+
var variables = configFile.EnvironmentVariables.Split(';');
75+
foreach(var variable in variables)
76+
{
77+
var kvp = variable.Split('=');
78+
if(kvp.Length == 2)
79+
{
80+
info.EnvironmentVariables[kvp[0]] = kvp[1];
81+
}
82+
}
83+
}
7284

7385
configInfo.FunctionInfos.Add(info);
7486
}
@@ -197,6 +209,21 @@ private static void ProcessYamlServerlessTemplateFunctionBased(LambdaConfigInfo
197209
Handler = handler
198210
};
199211

212+
if (resourceBody.Children.TryGetValue("Environment", out var environmentProperty) && environmentProperty is YamlMappingNode)
213+
{
214+
if (((YamlMappingNode)environmentProperty).Children.TryGetValue("Environment", out var variableProperty) && variableProperty is YamlMappingNode)
215+
{
216+
foreach(var kvp in ((YamlMappingNode)variableProperty).Children)
217+
{
218+
if(kvp.Key is YamlScalarNode keyNode && keyNode.Value != null &&
219+
kvp.Value is YamlScalarNode valueNode && valueNode.Value != null)
220+
{
221+
functionInfo.EnvironmentVariables[keyNode.Value] = valueNode.Value;
222+
}
223+
}
224+
}
225+
}
226+
200227
configInfo.FunctionInfos.Add(functionInfo);
201228
}
202229
}
@@ -255,6 +282,18 @@ private static void ProcessJsonServerlessTemplate(LambdaConfigInfo configInfo, s
255282
Handler = handler
256283
};
257284

285+
if(propertiesProperty.TryGetProperty("Environment", out var environmentProperty) &&
286+
environmentProperty.TryGetProperty("Variables", out var variablesProperty))
287+
{
288+
foreach(var property in variablesProperty.EnumerateObject())
289+
{
290+
if(property.Value.ValueKind == JsonValueKind.String)
291+
{
292+
functionInfo.EnvironmentVariables[property.Name] = property.Value.GetString();
293+
}
294+
}
295+
}
296+
258297
configInfo.FunctionInfos.Add(functionInfo);
259298
}
260299
}

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Runtime/LambdaExecutor.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
@@ -37,6 +38,18 @@ public async Task<ExecutionResponse> ExecuteAsync(ExecutionRequest request)
3738
Environment.SetEnvironmentVariable("AWS_PROFILE", request.AWSProfile);
3839
}
3940

41+
// Set the Lambda environment variable for the function name. Some libraries like
42+
// our Amazon.Lambda.AspNetCoreServer.Hosting use this environment variable to
43+
// tell if they are running in Lambda and if so activate. Since we are emulating
44+
// Lambda we want those libraries to activate.
45+
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", request.Function.FunctionInfo.Name);
46+
47+
// If Environment variables were defined for the function
48+
// then set them for the process to the emulated function picks up the variables.
49+
foreach (var kvp in request.Function.FunctionInfo.EnvironmentVariables)
50+
{
51+
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
52+
}
4053

4154
var context = new LocalLambdaContext()
4255
{
@@ -64,6 +77,12 @@ public async Task<ExecutionResponse> ExecuteAsync(ExecutionRequest request)
6477
}
6578
finally
6679
{
80+
// To avoid side effects remove the environment variables that were set specifically
81+
// for running the lambda function.
82+
foreach (var kvp in request.Function.FunctionInfo.EnvironmentVariables)
83+
{
84+
Environment.SetEnvironmentVariable(kvp.Key, null);
85+
}
6786
executeSlim.Release();
6887
}
6988
}

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Runtime/LambdaFunctionInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Amazon.Lambda.TestTool.Runtime
1+
using System.Collections.Generic;
2+
3+
namespace Amazon.Lambda.TestTool.Runtime
24
{
35
public class LambdaFunctionInfo
46
{
@@ -11,5 +13,7 @@ public class LambdaFunctionInfo
1113
/// The Lambda function handler string.
1214
/// </summary>
1315
public string Handler { get; set; }
16+
17+
public IDictionary<string, string> EnvironmentVariables { get; } = new Dictionary<string, string>();
1418
}
1519
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ public void LambdaFunctionWithName()
4141
File.Delete(jsonFile);
4242
}
4343
}
44+
45+
[Fact]
46+
public void LambdaFunctionWithEnvironmentVariables()
47+
{
48+
var jsonFile = WriteTempConfigFile("{\"function-handler\" : \"Assembly::Type::Method\", \"environment-variables\" : \"key1=value1;key2=value2\"}");
49+
try
50+
{
51+
var configInfo = LambdaDefaultsConfigFileParser.LoadFromFile(jsonFile);
52+
Assert.Single(configInfo.FunctionInfos);
53+
Assert.Equal("Assembly::Type::Method", configInfo.FunctionInfos[0].Handler);
54+
Assert.Equal("TheFunc", configInfo.FunctionInfos[0].Name);
55+
56+
Assert.Equal(2, configInfo.FunctionInfos[0].EnvironmentVariables.Count);
57+
Assert.Equal("value1", configInfo.FunctionInfos[0].EnvironmentVariables["key1"]);
58+
Assert.Equal("value2", configInfo.FunctionInfos[0].EnvironmentVariables["key2"]);
59+
}
60+
finally
61+
{
62+
File.Delete(jsonFile);
63+
}
64+
}
65+
4466
[Fact]
4567
public void LambdaFunctionWithImageCommand()
4668
{

0 commit comments

Comments
 (0)