-
Notifications
You must be signed in to change notification settings - Fork 59
/
config_test.go
500 lines (470 loc) · 14.1 KB
/
config_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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm_test
import (
"bytes"
"fmt"
"strings"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"github.com/juju/charm/v12"
)
type ConfigSuite struct {
config *charm.Config
}
var _ = gc.Suite(&ConfigSuite{})
func (s *ConfigSuite) SetUpSuite(c *gc.C) {
// Just use a single shared config for the whole suite. There's no use case
// for mutating a config, we assume that nobody will do so here.
var err error
s.config, err = charm.ReadConfig(bytes.NewBuffer([]byte(`
options:
title:
default: My Title
description: A descriptive title used for the application.
type: string
subtitle:
default: ""
description: An optional subtitle used for the application.
outlook:
description: No default outlook.
# type defaults to string in python
username:
default: admin001
description: The name of the initial account (given admin permissions).
type: string
skill-level:
description: A number indicating skill.
type: int
agility-ratio:
description: A number from 0 to 1 indicating agility.
type: float
reticulate-splines:
description: Whether to reticulate splines on launch, or not.
type: boolean
secret-foo:
description: A secret value.
type: secret
`)))
c.Assert(err, gc.IsNil)
}
func (s *ConfigSuite) TestReadSample(c *gc.C) {
c.Assert(s.config.Options, jc.DeepEquals, map[string]charm.Option{
"title": {
Default: "My Title",
Description: "A descriptive title used for the application.",
Type: "string",
},
"subtitle": {
Default: "",
Description: "An optional subtitle used for the application.",
Type: "string",
},
"username": {
Default: "admin001",
Description: "The name of the initial account (given admin permissions).",
Type: "string",
},
"outlook": {
Description: "No default outlook.",
Type: "string",
},
"skill-level": {
Description: "A number indicating skill.",
Type: "int",
},
"agility-ratio": {
Description: "A number from 0 to 1 indicating agility.",
Type: "float",
},
"reticulate-splines": {
Description: "Whether to reticulate splines on launch, or not.",
Type: "boolean",
},
"secret-foo": {
Description: "A secret value.",
Type: "secret",
},
})
}
func (s *ConfigSuite) TestDefaultSettings(c *gc.C) {
c.Assert(s.config.DefaultSettings(), jc.DeepEquals, charm.Settings{
"title": "My Title",
"subtitle": "",
"username": "admin001",
"secret-foo": nil,
"outlook": nil,
"skill-level": nil,
"agility-ratio": nil,
"reticulate-splines": nil,
})
}
func (s *ConfigSuite) TestFilterSettings(c *gc.C) {
settings := s.config.FilterSettings(charm.Settings{
"title": "something valid",
"username": nil,
"unknown": "whatever",
"outlook": "",
"skill-level": 5.5,
"agility-ratio": true,
"reticulate-splines": "hullo",
})
c.Assert(settings, jc.DeepEquals, charm.Settings{
"title": "something valid",
"username": nil,
"outlook": "",
})
}
func (s *ConfigSuite) TestValidateSettings(c *gc.C) {
for i, test := range []struct {
info string
input charm.Settings
expect charm.Settings
err string
}{
{
info: "nil settings are valid",
expect: charm.Settings{},
}, {
info: "empty settings are valid",
input: charm.Settings{},
}, {
info: "unknown keys are not valid",
input: charm.Settings{"foo": nil},
err: `unknown option "foo"`,
}, {
info: "nil is valid for every value type",
input: charm.Settings{
"outlook": nil,
"skill-level": nil,
"agility-ratio": nil,
"reticulate-splines": nil,
},
}, {
info: "correctly-typed values are valid",
input: charm.Settings{
"outlook": "stormy",
"skill-level": int64(123),
"agility-ratio": 0.5,
"reticulate-splines": true,
},
}, {
info: "empty string-typed values stay empty",
input: charm.Settings{"outlook": ""},
expect: charm.Settings{"outlook": ""},
}, {
info: "almost-correctly-typed values are valid",
input: charm.Settings{
"skill-level": 123,
"agility-ratio": float32(0.5),
},
expect: charm.Settings{
"skill-level": int64(123),
"agility-ratio": 0.5,
},
}, {
info: "bad string",
input: charm.Settings{"outlook": false},
err: `option "outlook" expected string, got false`,
}, {
info: "bad int",
input: charm.Settings{"skill-level": 123.4},
err: `option "skill-level" expected int, got 123.4`,
}, {
info: "bad float",
input: charm.Settings{"agility-ratio": "cheese"},
err: `option "agility-ratio" expected float, got "cheese"`,
}, {
info: "bad boolean",
input: charm.Settings{"reticulate-splines": 101},
err: `option "reticulate-splines" expected boolean, got 101`,
}, {
info: "invalid secret",
input: charm.Settings{"secret-foo": "cheese"},
err: `option "secret-foo" expected secret, got "cheese"`,
}, {
info: "valid secret",
input: charm.Settings{"secret-foo": "secret:cj4v5vm78ohs79o84r4g"},
expect: charm.Settings{"secret-foo": "secret:cj4v5vm78ohs79o84r4g"},
},
} {
c.Logf("test %d: %s", i, test.info)
result, err := s.config.ValidateSettings(test.input)
if test.err != "" {
c.Check(err, gc.ErrorMatches, test.err)
} else {
c.Check(err, gc.IsNil)
if test.expect == nil {
c.Check(result, jc.DeepEquals, test.input)
} else {
c.Check(result, jc.DeepEquals, test.expect)
}
}
}
}
var settingsWithNils = charm.Settings{
"outlook": nil,
"skill-level": nil,
"agility-ratio": nil,
"reticulate-splines": nil,
}
var settingsWithValues = charm.Settings{
"outlook": "whatever",
"skill-level": int64(123),
"agility-ratio": 2.22,
"reticulate-splines": true,
}
func (s *ConfigSuite) TestParseSettingsYAML(c *gc.C) {
for i, test := range []struct {
info string
yaml string
key string
expect charm.Settings
err string
}{{
info: "bad structure",
yaml: "`",
err: `cannot parse settings data: .*`,
}, {
info: "bad key",
yaml: "{}",
key: "blah",
err: `no settings found for "blah"`,
}, {
info: "bad settings key",
yaml: "blah:\n ping: pong",
key: "blah",
err: `unknown option "ping"`,
}, {
info: "bad type for string",
yaml: "blah:\n outlook: 123",
key: "blah",
err: `option "outlook" expected string, got 123`,
}, {
info: "bad type for int",
yaml: "blah:\n skill-level: 12.345",
key: "blah",
err: `option "skill-level" expected int, got 12.345`,
}, {
info: "bad type for float",
yaml: "blah:\n agility-ratio: blob",
key: "blah",
err: `option "agility-ratio" expected float, got "blob"`,
}, {
info: "bad type for boolean",
yaml: "blah:\n reticulate-splines: 123",
key: "blah",
err: `option "reticulate-splines" expected boolean, got 123`,
}, {
info: "bad string for int",
yaml: "blah:\n skill-level: cheese",
key: "blah",
err: `option "skill-level" expected int, got "cheese"`,
}, {
info: "bad string for float",
yaml: "blah:\n agility-ratio: blob",
key: "blah",
err: `option "agility-ratio" expected float, got "blob"`,
}, {
info: "bad string for boolean",
yaml: "blah:\n reticulate-splines: cannonball",
key: "blah",
err: `option "reticulate-splines" expected boolean, got "cannonball"`,
}, {
info: "empty dict is valid",
yaml: "blah: {}",
key: "blah",
expect: charm.Settings{},
}, {
info: "nil values are valid",
yaml: `blah:
outlook: null
skill-level: null
agility-ratio: null
reticulate-splines: null`,
key: "blah",
expect: settingsWithNils,
}, {
info: "empty strings for bool options are not accepted",
yaml: `blah:
outlook: ""
skill-level: 123
agility-ratio: 12.0
reticulate-splines: ""`,
key: "blah",
err: `option "reticulate-splines" expected boolean, got ""`,
}, {
info: "empty strings for int options are not accepted",
yaml: `blah:
outlook: ""
skill-level: ""
agility-ratio: 12.0
reticulate-splines: false`,
key: "blah",
err: `option "skill-level" expected int, got ""`,
}, {
info: "empty strings for float options are not accepted",
yaml: `blah:
outlook: ""
skill-level: 123
agility-ratio: ""
reticulate-splines: false`,
key: "blah",
err: `option "agility-ratio" expected float, got ""`,
}, {
info: "appropriate strings are valid",
yaml: `blah:
outlook: whatever
skill-level: "123"
agility-ratio: "2.22"
reticulate-splines: "true"`,
key: "blah",
expect: settingsWithValues,
}, {
info: "appropriate types are valid",
yaml: `blah:
outlook: whatever
skill-level: 123
agility-ratio: 2.22
reticulate-splines: y`,
key: "blah",
expect: settingsWithValues,
}} {
c.Logf("test %d: %s", i, test.info)
result, err := s.config.ParseSettingsYAML([]byte(test.yaml), test.key)
if test.err != "" {
c.Check(err, gc.ErrorMatches, test.err)
} else {
c.Check(err, gc.IsNil)
c.Check(result, jc.DeepEquals, test.expect)
}
}
}
func (s *ConfigSuite) TestParseSettingsStrings(c *gc.C) {
for i, test := range []struct {
info string
input map[string]string
expect charm.Settings
err string
}{{
info: "nil map is valid",
expect: charm.Settings{},
}, {
info: "empty map is valid",
input: map[string]string{},
expect: charm.Settings{},
}, {
info: "empty strings for string options are valid",
input: map[string]string{"outlook": ""},
expect: charm.Settings{"outlook": ""},
}, {
info: "empty strings for non-string options are invalid",
input: map[string]string{"skill-level": ""},
err: `option "skill-level" expected int, got ""`,
}, {
info: "strings are converted",
input: map[string]string{
"outlook": "whatever",
"skill-level": "123",
"agility-ratio": "2.22",
"reticulate-splines": "true",
},
expect: settingsWithValues,
}, {
info: "bad string for int",
input: map[string]string{"skill-level": "cheese"},
err: `option "skill-level" expected int, got "cheese"`,
}, {
info: "bad string for float",
input: map[string]string{"agility-ratio": "blob"},
err: `option "agility-ratio" expected float, got "blob"`,
}, {
info: "bad string for boolean",
input: map[string]string{"reticulate-splines": "cannonball"},
err: `option "reticulate-splines" expected boolean, got "cannonball"`,
}} {
c.Logf("test %d: %s", i, test.info)
result, err := s.config.ParseSettingsStrings(test.input)
if test.err != "" {
c.Check(err, gc.ErrorMatches, test.err)
} else {
c.Check(err, gc.IsNil)
c.Check(result, jc.DeepEquals, test.expect)
}
}
}
func (s *ConfigSuite) TestConfigError(c *gc.C) {
_, err := charm.ReadConfig(bytes.NewBuffer([]byte(`options: {t: {type: foo}}`)))
c.Assert(err, gc.ErrorMatches, `invalid config: option "t" has unknown type "foo"`)
}
func (s *ConfigSuite) TestConfigWithNoOptions(c *gc.C) {
_, err := charm.ReadConfig(strings.NewReader("other:\n"))
c.Assert(err, gc.ErrorMatches, "invalid config: empty configuration")
_, err = charm.ReadConfig(strings.NewReader("\n"))
c.Assert(err, gc.ErrorMatches, "invalid config: empty configuration")
_, err = charm.ReadConfig(strings.NewReader("null\n"))
c.Assert(err, gc.ErrorMatches, "invalid config: empty configuration")
_, err = charm.ReadConfig(strings.NewReader("options:\n"))
c.Assert(err, gc.IsNil)
}
func (s *ConfigSuite) TestDefaultType(c *gc.C) {
assertDefault := func(type_ string, value string, expected interface{}) {
config := fmt.Sprintf(`options: {x: {type: %s, default: %s}}`, type_, value)
result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config)))
c.Assert(err, gc.IsNil)
c.Assert(result.Options["x"].Default, gc.Equals, expected)
}
assertDefault("boolean", "true", true)
assertDefault("string", "golden grahams", "golden grahams")
assertDefault("string", `""`, "")
assertDefault("float", "2.211", 2.211)
assertDefault("int", "99", int64(99))
assertTypeError := func(type_, str, value string) {
config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, type_, str)
_, err := charm.ReadConfig(bytes.NewBuffer([]byte(config)))
expected := fmt.Sprintf(`invalid config default: option "t" expected %s, got %s`, type_, value)
c.Assert(err, gc.ErrorMatches, expected)
}
assertTypeError("boolean", "henry", `"henry"`)
assertTypeError("string", "2.5", "2.5")
assertTypeError("float", "123a", `"123a"`)
assertTypeError("int", "true", "true")
}
// When an empty config is supplied an error should be returned
func (s *ConfigSuite) TestEmptyConfigReturnsError(c *gc.C) {
config := ""
result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config)))
c.Assert(result, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "invalid config: empty configuration")
}
func (s *ConfigSuite) TestYAMLMarshal(c *gc.C) {
cfg, err := charm.ReadConfig(strings.NewReader(`
options:
minimal:
type: string
withdescription:
type: int
description: d
withdefault:
type: boolean
description: d
default: true
`))
c.Assert(err, gc.IsNil)
c.Assert(cfg.Options, gc.HasLen, 3)
newYAML, err := yaml.Marshal(cfg)
c.Assert(err, gc.IsNil)
newCfg, err := charm.ReadConfig(bytes.NewReader(newYAML))
c.Assert(err, gc.IsNil)
c.Assert(newCfg, jc.DeepEquals, cfg)
}
func (s *ConfigSuite) TestErrorOnInvalidOptionTypes(c *gc.C) {
cfg := charm.Config{
Options: map[string]charm.Option{"testOption": {Type: "invalid type"}},
}
_, err := cfg.ParseSettingsYAML([]byte("testKey:\n testOption: 12.345"), "testKey")
c.Assert(err, gc.ErrorMatches, "option \"testOption\" has unknown type \"invalid type\"")
_, err = cfg.ParseSettingsYAML([]byte("testKey:\n testOption: \"some string value\""), "testKey")
c.Assert(err, gc.ErrorMatches, "option \"testOption\" has unknown type \"invalid type\"")
}