From 417a3c6b8ba7cf0452201e44e67e18da22e77ea1 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Tue, 3 Jul 2012 16:23:47 +0000 Subject: [PATCH] Drop xenapi session.get_imported_xenapi() Historically it was used to allow the xenapi fake module to be inserted in place of the real XenAPI module. However, it was used inconsistently and is unnecessary now that the session carries that information as well. Remove it by using the session.XenAPI attribute consistently, and then removing the stubs and ensuring that the fake module gets inserted directly into sys.modules. Change-Id: Id058e19eb7d932abf7b07a1e4324637a8d4b2ea6 --- nova/tests/test_xenapi.py | 33 ++++++++++----------------------- nova/tests/test_xensm.py | 2 +- nova/tests/xenapi/stubs.py | 31 ++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index 5181e638..c3a311e0 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -134,7 +134,7 @@ def stub_vm_utils_with_vdi_attached_here(function, should_return=True): return decorated_function -class XenAPIVolumeTestCase(test.TestCase): +class XenAPIVolumeTestCase(stubs.XenAPITestBase): """Unit tests for Volume operations.""" def setUp(self): super(XenAPIVolumeTestCase, self).setUp() @@ -147,7 +147,6 @@ class XenAPIVolumeTestCase(test.TestCase): firewall_driver='nova.virt.xenapi.firewall.' 'Dom0IptablesFirewallDriver') db_fakes.stub_out_db_instance_api(self.stubs) - xenapi_fake.reset() self.instance_values = {'id': 1, 'project_id': self.user_id, 'user_id': 'fake', @@ -252,7 +251,7 @@ class XenAPIVolumeTestCase(test.TestCase): '/dev/sdc') -class XenAPIVMTestCase(test.TestCase): +class XenAPIVMTestCase(stubs.XenAPITestBase): """Unit tests for VM operations.""" def setUp(self): super(XenAPIVMTestCase, self).setUp() @@ -262,7 +261,6 @@ class XenAPIVMTestCase(test.TestCase): instance_name_template='%d', firewall_driver='nova.virt.xenapi.firewall.' 'Dom0IptablesFirewallDriver') - xenapi_fake.reset() xenapi_fake.create_local_srs() xenapi_fake.create_local_pifs() db_fakes.stub_out_db_instance_api(self.stubs) @@ -868,7 +866,7 @@ class XenAPIDiffieHellmanTestCase(test.TestCase): self._test_encryption(''.join(['abcd' for i in xrange(1024)])) -class XenAPIMigrateInstance(test.TestCase): +class XenAPIMigrateInstance(stubs.XenAPITestBase): """Unit test for verifying migration-related actions.""" def setUp(self): @@ -880,7 +878,6 @@ class XenAPIMigrateInstance(test.TestCase): 'Dom0IptablesFirewallDriver') stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests) db_fakes.stub_out_db_instance_api(self.stubs) - xenapi_fake.reset() xenapi_fake.create_network('fake', FLAGS.flat_network_bridge) self.user_id = 'fake' self.project_id = 'fake' @@ -1122,7 +1119,7 @@ class CompareVersionTestCase(test.TestCase): self.assertTrue(vmops.cmp_version('1.2.3', '1.2.3.4') < 0) -class XenAPIHostTestCase(test.TestCase): +class XenAPIHostTestCase(stubs.XenAPITestBase): """Tests HostState, which holds metrics from XenServer that get reported back to the Schedulers.""" @@ -1131,7 +1128,6 @@ class XenAPIHostTestCase(test.TestCase): self.flags(xenapi_connection_url='test_url', xenapi_connection_password='test_pass') stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests) - xenapi_fake.reset() xenapi_fake.create_local_srs() self.conn = xenapi_conn.XenAPIDriver(False) @@ -1175,7 +1171,7 @@ class XenAPIHostTestCase(test.TestCase): self._test_host_action(self.conn.set_host_enabled, False, 'disabled') -class XenAPIAutoDiskConfigTestCase(test.TestCase): +class XenAPIAutoDiskConfigTestCase(stubs.XenAPITestBase): def setUp(self): super(XenAPIAutoDiskConfigTestCase, self).setUp() self.flags(target_host='127.0.0.1', @@ -1184,7 +1180,6 @@ class XenAPIAutoDiskConfigTestCase(test.TestCase): firewall_driver='nova.virt.xenapi.firewall.' 'Dom0IptablesFirewallDriver') stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests) - xenapi_fake.reset() self.conn = xenapi_conn.XenAPIDriver(False) self.user_id = 'fake' @@ -1266,7 +1261,7 @@ class XenAPIAutoDiskConfigTestCase(test.TestCase): self.assertIsPartitionCalled(True) -class XenAPIGenerateLocal(test.TestCase): +class XenAPIGenerateLocal(stubs.XenAPITestBase): """Test generating of local disks, like swap and ephemeral""" def setUp(self): super(XenAPIGenerateLocal, self).setUp() @@ -1278,7 +1273,6 @@ class XenAPIGenerateLocal(test.TestCase): 'Dom0IptablesFirewallDriver') stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests) db_fakes.stub_out_db_instance_api(self.stubs) - xenapi_fake.reset() self.conn = xenapi_conn.XenAPIDriver(False) self.user_id = 'fake' @@ -1343,7 +1337,7 @@ class XenAPIGenerateLocal(test.TestCase): self.assertCalled(instance) -class XenAPIBWUsageTestCase(test.TestCase): +class XenAPIBWUsageTestCase(stubs.XenAPITestBase): def setUp(self): super(XenAPIBWUsageTestCase, self).setUp() self.stubs.Set(vm_utils, 'compile_metrics', @@ -1354,7 +1348,6 @@ class XenAPIBWUsageTestCase(test.TestCase): firewall_driver='nova.virt.xenapi.firewall.' 'Dom0IptablesFirewallDriver') stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests) - xenapi_fake.reset() self.conn = xenapi_conn.XenAPIDriver(False) @classmethod @@ -1378,7 +1371,7 @@ class XenAPIBWUsageTestCase(test.TestCase): # TODO(salvatore-orlando): this class and # nova.tests.test_libvirt.IPTablesFirewallDriverTestCase share a lot of code. # Consider abstracting common code in a base class for firewall driver testing. -class XenAPIDom0IptablesFirewallTestCase(test.TestCase): +class XenAPIDom0IptablesFirewallTestCase(stubs.XenAPITestBase): _in_nat_rules = [ '# Generated by iptables-save v1.4.10 on Sat Feb 19 00:03:19 2011', @@ -1424,7 +1417,6 @@ class XenAPIDom0IptablesFirewallTestCase(test.TestCase): instance_name_template='%d', firewall_driver='nova.virt.xenapi.firewall.' 'Dom0IptablesFirewallDriver') - xenapi_fake.reset() xenapi_fake.create_local_srs() xenapi_fake.create_local_pifs() self.user_id = 'mappin' @@ -1679,12 +1671,8 @@ class XenAPIDom0IptablesFirewallTestCase(test.TestCase): self.assertEqual(1, len(rules)) -class XenAPISRSelectionTestCase(test.TestCase): +class XenAPISRSelectionTestCase(stubs.XenAPITestBase): """Unit tests for testing we find the right SR.""" - def setUp(self): - super(XenAPISRSelectionTestCase, self).setUp() - xenapi_fake.reset() - def test_safe_find_sr_raise_exception(self): """Ensure StorageRepositoryNotFound is raise when wrong filter.""" self.flags(sr_matching_filter='yadayadayada') @@ -1733,7 +1721,7 @@ class XenAPISRSelectionTestCase(test.TestCase): expected) -class XenAPIAggregateTestCase(test.TestCase): +class XenAPIAggregateTestCase(stubs.XenAPITestBase): """Unit tests for aggregate operations.""" def setUp(self): super(XenAPIAggregateTestCase, self).setUp() @@ -1744,7 +1732,6 @@ class XenAPIAggregateTestCase(test.TestCase): firewall_driver='nova.virt.xenapi.firewall.' 'Dom0IptablesFirewallDriver', host='host') - xenapi_fake.reset() host_ref = xenapi_fake.get_all('host')[0] stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests) self.context = context.get_admin_context() diff --git a/nova/tests/test_xensm.py b/nova/tests/test_xensm.py index 18be0266..659a7b92 100644 --- a/nova/tests/test_xensm.py +++ b/nova/tests/test_xensm.py @@ -35,7 +35,7 @@ LOG = logging.getLogger(__name__) FLAGS = flags.FLAGS -class XenSMTestCase(test.TestCase): +class XenSMTestCase(stubs.XenAPITestBase): """Unit tests for Xen Storage Manager Volume operations.""" def _get_sm_backend_params(self): diff --git a/nova/tests/xenapi/stubs.py b/nova/tests/xenapi/stubs.py index 255f26a2..275e3ce6 100644 --- a/nova/tests/xenapi/stubs.py +++ b/nova/tests/xenapi/stubs.py @@ -16,8 +16,10 @@ """Stubouts, mocks and fixtures for the test suite""" import random +import sys from nova.openstack.common import jsonutils +from nova import test import nova.tests.image.fake from nova.virt.xenapi import connection as xenapi_conn from nova.virt.xenapi import fake @@ -51,17 +53,9 @@ def stubout_instance_snapshot(stubs): def stubout_session(stubs, cls, product_version=(5, 6, 2), **opt_args): - """Stubs out three methods from XenAPISession""" - def fake_import(self): - """Stubs out get_imported_xenapi of XenAPISession""" - fake_module = 'nova.virt.xenapi.fake' - from_list = ['fake'] - return __import__(fake_module, globals(), locals(), from_list, -1) - + """Stubs out methods from XenAPISession""" stubs.Set(xenapi_conn.XenAPISession, '_create_session', lambda s, url: cls(url, **opt_args)) - stubs.Set(xenapi_conn.XenAPISession, 'get_imported_xenapi', - fake_import) stubs.Set(xenapi_conn.XenAPISession, '_get_product_version', lambda s: product_version) @@ -363,3 +357,22 @@ def stub_out_migration_methods(stubs): stubs.Set(vm_utils, 'get_vdi_for_vm_safely', fake_get_vdi) stubs.Set(vm_utils, 'get_sr_path', fake_get_sr_path) stubs.Set(vm_utils, 'generate_ephemeral', fake_generate_ephemeral) + + +class XenAPITestBase(test.TestCase): + def setUp(self): + super(XenAPITestBase, self).setUp() + + self.orig_XenAPI = sys.modules.get('XenAPI') + sys.modules['XenAPI'] = fake + + fake.reset() + + def tearDown(self): + if self.orig_XenAPI is not None: + sys.modules['XenAPI'] = self.orig_XenAPI + self.orig_XenAPI = None + else: + sys.modules.pop('XenAPI') + + super(XenAPITestBase, self).tearDown()