Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 from mock import ( | |
2 Mock, | |
3 patch, | |
4 ) | |
5 import yaml | |
6 | |
7 from charmworld.lib.proof import ( | |
8 BundleProof, | |
9 CharmProof, | |
10 ProofError, | |
11 ) | |
12 from charmworld.models import Charm | |
13 from charmworld.testing import TestCase | |
14 | |
15 | |
16 class TestBundleProof(TestCase): | |
17 """Verify that a bundle can be proofed in charmworld.""" | |
18 | |
19 sample_deployer_file = """ | |
gary.poster
2013/10/18 20:10:28
...This is JSON...
| |
20 { | |
21 "wiki": { | |
22 "services": { | |
23 "wiki": { | |
24 "charm": "mediawiki", | |
25 "num_units": 2, | |
26 "branch": "lp:charms/precise/mediawiki", | |
27 "constraints": "mem=2" | |
28 } | |
29 }, | |
30 "series": "precise" }}""" | |
31 | |
32 def test_check_parseable_yaml(self): | |
33 """A deployer file should be parseable as yaml.""" | |
gary.poster
2013/10/18 20:10:28
"...as YAML or JSON."?
| |
34 | |
35 deployer_file = self.sample_deployer_file | |
36 result = BundleProof.check_parseable_deployer_file(deployer_file) | |
37 self.assertEqual(result['wiki']['services'].keys(), ['wiki']) | |
38 | |
39 def test_failing_parsing_yaml_throws_proof_error(self): | |
40 """If the yaml is not parse the yaml thows a ProofError""" | |
41 deployer_file = '{' | |
42 with self.assertRaises(ProofError) as exc: | |
43 BundleProof.check_parseable_deployer_file(deployer_file) | |
44 | |
45 self.assertEqual( | |
46 'Could not parse the yaml provided.', | |
47 exc.exception.msg | |
48 ) | |
49 self.assertEqual( | |
50 deployer_file, | |
51 exc.exception.debug_info | |
52 ) | |
53 | |
54 @patch('charmworld.lib.proof.resolve_charm_from_description') | |
55 def test_service_not_found_raises_proof_error(self, resolver): | |
56 """If we cannot find a service specified it's a ProofError""" | |
57 resolver.return_value = None | |
58 deployer = yaml.load(self.sample_deployer_file) | |
59 | |
60 with self.assertRaises(ProofError) as exc: | |
61 BundleProof.check_service_exists( | |
62 None, | |
63 'wiki', | |
64 deployer['wiki']['services']['wiki'], | |
65 deployer['wiki']) | |
66 | |
67 self.assertEqual( | |
68 "Could not find charm: wiki", | |
69 exc.exception.msg | |
70 ) | |
71 # We should have an array of logic behind the decision to look up the | |
72 # charm. Just check we've got some logic. This number might get | |
73 # tweaked, but we're just checking it made it to the exception, not | |
74 # what the reasons are. | |
75 self.assertEqual( | |
76 4, | |
77 len(exc.exception.debug_info) | |
78 ) | |
79 | |
80 | |
81 class TestCharmProof(TestCase): | |
82 | |
83 def make_fake_charm_config(self, options): | |
84 charm = Charm({ | |
85 '_id': '/precise/id', | |
86 'config': options | |
87 }) | |
88 | |
89 return charm | |
90 | |
91 def test_check_config_fails_when_no_options_defined(self): | |
92 """Bundles might attempt to set config values that are not valid""" | |
93 charm = Mock() | |
94 charm.options = {} | |
95 charm._id = 'precise/test' | |
96 | |
97 with self.assertRaises(ProofError) as exc: | |
98 CharmProof.check_config(charm, 'name', 'test') | |
99 | |
100 self.assertEqual( | |
101 'The charm has no options.', | |
102 exc.exception.msg | |
103 ) | |
104 self.assertEqual( | |
105 ['Looking at charm id: precise/test'], | |
106 exc.exception.debug_info | |
107 ) | |
108 | |
109 def test_check_config_bool(self): | |
110 """Bundles might attempt to set a boolean config value string/int""" | |
111 charm = Mock() | |
112 charm.options = { | |
113 'name': { | |
114 'type': 'boolean', | |
115 'default': True, | |
116 } | |
117 } | |
118 charm._id = 'precise/test' | |
119 | |
120 with self.assertRaises(ProofError) as exc: | |
121 CharmProof.check_config(charm, 'name', 'test') | |
122 | |
123 self.assertEqual( | |
124 'name is not of type boolean.', | |
125 exc.exception.msg | |
126 ) | |
127 self.assertEqual( | |
128 ['Looking at charm id: precise/test'], | |
129 exc.exception.debug_info | |
130 ) | |
131 | |
132 def test_check_config_fails_when_option_does_not_exist(self): | |
133 """Bundles might attempt to set config values that are not valid""" | |
134 charm = Mock() | |
135 charm.options = {'foo': {}} | |
136 charm._id = 'precise/test' | |
137 | |
138 with self.assertRaises(ProofError) as exc: | |
139 CharmProof.check_config(charm, 'name', 'test') | |
140 | |
141 self.assertEqual( | |
142 'The charm has no option for: name', | |
143 exc.exception.msg | |
144 ) | |
145 self.assertEqual( | |
146 [ | |
147 'Looking at charm id: precise/test', | |
148 'Found config keys: [\'foo\']' | |
149 ], | |
150 exc.exception.debug_info | |
151 ) | |
OLD | NEW |