Check if there is a context set before updating the store

There are cases when there is no request context set when calling an
async task in vCenter. Don't update the local store in those cases.

Change-Id: Ieef237bd5de0ad1c12afbf57426c29e3ca678dfc
This commit is contained in:
Radoslav Gerganov 2018-08-31 09:48:23 +03:00
parent 577dc74e44
commit a47f0d0c4f
2 changed files with 32 additions and 1 deletions

View File

@ -407,7 +407,8 @@ class VMwareAPISession(object):
:param task: managed object reference of the task
:param ctx: request context for the corresponding task
"""
ctx.update_store()
if ctx is not None:
ctx.update_store()
try:
# we poll tasks too often, so skip logging the opID as it generates
# too much noise in the logs

View File

@ -427,6 +427,36 @@ class VMwareAPISessionTest(base.TestCase):
mock_curr_ctx.assert_called_once()
self.assertEqual(3, ctx.update_store.call_count)
@mock.patch.object(context, 'get_current', return_value=None)
def test_wait_for_task_no_ctx(self, mock_curr_ctx):
api_session = self._create_api_session(True)
task_info_list = [('queued', 0), ('running', 40), ('success', 100)]
task_info_list_size = len(task_info_list)
def invoke_api_side_effect(module, method, *args, **kwargs):
(state, progress) = task_info_list.pop(0)
task_info = mock.Mock()
task_info.progress = progress
task_info.queueTime = datetime(2016, 12, 6, 15, 29, 43, 79060)
task_info.completeTime = datetime(2016, 12, 6, 15, 29, 50, 79060)
task_info.state = state
return task_info
api_session.invoke_api = mock.Mock(side_effect=invoke_api_side_effect)
task = mock.Mock()
with mock.patch.object(greenthread, 'sleep'):
ret = api_session.wait_for_task(task)
self.assertEqual('success', ret.state)
self.assertEqual(100, ret.progress)
api_session.invoke_api.assert_called_with(vim_util,
'get_object_property',
api_session.vim, task,
'info',
skip_op_id=True)
self.assertEqual(task_info_list_size,
api_session.invoke_api.call_count)
mock_curr_ctx.assert_called_once()
@mock.patch.object(context, 'get_current')
def test_wait_for_task_with_error_state(self, mock_curr_ctx):
api_session = self._create_api_session(True)