Fixes Attribute Error when trying to spawn instance from vhd on HyperV

When trying to boot an instance from a vhd image we get a AttributeError.
This happens when we try to get the root disk size from the old flavor.
Since on creation there is no old flavor, instance.get_flavor() will return
None and thus the AttributeError when trying to return te root_gb.

This patch fixes this problem by checking if the return value from
instance.get_flavor() is None, in which case it returns
instance.root_gb.

Change-Id: Ief16679a6b80f6f8b9246ea6a50773e40824bb7f
Closes-Bug: 1421272
This commit is contained in:
Adelina Tuvenie
2015-02-12 09:07:57 -08:00
parent 1092e95b2e
commit bbd4b5f372
3 changed files with 23 additions and 6 deletions

View File

@@ -1140,6 +1140,7 @@ class HyperVAPITestCase(HyperVAPIBaseTestCase):
self._instance_data = self._get_instance_data()
instance = db.instance_create(self._context, self._instance_data)
instance['system_metadata'] = {}
instance['old_flavor'] = mock.MagicMock()
network_info = fake_network.fake_get_instance_nw_info(self.stubs)
m = fake.PathUtils.get_instance_dir(mox.IsA(str))

View File

@@ -19,8 +19,10 @@ import mock
from oslo_config import cfg
from nova import exception
from nova import objects
from nova import test
from nova.tests.unit import fake_instance
from nova.tests.unit.objects import test_flavor
from nova.virt.hyperv import constants
from nova.virt.hyperv import imagecache
@@ -55,6 +57,22 @@ class ImageCacheTestCase(test.NoDBTestCase):
self.imagecache._pathutils = mock.MagicMock()
self.imagecache._vhdutils = mock.MagicMock()
def _test_get_root_vhd_size_gb(self, old_flavor=True):
if old_flavor:
mock_flavor = objects.Flavor(**test_flavor.fake_flavor)
self.instance.old_flavor = mock_flavor
else:
self.instance.old_flavor = None
return self.imagecache._get_root_vhd_size_gb(self.instance)
def test_get_root_vhd_size_gb_old_flavor(self):
ret_val = self._test_get_root_vhd_size_gb()
self.assertEqual(test_flavor.fake_flavor['root_gb'], ret_val)
def test_get_root_vhd_size_gb(self):
ret_val = self._test_get_root_vhd_size_gb(old_flavor=False)
self.assertEqual(self.instance.root_gb, ret_val)
def _prepare_get_cached_image(self, path_exists, use_cow):
self.instance.image_ref = self.FAKE_IMAGE_REF
self.imagecache._pathutils.get_base_vhd_dir.return_value = (

View File

@@ -40,12 +40,10 @@ class ImageCache(object):
self._vhdutils = utilsfactory.get_vhdutils()
def _get_root_vhd_size_gb(self, instance):
try:
# In case of resizes we need the old root disk size
old_flavor = instance.get_flavor('old')
return old_flavor.root_gb
except KeyError:
return instance['root_gb']
if instance.old_flavor:
return instance.old_flavor.root_gb
else:
return instance.root_gb
def _resize_and_cache_vhd(self, instance, vhd_path):
vhd_info = self._vhdutils.get_vhd_info(vhd_path)