-
Notifications
You must be signed in to change notification settings - Fork 48
/
test.go
174 lines (152 loc) · 4.57 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package sparta
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
"github.com/rs/zerolog"
)
const lambdaTestExecuteARN = "LambdaExecutor"
const s3BucketSourceArn = "arn:aws:s3:::sampleBucket"
const snsTopicSourceArn = "arn:aws:sns:us-west-2:000000000000:someTopic"
const dynamoDBTableArn = "arn:aws:dynamodb:us-west-2:000000000000:table/sampleTable"
func mockLambda1(ctx context.Context) (string, error) {
return "mockLambda1!", nil
}
func mockLambda2(ctx context.Context) (string, error) {
return "mockLambda2!", nil
}
func mockLambda3(ctx context.Context) (string, error) {
return "mockLambda3!", nil
}
func testLambdaData() []*LambdaAWSInfo {
var lambdaFunctions []*LambdaAWSInfo
//////////////////////////////////////////////////////////////////////////////
// Lambda function 1
lambdaFn1, lambdaFn1Err := NewAWSLambda(LambdaName(mockLambda1),
mockLambda1,
lambdaTestExecuteARN)
if lambdaFn1Err != nil {
panic("Failed to create lambda1")
}
lambdaFn1.Permissions = append(lambdaFn1.Permissions, S3Permission{
BasePermission: BasePermission{
SourceArn: s3BucketSourceArn,
},
// Event Filters are defined at
// http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
Events: []string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"},
})
lambdaFn1.Permissions = append(lambdaFn1.Permissions, SNSPermission{
BasePermission: BasePermission{
SourceArn: snsTopicSourceArn,
},
})
lambdaFn1.EventSourceMappings = append(lambdaFn1.EventSourceMappings, &EventSourceMapping{
StartingPosition: "TRIM_HORIZON",
EventSourceArn: dynamoDBTableArn,
BatchSize: 10,
})
lambdaFunctions = append(lambdaFunctions, lambdaFn1)
//////////////////////////////////////////////////////////////////////////////
// Lambda function 2
lambdaFn2, lambdaFn2Err := NewAWSLambda(LambdaName(mockLambda2),
mockLambda2,
lambdaTestExecuteARN)
if lambdaFn2Err != nil {
panic("Failed to create lambda2")
}
lambdaFunctions = append(lambdaFunctions, lambdaFn2)
//////////////////////////////////////////////////////////////////////////////
// Lambda function 3
// https://github.com/mweagle/Sparta/pull/1
lambdaFn3, lambdaFn3Err := NewAWSLambda(LambdaName(mockLambda3),
mockLambda3,
lambdaTestExecuteARN)
if lambdaFn3Err != nil {
panic("Failed to create lambda3")
}
lambdaFn3.Permissions = append(lambdaFn3.Permissions, SNSPermission{
BasePermission: BasePermission{
SourceArn: snsTopicSourceArn,
},
})
lambdaFunctions = append(lambdaFunctions, lambdaFn3)
return lambdaFunctions
}
// testProvisionEvaluator is the function that is called following a
// provision to determine if the result was successful
type testProvisionEvaluator func(t *testing.T, didError error) error
// assertSuccess is a default handler for the ProvisionRunner. If no
// evaluator is supplied, defaults to expecting no didError
func assertSuccess(t *testing.T, didError error) error {
if didError != nil {
t.Fatal("Provision failed: " + didError.Error())
}
return nil
}
// assertError returns a test evaluator that enforces that didError is not nil
func assertError(message string) testProvisionEvaluator {
return func(t *testing.T, didError error) error {
t.Logf("Checking provisioning error: %s", didError)
if didError == nil {
t.Fatal("Failed to reject error due to: " + message)
}
return nil
}
}
// testProvision is a convenience function for testProvisionEx
func testProvision(t *testing.T,
lambdaAWSInfos []*LambdaAWSInfo,
evaluator testProvisionEvaluator) {
testProvisionEx(t, lambdaAWSInfos, nil, nil, nil, false, evaluator)
}
// testProvisionEx handles mock provisioning a service and then
// supplying the result to the evaluator function
func testProvisionEx(t *testing.T,
lambdaAWSInfos []*LambdaAWSInfo,
api APIGateway,
site *S3Site,
workflowHooks *WorkflowHooks,
useCGO bool,
evaluator testProvisionEvaluator) {
if evaluator == nil {
evaluator = assertSuccess
}
logger, loggerErr := NewLogger(zerolog.InfoLevel.String())
if loggerErr != nil {
t.Fatalf("Failed to create test logger: %s", loggerErr)
}
var templateWriter bytes.Buffer
workingDir, workingDirErr := os.Getwd()
if workingDirErr != nil {
t.Error(workingDirErr)
}
fullPath, fullPathErr := filepath.Abs(workingDir)
if fullPathErr != nil {
t.Error(fullPathErr)
}
err := Build(context.Background(),
true,
"SampleProvision",
"",
lambdaAWSInfos,
nil,
nil,
false,
"testBuildID",
"",
fullPath,
"",
"",
&templateWriter,
nil,
logger)
if evaluator != nil {
err = evaluator(t, err)
}
if err != nil {
t.Fatalf("Failed to apply evaluator: " + err.Error())
}
}