Debian Codegraph @

heatclient/tests/unit/test_environment_format.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 import testscenarios
16 import testtools
17 import yaml
18
19 from heatclient.common import environment_format
20
21 load_tests = testscenarios.load_tests_apply_scenarios
22
23
24 class YamlEnvironmentTest(testtools.TestCase):
25
26 def test_minimal_yaml(self):
27 yaml1 = ''
28 yaml2 = '''
29 parameter_defaults: {}
30 encrypted_param_names: []
31 parameters: {}
32 resource_registry: {}
33 event_sinks: []
34 '''
35 tpl1 = environment_format.parse(yaml1)
36 environment_format.default_for_missing(tpl1)
37 tpl2 = environment_format.parse(yaml2)
38 self.assertEqual(tpl2, tpl1)
39
40 def test_param_valid_strategy_section(self):
41 yaml1 = ''
42 yaml2 = '''
43 parameters: {}
44 encrypted_param_names: []
45 parameter_defaults: {}
46 parameter_merge_strategies: {}
47 event_sinks: []
48 resource_registry: {}
49 '''
50 tpl1 = environment_format.parse(yaml1)
51 environment_format.default_for_missing(tpl1)
52 tpl2 = environment_format.parse(yaml2)
53 self.assertNotEqual(tpl1, tpl2)
54
55 def test_wrong_sections(self):
56 env = '''
57 parameters: {}
58 resource_regis: {}
59 '''
60 self.assertRaises(ValueError, environment_format.parse, env)
61
62 def test_bad_yaml(self):
63 env = '''
64 parameters: }
65 '''
66 self.assertRaises(ValueError, environment_format.parse, env)
67
68 def test_parse_string_environment(self):
69 env = 'just string'
70 expect = 'The environment is not a valid YAML mapping data type.'
71 e = self.assertRaises(ValueError, environment_format.parse, env)
72 self.assertIn(expect, str(e))
73
74 def test_parse_document(self):
75 env = '["foo", "bar"]'
76 expect = 'The environment is not a valid YAML mapping data type.'
77 e = self.assertRaises(ValueError, environment_format.parse, env)
78 self.assertIn(expect, str(e))
79
80
81 class YamlParseExceptions(testtools.TestCase):
82
83 scenarios = [
84 ('scanner', dict(raised_exception=yaml.scanner.ScannerError())),
85 ('parser', dict(raised_exception=yaml.parser.ParserError())),
86 ('reader',
87 dict(raised_exception=yaml.reader.ReaderError('', '', '', '', ''))),
88 ]
89
90 def test_parse_to_value_exception(self):
91 text = 'not important'
92
93 with mock.patch.object(yaml, 'load') as yaml_loader:
94 yaml_loader.side_effect = self.raised_exception
95
96 self.assertRaises(ValueError,
97 environment_format.parse, text)
98
99
100 class DetailedYAMLParseExceptions(testtools.TestCase):
101
102 def test_parse_to_value_exception(self):
103 yaml = """not important
104 but very:
105 - incorrect
106 """
107 ex = self.assertRaises(ValueError, environment_format.parse, yaml)
108 self.assertIn('but very:\n ^', str(ex))