forked from awslabs/goformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goformation_test.go
502 lines (392 loc) · 16.9 KB
/
goformation_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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package goformation_test
import (
"github.com/awslabs/goformation"
"github.com/awslabs/goformation/cloudformation"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
)
var _ = Describe("Goformation", func() {
Context("with a Serverless function matching 2016-10-31 specification", func() {
template, err := goformation.Open("test/yaml/aws-serverless-function-2016-10-31.yaml")
It("should successfully validate the SAM template", func() {
Expect(err).To(BeNil())
Expect(template).ShouldNot(BeNil())
})
functions := template.GetAllAWSServerlessFunctionResources()
It("should have exactly one function", func() {
Expect(functions).To(HaveLen(1))
Expect(functions).To(HaveKey("Function20161031"))
})
f := functions["Function20161031"]
It("should correctly parse all of the function properties", func() {
Expect(f.Handler).To(Equal("file.method"))
Expect(f.Runtime).To(Equal("nodejs"))
Expect(f.FunctionName).To(Equal("functionname"))
Expect(f.Description).To(Equal("description"))
Expect(f.MemorySize).To(Equal(128))
Expect(f.Timeout).To(Equal(30))
Expect(f.Role).To(Equal("aws::arn::123456789012::some/role"))
Expect(f.Policies.StringArray).To(PointTo(ContainElement("AmazonDynamoDBFullAccess")))
Expect(f.Environment).ToNot(BeNil())
Expect(f.Environment.Variables).To(HaveKeyWithValue("NAME", "VALUE"))
})
It("should correctly parse all of the function API event sources/endpoints", func() {
Expect(f.Events).ToNot(BeNil())
Expect(f.Events).To(HaveKey("TestApi"))
Expect(f.Events["TestApi"].Type).To(Equal("Api"))
Expect(f.Events["TestApi"].Properties.ApiEvent).ToNot(BeNil())
event := f.Events["TestApi"].Properties.ApiEvent
Expect(event.Method).To(Equal("any"))
Expect(event.Path).To(Equal("/testing"))
})
})
Context("with an AWS CloudFormation template that contains multiple resources", func() {
Context("described as Go structs", func() {
template := cloudformation.NewTemplate()
template.Resources["MySNSTopic"] = cloudformation.AWSSNSTopic{
DisplayName: "test-sns-topic-display-name",
TopicName: "test-sns-topic-name",
Subscription: []cloudformation.AWSSNSTopic_Subscription{
cloudformation.AWSSNSTopic_Subscription{
Endpoint: "test-sns-topic-subscription-endpoint",
Protocol: "test-sns-topic-subscription-protocol",
},
},
}
template.Resources["MyRoute53HostedZone"] = cloudformation.AWSRoute53HostedZone{
Name: "example.com",
}
topics := template.GetAllAWSSNSTopicResources()
It("should have one AWS::SNS::Topic resource", func() {
Expect(topics).To(HaveLen(1))
Expect(topics).To(HaveKey("MySNSTopic"))
})
topic, err := template.GetAWSSNSTopicWithName("MySNSTopic")
It("should be able to find the AWS::SNS::Topic by name", func() {
Expect(topic).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct AWS::SNS::Topic values", func() {
Expect(topic.DisplayName).To(Equal("test-sns-topic-display-name"))
Expect(topic.TopicName).To(Equal("test-sns-topic-name"))
Expect(topic.Subscription).To(HaveLen(1))
Expect(topic.Subscription[0].Endpoint).To(Equal("test-sns-topic-subscription-endpoint"))
Expect(topic.Subscription[0].Protocol).To(Equal("test-sns-topic-subscription-protocol"))
})
zones := template.GetAllAWSRoute53HostedZoneResources()
It("should have one AWS::Route53::HostedZone resource", func() {
Expect(zones).To(HaveLen(1))
Expect(zones).To(HaveKey("MyRoute53HostedZone"))
})
zone, err := template.GetAWSRoute53HostedZoneWithName("MyRoute53HostedZone")
It("should be able to find the AWS::Route53::HostedZone by name", func() {
Expect(zone).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct AWS::Route53::HostedZone values", func() {
Expect(zone.Name).To(Equal("example.com"))
})
})
Context("described as JSON", func() {
template := []byte(`{"AWSTemplateFormatVersion":"2010-09-09","Resources":{"MyRoute53HostedZone":{"Type":"AWS::Route53::HostedZone","Properties":{"Name":"example.com"}},"MySNSTopic":{"Type":"AWS::SNS::Topic","Properties":{"DisplayName":"test-sns-topic-display-name","Subscription":[{"Endpoint":"test-sns-topic-subscription-endpoint","Protocol":"test-sns-topic-subscription-protocol"}],"TopicName":"test-sns-topic-name"}}}}`)
expected := cloudformation.NewTemplate()
expected.Resources["MySNSTopic"] = cloudformation.AWSSNSTopic{
DisplayName: "test-sns-topic-display-name",
TopicName: "test-sns-topic-name",
Subscription: []cloudformation.AWSSNSTopic_Subscription{
cloudformation.AWSSNSTopic_Subscription{
Endpoint: "test-sns-topic-subscription-endpoint",
Protocol: "test-sns-topic-subscription-protocol",
},
},
}
expected.Resources["MyRoute53HostedZone"] = cloudformation.AWSRoute53HostedZone{
Name: "example.com",
}
result, err := goformation.ParseJSON(template)
It("should marshal to Go structs successfully", func() {
Expect(err).To(BeNil())
})
topics := result.GetAllAWSSNSTopicResources()
It("should have one AWS::SNS::Topic resource", func() {
Expect(topics).To(HaveLen(1))
Expect(topics).To(HaveKey("MySNSTopic"))
})
topic, err := result.GetAWSSNSTopicWithName("MySNSTopic")
It("should be able to find the AWS::SNS::Topic by name", func() {
Expect(topic).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct AWS::SNS::Topic values", func() {
Expect(topic.DisplayName).To(Equal("test-sns-topic-display-name"))
Expect(topic.TopicName).To(Equal("test-sns-topic-name"))
Expect(topic.Subscription).To(HaveLen(1))
Expect(topic.Subscription[0].Endpoint).To(Equal("test-sns-topic-subscription-endpoint"))
Expect(topic.Subscription[0].Protocol).To(Equal("test-sns-topic-subscription-protocol"))
})
zones := result.GetAllAWSRoute53HostedZoneResources()
It("should have one AWS::Route53::HostedZone resource", func() {
Expect(zones).To(HaveLen(1))
Expect(zones).To(HaveKey("MyRoute53HostedZone"))
})
zone, err := result.GetAWSRoute53HostedZoneWithName("MyRoute53HostedZone")
It("should be able to find the AWS::Route53::HostedZone by name", func() {
Expect(zone).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct AWS::Route53::HostedZone values", func() {
Expect(zone.Name).To(Equal("example.com"))
})
})
})
Context("with the official AWS SAM example templates", func() {
inputs := []string{
"test/yaml/sam-official-samples/alexa_skill/template.yaml",
"test/yaml/sam-official-samples/api_backend/template.yaml",
"test/yaml/sam-official-samples/api_swagger_cors/template.yaml",
"test/yaml/sam-official-samples/encryption_proxy/template.yaml",
"test/yaml/sam-official-samples/hello_world/template.yaml",
"test/yaml/sam-official-samples/inline_swagger/template.yaml",
"test/yaml/sam-official-samples/iot_backend/template.yaml",
"test/yaml/sam-official-samples/s3_processor/template.yaml",
"test/yaml/sam-official-samples/schedule/template.yaml",
"test/yaml/sam-official-samples/stream_processor/template.yaml",
}
for _, filename := range inputs {
Context("including "+filename, func() {
template, err := goformation.Open(filename)
It("should successfully parse the SAM template", func() {
Expect(err).To(BeNil())
Expect(template).ShouldNot(BeNil())
})
})
}
})
Context("with the default AWS CodeStar templates", func() {
inputs := []string{
"test/yaml/codestar/nodejs.yml",
"test/yaml/codestar/python.yml",
"test/yaml/codestar/java.yml",
}
for _, filename := range inputs {
Context("including "+filename, func() {
template, err := goformation.Open(filename)
It("should successfully validate the SAM template", func() {
Expect(err).To(BeNil())
Expect(template).ShouldNot(BeNil())
})
})
}
})
// pmaddox@ 2017-08-17:
// Commented out until we have support for YAML tag intrinsic functions (e.g. !Sub)
Context("with a YAML template containing intrinsic tags (e.g. !Sub)", func() {
template, err := goformation.Open("test/yaml/yaml-intrinsic-tags.yaml")
It("should successfully validate the SAM template", func() {
Expect(err).To(BeNil())
Expect(template).ShouldNot(PointTo(BeNil()))
})
function, err := template.GetAWSServerlessFunctionWithName("IntrinsicFunctionTest")
It("should have a function named 'IntrinsicFunctionTest'", func() {
Expect(function).To(Not(BeNil()))
Expect(err).To(BeNil())
})
It("it should have the correct values", func() {
Expect(function.Runtime).To(Equal("4.3"))
Expect(function.Timeout).To(Equal(10))
})
})
Context("with a Serverless template containing different CodeUri formats", func() {
template, err := goformation.Open("test/yaml/aws-serverless-function-string-or-s3-location.yaml")
It("should successfully parse the template", func() {
Expect(err).To(BeNil())
Expect(template).ShouldNot(BeNil())
})
functions := template.GetAllAWSServerlessFunctionResources()
It("should have exactly three functions", func() {
Expect(functions).To(HaveLen(3))
Expect(functions).To(HaveKey("CodeUriWithS3LocationSpecifiedAsString"))
Expect(functions).To(HaveKey("CodeUriWithS3LocationSpecifiedAsObject"))
Expect(functions).To(HaveKey("CodeUriWithString"))
})
f1 := functions["CodeUriWithS3LocationSpecifiedAsString"]
It("should parse a CodeUri property with an S3 location specified as a string", func() {
Expect(f1.CodeUri.String).To(PointTo(Equal("s3://testbucket/testkey.zip")))
})
f2 := functions["CodeUriWithS3LocationSpecifiedAsObject"]
It("should parse a CodeUri property with an S3 location specified as an object", func() {
Expect(f2.CodeUri.S3Location.Key).To(Equal("testkey.zip"))
Expect(f2.CodeUri.S3Location.Version).To(Equal(5))
})
f3 := functions["CodeUriWithString"]
It("should parse a CodeUri property with a string", func() {
Expect(f3.CodeUri.String).To(PointTo(Equal("./testfolder")))
})
})
Context("with a template defined as Go code", func() {
template := &cloudformation.Template{
Resources: map[string]interface{}{
"MyLambdaFunction": cloudformation.AWSLambdaFunction{
Handler: "nodejs6.10",
},
},
}
functions := template.GetAllAWSLambdaFunctionResources()
It("should be able to retrieve all Lambda functions with GetAllAWSLambdaFunction(template)", func() {
Expect(functions).To(HaveLen(1))
})
function, err := template.GetAWSLambdaFunctionWithName("MyLambdaFunction")
It("should be able to retrieve a specific Lambda function with GetAWSLambdaFunctionWithName(template, name)", func() {
Expect(err).To(BeNil())
Expect(function).To(BeAssignableToTypeOf(cloudformation.AWSLambdaFunction{}))
})
It("should have the correct Handler property", func() {
Expect(function.Handler).To(Equal("nodejs6.10"))
})
})
Context("with a template that defines an AWS::Serverless::Function", func() {
Context("that has a CodeUri property set as an S3 Location", func() {
template := &cloudformation.Template{
Resources: map[string]interface{}{
"MySAMFunction": cloudformation.AWSServerlessFunction{
Handler: "nodejs6.10",
CodeUri: &cloudformation.AWSServerlessFunction_StringOrS3Location{
S3Location: &cloudformation.AWSServerlessFunction_S3Location{
Bucket: "test-bucket",
Key: "test-key",
Version: 100,
},
},
},
},
}
function, err := template.GetAWSServerlessFunctionWithName("MySAMFunction")
It("should have an AWS::Serverless::Function called MySAMFunction", func() {
Expect(function).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct S3 bucket/key/version", func() {
Expect(function.CodeUri.S3Location.Bucket).To(Equal("test-bucket"))
Expect(function.CodeUri.S3Location.Key).To(Equal("test-key"))
Expect(function.CodeUri.S3Location.Version).To(Equal(100))
})
})
Context("that has a CodeUri property set as a string", func() {
codeuri := "./some-folder"
template := &cloudformation.Template{
Resources: map[string]interface{}{
"MySAMFunction": cloudformation.AWSServerlessFunction{
Handler: "nodejs6.10",
CodeUri: &cloudformation.AWSServerlessFunction_StringOrS3Location{
String: &codeuri,
},
},
},
}
function, err := template.GetAWSServerlessFunctionWithName("MySAMFunction")
It("should have an AWS::Serverless::Function called MySAMFunction", func() {
Expect(function).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct CodeUri", func() {
Expect(function.CodeUri.String).To(PointTo(Equal("./some-folder")))
})
})
})
Context("with a YAML template that contains AWS::Serverless::SimpleTable resource(s)", func() {
template, err := goformation.Open("test/yaml/aws-serverless-simpletable.yaml")
It("should parse the template successfully", func() {
Expect(template).ToNot(BeNil())
Expect(err).To(BeNil())
})
table, err := template.GetAWSServerlessSimpleTableWithName("TestSimpleTable")
It("should have a table named 'TestSimpleTable'", func() {
Expect(table).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have a primary key set", func() {
Expect(table.PrimaryKey).ToNot(BeNil())
})
It("should have the correct value for the primary key name", func() {
Expect(table.PrimaryKey.Name).To(Equal("test-primary-key-name"))
})
It("should have the correct value for the primary key type", func() {
Expect(table.PrimaryKey.Type).To(Equal("test-primary-key-type"))
})
It("should have provisioned throughput set", func() {
Expect(table.ProvisionedThroughput).ToNot(BeNil())
})
It("should have the correct value for ReadCapacityUnits", func() {
Expect(table.ProvisionedThroughput.ReadCapacityUnits).To(Equal(100))
})
It("should have the correct value for WriteCapacityUnits", func() {
Expect(table.ProvisionedThroughput.WriteCapacityUnits).To(Equal(200))
})
It("should have a table named 'TestSimpleTableNoProperties'", func() {
nopropertiesTable, err := template.GetAWSServerlessSimpleTableWithName("TestSimpleTableNoProperties")
Expect(nopropertiesTable).ToNot(BeNil())
Expect(err).To(BeNil())
})
})
Context("with a YAML template that contains AWS::Serverless::Api resource(s)", func() {
template, err := goformation.Open("test/yaml/aws-serverless-api.yaml")
It("should parse the template successfully", func() {
Expect(template).ToNot(BeNil())
Expect(err).To(BeNil())
})
api1, err := template.GetAWSServerlessApiWithName("ServerlessApiWithDefinitionUriAsString")
It("should have an AWS::Serverless::Api named 'ServerlessApiWithDefinitionUriAsString'", func() {
Expect(api1).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct value for Name", func() {
Expect(api1.Name).To(Equal("test-name"))
})
It("should have the correct value for StageName", func() {
Expect(api1.StageName).To(Equal("test-stage-name"))
})
It("should have the correct value for DefinitionUri", func() {
Expect(api1.DefinitionUri.String).To(PointTo(Equal("test-definition-uri")))
})
It("should have the correct value for CacheClusterEnabled", func() {
Expect(api1.CacheClusterEnabled).To(Equal(true))
})
It("should have the correct value for CacheClusterSize", func() {
Expect(api1.CacheClusterSize).To(Equal("test-cache-cluster-size"))
})
It("should have the correct value for Variables", func() {
Expect(api1.Variables).To(HaveKeyWithValue("NAME", "VALUE"))
})
api2, err := template.GetAWSServerlessApiWithName("ServerlessApiWithDefinitionUriAsS3Location")
It("should have an AWS::Serverless::Api named 'ServerlessApiWithDefinitionUriAsS3Location'", func() {
Expect(api2).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct value for DefinitionUri", func() {
Expect(api2.DefinitionUri.S3Location.Bucket).To(Equal("test-bucket"))
Expect(api2.DefinitionUri.S3Location.Key).To(Equal("test-key"))
Expect(api2.DefinitionUri.S3Location.Version).To(Equal(1))
})
api3, err := template.GetAWSServerlessApiWithName("ServerlessApiWithDefinitionBodyAsJSON")
It("should have an AWS::Serverless::Api named 'ServerlessApiWithDefinitionBodyAsJSON'", func() {
Expect(api3).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct value for DefinitionBody", func() {
Expect(api3.DefinitionBody).To(Equal("{\n \"DefinitionKey\": \"test-definition-value\"\n}\n"))
})
api4, err := template.GetAWSServerlessApiWithName("ServerlessApiWithDefinitionBodyAsYAML")
It("should have an AWS::Serverless::Api named 'ServerlessApiWithDefinitionBodyAsYAML'", func() {
Expect(api4).ToNot(BeNil())
Expect(err).To(BeNil())
})
It("should have the correct value for DefinitionBody", func() {
var expected map[string]interface{}
expected = map[string]interface{}{
"DefinitionKey": "test-definition-value",
}
Expect(api4.DefinitionBody).To(Equal(expected))
})
})
})