| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from heatclient import exc |
| 18 |
from heatclient.osc.v1 import template |
| 19 |
from heatclient.tests.unit.osc.v1 import fakes |
| 20 |
from heatclient.v1 import template_versions |
| 21 |
|
| 22 |
|
| 23 |
class TestTemplate(fakes.TestOrchestrationv1): |
| 24 |
def setUp(self): |
| 25 |
super().setUp() |
| 26 |
self.mock_client = self.app.client_manager.orchestration |
| 27 |
self.template_versions = self.mock_client.template_versions |
| 28 |
|
| 29 |
|
| 30 |
class TestTemplateVersionList(TestTemplate): |
| 31 |
|
| 32 |
def _stub_versions_list(self, ret_data): |
| 33 |
tv1 = template_versions.TemplateVersion(None, ret_data[0]) |
| 34 |
tv2 = template_versions.TemplateVersion(None, ret_data[1]) |
| 35 |
self.template_versions.list.return_value = [tv1, tv2] |
| 36 |
self.cmd = template.VersionList(self.app, None) |
| 37 |
|
| 38 |
def test_version_list(self): |
| 39 |
ret_data = [ |
| 40 |
{'version': 'HOT123', 'type': 'hot'}, |
| 41 |
{'version': 'CFN456', 'type': 'cfn'}] |
| 42 |
self._stub_versions_list(ret_data) |
| 43 |
parsed_args = self.check_parser(self.cmd, [], []) |
| 44 |
|
| 45 |
columns, data = self.cmd.take_action(parsed_args) |
| 46 |
|
| 47 |
self.assertEqual(['Version', 'Type'], columns) |
| 48 |
self.assertEqual([('HOT123', 'hot'), ('CFN456', 'cfn')], list(data)) |
| 49 |
|
| 50 |
def test_version_list_with_aliases(self): |
| 51 |
ret_data = [ |
| 52 |
{'version': 'HOT123', 'type': 'hot', 'aliases': ['releasex']}, |
| 53 |
{'version': 'CFN456', 'type': 'cfn', 'aliases': ['releasey']}] |
| 54 |
self._stub_versions_list(ret_data) |
| 55 |
parsed_args = self.check_parser(self.cmd, [], []) |
| 56 |
|
| 57 |
columns, data = self.cmd.take_action(parsed_args) |
| 58 |
|
| 59 |
self.assertEqual(['Version', 'Type', 'Aliases'], columns) |
| 60 |
self.assertEqual([ |
| 61 |
('HOT123', 'hot', template.ListColumn(['releasex'])), |
| 62 |
('CFN456', 'cfn', template.ListColumn(['releasey'])) |
| 63 |
], list(data)) |
| 64 |
|
| 65 |
|
| 66 |
class TestTemplateFunctionList(TestTemplate): |
| 67 |
|
| 68 |
defaults = [ |
| 69 |
{'functions': 'func1', 'description': 'Function 1'}, |
| 70 |
{'functions': 'func2', 'description': 'Function 2'}, |
| 71 |
{'functions': 'condition func', 'description': 'Condition Function'} |
| 72 |
] |
| 73 |
|
| 74 |
def setUp(self): |
| 75 |
super().setUp() |
| 76 |
self.tv1 = template_versions.TemplateVersion(None, self.defaults[0]) |
| 77 |
self.tv2 = template_versions.TemplateVersion(None, self.defaults[1]) |
| 78 |
self.tv_with_cf = template_versions.TemplateVersion( |
| 79 |
None, self.defaults[2]) |
| 80 |
|
| 81 |
self.cmd = template.FunctionList(self.app, None) |
| 82 |
|
| 83 |
def test_function_list(self): |
| 84 |
arglist = ['version1'] |
| 85 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 86 |
self.template_versions.get.return_value = [self.tv1, self.tv2] |
| 87 |
columns, data = self.cmd.take_action(parsed_args) |
| 88 |
|
| 89 |
self.assertEqual(['Functions', 'Description'], columns) |
| 90 |
self.assertEqual([('func1', 'Function 1'), ('func2', 'Function 2')], |
| 91 |
list(data)) |
| 92 |
|
| 93 |
def test_function_list_with_condition_func(self): |
| 94 |
arglist = ['version1', '--with_conditions'] |
| 95 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 96 |
self.template_versions.get.return_value = [self.tv1, self.tv2, |
| 97 |
self.tv_with_cf] |
| 98 |
columns, data = self.cmd.take_action(parsed_args) |
| 99 |
|
| 100 |
self.assertEqual(['Functions', 'Description'], columns) |
| 101 |
self.assertEqual([('func1', 'Function 1'), |
| 102 |
('func2', 'Function 2'), |
| 103 |
('condition func', 'Condition Function')], |
| 104 |
list(data)) |
| 105 |
|
| 106 |
def test_function_list_not_found(self): |
| 107 |
arglist = ['bad_version'] |
| 108 |
self.template_versions.get.side_effect = exc.HTTPNotFound |
| 109 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 110 |
|
| 111 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 112 |
|
| 113 |
|
| 114 |
class TestTemplateValidate(TestTemplate): |
| 115 |
|
| 116 |
template_path = 'heatclient/tests/test_templates/empty.yaml' |
| 117 |
env_path = 'heatclient/tests/unit/var/environment.json' |
| 118 |
|
| 119 |
defaults = { |
| 120 |
'environment': {}, |
| 121 |
'files': {}, |
| 122 |
'parameters': {}, |
| 123 |
'template': {'heat_template_version': '2013-05-23'} |
| 124 |
} |
| 125 |
|
| 126 |
def setUp(self): |
| 127 |
super().setUp() |
| 128 |
self.stack_client = self.app.client_manager.orchestration.stacks |
| 129 |
self.stack_client.validate = mock.MagicMock(return_value={}) |
| 130 |
self.cmd = template.Validate(self.app, None) |
| 131 |
|
| 132 |
def test_validate(self): |
| 133 |
arglist = ['-t', self.template_path] |
| 134 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 135 |
columns, data = self.cmd.take_action(parsed_args) |
| 136 |
|
| 137 |
self.stack_client.validate.assert_called_once_with(**self.defaults) |
| 138 |
self.assertEqual([], columns) |
| 139 |
self.assertEqual([], data) |
| 140 |
|
| 141 |
def test_validate_env(self): |
| 142 |
arglist = ['-t', self.template_path, '-e', self.env_path] |
| 143 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 144 |
columns, data = self.cmd.take_action(parsed_args) |
| 145 |
|
| 146 |
self.assertEqual(1, self.stack_client.validate.call_count) |
| 147 |
args = self.stack_client.validate.call_args[1] |
| 148 |
self.assertEqual(args.get('environment'), {'parameters': {}}) |
| 149 |
self.assertIn(self.env_path, args.get('environment_files')[0]) |
| 150 |
self.assertEqual([], columns) |
| 151 |
self.assertEqual([], data) |
| 152 |
|
| 153 |
def test_validate_nested(self): |
| 154 |
arglist = ['-t', self.template_path, '--show-nested'] |
| 155 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 156 |
columns, data = self.cmd.take_action(parsed_args) |
| 157 |
args = dict(self.defaults) |
| 158 |
args['show_nested'] = True |
| 159 |
self.stack_client.validate.assert_called_once_with(**args) |
| 160 |
self.assertEqual([], columns) |
| 161 |
self.assertEqual([], data) |
| 162 |
|
| 163 |
def test_validate_parameter(self): |
| 164 |
arglist = ['-t', self.template_path, |
| 165 |
'--parameter', 'key1=value1', |
| 166 |
'--parameter', 'key2=value2'] |
| 167 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 168 |
columns, data = self.cmd.take_action(parsed_args) |
| 169 |
args = dict(self.defaults) |
| 170 |
args['parameters'] = {'key1': 'value1', 'key2': 'value2'} |
| 171 |
self.stack_client.validate.assert_called_once_with(**args) |
| 172 |
self.assertEqual([], columns) |
| 173 |
self.assertEqual([], data) |
| 174 |
|
| 175 |
def test_validate_ignore_errors(self): |
| 176 |
arglist = ['-t', self.template_path, |
| 177 |
'--ignore-errors', 'err1,err2'] |
| 178 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 179 |
columns, data = self.cmd.take_action(parsed_args) |
| 180 |
args = dict(self.defaults) |
| 181 |
args['ignore_errors'] = 'err1,err2' |
| 182 |
self.stack_client.validate.assert_called_once_with(**args) |
| 183 |
self.assertEqual([], columns) |
| 184 |
self.assertEqual([], data) |