diff options
author | Jenkins <jenkins@review.openstack.org> | 2014-01-28 18:18:18 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-01-28 18:18:18 +0000 |
commit | 14b6bc655145c832bd9c822e48f877818e0e53ff (patch) | |
tree | e2af8c7738d701ad8df07e351e9e875340d3e397 /nova | |
parent | f80eba8e1157c732b99ce4ce34662e457a466942 (diff) | |
parent | e19d9653604a80839534abf7462a4fa8b4d146fa (diff) | |
download | nova-14b6bc655145c832bd9c822e48f877818e0e53ff.tar.gz |
Merge "disk/api.py: refactors extends and adds missing tests"
Diffstat (limited to 'nova')
-rw-r--r-- | nova/tests/virt/disk/test_api.py | 68 | ||||
-rw-r--r-- | nova/virt/disk/api.py | 20 |
2 files changed, 83 insertions, 5 deletions
diff --git a/nova/tests/virt/disk/test_api.py b/nova/tests/virt/disk/test_api.py index 9c366af712..4995b92617 100644 --- a/nova/tests/virt/disk/test_api.py +++ b/nova/tests/virt/disk/test_api.py @@ -23,10 +23,28 @@ from nova.openstack.common import processutils from nova import test from nova import utils from nova.virt.disk import api +from nova.virt.disk.mount import api as mount + + +class FakeMount(object): + device = None + + @staticmethod + def instance_for_format(imgfile, mountdir, partition, imgfmt): + return FakeMount() + + def get_dev(self): + pass + + def unget_dev(self): + pass class APITestCase(test.NoDBTestCase): + def setUp(self): + super(APITestCase, self).setUp() + def test_can_resize_need_fs_type_specified(self): # NOTE(mikal): Bug 1094373 saw a regression where we failed to # treat a failure to mount as a failure to be able to resize the @@ -90,3 +108,53 @@ class APITestCase(test.NoDBTestCase): processutils.ProcessExecutionError("fs error")) self.mox.ReplayAll() api.resize2fs(imgfile) + + def test_extend_qcow_success(self): + imgfile = tempfile.NamedTemporaryFile() + imgsize = 10 + device = "/dev/sdh" + use_cow = True + + self.flags(resize_fs_using_block_device=True) + mounter = FakeMount.instance_for_format( + imgfile, None, None, 'qcow2') + mounter.device = device + + self.mox.StubOutWithMock(api, 'can_resize_image') + self.mox.StubOutWithMock(utils, 'execute') + self.mox.StubOutWithMock(api, 'is_image_partitionless') + self.mox.StubOutWithMock(mounter, 'get_dev') + self.mox.StubOutWithMock(mounter, 'unget_dev') + self.mox.StubOutWithMock(api, 'resize2fs') + self.mox.StubOutWithMock(mount.Mount, 'instance_for_format') + + api.can_resize_image(imgfile, imgsize).AndReturn(True) + utils.execute('qemu-img', 'resize', imgfile, imgsize) + api.is_image_partitionless(imgfile, use_cow).AndReturn(True) + mount.Mount.instance_for_format( + imgfile, None, None, 'qcow2').AndReturn(mounter) + mounter.get_dev().AndReturn(True) + api.resize2fs(mounter.device, run_as_root=True, check_exit_code=[0]) + mounter.unget_dev() + + self.mox.ReplayAll() + api.extend(imgfile, imgsize, use_cow=use_cow) + + def test_extend_raw_success(self): + imgfile = tempfile.NamedTemporaryFile() + imgsize = 10 + device = "/dev/sdh" + use_cow = False + + self.mox.StubOutWithMock(api, 'can_resize_image') + self.mox.StubOutWithMock(utils, 'execute') + self.mox.StubOutWithMock(api, 'is_image_partitionless') + self.mox.StubOutWithMock(api, 'resize2fs') + + api.can_resize_image(imgfile, imgsize).AndReturn(True) + utils.execute('qemu-img', 'resize', imgfile, imgsize) + api.is_image_partitionless(imgfile, use_cow).AndReturn(True) + api.resize2fs(imgfile, run_as_root=False, check_exit_code=[0]) + + self.mox.ReplayAll() + api.extend(imgfile, imgsize, use_cow=use_cow) diff --git a/nova/virt/disk/api.py b/nova/virt/disk/api.py index ac43253902..33ad2da431 100644 --- a/nova/virt/disk/api.py +++ b/nova/virt/disk/api.py @@ -157,18 +157,28 @@ def extend(image, size, use_cow=False): if not is_image_partitionless(image, use_cow): return + def safe_resize2fs(dev, run_as_root=False, finally_call=lambda: None): + try: + resize2fs(dev, run_as_root=run_as_root, check_exit_code=[0]) + except processutils.ProcessExecutionError as exc: + LOG.debug(_("Resizing the file system with resize2fs " + "has failed with error: %s"), exc) + finally: + finally_call() + # NOTE(vish): attempts to resize filesystem if use_cow: if CONF.resize_fs_using_block_device: # in case of non-raw disks we can't just resize the image, but # rather the mounted device instead - mounter = mount.Mount.instance_for_format(image, None, None, - 'qcow2') + mounter = mount.Mount.instance_for_format( + image, None, None, 'qcow2') if mounter.get_dev(): - resize2fs(mounter.device, run_as_root=True) - mounter.unget_dev() + safe_resize2fs(mounter.device, + run_as_root=True, + finally_call=mounter.unget_dev) else: - resize2fs(image) + safe_resize2fs(image) def can_resize_image(image, size): |