Merge "Handle deleted instances when refreshing the info_cache"

This commit is contained in:
Jenkins
2017-09-21 13:33:50 +00:00
committed by Gerrit Code Review
2 changed files with 25 additions and 2 deletions

View File

@@ -562,8 +562,14 @@ def refresh_info_cache_for_instance(context, instance):
:param instance: The instance object.
"""
if instance.info_cache is not None:
instance.info_cache.refresh()
if instance.info_cache is not None and not instance.deleted:
# Catch the exception in case the instance got deleted after the check
# instance.deleted was executed
try:
instance.info_cache.refresh()
except exception.InstanceInfoCacheNotFound:
LOG.debug("Can not refresh info_cache because instance "
"was not found", instance=instance)
def usage_volume_info(vol_usage):

View File

@@ -857,6 +857,23 @@ class ComputeUtilsGetValFromSysMetadata(test.NoDBTestCase):
self.assertEqual(0, result)
class ComputeUtilsRefreshInfoCacheForInstance(test.NoDBTestCase):
def test_instance_info_cache_not_found(self):
inst = fake_instance.fake_instance_obj('fake-context')
net_info = model.NetworkInfo([])
info_cache = objects.InstanceInfoCache(network_info=net_info)
inst.info_cache = info_cache
with mock.patch.object(inst.info_cache, 'refresh',
side_effect=exception.InstanceInfoCacheNotFound(
instance_uuid=inst.uuid)):
# we expect that the raised exception is ok
with mock.patch.object(compute_utils.LOG, 'debug') as log_mock:
compute_utils.refresh_info_cache_for_instance(None, inst)
log_mock.assert_called_once_with(
'Can not refresh info_cache because instance '
'was not found', instance=inst)
class ComputeUtilsGetRebootTypes(test.NoDBTestCase):
def setUp(self):
super(ComputeUtilsGetRebootTypes, self).setUp()