-
Notifications
You must be signed in to change notification settings - Fork 2
/
conf_test.go
334 lines (274 loc) · 8.23 KB
/
conf_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
// Package mute implements functions to execute other programs muting std streams if required
// license: MIT, see LICENSE for details.
package mute
import (
"os"
"testing"
)
func TestCodesContain(t *testing.T) {
haystack := []int{1, 2}
if !codesContain(haystack, 1) {
t.Errorf("codesContain got 'false' want 'true'")
}
if codesContain(haystack, 3) {
t.Errorf("codesContain got 'true' want 'false'")
}
}
func TestStdoutPatternsContain(t *testing.T) {
stdp1 := NewStdoutPattern("hi")
stdp2 := NewStdoutPattern(".+not[1-9]+so.*simple")
stdp3 := NewStdoutPattern("I was 3rd")
stdp4 := NewStdoutPattern(".+not[1-9]+so.*close")
haystack := []*StdoutPattern{stdp1, stdp2, stdp3}
if !stdoutPatternsContain(haystack, stdp2) {
t.Errorf("stdoutPatternsContain got 'false' want 'true'")
}
if stdoutPatternsContain(haystack, stdp4) {
t.Errorf("stdoutPatternsContain got 'true' want 'false'")
}
}
func TestCriterionEmpty(t *testing.T) {
c1 := NewCriterion([]int{0}, []string{})
c2 := NewCriterion([]int{}, []string{""})
c3 := NewCriterion([]int{}, []string{})
if c1.IsEmpty() {
t.Errorf("Criterion with exit codes IsEmpty got 'true' want 'false'")
}
if c2.IsEmpty() {
t.Errorf("Criterion with patterns IsEmpty got 'true' want 'false'")
}
if !c3.IsEmpty() {
t.Errorf("Empty Criterion.IsEmpty 'false' want 'true'")
}
}
func TestCriterionEqual(t *testing.T) {
c1 := NewCriterion([]int{0, 1, 2}, []string{})
c2 := NewCriterion([]int{0, 1, 2}, []string{})
if !c1.equal(c2) {
t.Errorf("Criterion.equal got 'false' want 'true'")
}
c2 = NewCriterion([]int{0, 2}, []string{})
if c1.equal(c2) {
t.Errorf("Criterion.equal unmatched codes got 'true' want 'false'")
}
c2 = NewCriterion([]int{0, 1, 2}, []string{"ok"})
if c1.equal(c2) {
t.Errorf("Criterion.equal unmatched patterns got 'true' want 'false'")
}
}
func TestCriterionString(t *testing.T) {
var want, got string
var c1 *Criterion
c1 = NewCriterion([]int{0}, []string{})
want = "<Criterion codes=\"0\" patterns_count=\"0\">"
got = c1.String()
if want != got {
t.Errorf("Criterion String failed. want: '%v' got '%v'", want, got)
}
c1 = NewCriterion([]int{0}, []string{"OK"})
want = "<Criterion codes=\"0\" patterns_count=\"1\">"
got = c1.String()
if want != got {
t.Errorf("Criterion String failed. want: '%v' got '%v'", want, got)
}
}
func TestCriteriaEqual(t *testing.T) {
c1 := NewCriterion([]int{0, 1, 2}, []string{})
c2 := NewCriterion([]int{0, 1, 2}, []string{})
criteria1 := new(Criteria)
criteria1.add(c1, c2)
criteria2 := new(Criteria)
criteria2.add(c1, c2)
if !c1.equal(c2) {
t.Errorf("Criteria.equal got 'false' want 'true'")
}
c3 := NewCriterion([]int{0, 1, 2}, []string{})
criteria2.add(c3)
if !c1.equal(c2) {
t.Errorf("Criteria.equal got 'true' want 'false'")
}
}
func TestDefaultConf(t *testing.T) {
c1 := NewCriterion([]int{0}, []string{})
crt1 := new(Criteria)
crt1.add(c1)
conf := DefaultConf()
if !conf.Default.equal(crt1) {
t.Errorf("DefaultConf().Default didn't match zero exit code")
}
}
func TestConfEqual(t *testing.T) {
crt := NewCriterion([]int{1, 2}, []string{"OK"})
empty1 := new(Conf)
empty2 := new(Conf)
simple1 := createSimpleConf()
simple2 := createSimpleConf()
cmd1 := createSimpleConf()
cmd1.Commands["/usr/local/bin/mute"] = Criteria{crt}
cmd2 := createSimpleConf()
cmd2.Commands["/usr/local/bin/mute"] = Criteria{crt}
if !empty1.equal(empty2) {
t.Errorf("Conf empty1 should be equal to empty2")
}
if !simple1.equal(simple2) {
t.Errorf("Conf simple should be equal to simple2")
}
if empty1.equal(simple1) {
t.Errorf("Conf empty should not be equal to simple")
}
if !cmd1.equal(cmd2) {
t.Errorf("Conf cmd1 should be equal to cmd2")
}
cmd2.Commands["test"] = Criteria{crt}
if cmd1.equal(cmd2) {
t.Errorf("Conf cmd1 should not be equal to cmd2 with extra command")
}
}
func TestReadConfFileError(t *testing.T) {
_, err := ReadConfFile("test/data/no_such_file.toml")
if err == nil {
t.Errorf("ReadConfFile should have returned error")
}
if _, ok := err.(ConfAccessError); !ok {
t.Errorf("ReadConfFile should have returned ConfAccessError")
}
}
func TestReadConfFileSimple(t *testing.T) {
c1 := NewCriterion([]int{0}, []string{})
c2 := NewCriterion([]int{1, 2}, []string{"OK"})
want := new(Conf)
want.Default.add(c1).add(c2)
got, err := ReadConfFile("test/data/simple.toml")
if err != nil {
t.Errorf("ReadConfFile had error: %v", err)
}
if !want.equal(got) {
t.Errorf("ReadConfFile simple didn't match want %v got %v", want, got)
}
c3 := NewCriterion([]int{1}, []string{})
extraCodesConf := new(Conf)
extraCodesConf.Default.add(c1, c3)
if extraCodesConf.equal(got) {
t.Errorf("ReadConfFile simple matched extra codes conf")
}
missingCodesConf := new(Conf)
missingCodesConf.Default.add(c1)
if missingCodesConf.equal(got) {
t.Errorf("ReadConfFile simple matched missing codes conf")
}
}
func TestConfFromEnvStr(t *testing.T) {
var got, want, defaultConf *Conf
var err error
defaultConf = DefaultConf()
got, err = ConfFromEnvStr("", "")
if err != nil {
t.Errorf("ConfFromEnvStr empty want no error, got: %v", err)
}
if !got.IsEmpty() {
t.Errorf("ConfFromEnvStr want empty conf, got: %v", got)
}
got, err = ConfFromEnvStr("0", "")
if err != nil {
t.Errorf("ConfFromEnvStr default want no error, got: %v", err)
}
if !defaultConf.equal(got) {
t.Errorf("ConfFromEnvStr want default conf, got: %v", got)
}
want = new(Conf)
c1 := NewCriterion([]int{1, 2}, []string{"[0-9]test"})
want.Default.add(c1)
got, err = ConfFromEnvStr("1,2", "[0-9]test")
if err != nil {
t.Errorf("ConfFromEnvStr test want no error, got: %v", err)
}
if !want.equal(got) {
t.Errorf("ConfFromEnvStr test want: %v, got: %v", want, got)
}
}
func TestGetCmdConfFromEnv(t *testing.T) {
var got, want *Conf
var err error
os.Setenv(EnvConfig, "test/data/simple.toml")
defer os.Unsetenv(EnvConfig)
os.Unsetenv(EnvExitCodes)
os.Unsetenv(EnvStdoutPattern)
want = createSimpleConf()
got, err = GetCmdConf()
if err != nil {
t.Errorf("GetCmdConf no env conf want no error, got: %v", err)
}
if !want.equal(got) {
t.Errorf("GetCmdConf no env conf want simple %v got %v", want, got)
}
os.Setenv(EnvExitCodes, "")
defer os.Unsetenv(EnvExitCodes)
os.Setenv(EnvStdoutPattern, "")
defer os.Unsetenv(EnvStdoutPattern)
got, err = GetCmdConf()
if err != nil {
t.Errorf("GetCmdConf empty env conf want no error, got: %v", err)
}
if !want.equal(got) {
t.Errorf("GetCmdConf empty env conf want simple %v got %v", want, got)
}
c1 := NewCriterion([]int{4, 5}, []string{"[0-9]test"})
want = new(Conf)
want.Default.add(c1)
os.Setenv(EnvExitCodes, "4,5")
os.Setenv(EnvStdoutPattern, "[0-9]test")
got, err = GetCmdConf()
if err != nil {
t.Errorf("GetCmdConf env conf want no error, got: %v", err)
}
if !want.equal(got) {
t.Errorf("GetCmdConf env want %v got %v", want, got)
}
os.Setenv(EnvExitCodes, "4,z")
got, err = GetCmdConf()
if err == nil {
t.Errorf("GetCmdConf env inavlid exit code want error, got: %v", got)
}
if !got.IsEmpty() {
t.Errorf("GetCmdConf env invalid exit code want empty conf, got %v", got)
}
os.Setenv(EnvExitCodes, "")
os.Setenv(EnvStdoutPattern, "[")
got, err = GetCmdConf()
if err == nil {
t.Errorf("GetCmdConf env inavlid stdout pattern want error, got: %v", got)
}
if !got.IsEmpty() {
t.Errorf("GetCmdConf env invalid stdout pattern want empty conf, got %v", got)
}
}
func TestGetCmdConfFromFile(t *testing.T) {
os.Setenv(EnvConfig, "test/data/simple.toml")
defer os.Unsetenv(EnvConfig)
want := createSimpleConf()
defaultConf := DefaultConf()
got, err := GetCmdConf()
if err != nil {
t.Errorf("GetCmdConf simple want no error, got: %v", err)
}
if !want.equal(got) {
t.Errorf("GetCmdConf simple conf want simple %v got %v", want, got)
}
os.Setenv(EnvConfig, "")
got, err = GetCmdConf()
if err != nil {
t.Errorf("GetCmdConf empty env want no error, got: %v", err)
}
if !defaultConf.equal(got) {
t.Errorf("GetCmdConf empty env conf want default conf, got %v", got)
}
}
// createSimpleConf returns a Conf with simple criterions for testing
func createSimpleConf() *Conf {
c1 := NewCriterion([]int{0}, []string{})
c2 := NewCriterion([]int{1, 2}, []string{"OK"})
conf := new(Conf)
conf.Default.add(c1).add(c2)
conf.Commands = make(map[string]Criteria)
return conf
}