6
6
7
7
"github.com/gruntwork-io/terratest/modules/aws"
8
8
"github.com/gruntwork-io/terratest/modules/random"
9
+ "github.com/gruntwork-io/terratest/modules/shell"
9
10
"github.com/gruntwork-io/terratest/modules/terraform"
10
11
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
11
12
"github.com/stretchr/testify/assert"
@@ -20,6 +21,9 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
20
21
// against the same terraform module.
21
22
exampleFolder := test_structure .CopyTerraformFolderToTemp (t , "../" , "examples/terraform-aws-lambda-example" )
22
23
24
+ err := buildLambdaBinary (t , exampleFolder )
25
+ require .NoError (t , err )
26
+
23
27
// Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
24
28
// in your AWS account
25
29
functionName := fmt .Sprintf ("terratest-aws-lambda-example-%s" , random .UniqueId ())
@@ -53,14 +57,34 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
53
57
assert .Equal (t , `"hi!"` , string (response ))
54
58
55
59
// Invoke the function, this time causing it to error and capturing the error
56
- _ , err : = aws .InvokeFunctionE (t , awsRegion , functionName , ExampleFunctionPayload {ShouldFail : true , Echo : "hi!" })
60
+ _ , err = aws .InvokeFunctionE (t , awsRegion , functionName , ExampleFunctionPayload {ShouldFail : true , Echo : "hi!" })
57
61
58
62
// Function-specific errors have their own special return
59
63
functionError , ok := err .(* aws.FunctionError )
60
64
require .True (t , ok )
61
65
62
66
// Make sure the function-specific error comes back
63
- assert .Contains (t , string (functionError .Payload ), "Failed to handle" )
67
+ assert .Contains (t , string (functionError .Payload ), "failed to handle" )
68
+ }
69
+
70
+ func buildLambdaBinary (t * testing.T , tempDir string ) error {
71
+ cmd := shell.Command {
72
+ Command : "go" ,
73
+ Args : []string {
74
+ "build" ,
75
+ "-o" ,
76
+ tempDir + "/src/bootstrap" ,
77
+ tempDir + "/src/bootstrap.go" ,
78
+ },
79
+ Env : map [string ]string {
80
+ "GOOS" : "linux" ,
81
+ "GOARCH" : "amd64" ,
82
+ "CGO_ENABLED" : "0" ,
83
+ },
84
+ }
85
+
86
+ _ , err := shell .RunCommandAndGetOutputE (t , cmd )
87
+ return err
64
88
}
65
89
66
90
// Another example of how to test the Terraform module in
@@ -73,6 +97,9 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
73
97
// against the same terraform module.
74
98
exampleFolder := test_structure .CopyTerraformFolderToTemp (t , "../" , "examples/terraform-aws-lambda-example" )
75
99
100
+ err := buildLambdaBinary (t , exampleFolder )
101
+ require .NoError (t , err )
102
+
76
103
// Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
77
104
// in your AWS account
78
105
functionName := fmt .Sprintf ("terratest-aws-lambda-withparams-example-%s" , random .UniqueId ())
@@ -118,13 +145,13 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
118
145
InvocationType : & invocationType ,
119
146
Payload : ExampleFunctionPayload {ShouldFail : true , Echo : "hi!" },
120
147
}
121
- out , err : = aws .InvokeFunctionWithParamsE (t , awsRegion , functionName , input )
148
+ out , err = aws .InvokeFunctionWithParamsE (t , awsRegion , functionName , input )
122
149
123
150
// The Lambda executed, but should have failed.
124
151
assert .Error (t , err , "Unhandled" )
125
152
126
153
// Make sure the function-specific error comes back
127
- assert .Contains (t , string (out .Payload ), "Failed to handle" )
154
+ assert .Contains (t , string (out .Payload ), "failed to handle" )
128
155
129
156
// Call InvokeFunctionWithParamsE with a LambdaOptions struct that has
130
157
// an unsupported InvocationType. The function should fail.
0 commit comments