Skip to content

Commit 71e1ade

Browse files
committed
Allow error status to be specified
For some apis, heat, the error status is "failed". This patch changes the wait_for_status method to allow for the error status to be passed in the same way as the success status. Change-Id: I20db4051d3f5611a4b13fe23ea8798b82a40da81
1 parent 5f6f456 commit 71e1ade

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

openstackclient/common/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ def wait_for_status(status_f,
298298
res_id,
299299
status_field='status',
300300
success_status=['active'],
301+
error_status=['error'],
301302
sleep_time=5,
302303
callback=None):
303304
"""Wait for status change on a resource during a long-running operation
@@ -316,7 +317,7 @@ def wait_for_status(status_f,
316317
if status in success_status:
317318
retval = True
318319
break
319-
elif status == 'error':
320+
elif status in error_status:
320321
retval = False
321322
break
322323
if callback:

openstackclient/tests/common/test_utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,46 @@ def test_sort_items_with_invalid_direction(self):
136136
utils.sort_items,
137137
items, sort_str)
138138

139+
@mock.patch.object(time, 'sleep')
140+
def test_wait_for_status_ok(self, mock_sleep):
141+
# Tests the normal flow that the resource is status=active
142+
resource = mock.MagicMock(status='ACTIVE')
143+
status_f = mock.Mock(return_value=resource)
144+
res_id = str(uuid.uuid4())
145+
self.assertTrue(utils.wait_for_status(status_f, res_id,))
146+
self.assertFalse(mock_sleep.called)
147+
148+
@mock.patch.object(time, 'sleep')
149+
def test_wait_for_status_ok__with_overrides(self, mock_sleep):
150+
# Tests the normal flow that the resource is status=complete
151+
resource = mock.MagicMock(my_status='COMPLETE')
152+
status_f = mock.Mock(return_value=resource)
153+
res_id = str(uuid.uuid4())
154+
self.assertTrue(utils.wait_for_status(status_f, res_id,
155+
status_field='my_status',
156+
success_status=['complete']))
157+
self.assertFalse(mock_sleep.called)
158+
159+
@mock.patch.object(time, 'sleep')
160+
def test_wait_for_status_error(self, mock_sleep):
161+
# Tests that we fail if the resource is status=error
162+
resource = mock.MagicMock(status='ERROR')
163+
status_f = mock.Mock(return_value=resource)
164+
res_id = str(uuid.uuid4())
165+
self.assertFalse(utils.wait_for_status(status_f, res_id))
166+
self.assertFalse(mock_sleep.called)
167+
168+
@mock.patch.object(time, 'sleep')
169+
def test_wait_for_status_error_with_overrides(self, mock_sleep):
170+
# Tests that we fail if the resource is my_status=failed
171+
resource = mock.MagicMock(my_status='FAILED')
172+
status_f = mock.Mock(return_value=resource)
173+
res_id = str(uuid.uuid4())
174+
self.assertFalse(utils.wait_for_status(status_f, res_id,
175+
status_field='my_status',
176+
error_status=['failed']))
177+
self.assertFalse(mock_sleep.called)
178+
139179
@mock.patch.object(time, 'sleep')
140180
def test_wait_for_delete_ok(self, mock_sleep):
141181
# Tests the normal flow that the resource is deleted with a 404 coming

0 commit comments

Comments
 (0)