Add UT for resize operation

Add UT for resize operation.
Add exception for resizing disk smaller.
Change destroy UT to not use UUID cache mock since it's expected
to be removed when we can set PowerVM uuids.

Change-Id: Ib8ebd0efb9007ea61c3bffcc31badad3609bf4c0
This commit is contained in:
Kyle L. Henderson 2015-02-05 16:26:03 -06:00
parent a848f3bfd0
commit 76dd2cdbc2
3 changed files with 52 additions and 4 deletions

View File

@ -21,7 +21,7 @@ import sys
TEST_FLAVOR = flavor.Flavor(memory_mb=2048,
swap=0,
vcpu_weight=None,
root_gb=1,
root_gb=10,
id=2,
name=u'm1.small',
ephemeral_gb=0,
@ -33,6 +33,8 @@ TEST_INSTANCE = {
'id': 1,
'uuid': '49629a5c-f4c4-4721-9511-9725786ff2e5',
'display_name': 'Fake Instance',
'root_gb': 10,
'ephemeral_gb': 0,
'instance_type_id': '5',
'flavor': TEST_FLAVOR
}

View File

@ -228,12 +228,12 @@ class TestPowerVMDriver(test.TestCase):
'dlt_vopt')
@mock.patch('nova_powervm.virt.powervm.media.ConfigDrivePowerVM.'
'_validate_vopt_vg')
@mock.patch('nova_powervm.virt.powervm.vm.UUIDCache')
@mock.patch('nova_powervm.virt.powervm.vm.get_pvm_uuid')
@mock.patch('nova.objects.flavor.Flavor.get_by_id')
@mock.patch('nova_powervm.virt.powervm.localdisk.LocalStorage')
@mock.patch('pypowervm.jobs.power.power_off')
def test_destroy(self, mock_pwroff, mock_disk, mock_get_flv,
mock_uuidcache, mock_val_vopt, mock_dlt_vopt, mock_dlt,
mock_pvmuuid, mock_val_vopt, mock_dlt_vopt, mock_dlt,
mock_find, mock_apt, mock_sess):
"""Validates the basic PowerVM destroy."""
@ -261,6 +261,45 @@ class TestPowerVMDriver(test.TestCase):
# Delete LPAR was called
mock_dlt.assert_called_with(mock_apt, mock.ANY)
@mock.patch('pypowervm.adapter.Session')
@mock.patch('pypowervm.adapter.Adapter')
@mock.patch('nova_powervm.virt.powervm.host.find_entry_by_mtm_serial')
@mock.patch('nova_powervm.virt.powervm.vm.power_off')
@mock.patch('nova_powervm.virt.powervm.vm.update')
@mock.patch('nova.objects.flavor.Flavor.get_by_id')
@mock.patch('nova_powervm.virt.powervm.localdisk.LocalStorage')
def test_resize(self, mock_disk, mock_get_flv,
mock_update, mock_pwr_off, mock_find,
mock_apt, mock_sess):
"""Validates the PowerVM driver resize operation."""
drv = driver.PowerVMDriver(fake.FakeVirtAPI())
drv.init_host('FakeHost')
drv.adapter = mock_apt
# Set up the mocks to the resize operation.
inst = objects.Instance(**powervm.TEST_INSTANCE)
# Catch root disk resize smaller.
small_root = objects.Flavor(vcpus=1, memory_mb=2048, root_gb=9)
self.assertRaises(
exc.InstanceFaultRollback, drv.migrate_disk_and_power_off,
'context', inst, 'dest', small_root, 'network_info')
new_flav = objects.Flavor(vcpus=1, memory_mb=2048, root_gb=10)
# We don't support resize to different host.
self.assertRaises(
NotImplementedError, drv.migrate_disk_and_power_off,
'context', inst, 'bogus host', new_flav, 'network_info')
host = drv.get_host_ip_addr()
drv.migrate_disk_and_power_off(
'context', inst, host, new_flav, 'network_info')
mock_pwr_off.assert_called_with(
drv.adapter, inst, drv.host_uuid)
mock_update.assert_called_with(
drv.adapter, drv.host_uuid, inst, new_flav)
@mock.patch('nova_powervm.virt.powervm.driver.LOG')
def test_log_op(self, mock_log):
"""Validates the log_operations."""

View File

@ -17,7 +17,8 @@
from nova.compute import task_states
from nova import context as ctx
from nova.i18n import _LI
from nova import exception
from nova.i18n import _LI, _
from nova.objects import flavor as flavor_obj
from nova.openstack.common import log as logging
from nova.virt import configdrive
@ -379,6 +380,12 @@ class PowerVMDriver(driver.ComputeDriver):
# downstream code is expecting an object, so convert it.
if flavor and not isinstance(flavor, flavor_obj.Flavor):
flav_obj = flavor_obj.Flavor.get_by_id(context, flavor['id'])
else:
flav_obj = flavor
if flav_obj and flav_obj.root_gb < instance.root_gb:
raise exception.InstanceFaultRollback(
exception.ResizeError(reason=_('Cannot reduce disk size.')))
if dest == self.get_host_ip_addr():
self._log_operation('resize', instance)