-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
205 lines (184 loc) · 5.45 KB
/
main_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
package main
import (
"encoding/json"
"testing"
"github.com/cultureamp/cfparams/parameterstore"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var cfnTemplate = []byte(`
Parameters:
Greeting:
Type: String
Description: greeting message to send
Default: Hello
Recipient:
Description: name of the greeting recipient
Type: String
ImageRepo:
Type: String
Description: repository of Docker image to run
Default: "123.dkr.ecr.us-east-1.amazonaws.com/greeting"
ImageTag:
Type: String
Description: tag of Docker image to run
Default: latest
Cluster:
Description: ECS cluster ID to run service on
Type: String
`)
func TestUsePreviousAll(t *testing.T) {
input := &Input{TemplateBody: []byte(cfnTemplate)}
actual := mustGetParameterItems(t, input)
expected := []ParameterItem{
{ParameterKey: "Greeting", UsePreviousValue: true},
{ParameterKey: "Recipient", UsePreviousValue: true},
{ParameterKey: "ImageRepo", UsePreviousValue: true},
{ParameterKey: "ImageTag", UsePreviousValue: true},
{ParameterKey: "Cluster", UsePreviousValue: true},
}
assert.Equal(t, len(expected), len(actual))
for _, item := range actual {
assert.Contains(t, expected, item)
}
}
func TestLaunchScenarioCLI(t *testing.T) {
input := &Input{
TemplateBody: cfnTemplate,
AcceptDefaults: true,
NoPrevious: true,
ParametersCLI: []string{
"Recipient=world",
"ImageTag=v1",
"Cluster=nanoservices",
},
}
actual := mustGetParameterItems(t, input)
expected := []ParameterItem{
{ParameterKey: "Recipient", ParameterValue: "world"},
{ParameterKey: "ImageTag", ParameterValue: "v1"},
{ParameterKey: "Cluster", ParameterValue: "nanoservices"},
}
assert.Equal(t, len(expected), len(actual))
for _, item := range expected {
assert.Contains(t, actual, item)
}
}
func TestLaunchScenarioFile(t *testing.T) {
input := &Input{
TemplateBody: cfnTemplate,
AcceptDefaults: true,
NoPrevious: true,
ParametersCLI: []string{"ImageTag=v1"},
ParametersYAML: []byte("---\nRecipient: world\nCluster: nanoservices\n"),
}
actual := mustGetParameterItems(t, input)
expected := []ParameterItem{
{ParameterKey: "Recipient", ParameterValue: "world"},
{ParameterKey: "ImageTag", ParameterValue: "v1"},
{ParameterKey: "Cluster", ParameterValue: "nanoservices"},
}
assert.Equal(t, len(expected), len(actual))
for _, item := range expected {
assert.Contains(t, actual, item)
}
}
func TestDeployScenario(t *testing.T) {
input := &Input{
TemplateBody: cfnTemplate,
ParametersCLI: []string{"ImageTag=v2"},
}
actual := mustGetParameterItems(t, input)
expected := []ParameterItem{
{ParameterKey: "Greeting", UsePreviousValue: true},
{ParameterKey: "Recipient", UsePreviousValue: true},
{ParameterKey: "ImageRepo", UsePreviousValue: true},
{ParameterKey: "ImageTag", ParameterValue: "v2"},
{ParameterKey: "Cluster", UsePreviousValue: true},
}
assert.Equal(t, len(expected), len(actual))
for _, item := range expected {
assert.Contains(t, actual, item)
}
}
func TestBlankDefault(t *testing.T) {
input := &Input{
TemplateBody: []byte("Parameters:\n Foo:\n Default: \"\"\n"),
AcceptDefaults: true,
}
actual := mustGetParameterItems(t, input)
assert.Equal(t, 0, len(actual))
}
func TestJsonUsePreviousValue(t *testing.T) {
input := &Input{
TemplateBody: []byte("Parameters:\n Foo:\n Description: \"may be blank\"\n"),
}
j := mustGetJson(t, input)
assert.Contains(t, j, "ParameterKey")
assert.NotContains(t, j, "ParameterValue")
assert.Contains(t, j, "UsePreviousValue")
}
func TestJsonNonBlankParameterFileValue(t *testing.T) {
input := &Input{
TemplateBody: []byte("Parameters:\n Foo:\n Description: \"may be blank\"\n"),
ParametersYAML: []byte("---\nFoo: bar\n"),
}
j := mustGetJson(t, input)
assert.Contains(t, j, "ParameterKey")
assert.Contains(t, j, "ParameterValue")
assert.NotContains(t, j, "UsePreviousValue")
}
func TestJsonBlankParameterFileValue(t *testing.T) {
input := &Input{
TemplateBody: []byte("Parameters:\n Foo:\n Description: \"may be blank\"\n"),
ParametersYAML: []byte("---\nFoo:\n"),
}
j := mustGetJson(t, input)
assert.Contains(t, j, "ParameterKey")
assert.Contains(t, j, "ParameterValue")
assert.NotContains(t, j, "UsePreviousValue")
}
func TestCustomYamlTags(t *testing.T) {
parameterstore.Fake(map[string]string{
"/path/to/secret": "hunter2",
})
input := &Input{
TemplateBody: []byte("Parameters:\n Secret:\n"),
ParametersYAML: []byte("Secret: !ParameterStore /path/to/secret\n"),
}
item := mustGetParameterItems(t, input)[0]
require.Equal(t, "Secret", item.ParameterKey)
require.Equal(t, "hunter2", item.ParameterValue)
require.Equal(t, false, item.UsePreviousValue)
}
func TestTags(t *testing.T) {
input := &Input{
TagsBody: []byte("Foo: 1\nBar: two\n"),
}
j, err := getJsonForInput(input)
require.NoError(t, err)
type tagItem struct {
Key string
Value string
}
var items []tagItem
err = json.Unmarshal(j, &items)
require.ElementsMatch(t, []tagItem{
{Key: "Foo", Value: "1"},
{Key: "Bar", Value: "two"},
}, items)
require.NoError(t, err)
}
func mustGetJson(t *testing.T, input *Input) string {
j, err := getJsonForInput(input)
require.NoError(t, err)
return string(j)
}
func mustGetParameterItems(t *testing.T, input *Input) []ParameterItem {
j, err := getJsonForInput(input)
require.NoError(t, err)
var items []ParameterItem
err = json.Unmarshal(j, &items)
require.NoError(t, err)
return items
}