forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
120 lines (96 loc) · 3.13 KB
/
api_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
package template_test
import (
"fmt"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/weaveworks/eksctl/pkg/cfn/template"
. "github.com/weaveworks/eksctl/pkg/cfn/template/matchers"
)
var _ = Describe("CloudFormation template", func() {
It("can create and render a minimal template", func() {
t := NewTemplate()
t.Description = "a template"
roleRef := t.NewResource("aRole", &IAMRole{
RoleName: "foo",
ManagedPolicyArns: []interface{}{"abc"},
})
t.Outputs["aRole"] = Output{
Value: MakeFnGetAttString("aRole.Arn"),
Export: &OutputExport{
Name: MakeFnSubString(fmt.Sprintf("${%s}::aRole", StackName)),
},
}
t.Outputs["foo"] = Output{
Value: NewString("bar"),
}
jsRoleRef, err := roleRef.MarshalJSON()
Expect(err).ToNot(HaveOccurred())
Expect(jsRoleRef).To(MatchJSON(`{"Ref": "aRole"}`))
policyRef := t.NewResource("aPolicy", &IAMPolicy{
PolicyName: NewString("foo"),
Roles: MakeSlice(roleRef),
})
jsPolicyRef, err := policyRef.MarshalJSON()
Expect(err).ToNot(HaveOccurred())
Expect(jsPolicyRef).To(MatchJSON(`{"Ref": "aPolicy"}`))
js, err := t.RenderJSON()
Expect(err).ToNot(HaveOccurred())
Expect(js).To(MatchJSON([]byte(templateExample1)))
})
It("can parse a template", func() {
t := NewTemplate()
Expect(t).To(LoadStringWithoutErrors(templateExample1))
Expect(t.Description).To(Equal("a template"))
Expect(t).To(HaveResource("aPolicy", "AWS::IAM::Policy"))
Expect(t).To(HaveResourceWithPropertyValue("aPolicy", "PolicyName", `"foo"`))
Expect(t).To(HaveResource("aRole", "AWS::IAM::Role"))
Expect(t).To(HaveResourceWithPropertyValue("aRole", "RoleName", `"foo"`))
Expect(t).ToNot(HaveResourceWithPropertyValue("aRole", "RoleName", `"bar"`))
Expect(t).ToNot(HaveResource("aRole", "AWS::Foo::Bar"))
Expect(t).ToNot(HaveResource("foo", "*"))
Expect(t).To(HaveOutputs("aRole"))
Expect(t).ToNot(HaveOutputs("foo", "bar"))
Expect(t).To(HaveOutputWithValue("aRole", `{ "Fn::GetAtt": "aRole.Arn" }`))
Expect(t).To(HaveOutputExportedAs("aRole", `{ "Fn::Sub": "${AWS::StackName}::aRole" }`))
Expect(t).ToNot(HaveOutputExportedAs("aRole", `{}`))
Expect(t).ToNot(HaveOutputExportedAs("aRole", `{ "Fn::GetAtt": "aRole.Arn" }`))
Expect(t).ToNot(HaveOutputExportedAs("foo", `{ "Fn::Sub": "${AWS::StackName}::aRole" }`))
})
It("can load multiple real templates", func() {
examples, err := filepath.Glob("testdata/*.json")
Expect(err).ToNot(HaveOccurred())
for _, example := range examples {
Expect(NewTemplate()).To(LoadFileWithoutErrors(example))
}
})
})
const templateExample1 = `{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "a template",
"Resources": {
"aPolicy": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": "foo",
"Roles": [
{ "Ref": "aRole" }
]
}
},
"aRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": "foo",
"ManagedPolicyArns": [ "abc" ]
}
}
},
"Outputs": {
"foo": { "Value": "bar" },
"aRole": {
"Value": { "Fn::GetAtt": "aRole.Arn" },
"Export": { "Name": { "Fn::Sub": "${AWS::StackName}::aRole" } }
}
}
}`