From 252f43e6e88225304f86f66b563919d573bb0714 Mon Sep 17 00:00:00 2001 From: Giulio Fidente Date: Thu, 25 Aug 2016 02:43:22 +0200 Subject: [PATCH] Use heatclient poll_for_events in wait_for_stack_ready Resort to the new heatclient builtin to check for the stack status and removes custom implementation from tripleoclient utils. Change-Id: I5f48eb87a5e06846cd40c1b61aade60acdcb2a18 --- requirements.txt | 2 +- tripleoclient/tests/test_utils.py | 14 ++------ tripleoclient/utils.py | 55 ++++++------------------------- 3 files changed, 14 insertions(+), 57 deletions(-) diff --git a/requirements.txt b/requirements.txt index fc4662fe9..23bd47392 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ cliff!=1.16.0,!=1.17.0,>=1.15.0 # Apache-2.0 ipaddress>=1.0.7;python_version<'3.3' # PSF passlib>=1.6 # BSD python-ironic-inspector-client>=1.5.0 # Apache-2.0 -python-heatclient>=1.1.0 # Apache-2.0 +python-heatclient>=1.4.0 # Apache-2.0 python-ironicclient>=1.6.0 # Apache-2.0 python-mistralclient>=2.0.0 # Apache-2.0 python-openstackclient>=2.1.0 # Apache-2.0 diff --git a/tripleoclient/tests/test_utils.py b/tripleoclient/tests/test_utils.py index 7e7935168..a65f11b61 100644 --- a/tripleoclient/tests/test_utils.py +++ b/tripleoclient/tests/test_utils.py @@ -182,8 +182,7 @@ class TestWaitForStackUtil(TestCase): return e @mock.patch("heatclient.common.event_utils.get_events") - @mock.patch('time.sleep', return_value=None) - def test_wait_for_stack_ready(self, sleep_mock, mock_el): + def test_wait_for_stack_ready(self, mock_el): stack = mock.Mock() stack.stack_name = 'stack' stack.stack_status = "CREATE_COMPLETE" @@ -191,7 +190,6 @@ class TestWaitForStackUtil(TestCase): complete = utils.wait_for_stack_ready(self.mock_orchestration, 'stack') self.assertTrue(complete) - sleep_mock.assert_not_called() def test_wait_for_stack_ready_no_stack(self): self.mock_orchestration.stacks.get.return_value = None @@ -201,8 +199,7 @@ class TestWaitForStackUtil(TestCase): self.assertFalse(complete) @mock.patch("heatclient.common.event_utils.get_events") - @mock.patch('time.sleep', return_value=None) - def test_wait_for_stack_ready_failed(self, sleep_mock, mock_el): + def test_wait_for_stack_ready_failed(self, mock_el): stack = mock.Mock() stack.stack_name = 'stack' stack.stack_status = "CREATE_FAILED" @@ -212,11 +209,8 @@ class TestWaitForStackUtil(TestCase): 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): + def test_wait_for_stack_in_progress(self, mock_el): mock_el.side_effect = [[ self.mock_event('stack', 'aaa', 'Stack CREATE started', @@ -242,8 +236,6 @@ class TestWaitForStackUtil(TestCase): utils.wait_for_stack_ready(self.mock_orchestration, 'stack') - self.assertEqual(2, sleep_mock.call_count) - def test_create_environment_file(self): json_file_path = "env.json" diff --git a/tripleoclient/utils.py b/tripleoclient/utils.py index f63661f32..ab6484c37 100644 --- a/tripleoclient/utils.py +++ b/tripleoclient/utils.py @@ -26,6 +26,7 @@ import six import socket import struct import subprocess +import sys import time import yaml @@ -271,51 +272,15 @@ def wait_for_stack_ready(orchestration_client, stack_name, marker=None, return False stack_name = stack.stack_name - while True: - - events = event_utils.get_events(orchestration_client, - stack_id=stack_name, nested_depth=2, - event_args={'sort_dir': 'asc', - 'marker': marker}) - - if len(events) >= 1: - # set marker to last event that was received. - marker = getattr(events[-1], 'id', None) - - if verbose: - events_log = event_log_formatter(events) - print(events_log) - - 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) - - -def event_log_formatter(events): - """Return the events in log format.""" - event_log = [] - log_format = ("%(event_time)s " - "[%(rsrc_name)s]: %(rsrc_status)s %(rsrc_status_reason)s") - for event in events: - event_time = getattr(event, 'event_time', '') - log = log_format % { - 'event_time': event_time.replace('T', ' '), - 'rsrc_name': getattr(event, 'resource_name', ''), - 'rsrc_status': getattr(event, 'resource_status', ''), - 'rsrc_status_reason': getattr(event, 'resource_status_reason', '') - } - event_log.append(log) - - return "\n".join(event_log) + if verbose: + out = sys.stdout + else: + out = open(os.devnull, "w") + stack_status, msg = event_utils.poll_for_events( + orchestration_client, stack_name, action=action, + poll_period=5, marker=marker, out=out, nested_depth=2) + print(msg) + return stack_status == '%s_COMPLETE' % action def nodes_in_states(baremetal_client, states):