Merge "Fix to tackle instances without an image assigned"

This commit is contained in:
Jenkins 2014-01-15 15:38:04 +00:00 committed by Gerrit Code Review
commit 8557ad1c0d
2 changed files with 24 additions and 1 deletions

View File

@ -82,7 +82,14 @@ class Client(object):
instance.flavor[attr] = getattr(flavor, attr, default) instance.flavor[attr] = getattr(flavor, attr, default)
def _with_image(self, instance): def _with_image(self, instance):
try:
iid = instance.image['id'] iid = instance.image['id']
except TypeError:
instance.image = None
instance.kernel_id = None
instance.ramdisk_id = None
return
try: try:
image = self.nova_client.images.get(iid) image = self.nova_client.images.get(iid)
except novaclient.exceptions.NotFound: except novaclient.exceptions.NotFound:

View File

@ -132,6 +132,14 @@ class TestNovaClient(test.BaseTestCase):
a.image = {'id': args[0]} a.image = {'id': args[0]}
return [a] return [a]
@staticmethod
def fake_instance_image_missing(*args, **kwargs):
a = mock.MagicMock()
a.id = 42
a.flavor = {'id': 666}
a.image = None
return [a]
def test_instance_get_all_by_host_unknown_image(self): def test_instance_get_all_by_host_unknown_image(self):
with patch.object(self.nv.nova_client.servers, 'list', with patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list_unknown_image): side_effect=self.fake_servers_list_unknown_image):
@ -196,3 +204,11 @@ class TestNovaClient(test.BaseTestCase):
instance = results[0] instance = results[0]
self.assertIsNone(instance.kernel_id) self.assertIsNone(instance.kernel_id)
self.assertEqual(instance.ramdisk_id, 21) self.assertEqual(instance.ramdisk_id, 21)
def test_with_missing_image_instance(self):
instances = self.fake_instance_image_missing()
results = self.nv._with_flavor_and_image(instances)
instance = results[0]
self.assertIsNone(instance.kernel_id)
self.assertIsNone(instance.image)
self.assertIsNone(instance.ramdisk_id)