From 41bc820e14e0f50af627ae52fa0a8abbe134d5a5 Mon Sep 17 00:00:00 2001 From: Eli Qiao Date: Sat, 28 Feb 2015 17:13:00 +0800 Subject: [PATCH] Libvirt: preallocate_images CONFIG can be arbitrary characters We can now set preallocate_images as any characters as long as it is not equal to 'none' to enable preallocate images function, this is too bad. This patch changes the condition to equal to 'space'/'Space'/'SPACE' Closes-Bug: 1427092 Change-Id: Ie8f4861fb77d2c3c3a8976fb7768ced12e7d8d5b --- .../unit/virt/libvirt/test_imagebackend.py | 24 +++++++++++++++++++ nova/virt/libvirt/imagebackend.py | 7 ++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/nova/tests/unit/virt/libvirt/test_imagebackend.py b/nova/tests/unit/virt/libvirt/test_imagebackend.py index 4739a59322cc..cfb6a7db3c6f 100644 --- a/nova/tests/unit/virt/libvirt/test_imagebackend.py +++ b/nova/tests/unit/virt/libvirt/test_imagebackend.py @@ -1363,9 +1363,33 @@ class BackendTestCase(test.NoDBTestCase): def test_image_raw(self): self._test_image('raw', imagebackend.Raw, imagebackend.Raw) + def test_image_raw_preallocate_images(self): + flags = ('space', 'Space', 'SPACE') + for f in flags: + self.flags(preallocate_images=f) + raw = imagebackend.Raw(self.INSTANCE, 'fake_disk', '/tmp/xyz') + self.assertTrue(raw.preallocate) + + def test_image_raw_preallocate_images_bad_conf(self): + self.flags(preallocate_images='space1') + raw = imagebackend.Raw(self.INSTANCE, 'fake_disk', '/tmp/xyz') + self.assertFalse(raw.preallocate) + def test_image_qcow2(self): self._test_image('qcow2', imagebackend.Qcow2, imagebackend.Qcow2) + def test_image_qcow2_preallocate_images(self): + flags = ('space', 'Space', 'SPACE') + for f in flags: + self.flags(preallocate_images=f) + qcow = imagebackend.Qcow2(self.INSTANCE, 'fake_disk', '/tmp/xyz') + self.assertTrue(qcow.preallocate) + + def test_image_qcow2_preallocate_images_bad_conf(self): + self.flags(preallocate_images='space1') + qcow = imagebackend.Qcow2(self.INSTANCE, 'fake_disk', '/tmp/xyz') + self.assertFalse(qcow.preallocate) + def test_image_lvm(self): self.flags(images_volume_group='FakeVG', group='libvirt') self._test_image('lvm', imagebackend.Lvm, imagebackend.Lvm) diff --git a/nova/virt/libvirt/imagebackend.py b/nova/virt/libvirt/imagebackend.py index b85b1e8c7418..266f677b6a6f 100644 --- a/nova/virt/libvirt/imagebackend.py +++ b/nova/virt/libvirt/imagebackend.py @@ -23,6 +23,7 @@ from oslo_config import cfg from oslo_log import log as logging from oslo_serialization import jsonutils from oslo_utils import excutils +from oslo_utils import strutils from oslo_utils import units import six @@ -383,7 +384,8 @@ class Raw(Image): self.path = (path or os.path.join(libvirt_utils.get_instance_path(instance), disk_name)) - self.preallocate = CONF.preallocate_images != 'none' + self.preallocate = ( + strutils.to_slug(CONF.preallocate_images) == 'space') self.disk_info_path = os.path.join(os.path.dirname(self.path), 'disk.info') self.correct_format() @@ -455,7 +457,8 @@ class Qcow2(Image): self.path = (path or os.path.join(libvirt_utils.get_instance_path(instance), disk_name)) - self.preallocate = CONF.preallocate_images != 'none' + self.preallocate = ( + strutils.to_slug(CONF.preallocate_images) == 'space') self.disk_info_path = os.path.join(os.path.dirname(self.path), 'disk.info') self.resolve_driver_format()