From 1b7cea76abde83e9f937e33b56d54fa885f2a0b9 Mon Sep 17 00:00:00 2001 From: Michael Still Date: Mon, 24 Dec 2012 09:51:19 +1100 Subject: [PATCH] Verify the disk file exists before running qemu-img on it. Should resolve bug 955788, although it is a little hard to tell because the bug is so old. Change-Id: Ic0c47f4b6181f56a98cf58d4ebe2cc926d06d524 --- nova/tests/test_image_utils.py | 20 ++++++++++++++++++++ nova/tests/test_libvirt.py | 11 +++++++++++ nova/tests/test_libvirt_utils.py | 4 ++++ nova/virt/images.py | 3 +++ 4 files changed, 38 insertions(+) diff --git a/nova/tests/test_image_utils.py b/nova/tests/test_image_utils.py index 9c040f2e1cc5..a9768f8216f8 100644 --- a/nova/tests/test_image_utils.py +++ b/nova/tests/test_image_utils.py @@ -14,6 +14,8 @@ # License for the specific language governing permissions and limitations # under the License. +import os + from nova import test from nova import utils @@ -41,7 +43,9 @@ disk size: 96K 'format': f, 'path': path, }) + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((output, '')) self.mox.ReplayAll() @@ -60,7 +64,9 @@ disk size: 96K output = template_output % ({ 'path': path, }) + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((output, '')) self.mox.ReplayAll() @@ -84,7 +90,9 @@ disk size: 96K 'vsize_b': i, 'path': path, }) + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((output, '')) self.mox.ReplayAll() @@ -96,7 +104,9 @@ disk size: 96K 'vsize_b': i, 'path': path, }) + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((output, '')) self.mox.ReplayAll() @@ -113,7 +123,9 @@ cluster_size: 65536 disk size: 96K blah BLAH: bb """ + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((example_output, '')) self.mox.ReplayAll() @@ -133,7 +145,9 @@ cluster_size: 65536 disk size: 963434 backing file: /var/lib/nova/a328c7998805951a_2 """ + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((example_output, '')) self.mox.ReplayAll() @@ -158,7 +172,9 @@ ID TAG VM SIZE DATE VM CLOCK 1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000 backing file: /var/lib/nova/a328c7998805951a_2 (actual path: /b/3a988059e51a_2) """ + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((example_output, '')) self.mox.ReplayAll() @@ -184,7 +200,9 @@ ID TAG VM SIZE DATE VM CLOCK 4 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000 junk stuff: bbb """ + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((example_output, '')) self.mox.ReplayAll() @@ -206,7 +224,9 @@ ID TAG VM SIZE DATE VM CLOCK 3 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000 4 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000 """ + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((example_output, '')) self.mox.ReplayAll() diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 6e941cb45a5e..e4466fdf36f9 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -2366,6 +2366,9 @@ class LibvirtConnTestCase(test.TestCase): "cluster_size: 2097152\n" "backing file: /test/dummy (actual path: /backing/file)\n") + self.mox.StubOutWithMock(os.path, "exists") + os.path.exists('/test/disk.local').AndReturn(True) + self.mox.StubOutWithMock(utils, "execute") utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', '/test/disk.local').AndReturn((ret, '')) @@ -3866,8 +3869,10 @@ class LibvirtUtilsTestCase(test.TestCase): libvirt_utils.create_image('qcow2', '/some/stuff', '1234567891234') def test_create_cow_image(self): + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') rval = ('', '') + os.path.exists('/some/path').AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', '/some/path').AndReturn(rval) utils.execute('qemu-img', 'create', '-f', 'qcow2', @@ -3891,7 +3896,9 @@ class LibvirtUtilsTestCase(test.TestCase): self.assertEquals(result, expected_result) def test_get_disk_size(self): + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists('/some/path').AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', '/some/path').AndReturn(('''image: 00000001 file format: raw @@ -4050,7 +4057,11 @@ disk size: 4.4M''', '')) "backing file: /foo/bar/baz\n" "...: ...\n"), '' + def return_true(*args, **kwargs): + return True + self.stubs.Set(utils, 'execute', fake_execute) + self.stubs.Set(os.path, 'exists', return_true) out = libvirt_utils.get_disk_backing_file('') self.assertEqual(out, 'baz') diff --git a/nova/tests/test_libvirt_utils.py b/nova/tests/test_libvirt_utils.py index 89410390bcea..60f0682a8137 100644 --- a/nova/tests/test_libvirt_utils.py +++ b/nova/tests/test_libvirt_utils.py @@ -15,6 +15,8 @@ # License for the specific language governing permissions and limitations # under the License. +import os + from nova import test from nova import utils from nova.virt.libvirt import utils as libvirt_utils @@ -30,7 +32,9 @@ cluster_size: 65536 disk size: 96K blah BLAH: bb """ + self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') + os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((example_output, '')) self.mox.ReplayAll() diff --git a/nova/virt/images.py b/nova/virt/images.py index 514c8755f8bb..244b33ab777e 100644 --- a/nova/virt/images.py +++ b/nova/virt/images.py @@ -176,6 +176,9 @@ class QemuImgInfo(object): def qemu_img_info(path): """Return a object containing the parsed output from qemu-img info.""" + if not os.path.exists(path): + return QemuImgInfo() + out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) return QemuImgInfo(out)