| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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)) |