Make xenapi driver use flavor fields instead of legacy ones

This makes the xenapi driver use instance.flavor.root_gb instead
of the legacy instance.root_gb field (and friends).

The actual change here is very small, but a lot of xenapi tests
around this needed to be object-ified.

Change-Id: I6a45a22207a44e57506bc2171b10ba1df76277e0
This commit is contained in:
Dan Smith
2016-07-12 10:59:29 -07:00
parent bb7e7ecfb4
commit 0353b43aae
3 changed files with 41 additions and 20 deletions

View File

@@ -894,7 +894,9 @@ class MigrateDiskAndPowerOffTestCase(VMOpsTestBase):
def test_migrate_disk_and_power_off_works_down(self,
migrate_up, migrate_down, *mocks):
instance = {"root_gb": 2, "ephemeral_gb": 0, "uuid": "uuid"}
instance = objects.Instance(
flavor=objects.Flavor(root_gb=2, ephemeral_gb=0),
uuid="uuid")
flavor = fake_flavor.fake_flavor_obj(self.context, root_gb=1,
ephemeral_gb=0)
@@ -906,7 +908,10 @@ class MigrateDiskAndPowerOffTestCase(VMOpsTestBase):
def test_migrate_disk_and_power_off_works_up(self,
migrate_up, migrate_down, *mocks):
instance = {"root_gb": 1, "ephemeral_gb": 1, "uuid": "uuid"}
instance = objects.Instance(
flavor=objects.Flavor(root_gb=1,
ephemeral_gb=1),
uuid="uuid")
flavor = fake_flavor.fake_flavor_obj(self.context, root_gb=2,
ephemeral_gb=2)
@@ -918,7 +923,7 @@ class MigrateDiskAndPowerOffTestCase(VMOpsTestBase):
def test_migrate_disk_and_power_off_resize_down_ephemeral_fails(self,
migrate_up, migrate_down, *mocks):
instance = {"ephemeral_gb": 2}
instance = objects.Instance(flavor=objects.Flavor(ephemeral_gb=2))
flavor = fake_flavor.fake_flavor_obj(self.context, ephemeral_gb=1)
self.assertRaises(exception.ResizeError,
@@ -1184,19 +1189,22 @@ class BootableTestCase(VMOpsTestBase):
@mock.patch.object(vm_utils, 'update_vdi_virtual_size', autospec=True)
class ResizeVdisTestCase(VMOpsTestBase):
def test_dont_resize_root_volumes_osvol_false(self, mock_resize):
instance = fake_instance.fake_db_instance(root_gb=20)
instance = fake_instance.fake_instance_obj(
None, flavor=objects.Flavor(root_gb=20))
vdis = {'root': {'osvol': False, 'ref': 'vdi_ref'}}
self.vmops._resize_up_vdis(instance, vdis)
self.assertTrue(mock_resize.called)
def test_dont_resize_root_volumes_osvol_true(self, mock_resize):
instance = fake_instance.fake_db_instance(root_gb=20)
instance = fake_instance.fake_instance_obj(
None, flavor=objects.Flavor(root_gb=20))
vdis = {'root': {'osvol': True}}
self.vmops._resize_up_vdis(instance, vdis)
self.assertFalse(mock_resize.called)
def test_dont_resize_root_volumes_no_osvol(self, mock_resize):
instance = fake_instance.fake_db_instance(root_gb=20)
instance = fake_instance.fake_instance_obj(
None, flavor=objects.Flavor(root_gb=20))
vdis = {'root': {}}
self.vmops._resize_up_vdis(instance, vdis)
self.assertFalse(mock_resize.called)
@@ -1205,7 +1213,8 @@ class ResizeVdisTestCase(VMOpsTestBase):
def test_ensure_ephemeral_resize_with_root_volume(self, mock_sizes,
mock_resize):
mock_sizes.return_value = [2000, 1000]
instance = fake_instance.fake_db_instance(root_gb=20, ephemeral_gb=20)
instance = fake_instance.fake_instance_obj(
None, flavor=objects.Flavor(root_gb=20, ephemeral_gb=20))
ephemerals = {"4": {"ref": 4}, "5": {"ref": 5}}
vdis = {'root': {'osvol': True, 'ref': 'vdi_ref'},
'ephemerals': ephemerals}
@@ -1220,18 +1229,21 @@ class ResizeVdisTestCase(VMOpsTestBase):
self.assertFalse(g.called)
def test_resize_up_vdis_root(self, mock_resize):
instance = {"root_gb": 20, "ephemeral_gb": 0}
instance = objects.Instance(flavor=objects.Flavor(root_gb=20,
ephemeral_gb=0))
self.vmops._resize_up_vdis(instance, {"root": {"ref": "vdi_ref"}})
mock_resize.assert_called_once_with(self.vmops._session, instance,
"vdi_ref", 20)
def test_resize_up_vdis_zero_disks(self, mock_resize):
instance = {"root_gb": 0, "ephemeral_gb": 0}
instance = objects.Instance(flavor=objects.Flavor(root_gb=0,
ephemeral_gb=0))
self.vmops._resize_up_vdis(instance, {"root": {}})
self.assertFalse(mock_resize.called)
def test_resize_up_vdis_no_vdis_like_initial_spawn(self, mock_resize):
instance = {"root_gb": 0, "ephemeral_gb": 3000}
instance = objects.Instance(flavor=objects.Flavor(root_gb=0,
ephemeral_gb=3000))
vdis = {}
self.vmops._resize_up_vdis(instance, vdis)
@@ -1241,7 +1253,8 @@ class ResizeVdisTestCase(VMOpsTestBase):
@mock.patch.object(vm_utils, 'get_ephemeral_disk_sizes')
def test_resize_up_vdis_ephemeral(self, mock_sizes, mock_resize):
mock_sizes.return_value = [2000, 1000]
instance = {"root_gb": 0, "ephemeral_gb": 3000}
instance = objects.Instance(flavor=objects.Flavor(root_gb=0,
ephemeral_gb=3000))
ephemerals = {"4": {"ref": 4}, "5": {"ref": 5}}
vdis = {"ephemerals": ephemerals}
@@ -1258,7 +1271,9 @@ class ResizeVdisTestCase(VMOpsTestBase):
mock_generate,
mock_resize):
mock_sizes.return_value = [2000, 1000]
instance = {"root_gb": 0, "ephemeral_gb": 3000, "uuid": "a"}
instance = objects.Instance(uuid='a',
flavor=objects.Flavor(root_gb=0,
ephemeral_gb=3000))
ephemerals = {"4": {"ref": 4}}
vdis = {"ephemerals": ephemerals}

