-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudformation_test.go
339 lines (310 loc) · 7.87 KB
/
cloudformation_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package godeploycfn
import (
"fmt"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
)
type mockCFClient struct {
cloudformationiface.CloudFormationAPI
callsBeforeStackFinished int
calls *int
}
func newMockCFClient(calls int, callsBeforeFinished int) mockCFClient {
return mockCFClient{
callsBeforeStackFinished: callsBeforeFinished,
calls: &calls,
}
}
// this mocked method returns a stack with no error of the name contains `update`, and returns
// an error `stackname does not exist` to satisfy the AWS behaviour when the stack is not present
// to mimic a `create`. It also returns an invalid response when the stackname contains error.
//
// in the case that as stack is in the update state, it will return StackStatusUpdateInProgress if
// the internal calls variable is not equal to a present number. In normal cases when omitted, because
// both zero-values are 0, it does nothing and the stack is always in StackStatusUpdateComplete.
func (m mockCFClient) DescribeStacks(input *cloudformation.DescribeStacksInput) (*cloudformation.DescribeStacksOutput, error) {
if strings.Contains(*input.StackName, "update") {
status := cloudformation.StackStatusUpdateInProgress
if *m.calls >= m.callsBeforeStackFinished {
status = cloudformation.StackStatusUpdateComplete
}
*(m.calls)++
fmt.Printf("status %v\n", status)
return &cloudformation.DescribeStacksOutput{
NextToken: nil,
Stacks: []*cloudformation.Stack{
{
StackName: input.StackName,
StackStatus: aws.String(status),
},
},
}, nil
} else if strings.Contains(*input.StackName, "error") {
return &cloudformation.DescribeStacksOutput{
NextToken: nil,
Stacks: nil,
}, fmt.Errorf("unexpected error")
}
return &cloudformation.DescribeStacksOutput{
NextToken: nil,
Stacks: nil,
}, fmt.Errorf("stackname %v does not exist", *input.StackName)
}
func (m mockCFClient) ExecuteChangeSet(*cloudformation.ExecuteChangeSetInput) (*cloudformation.ExecuteChangeSetOutput, error) {
return &cloudformation.ExecuteChangeSetOutput{}, nil
}
func TestCloudformation_executeChangeSet(t *testing.T) {
type fields struct {
CFClient mockCFClient
StackName string
}
type args struct {
changeSetName string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
expectedNumberOfCalls int
}{
{
name: "test execute changeset happy path",
fields: fields{
CFClient: newMockCFClient(0, 1),
StackName: "test update stack",
},
args: args{
changeSetName: "foobar doesn't matter",
},
wantErr: false,
expectedNumberOfCalls: 2, // callsBeforeFinished + 1
},
{
name: "test execute changeset happy path no retry",
fields: fields{
CFClient: newMockCFClient(0, 0),
StackName: "test update stack noretry",
},
args: args{
changeSetName: "foobar doesn't matter",
},
wantErr: false,
expectedNumberOfCalls: 1, // callsBeforeFinished + 1
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cloudformation{
CFClient: tt.fields.CFClient,
StackName: tt.fields.StackName,
}
if err := c.executeChangeSet(tt.args.changeSetName); (err != nil) != tt.wantErr {
t.Errorf("executeChangeSet() error = %v, wantErr %v", err, tt.wantErr)
}
gotCalls := *tt.fields.CFClient.calls
wantCalls := tt.expectedNumberOfCalls
if gotCalls != wantCalls {
t.Errorf("unexpected no. of calls, expected %v but got %v", wantCalls, gotCalls)
}
})
}
}
func TestCloudformation_getCreateType(t *testing.T) {
type fields struct {
CFClient cloudformationiface.CloudFormationAPI
StackName string
}
tests := []struct {
name string
fields fields
want string
wantErr bool
}{
{
name: "Test getting UPDATE changeset",
fields: fields{
CFClient: newMockCFClient(0, 0),
StackName: "stack with update",
},
want: "UPDATE",
wantErr: false,
},
{
name: "Test getting CREATE changeset",
fields: fields{
CFClient: newMockCFClient(0, 0),
StackName: "stack with create",
},
want: "CREATE",
wantErr: false,
},
{
name: "Test getting unexpected error",
fields: fields{
CFClient: newMockCFClient(0, 0),
StackName: "stack with error",
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cloudformation{
CFClient: tt.fields.CFClient,
StackName: tt.fields.StackName,
}
got, err := c.getCreateType()
if (err != nil) != tt.wantErr {
t.Errorf("getCreateType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getCreateType() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_changeSetIsEmpty(t *testing.T) {
type args struct {
o *cloudformation.DescribeChangeSetOutput
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Test changeset IS empty",
args: args{
o: &cloudformation.DescribeChangeSetOutput{
Status: aws.String("FAILED"),
StatusReason: aws.String("foobar submitted information didn't contain changes"),
},
},
want: true,
},
{
name: "Test changeset is not empty (other status reason)",
args: args{
o: &cloudformation.DescribeChangeSetOutput{
Status: aws.String("FAILED"),
StatusReason: aws.String("foobar foo"),
},
},
want: false,
},
{
name: "Test changeset is not empty (other status)",
args: args{
o: &cloudformation.DescribeChangeSetOutput{
Status: aws.String("BANANA"),
StatusReason: aws.String("foobar submitted information didn't contain changes"),
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := changeSetIsEmpty(tt.args.o); got != tt.want {
t.Errorf("changeSetIsEmpty() = %v, want %v", got, tt.want)
}
})
}
}
func Test_trimStackName(t *testing.T) {
type args struct {
stackName string
max int
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test that trim works",
args: args{
stackName: "foobar",
max: 3,
},
want: "foo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := trimStackName(tt.args.stackName, tt.args.max); got != tt.want {
t.Errorf("trimStackName() = %v, want %v", got, tt.want)
}
})
}
}
func Test_CreateStackName(t *testing.T) {
type args struct {
alarmName string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test Global Alarm Name Generation",
args: args{
alarmName: "AWS/Lambda/Global/ConcurrentExecutions",
},
want: "aws-lambda-global-concurrentexecutions",
},
{
name: "Test Lambda Alarm Name Generation 2nd Case",
args: args{
alarmName: "AWS/Lambda/Global/Error",
},
want: "aws-lambda-global-error",
},
{
name: "Test Lambda Alarm Name Generation No Periods",
args: args{
alarmName: "AWS/Lambda/Global/Spot.foo.bar",
},
want: "aws-lambda-global-spot-foo-bar",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateStackName(tt.args.alarmName); got != tt.want {
t.Errorf("createStackName() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateLogicalName(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test normal create logical name",
args: args{
s: "Foo/Bar - baz_bux.foo",
},
want: "FooBarbazbuxfoo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateLogicalName(tt.args.s); got != tt.want {
t.Errorf("CreateLogicalName() = %v, want %v", got, tt.want)
}
})
}
}