From cce455ab1afed0256ffe5b537517bcfb66240395 Mon Sep 17 00:00:00 2001 From: James Slagle Date: Tue, 2 Feb 2016 07:06:42 -0500 Subject: [PATCH] Simplify checking for stack complete This is a small simplification over Change-Id: Id325e793a510e751ec4736883f6f97cb80a5623f that should also not print out duplicated event lists. This fixes the issue where the last event is seen, but the overcloud stack is not yet CREATE_COMPLETE, which caused an infinte loop in the client. Change-Id: Idb5b02435667d6ed504e60a4ef6779ee4191c726 --- tripleoclient/tests/test_utils.py | 55 +++++++++++++++++-------------- tripleoclient/utils.py | 33 +++++++++---------- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/tripleoclient/tests/test_utils.py b/tripleoclient/tests/test_utils.py index c1113f77a..bbf4d85e7 100644 --- a/tripleoclient/tests/test_utils.py +++ b/tripleoclient/tests/test_utils.py @@ -139,26 +139,12 @@ class TestWaitForStackUtil(TestCase): def test_wait_for_stack_ready(self, sleep_mock, mock_el): stack = mock.Mock() stack.stack_name = 'stack' + stack.stack_status = "CREATE_COMPLETE" self.mock_orchestration.stacks.get.return_value = stack - mock_el.side_effect = [[ - self.mock_event('stack', 'aaa', 'Stack CREATE started', - 'CREATE_IN_PROGRESS', '2015-10-14T02:25:21Z'), - self.mock_event('thing', 'bbb', 'state changed', - 'CREATE_IN_PROGRESS', '2015-10-14T02:25:21Z'), - ], [ - self.mock_event('thing', 'ccc', 'state changed', - 'CREATE_COMPLETE', '2015-10-14T02:25:43Z'), - self.mock_event('stack', 'ddd', - 'Stack CREATE completed successfully', - 'CREATE_COMPLETE', '2015-10-14T02:25:43Z'), - ]] - complete = utils.wait_for_stack_ready(self.mock_orchestration, 'stack') - self.assertTrue(complete) - - sleep_mock.assert_called_once_with(mock.ANY) + sleep_mock.assert_not_called() def test_wait_for_stack_ready_no_stack(self): self.mock_orchestration.stacks.get.return_value = None @@ -172,25 +158,44 @@ class TestWaitForStackUtil(TestCase): def test_wait_for_stack_ready_failed(self, sleep_mock, mock_el): stack = mock.Mock() stack.stack_name = 'stack' + stack.stack_status = "CREATE_FAILED" self.mock_orchestration.stacks.get.return_value = stack + + complete = utils.wait_for_stack_ready(self.mock_orchestration, 'stack') + + self.assertFalse(complete) + + sleep_mock.assert_not_called() + + @mock.patch("heatclient.common.event_utils.get_events") + @mock.patch('time.sleep', return_value=None) + def test_wait_for_stack_in_progress(self, sleep_mock, mock_el): + mock_el.side_effect = [[ self.mock_event('stack', 'aaa', 'Stack CREATE started', 'CREATE_IN_PROGRESS', '2015-10-14T02:25:21Z'), self.mock_event('thing', 'bbb', 'state changed', 'CREATE_IN_PROGRESS', '2015-10-14T02:25:21Z'), ], [ - self.mock_event('thing', 'ccc', 'ouch', - 'CREATE_FAILED', '2015-10-14T02:25:43Z'), - self.mock_event('stack', 'ddd', 'ouch', - 'CREATE_FAILED', '2015-10-14T02:25:43Z'), - ]] + self.mock_event('thing', 'ccc', 'state changed', + 'CREATE_COMPLETE', '2015-10-14T02:25:43Z'), + self.mock_event('stack', 'ddd', + 'Stack CREATE completed successfully', + 'CREATE_COMPLETE', '2015-10-14T02:25:43Z'), + ], [], []] - complete = utils.wait_for_stack_ready(self.mock_orchestration, 'stack', - verbose=True) + stack = mock.Mock() + stack.stack_name = 'stack' + stack.stack_status = 'CREATE_IN_PROGRESS' + complete_stack = mock.Mock() + complete_stack.stack_name = 'stack' + complete_stack.stack_status = 'CREATE_COMPLETE' + self.mock_orchestration.stacks.get.side_effect = [ + stack, stack, stack, complete_stack] - self.assertFalse(complete) + utils.wait_for_stack_ready(self.mock_orchestration, 'stack') - sleep_mock.assert_called_once_with(mock.ANY) + self.assertEqual(2, sleep_mock.call_count) class TestWaitForIntrospection(TestCase): diff --git a/tripleoclient/utils.py b/tripleoclient/utils.py index 672f56e56..2be21de79 100644 --- a/tripleoclient/utils.py +++ b/tripleoclient/utils.py @@ -207,35 +207,32 @@ def wait_for_stack_ready(orchestration_client, stack_name, marker=None, return False stack_name = stack.stack_name - current_marker = marker while True: + events = event_utils.get_events(orchestration_client, stack_id=stack_name, nested_depth=2, event_args={'sort_dir': 'asc', - 'marker': current_marker}) + 'marker': marker}) if len(events) >= 1: # set marker to last event that was received. - new_marker = getattr(events[-1], 'id', None) - if new_marker == current_marker: - # We got the same marker twice, wrap around - current_marker = marker - else: - current_marker = new_marker + marker = getattr(events[-1], 'id', None) if verbose: events_log = event_log_formatter(events) print(events_log) - for event in events: - # check if stack event was also received - if getattr(event, 'resource_name', '') == stack_name: - stack_status = getattr(event, 'resource_status', '') - print("Stack %(name)s %(status)s" % dict( - name=stack_name, status=stack_status)) - if stack_status == '%s_COMPLETE' % action: - return True - elif stack_status == '%s_FAILED' % action: - return False + + stack = get_stack(orchestration_client, stack_name) + stack_status = stack.stack_status + if stack_status == '%s_COMPLETE' % action: + print("Stack %(name)s %(status)s" % dict( + name=stack_name, status=stack_status)) + return True + elif stack_status == '%s_FAILED' % action: + print("Stack %(name)s %(status)s" % dict( + name=stack_name, status=stack_status)) + return False + time.sleep(5)