Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2011, 2012, 2013 Canonical Ltd. | 1 // Copyright 2011, 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package charm_test | 4 package charm_test |
5 | 5 |
6 import ( | 6 import ( |
7 "bytes" | 7 "bytes" |
8 "fmt" | 8 "fmt" |
9 . "launchpad.net/gocheck" | 9 . "launchpad.net/gocheck" |
10 "launchpad.net/juju-core/charm" | 10 "launchpad.net/juju-core/charm" |
11 ) | 11 ) |
12 | 12 |
13 type ConfigSuite struct { | 13 type ConfigSuite struct { |
14 config *charm.Config | 14 config *charm.Config |
15 } | 15 } |
16 | 16 |
17 var _ = Suite(&ConfigSuite{}) | 17 var _ = Suite(&ConfigSuite{}) |
18 | 18 |
19 func (s *ConfigSuite) SetUpSuite(c *C) { | 19 func (s *ConfigSuite) SetUpSuite(c *C) { |
20 // Just use a single shared config for the whole suite. There's no use c ase | 20 // Just use a single shared config for the whole suite. There's no use c ase |
21 // for mutating a config, we we assume that nobody will do so here. | 21 // for mutating a config, we we assume that nobody will do so here. |
22 var err error | 22 var err error |
23 s.config, err = charm.ReadConfig(bytes.NewBuffer([]byte(` | 23 s.config, err = charm.ReadConfig(bytes.NewBuffer([]byte(` |
24 options: | 24 options: |
25 title: | 25 title: |
26 default: My Title | 26 default: My Title |
27 description: A descriptive title used for the service. | 27 description: A descriptive title used for the service. |
28 type: string | 28 type: string |
29 subtitle: | |
30 default: "" | |
31 description: An optional subtitle used for the service. | |
29 outlook: | 32 outlook: |
30 description: No default outlook. | 33 description: No default outlook. |
31 # type defaults to string in python | 34 # type defaults to string in python |
32 username: | 35 username: |
33 default: admin001 | 36 default: admin001 |
34 description: The name of the initial account (given admin permissions). | 37 description: The name of the initial account (given admin permissions). |
35 type: string | 38 type: string |
36 skill-level: | 39 skill-level: |
37 description: A number indicating skill. | 40 description: A number indicating skill. |
38 type: int | 41 type: int |
39 agility-ratio: | 42 agility-ratio: |
40 description: A number from 0 to 1 indicating agility. | 43 description: A number from 0 to 1 indicating agility. |
41 type: float | 44 type: float |
42 reticulate-splines: | 45 reticulate-splines: |
43 description: Whether to reticulate splines on launch, or not. | 46 description: Whether to reticulate splines on launch, or not. |
44 type: boolean | 47 type: boolean |
45 `))) | 48 `))) |
46 c.Assert(err, IsNil) | 49 c.Assert(err, IsNil) |
47 } | 50 } |
48 | 51 |
49 func (s *ConfigSuite) TestReadSample(c *C) { | 52 func (s *ConfigSuite) TestReadSample(c *C) { |
50 c.Assert(s.config.Options, DeepEquals, map[string]charm.Option{ | 53 c.Assert(s.config.Options, DeepEquals, map[string]charm.Option{ |
51 "title": { | 54 "title": { |
52 Default: "My Title", | 55 Default: "My Title", |
53 Description: "A descriptive title used for the service." , | 56 Description: "A descriptive title used for the service." , |
54 Type: "string", | 57 Type: "string", |
55 }, | 58 }, |
59 "subtitle": { | |
60 Default: "", | |
61 Description: "An optional subtitle used for the service. ", | |
62 Type: "string", | |
63 }, | |
56 "username": { | 64 "username": { |
57 Default: "admin001", | 65 Default: "admin001", |
58 Description: "The name of the initial account (given adm in permissions).", | 66 Description: "The name of the initial account (given adm in permissions).", |
59 Type: "string", | 67 Type: "string", |
60 }, | 68 }, |
61 "outlook": { | 69 "outlook": { |
62 Description: "No default outlook.", | 70 Description: "No default outlook.", |
63 Type: "string", | 71 Type: "string", |
64 }, | 72 }, |
65 "skill-level": { | 73 "skill-level": { |
66 Description: "A number indicating skill.", | 74 Description: "A number indicating skill.", |
67 Type: "int", | 75 Type: "int", |
68 }, | 76 }, |
69 "agility-ratio": { | 77 "agility-ratio": { |
70 Description: "A number from 0 to 1 indicating agility.", | 78 Description: "A number from 0 to 1 indicating agility.", |
71 Type: "float", | 79 Type: "float", |
72 }, | 80 }, |
73 "reticulate-splines": { | 81 "reticulate-splines": { |
74 Description: "Whether to reticulate splines on launch, o r not.", | 82 Description: "Whether to reticulate splines on launch, o r not.", |
75 Type: "boolean", | 83 Type: "boolean", |
76 }, | 84 }, |
77 }) | 85 }) |
78 } | 86 } |
79 | 87 |
80 func (s *ConfigSuite) TestDefaultSettings(c *C) { | 88 func (s *ConfigSuite) TestDefaultSettings(c *C) { |
81 c.Assert(s.config.DefaultSettings(), DeepEquals, charm.Settings{ | 89 c.Assert(s.config.DefaultSettings(), DeepEquals, charm.Settings{ |
82 "title": "My Title", | 90 "title": "My Title", |
91 "subtitle": "", | |
83 "username": "admin001", | 92 "username": "admin001", |
84 "outlook": nil, | 93 "outlook": nil, |
85 "skill-level": nil, | 94 "skill-level": nil, |
86 "agility-ratio": nil, | 95 "agility-ratio": nil, |
87 "reticulate-splines": nil, | 96 "reticulate-splines": nil, |
88 }) | 97 }) |
89 } | 98 } |
90 | 99 |
91 func (s *ConfigSuite) TestFilterSettings(c *C) { | 100 func (s *ConfigSuite) TestFilterSettings(c *C) { |
92 settings := s.config.FilterSettings(charm.Settings{ | 101 settings := s.config.FilterSettings(charm.Settings{ |
93 "title": "something valid", | 102 "title": "something valid", |
103 "subtitle": "", | |
fwereade
2013/06/28 09:32:11
Just skip this. You haven't changed the behaviour
mue
2013/06/28 13:15:57
Done.
| |
94 "username": nil, | 104 "username": nil, |
95 "unknown": "whatever", | 105 "unknown": "whatever", |
96 "outlook": "", | 106 "outlook": "", |
97 "skill-level": 5.5, | 107 "skill-level": 5.5, |
98 "agility-ratio": true, | 108 "agility-ratio": true, |
99 "reticulate-splines": "hullo", | 109 "reticulate-splines": "hullo", |
100 }) | 110 }) |
101 c.Assert(settings, DeepEquals, charm.Settings{ | 111 c.Assert(settings, DeepEquals, charm.Settings{ |
102 "title": "something valid", | 112 "title": "something valid", |
113 "subtitle": nil, | |
dimitern
2013/06/27 15:01:20
shouldn't this be "" ?
fwereade
2013/06/28 09:32:11
Nope -- all we've changed is ReadConfig, the usual
| |
103 "username": nil, | 114 "username": nil, |
104 "outlook": nil, | 115 "outlook": nil, |
105 }) | 116 }) |
106 } | 117 } |
107 | 118 |
108 func (s *ConfigSuite) TestValidateSettings(c *C) { | 119 func (s *ConfigSuite) TestValidateSettings(c *C) { |
109 for i, test := range []struct { | 120 for i, test := range []struct { |
110 info string | 121 info string |
111 input charm.Settings | 122 input charm.Settings |
112 expect charm.Settings | 123 expect charm.Settings |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 func (s *ConfigSuite) TestDefaultType(c *C) { | 380 func (s *ConfigSuite) TestDefaultType(c *C) { |
370 assertDefault := func(type_ string, value string, expected interface{}) { | 381 assertDefault := func(type_ string, value string, expected interface{}) { |
371 config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, t ype_, value) | 382 config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, t ype_, value) |
372 result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) | 383 result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) |
373 c.Assert(err, IsNil) | 384 c.Assert(err, IsNil) |
374 c.Assert(result.Options["t"].Default, Equals, expected) | 385 c.Assert(result.Options["t"].Default, Equals, expected) |
375 } | 386 } |
376 | 387 |
377 assertDefault("boolean", "true", true) | 388 assertDefault("boolean", "true", true) |
378 assertDefault("string", "golden grahams", "golden grahams") | 389 assertDefault("string", "golden grahams", "golden grahams") |
390 assertDefault("string", `""`, "") | |
379 assertDefault("float", "2.2e11", 2.2e11) | 391 assertDefault("float", "2.2e11", 2.2e11) |
380 assertDefault("int", "99", int64(99)) | 392 assertDefault("int", "99", int64(99)) |
381 | 393 |
382 assertTypeError := func(type_, str, value string) { | 394 assertTypeError := func(type_, str, value string) { |
383 config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, t ype_, str) | 395 config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, t ype_, str) |
384 _, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) | 396 _, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) |
385 expected := fmt.Sprintf(`invalid config default: option "t" expe cted %s, got %s`, type_, value) | 397 expected := fmt.Sprintf(`invalid config default: option "t" expe cted %s, got %s`, type_, value) |
386 c.Assert(err, ErrorMatches, expected) | 398 c.Assert(err, ErrorMatches, expected) |
387 } | 399 } |
388 | 400 |
389 assertTypeError("boolean", "henry", `"henry"`) | 401 assertTypeError("boolean", "henry", `"henry"`) |
390 assertTypeError("string", "2.5", "2.5") | 402 assertTypeError("string", "2.5", "2.5") |
391 assertTypeError("float", "123", "123") | 403 assertTypeError("float", "123", "123") |
392 assertTypeError("int", "true", "true") | 404 assertTypeError("int", "true", "true") |
393 } | 405 } |
OLD | NEW |