Skip to content

Commit c00e3dc

Browse files
committed
Add tests for Amazon.Lambda.PowerShellHost
1 parent 4c54f99 commit c00e3dc

18 files changed

+561
-1
lines changed

Libraries/Libraries.sln

+24
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFunctionCSharp", "test\
6969
EndProject
7070
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.SQSEvents", "src\Amazon.Lambda.SQSEvents\Amazon.Lambda.SQSEvents.csproj", "{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}"
7171
EndProject
72+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.PowerShellHost", "src\Amazon.Lambda.PowerShellHost\Amazon.Lambda.PowerShellHost.csproj", "{51CAD9E3-B5AE-4787-915E-7FA096CF783F}"
73+
EndProject
74+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestPowerShellFunctions", "TestPowerShellFunctions", "{ADEC039D-0C34-4DA7-802B-6204FFE3F1F5}"
75+
EndProject
76+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerShellTests", "test\PowerShellTests\PowerShellTests.csproj", "{997B1047-4361-4E6D-9850-F130EC188141}"
77+
EndProject
78+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerShellScriptsAsFunctions", "test\TestPowerShellFunctions\PowerShellScriptsAsFunctions\PowerShellScriptsAsFunctions.csproj", "{0AD1E5D6-AC23-47C1-97BF-227007021B6F}"
79+
EndProject
7280
Global
7381
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7482
Debug|Any CPU = Debug|Any CPU
@@ -183,6 +191,18 @@ Global
183191
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
184192
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
185193
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}.Release|Any CPU.Build.0 = Release|Any CPU
194+
{51CAD9E3-B5AE-4787-915E-7FA096CF783F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195+
{51CAD9E3-B5AE-4787-915E-7FA096CF783F}.Debug|Any CPU.Build.0 = Debug|Any CPU
196+
{51CAD9E3-B5AE-4787-915E-7FA096CF783F}.Release|Any CPU.ActiveCfg = Release|Any CPU
197+
{51CAD9E3-B5AE-4787-915E-7FA096CF783F}.Release|Any CPU.Build.0 = Release|Any CPU
198+
{997B1047-4361-4E6D-9850-F130EC188141}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
199+
{997B1047-4361-4E6D-9850-F130EC188141}.Debug|Any CPU.Build.0 = Debug|Any CPU
200+
{997B1047-4361-4E6D-9850-F130EC188141}.Release|Any CPU.ActiveCfg = Release|Any CPU
201+
{997B1047-4361-4E6D-9850-F130EC188141}.Release|Any CPU.Build.0 = Release|Any CPU
202+
{0AD1E5D6-AC23-47C1-97BF-227007021B6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
203+
{0AD1E5D6-AC23-47C1-97BF-227007021B6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
204+
{0AD1E5D6-AC23-47C1-97BF-227007021B6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
205+
{0AD1E5D6-AC23-47C1-97BF-227007021B6F}.Release|Any CPU.Build.0 = Release|Any CPU
186206
EndGlobalSection
187207
GlobalSection(SolutionProperties) = preSolution
188208
HideSolutionNode = FALSE
@@ -216,6 +236,10 @@ Global
216236
{CB38FA21-5814-4573-94C7-3672D61BC8B8} = {BB021F02-D90F-469C-8D56-919771B7F4DE}
217237
{2810A66B-3A03-4889-A4D0-0E8838474A4A} = {BB021F02-D90F-469C-8D56-919771B7F4DE}
218238
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F} = {AAB54E74-20B1-42ED-BC3D-CE9F7BC7FD12}
239+
{51CAD9E3-B5AE-4787-915E-7FA096CF783F} = {AAB54E74-20B1-42ED-BC3D-CE9F7BC7FD12}
240+
{ADEC039D-0C34-4DA7-802B-6204FFE3F1F5} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
241+
{997B1047-4361-4E6D-9850-F130EC188141} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
242+
{0AD1E5D6-AC23-47C1-97BF-227007021B6F} = {ADEC039D-0C34-4DA7-802B-6204FFE3F1F5}
219243
EndGlobalSection
220244
GlobalSection(ExtensibilityGlobals) = postSolution
221245
SolutionGuid = {503678A4-B8D1-4486-8915-405A3E9CF0EB}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using Xunit;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
using Amazon.Lambda.TestUtilities;
10+
using Amazon.Lambda.PowerShellHost;
11+
12+
using static Amazon.Lambda.PowerShellTests.TestUtilites;
13+
14+
namespace Amazon.Lambda.PowerShellTests
15+
{
16+
public class ExceptionHandlingTests
17+
{
18+
19+
[Fact]
20+
public void CheckIfErrorCode()
21+
{
22+
Assert.True(ExceptionManager.IsErrorCode("ErrorCode1"));
23+
Assert.True(ExceptionManager.IsErrorCode("Error_Code"));
24+
Assert.True(ExceptionManager.IsErrorCode("ErrorϴCode"));
25+
26+
Assert.False(ExceptionManager.IsErrorCode("1ErrorCode"));
27+
Assert.False(ExceptionManager.IsErrorCode("Error Code"));
28+
Assert.False(ExceptionManager.IsErrorCode("Error@Code"));
29+
}
30+
31+
[Fact]
32+
public void ValidateSystemException()
33+
{
34+
ExceptionValidator("ThrowSystemException", "FileNotFoundException", null);
35+
}
36+
37+
[Fact]
38+
public void ValidateCustomException()
39+
{
40+
ExceptionValidator("CustomException", "AccountNotFound", "The Account is not found");
41+
}
42+
43+
[Fact]
44+
public void CustomExceptionNoMessage()
45+
{
46+
ExceptionValidator("CustomExceptionNoMessage", "CustomExceptionNoMessage", "CustomExceptionNoMessage");
47+
}
48+
49+
[Fact]
50+
public void TestMultipleInvokesWithSameCustomException()
51+
{
52+
PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1");
53+
54+
ExceptionValidator(function, "CustomException", "AccountNotFound", "The Account is not found");
55+
ExceptionValidator(function, "CustomException", "AccountNotFound", "The Account is not found");
56+
}
57+
58+
[Fact]
59+
public void TestMultipleInvokesWithDifferentCustomException()
60+
{
61+
PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1");
62+
63+
ExceptionValidator(function, "CustomException", "AccountNotFound", "The Account is not found");
64+
ExceptionValidator(function, "CustomExceptionNoMessage", "CustomExceptionNoMessage", null);
65+
}
66+
67+
[Fact]
68+
public void ThrowWithStringMessage()
69+
{
70+
ExceptionValidator("ThrowWithStringMessage", "RuntimeException", "Here is your error");
71+
}
72+
73+
[Fact]
74+
public void ThrowWithStringErrorCode()
75+
{
76+
ExceptionValidator("ThrowWithStringErrorCode", "ErrorCode42", "ErrorCode42");
77+
}
78+
79+
[Fact]
80+
public void WriteErrorWithMessageTest()
81+
{
82+
ExceptionValidator("WriteErrorWithMessageTest", "WriteErrorException", "Testing out Write-Error");
83+
}
84+
85+
[Fact]
86+
public void WriteErrorWithExceptionTest()
87+
{
88+
ExceptionValidator("WriteErrorWithExceptionTest", "FileNotFoundException", null);
89+
}
90+
91+
private void ExceptionValidator(string psFunction, string exceptionType, string message)
92+
{
93+
PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1");
94+
ExceptionValidator(function, psFunction, exceptionType, message);
95+
}
96+
97+
private void ExceptionValidator(PowerShellScriptsAsFunctions.Function function, string psFunction, string exceptionType, string message)
98+
{
99+
var logger = new TestLambdaLogger();
100+
var context = new TestLambdaContext
101+
{
102+
Logger = logger
103+
};
104+
105+
function.PowerShellFunctionName = psFunction;
106+
107+
Exception foundException = null;
108+
try
109+
{
110+
function.ExecuteFunction(new MemoryStream(), context);
111+
}
112+
catch (Exception e)
113+
{
114+
foundException = e;
115+
}
116+
117+
Assert.NotNull(foundException);
118+
Assert.True(foundException.GetType().Name.EndsWith(exceptionType));
119+
120+
if(message != null)
121+
{
122+
Assert.Equal(message, foundException.Message);
123+
}
124+
125+
}
126+
}
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using Xunit;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
using Amazon.Lambda.TestUtilities;
10+
using Amazon.Lambda.PowerShellHost;
11+
12+
using static Amazon.Lambda.PowerShellTests.TestUtilites;
13+
14+
namespace Amazon.Lambda.PowerShellTests
15+
{
16+
public class FunctionInvokeTests
17+
{
18+
[Fact]
19+
public void FunctionWithBothInputAndContext()
20+
{
21+
var logger = new TestLambdaLogger();
22+
var context = new TestLambdaContext
23+
{
24+
Logger = logger
25+
};
26+
27+
var inputString = "\"YOU WILL BE LOWER\"";
28+
var function = new PowerShellScriptsAsFunctions.Function("FunctionTests.ps1")
29+
{
30+
PowerShellFunctionName = "ToLowerWithBothParams"
31+
};
32+
33+
var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context);
34+
var resultString = ConvertToString(resultStream);
35+
Assert.Contains("Calling ToLower with both parameters", logger.Buffer.ToString());
36+
Assert.Contains("TestLambdaContext", logger.Buffer.ToString());
37+
Assert.Equal(inputString.ToLower().Replace("\"", ""), resultString);
38+
}
39+
40+
[Fact]
41+
public void FunctionWithNoContext()
42+
{
43+
var logger = new TestLambdaLogger();
44+
var context = new TestLambdaContext
45+
{
46+
Logger = logger
47+
};
48+
49+
var inputString = "\"YOU WILL BE LOWER\"";
50+
var function = new PowerShellScriptsAsFunctions.Function("FunctionTests.ps1")
51+
{
52+
PowerShellFunctionName = "ToLowerNoContext"
53+
};
54+
55+
var resultStream = function.ExecuteFunction(ConvertToStream(inputString), context);
56+
var resultString = ConvertToString(resultStream);
57+
Assert.Contains("Calling ToLower with no context", logger.Buffer.ToString());
58+
Assert.DoesNotContain("TestLambdaContext", logger.Buffer.ToString());
59+
Assert.Equal(inputString.ToLower().Replace("\"", ""), resultString);
60+
}
61+
62+
[Fact]
63+
public void FunctionWithNoParameters()
64+
{
65+
var logger = new TestLambdaLogger();
66+
var context = new TestLambdaContext
67+
{
68+
Logger = logger
69+
};
70+
71+
var function = new PowerShellScriptsAsFunctions.Function("FunctionTests.ps1")
72+
{
73+
PowerShellFunctionName = "NoParameters"
74+
};
75+
76+
function.ExecuteFunction(new MemoryStream(), context);
77+
Assert.Contains("Calling NoParameters", logger.Buffer.ToString());
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<AssemblyName>PowerShellTests</AssemblyName>
6+
<PackageId>PowerShellTests</PackageId>
7+
</PropertyGroup>
8+
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\src\Amazon.Lambda.PowerShellHost\Amazon.Lambda.PowerShellHost.csproj" />
12+
<ProjectReference Include="..\TestPowerShellFunctions\PowerShellScriptsAsFunctions\PowerShellScriptsAsFunctions.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
18+
<PackageReference Include="xunit" Version="2.2.0" />
19+
</ItemGroup>
20+
21+
22+
</Project>

0 commit comments

Comments
 (0)