| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
from osc_lib import exceptions as exc |
| 17 |
import yaml |
| 18 |
|
| 19 |
from heatclient import exc as heat_exc |
| 20 |
from heatclient.osc.v1 import software_config |
| 21 |
from heatclient.tests.unit.osc.v1 import fakes as orchestration_fakes |
| 22 |
from heatclient.v1 import software_configs |
| 23 |
|
| 24 |
|
| 25 |
class TestConfig(orchestration_fakes.TestOrchestrationv1): |
| 26 |
def setUp(self): |
| 27 |
super().setUp() |
| 28 |
self.mock_client = self.app.client_manager.orchestration |
| 29 |
|
| 30 |
|
| 31 |
class TestDeleteConfig(TestConfig): |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
super().setUp() |
| 35 |
self.cmd = software_config.DeleteConfig(self.app, None) |
| 36 |
self.mock_delete = self.mock_client.software_configs.delete |
| 37 |
|
| 38 |
def test_config_delete(self): |
| 39 |
arglist = ['id_123'] |
| 40 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 41 |
self.cmd.take_action(parsed_args) |
| 42 |
self.mock_delete.assert_called_with( |
| 43 |
config_id='id_123') |
| 44 |
|
| 45 |
def test_config_delete_multi(self): |
| 46 |
arglist = ['id_123', 'id_456'] |
| 47 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 48 |
self.cmd.take_action(parsed_args) |
| 49 |
self.mock_delete.assert_has_calls( |
| 50 |
[mock.call(config_id='id_123'), |
| 51 |
mock.call(config_id='id_456')]) |
| 52 |
|
| 53 |
def test_config_delete_not_found(self): |
| 54 |
arglist = ['id_123', 'id_456', 'id_789'] |
| 55 |
self.mock_client.software_configs.delete.side_effect = [ |
| 56 |
None, heat_exc.HTTPNotFound, None] |
| 57 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 58 |
error = self.assertRaises(exc.CommandError, self.cmd.take_action, |
| 59 |
parsed_args) |
| 60 |
self.mock_delete.assert_has_calls( |
| 61 |
[mock.call(config_id='id_123'), |
| 62 |
mock.call(config_id='id_456'), |
| 63 |
mock.call(config_id='id_789')]) |
| 64 |
self.assertEqual('Unable to delete 1 of the 3 software configs.', |
| 65 |
str(error)) |
| 66 |
|
| 67 |
|
| 68 |
class TestListConfig(TestConfig): |
| 69 |
|
| 70 |
def setUp(self): |
| 71 |
super().setUp() |
| 72 |
self.cmd = software_config.ListConfig(self.app, None) |
| 73 |
self.mock_client.software_configs.list.return_value = [ |
| 74 |
software_configs.SoftwareConfig(None, {})] |
| 75 |
|
| 76 |
def test_config_list(self): |
| 77 |
arglist = [] |
| 78 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 79 |
self.cmd.take_action(parsed_args) |
| 80 |
self.mock_client.software_configs.list.assert_called_once_with() |
| 81 |
|
| 82 |
def test_config_list_limit(self): |
| 83 |
arglist = ['--limit', '3'] |
| 84 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 85 |
self.cmd.take_action(parsed_args) |
| 86 |
self.mock_client.software_configs.list.assert_called_with(limit=3) |
| 87 |
|
| 88 |
def test_config_list_marker(self): |
| 89 |
arglist = ['--marker', 'id123'] |
| 90 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 91 |
self.cmd.take_action(parsed_args) |
| 92 |
self.mock_client.software_configs.list.assert_called_with( |
| 93 |
marker='id123') |
| 94 |
|
| 95 |
|
| 96 |
class TestCreateConfig(TestConfig): |
| 97 |
|
| 98 |
def setUp(self): |
| 99 |
super().setUp() |
| 100 |
self.cmd = software_config.CreateConfig(self.app, None) |
| 101 |
self.mock_client.software_configs.create.return_value = \ |
| 102 |
software_configs.SoftwareConfig(None, {}) |
| 103 |
|
| 104 |
def test_config_create(self): |
| 105 |
properties = { |
| 106 |
'config': '', |
| 107 |
'group': 'Heat::Ungrouped', |
| 108 |
'name': 'test', |
| 109 |
'options': {}, |
| 110 |
'inputs': [], |
| 111 |
'outputs': [] |
| 112 |
} |
| 113 |
arglist = ['test'] |
| 114 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 115 |
columns, rows = self.cmd.take_action(parsed_args) |
| 116 |
self.mock_client.stacks.validate.assert_called_with(**{ |
| 117 |
'template': { |
| 118 |
'heat_template_version': '2013-05-23', |
| 119 |
'resources': { |
| 120 |
'test': { |
| 121 |
'type': 'OS::Heat::SoftwareConfig', |
| 122 |
'properties': properties}}}}) |
| 123 |
self.mock_client.software_configs.create.assert_called_with( |
| 124 |
**properties) |
| 125 |
|
| 126 |
def test_config_create_group(self): |
| 127 |
properties = { |
| 128 |
'config': '', |
| 129 |
'group': 'group', |
| 130 |
'name': 'test', |
| 131 |
'options': {}, |
| 132 |
'inputs': [], |
| 133 |
'outputs': [] |
| 134 |
} |
| 135 |
arglist = ['test', '--group', 'group'] |
| 136 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 137 |
columns, rows = self.cmd.take_action(parsed_args) |
| 138 |
self.mock_client.stacks.validate.assert_called_with(**{ |
| 139 |
'template': { |
| 140 |
'heat_template_version': '2013-05-23', |
| 141 |
'resources': { |
| 142 |
'test': { |
| 143 |
'type': 'OS::Heat::SoftwareConfig', |
| 144 |
'properties': properties}}}}) |
| 145 |
self.mock_client.software_configs.create.assert_called_with( |
| 146 |
**properties) |
| 147 |
|
| 148 |
@mock.patch('urllib.request.urlopen') |
| 149 |
def test_config_create_config_file(self, urlopen): |
| 150 |
properties = { |
| 151 |
'config': 'config', |
| 152 |
'group': 'Heat::Ungrouped', |
| 153 |
'name': 'test', |
| 154 |
'options': {}, |
| 155 |
'inputs': [], |
| 156 |
'outputs': [] |
| 157 |
} |
| 158 |
data = mock.Mock() |
| 159 |
data.read.side_effect = ['config'] |
| 160 |
urlopen.return_value = data |
| 161 |
|
| 162 |
arglist = ['test', '--config-file', 'config_file'] |
| 163 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 164 |
columns, rows = self.cmd.take_action(parsed_args) |
| 165 |
self.mock_client.stacks.validate.assert_called_with(**{ |
| 166 |
'template': { |
| 167 |
'heat_template_version': '2013-05-23', |
| 168 |
'resources': { |
| 169 |
'test': { |
| 170 |
'type': 'OS::Heat::SoftwareConfig', |
| 171 |
'properties': properties}}}}) |
| 172 |
self.mock_client.software_configs.create.assert_called_with( |
| 173 |
**properties) |
| 174 |
|
| 175 |
@mock.patch('urllib.request.urlopen') |
| 176 |
def test_config_create_definition_file(self, urlopen): |
| 177 |
definition = { |
| 178 |
'inputs': [ |
| 179 |
{'name': 'input'}, |
| 180 |
], |
| 181 |
'outputs': [ |
| 182 |
{'name': 'output'} |
| 183 |
], |
| 184 |
'options': {'option': 'value'} |
| 185 |
} |
| 186 |
|
| 187 |
properties = { |
| 188 |
'config': '', |
| 189 |
'group': 'Heat::Ungrouped', |
| 190 |
'name': 'test' |
| 191 |
} |
| 192 |
properties.update(definition) |
| 193 |
|
| 194 |
data = mock.Mock() |
| 195 |
data.read.side_effect = [yaml.safe_dump(definition)] |
| 196 |
urlopen.return_value = data |
| 197 |
|
| 198 |
arglist = ['test', '--definition-file', 'definition-file'] |
| 199 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 200 |
columns, rows = self.cmd.take_action(parsed_args) |
| 201 |
self.mock_client.stacks.validate.assert_called_with(**{ |
| 202 |
'template': { |
| 203 |
'heat_template_version': '2013-05-23', |
| 204 |
'resources': { |
| 205 |
'test': { |
| 206 |
'type': 'OS::Heat::SoftwareConfig', |
| 207 |
'properties': properties}}}}) |
| 208 |
self.mock_client.software_configs.create.assert_called_with( |
| 209 |
**properties) |
| 210 |
|
| 211 |
|
| 212 |
class TestConfigShow(TestConfig): |
| 213 |
columns = ( |
| 214 |
'id', |
| 215 |
'name', |
| 216 |
'group', |
| 217 |
'config', |
| 218 |
'inputs', |
| 219 |
'outputs', |
| 220 |
'options', |
| 221 |
'creation_time', |
| 222 |
) |
| 223 |
|
| 224 |
data = ( |
| 225 |
'96dfee3f-27b7-42ae-a03e-966226871ae6', |
| 226 |
'test', |
| 227 |
'Heat::Ungrouped', |
| 228 |
'', |
| 229 |
[], |
| 230 |
[], |
| 231 |
{}, |
| 232 |
'2015-12-09T11:55:06', |
| 233 |
) |
| 234 |
|
| 235 |
response = dict(zip(columns, data)) |
| 236 |
|
| 237 |
def setUp(self): |
| 238 |
super().setUp() |
| 239 |
self.cmd = software_config.ShowConfig(self.app, None) |
| 240 |
self.mock_client.software_configs.get.return_value = \ |
| 241 |
software_configs.SoftwareConfig(None, self.response) |
| 242 |
|
| 243 |
def test_config_show(self): |
| 244 |
arglist = ['96dfee3f-27b7-42ae-a03e-966226871ae6'] |
| 245 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 246 |
columns, data = self.cmd.take_action(parsed_args) |
| 247 |
self.mock_client.software_configs.get.assert_called_with(**{ |
| 248 |
'config_id': '96dfee3f-27b7-42ae-a03e-966226871ae6', |
| 249 |
}) |
| 250 |
self.assertEqual(self.columns, columns) |
| 251 |
self.assertEqual(self.data, data) |
| 252 |
|
| 253 |
def test_config_show_config_only(self): |
| 254 |
arglist = ['--config-only', '96dfee3f-27b7-42ae-a03e-966226871ae6'] |
| 255 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 256 |
columns, data = self.cmd.take_action(parsed_args) |
| 257 |
self.mock_client.software_configs.get.assert_called_with(**{ |
| 258 |
'config_id': '96dfee3f-27b7-42ae-a03e-966226871ae6', |
| 259 |
}) |
| 260 |
self.assertIsNone(columns) |
| 261 |
self.assertIsNone(data) |
| 262 |
|
| 263 |
def test_config_show_not_found(self): |
| 264 |
arglist = ['96dfee3f-27b7-42ae-a03e-966226871ae6'] |
| 265 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 266 |
self.mock_client.software_configs.get.side_effect = \ |
| 267 |
heat_exc.HTTPNotFound() |
| 268 |
self.assertRaises( |
| 269 |
exc.CommandError, |
| 270 |
self.cmd.take_action, |
| 271 |
parsed_args) |