| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
import copy |
| 15 |
import io |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
from osc_lib import exceptions as exc |
| 19 |
from osc_lib import utils |
| 20 |
import testscenarios |
| 21 |
import yaml |
| 22 |
|
| 23 |
from heatclient.common import template_format |
| 24 |
from heatclient import exc as heat_exc |
| 25 |
from heatclient.osc.v1 import stack |
| 26 |
from heatclient.tests import inline_templates |
| 27 |
from heatclient.tests.unit.osc.v1 import fakes as orchestration_fakes |
| 28 |
from heatclient.v1 import events |
| 29 |
from heatclient.v1 import resources |
| 30 |
from heatclient.v1 import stacks |
| 31 |
|
| 32 |
load_tests = testscenarios.load_tests_apply_scenarios |
| 33 |
|
| 34 |
|
| 35 |
class TestStack(orchestration_fakes.TestOrchestrationv1): |
| 36 |
def setUp(self): |
| 37 |
super().setUp() |
| 38 |
self.mock_client = self.app.client_manager.orchestration |
| 39 |
self.stack_client = self.app.client_manager.orchestration.stacks |
| 40 |
self.projects_mock = self.app.client_manager.identity.projects |
| 41 |
self.projects_mock.reset_mock() |
| 42 |
|
| 43 |
|
| 44 |
class TestStackCreate(TestStack): |
| 45 |
|
| 46 |
template_path = 'heatclient/tests/test_templates/empty.yaml' |
| 47 |
env_path = 'heatclient/tests/unit/var/environment.json' |
| 48 |
|
| 49 |
defaults = { |
| 50 |
'stack_name': 'my_stack', |
| 51 |
'disable_rollback': True, |
| 52 |
'parameters': {}, |
| 53 |
'template': {'heat_template_version': '2013-05-23'}, |
| 54 |
'files': {}, |
| 55 |
'environment': {} |
| 56 |
} |
| 57 |
|
| 58 |
def setUp(self): |
| 59 |
super().setUp() |
| 60 |
self.cmd = stack.CreateStack(self.app, None) |
| 61 |
self.stack_client.create.return_value = {'stack': {'id': '1234'}} |
| 62 |
self.stack_client.get.return_value = { |
| 63 |
'stack_status': 'create_complete'} |
| 64 |
self.stack_client.preview.return_value = stacks.Stack( |
| 65 |
None, {'stack': {'id', '1234'}}) |
| 66 |
stack._authenticated_fetcher = mock.MagicMock() |
| 67 |
|
| 68 |
def test_stack_create_defaults(self): |
| 69 |
arglist = ['my_stack', '-t', self.template_path] |
| 70 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 71 |
|
| 72 |
self.cmd.take_action(parsed_args) |
| 73 |
|
| 74 |
self.stack_client.create.assert_called_with(**self.defaults) |
| 75 |
|
| 76 |
def test_stack_create_with_env(self): |
| 77 |
arglist = ['my_stack', '-t', self.template_path, '-e', self.env_path] |
| 78 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 79 |
self.cmd.take_action(parsed_args) |
| 80 |
|
| 81 |
self.assertEqual(1, self.stack_client.create.call_count) |
| 82 |
args = self.stack_client.create.call_args[1] |
| 83 |
self.assertEqual({'parameters': {}}, args.get('environment')) |
| 84 |
self.assertIn(self.env_path, args.get('environment_files')[0]) |
| 85 |
|
| 86 |
def test_stack_create_rollback(self): |
| 87 |
arglist = ['my_stack', '-t', self.template_path, '--enable-rollback'] |
| 88 |
kwargs = copy.deepcopy(self.defaults) |
| 89 |
kwargs['disable_rollback'] = False |
| 90 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 91 |
|
| 92 |
self.cmd.take_action(parsed_args) |
| 93 |
|
| 94 |
self.stack_client.create.assert_called_with(**kwargs) |
| 95 |
|
| 96 |
def test_stack_create_parameters(self): |
| 97 |
template_path = ('/'.join(self.template_path.split('/')[:-1]) + |
| 98 |
'/parameters.yaml') |
| 99 |
arglist = ['my_stack', '-t', template_path, '--parameter', 'p1=a', |
| 100 |
'--parameter', 'p2=6'] |
| 101 |
kwargs = copy.deepcopy(self.defaults) |
| 102 |
kwargs['parameters'] = {'p1': 'a', 'p2': '6'} |
| 103 |
kwargs['template']['parameters'] = {'p1': {'type': 'string'}, |
| 104 |
'p2': {'type': 'number'}} |
| 105 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 106 |
|
| 107 |
self.cmd.take_action(parsed_args) |
| 108 |
|
| 109 |
self.stack_client.create.assert_called_with(**kwargs) |
| 110 |
|
| 111 |
def test_stack_create_tags(self): |
| 112 |
arglist = ['my_stack', '-t', self.template_path, '--tags', 'tag1,tag2'] |
| 113 |
kwargs = copy.deepcopy(self.defaults) |
| 114 |
kwargs['tags'] = 'tag1,tag2' |
| 115 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 116 |
|
| 117 |
self.cmd.take_action(parsed_args) |
| 118 |
|
| 119 |
self.stack_client.create.assert_called_with(**kwargs) |
| 120 |
|
| 121 |
def test_stack_create_timeout(self): |
| 122 |
arglist = ['my_stack', '-t', self.template_path, '--timeout', '60'] |
| 123 |
kwargs = copy.deepcopy(self.defaults) |
| 124 |
kwargs['timeout_mins'] = 60 |
| 125 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 126 |
|
| 127 |
self.cmd.take_action(parsed_args) |
| 128 |
|
| 129 |
self.stack_client.create.assert_called_with(**kwargs) |
| 130 |
|
| 131 |
def test_stack_create_pre_create(self): |
| 132 |
arglist = ['my_stack', '-t', self.template_path, '--pre-create', 'a'] |
| 133 |
kwargs = copy.deepcopy(self.defaults) |
| 134 |
kwargs['environment'] = { |
| 135 |
'resource_registry': {'resources': {'a': {'hooks': 'pre-create'}}} |
| 136 |
} |
| 137 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 138 |
|
| 139 |
self.cmd.take_action(parsed_args) |
| 140 |
|
| 141 |
self.stack_client.create.assert_called_with(**kwargs) |
| 142 |
|
| 143 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 144 |
return_value=('CREATE_COMPLETE', |
| 145 |
'Stack my_stack CREATE_COMPLETE')) |
| 146 |
def test_stack_create_wait(self, mock_poll): |
| 147 |
arglist = ['my_stack', '-t', self.template_path, '--wait'] |
| 148 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 149 |
|
| 150 |
self.cmd.take_action(parsed_args) |
| 151 |
|
| 152 |
mock_poll.assert_called_once_with(mock.ANY, 'my_stack', |
| 153 |
action='CREATE', poll_period=5) |
| 154 |
self.stack_client.create.assert_called_with(**self.defaults) |
| 155 |
self.stack_client.get.assert_called_with(**{'stack_id': '1234', |
| 156 |
'resolve_outputs': False}) |
| 157 |
|
| 158 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 159 |
return_value=('CREATE_COMPLETE', |
| 160 |
'Stack my_stack CREATE_COMPLETE')) |
| 161 |
def test_stack_create_wait_with_poll(self, mock_poll): |
| 162 |
arglist = ['my_stack', '-t', self.template_path, '--wait', |
| 163 |
'--poll', '10'] |
| 164 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 165 |
|
| 166 |
self.cmd.take_action(parsed_args) |
| 167 |
|
| 168 |
mock_poll.assert_called_once_with(mock.ANY, 'my_stack', |
| 169 |
action='CREATE', poll_period=10) |
| 170 |
self.stack_client.create.assert_called_with(**self.defaults) |
| 171 |
self.stack_client.get.assert_called_with(**{'stack_id': '1234', |
| 172 |
'resolve_outputs': False}) |
| 173 |
|
| 174 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 175 |
return_value=('CREATE_FAILED', |
| 176 |
'Stack my_stack CREATE_FAILED')) |
| 177 |
def test_stack_create_wait_fail(self, mock_wait): |
| 178 |
arglist = ['my_stack', '-t', self.template_path, '--wait'] |
| 179 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 180 |
|
| 181 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 182 |
|
| 183 |
def test_stack_create_dry_run(self): |
| 184 |
arglist = ['my_stack', '-t', self.template_path, '--dry-run'] |
| 185 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 186 |
|
| 187 |
self.cmd.take_action(parsed_args) |
| 188 |
|
| 189 |
self.stack_client.preview.assert_called_with(**self.defaults) |
| 190 |
self.stack_client.create.assert_not_called() |
| 191 |
|
| 192 |
|
| 193 |
class TestStackUpdate(TestStack): |
| 194 |
|
| 195 |
template_path = 'heatclient/tests/test_templates/empty.yaml' |
| 196 |
env_path = 'heatclient/tests/unit/var/environment.json' |
| 197 |
|
| 198 |
defaults = { |
| 199 |
'stack_id': 'my_stack', |
| 200 |
'environment': {}, |
| 201 |
'existing': False, |
| 202 |
'files': {}, |
| 203 |
'template': {'heat_template_version': '2013-05-23'}, |
| 204 |
'parameters': {}, |
| 205 |
} |
| 206 |
|
| 207 |
def setUp(self): |
| 208 |
super().setUp() |
| 209 |
self.cmd = stack.UpdateStack(self.app, None) |
| 210 |
self.stack_client.update.return_value = {'stack': {'id': '1234'}} |
| 211 |
self.stack_client.preview_update.return_value = { |
| 212 |
'resource_changes': {'added': [], |
| 213 |
'deleted': [], |
| 214 |
'replaced': [], |
| 215 |
'unchanged': [], |
| 216 |
'updated': []}} |
| 217 |
self.stack_client.get.return_value = { |
| 218 |
'stack_status': 'create_complete'} |
| 219 |
|
| 220 |
def test_stack_update_defaults(self): |
| 221 |
arglist = ['my_stack', '-t', self.template_path] |
| 222 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 223 |
|
| 224 |
self.cmd.take_action(parsed_args) |
| 225 |
|
| 226 |
self.stack_client.update.assert_called_with(**self.defaults) |
| 227 |
|
| 228 |
def test_stack_update_with_env(self): |
| 229 |
arglist = ['my_stack', '-t', self.template_path, '-e', self.env_path] |
| 230 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 231 |
self.cmd.take_action(parsed_args) |
| 232 |
|
| 233 |
self.assertEqual(1, self.stack_client.update.call_count) |
| 234 |
args = self.stack_client.update.call_args[1] |
| 235 |
self.assertEqual({'parameters': {}}, args.get('environment')) |
| 236 |
self.assertIn(self.env_path, args.get('environment_files')[0]) |
| 237 |
|
| 238 |
def test_stack_update_rollback_enabled(self): |
| 239 |
arglist = ['my_stack', '-t', self.template_path, '--rollback', |
| 240 |
'enabled'] |
| 241 |
kwargs = copy.deepcopy(self.defaults) |
| 242 |
kwargs['disable_rollback'] = False |
| 243 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 244 |
|
| 245 |
self.cmd.take_action(parsed_args) |
| 246 |
|
| 247 |
self.stack_client.update.assert_called_with(**kwargs) |
| 248 |
|
| 249 |
def test_stack_update_rollback_disabled(self): |
| 250 |
arglist = ['my_stack', '-t', self.template_path, '--rollback', |
| 251 |
'disabled'] |
| 252 |
kwargs = copy.deepcopy(self.defaults) |
| 253 |
kwargs['disable_rollback'] = True |
| 254 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 255 |
|
| 256 |
self.cmd.take_action(parsed_args) |
| 257 |
|
| 258 |
self.stack_client.update.assert_called_with(**kwargs) |
| 259 |
|
| 260 |
def test_stack_update_rollback_keep(self): |
| 261 |
arglist = ['my_stack', '-t', self.template_path, '--rollback', |
| 262 |
'keep'] |
| 263 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 264 |
|
| 265 |
self.cmd.take_action(parsed_args) |
| 266 |
|
| 267 |
self.assertNotIn('disable_rollback', self.defaults) |
| 268 |
self.stack_client.update.assert_called_with(**self.defaults) |
| 269 |
|
| 270 |
def test_stack_update_parameters(self): |
| 271 |
template_path = ('/'.join(self.template_path.split('/')[:-1]) + |
| 272 |
'/parameters.yaml') |
| 273 |
arglist = ['my_stack', '-t', template_path, '--parameter', 'p1=a', |
| 274 |
'--parameter', 'p2=6'] |
| 275 |
kwargs = copy.deepcopy(self.defaults) |
| 276 |
kwargs['parameters'] = {'p1': 'a', 'p2': '6'} |
| 277 |
kwargs['template']['parameters'] = {'p1': {'type': 'string'}, |
| 278 |
'p2': {'type': 'number'}} |
| 279 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 280 |
|
| 281 |
self.cmd.take_action(parsed_args) |
| 282 |
|
| 283 |
self.stack_client.update.assert_called_with(**kwargs) |
| 284 |
|
| 285 |
def test_stack_update_clear_parameters(self): |
| 286 |
arglist = ['my_stack', '-t', self.template_path, '--clear-parameter', |
| 287 |
'a'] |
| 288 |
kwargs = copy.deepcopy(self.defaults) |
| 289 |
kwargs['clear_parameters'] = ['a'] |
| 290 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 291 |
|
| 292 |
self.cmd.take_action(parsed_args) |
| 293 |
|
| 294 |
self.stack_client.update.assert_called_with(**kwargs) |
| 295 |
|
| 296 |
def test_stack_update_tags(self): |
| 297 |
arglist = ['my_stack', '-t', self.template_path, '--tags', 'tag1,tag2'] |
| 298 |
kwargs = copy.deepcopy(self.defaults) |
| 299 |
kwargs['tags'] = 'tag1,tag2' |
| 300 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 301 |
|
| 302 |
self.cmd.take_action(parsed_args) |
| 303 |
|
| 304 |
self.stack_client.update.assert_called_with(**kwargs) |
| 305 |
|
| 306 |
def test_stack_update_timeout(self): |
| 307 |
arglist = ['my_stack', '-t', self.template_path, '--timeout', '60'] |
| 308 |
kwargs = copy.deepcopy(self.defaults) |
| 309 |
kwargs['timeout_mins'] = 60 |
| 310 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 311 |
|
| 312 |
self.cmd.take_action(parsed_args) |
| 313 |
|
| 314 |
self.stack_client.update.assert_called_with(**kwargs) |
| 315 |
|
| 316 |
def test_stack_update_converge(self): |
| 317 |
arglist = ['my_stack', '-t', self.template_path, '--converge'] |
| 318 |
kwargs = copy.deepcopy(self.defaults) |
| 319 |
kwargs['converge'] = True |
| 320 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 321 |
|
| 322 |
self.cmd.take_action(parsed_args) |
| 323 |
|
| 324 |
self.stack_client.update.assert_called_with(**kwargs) |
| 325 |
|
| 326 |
def test_stack_update_pre_update(self): |
| 327 |
arglist = ['my_stack', '-t', self.template_path, '--pre-update', 'a'] |
| 328 |
kwargs = copy.deepcopy(self.defaults) |
| 329 |
kwargs['environment'] = { |
| 330 |
'resource_registry': {'resources': {'a': {'hooks': 'pre-update'}}} |
| 331 |
} |
| 332 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 333 |
|
| 334 |
self.cmd.take_action(parsed_args) |
| 335 |
|
| 336 |
self.stack_client.update.assert_called_with(**kwargs) |
| 337 |
|
| 338 |
def test_stack_update_existing(self): |
| 339 |
arglist = ['my_stack', '-t', self.template_path, '--existing'] |
| 340 |
kwargs = copy.deepcopy(self.defaults) |
| 341 |
kwargs['existing'] = True |
| 342 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 343 |
|
| 344 |
self.cmd.take_action(parsed_args) |
| 345 |
|
| 346 |
self.stack_client.update.assert_called_with(**kwargs) |
| 347 |
|
| 348 |
def test_stack_update_dry_run(self): |
| 349 |
arglist = ['my_stack', '-t', self.template_path, '--dry-run'] |
| 350 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 351 |
|
| 352 |
self.cmd.take_action(parsed_args) |
| 353 |
|
| 354 |
self.stack_client.preview_update.assert_called_with(**self.defaults) |
| 355 |
self.stack_client.update.assert_not_called() |
| 356 |
|
| 357 |
def test_stack_update_dry_run_show_nested(self): |
| 358 |
arglist = ['my_stack', '-t', self.template_path, '--dry-run', |
| 359 |
'--show-nested'] |
| 360 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 361 |
|
| 362 |
self.cmd.take_action(parsed_args) |
| 363 |
|
| 364 |
self.stack_client.preview_update.assert_called_with( |
| 365 |
show_nested=True, **self.defaults) |
| 366 |
self.stack_client.update.assert_not_called() |
| 367 |
|
| 368 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 369 |
return_value=('UPDATE_COMPLETE', |
| 370 |
'Stack my_stack UPDATE_COMPLETE')) |
| 371 |
@mock.patch('heatclient.common.event_utils.get_events', return_value=[]) |
| 372 |
def test_stack_update_wait(self, ge, mock_poll): |
| 373 |
arglist = ['my_stack', '-t', self.template_path, '--wait'] |
| 374 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 375 |
self.stack_client.get.return_value = mock.MagicMock( |
| 376 |
stack_name='my_stack') |
| 377 |
|
| 378 |
self.cmd.take_action(parsed_args) |
| 379 |
|
| 380 |
self.stack_client.update.assert_called_with(**self.defaults) |
| 381 |
self.stack_client.get.assert_called_with(**{'stack_id': 'my_stack', |
| 382 |
'resolve_outputs': False}) |
| 383 |
|
| 384 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 385 |
return_value=('UPDATE_FAILED', |
| 386 |
'Stack my_stack UPDATE_FAILED')) |
| 387 |
@mock.patch('heatclient.common.event_utils.get_events', return_value=[]) |
| 388 |
def test_stack_update_wait_fail(self, ge, mock_poll): |
| 389 |
arglist = ['my_stack', '-t', self.template_path, '--wait'] |
| 390 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 391 |
self.stack_client.get.return_value = mock.MagicMock( |
| 392 |
stack_name='my_stack') |
| 393 |
|
| 394 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 395 |
|
| 396 |
|
| 397 |
class TestStackShow(TestStack): |
| 398 |
|
| 399 |
scenarios = [ |
| 400 |
('table', dict( |
| 401 |
format='table')), |
| 402 |
('shell', dict( |
| 403 |
format='shell')), |
| 404 |
('value', dict( |
| 405 |
format='value')), |
| 406 |
] |
| 407 |
|
| 408 |
get_response = {"stack": { |
| 409 |
"disable_rollback": True, |
| 410 |
"description": "This is a\ndescription\n", |
| 411 |
"parent": None, |
| 412 |
"tags": None, |
| 413 |
"stack_name": "a", |
| 414 |
"stack_user_project_id": "02ad9bd403d44ff9ba128cf9ce77f989", |
| 415 |
"stack_status_reason": "Stack UPDATE completed successfully", |
| 416 |
"creation_time": "2015-08-04T04:46:10", |
| 417 |
"links": [{ |
| 418 |
"href": "http://192.0.2.1:8004/v1/5dcd28/stacks/a/4af43781", |
| 419 |
"rel": "self" |
| 420 |
}], |
| 421 |
"capabilities": [], |
| 422 |
"notification_topics": [], |
| 423 |
"updated_time": "2015-08-05T21:33:28", |
| 424 |
"timeout_mins": None, |
| 425 |
"stack_status": "UPDATE_COMPLETE", |
| 426 |
"stack_owner": None, |
| 427 |
"parameters": { |
| 428 |
"OS::project_id": "e0e5e140c5854c259a852621b65dcd28", |
| 429 |
"OS::stack_id": "4af43781", |
| 430 |
"OS::stack_name": "a" |
| 431 |
}, |
| 432 |
"id": "4af43781", |
| 433 |
"outputs": [], |
| 434 |
"template_description": "This is a\ndescription\n"} |
| 435 |
} |
| 436 |
|
| 437 |
def setUp(self): |
| 438 |
super().setUp() |
| 439 |
self.cmd = stack.ShowStack(self.app, None) |
| 440 |
self.stack_client.get.return_value = stacks.Stack( |
| 441 |
None, self.get_response) |
| 442 |
|
| 443 |
def test_stack_show(self): |
| 444 |
arglist = ['--format', self.format, 'my_stack'] |
| 445 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 446 |
self.cmd.take_action(parsed_args) |
| 447 |
self.stack_client.get.assert_called_with(**{ |
| 448 |
'stack_id': 'my_stack', |
| 449 |
'resolve_outputs': True, |
| 450 |
}) |
| 451 |
|
| 452 |
def test_stack_show_explicit_no_resolve(self): |
| 453 |
arglist = ['--no-resolve-outputs', '--format', self.format, 'my_stack'] |
| 454 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 455 |
self.cmd.take_action(parsed_args) |
| 456 |
self.stack_client.get.assert_called_with(**{ |
| 457 |
'stack_id': 'my_stack', |
| 458 |
'resolve_outputs': False, |
| 459 |
}) |
| 460 |
|
| 461 |
def test_stack_show_short(self): |
| 462 |
expected = ['id', 'stack_name', 'description', 'creation_time', |
| 463 |
'updated_time', 'stack_status', 'stack_status_reason'] |
| 464 |
|
| 465 |
columns, data = stack._show_stack(self.mock_client, 'my_stack', |
| 466 |
short=True) |
| 467 |
|
| 468 |
self.assertEqual(expected, columns) |
| 469 |
|
| 470 |
|
| 471 |
class TestStackList(TestStack): |
| 472 |
|
| 473 |
defaults = { |
| 474 |
'limit': None, |
| 475 |
'marker': None, |
| 476 |
'filters': {}, |
| 477 |
'tags': None, |
| 478 |
'tags_any': None, |
| 479 |
'not_tags': None, |
| 480 |
'not_tags_any': None, |
| 481 |
'global_tenant': False, |
| 482 |
'show_deleted': False, |
| 483 |
'show_hidden': False, |
| 484 |
} |
| 485 |
|
| 486 |
columns = ['ID', 'Stack Name', 'Stack Status', 'Creation Time', |
| 487 |
'Updated Time'] |
| 488 |
|
| 489 |
data = { |
| 490 |
'id': '1234', |
| 491 |
'stack_name': 'my_stack', |
| 492 |
'stack_status': 'CREATE_COMPLETE', |
| 493 |
'creation_time': '2015-10-21T07:28:00Z', |
| 494 |
'update_time': '2015-10-21T07:30:00Z', |
| 495 |
'deletion_time': '2015-10-21T07:50:00Z', |
| 496 |
} |
| 497 |
|
| 498 |
data_with_project = copy.deepcopy(data) |
| 499 |
data_with_project['project'] = 'test_project' |
| 500 |
|
| 501 |
def setUp(self): |
| 502 |
super().setUp() |
| 503 |
self.cmd = stack.ListStack(self.app, None) |
| 504 |
self.stack_client.list.return_value = [stacks.Stack(None, self.data)] |
| 505 |
utils.get_dict_properties = mock.MagicMock(return_value='') |
| 506 |
|
| 507 |
def test_stack_list_defaults(self): |
| 508 |
arglist = [] |
| 509 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 510 |
|
| 511 |
columns, data = self.cmd.take_action(parsed_args) |
| 512 |
|
| 513 |
self.stack_client.list.assert_called_with(**self.defaults) |
| 514 |
self.assertEqual(self.columns, columns) |
| 515 |
|
| 516 |
def test_stack_list_nested(self): |
| 517 |
kwargs = copy.deepcopy(self.defaults) |
| 518 |
kwargs['show_nested'] = True |
| 519 |
cols = copy.deepcopy(self.columns) |
| 520 |
cols.append('Parent') |
| 521 |
arglist = ['--nested'] |
| 522 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 523 |
|
| 524 |
columns, data = self.cmd.take_action(parsed_args) |
| 525 |
|
| 526 |
self.stack_client.list.assert_called_with(**kwargs) |
| 527 |
self.assertEqual(cols, columns) |
| 528 |
|
| 529 |
def test_stack_list_deleted(self): |
| 530 |
kwargs = copy.deepcopy(self.defaults) |
| 531 |
kwargs['show_deleted'] = True |
| 532 |
cols = copy.deepcopy(self.columns) |
| 533 |
cols.append('Deletion Time') |
| 534 |
arglist = ['--deleted'] |
| 535 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 536 |
|
| 537 |
columns, data = self.cmd.take_action(parsed_args) |
| 538 |
|
| 539 |
self.stack_client.list.assert_called_with(**kwargs) |
| 540 |
self.assertEqual(cols, columns) |
| 541 |
|
| 542 |
def test_stack_list_all_projects(self): |
| 543 |
self.stack_client.list.return_value = [ |
| 544 |
stacks.Stack(None, self.data_with_project)] |
| 545 |
kwargs = copy.deepcopy(self.defaults) |
| 546 |
kwargs['global_tenant'] = True |
| 547 |
cols = copy.deepcopy(self.columns) |
| 548 |
cols.insert(2, 'Project') |
| 549 |
arglist = ['--all-projects'] |
| 550 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 551 |
|
| 552 |
columns, data = self.cmd.take_action(parsed_args) |
| 553 |
|
| 554 |
self.stack_client.list.assert_called_with(**kwargs) |
| 555 |
self.assertEqual(cols, columns) |
| 556 |
|
| 557 |
def test_stack_list_with_project(self): |
| 558 |
self.stack_client.list.return_value = [ |
| 559 |
stacks.Stack(None, self.data_with_project)] |
| 560 |
kwargs = copy.deepcopy(self.defaults) |
| 561 |
cols = copy.deepcopy(self.columns) |
| 562 |
cols.insert(2, 'Project') |
| 563 |
arglist = [] |
| 564 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 565 |
|
| 566 |
columns, data = self.cmd.take_action(parsed_args) |
| 567 |
|
| 568 |
self.stack_client.list.assert_called_with(**kwargs) |
| 569 |
self.assertEqual(cols, columns) |
| 570 |
|
| 571 |
def test_stack_list_filter_project(self): |
| 572 |
kwargs = copy.deepcopy(self.defaults) |
| 573 |
project_id = '123456' |
| 574 |
self.projects_mock.get.return_value = ( |
| 575 |
mock.MagicMock(id=project_id)) |
| 576 |
kwargs['tenant'] = project_id |
| 577 |
arglist = ['--project', project_id] |
| 578 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 579 |
|
| 580 |
columns, data = self.cmd.take_action(parsed_args) |
| 581 |
|
| 582 |
self.stack_client.list.assert_called_with(**kwargs) |
| 583 |
|
| 584 |
def test_stack_list_long(self): |
| 585 |
self.stack_client.list.return_value = [ |
| 586 |
stacks.Stack(None, self.data_with_project)] |
| 587 |
kwargs = copy.deepcopy(self.defaults) |
| 588 |
kwargs['global_tenant'] = True |
| 589 |
cols = copy.deepcopy(self.columns) |
| 590 |
cols.insert(2, 'Stack Owner') |
| 591 |
cols.insert(2, 'Project') |
| 592 |
arglist = ['--long'] |
| 593 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 594 |
|
| 595 |
columns, data = self.cmd.take_action(parsed_args) |
| 596 |
|
| 597 |
self.stack_client.list.assert_called_with(**kwargs) |
| 598 |
self.assertEqual(cols, columns) |
| 599 |
|
| 600 |
def test_stack_list_short(self): |
| 601 |
cols = ['ID', 'Stack Name', 'Stack Status'] |
| 602 |
arglist = ['--short'] |
| 603 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 604 |
|
| 605 |
columns, data = self.cmd.take_action(parsed_args) |
| 606 |
|
| 607 |
self.stack_client.list.assert_called_with(**self.defaults) |
| 608 |
self.assertEqual(cols, columns) |
| 609 |
|
| 610 |
def test_stack_list_sort(self): |
| 611 |
arglist = ['--sort', 'stack_name:desc,id'] |
| 612 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 613 |
|
| 614 |
columns, data = self.cmd.take_action(parsed_args) |
| 615 |
|
| 616 |
self.stack_client.list.assert_called_with(**self.defaults) |
| 617 |
self.assertEqual(self.columns, columns) |
| 618 |
|
| 619 |
def test_stack_list_sort_invalid_key(self): |
| 620 |
arglist = ['--sort', 'bad_key'] |
| 621 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 622 |
|
| 623 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 624 |
|
| 625 |
def test_stack_list_tags(self): |
| 626 |
kwargs = copy.deepcopy(self.defaults) |
| 627 |
kwargs['tags'] = 'tag1,tag2' |
| 628 |
arglist = ['--tags', 'tag1,tag2'] |
| 629 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 630 |
|
| 631 |
columns, data = self.cmd.take_action(parsed_args) |
| 632 |
|
| 633 |
self.stack_client.list.assert_called_with(**kwargs) |
| 634 |
self.assertEqual(self.columns, columns) |
| 635 |
|
| 636 |
def test_stack_list_tags_mode(self): |
| 637 |
kwargs = copy.deepcopy(self.defaults) |
| 638 |
kwargs['not_tags'] = 'tag1,tag2' |
| 639 |
arglist = ['--tags', 'tag1,tag2', '--tag-mode', 'not'] |
| 640 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 641 |
|
| 642 |
columns, data = self.cmd.take_action(parsed_args) |
| 643 |
|
| 644 |
self.stack_client.list.assert_called_with(**kwargs) |
| 645 |
self.assertEqual(self.columns, columns) |
| 646 |
|
| 647 |
def test_stack_list_tags_bad_mode(self): |
| 648 |
arglist = ['--tags', 'tag1,tag2', '--tag-mode', 'bad_mode'] |
| 649 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 650 |
|
| 651 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 652 |
|
| 653 |
|
| 654 |
class TestStackDelete(TestStack): |
| 655 |
|
| 656 |
def setUp(self): |
| 657 |
super().setUp() |
| 658 |
self.cmd = stack.DeleteStack(self.app, None) |
| 659 |
self.stack_client.get.side_effect = heat_exc.HTTPNotFound |
| 660 |
|
| 661 |
def test_stack_delete(self): |
| 662 |
arglist = ['stack1', 'stack2', 'stack3'] |
| 663 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 664 |
|
| 665 |
self.cmd.take_action(parsed_args) |
| 666 |
|
| 667 |
self.stack_client.delete.assert_any_call('stack1') |
| 668 |
self.stack_client.delete.assert_any_call('stack2') |
| 669 |
self.stack_client.delete.assert_any_call('stack3') |
| 670 |
|
| 671 |
def test_stack_delete_not_found(self): |
| 672 |
arglist = ['my_stack'] |
| 673 |
self.stack_client.delete.side_effect = heat_exc.HTTPNotFound |
| 674 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 675 |
|
| 676 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 677 |
|
| 678 |
def test_stack_delete_forbidden(self): |
| 679 |
arglist = ['my_stack'] |
| 680 |
self.stack_client.delete.side_effect = heat_exc.Forbidden |
| 681 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 682 |
|
| 683 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 684 |
|
| 685 |
def test_stack_delete_one_found_one_not_found(self): |
| 686 |
arglist = ['stack1', 'stack2'] |
| 687 |
self.stack_client.delete.side_effect = [None, heat_exc.HTTPNotFound] |
| 688 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 689 |
|
| 690 |
error = self.assertRaises(exc.CommandError, |
| 691 |
self.cmd.take_action, parsed_args) |
| 692 |
|
| 693 |
self.stack_client.delete.assert_any_call('stack1') |
| 694 |
self.stack_client.delete.assert_any_call('stack2') |
| 695 |
self.assertEqual('Unable to delete 1 of the 2 stacks.', str(error)) |
| 696 |
|
| 697 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 698 |
return_value=('DELETE_COMPLETE', |
| 699 |
'Stack my_stack DELETE_COMPLETE')) |
| 700 |
@mock.patch('heatclient.common.event_utils.get_events', return_value=[]) |
| 701 |
def test_stack_delete_wait(self, mock_get_event, mock_poll, ): |
| 702 |
arglist = ['stack1', 'stack2', 'stack3', '--wait'] |
| 703 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 704 |
self.cmd.take_action(parsed_args) |
| 705 |
self.stack_client.delete.assert_any_call('stack1') |
| 706 |
self.stack_client.delete.assert_any_call('stack2') |
| 707 |
self.stack_client.delete.assert_any_call('stack3') |
| 708 |
|
| 709 |
@mock.patch('heatclient.common.event_utils.poll_for_events') |
| 710 |
@mock.patch('heatclient.common.event_utils.get_events', return_value=[]) |
| 711 |
def test_stack_delete_wait_fail(self, mock_get_event, mock_poll): |
| 712 |
mock_poll.side_effect = [['DELETE_COMPLETE', |
| 713 |
'Stack my_stack DELETE_COMPLETE'], |
| 714 |
['DELETE_FAILED', |
| 715 |
'Stack my_stack DELETE_FAILED'], |
| 716 |
['DELETE_COMPLETE', |
| 717 |
'Stack my_stack DELETE_COMPLETE']] |
| 718 |
arglist = ['stack1', 'stack2', 'stack3', '--wait'] |
| 719 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 720 |
error = self.assertRaises(exc.CommandError, |
| 721 |
self.cmd.take_action, parsed_args) |
| 722 |
self.stack_client.delete.assert_any_call('stack1') |
| 723 |
self.stack_client.delete.assert_any_call('stack2') |
| 724 |
self.stack_client.delete.assert_any_call('stack3') |
| 725 |
self.assertEqual('Unable to delete 1 of the 3 stacks.', str(error)) |
| 726 |
|
| 727 |
@mock.patch('sys.stdin', spec=io.StringIO) |
| 728 |
def test_stack_delete_prompt(self, mock_stdin): |
| 729 |
arglist = ['my_stack'] |
| 730 |
mock_stdin.isatty.return_value = True |
| 731 |
mock_stdin.readline.return_value = 'y' |
| 732 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 733 |
|
| 734 |
self.cmd.take_action(parsed_args) |
| 735 |
|
| 736 |
mock_stdin.readline.assert_called_with() |
| 737 |
self.stack_client.delete.assert_called_with('my_stack') |
| 738 |
|
| 739 |
@mock.patch('sys.stdin', spec=io.StringIO) |
| 740 |
def test_stack_delete_prompt_no(self, mock_stdin): |
| 741 |
arglist = ['my_stack'] |
| 742 |
mock_stdin.isatty.return_value = True |
| 743 |
mock_stdin.readline.return_value = 'n' |
| 744 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 745 |
|
| 746 |
self.cmd.take_action(parsed_args) |
| 747 |
|
| 748 |
mock_stdin.readline.assert_called_with() |
| 749 |
self.stack_client.delete.assert_not_called() |
| 750 |
|
| 751 |
|
| 752 |
class TestStackAdopt(TestStack): |
| 753 |
|
| 754 |
adopt_file = 'heatclient/tests/test_templates/adopt.json' |
| 755 |
adopt_with_files = ('heatclient/tests/test_templates/adopt_with_file.json') |
| 756 |
|
| 757 |
with open(adopt_file) as f: |
| 758 |
adopt_data = f.read() |
| 759 |
with open(adopt_with_files) as f: |
| 760 |
adopt_with_files_data = f.read() |
| 761 |
|
| 762 |
defaults = { |
| 763 |
'stack_name': 'my_stack', |
| 764 |
'disable_rollback': True, |
| 765 |
'adopt_stack_data': adopt_data, |
| 766 |
'parameters': {}, |
| 767 |
'files': {}, |
| 768 |
'environment': {}, |
| 769 |
'timeout': None |
| 770 |
} |
| 771 |
|
| 772 |
child_stack_yaml = "{\"heat_template_version\": \"2015-10-15\"}" |
| 773 |
|
| 774 |
expected_with_files = { |
| 775 |
'stack_name': 'my_stack', |
| 776 |
'disable_rollback': True, |
| 777 |
'adopt_stack_data': adopt_with_files_data, |
| 778 |
'parameters': {}, |
| 779 |
'files': {'file://empty.yaml': child_stack_yaml}, |
| 780 |
'environment': {}, |
| 781 |
'timeout': None |
| 782 |
} |
| 783 |
|
| 784 |
def setUp(self): |
| 785 |
super().setUp() |
| 786 |
self.cmd = stack.AdoptStack(self.app, None) |
| 787 |
self.stack_client.create.return_value = {'stack': {'id': '1234'}} |
| 788 |
|
| 789 |
def test_stack_adopt_defaults(self): |
| 790 |
arglist = ['my_stack', '--adopt-file', self.adopt_file] |
| 791 |
cols = ['id', 'stack_name', 'description', 'creation_time', |
| 792 |
'updated_time', 'stack_status', 'stack_status_reason'] |
| 793 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 794 |
|
| 795 |
columns, data = self.cmd.take_action(parsed_args) |
| 796 |
|
| 797 |
self.stack_client.create.assert_called_with(**self.defaults) |
| 798 |
self.assertEqual(cols, columns) |
| 799 |
|
| 800 |
def test_stack_adopt_enable_rollback(self): |
| 801 |
arglist = ['my_stack', '--adopt-file', self.adopt_file, |
| 802 |
'--enable-rollback'] |
| 803 |
kwargs = copy.deepcopy(self.defaults) |
| 804 |
kwargs['disable_rollback'] = False |
| 805 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 806 |
|
| 807 |
self.cmd.take_action(parsed_args) |
| 808 |
|
| 809 |
self.stack_client.create.assert_called_with(**kwargs) |
| 810 |
|
| 811 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 812 |
return_value=('ADOPT_COMPLETE', |
| 813 |
'Stack my_stack ADOPT_COMPLETE')) |
| 814 |
def test_stack_adopt_wait(self, mock_poll): |
| 815 |
arglist = ['my_stack', '--adopt-file', self.adopt_file, '--wait'] |
| 816 |
self.stack_client.get.return_value = stacks.Stack( |
| 817 |
None, {'stack_status': 'ADOPT_COMPLETE'}) |
| 818 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 819 |
|
| 820 |
self.cmd.take_action(parsed_args) |
| 821 |
|
| 822 |
self.stack_client.create.assert_called_with(**self.defaults) |
| 823 |
self.stack_client.get.assert_called_with(**{'stack_id': '1234', |
| 824 |
'resolve_outputs': False}) |
| 825 |
|
| 826 |
@mock.patch('heatclient.common.event_utils.poll_for_events', |
| 827 |
return_value=('ADOPT_FAILED', |
| 828 |
'Stack my_stack ADOPT_FAILED')) |
| 829 |
def test_stack_adopt_wait_fail(self, mock_poll): |
| 830 |
arglist = ['my_stack', '--adopt-file', self.adopt_file, '--wait'] |
| 831 |
self.stack_client.get.return_value = stacks.Stack( |
| 832 |
None, {'stack_status': 'ADOPT_FAILED'}) |
| 833 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 834 |
|
| 835 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 836 |
|
| 837 |
def test_stack_adopt_with_adopt_files(self): |
| 838 |
|
| 839 |
|
| 840 |
arglist = ['my_stack', '--adopt-file', self.adopt_with_files] |
| 841 |
cols = ['id', 'stack_name', 'description', 'creation_time', |
| 842 |
'updated_time', 'stack_status', 'stack_status_reason'] |
| 843 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 844 |
|
| 845 |
columns, data = self.cmd.take_action(parsed_args) |
| 846 |
|
| 847 |
self.stack_client.create.assert_called_with(**self.expected_with_files) |
| 848 |
self.assertEqual(cols, columns) |
| 849 |
|
| 850 |
|
| 851 |
class TestStackExport(TestStack): |
| 852 |
|
| 853 |
columns = ['stack_name', 'stack_status', 'id'] |
| 854 |
data = ['my_stack', 'ABANDONED', '1234'] |
| 855 |
|
| 856 |
response = dict(zip(columns, data)) |
| 857 |
|
| 858 |
def setUp(self): |
| 859 |
super().setUp() |
| 860 |
self.cmd = stack.ExportStack(self.app, None) |
| 861 |
self.stack_client.export.return_value = self.response |
| 862 |
|
| 863 |
def test_stack_export(self): |
| 864 |
arglist = ['my_stack'] |
| 865 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 866 |
|
| 867 |
columns, data = self.cmd.take_action(parsed_args) |
| 868 |
|
| 869 |
for column in self.columns: |
| 870 |
self.assertIn(column, columns) |
| 871 |
for datum in self.data: |
| 872 |
self.assertIn(datum, data) |
| 873 |
|
| 874 |
@mock.patch('heatclient.osc.v1.stack.open', create=True) |
| 875 |
def test_stack_export_output_file(self, mock_open): |
| 876 |
arglist = ['my_stack', '--output-file', 'file.json'] |
| 877 |
mock_open.return_value = mock.MagicMock(spec=io.IOBase) |
| 878 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 879 |
|
| 880 |
columns, data = self.cmd.take_action(parsed_args) |
| 881 |
|
| 882 |
mock_open.assert_called_once_with('file.json', 'w') |
| 883 |
self.assertEqual([], columns) |
| 884 |
self.assertIsNone(data) |
| 885 |
|
| 886 |
|
| 887 |
class TestStackAbandon(TestStack): |
| 888 |
|
| 889 |
columns = ['stack_name', 'stack_status', 'id'] |
| 890 |
data = ['my_stack', 'ABANDONED', '1234'] |
| 891 |
|
| 892 |
response = dict(zip(columns, data)) |
| 893 |
|
| 894 |
def setUp(self): |
| 895 |
super().setUp() |
| 896 |
self.cmd = stack.AbandonStack(self.app, None) |
| 897 |
self.stack_client.abandon.return_value = self.response |
| 898 |
|
| 899 |
def test_stack_abandon(self): |
| 900 |
arglist = ['my_stack'] |
| 901 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 902 |
|
| 903 |
columns, data = self.cmd.take_action(parsed_args) |
| 904 |
|
| 905 |
for column in self.columns: |
| 906 |
self.assertIn(column, columns) |
| 907 |
for datum in self.data: |
| 908 |
self.assertIn(datum, data) |
| 909 |
|
| 910 |
def test_stack_abandon_not_found(self): |
| 911 |
arglist = ['my_stack'] |
| 912 |
self.stack_client.abandon.side_effect = heat_exc.HTTPNotFound |
| 913 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 914 |
|
| 915 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 916 |
|
| 917 |
@mock.patch('heatclient.osc.v1.stack.open', create=True) |
| 918 |
def test_stack_abandon_output_file(self, mock_open): |
| 919 |
arglist = ['my_stack', '--output-file', 'file.json'] |
| 920 |
mock_open.return_value = mock.MagicMock(spec=io.IOBase) |
| 921 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 922 |
|
| 923 |
columns, data = self.cmd.take_action(parsed_args) |
| 924 |
|
| 925 |
mock_open.assert_called_once_with('file.json', 'w') |
| 926 |
self.assertEqual([], columns) |
| 927 |
self.assertIsNone(data) |
| 928 |
|
| 929 |
@mock.patch('heatclient.osc.v1.stack.open', create=True, |
| 930 |
side_effect=IOError) |
| 931 |
def test_stack_abandon_output_file_error(self, mock_open): |
| 932 |
arglist = ['my_stack', '--output-file', 'file.json'] |
| 933 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 934 |
|
| 935 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 936 |
|
| 937 |
|
| 938 |
class TestStackOutputShow(TestStack): |
| 939 |
|
| 940 |
outputs = [ |
| 941 |
{'output_key': 'output1', 'output_value': 'value1'}, |
| 942 |
{'output_key': 'output2', 'output_value': 'value2', |
| 943 |
'output_error': 'error'} |
| 944 |
] |
| 945 |
|
| 946 |
response = { |
| 947 |
'outputs': outputs, |
| 948 |
'stack_name': 'my_stack' |
| 949 |
} |
| 950 |
|
| 951 |
def setUp(self): |
| 952 |
super().setUp() |
| 953 |
self.cmd = stack.OutputShowStack(self.app, None) |
| 954 |
self.stack_client.get.return_value = stacks.Stack(None, self.response) |
| 955 |
|
| 956 |
def test_stack_output_show_no_output(self): |
| 957 |
arglist = ['my_stack'] |
| 958 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 959 |
|
| 960 |
error = self.assertRaises(exc.CommandError, |
| 961 |
self.cmd.take_action, parsed_args) |
| 962 |
self.assertEqual('Either <OUTPUT NAME> or --all must be specified.', |
| 963 |
str(error)) |
| 964 |
|
| 965 |
def test_stack_output_show_output_and_all(self): |
| 966 |
arglist = ['my_stack', 'output1', '--all'] |
| 967 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 968 |
|
| 969 |
error = self.assertRaises(exc.CommandError, |
| 970 |
self.cmd.take_action, parsed_args) |
| 971 |
self.assertEqual('Cannot specify both <OUTPUT NAME> and --all.', |
| 972 |
str(error)) |
| 973 |
|
| 974 |
def test_stack_output_show_all(self): |
| 975 |
arglist = ['my_stack', '--all'] |
| 976 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 977 |
|
| 978 |
columns, outputs = self.cmd.take_action(parsed_args) |
| 979 |
|
| 980 |
self.stack_client.get.assert_called_with('my_stack') |
| 981 |
self.assertEqual(['output1', 'output2'], columns) |
| 982 |
|
| 983 |
def test_stack_output_show_output(self): |
| 984 |
arglist = ['my_stack', 'output1'] |
| 985 |
self.stack_client.output_show.return_value = { |
| 986 |
'output': self.outputs[0]} |
| 987 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 988 |
|
| 989 |
columns, outputs = self.cmd.take_action(parsed_args) |
| 990 |
|
| 991 |
self.stack_client.output_show.assert_called_with('my_stack', 'output1') |
| 992 |
self.assertEqual(('output_key', 'output_value'), columns) |
| 993 |
self.assertEqual(('output1', 'value1'), outputs) |
| 994 |
|
| 995 |
def test_stack_output_show_not_found(self): |
| 996 |
arglist = ['my_stack', '--all'] |
| 997 |
self.stack_client.get.side_effect = heat_exc.HTTPNotFound |
| 998 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 999 |
|
| 1000 |
error = self.assertRaises(exc.CommandError, |
| 1001 |
self.cmd.take_action, parsed_args) |
| 1002 |
self.assertEqual('Stack not found: my_stack', str(error)) |
| 1003 |
|
| 1004 |
def test_stack_output_show_output_error(self): |
| 1005 |
arglist = ['my_stack', 'output2'] |
| 1006 |
self.stack_client.output_show.return_value = { |
| 1007 |
'output': self.outputs[1]} |
| 1008 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1009 |
|
| 1010 |
error = self.assertRaises(exc.CommandError, |
| 1011 |
self.cmd.take_action, parsed_args) |
| 1012 |
self.assertEqual('Output error: error', str(error)) |
| 1013 |
self.stack_client.output_show.assert_called_with('my_stack', 'output2') |
| 1014 |
|
| 1015 |
def test_stack_output_show_bad_output(self): |
| 1016 |
arglist = ['my_stack', 'output3'] |
| 1017 |
self.stack_client.output_show.side_effect = heat_exc.HTTPNotFound |
| 1018 |
self.stack_client.get.side_effect = heat_exc.HTTPNotFound |
| 1019 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1020 |
|
| 1021 |
error = self.assertRaises(exc.CommandError, |
| 1022 |
self.cmd.take_action, parsed_args) |
| 1023 |
self.assertEqual('Stack my_stack or output output3 not found.', |
| 1024 |
str(error)) |
| 1025 |
self.stack_client.output_show.assert_called_with('my_stack', 'output3') |
| 1026 |
|
| 1027 |
def test_stack_output_show_old_api(self): |
| 1028 |
arglist = ['my_stack', 'output1'] |
| 1029 |
self.stack_client.output_show.side_effect = heat_exc.HTTPNotFound |
| 1030 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1031 |
|
| 1032 |
columns, outputs = self.cmd.take_action(parsed_args) |
| 1033 |
|
| 1034 |
self.stack_client.get.assert_called_with('my_stack') |
| 1035 |
self.assertEqual(('output_key', 'output_value'), columns) |
| 1036 |
self.assertEqual(('output1', 'value1'), outputs) |
| 1037 |
|
| 1038 |
|
| 1039 |
class TestStackOutputList(TestStack): |
| 1040 |
|
| 1041 |
response = {'outputs': [{'output_key': 'key1', 'description': 'desc1'}, |
| 1042 |
{'output_key': 'key2', 'description': 'desc2'}]} |
| 1043 |
stack_response = { |
| 1044 |
'stack_name': 'my_stack', |
| 1045 |
'outputs': response['outputs'] |
| 1046 |
} |
| 1047 |
|
| 1048 |
def setUp(self): |
| 1049 |
super().setUp() |
| 1050 |
self.cmd = stack.OutputListStack(self.app, None) |
| 1051 |
self.stack_client.get = mock.MagicMock( |
| 1052 |
return_value=stacks.Stack(None, self.response)) |
| 1053 |
|
| 1054 |
def test_stack_output_list(self): |
| 1055 |
arglist = ['my_stack'] |
| 1056 |
self.stack_client.output_list.return_value = self.response |
| 1057 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1058 |
|
| 1059 |
columns, data = self.cmd.take_action(parsed_args) |
| 1060 |
|
| 1061 |
self.assertEqual(['output_key', 'description'], columns) |
| 1062 |
self.stack_client.output_list.assert_called_with('my_stack') |
| 1063 |
|
| 1064 |
def test_stack_output_list_not_found(self): |
| 1065 |
arglist = ['my_stack'] |
| 1066 |
self.stack_client.output_list.side_effect = heat_exc.HTTPNotFound |
| 1067 |
self.stack_client.get.side_effect = heat_exc.HTTPNotFound |
| 1068 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1069 |
|
| 1070 |
error = self.assertRaises(exc.CommandError, |
| 1071 |
self.cmd.take_action, parsed_args) |
| 1072 |
self.assertEqual('Stack not found: my_stack', str(error)) |
| 1073 |
|
| 1074 |
def test_stack_output_list_old_api(self): |
| 1075 |
arglist = ['my_stack'] |
| 1076 |
self.stack_client.output_list.side_effect = heat_exc.HTTPNotFound |
| 1077 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1078 |
|
| 1079 |
columns, outputs = self.cmd.take_action(parsed_args) |
| 1080 |
|
| 1081 |
self.stack_client.get.assert_called_with('my_stack') |
| 1082 |
self.assertEqual(['output_key', 'description'], columns) |
| 1083 |
|
| 1084 |
|
| 1085 |
class TestStackTemplateShow(TestStack): |
| 1086 |
|
| 1087 |
fields = ['heat_template_version', 'description', 'parameter_groups', |
| 1088 |
'parameters', 'resources', 'outputs'] |
| 1089 |
|
| 1090 |
def setUp(self): |
| 1091 |
super().setUp() |
| 1092 |
self.cmd = stack.TemplateShowStack(self.app, None) |
| 1093 |
|
| 1094 |
def test_stack_template_show_full_template(self): |
| 1095 |
arglist = ['my_stack'] |
| 1096 |
self.stack_client.template.return_value = yaml.load( |
| 1097 |
inline_templates.FULL_TEMPLATE, |
| 1098 |
Loader=template_format.yaml_loader) |
| 1099 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1100 |
|
| 1101 |
columns, outputs = self.cmd.take_action(parsed_args) |
| 1102 |
|
| 1103 |
for f in self.fields: |
| 1104 |
self.assertIn(f, columns) |
| 1105 |
|
| 1106 |
def test_stack_template_show_short_template(self): |
| 1107 |
arglist = ['my_stack'] |
| 1108 |
self.stack_client.template.return_value = yaml.load( |
| 1109 |
inline_templates.SHORT_TEMPLATE, |
| 1110 |
Loader=template_format.yaml_loader) |
| 1111 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1112 |
|
| 1113 |
columns, outputs = self.cmd.take_action(parsed_args) |
| 1114 |
|
| 1115 |
for f in ['heat_template_version', 'resources']: |
| 1116 |
self.assertIn(f, columns) |
| 1117 |
|
| 1118 |
def test_stack_template_show_not_found(self): |
| 1119 |
arglist = ['my_stack'] |
| 1120 |
self.stack_client.template.side_effect = heat_exc.HTTPNotFound |
| 1121 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1122 |
|
| 1123 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 1124 |
|
| 1125 |
|
| 1126 |
class _TestStackCheckBase: |
| 1127 |
|
| 1128 |
stack = stacks.Stack(None, { |
| 1129 |
"id": '1234', |
| 1130 |
"stack_name": 'my_stack', |
| 1131 |
"creation_time": "2013-08-04T20:57:55Z", |
| 1132 |
"updated_time": "2013-08-04T20:57:55Z", |
| 1133 |
"stack_status": "CREATE_COMPLETE" |
| 1134 |
}) |
| 1135 |
|
| 1136 |
columns = ['ID', 'Stack Name', 'Stack Status', 'Creation Time', |
| 1137 |
'Updated Time'] |
| 1138 |
|
| 1139 |
def _setUp(self, cmd, action, action_name=None): |
| 1140 |
self.cmd = cmd |
| 1141 |
self.action = action |
| 1142 |
self.action_name = action_name |
| 1143 |
self.mock_client.stacks.get.return_value = self.stack |
| 1144 |
|
| 1145 |
def _test_stack_action(self, get_call_count=1): |
| 1146 |
arglist = ['my_stack'] |
| 1147 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1148 |
columns, rows = self.cmd.take_action(parsed_args) |
| 1149 |
self.action.assert_called_once_with('my_stack') |
| 1150 |
self.mock_client.stacks.get.assert_called_with('my_stack') |
| 1151 |
self.assertEqual(get_call_count, |
| 1152 |
self.mock_client.stacks.get.call_count) |
| 1153 |
self.assertEqual(self.columns, columns) |
| 1154 |
self.assertEqual(1, len(rows)) |
| 1155 |
|
| 1156 |
def _test_stack_action_multi(self, get_call_count=2): |
| 1157 |
arglist = ['my_stack1', 'my_stack2'] |
| 1158 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1159 |
columns, rows = self.cmd.take_action(parsed_args) |
| 1160 |
self.assertEqual(2, self.action.call_count) |
| 1161 |
self.assertEqual(get_call_count, |
| 1162 |
self.mock_client.stacks.get.call_count) |
| 1163 |
self.action.assert_called_with('my_stack2') |
| 1164 |
self.mock_client.stacks.get.assert_called_with('my_stack2') |
| 1165 |
self.assertEqual(self.columns, columns) |
| 1166 |
self.assertEqual(2, len(rows)) |
| 1167 |
|
| 1168 |
@mock.patch('heatclient.common.event_utils.poll_for_events') |
| 1169 |
@mock.patch('heatclient.common.event_utils.get_events', return_value=[]) |
| 1170 |
def _test_stack_action_wait(self, ge, mock_poll): |
| 1171 |
arglist = ['my_stack', '--wait'] |
| 1172 |
mock_poll.return_value = ( |
| 1173 |
'%s_COMPLETE' % self.action_name, |
| 1174 |
'Stack my_stack %s_COMPLETE' % self.action_name |
| 1175 |
) |
| 1176 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1177 |
columns, rows = self.cmd.take_action(parsed_args) |
| 1178 |
self.action.assert_called_with('my_stack') |
| 1179 |
self.mock_client.stacks.get.assert_called_with('my_stack') |
| 1180 |
self.assertEqual(self.columns, columns) |
| 1181 |
self.assertEqual(1, len(rows)) |
| 1182 |
|
| 1183 |
@mock.patch('heatclient.common.event_utils.poll_for_events') |
| 1184 |
@mock.patch('heatclient.common.event_utils.get_events', return_value=[]) |
| 1185 |
def _test_stack_action_wait_error(self, ge, mock_poll): |
| 1186 |
arglist = ['my_stack', '--wait'] |
| 1187 |
mock_poll.return_value = ( |
| 1188 |
'%s_FAILED' % self.action_name, |
| 1189 |
'Error waiting for status from stack my_stack' |
| 1190 |
) |
| 1191 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1192 |
error = self.assertRaises(exc.CommandError, |
| 1193 |
self.cmd.take_action, |
| 1194 |
parsed_args) |
| 1195 |
self.assertEqual('Error waiting for status from stack my_stack', |
| 1196 |
str(error)) |
| 1197 |
|
| 1198 |
def _test_stack_action_exception(self): |
| 1199 |
self.action.side_effect = heat_exc.HTTPNotFound |
| 1200 |
arglist = ['my_stack'] |
| 1201 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1202 |
error = self.assertRaises(exc.CommandError, |
| 1203 |
self.cmd.take_action, |
| 1204 |
parsed_args) |
| 1205 |
self.assertEqual('Stack not found: my_stack', |
| 1206 |
str(error)) |
| 1207 |
|
| 1208 |
|
| 1209 |
class TestStackSuspend(_TestStackCheckBase, TestStack): |
| 1210 |
|
| 1211 |
def setUp(self): |
| 1212 |
super().setUp() |
| 1213 |
self._setUp( |
| 1214 |
stack.SuspendStack(self.app, None), |
| 1215 |
self.mock_client.actions.suspend, |
| 1216 |
'SUSPEND' |
| 1217 |
) |
| 1218 |
|
| 1219 |
def test_stack_suspend(self): |
| 1220 |
self._test_stack_action() |
| 1221 |
|
| 1222 |
def test_stack_suspend_multi(self): |
| 1223 |
self._test_stack_action_multi() |
| 1224 |
|
| 1225 |
def test_stack_suspend_wait(self): |
| 1226 |
self._test_stack_action_wait() |
| 1227 |
|
| 1228 |
def test_stack_suspend_wait_error(self): |
| 1229 |
self._test_stack_action_wait_error() |
| 1230 |
|
| 1231 |
def test_stack_suspend_exception(self): |
| 1232 |
self._test_stack_action_exception() |
| 1233 |
|
| 1234 |
|
| 1235 |
class TestStackResume(_TestStackCheckBase, TestStack): |
| 1236 |
|
| 1237 |
def setUp(self): |
| 1238 |
super().setUp() |
| 1239 |
self._setUp( |
| 1240 |
stack.ResumeStack(self.app, None), |
| 1241 |
self.mock_client.actions.resume, |
| 1242 |
'RESUME' |
| 1243 |
) |
| 1244 |
|
| 1245 |
def test_stack_resume(self): |
| 1246 |
self._test_stack_action() |
| 1247 |
|
| 1248 |
def test_stack_resume_multi(self): |
| 1249 |
self._test_stack_action_multi() |
| 1250 |
|
| 1251 |
def test_stack_resume_wait(self): |
| 1252 |
self._test_stack_action_wait() |
| 1253 |
|
| 1254 |
def test_stack_resume_wait_error(self): |
| 1255 |
self._test_stack_action_wait_error() |
| 1256 |
|
| 1257 |
def test_stack_resume_exception(self): |
| 1258 |
self._test_stack_action_exception() |
| 1259 |
|
| 1260 |
|
| 1261 |
class TestStackCancel(_TestStackCheckBase, TestStack): |
| 1262 |
|
| 1263 |
stack_update_in_progress = stacks.Stack(None, { |
| 1264 |
"id": '1234', |
| 1265 |
"stack_name": 'my_stack', |
| 1266 |
"creation_time": "2013-08-04T20:57:55Z", |
| 1267 |
"updated_time": "2013-08-04T20:57:55Z", |
| 1268 |
"stack_status": "UPDATE_IN_PROGRESS" |
| 1269 |
}) |
| 1270 |
|
| 1271 |
def setUp(self): |
| 1272 |
super().setUp() |
| 1273 |
self._setUp( |
| 1274 |
stack.CancelStack(self.app, None), |
| 1275 |
self.mock_client.actions.cancel_update, |
| 1276 |
'ROLLBACK' |
| 1277 |
) |
| 1278 |
self.mock_client.stacks.get.return_value = ( |
| 1279 |
self.stack_update_in_progress) |
| 1280 |
|
| 1281 |
def test_stack_cancel(self): |
| 1282 |
self._test_stack_action(2) |
| 1283 |
|
| 1284 |
def _test_stack_cancel_no_rollback(self, call_count): |
| 1285 |
self.action = self.mock_client.actions.cancel_without_rollback |
| 1286 |
arglist = ['my_stack', '--no-rollback'] |
| 1287 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1288 |
columns, rows = self.cmd.take_action(parsed_args) |
| 1289 |
self.action.assert_called_once_with('my_stack') |
| 1290 |
self.mock_client.stacks.get.assert_called_with('my_stack') |
| 1291 |
self.assertEqual(call_count, |
| 1292 |
self.mock_client.stacks.get.call_count) |
| 1293 |
self.assertEqual(self.columns, columns) |
| 1294 |
self.assertEqual(1, len(rows)) |
| 1295 |
|
| 1296 |
def test_stack_cancel_no_rollback(self): |
| 1297 |
self._test_stack_cancel_no_rollback(2) |
| 1298 |
|
| 1299 |
def test_stack_cancel_multi(self): |
| 1300 |
self._test_stack_action_multi(4) |
| 1301 |
|
| 1302 |
def test_stack_cancel_wait(self): |
| 1303 |
self._test_stack_action_wait() |
| 1304 |
|
| 1305 |
def test_stack_cancel_wait_error(self): |
| 1306 |
self._test_stack_action_wait_error() |
| 1307 |
|
| 1308 |
def test_stack_cancel_exception(self): |
| 1309 |
self._test_stack_action_exception() |
| 1310 |
|
| 1311 |
def test_stack_cancel_unsupported_state(self): |
| 1312 |
self.stack.stack_status = "CREATE_COMPLETE" |
| 1313 |
self.mock_client.stacks.get.return_value = self.stack |
| 1314 |
error = self.assertRaises(exc.CommandError, |
| 1315 |
self._test_stack_action, |
| 1316 |
2) |
| 1317 |
self.assertEqual('Stack my_stack with status \'create_complete\' ' |
| 1318 |
'not in cancelable state', |
| 1319 |
str(error)) |
| 1320 |
|
| 1321 |
def test_stack_cancel_create_in_progress(self): |
| 1322 |
self.stack.stack_status = "CREATE_IN_PROGRESS" |
| 1323 |
self.mock_client.stacks.get.return_value = self.stack |
| 1324 |
error = self.assertRaises(exc.CommandError, |
| 1325 |
self._test_stack_action, |
| 1326 |
2) |
| 1327 |
self.assertEqual('Stack my_stack with status \'create_in_progress\' ' |
| 1328 |
'not in cancelable state', |
| 1329 |
str(error)) |
| 1330 |
self._test_stack_cancel_no_rollback(3) |
| 1331 |
|
| 1332 |
|
| 1333 |
class TestStackCheck(_TestStackCheckBase, TestStack): |
| 1334 |
|
| 1335 |
def setUp(self): |
| 1336 |
super().setUp() |
| 1337 |
self._setUp( |
| 1338 |
stack.CheckStack(self.app, None), |
| 1339 |
self.mock_client.actions.check, |
| 1340 |
'CHECK' |
| 1341 |
) |
| 1342 |
|
| 1343 |
def test_stack_check(self): |
| 1344 |
self._test_stack_action() |
| 1345 |
|
| 1346 |
def test_stack_check_multi(self): |
| 1347 |
self._test_stack_action_multi() |
| 1348 |
|
| 1349 |
def test_stack_check_wait(self): |
| 1350 |
self._test_stack_action_wait() |
| 1351 |
|
| 1352 |
def test_stack_check_wait_error(self): |
| 1353 |
self._test_stack_action_wait_error() |
| 1354 |
|
| 1355 |
def test_stack_check_exception(self): |
| 1356 |
self._test_stack_action_exception() |
| 1357 |
|
| 1358 |
|
| 1359 |
class TestStackHookPoll(TestStack): |
| 1360 |
|
| 1361 |
stack = stacks.Stack(None, { |
| 1362 |
"id": '1234', |
| 1363 |
"stack_name": 'my_stack', |
| 1364 |
"creation_time": "2013-08-04T20:57:55Z", |
| 1365 |
"updated_time": "2013-08-04T20:57:55Z", |
| 1366 |
"stack_status": "CREATE_IN_PROGRESS" |
| 1367 |
}) |
| 1368 |
resource = resources.Resource(None, { |
| 1369 |
'resource_name': 'resource1', |
| 1370 |
'links': [{'href': 'http://heat.example.com:8004/resource1', |
| 1371 |
'rel': 'self'}, |
| 1372 |
{'href': 'http://192.168.27.100:8004/my_stack', |
| 1373 |
'rel': 'stack'}], |
| 1374 |
'logical_resource_id': 'random_group', |
| 1375 |
'creation_time': '2015-12-03T16:50:56', |
| 1376 |
'resource_status': 'INIT_COMPLETE', |
| 1377 |
'updated_time': '2015-12-03T16:50:56', |
| 1378 |
'required_by': [], |
| 1379 |
'resource_status_reason': '', |
| 1380 |
'physical_resource_id': '', |
| 1381 |
'resource_type': 'OS::Heat::ResourceGroup', |
| 1382 |
'id': '1111' |
| 1383 |
}) |
| 1384 |
columns = ['ID', 'Resource Status Reason', 'Resource Status', |
| 1385 |
'Event Time'] |
| 1386 |
event0 = events.Event(manager=None, info={ |
| 1387 |
'resource_name': 'my_stack', |
| 1388 |
'event_time': '2015-12-02T16:50:56', |
| 1389 |
'logical_resource_id': 'my_stack', |
| 1390 |
'resource_status': 'CREATE_IN_PROGRESS', |
| 1391 |
'resource_status_reason': 'Stack CREATE started', |
| 1392 |
'id': '1234' |
| 1393 |
}) |
| 1394 |
event1 = events.Event(manager=None, info={ |
| 1395 |
'resource_name': 'resource1', |
| 1396 |
'event_time': '2015-12-03T19:59:58', |
| 1397 |
'logical_resource_id': 'resource1', |
| 1398 |
'resource_status': 'INIT_COMPLETE', |
| 1399 |
'resource_status_reason': |
| 1400 |
'CREATE paused until Hook pre-create is cleared', |
| 1401 |
'id': '1111' |
| 1402 |
}) |
| 1403 |
row1 = ('resource1', |
| 1404 |
'1111', |
| 1405 |
'CREATE paused until Hook pre-create is cleared', |
| 1406 |
'INIT_COMPLETE', |
| 1407 |
'2015-12-03T19:59:58' |
| 1408 |
) |
| 1409 |
|
| 1410 |
def setUp(self): |
| 1411 |
super().setUp() |
| 1412 |
self.cmd = stack.StackHookPoll(self.app, None) |
| 1413 |
self.mock_client.stacks.get.return_value = self.stack |
| 1414 |
self.mock_client.events.list.return_value = [self.event0, self.event1] |
| 1415 |
self.mock_client.resources.list.return_value = [self.resource] |
| 1416 |
|
| 1417 |
def test_hook_poll(self): |
| 1418 |
expected_columns = ['Resource Name'] + self.columns |
| 1419 |
expected_rows = [self.row1] |
| 1420 |
arglist = ['my_stack'] |
| 1421 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1422 |
columns, rows = self.cmd.take_action(parsed_args) |
| 1423 |
self.assertEqual(expected_rows, list(rows)) |
| 1424 |
self.assertEqual(expected_columns, columns) |
| 1425 |
|
| 1426 |
def test_hook_poll_nested(self): |
| 1427 |
expected_columns = ['Resource Name'] + self.columns + ['Stack Name'] |
| 1428 |
expected_rows = [self.row1 + ('my_stack',)] |
| 1429 |
arglist = ['my_stack', '--nested-depth=10'] |
| 1430 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1431 |
columns, rows = self.cmd.take_action(parsed_args) |
| 1432 |
self.assertEqual(expected_rows, list(rows)) |
| 1433 |
self.assertEqual(expected_columns, columns) |
| 1434 |
|
| 1435 |
def test_hook_poll_nested_invalid(self): |
| 1436 |
arglist = ['my_stack', '--nested-depth=ugly'] |
| 1437 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1438 |
self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) |
| 1439 |
|
| 1440 |
|
| 1441 |
class TestStackHookClear(TestStack): |
| 1442 |
|
| 1443 |
stack = stacks.Stack(None, { |
| 1444 |
"id": '1234', |
| 1445 |
"stack_name": 'my_stack', |
| 1446 |
"creation_time": "2013-08-04T20:57:55Z", |
| 1447 |
"updated_time": "2013-08-04T20:57:55Z", |
| 1448 |
"stack_status": "CREATE_IN_PROGRESS" |
| 1449 |
}) |
| 1450 |
resource = resources.Resource(None, { |
| 1451 |
'stack_id': 'my_stack', |
| 1452 |
'resource_name': 'resource' |
| 1453 |
}) |
| 1454 |
|
| 1455 |
def setUp(self): |
| 1456 |
super().setUp() |
| 1457 |
self.cmd = stack.StackHookClear(self.app, None) |
| 1458 |
self.mock_client.stacks.get.return_value = self.stack |
| 1459 |
self.mock_client.resources.list.return_value = [self.resource] |
| 1460 |
|
| 1461 |
def test_hook_clear(self): |
| 1462 |
arglist = ['my_stack', 'resource'] |
| 1463 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1464 |
self.cmd.take_action(parsed_args) |
| 1465 |
self.mock_client.resources.signal.assert_called_once_with( |
| 1466 |
data={'unset_hook': 'pre-create'}, |
| 1467 |
resource_name='resource', |
| 1468 |
stack_id='my_stack') |
| 1469 |
|
| 1470 |
def test_hook_clear_pre_create(self): |
| 1471 |
arglist = ['my_stack', 'resource', '--pre-create'] |
| 1472 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1473 |
self.cmd.take_action(parsed_args) |
| 1474 |
self.mock_client.resources.signal.assert_called_once_with( |
| 1475 |
data={'unset_hook': 'pre-create'}, |
| 1476 |
resource_name='resource', |
| 1477 |
stack_id='my_stack') |
| 1478 |
|
| 1479 |
def test_hook_clear_pre_update(self): |
| 1480 |
arglist = ['my_stack', 'resource', '--pre-update'] |
| 1481 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1482 |
self.cmd.take_action(parsed_args) |
| 1483 |
self.mock_client.resources.signal.assert_called_once_with( |
| 1484 |
data={'unset_hook': 'pre-update'}, |
| 1485 |
resource_name='resource', |
| 1486 |
stack_id='my_stack') |
| 1487 |
|
| 1488 |
def test_hook_clear_pre_delete(self): |
| 1489 |
arglist = ['my_stack', 'resource', '--pre-delete'] |
| 1490 |
parsed_args = self.check_parser(self.cmd, arglist, []) |
| 1491 |
self.cmd.take_action(parsed_args) |
| 1492 |
self.mock_client.resources.signal.assert_called_once_with( |
| 1493 |
data={'unset_hook': 'pre-delete'}, |
| 1494 |
resource_name='resource', |
| 1495 |
stack_id='my_stack') |
| 1496 |
|
| 1497 |
|
| 1498 |
class TestEnvironmentStackShow(TestStack): |
| 1499 |
|
| 1500 |
SAMPLE_ENV = { |
| 1501 |
'parameters': {'p1': 'v1'}, |
| 1502 |
'resource_registry': {'resources': {'r1': 't1'}}, |
| 1503 |
'parameter_defaults': {'p1': 'v_default'} |
| 1504 |
} |
| 1505 |
|
| 1506 |
def setUp(self): |
| 1507 |
super().setUp() |
| 1508 |
self.cmd = stack.EnvironmentShowStack(self.app, None) |
| 1509 |
|
| 1510 |
def test_stack_environment_show(self): |
| 1511 |
|
| 1512 |
columns, outputs = self._test_stack_environment_show(self.SAMPLE_ENV) |
| 1513 |
|
| 1514 |
|
| 1515 |
self.assertEqual([{'p1': 'v1'}, {'resources': {'r1': 't1'}}, |
| 1516 |
{'p1': 'v_default'}], outputs) |
| 1517 |
|
| 1518 |
def test_stack_environment_show_no_parameters(self): |
| 1519 |
|
| 1520 |
sample_env = copy.deepcopy(self.SAMPLE_ENV) |
| 1521 |
sample_env['parameters'] = {} |
| 1522 |
|
| 1523 |
|
| 1524 |
columns, outputs = self._test_stack_environment_show(sample_env) |
| 1525 |
|
| 1526 |
|
| 1527 |
self.assertEqual([{}, {'resources': {'r1': 't1'}}, |
| 1528 |
{'p1': 'v_default'}], outputs) |
| 1529 |
|
| 1530 |
def test_stack_environment_show_no_registry(self): |
| 1531 |
|
| 1532 |
sample_env = copy.deepcopy(self.SAMPLE_ENV) |
| 1533 |
sample_env['resource_registry'] = {'resources': {}} |
| 1534 |
|
| 1535 |
|
| 1536 |
columns, outputs = self._test_stack_environment_show(sample_env) |
| 1537 |
|
| 1538 |
|
| 1539 |
self.assertEqual([{'p1': 'v1'}, {'resources': {}}, |
| 1540 |
{'p1': 'v_default'}], outputs) |
| 1541 |
|
| 1542 |
def test_stack_environment_show_no_param_defaults(self): |
| 1543 |
|
| 1544 |
sample_env = copy.deepcopy(self.SAMPLE_ENV) |
| 1545 |
sample_env['parameter_defaults'] = {} |
| 1546 |
|
| 1547 |
|
| 1548 |
columns, outputs = self._test_stack_environment_show(sample_env) |
| 1549 |
|
| 1550 |
|
| 1551 |
self.assertEqual([{'p1': 'v1'}, {'resources': {'r1': 't1'}}, {}], |
| 1552 |
outputs) |
| 1553 |
|
| 1554 |
def _test_stack_environment_show(self, env): |
| 1555 |
self.stack_client.environment = mock.MagicMock( |
| 1556 |
return_value=env |
| 1557 |
) |
| 1558 |
|
| 1559 |
parsed_args = self.check_parser(self.cmd, ['test-stack'], []) |
| 1560 |
columns, outputs = self.cmd.take_action(parsed_args) |
| 1561 |
self.assertEqual(['parameters', 'resource_registry', |
| 1562 |
'parameter_defaults'], columns) |
| 1563 |
|
| 1564 |
return columns, outputs |