From 6b494dc99d0ecc4efe44c703b91fa7918d52e62f Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Thu, 21 Feb 2013 19:33:23 +0000 Subject: [PATCH] Fix launching libvirt instances with swap The code in commit 7be531fe9462f2b07d4a1abf6687f649d1dfbb89 Author: Daniel P. Berrange Date: Tue Jan 15 20:43:48 2013 +0000 Refactor code for setting up libvirt disk mappings accidentally broken launching of instances where the flavor had non-zero swap specified. This was not caught due to complete lack of test coverage for the '_create_image' method. This adds two test cases for that method, exercising this bug. It also fixes a bug in netutils which was not handling a NULL network mapping, which is a valid scenario for several callers. Fixes bug #1131306 Change-Id: I214561ba3ec6440d11a5afb0665e6b6f990bd148 Signed-off-by: Daniel P. Berrange --- nova/tests/test_libvirt.py | 123 +++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 945d627f..b1135902 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -2569,6 +2569,129 @@ class LibvirtConnTestCase(test.TestCase): None) self.assertTrue(self.create_image_called) + def test_create_image_plain(self): + gotFiles = [] + + def fake_image(self, instance, name, image_type=''): + class FakeImage(imagebackend.Image): + def __init__(self, instance, name): + self.path = os.path.join(instance['name'], name) + + def create_image(self, prepare_template, base, + size, *args, **kwargs): + pass + + def cache(self, fetch_func, filename, size=None, + *args, **kwargs): + gotFiles.append({'filename': filename, + 'size': size}) + + def snapshot(self, name): + pass + + return FakeImage(instance, name) + + def fake_none(*args, **kwargs): + return + + def fake_get_info(instance): + return {'state': power_state.RUNNING} + + # Stop 'libvirt_driver._create_image' touching filesystem + self.stubs.Set(nova.virt.libvirt.imagebackend.Backend, "image", + fake_image) + + instance_ref = self.test_instance + instance_ref['image_ref'] = 1 + instance = db.instance_create(self.context, instance_ref) + + conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) + self.stubs.Set(conn, 'to_xml', fake_none) + self.stubs.Set(conn, '_create_domain_and_network', fake_none) + self.stubs.Set(conn, 'get_info', fake_get_info) + + image_meta = {'id': instance['image_ref']} + disk_info = blockinfo.get_disk_info(CONF.libvirt_type, + instance, + None, + image_meta) + xml = conn.to_xml(instance, None, + disk_info, image_meta) + conn._create_image(context, instance, xml, + disk_info['mapping']) + + wantFiles = [ + {'filename': '356a192b7913b04c54574d18c28d46e6395428ab', + 'size': 10 * 1024 * 1024 * 1024}, + {'filename': 'ephemeral_20_default', + 'size': 20 * 1024 * 1024 * 1024}, + ] + self.assertEquals(gotFiles, wantFiles) + + def test_create_image_with_swap(self): + gotFiles = [] + + def fake_image(self, instance, name, image_type=''): + class FakeImage(imagebackend.Image): + def __init__(self, instance, name): + self.path = os.path.join(instance['name'], name) + + def create_image(self, prepare_template, base, + size, *args, **kwargs): + pass + + def cache(self, fetch_func, filename, size=None, + *args, **kwargs): + gotFiles.append({'filename': filename, + 'size': size}) + + def snapshot(self, name): + pass + + return FakeImage(instance, name) + + def fake_none(*args, **kwargs): + return + + def fake_get_info(instance): + return {'state': power_state.RUNNING} + + # Stop 'libvirt_driver._create_image' touching filesystem + self.stubs.Set(nova.virt.libvirt.imagebackend.Backend, "image", + fake_image) + + instance_ref = self.test_instance + instance_ref['image_ref'] = 1 + instance = db.instance_create(self.context, instance_ref) + + # Turn on some swap to exercise that codepath in _create_image + instance['instance_type']['swap'] = 500 + + conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) + self.stubs.Set(conn, 'to_xml', fake_none) + self.stubs.Set(conn, '_create_domain_and_network', fake_none) + self.stubs.Set(conn, 'get_info', fake_get_info) + + image_meta = {'id': instance['image_ref']} + disk_info = blockinfo.get_disk_info(CONF.libvirt_type, + instance, + None, + image_meta) + xml = conn.to_xml(instance, None, + disk_info, image_meta) + conn._create_image(context, instance, xml, + disk_info['mapping']) + + wantFiles = [ + {'filename': '356a192b7913b04c54574d18c28d46e6395428ab', + 'size': 10 * 1024 * 1024 * 1024}, + {'filename': 'ephemeral_20_default', + 'size': 20 * 1024 * 1024 * 1024}, + {'filename': 'swap_500', + 'size': 500 * 1024 * 1024}, + ] + self.assertEquals(gotFiles, wantFiles) + def test_get_console_output_file(self): fake_libvirt_utils.files['console.log'] = '01234567890'