View File

@@ -722,14 +722,16 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
fake_inject_instance_metadata)
if create_record:
flavor = objects.Flavor.get_by_id(self.context,
instance_type_id)
instance = objects.Instance(context=self.context)
instance.project_id = self.project_id
instance.user_id = self.user_id
instance.image_ref = image_ref
instance.kernel_id = kernel_id
instance.ramdisk_id = ramdisk_id
instance.root_gb = 20
instance.ephemeral_gb = 0
instance.root_gb = flavor.root_gb
instance.ephemeral_gb = flavor.ephemeral_gb
instance.instance_type_id = instance_type_id
instance.os_type = os_type
instance.hostname = hostname
@@ -737,8 +739,6 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
instance.architecture = architecture
instance.system_metadata = {}
flavor = objects.Flavor.get_by_id(self.context,
instance_type_id)
instance.flavor = flavor
instance.create()
else:
@@ -1704,6 +1704,8 @@ class XenAPIMigrateInstance(stubs.XenAPITestBase):
values = self.instance_values.copy()
values.update(kw)
instance = objects.Instance(context=self.context, **values)
instance.flavor = objects.Flavor(root_gb=80,
ephemeral_gb=0)
instance.create()
return instance
@@ -1750,6 +1752,8 @@ class XenAPIMigrateInstance(stubs.XenAPITestBase):
flavor = fake_flavor.fake_flavor_obj(self.context, root_gb=0,
ephemeral_gb=0)
instance = self._create_instance(root_gb=0, ephemeral_gb=0)
instance.flavor.root_gb = 0
instance.flavor.ephemeral_gb = 0
xenapi_fake.create_vm(instance['name'], 'Running')
conn = xenapi_conn.XenAPIDriver(fake.FakeVirtAPI(), False)
vm_ref = vm_utils.lookup(conn._session, instance['name'])
@@ -1851,6 +1855,8 @@ class XenAPIMigrateInstance(stubs.XenAPITestBase):
values["root_gb"] = 0
values["ephemeral_gb"] = 0
instance = create_instance_with_system_metadata(self.context, values)
instance.flavor.root_gb = 0
instance.flavor.ephemeral_gb = 0
def fake_vdi_resize(*args, **kwargs):
raise Exception("This shouldn't be called")

View File

@@ -1212,7 +1212,7 @@ class VMOps(object):
vm_utils.set_vm_name_label(self._session, vm_ref, name_label)
def _ensure_not_resize_down_ephemeral(self, instance, flavor):
old_gb = instance["ephemeral_gb"]
old_gb = instance.flavor.ephemeral_gb
new_gb = flavor.ephemeral_gb
if old_gb > new_gb:
@@ -1235,7 +1235,7 @@ class VMOps(object):
step=0,
total_steps=RESIZE_TOTAL_STEPS)
old_gb = instance['root_gb']
old_gb = instance.flavor.root_gb
new_gb = flavor.root_gb
resize_down = old_gb > new_gb
@@ -1272,7 +1272,7 @@ class VMOps(object):
mount_device)
def _resize_up_vdis(self, instance, vdis):
new_root_gb = instance['root_gb']
new_root_gb = instance.flavor.root_gb
root_vdi = vdis.get('root')
if new_root_gb and root_vdi:
if root_vdi.get('osvol', False): # Don't resize root volumes.
@@ -1289,7 +1289,7 @@ class VMOps(object):
# to resize, so nothing more to do here.
return
total_ephemeral_gb = instance['ephemeral_gb']
total_ephemeral_gb = instance.flavor.ephemeral_gb
if total_ephemeral_gb:
sizes = vm_utils.get_ephemeral_disk_sizes(total_ephemeral_gb)
# resize existing (migrated) ephemeral disks,