-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
config_test.go
406 lines (383 loc) · 10.9 KB
/
config_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
package ecspresso_test
import (
"context"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/kayac/ecspresso/v2"
)
func TestLoadServiceDefinition(t *testing.T) {
ctx := context.Background()
app, err := ecspresso.New(ctx, &ecspresso.CLIOptions{ConfigFilePath: "tests/test.yaml"})
if err != nil {
t.Error(err)
}
c := app.Config()
for _, ext := range []string{"", "net"} {
sv, err := app.LoadServiceDefinition(c.ServiceDefinitionPath + ext)
if err != nil || sv == nil {
t.Errorf("%s load failed: %s", c.ServiceDefinitionPath, err)
}
if *sv.ServiceName != "test" ||
aws.ToInt32(sv.DesiredCount) != 2 ||
aws.ToString(sv.LoadBalancers[0].TargetGroupArn) != "arn:aws:elasticloadbalancing:us-east-1:1111111111:targetgroup/test/12345678" ||
sv.LaunchType != types.LaunchTypeEc2 ||
sv.SchedulingStrategy != types.SchedulingStrategyReplica ||
sv.PropagateTags != types.PropagateTagsService ||
*sv.Tags[0].Key != "cluster" ||
*sv.Tags[0].Value != "default2" {
t.Errorf("unexpected service definition %#v", sv)
}
if dc := sv.DeploymentConfiguration; dc == nil {
t.Error("deployment configuration is nil")
} else {
if *dc.MaximumPercent != 200 || *dc.MinimumHealthyPercent != 50 {
t.Errorf("unexpected deployment configuration %#v", dc)
}
if dc.Alarms == nil {
t.Errorf("deployment configuration alarms is nil")
} else {
if len(dc.Alarms.AlarmNames) != 1 || dc.Alarms.AlarmNames[0] != "HighResponseLatencyAlarm" {
t.Errorf("unexpected alarms %#v", dc.Alarms)
}
}
}
}
}
func TestLoadConfigWithPluginAbsPath(t *testing.T) {
testLoadConfigWithPlugin(t, "tests/config_abs.yaml")
}
func TestLoadConfigWithPluginMultiple(t *testing.T) {
testLoadConfigWithPlugin(t, "tests/config_multiple_plugins.yaml")
}
func TestLoadConfigWithPluginDuplicate(t *testing.T) {
t.Setenv("TAG", "testing")
t.Setenv("JSON", `{"foo":"bar"}`)
ctx := context.Background()
loader := ecspresso.NewConfigLoader(nil, nil)
_, err := loader.Load(ctx, "tests/config_duplicate_plugins.yaml", "")
if err == nil {
t.Log("expected an error to occur, but it didn't.")
t.FailNow()
}
expectedEnds := "already exists. set func_prefix to tfstate plugin"
if !strings.HasSuffix(err.Error(), expectedEnds) {
t.Log("unexpected error message")
t.Log("expected ends:", expectedEnds)
t.Log("actual: ", err.Error())
t.FailNow()
}
}
func TestLoadConfigWithPlugin(t *testing.T) {
for _, ext := range []string{".yml", ".yaml", ".json", ".jsonnet"} {
t.Run("tests/ecspresso"+ext, func(t *testing.T) {
testLoadConfigWithPlugin(t, "tests/ecspresso"+ext)
})
}
}
func testLoadConfigWithPlugin(t *testing.T, path string) {
t.Setenv("TAG", "testing")
t.Setenv("JSON", `{"foo":"bar"}`)
t.Setenv("AWS_REGION", "ap-northeast-1")
ctx := context.Background()
app, err := ecspresso.New(ctx, &ecspresso.CLIOptions{ConfigFilePath: path})
if err != nil {
t.Error(err)
}
if app.Name() != "test/default" {
t.Errorf("unexpected name got %s", app.Name())
}
conf := app.Config()
if conf.Timeout.Duration != time.Minute*10 {
t.Errorf("unexpected timeout got %s expected %s", conf.Timeout.Duration, time.Minute*10)
}
svd, err := app.LoadServiceDefinition(conf.ServiceDefinitionPath)
if err != nil {
t.Error(err)
}
t.Log(str(svd))
sgID := svd.NetworkConfiguration.AwsvpcConfiguration.SecurityGroups[0]
subnetID := svd.NetworkConfiguration.AwsvpcConfiguration.Subnets[0]
if sgID != "sg-12345678" {
t.Errorf("unexpected sg id got:%s", sgID)
}
if subnetID != "subnet-07ac54af5e41a4fc4" {
t.Errorf("unexpected subnet id got:%s", subnetID)
}
cb := *svd.DeploymentConfiguration.DeploymentCircuitBreaker
if !cb.Enable {
t.Errorf("unexpected deploymentCircuitBreaker.enable got:%v", cb.Enable)
}
if !cb.Rollback {
t.Errorf("unexpected deploymentCircuitBreaker.rollback got:%v", cb.Rollback)
}
if len(svd.Tags) != 1 {
t.Errorf("unexpected tags got:%s", str(svd.Tags))
}
if tag := svd.Tags[0]; *tag.Key != "Name" || *tag.Value != "test" {
t.Errorf("unexpected tag got:%s", str(tag))
}
td, err := app.LoadTaskDefinition(conf.TaskDefinitionPath)
if err != nil {
t.Error(err)
}
t.Log(str(td))
image := *td.ContainerDefinitions[0].Image
if image != "123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/app:testing" {
t.Errorf("unexpected image got:%s", image)
}
env := td.ContainerDefinitions[0].Environment[0]
if *env.Name != "JSON" || *env.Value != `{"foo":"bar"}` {
t.Errorf("unexpected JSON got:%s", *env.Value)
}
if len(td.Tags) != 1 {
t.Errorf("unexpected tags got:%s", str(td.Tags))
}
if tag := td.Tags[0]; *tag.Key != "Name" || *tag.Value != "test" {
t.Errorf("unexpected tag got:%s", str(tag))
}
}
func TestRestrictConfigWithRequiredVersion(t *testing.T) {
cases := []struct {
RequiredVersion string
CurrentVersion string
}{
{
RequiredVersion: ">= v1.0.0",
CurrentVersion: "v1.2.1",
},
{
RequiredVersion: "= v1.0.0",
CurrentVersion: "1.0.0",
},
{
RequiredVersion: "~> v1.1.0",
CurrentVersion: "1.1.5",
},
{
RequiredVersion: "~> v1.0",
CurrentVersion: "1.2.1",
},
{
RequiredVersion: ">= v1, < v2",
CurrentVersion: "1.2.1",
},
{
RequiredVersion: ">= v1.2.1, < v2",
CurrentVersion: "v1.2.1+3-g04fdc8e",
},
{
RequiredVersion: ">= v1",
CurrentVersion: "current",
},
}
for _, c := range cases {
t.Run(c.CurrentVersion+":"+c.RequiredVersion, func(t *testing.T) {
conf := ecspresso.NewDefaultConfig()
conf.RequiredVersion = c.RequiredVersion
if err := conf.ValidateVersion(c.CurrentVersion); err != nil {
t.Error(err)
}
})
}
}
func TestConfigWithRequiredVersionUnsatisfied(t *testing.T) {
cases := []struct {
RequiredVersion string
CurrentVersion string
ErrorMessage string
}{
{
RequiredVersion: "= v1.0.0",
CurrentVersion: "v1.2.1",
ErrorMessage: "does not satisfy constraints",
},
{
RequiredVersion: "~> v1.1.0",
CurrentVersion: "v1.2.0",
ErrorMessage: "does not satisfy constraints",
},
{
RequiredVersion: ">= v1.2.2, < v2",
CurrentVersion: "v1.2.1+3-g04fdc8e",
ErrorMessage: "does not satisfy constraints",
},
{
RequiredVersion: ">= v0, <v1",
CurrentVersion: "v1.2.1",
ErrorMessage: "does not satisfy constraints",
},
}
ctx := context.Background()
for _, c := range cases {
t.Run(c.CurrentVersion+":"+c.RequiredVersion, func(t *testing.T) {
conf := ecspresso.NewDefaultConfig()
conf.RequiredVersion = c.RequiredVersion
if err := conf.Restrict(ctx); err != nil {
t.Error(err)
return
}
err := conf.ValidateVersion(c.CurrentVersion)
if err == nil {
t.Error("expected any error, but no error")
return
}
if !strings.Contains(err.Error(), c.ErrorMessage) {
t.Errorf("unexpected error got:%s", err)
}
})
}
}
func TestConfigWithInvalidRequiredVersion(t *testing.T) {
cases := []struct {
RequiredVersion string
CurrentVersion string
ErrorMessage string
}{
{
RequiredVersion: "hoge",
CurrentVersion: "v1.2.1",
ErrorMessage: "invalid format",
},
}
ctx := context.Background()
for _, c := range cases {
t.Run(c.CurrentVersion+":"+c.RequiredVersion, func(t *testing.T) {
conf := ecspresso.NewDefaultConfig()
conf.RequiredVersion = c.RequiredVersion
err := conf.Restrict(ctx)
if err == nil {
t.Error("expected any error, but no error")
return
}
if !strings.Contains(err.Error(), c.ErrorMessage) {
t.Errorf("unexpected error got:%s", err)
}
})
}
}
func TestLoadConfigWithoutTimeout(t *testing.T) {
t.Setenv("AWS_REGION", "ap-northeast-2")
ctx := context.Background()
loader := ecspresso.NewConfigLoader(nil, nil)
conf, err := loader.Load(ctx, "tests/notimeout.yml", "")
if err != nil {
t.Log("unexpected an error", err)
t.FailNow()
}
if conf.Timeout == nil {
t.Error("expected default timeout, but nil")
}
if conf.Timeout.Duration != ecspresso.DefaultTimeout {
t.Errorf("expected default timeout, but %v", conf.Timeout.Duration)
}
if conf.Region != "ap-northeast-2" {
t.Errorf("expected region from AWS_REGION, but %v", conf.Region)
}
}
func TestLoadConfigForCodeDeploy(t *testing.T) {
ctx := context.Background()
loader := ecspresso.NewConfigLoader(nil, nil)
for _, ext := range []string{"yml", "json", "jsonnet"} {
name := "tests/config_codedeploy." + ext
conf, err := loader.Load(ctx, name, "")
if err != nil {
t.Error(err)
}
if conf.CodeDeploy.ApplicationName != "myapp" {
t.Errorf("expected application name, but %v", conf.CodeDeploy.ApplicationName)
}
if conf.CodeDeploy.DeploymentGroupName != "mydeployment" {
t.Errorf("expected deployment group name, but %v", conf.CodeDeploy.DeploymentGroupName)
}
}
}
var FilterCommandTests = []struct {
Env string
Expected string
}{
{"", "fzf"},
{"peco", "peco"},
}
func TestFilterCommandDeprecated(t *testing.T) {
ctx := context.Background()
for _, ts := range FilterCommandTests {
app, err := ecspresso.New(ctx, &ecspresso.CLIOptions{
ConfigFilePath: "tests/filter_command.yml",
FilterCommand: ts.Env,
})
if err != nil {
t.Error(err)
}
if app.FilterCommand() != ts.Expected {
t.Errorf("expected %s, but got %s", ts.Expected, app.FilterCommand())
}
}
}
var ConfigIgnoreTests = []struct {
name string
ignore *ecspresso.ConfigIgnore
resourceTags []types.Tag
expectedTags []types.Tag
}{
{
name: "ignore all",
ignore: &ecspresso.ConfigIgnore{Tags: []string{"foo", "bar", "baz"}},
resourceTags: []types.Tag{
{Key: ptr("foo"), Value: ptr("x")},
{Key: ptr("bar"), Value: ptr("y")},
{Key: ptr("baz"), Value: ptr("z")},
},
expectedTags: []types.Tag{},
},
{
name: "ignore some",
ignore: &ecspresso.ConfigIgnore{Tags: []string{"foo", "bar"}},
resourceTags: []types.Tag{
{Key: ptr("foo"), Value: ptr("x")},
{Key: ptr("bar"), Value: ptr("y")},
{Key: ptr("baz"), Value: ptr("z")},
},
expectedTags: []types.Tag{
{Key: ptr("baz"), Value: ptr("z")},
},
},
{
name: "ignore nil",
ignore: nil,
resourceTags: []types.Tag{
{Key: ptr("foo"), Value: ptr("x")},
{Key: ptr("bar"), Value: ptr("y")},
},
expectedTags: []types.Tag{
{Key: ptr("foo"), Value: ptr("x")},
{Key: ptr("bar"), Value: ptr("y")},
},
},
{
name: "ignore case sensitive",
ignore: &ecspresso.ConfigIgnore{Tags: []string{"Foo"}},
resourceTags: []types.Tag{
{Key: ptr("foo"), Value: ptr("x")},
{Key: ptr("Foo"), Value: ptr("y")},
},
expectedTags: []types.Tag{
{Key: ptr("foo"), Value: ptr("x")},
},
},
}
func TestConfigIgnore(t *testing.T) {
opt := cmpopts.IgnoreUnexported(types.Tag{})
for _, tt := range ConfigIgnoreTests {
t.Run(tt.name, func(t *testing.T) {
tags := tt.ignore.FilterTags(tt.resourceTags)
if diff := cmp.Diff(tt.expectedTags, tags, opt); diff != "" {
t.Errorf("unexpected tags (-want +got):\n%s", diff)
}
})
}
}