-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathyaml_suite_test.go
More file actions
420 lines (357 loc) · 10.4 KB
/
yaml_suite_test.go
File metadata and controls
420 lines (357 loc) · 10.4 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package suite
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/commander-cli/commander/v2/pkg/runtime"
)
const ExpectedLineCount = 10
func TestYAMLConfig_UnmarshalYAML(t *testing.T) {
yaml := []byte(`
tests:
it should print hello:
command: echo hello
exit-code: 0
stdout: hello
stderr: anything
`)
got := ParseYAML(yaml, "")
tests := got.GetTests()
assert.Len(t, tests, 1)
assert.Equal(t, "echo hello", tests[0].Command.Cmd)
assert.Equal(t, 0, tests[0].Expected.ExitCode)
assert.Equal(t, "it should print hello", tests[0].Title)
assert.Equal(t, "hello", tests[0].Expected.Stdout.Contains[0])
assert.Equal(t, "anything", tests[0].Expected.Stderr.Contains[0])
}
func TestYAMLConfig_UnmarshalYAML_ShouldUseTitleAsCommand(t *testing.T) {
yaml := []byte(`
tests:
echo hello:
exit-code: 0
stdout: hello
stderr: anything
`)
tests := ParseYAML(yaml, "").GetTests()
assert.Equal(t, "echo hello", tests[0].Command.Cmd)
assert.Equal(t, "echo hello", tests[0].Title)
}
func TestYAMLConfig_UnmarshalYAML_ShouldParseLineCount(t *testing.T) {
yaml := []byte(`
tests:
echo hello:
exit-code: 0
stdout:
line-count: 10
`)
tests := ParseYAML(yaml, "").GetTests()
assert.Equal(t, ExpectedLineCount, tests[0].Expected.Stdout.LineCount)
}
func TestYAMLConfig_UnmarshalYAML_ShouldParseLines(t *testing.T) {
yaml := []byte(`
tests:
printf "line1\nline2\nline3\nline4":
exit-code: 0
stdout:
lines:
0: line1
1: line2
3: line4
`)
tests := ParseYAML(yaml, "").GetTests()
assert.Equal(t, "line1", tests[0].Expected.Stdout.Lines[0])
assert.Equal(t, "line2", tests[0].Expected.Stdout.Lines[1])
assert.Equal(t, "line4", tests[0].Expected.Stdout.Lines[3])
}
func TestYAMLConfig_UnmarshalYAML_ShouldDisable(t *testing.T) {
yaml := []byte(`
tests:
it should print hello:
command: echo hello
exit-code: 0
stdout: hello
stderr: anything
skip: true
`)
got := ParseYAML(yaml, "")
tests := got.GetTests()
assert.Len(t, tests, 1)
assert.Equal(t, "echo hello", tests[0].Command.Cmd)
assert.Equal(t, 0, tests[0].Expected.ExitCode)
assert.Equal(t, "it should print hello", tests[0].Title)
assert.Equal(t, "hello", tests[0].Expected.Stdout.Contains[0])
assert.Equal(t, "anything", tests[0].Expected.Stderr.Contains[0])
assert.True(t, tests[0].Skip)
}
func TestYAMLConfig_UnmarshalYAML_ShouldConvertStdoutToExpectedOut(t *testing.T) {
yaml := []byte(`
tests:
echo hello:
exit-code: 0
stdout:
contains:
- hello
- another hello
not-contains:
- bonjour
exactly: exactly hello
json:
$.object.attr: jsontest
`)
tests := ParseYAML(yaml, "").GetTests()
assert.Equal(t, "hello", tests[0].Expected.Stdout.Contains[0])
assert.Equal(t, "exactly hello", tests[0].Expected.Stdout.Exactly)
assert.Equal(t, "bonjour", tests[0].Expected.Stdout.NotContains[0])
assert.Equal(t, "jsontest", tests[0].Expected.Stdout.JSON["$.object.attr"])
}
func TestYAMLConfig_UnmarshalYAML_ShouldConvertWithoutContains(t *testing.T) {
yaml := []byte(`
tests:
echo hello:
exit-code: 0
stderr:
exactly: exactly stderr
`)
tests := ParseYAML(yaml, "").GetTests()
assert.Equal(t, "exactly stderr", tests[0].Expected.Stderr.Exactly)
assert.IsType(t, runtime.ExpectedOut{}, tests[0].Expected.Stdout)
}
func Test_YAMLConfig_convertToExpectedOut(t *testing.T) {
in := map[interface{}]interface{}{"exactly": "exactly stderr"}
y := YAMLSuiteConf{}
got := y.convertToExpectedOut(in)
assert.IsType(t, runtime.ExpectedOut{}, got)
assert.Equal(t, "exactly stderr", got.Exactly)
}
func TestYAMLConfig_UnmarshalYAML_ShouldPanicIfKeyDoesNotExist(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Unknown keys should not be parsed, the program should panic")
}
}()
yaml := []byte(`
tests:
echo hello:
exit-code: 0
stderr:
typo: exactly stderr
`)
_ = ParseYAML(yaml, "")
}
func TestYAMLSuite_GetTestByTitle(t *testing.T) {
yaml := []byte(`
tests:
echo hello:
exit-code: 0
`)
test, err := ParseYAML(yaml, "").GetTestByTitle("echo hello")
assert.Nil(t, err)
assert.Equal(t, "echo hello", test.Title)
}
func TestYAMLSuite_GetTestByTitleShouldReturnError(t *testing.T) {
yaml := []byte(`
tests:
echo hello:
exit-code: 0
`)
_, err := ParseYAML(yaml, "").GetTestByTitle("does not exist")
assert.Equal(t, "could not find test does not exist", err.Error())
}
func TestYAMLSuite_ShouldParseGlobalConfig(t *testing.T) {
yaml := []byte(`
config:
env:
KEY: value
dir: /home/commander/
tests:
echo hello:
exit-code: 0
`)
got := NewSuite(yaml, nil, "")
assert.Equal(t, map[string]string{"KEY": "value"}, got.GetGlobalConfig().Env)
assert.Equal(t, map[string]string{"KEY": "value"}, got.GetTests()[0].Command.Env)
assert.Equal(t, "/home/commander/", got.GetTests()[0].Command.Dir)
assert.Equal(t, "/home/commander/", got.GetGlobalConfig().Dir)
}
func TestYAMLSuite_ShouldPreferLocalTestConfigs(t *testing.T) {
yaml := []byte(`
config:
env:
KEY: global
ANOTHER_KEY: another_global
dir: /home/commander/
timeout: 10ms
retries: 2
interval: 500ms
inherit-env: true
tests:
echo hello:
exit-code: 0
config:
env:
KEY: local
dir: /home/test
timeout: 1s
retries: 10
interval: 5s
`)
got := NewSuite(yaml, nil, "")
// Assert global variables
assert.Equal(t, map[string]string{"KEY": "global", "ANOTHER_KEY": "another_global"}, got.GetGlobalConfig().Env)
assert.Equal(t, "/home/commander/", got.GetGlobalConfig().Dir)
assert.Equal(t, "10ms", got.GetGlobalConfig().Timeout)
assert.Equal(t, 2, got.GetGlobalConfig().Retries)
assert.Equal(t, "500ms", got.GetGlobalConfig().Interval)
assert.True(t, got.GetGlobalConfig().InheritEnv)
// Assert local variables
assert.Equal(t, map[string]string{"KEY": "local", "ANOTHER_KEY": "another_global"}, got.GetTests()[0].Command.Env)
assert.Equal(t, "/home/test", got.GetTests()[0].Command.Dir)
assert.Equal(t, "1s", got.GetTests()[0].Command.Timeout)
assert.Equal(t, 10, got.GetTests()[0].Command.Retries)
assert.Equal(t, "5s", got.GetTests()[0].Command.Interval)
assert.True(t, got.GetTests()[0].Command.InheritEnv)
}
func TestYAMLSuite_OverwriteConfigContext(t *testing.T) {
yaml := []byte(`
config:
env:
KEY: value
dir: /home/commander/
tests:
echo hello:
exit-code: 0
`)
global := []byte(`
config:
env:
OVERWRITE_KEY: overwrite_key
retries: 2
dir: /do/not/override/
`)
got := NewSuite(yaml, global, "")
assert.Equal(t, map[string]string{"KEY": "value", "OVERWRITE_KEY": "overwrite_key"}, got.GetTests()[0].Command.Env)
assert.Equal(t, "/home/commander/", got.GetTests()[0].Command.Dir)
assert.Equal(t, 2, got.GetTests()[0].Command.Retries)
assert.Equal(t, "overwrite_key", got.GetTests()[0].Command.Env["OVERWRITE_KEY"])
}
func TestYAMLSuite_ShouldThrowAnErrorIfFieldIsNotRegistered(t *testing.T) {
defer func() {
r := recover()
if r != nil {
assert.Contains(t, r, "field stdot not found in type suite.YAMLTest")
}
assert.NotNil(t, r)
}()
yaml := []byte(`
tests:
echo hello:
stdot: yeah
`)
_ = ParseYAML(yaml, "")
}
func TestYamlSuite_ShouldFailIfArrayIsGivenToExpectedOut(t *testing.T) {
defer func() {
r := recover()
if r != nil {
assert.Contains(t, r, "Failed to parse Stdout or Stderr with values: [yeah]")
}
assert.NotNil(t, r)
}()
yaml := []byte(`
tests:
echo hello:
stdout:
- yeah
`)
_ = ParseYAML(yaml, "")
}
func Test_YAMLConfig_MarshalYAML(t *testing.T) {
conf := YAMLSuiteConf{Tests: map[string]YAMLTest{
"return_string": {
Stdout: runtime.ExpectedOut{Contains: []string{"stdout string"}},
Stderr: runtime.ExpectedOut{Contains: []string{"stderr string"}},
},
"return_struct": {
Stdout: runtime.ExpectedOut{
Contains: []string{"stdout"},
LineCount: 10,
},
Stderr: runtime.ExpectedOut{
Contains: []string{"stderr"},
LineCount: 10,
},
},
"return_nil": {
Stdout: runtime.ExpectedOut{},
Stderr: runtime.ExpectedOut{},
},
}}
out, _ := conf.MarshalYAML()
r := out.(YAMLSuiteConf)
assert.Equal(t, "stdout string", r.Tests["return_string"].Stdout)
assert.Equal(t, "stderr string", r.Tests["return_string"].Stderr)
assert.Equal(t, conf.Tests["return_struct"].Stdout, r.Tests["return_struct"].Stdout)
assert.Equal(t, conf.Tests["return_struct"].Stderr, r.Tests["return_struct"].Stderr)
assert.Nil(t, r.Tests["return_nil"].Stdout)
assert.Nil(t, r.Tests["return_nil"].Stderr)
}
func Test_convertExpectOut_ReturnNilIfEmpty(t *testing.T) {
out := runtime.ExpectedOut{
Contains: []string{""},
}
r := convertExpectedOut(out)
assert.Nil(t, r)
}
func Test_convertExpectedOut_ReturnContainsAsString(t *testing.T) {
out := runtime.ExpectedOut{
Contains: []string{"test"},
}
r := convertExpectedOut(out)
assert.Equal(t, "test", r)
}
func Test_convertExpectedOut_ReturnFullStruct(t *testing.T) {
out := runtime.ExpectedOut{
Contains: []string{"hello", "hi"},
LineCount: 10,
Exactly: "test",
}
r := convertExpectedOut(out)
assert.Equal(t, out, r)
}
func TestYAMLSuite_should_parse_ssh(t *testing.T) {
yaml := []byte(`
nodes:
ssh-host1:
type: ssh
addr: localhost
user: root
pass: 12345!
identity-file: ".ssh/id_rsa"
docker-host:
type: docker
image: ubuntu:18.04
privileged: true
tests:
echo hello:
config:
nodes:
- docker-host
- ssh-host1
exit-code: 0
`)
got := ParseYAML(yaml, "")
assert.Len(t, got.GetNodes(), 2)
node, err := got.GetNodeByName("ssh-host1")
assert.Nil(t, err)
assert.Equal(t, "ssh-host1", node.Name)
assert.Equal(t, "localhost", node.Addr)
assert.Equal(t, "root", node.User)
assert.Equal(t, "12345!", node.Pass)
assert.Equal(t, "ssh", node.Type)
assert.Equal(t, ".ssh/id_rsa", node.IdentityFile)
dockerNode, _ := got.GetNodeByName("docker-host")
assert.Equal(t, "ubuntu:18.04", dockerNode.Image)
assert.Equal(t, "docker", dockerNode.Type)
assert.True(t, dockerNode.Privileged)
assert.Contains(t, got.GetTests()[0].Nodes, "docker-host")
assert.Contains(t, got.GetTests()[0].Nodes, "ssh-host1")
}