From 132e53ffe3754723d72b007093c7291967957817 Mon Sep 17 00:00:00 2001 From: Brian Lamar Date: Thu, 3 Nov 2011 22:27:27 -0400 Subject: [PATCH] Skip libvirt tests when libvirt not present. Currently the libvirt tests don't make use of the test.skip_if decorator. This should make it more obvious when the tests are not getting run. We recently ran into an issue where libvirt tests were 'passing' in Jenkins but they were actually not getting run, this should make things more clear. Change-Id: Ied18d2403fc9accd6c6f739b08c9fe7cc3624bb2 --- nova/tests/test_libvirt.py | 100 +++++++++++-------------------------- 1 file changed, 30 insertions(+), 70 deletions(-) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 0de8aff9..0fe2a487 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -43,7 +43,21 @@ from nova.virt.libvirt import volume from nova.volume import driver as volume_driver from nova.tests import fake_network -libvirt = None + +try: + import libvirt + connection.libvirt = libvirt +except ImportError: + libvirt = None + + +try: + import libxml2 + connection.libxml2 = libxml2 +except ImportError: + libxml2 = None + + FLAGS = flags.FLAGS LOG = logging.getLogger('nova.tests.test_libvirt') @@ -228,6 +242,10 @@ class FakeVolumeDriver(object): return "" +def missing_libvirt(): + return libvirt is None or libxml2 is None + + class LibvirtConnTestCase(test.TestCase): def setUp(self): @@ -252,20 +270,6 @@ class LibvirtConnTestCase(test.TestCase): 'local_gb': 20, 'instance_type_id': '5'} # m1.small - def lazy_load_library_exists(self): - """check if libvirt is available.""" - # try to connect libvirt. if fail, skip test. - try: - import libvirt - import libxml2 - except ImportError: - return False - global libvirt - libvirt = __import__('libvirt') - connection.libvirt = __import__('libvirt') - connection.libxml2 = __import__('libxml2') - return True - def create_fake_libvirt_mock(self, **kwargs): """Defining mocks for LibvirtConnection(libvirt is not used).""" @@ -352,10 +356,8 @@ class LibvirtConnTestCase(test.TestCase): instance_data = dict(self.test_instance) self._check_xml_and_container(instance_data) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_in_ami_format(self): - if not self.lazy_load_library_exists(): - return - self.flags(image_service='nova.image.fake.FakeImageService') # Start test @@ -392,10 +394,8 @@ class LibvirtConnTestCase(test.TestCase): self.assertEquals(snapshot['disk_format'], 'ami') self.assertEquals(snapshot['name'], snapshot_name) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_in_raw_format(self): - if not self.lazy_load_library_exists(): - return - self.flags(image_service='nova.image.fake.FakeImageService') # Start test @@ -428,10 +428,8 @@ class LibvirtConnTestCase(test.TestCase): self.assertEquals(snapshot['disk_format'], 'raw') self.assertEquals(snapshot['name'], snapshot_name) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_in_qcow2_format(self): - if not self.lazy_load_library_exists(): - return - self.flags(image_service='nova.image.fake.FakeImageService') self.flags(snapshot_image_format='qcow2') @@ -465,10 +463,8 @@ class LibvirtConnTestCase(test.TestCase): self.assertEquals(snapshot['disk_format'], 'qcow2') self.assertEquals(snapshot['name'], snapshot_name) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_no_image_architecture(self): - if not self.lazy_load_library_exists(): - return - self.flags(image_service='nova.image.fake.FakeImageService') # Start test @@ -732,12 +728,9 @@ class LibvirtConnTestCase(test.TestCase): conn.update_available_resource, self.context, 'dummy') + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_ensure_filtering_rules_for_instance_timeout(self): """ensure_filtering_fules_for_instance() finishes with timeout.""" - # Skip if non-libvirt environment - if not self.lazy_load_library_exists(): - return - # Preparing mocks def fake_none(self, *args): return @@ -786,12 +779,9 @@ class LibvirtConnTestCase(test.TestCase): db.instance_destroy(self.context, instance_ref['id']) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_live_migration_raises_exception(self): """Confirms recover method is called when exceptions are raised.""" - # Skip if non-libvirt environment - if not self.lazy_load_library_exists(): - return - # Preparing data self.compute = utils.import_object(FLAGS.compute_manager) instance_dict = {'host': 'fake', @@ -860,13 +850,9 @@ class LibvirtConnTestCase(test.TestCase): self.mox.ReplayAll() self.assertEqual(conn.pre_live_migration(vol), None) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_pre_block_migration_works_correctly(self): """Confirms pre_block_migration works correctly.""" - - # Skip if non-libvirt environment - if not self.lazy_load_library_exists(): - return - # Replace instances_path since this testcase creates tmpfile tmpdir = tempfile.mkdtemp() store = FLAGS.instances_path @@ -897,12 +883,9 @@ class LibvirtConnTestCase(test.TestCase): # Restore FLAGS.instances_path FLAGS.instances_path = store + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_get_instance_disk_info_works_correctly(self): """Confirms pre_block_migration works correctly.""" - # Skip if non-libvirt environment - if not self.lazy_load_library_exists(): - return - # Test data instance_ref = db.instance_create(self.context, self.test_instance) dummyxml = ("instance-0000000a" @@ -955,11 +938,8 @@ class LibvirtConnTestCase(test.TestCase): db.instance_destroy(self.context, instance_ref['id']) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_spawn_with_network_info(self): - # Skip if non-libvirt environment - if not self.lazy_load_library_exists(): - return - # Preparing mocks def fake_none(self, instance): return @@ -1074,11 +1054,8 @@ class LibvirtConnTestCase(test.TestCase): compute_driver = driver.ComputeDriver() self.assertRaises(NotImplementedError, compute_driver.reboot, *args) + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_immediate_delete(self): - # Skip if non-libvirt environment - if not self.lazy_load_library_exists(): - return - conn = connection.LibvirtConnection(False) self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') connection.LibvirtConnection._conn.lookupByName = lambda x: None @@ -1129,20 +1106,6 @@ class IptablesFirewallTestCase(test.TestCase): self.fw = firewall.IptablesFirewallDriver( get_connection=lambda: self.fake_libvirt_connection) - def lazy_load_library_exists(self): - """check if libvirt is available.""" - # try to connect libvirt. if fail, skip test. - try: - import libvirt - import libxml2 - except ImportError: - return False - global libvirt - libvirt = __import__('libvirt') - connection.libvirt = __import__('libvirt') - connection.libxml2 = __import__('libxml2') - return True - in_nat_rules = [ '# Generated by iptables-save v1.4.10 on Sat Feb 19 00:03:19 2011', '*nat', @@ -1366,11 +1329,8 @@ class IptablesFirewallTestCase(test.TestCase): self.mox.ReplayAll() self.fw.do_refresh_security_group_rules("fake") + @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_unfilter_instance_undefines_nwfilter(self): - # Skip if non-libvirt environment - if not self.lazy_load_library_exists(): - return - admin_ctxt = context.get_admin_context() fakefilter = NWFilterFakes()