This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
config_test.go
205 lines (184 loc) · 5.01 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
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"testing"
)
func Test_mergeConfigs(t *testing.T) {
t.Parallel()
a := &Config{
ListenAddr: "localhost:8080",
Verbose: false,
Commands: []*Command{
{Cmd: "echo"},
},
}
b := &Config{
ListenAddr: "localhost:8081",
Verbose: true,
Commands: []*Command{
{Cmd: "/bin/echo"},
},
}
merged := mergeConfigs(a, b)
if merged.ListenAddr != b.ListenAddr {
t.Errorf("Wrong ListenAddr for merged config; got %s, want %s", merged.ListenAddr, b.ListenAddr)
}
if merged.Verbose != b.Verbose {
t.Errorf("Wrong Verbose for merged config; got %v, want %v", merged.Verbose, b.Verbose)
}
allCmds := make([]*Command, 0)
allCmds = append(allCmds, a.Commands...)
allCmds = append(allCmds, b.Commands...)
for _, cmd := range allCmds {
if !merged.HasCommand(cmd) {
t.Errorf("Missing command %#v", cmd)
}
}
yamlFile := `---
listen_address: ":23222"
verbose: false
commands:
- cmd: echo
args: ["banana", "tomato"]
match_labels:
"env": "testing"
"owner": "me"
- cmd: /bin/true
match_labels:
"beep": "boop"
`
yamlConf := &Config{}
err := yaml.Unmarshal([]byte(yamlFile), yamlConf)
if err != nil {
t.Errorf("Failed to unmarshal yaml config file; %v", err)
}
mergedAgain := mergeConfigs(merged, yamlConf)
if mergedAgain.ListenAddr != yamlConf.ListenAddr {
t.Errorf("Wrong ListenAddr for merged config; got %s, want %s", mergedAgain.ListenAddr, yamlConf.ListenAddr)
}
if mergedAgain.Verbose != (merged.Verbose || yamlConf.Verbose) {
t.Errorf("Wrong Verbose for merged config; got %v, want %v", mergedAgain.Verbose, (merged.Verbose || yamlConf.Verbose))
}
allCmds = append(allCmds, yamlConf.Commands...)
for _, cmd := range allCmds {
if !mergedAgain.HasCommand(cmd) {
t.Errorf("Missing command %#v", cmd)
}
}
}
func Test_readConfigFile(t *testing.T) {
tempfile, err := ioutil.TempFile("", "am-executor_readConfigFile-*.yml")
if err != nil {
t.Error(err)
}
defer func() {
_ = os.Remove(tempfile.Name())
}()
yamlFile := `---
listen_address: ":23222"
verbose: true
commands:
- cmd: echo
args: ["banana", "tomato"]
match_labels:
"env": "testing"
"owner": "me"
notify_on_failure: false
resolved_signal: sigusr2
- cmd: /bin/true
match_labels:
"beep": "boop"
ignore_resolved: true
`
_, err = tempfile.Write([]byte(yamlFile))
if err != nil {
t.Fatal(err)
}
err = tempfile.Close()
if err != nil {
t.Fatal(err)
}
c, err := readConfigFile(tempfile.Name())
if err != nil {
t.Fatalf("Failed to read configuration file from %s: %v", tempfile.Name(), err)
}
if c.ListenAddr != ":23222" {
t.Errorf("Wrong ListenAddr; got %s, want %s", c.ListenAddr, ":23222")
}
if c.Verbose != true {
t.Errorf("Wrong Verbose value; got %v, want %v", c.Verbose, true)
}
if len(c.Commands) != 2 {
t.Errorf("Wrong number of commands defined; got %d, want %d", len(c.Commands), 2)
}
// We can create pointers to variables, but not to primitive values like true/false directly.
var alsoTrue = true
var alsoFalse = false
var cases = []struct {
cmd *Command
shouldNotify bool
shouldIgnoreResolved bool
}{
{
cmd: &Command{
Cmd: "echo",
Args: []string{"banana", "tomato"},
MatchLabels: map[string]string{
"env": "testing",
"owner": "me",
},
NotifyOnFailure: &alsoFalse,
ResolvedSig: "sigusr2",
},
shouldNotify: false,
shouldIgnoreResolved: false,
},
{
cmd: &Command{
Cmd: "/bin/true",
MatchLabels: map[string]string{
"beep": "boop",
},
IgnoreResolved: &alsoTrue,
},
shouldNotify: true,
shouldIgnoreResolved: true,
},
}
for i, tc := range cases {
if !c.Commands[i].Equal(tc.cmd) {
t.Errorf("Commands not equal; %q and %q", c.Commands[i], tc.cmd.String())
}
if c.Commands[i].ShouldNotify() != tc.shouldNotify {
t.Errorf("Wrong NotifyOnFailure value for %q; got %v, want %v", c.Commands[i].String(), c.Commands[i].ShouldNotify(), tc.shouldNotify)
}
if c.Commands[i].ShouldIgnoreResolved() != tc.shouldIgnoreResolved {
t.Errorf("Wrong IgnoreResolved value for %q; got %v, want %v", c.Commands[i].String(), c.Commands[i].ShouldIgnoreResolved(), tc.shouldIgnoreResolved)
}
if c.Commands[i].ResolvedSig != tc.cmd.ResolvedSig {
t.Errorf("Wrong ResolvedSig value for %q; got %s, want %s", c.Commands[i].String(), c.Commands[i].ResolvedSig, tc.cmd.ResolvedSig)
}
_, err := c.Commands[i].ParseSignal()
if err != nil {
t.Fatalf("Failed to convert command %q ResolvedSig value %s to signal: %v", c.Commands[i].String(), c.Commands[i].ResolvedSig, err)
}
}
}
func TestConfig_HasCommand(t *testing.T) {
t.Parallel()
a := &Command{
Cmd: "echo",
Args: []string{"banana", "lemon"},
MatchLabels: map[string]string{"env": "test", "owner": "me"},
}
c := Config{}
if c.HasCommand(a) {
t.Errorf("Config should not have command")
}
c.Commands = append(c.Commands, a)
if !c.HasCommand(a) {
t.Errorf("Config should have command")
}
}