private void ExceptionValidator(PowerShellScriptsAsFunctions.Function function, string psFunction, string exceptionType, string message) { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; function.PowerShellFunctionName = psFunction; Exception foundException = null; try { function.ExecuteFunction(new MemoryStream(), context); } catch (Exception e) { foundException = e; } Assert.NotNull(foundException); Assert.True(foundException.GetType().Name.EndsWith(exceptionType)); if (message != null) { Assert.Equal(message, foundException.Message); } }
public void TestMultipleInvokesWithDifferentCustomException() { PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1"); ExceptionValidator(function, "CustomException", "AccountNotFound", "The Account is not found"); ExceptionValidator(function, "CustomExceptionNoMessage", "CustomExceptionNoMessage", null); }
public void CallingUnknownCommandTest() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = ""; var function = new PowerShellScriptsAsFunctions.Function("CallingUnknownCommand.ps1"); Exception foundException = null; try { function.ExecuteFunction(ConvertToStream(inputString), context); } catch (Exception e) { foundException = e; } Assert.NotNull(foundException); Assert.Contains("New-MagicBeanCmdLet", logger.Buffer.ToString()); Assert.Contains("New-MagicBeanCmdLet", foundException.Message); }
public void TestExternalModuleLoaded() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = ""; var function = new PowerShellScriptsAsFunctions.Function("TestExternalModuleLoaded.ps1"); function.ExecuteFunction(ConvertToStream(inputString), context); Assert.Contains("Returns meta data about all the tasks defined in the provided psake script.", logger.Buffer.ToString()); }
public void FunctionWithNoParameters() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var function = new PowerShellScriptsAsFunctions.Function("FunctionTests.ps1") { PowerShellFunctionName = "NoParameters" }; function.ExecuteFunction(new MemoryStream(), context); Assert.Contains("Calling NoParameters", logger.Buffer.ToString()); }
public void CheckTempEnvironmentVariable() { // Non Lambda Environment { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = "\"hello world from powershell\""; var function = new PowerShellScriptsAsFunctions.Function("TempEnvCheck.ps1"); var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context); var resultString = ConvertToString(resultStream); Assert.Equal(Path.GetTempPath(), resultString); } var currentHome = Environment.GetEnvironmentVariable("HOME"); // Lambda environment try { Environment.SetEnvironmentVariable("HOME", null); Environment.SetEnvironmentVariable("LAMBDA_TASK_ROOT", "/var/task"); var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = "\"hello world from powershell\""; var function = new PowerShellScriptsAsFunctions.Function("TempEnvCheck.ps1"); var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context); var resultString = ConvertToString(resultStream); Assert.Equal("/tmp", resultString); Assert.Contains("/tmp/home", logger.Buffer.ToString()); } finally { Environment.SetEnvironmentVariable("LAMBDA_TASK_ROOT", null); Environment.SetEnvironmentVariable("HOME", currentHome); } }
public void ForObjectParallelTest() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = ""; var function = new PowerShellScriptsAsFunctions.Function("ForObjectParallel.ps1"); function.ExecuteFunction(ConvertToStream(inputString), context); Assert.Contains("Running against: 1 for SharedVariable: Hello Shared Variable", logger.Buffer.ToString()); Assert.Contains("Running against: 50 for SharedVariable: Hello Shared Variable", logger.Buffer.ToString()); Assert.Contains("Running against: 100 for SharedVariable: Hello Shared Variable", logger.Buffer.ToString()); }
public void ToUpperTest() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = "\"hello world from powershell\""; var function = new PowerShellScriptsAsFunctions.Function("ToUpperScript.ps1"); var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context); var resultString = ConvertToString(resultStream); Assert.Equal(inputString.ToUpper().Replace("\"", ""), resultString); Assert.Contains("Executing Script", logger.Buffer.ToString()); Assert.Contains("Logging From Context", logger.Buffer.ToString()); }
public void UseAWSPowerShellCmdLetTest() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = ""; var function = new PowerShellScriptsAsFunctions.Function("UseAWSPowerShellCmdLetTest.ps1"); var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context); var resultString = ConvertToString(resultStream); Assert.Contains(@"Importing module ./Modules/AWSPowerShell.NetCore", logger.Buffer.ToString().Replace('\\', '/')); Assert.Contains("ServiceName", resultString); Assert.Contains("AWS Lambda", resultString); }
public void TestMarshalComplexResponse() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = ""; var function = new PowerShellScriptsAsFunctions.Function("TestMarshalComplexResponse.ps1"); var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context); var resultString = ConvertToString(resultStream); var marshalledResponse = JsonConvert.DeserializeObject(resultString) as JObject; Assert.Equal("Hello World from PowerShell in Lambda", marshalledResponse["Body"].ToString()); Assert.Equal(200, (int)marshalledResponse["StatusCode"]); Assert.Equal("text/plain", marshalledResponse["Headers"]["ContentType"]); }
public void FunctionWithNoContext() { var logger = new TestLambdaLogger(); var context = new TestLambdaContext { Logger = logger }; var inputString = "\"YOU WILL BE LOWER\""; var function = new PowerShellScriptsAsFunctions.Function("FunctionTests.ps1") { PowerShellFunctionName = "ToLowerNoContext" }; var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context); var resultString = ConvertToString(resultStream); Assert.Contains("Calling ToLower with no context", logger.Buffer.ToString()); Assert.DoesNotContain("TestLambdaContext", logger.Buffer.ToString()); Assert.Equal(inputString.ToLower().Replace("\"", ""), resultString); }
private void ExceptionValidator(string psFunction, string exceptionType, string message) { PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1"); ExceptionValidator(function, psFunction, exceptionType, message); }