forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_test.go
519 lines (490 loc) · 16 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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charm
import (
"bytes"
"encoding/json"
gc "gopkg.in/check.v1"
)
type ActionsSuite struct{}
var _ = gc.Suite(&ActionsSuite{})
func (s *ActionsSuite) TestNewActions(c *gc.C) {
emptyAction := NewActions()
c.Assert(emptyAction, gc.DeepEquals, &Actions{})
}
func (s *ActionsSuite) TestValidateOk(c *gc.C) {
var validActionTests = []struct {
description string
actionSpec *ActionSpec
goodActionJson string
}{{
description: "Validation of one required value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "Snapshot params",
"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"}}},
goodActionJson: `{"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 params",
"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"}}},
goodActionJson: `{"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 params",
"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"}}},
goodActionJson: `
{ "outfile": "out-2014-06-12.bz2", "quality": 5 }`,
}}
for i, test := range validActionTests {
c.Logf("test %d: %s", i, test.description)
var params interface{}
jsonBytes := []byte(test.goodActionJson)
err := json.Unmarshal(jsonBytes, ¶ms)
c.Assert(err, gc.IsNil)
_, err = test.actionSpec.ValidateParams(params)
c.Assert(err, gc.IsNil)
}
}
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 params",
"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: "JSON validation failed: (root).outfile : must be of type string, given 5",
}, {
description: "Validation of one required and one optional value.",
actionSpec: &ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "Snapshot params",
"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: "JSON 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 params",
"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: "JSON 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 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, gc.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) TestReadGoodActionsYaml(c *gc.C) {
var goodActionsYamlTests = []struct {
description string
yaml string
expectedActions *Actions
}{{
description: "A simple snapshot actions YAML with one parameter.",
yaml: `
actions:
snapshot:
description: Take a snapshot of the database.
params:
title: "Snapshot"
type: "object"
properties:
outfile:
description: "The file to write out to."
type: string
required: ["outfile"]
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "Snapshot",
"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{},
}, {
description: "A more complex schema with hyphenated names and multiple parameters.",
yaml: `
actions:
snapshot:
description: "Take a snapshot of the database."
params:
title: "Snapshot"
type: "object"
properties:
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:
title: "Remote sync"
type: "object"
properties:
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": ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"title": "Snapshot",
"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": ActionSpec{
Description: "Sync a file to a remote host.",
Params: map[string]interface{}{
"title": "Remote sync",
"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 an empty \"params\" key, implying no options.",
yaml: `
actions:
snapshot:
description: Take a snapshot of the database.
params:
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{}}}},
}, {
description: "A schema with no \"params\" key, implying no options.",
yaml: `
actions:
snapshot:
description: Take a snapshot of the database.
`,
expectedActions: &Actions{map[string]ActionSpec{
"snapshot": ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{}}}},
}}
// 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.Assert(loadedAction, gc.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: `
actions:
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: `
actions:
snapshot:
description: Take a snapshot of the database.
params:
title: "snapshot"
type: "object"
properties:
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: `
actions:
snapshot:
description: Take a snapshot of the database.
params:
outfile:
The file to write out to.
type: string
default: foo.bz2
`,
expectedError: "YAML error: line 7: mapping values are not allowed in this context",
}, {
description: "Malformed JSON-Schema: $schema element misplaced.",
yaml: `
actions:
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 error: line 4: mapping values are not allowed in this context",
}, {
description: "Malformed Actions: hyphen at beginning of action name.",
yaml: `
actions:
-snapshot:
description: Take a snapshot of the database.
`,
expectedError: "bad action name -snapshot",
}, {
description: "Malformed Actions: hyphen after action name.",
yaml: `
actions:
snapshot-:
description: Take a snapshot of the database.
`,
expectedError: "bad action name snapshot-",
}, {
description: "Malformed Actions: caps in action name.",
yaml: `
actions:
Snapshot:
description: Take a snapshot of the database.
`,
expectedError: "bad action name Snapshot",
}}
for i, test := range badActionsYamlTests {
c.Logf("test %d: %s", i, test.description)
reader := bytes.NewReader([]byte(test.yaml))
_, err := ReadActionsYaml(reader)
c.Assert(err, gc.NotNil)
c.Assert(err.Error(), gc.Equals, test.expectedError)
}
}