forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_test.go
963 lines (928 loc) · 28.4 KB
/
actions_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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
// Copyright 2011-2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"bytes"
"encoding/json"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
)
type ActionsSuite struct{}
var _ = gc.Suite(&ActionsSuite{})
func (s *ActionsSuite) TestNewActions(c *gc.C) {
emptyAction := NewActions()
c.Assert(emptyAction, jc.DeepEquals, &Actions{})
}
func (s *ActionsSuite) TestValidateOk(c *gc.C) {
for i, test := range []struct {
description string
actionSpec *ActionSpec
objectToValidate map[string]interface{}
}{{
description: "Validation of an empty object is ok.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}}}},
objectToValidate: nil,
}, {
description: "Validation of one required value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}},
"required": []interface{}{"outfile"}}},
objectToValidate: map[string]interface{}{
"outfile": "out-2014-06-12.bz2",
},
}, {
description: "Validation of one required and one optional value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"},
"quality": map[string]interface{}{
"description": "Compression quality",
"type": "integer",
"minimum": 0,
"maximum": 9}},
"required": []interface{}{"outfile"}}},
objectToValidate: map[string]interface{}{
"outfile": "out-2014-06-12.bz2",
},
}, {
description: "Validation of an optional, range limited value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"},
"quality": map[string]interface{}{
"description": "Compression quality",
"type": "integer",
"minimum": 0,
"maximum": 9}},
"required": []interface{}{"outfile"}}},
objectToValidate: map[string]interface{}{
"outfile": "out-2014-06-12.bz2",
"quality": 5,
},
}} {
c.Logf("test %d: %s", i, test.description)
err := test.actionSpec.ValidateParams(test.objectToValidate)
c.Assert(err, jc.ErrorIsNil)
}
}
func (s *ActionsSuite) TestValidateFail(c *gc.C) {
var validActionTests = []struct {
description string
actionSpec *ActionSpec
badActionJson string
expectedError string
}{{
description: "Validation of one required value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}},
"required": []interface{}{"outfile"}}},
badActionJson: `{"outfile": 5}`,
expectedError: "validation failed: (root).outfile : must be of type string, given 5",
}, {
description: "Restrict to only one property",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}},
"required": []interface{}{"outfile"},
"additionalProperties": false}},
badActionJson: `{"outfile": "foo.bz", "bar": "foo"}`,
expectedError: "validation failed: (root) : additional property \"bar\" is not allowed, given {\"bar\":\"foo\",\"outfile\":\"foo.bz\"}",
}, {
description: "Validation of one required and one optional value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"},
"quality": map[string]interface{}{
"description": "Compression quality",
"type": "integer",
"minimum": 0,
"maximum": 9}},
"required": []interface{}{"outfile"}}},
badActionJson: `{"quality": 5}`,
expectedError: "validation failed: (root) : \"outfile\" property is missing and required, given {\"quality\":5}",
}, {
description: "Validation of an optional, range limited value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"},
"quality": map[string]interface{}{
"description": "Compression quality",
"type": "integer",
"minimum": 0,
"maximum": 9}},
"required": []interface{}{"outfile"}}},
badActionJson: `
{ "outfile": "out-2014-06-12.bz2", "quality": "two" }`,
expectedError: "validation failed: (root).quality : must be of type integer, given \"two\"",
}}
for i, test := range validActionTests {
c.Logf("test %d: %s", i, test.description)
var params map[string]interface{}
jsonBytes := []byte(test.badActionJson)
err := json.Unmarshal(jsonBytes, ¶ms)
c.Assert(err, gc.IsNil)
err = test.actionSpec.ValidateParams(params)
c.Assert(err.Error(), gc.Equals, test.expectedError)
}
}
func (s *ActionsSuite) TestCleanseOk(c *gc.C) {
var goodInterfaceTests = []struct {
description string
acceptableInterface map[string]interface{}
expectedInterface map[string]interface{}
}{{
description: "An interface requiring no changes.",
acceptableInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[string]interface{}{
"foo1": "val1",
"foo2": "val2"}},
expectedInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[string]interface{}{
"foo1": "val1",
"foo2": "val2"}},
}, {
description: "Substitute a single inner map[i]i.",
acceptableInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[interface{}]interface{}{
"foo1": "val1",
"foo2": "val2"}},
expectedInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[string]interface{}{
"foo1": "val1",
"foo2": "val2"}},
}, {
description: "Substitute nested inner map[i]i.",
acceptableInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": "val2a",
"key3a": map[interface{}]interface{}{
"key1b": "val1b",
"key2b": map[interface{}]interface{}{
"key1c": "val1c"}}},
expectedInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": "val2a",
"key3a": map[string]interface{}{
"key1b": "val1b",
"key2b": map[string]interface{}{
"key1c": "val1c"}}},
}, {
description: "Substitute nested map[i]i within []i.",
acceptableInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": []interface{}{5, "foo", map[string]interface{}{
"key1b": "val1b",
"key2b": map[interface{}]interface{}{
"key1c": "val1c"}}}},
expectedInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": []interface{}{5, "foo", map[string]interface{}{
"key1b": "val1b",
"key2b": map[string]interface{}{
"key1c": "val1c"}}}},
}}
for i, test := range goodInterfaceTests {
c.Logf("test %d: %s", i, test.description)
cleansedInterfaceMap, err := cleanse(test.acceptableInterface)
c.Assert(err, gc.IsNil)
c.Assert(cleansedInterfaceMap, jc.DeepEquals, test.expectedInterface)
}
}
func (s *ActionsSuite) TestCleanseFail(c *gc.C) {
var badInterfaceTests = []struct {
description string
failInterface map[string]interface{}
expectedError string
}{{
description: "An inner map[interface{}]interface{} with an int key.",
failInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[interface{}]interface{}{
"foo1": "val1",
5: "val2"}},
expectedError: "map keyed with non-string value",
}, {
description: "An inner []interface{} containing a map[i]i with an int key.",
failInterface: map[string]interface{}{
"key1a": "val1b",
"key2a": "val2b",
"key3a": []interface{}{"foo1", 5, map[interface{}]interface{}{
"key1b": "val1b",
"key2b": map[interface{}]interface{}{
"key1c": "val1c",
5: "val2c"}}}},
expectedError: "map keyed with non-string value",
}}
for i, test := range badInterfaceTests {
c.Logf("test %d: %s", i, test.description)
_, err := cleanse(test.failInterface)
c.Assert(err, gc.NotNil)
c.Assert(err.Error(), gc.Equals, test.expectedError)
}
}
func (s *ActionsSuite) TestGetActionNameRule(c *gc.C) {
var regExCheck = []struct {
description string
regExString string
}{{
description: "Check returned actionNameRule regex",
regExString: "^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$",
}}
for i, t := range regExCheck {
c.Logf("test %d: %v: %#v\n", i, t.description, t.regExString)
obtained := GetActionNameRule()
c.Assert(obtained.String(), gc.Equals, t.regExString)
}
}
func (s *ActionsSuite) TestReadGoodActionsYaml(c *gc.C) {
var goodActionsYamlTests = []struct {
description string
yaml string
expectedActions *Actions
}{{
description: "A simple snapshot actions YAML with one parameter.",
yaml: `
snapshot:
description: Take a snapshot of the database.
params:
outfile:
description: "The file to write out to."
type: string
required: ["outfile"]
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": {
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}},
"required": []interface{}{"outfile"}}}}},
}, {
description: "An empty Actions definition.",
yaml: "",
expectedActions: &Actions{
ActionSpecs: map[string]ActionSpec{},
},
}, {
description: "A more complex schema with hyphenated names and multiple parameters.",
yaml: `
snapshot:
description: "Take a snapshot of the database."
params:
outfile:
description: "The file to write out to."
type: "string"
compression-quality:
description: "The compression quality."
type: "integer"
minimum: 0
maximum: 9
exclusiveMaximum: false
remote-sync:
description: "Sync a file to a remote host."
params:
file:
description: "The file to send out."
type: "string"
format: "uri"
remote-uri:
description: "The host to sync to."
type: "string"
format: "uri"
util:
description: "The util to perform the sync (rsync or scp.)"
type: "string"
enum: ["rsync", "scp"]
required: ["file", "remote-uri"]
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": {
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"},
"compression-quality": map[string]interface{}{
"description": "The compression quality.",
"type": "integer",
"minimum": 0,
"maximum": 9,
"exclusiveMaximum": false}}}},
"remote-sync": {
Description: "Sync a file to a remote host.",
Params: map[string]interface{}{
"title": "remote-sync",
"description": "Sync a file to a remote host.",
"type": "object",
"properties": map[string]interface{}{
"file": map[string]interface{}{
"description": "The file to send out.",
"type": "string",
"format": "uri"},
"remote-uri": map[string]interface{}{
"description": "The host to sync to.",
"type": "string",
"format": "uri"},
"util": map[string]interface{}{
"description": "The util to perform the sync (rsync or scp.)",
"type": "string",
"enum": []interface{}{"rsync", "scp"}}},
"required": []interface{}{"file", "remote-uri"}}}}},
}, {
description: "A schema with other keys, e.g. \"definitions\"",
yaml: `
snapshot:
description: "Take a snapshot of the database."
params:
outfile:
description: "The file to write out to."
type: "string"
compression-quality:
description: "The compression quality."
type: "integer"
minimum: 0
maximum: 9
exclusiveMaximum: false
definitions:
diskdevice: {}
something-else: {}
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": {
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "snapshot",
"description": "Take a snapshot of the database.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string",
},
"compression-quality": map[string]interface{}{
"description": "The compression quality.",
"type": "integer",
"minimum": 0,
"maximum": 9,
"exclusiveMaximum": false,
},
},
"definitions": map[string]interface{}{
"diskdevice": map[string]interface{}{},
"something-else": map[string]interface{}{},
},
},
},
}},
}, {
description: "A schema with no \"params\" key, implying no options.",
yaml: `
snapshot:
description: Take a snapshot of the database.
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": {
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"description": "Take a snapshot of the database.",
"title": "snapshot",
"type": "object",
"properties": map[string]interface{}{},
}}}},
}, {
description: "A schema with no values at all, implying no options.",
yaml: `
snapshot:
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": {
Description: "No description",
Params: map[string]interface{}{
"description": "No description",
"title": "snapshot",
"type": "object",
"properties": map[string]interface{}{},
}}}},
}, {
description: "A simple snapshot actions YAML with names ending characters.",
yaml: `
snapshot-01:
description: Take database first snapshot.
params:
outfile-01:
description: "The file to write out to."
type: string
required: ["outfile"]
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot-01": {
Description: "Take database first snapshot.",
Params: map[string]interface{}{
"title": "snapshot-01",
"description": "Take database first snapshot.",
"type": "object",
"properties": map[string]interface{}{
"outfile-01": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}},
"required": []interface{}{"outfile"}}}}},
}, {
description: "A simple snapshot actions YAML with names containing characters.",
yaml: `
snapshot-0-foo:
description: Take database first snapshot.
params:
outfile:
description: "The file to write out to."
type: string
required: ["outfile"]
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot-0-foo": {
Description: "Take database first snapshot.",
Params: map[string]interface{}{
"title": "snapshot-0-foo",
"description": "Take database first snapshot.",
"type": "object",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}},
"required": []interface{}{"outfile"}}}}},
}, {
description: "A simple snapshot actions YAML with names starting characters.",
yaml: `
01-snapshot:
description: Take database first snapshot.
params:
01-outfile:
description: "The file to write out to."
type: string
required: ["outfile"]
`,
expectedActions: &Actions{map[string]ActionSpec{
"01-snapshot": {
Description: "Take database first snapshot.",
Params: map[string]interface{}{
"title": "01-snapshot",
"description": "Take database first snapshot.",
"type": "object",
"properties": map[string]interface{}{
"01-outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string"}},
"required": []interface{}{"outfile"}}}}},
}}
// Beginning of testing loop
for i, test := range goodActionsYamlTests {
c.Logf("test %d: %s", i, test.description)
reader := bytes.NewReader([]byte(test.yaml))
loadedAction, err := ReadActionsYaml(reader)
c.Assert(err, gc.IsNil)
c.Check(loadedAction, jc.DeepEquals, test.expectedActions)
}
}
func (s *ActionsSuite) TestReadBadActionsYaml(c *gc.C) {
var badActionsYamlTests = []struct {
description string
yaml string
expectedError string
}{{
description: "Reject JSON-Schema containing references.",
yaml: `
snapshot:
description: Take a snapshot of the database.
params:
$schema: "http://json-schema.org/draft-03/schema#"
`,
expectedError: `schema key "\$schema" not compatible with this version of juju`,
}, {
description: "Reject JSON-Schema containing references.",
yaml: `
snapshot:
description: Take a snapshot of the database.
params:
outfile: { $ref: "http://json-schema.org/draft-03/schema#" }
`,
expectedError: `schema key "\$ref" not compatible with this version of juju`,
}, {
description: "Malformed YAML: missing key in \"outfile\".",
yaml: `
snapshot:
description: Take a snapshot of the database.
params:
outfile:
The file to write out to.
type: string
default: foo.bz2
`,
expectedError: `yaml: line [0-9]: mapping values are not allowed in this context`,
}, {
description: "Malformed JSON-Schema: $schema element misplaced.",
yaml: `
snapshot:
description: Take a snapshot of the database.
params:
outfile:
$schema: http://json-schema.org/draft-03/schema#
description: The file to write out to.
type: string
default: foo.bz2
`,
expectedError: `yaml: line [0-9]: mapping values are not allowed in this context`,
}, {
description: "Malformed Actions: hyphen at beginning of action name.",
yaml: `
-snapshot:
description: Take a snapshot of the database.
`,
expectedError: `bad action name -snapshot`,
}, {
description: "Malformed Actions: hyphen after action name.",
yaml: `
snapshot-:
description: Take a snapshot of the database.
`,
expectedError: `bad action name snapshot-`,
}, {
description: "Malformed Actions: caps in action name.",
yaml: `
Snapshot:
description: Take a snapshot of the database.
`,
expectedError: `bad action name Snapshot`,
}, {
description: `Reserved Action Name: "juju".`,
yaml: `
juju:
description: A reserved action.
`,
expectedError: `cannot use action name juju: "juju" is a reserved name`,
}, {
description: `Reserved Action Name: "juju-run".`,
yaml: `
juju-run:
description: A reserved action.
`,
expectedError: `cannot use action name juju-run: the "juju-" prefix is reserved`,
}, {
description: "A non-string description fails to parse",
yaml: `
snapshot:
description: ["Take a snapshot of the database."]
`,
expectedError: `value for schema key "description" must be a string`,
}, {
description: "A non-list \"required\" key",
yaml: `
snapshot:
description: Take a snapshot of the database.
params:
outfile:
description: "The file to write out to."
type: string
required: "outfile"
`,
expectedError: `value for schema key "required" must be a YAML list`,
}, {
description: "A schema with an empty \"params\" key fails to parse",
yaml: `
snapshot:
description: Take a snapshot of the database.
params:
`,
expectedError: `params failed to parse as a map`,
}, {
description: "A schema with a non-map \"params\" value fails to parse",
yaml: `
snapshot:
description: Take a snapshot of the database.
params: ["a", "b"]
`,
expectedError: `params failed to parse as a map`,
}, {
description: "\"definitions\" goes against JSON-Schema definition",
yaml: `
snapshot:
description: "Take a snapshot of the database."
params:
outfile:
description: "The file to write out to."
type: "string"
definitions:
diskdevice: ["a"]
something-else: {"a": "b"}
`,
expectedError: `invalid params schema for action schema snapshot: definitions must be of type array of schemas`,
}, {
description: "excess keys not in the JSON-Schema spec will be rejected",
yaml: `
snapshot:
description: "Take a snapshot of the database."
params:
outfile:
description: "The file to write out to."
type: "string"
compression-quality:
description: "The compression quality."
type: "integer"
minimum: 0
maximum: 9
exclusiveMaximum: false
definitions:
diskdevice: {}
something-else: {}
other-key: ["some", "values"],
`,
expectedError: `yaml: line [0-9]+: did not find expected key`,
}}
for i, test := range badActionsYamlTests {
c.Logf("test %d: %s", i, test.description)
reader := bytes.NewReader([]byte(test.yaml))
_, err := ReadActionsYaml(reader)
c.Check(err, gc.ErrorMatches, test.expectedError)
}
}
func (s *ActionsSuite) TestRecurseMapOnKeys(c *gc.C) {
tests := []struct {
should string
givenKeys []string
givenMap map[string]interface{}
expected interface{}
shouldFail bool
}{{
should: "fail if the specified key was not in the map",
givenKeys: []string{"key", "key2"},
givenMap: map[string]interface{}{
"key": map[string]interface{}{
"key": "value",
},
},
shouldFail: true,
}, {
should: "fail if a key was not a string",
givenKeys: []string{"key", "key2"},
givenMap: map[string]interface{}{
"key": map[interface{}]interface{}{
5: "value",
},
},
shouldFail: true,
}, {
should: "fail if we have more keys but not a recursable val",
givenKeys: []string{"key", "key2"},
givenMap: map[string]interface{}{
"key": []string{"a", "b", "c"},
},
shouldFail: true,
}, {
should: "retrieve a good value",
givenKeys: []string{"key", "key2"},
givenMap: map[string]interface{}{
"key": map[string]interface{}{
"key2": "value",
},
},
expected: "value",
}, {
should: "retrieve a map",
givenKeys: []string{"key"},
givenMap: map[string]interface{}{
"key": map[string]interface{}{
"key": "value",
},
},
expected: map[string]interface{}{
"key": "value",
},
}, {
should: "retrieve a slice",
givenKeys: []string{"key"},
givenMap: map[string]interface{}{
"key": []string{"a", "b", "c"},
},
expected: []string{"a", "b", "c"},
}}
for i, t := range tests {
c.Logf("test %d: should %s\n map: %#v\n keys: %#v", i, t.should, t.givenMap, t.givenKeys)
obtained, failed := recurseMapOnKeys(t.givenKeys, t.givenMap)
c.Assert(!failed, gc.Equals, t.shouldFail)
if !t.shouldFail {
c.Check(obtained, jc.DeepEquals, t.expected)
}
}
}
func (s *ActionsSuite) TestInsertDefaultValues(c *gc.C) {
schemas := map[string]string{
"simple": `
act:
params:
val:
type: string
default: somestr
`[1:],
"complicated": `
act:
params:
val:
type: object
properties:
foo:
type: string
bar:
type: object
properties:
baz:
type: string
default: boz
`[1:],
"default-object": `
act:
params:
val:
type: object
default:
foo: bar
bar:
baz: woz
`[1:],
"none": `
act:
params:
val:
type: object
properties:
var:
type: object
properties:
x:
type: string
`[1:]}
for i, t := range []struct {
should string
schema string
withParams map[string]interface{}
expectedResult map[string]interface{}
expectedError string
}{{
should: "error with no schema",
expectedError: "schema must be of type object",
}, {
should: "create a map if handed nil",
schema: schemas["none"],
withParams: nil,
expectedResult: map[string]interface{}{},
}, {
should: "create and fill target if handed nil",
schema: schemas["simple"],
withParams: nil,
expectedResult: map[string]interface{}{"val": "somestr"},
}, {
should: "create a simple default value",
schema: schemas["simple"],
withParams: map[string]interface{}{},
expectedResult: map[string]interface{}{"val": "somestr"},
}, {
should: "do nothing for no default value",
schema: schemas["none"],
withParams: map[string]interface{}{},
expectedResult: map[string]interface{}{},
}, {
should: "insert a default value within a nested map",
schema: schemas["complicated"],
withParams: map[string]interface{}{},
expectedResult: map[string]interface{}{
"val": map[string]interface{}{
"bar": map[string]interface{}{
"baz": "boz",
}}},
}, {
should: "create a default value which is an object",
schema: schemas["default-object"],
withParams: map[string]interface{}{},
expectedResult: map[string]interface{}{
"val": map[string]interface{}{
"foo": "bar",
"bar": map[string]interface{}{
"baz": "woz",
}}},
}, {
should: "not overwrite existing values with default objects",
schema: schemas["default-object"],
withParams: map[string]interface{}{"val": 5},
expectedResult: map[string]interface{}{"val": 5},
}, {
should: "interleave defaults into existing objects",
schema: schemas["complicated"],
withParams: map[string]interface{}{
"val": map[string]interface{}{
"foo": "bar",
"bar": map[string]interface{}{
"faz": "foz",
}}},
expectedResult: map[string]interface{}{
"val": map[string]interface{}{
"foo": "bar",
"bar": map[string]interface{}{
"baz": "boz",
"faz": "foz",
}}},
}} {
c.Logf("test %d: should %s", i, t.should)
schema := getSchemaForAction(c, t.schema)
// Testing this method
result, err := schema.InsertDefaults(t.withParams)
if t.expectedError != "" {
c.Check(err, gc.ErrorMatches, t.expectedError)
continue
}
c.Assert(err, jc.ErrorIsNil)
c.Check(result, jc.DeepEquals, t.expectedResult)
}
}
func getSchemaForAction(c *gc.C, wholeSchema string) ActionSpec {
// Load up the YAML schema definition.
reader := bytes.NewReader([]byte(wholeSchema))
loadedActions, err := ReadActionsYaml(reader)
c.Assert(err, gc.IsNil)
// Same action name for all tests, "act".
return loadedActions.ActionSpecs["act"]
}