forked from Songmu/ecschedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
159 lines (142 loc) · 3.69 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
package ecschedule
import (
"context"
"os"
"reflect"
"testing"
"text/template"
"github.com/aws/aws-sdk-go/aws"
)
func TestLoadConfig(t *testing.T) {
path := "testdata/sample.yaml"
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c, err := LoadConfig(context.Background(), f, "334", path)
if err != nil {
t.Errorf("error shoud be nil, but: %s", err)
}
expect := &Config{
Role: "ecsEventsRole",
BaseConfig: &BaseConfig{
Region: "us-east-1",
Cluster: "api",
AccountID: "334",
},
Rules: []*Rule{
{
Name: "hoge-task-name",
Description: "hoge description",
ScheduleExpression: "cron(0 0 * * ? *)",
Disabled: false,
Target: &Target{
TargetID: "",
TaskDefinition: "task1",
TaskCount: 0,
Group: "xxx",
PlatformVersion: "1.4.0",
LaunchType: "FARGATE",
NetworkConfiguration: &NetworkConfiguration{
AwsVpcConfiguration: &AwsVpcConfiguration{
Subnets: []string{"subnet-01234567", "subnet-12345678"},
SecurityGroups: []string{"sg-11111111", "sg-99999999"},
AssinPublicIP: "ENABLED",
},
},
ContainerOverrides: []*ContainerOverride{
{
Name: "container1",
Command: []string{
"subcmd",
"argument",
},
Environment: map[string]string{
"HOGE_ENV": "HOGEGE",
},
},
},
DeadLetterConfig: &DeadLetterConfig{
Sqs: "queue1",
},
PropagateTags: aws.String("TASK_DEFINITION"),
Role: "ecsEventsRole",
},
BaseConfig: &BaseConfig{
Region: "us-east-1",
Cluster: "api",
AccountID: "334",
},
},
},
Plugins: []*Plugin(nil),
templateFuncs: []template.FuncMap(nil),
dir: "testdata",
}
if !reflect.DeepEqual(c, expect) {
t.Errorf("unexpected output: %#v", c)
}
}
func TestLoadConfig_mustEnv(t *testing.T) {
path := "testdata/sample2.yaml"
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c, err := LoadConfig(context.Background(), f, "335", path)
if err != nil {
t.Errorf("error shoud be nil, but: %s", err)
}
ru := c.GetRuleByName("hoge-task-name")
err = ru.validateEnv()
if err == nil {
t.Errorf("error should be occurred but nil")
}
if g, e := err.Error(), "environment variable DUMMY_HOGE_ENV is not defined"; g != e {
t.Errorf("error shoud be %q, but: %q", e, g)
}
}
func TestLoadConfig_tfstate(t *testing.T) {
path := "testdata/sample3.yaml"
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c, err := LoadConfig(context.Background(), f, "336", path)
if err != nil {
t.Errorf("error shoud be nil, but: %s", err)
}
if !reflect.DeepEqual(c.Plugins, []*Plugin{
{Name: "tfstate", Config: map[string]interface{}{"path": "testdata/terraform.tfstate"}},
}) {
t.Errorf("unexpected output: %#v", c)
}
as := c.Rules[0].NetworkConfiguration.AwsVpcConfiguration.Subnets
es := []string{"subnet-01234567", "subnet-12345678"}
if !reflect.DeepEqual(as, es) {
t.Errorf("error shoud be %v, but: %v", as, es)
}
asg := c.Rules[0].NetworkConfiguration.AwsVpcConfiguration.SecurityGroups
esg := []string{"sg-11111111", "sg-99999999"}
if !reflect.DeepEqual(asg, esg) {
t.Errorf("error shoud be %v, but: %v", asg, esg)
}
}
func TestLoadConfig_undefined(t *testing.T) {
path := "testdata/sample4.yaml"
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c, err := LoadConfig(context.Background(), f, "337", path)
if err != nil {
t.Errorf("error shoud be nil, but: %s", err)
}
if c.Rules[0].PropagateTags != nil {
t.Errorf("error shoud be nil, but: %v", c.Rules[0].PropagateTags)
}
}