forked from geofffranks/spruce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
621 lines (595 loc) · 16.5 KB
/
main_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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package main
import (
"fmt"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestParseYAML(t *testing.T) {
Convey("parseYAML()", t, func() {
Convey("returns error for invalid yaml data", func() {
data := `
asdf: fdsa
- asdf: fdsa
`
obj, err := parseYAML([]byte(data))
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "unmarshal []byte to yaml failed:")
So(obj, ShouldBeNil)
})
Convey("returns error if yaml was not a top level map", func() {
data := `
- 1
- 2
`
obj, err := parseYAML([]byte(data))
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Root of YAML document is not a hash/map:")
So(obj, ShouldBeNil)
})
Convey("returns expected datastructure from valid yaml", func() {
data := `
top:
subarray:
- one
- two
`
obj, err := parseYAML([]byte(data))
expect := map[interface{}]interface{}{
"top": map[interface{}]interface{}{
"subarray": []interface{}{"one", "two"},
},
}
So(obj, ShouldResemble, expect)
So(err, ShouldBeNil)
})
})
}
func TestMergeAllDocs(t *testing.T) {
Convey("mergeAllDocs()", t, func() {
Convey("Fails with readFile error on bad first doc", func() {
target := map[interface{}]interface{}{}
err := mergeAllDocs(target, []string{"assets/merge/nonexistent.yml", "assets/merge/second.yml"})
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Error reading file assets/merge/nonexistent.yml:")
})
Convey("Fails with parseYAML error on bad second doc", func() {
target := map[interface{}]interface{}{}
err := mergeAllDocs(target, []string{"assets/merge/first.yml", "assets/merge/bad.yml"})
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "assets/merge/bad.yml: Root of YAML document is not a hash/map:")
})
Convey("Fails with mergeMap error", func() {
target := map[interface{}]interface{}{}
err := mergeAllDocs(target, []string{"assets/merge/first.yml", "assets/merge/error.yml"})
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "$.array_inline.0: new object is a string, not a map - cannot merge using keys")
})
Convey("Succeeds with valid files + yaml", func() {
target := map[interface{}]interface{}{}
expect := map[interface{}]interface{}{
"key": "overridden",
"array_append": []interface{}{"one", "two", "three"},
"array_prepend": []interface{}{"three", "four", "five"},
"array_replace": []interface{}{[]interface{}{1, 2, 3}},
"array_inline": []interface{}{
map[interface{}]interface{}{"name": "first_elem", "val": "overwritten"},
"second_elem was overwritten",
"third elem is appended",
},
"array_default": []interface{}{
"FIRST",
"SECOND",
"third",
},
"array_map_default": []interface{}{
map[interface{}]interface{}{
"name": "AAA",
"k1": "key 1",
"k2": "updated",
},
map[interface{}]interface{}{
"name": "BBB",
"k2": "final",
"k3": "original",
},
},
"map": map[interface{}]interface{}{
"key": "value",
"key2": "val2",
},
}
err := mergeAllDocs(target, []string{"assets/merge/first.yml", "assets/merge/second.yml"})
So(err, ShouldBeNil)
So(target, ShouldResemble, expect)
})
})
}
func TestMain(t *testing.T) {
Convey("main()", t, func() {
var stdout string
printfStdOut = func(format string, args ...interface{}) {
stdout = fmt.Sprintf(format, args...)
}
var stderr string
printfStdErr = func(format string, args ...interface{}) {
stderr = fmt.Sprintf(format, args...)
}
rc := 256 // invalid return code to catch any issues
exit = func(code int) {
rc = code
}
usage = func() {
stderr = "usage was called"
exit(1)
}
Convey("Should output usage if bad args are passed", func() {
os.Args = []string{"spruce", "fdsafdada"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "usage was called")
So(rc, ShouldEqual, 1)
})
Convey("Should output usage if no args at all", func() {
os.Args = []string{"spruce"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "usage was called")
So(rc, ShouldEqual, 1)
})
Convey("Should output usage if no args to merge", func() {
os.Args = []string{"spruce", "merge"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "usage was called")
So(rc, ShouldEqual, 1)
})
Convey("Should output version", func() {
Convey("When '-v' is specified", func() {
os.Args = []string{"spruce", "-v"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, "")
So(stderr, ShouldEqual, fmt.Sprintf("spruce - Version %s\n", VERSION))
So(rc, ShouldEqual, 0)
})
Convey("When '--version' is specified", func() {
os.Args = []string{"spruce", "--version"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, "")
So(stderr, ShouldEqual, fmt.Sprintf("spruce - Version %s\n", VERSION))
So(rc, ShouldEqual, 0)
})
})
Convey("Should panic on errors merging docs", func() {
os.Args = []string{"spruce", "merge", "assets/merge/bad.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldContainSubstring, "assets/merge/bad.yml: Root of YAML document is not a hash/map:")
So(rc, ShouldEqual, 2)
})
/* Fixme - how to trigger this?
Convey("Should panic on errors marshalling yaml", func () {
})
*/
Convey("Should output merged yaml on success", func() {
os.Args = []string{"spruce", "merge", "assets/merge/first.yml", "assets/merge/second.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, `array_append:
- one
- two
- three
array_default:
- FIRST
- SECOND
- third
array_inline:
- name: first_elem
val: overwritten
- second_elem was overwritten
- third elem is appended
array_map_default:
- k1: key 1
k2: updated
name: AAA
- k2: final
k3: original
name: BBB
array_prepend:
- three
- four
- five
array_replace:
- - 1
- 2
- 3
key: overridden
map:
key: value
key2: val2
`)
So(stderr, ShouldEqual, "")
})
Convey("Should not fail when handling concourse-style yaml and --concourse", func() {
os.Args = []string{"spruce", "--concourse", "merge", "assets/concourse/first.yml", "assets/concourse/second.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, `jobs:
- curlies: {{my-variable_123}}
name: thing1
- curlies: {{more}}
name: thing2
`)
So(stderr, ShouldEqual, "")
})
Convey("Should handle de-referencing", func() {
os.Args = []string{"spruce", "merge", "assets/dereference/first.yml", "assets/dereference/second.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, `jobs:
- name: my-server
static_ips:
- 192.168.1.0
properties:
client:
servers:
- 192.168.1.0
`)
So(stderr, ShouldEqual, "")
})
Convey("De-referencing cyclical datastructures should throw an error", func() {
os.Args = []string{"spruce", "merge", "assets/dereference/cyclic-data.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, "")
So(stderr, ShouldContainSubstring, "hit max recursion depth. You seem to have a self-referencing dataset\n")
So(rc, ShouldEqual, 2)
})
Convey("Dereferencing multiple values should behave as desired", func() {
os.Args = []string{"spruce", "merge", "assets/dereference/multi-value.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, `jobs:
- instances: 1
name: api_z1
networks:
- name: net1
static_ips:
- 192.168.1.2
- instances: 1
name: api_z2
networks:
- name: net2
static_ips:
- 192.168.2.2
networks:
- name: net1
subnets:
- cloud_properties: random
static:
- 192.168.1.2 - 192.168.1.30
- name: net2
subnets:
- cloud_properties: random
static:
- 192.168.2.2 - 192.168.2.30
properties:
api_server_primary: 192.168.1.2
api_servers:
- 192.168.1.2
- 192.168.2.2
`)
So(stderr, ShouldEqual, "")
})
Convey("Should output error on bad de-reference", func() {
os.Args = []string{"spruce", "merge", "assets/dereference/bad.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldContainSubstring, "$.bad.dereference: Unable to resolve `my.value`")
So(rc, ShouldEqual, 2)
})
Convey("Pruning should happen after de-referencing", func() {
os.Args = []string{"spruce", "merge", "--prune", "jobs", "--prune", "properties.client.servers", "assets/dereference/first.yml", "assets/dereference/second.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `properties:
client: {}
`)
})
Convey("can dereference ~ / null values", func() {
os.Args = []string{"spruce", "merge", "--prune", "meta", "assets/dereference/null.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `value: null
`)
})
Convey("can dereference nestedly", func() {
os.Args = []string{"spruce", "merge", "assets/dereference/multi.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `name1: name
name2: name
name3: name
name4: name
`)
})
Convey("static_ips() failures return errors to the user", func() {
os.Args = []string{"spruce", "merge", "assets/static_ips/jobs.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldContainSubstring, "$.jobs.api_z1.networks.net1.static_ips: `$.networks` could not be found in the YAML datastructure\n")
So(stdout, ShouldEqual, "")
})
Convey("static_ips() get resolved, and are resolved prior to dereferencing", func() {
os.Args = []string{"spruce", "merge", "assets/static_ips/properties.yml", "assets/static_ips/jobs.yml", "assets/static_ips/network.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `jobs:
- instances: 3
name: api_z1
networks:
- name: net1
static_ips:
- 10.0.0.2
- 10.0.0.3
- 10.0.0.4
networks:
- name: net1
subnets:
- static:
- 10.0.0.2 - 10.0.0.20
properties:
api_servers:
- 10.0.0.2
- 10.0.0.3
- 10.0.0.4
`)
})
Convey("Parameters override their requirement", func() {
os.Args = []string{"spruce", "merge", "assets/params/global.yml", "assets/params/good.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, `cpu: 3
nested:
key:
override: true
networks:
- true
storage: 4096
`)
So(stderr, ShouldEqual, "")
})
Convey("Parameters must be specified", func() {
os.Args = []string{"spruce", "merge", "assets/params/global.yml", "assets/params/fail.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, "")
So(stderr, ShouldContainSubstring, "$.nested.key.override: provide nested override\n")
})
Convey("Pruning takes place before parameters", func() {
os.Args = []string{"spruce", "merge", "--prune", "nested", "assets/params/global.yml", "assets/params/fail.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, `cpu: 3
networks: specified
storage: 4096
`)
So(stderr, ShouldEqual, "")
})
Convey("string concatenation works", func() {
os.Args = []string{"spruce", "merge", "--prune", "local", "--prune", "env", "--prune", "cluster", "assets/concat/concat.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `ident: c=mjolnir/prod;1234567890-abcdef
`)
})
Convey("string concatenation handles non-strings correctly", func() {
os.Args = []string{"spruce", "merge", "--prune", "local", "assets/concat/coerce.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `url: http://domain.example.com/?v=1.3&rev=42
`)
})
Convey("string concatenation failure detected", func() {
os.Args = []string{"spruce", "merge", "assets/concat/fail.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldContainSubstring, "$.ident: Unable to resolve `local.sites.[42].uuid`:")
So(stdout, ShouldEqual, "")
})
Convey("string concatentation handles multiple levels of reference", func() {
os.Args = []string{"spruce", "merge", "assets/concat/multi.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `bar: quux.bar
baz: quux.bar.baz
foo: quux.bar.baz.foo
quux: quux
`)
Convey("string concatenation handles infinite loop self-reference", func() {
os.Args = []string{"spruce", "merge", "assets/concat/loop.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldContainSubstring, "possible recursion detected in call to (( concat ))")
So(stdout, ShouldEqual, "")
})
})
Convey("all errors are displayed", func() {
os.Args = []string{"spruce", "merge", "assets/errors/multi.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, "")
So(stderr, ShouldEqual, ""+
"3 error(s) detected:\n"+
" - $.an-error: missing param!\n"+
" - $.another-error: Unable to resolve `meta.enoent`: `meta` could not be found in the YAML datastructure\n"+
" - $.last-problem: Unable to resolve `meta.missing.host`: `meta` could not be found in the YAML datastructure\n"+
"\n\n"+
"")
})
Convey("multiple errors of the same type on the same level are displayed", func() {
os.Args = []string{"spruce", "merge", "assets/errors/multi2.yml"}
stdout = ""
stderr = ""
main()
So(stdout, ShouldEqual, "")
So(stderr, ShouldEqual, ""+
"3 error(s) detected:\n"+
" - $.a: first\n"+
" - $.b: second\n"+
" - $.c: third\n"+
"\n\n"+
"")
})
})
}
func TestDebug(t *testing.T) {
var stderr string
usage = func() {}
printfStdErr = func(format string, args ...interface{}) {
stderr = fmt.Sprintf(format, args...)
}
Convey("debug", t, func() {
Convey("Outputs when debug is set to true", func() {
stderr = ""
debug = true
DEBUG("test debugging")
So(stderr, ShouldEqual, "DEBUG> test debugging\n")
})
Convey("Multi-line debug inputs are each prefixed", func() {
stderr = ""
debug = true
DEBUG("test debugging\nsecond line")
So(stderr, ShouldEqual, "DEBUG> test debugging\nDEBUG> second line\n")
})
Convey("Doesn't output when debug is set to false", func() {
stderr = ""
debug = false
DEBUG("test debugging")
So(stderr, ShouldEqual, "")
})
})
Convey("debug flags:", t, func() {
Convey("-D enables debugging", func() {
os.Args = []string{"spruce", "-D"}
debug = false
main()
So(debug, ShouldBeTrue)
})
Convey("--debug enables debugging", func() {
os.Args = []string{"spruce", "--debug"}
debug = false
main()
So(debug, ShouldBeTrue)
})
Convey("DEBUG=\"tRuE\" enables debugging", func() {
os.Setenv("DEBUG", "tRuE")
os.Args = []string{"spruce"}
debug = false
main()
So(debug, ShouldBeTrue)
})
Convey("DEBUG=1 enables debugging", func() {
os.Setenv("DEBUG", "1")
os.Args = []string{"spruce"}
debug = false
main()
So(debug, ShouldBeTrue)
})
Convey("DEBUG=randomval enables debugging", func() {
os.Setenv("DEBUG", "randomval")
os.Args = []string{"spruce"}
debug = false
main()
So(debug, ShouldBeTrue)
})
Convey("DEBUG=\"fAlSe\" disables debugging", func() {
os.Setenv("DEBUG", "fAlSe")
os.Args = []string{"spruce"}
debug = false
main()
So(debug, ShouldBeFalse)
})
Convey("DEBUG=0 disables debugging", func() {
os.Setenv("DEBUG", "0")
os.Args = []string{"spruce"}
debug = false
main()
So(debug, ShouldBeFalse)
})
Convey("DEBUG=\"\" disables debugging", func() {
os.Setenv("DEBUG", "")
os.Args = []string{"spruce"}
debug = false
main()
So(debug, ShouldBeFalse)
})
})
}
func TestQuoteConcourse(t *testing.T) {
Convey("quoteConcourse()", t, func() {
Convey("Correctly double-quotes incoming {{\\S}} patterns", func() {
Convey("adds quotes", func() {
input := []byte("name: {{var-_1able}}")
So(string(quoteConcourse(input)), ShouldEqual, "name: \"{{var-_1able}}\"")
})
})
Convey("doesn't affect regularly quoted things", func() {
input := []byte("name: \"my value\"")
So(string(quoteConcourse(input)), ShouldEqual, "name: \"my value\"")
})
})
}
func TestDequoteConcourse(t *testing.T) {
Convey("dequoteConcourse()", t, func() {
Convey("Correctly removes quotes from incoming {{\\S}} patterns", func() {
Convey("with single quotes", func() {
input := []byte("name: '{{var-_1able}}'")
So(dequoteConcourse(input), ShouldEqual, "name: {{var-_1able}}")
})
Convey("with double quotes", func() {
input := []byte("name: \"{{var-_1able}}\"")
So(dequoteConcourse(input), ShouldEqual, "name: {{var-_1able}}")
})
})
Convey("doesn't affect regularly quoted things", func() {
input := []byte("name: \"my value\"")
So(dequoteConcourse(input), ShouldEqual, "name: \"my value\"")
})
})
}