summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nova/tests/test_xenapi.py33
-rw-r--r--nova/tests/test_xensm.py2
-rw-r--r--nova/tests/xenapi/stubs.py31
-rw-r--r--nova/virt/xenapi/connection.py7
-rw-r--r--nova/virt/xenapi/host.py6
-rw-r--r--nova/virt/xenapi/pool.py9
-rw-r--r--nova/virt/xenapi/vmops.py11
-rw-r--r--nova/virt/xenapi/volumeops.py9
8 files changed, 50 insertions, 58 deletions
diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py
index 5181e6385..c3a311e0e 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 18be0266c..659a7b92f 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 255f26a22..275e3ce63 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()
diff --git a/nova/virt/xenapi/connection.py b/nova/virt/xenapi/connection.py
index 2777fa883..4d29b7ed8 100644
--- a/nova/virt/xenapi/connection.py
+++ b/nova/virt/xenapi/connection.py
@@ -500,7 +500,8 @@ class XenAPISession(object):
"""The session to invoke XenAPI SDK calls"""
def __init__(self, url, user, pw):
- self.XenAPI = self.get_imported_xenapi()
+ import XenAPI
+ self.XenAPI = XenAPI
self._sessions = queue.Queue()
self.is_slave = False
exception = self.XenAPI.Failure(_("Unable to log in to XenAPI "
@@ -558,10 +559,6 @@ class XenAPISession(object):
product_version = software_version['product_version']
return tuple(int(part) for part in product_version.split('.'))
- def get_imported_xenapi(self):
- """Stubout point. This can be replaced with a mock xenapi module."""
- return __import__('XenAPI')
-
def get_session_id(self):
"""Return a string session_id. Used for vnc consoles."""
with self._get_session() as session:
diff --git a/nova/virt/xenapi/host.py b/nova/virt/xenapi/host.py
index 7fe8a7b77..4633695fd 100644
--- a/nova/virt/xenapi/host.py
+++ b/nova/virt/xenapi/host.py
@@ -38,7 +38,6 @@ class Host(object):
Implements host related operations.
"""
def __init__(self, session):
- self.XenAPI = session.get_imported_xenapi()
self._session = session
def host_power_action(self, _host, action):
@@ -95,7 +94,7 @@ class Host(object):
notifications.send_update(ctxt, old_ref, new_ref)
break
- except self.XenAPI.Failure:
+ except self._session.XenAPI.Failure:
LOG.exception('Unable to migrate VM %(vm_ref)s'
'from %(host)s' % locals())
(old_ref, new_ref) = db.instance_update_and_get_original(
@@ -173,7 +172,6 @@ def call_xenhost(session, method, arg_dict):
out that behavior.
"""
# Create a task ID as something that won't match any instance ID
- XenAPI = session.get_imported_xenapi()
try:
result = session.call_plugin('xenhost', method, args=arg_dict)
if not result:
@@ -182,7 +180,7 @@ def call_xenhost(session, method, arg_dict):
except ValueError:
LOG.exception(_("Unable to get updated status"))
return None
- except XenAPI.Failure as e:
+ except session.XenAPI.Failure as e:
LOG.error(_("The call to %(method)s returned "
"an error: %(e)s.") % locals())
return e.details[1]
diff --git a/nova/virt/xenapi/pool.py b/nova/virt/xenapi/pool.py
index cb00faed5..07a03d029 100644
--- a/nova/virt/xenapi/pool.py
+++ b/nova/virt/xenapi/pool.py
@@ -48,7 +48,6 @@ class ResourcePool(object):
Implements resource pool operations.
"""
def __init__(self, session):
- self.XenAPI = session.get_imported_xenapi()
host_ref = session.get_xenapi_host()
host_rec = session.call_xenapi('host.get_record', host_ref)
self._host_name = host_rec['hostname']
@@ -139,7 +138,7 @@ class ResourcePool(object):
'master_user': FLAGS.xenapi_connection_username,
'master_pass': FLAGS.xenapi_connection_password, }
self._session.call_plugin('xenhost', 'host_join', args)
- except self.XenAPI.Failure as e:
+ except self._session.XenAPI.Failure as e:
LOG.error(_("Pool-Join failed: %(e)s") % locals())
raise exception.AggregateError(aggregate_id=aggregate_id,
action='add_to_aggregate',
@@ -158,7 +157,7 @@ class ResourcePool(object):
host_ref = self._session.call_xenapi('host.get_by_uuid', host_uuid)
self._session.call_xenapi("pool.eject", host_ref)
- except self.XenAPI.Failure as e:
+ except self._session.XenAPI.Failure as e:
LOG.error(_("Pool-eject failed: %(e)s") % locals())
raise exception.AggregateError(aggregate_id=aggregate_id,
action='remove_from_aggregate',
@@ -170,7 +169,7 @@ class ResourcePool(object):
pool_ref = self._session.call_xenapi("pool.get_all")[0]
self._session.call_xenapi("pool.set_name_label",
pool_ref, aggregate_name)
- except self.XenAPI.Failure as e:
+ except self._session.XenAPI.Failure as e:
LOG.error(_("Unable to set up pool: %(e)s.") % locals())
raise exception.AggregateError(aggregate_id=aggregate_id,
action='add_to_aggregate',
@@ -181,7 +180,7 @@ class ResourcePool(object):
try:
pool_ref = self._session.call_xenapi('pool.get_all')[0]
self._session.call_xenapi('pool.set_name_label', pool_ref, '')
- except self.XenAPI.Failure as e:
+ except self._session.XenAPI.Failure as e:
LOG.error(_("Pool-set_name_label failed: %(e)s") % locals())
raise exception.AggregateError(aggregate_id=aggregate_id,
action='remove_from_aggregate',
diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py
index 85fe73cd8..6af2ac9e0 100644
--- a/nova/virt/xenapi/vmops.py
+++ b/nova/virt/xenapi/vmops.py
@@ -152,7 +152,6 @@ class VMOps(object):
Management class for VM-related tasks
"""
def __init__(self, session):
- self.XenAPI = session.get_imported_xenapi()
self.compute_api = compute.API()
self._session = session
self.poll_rescue_last_ran = None
@@ -619,7 +618,7 @@ class VMOps(object):
template_vm_ref, template_vdi_uuids = vm_utils.create_snapshot(
self._session, instance, vm_ref, label)
return template_vm_ref, template_vdi_uuids
- except self.XenAPI.Failure, exc:
+ except self._session.XenAPI.Failure, exc:
LOG.error(_("Unable to Snapshot instance: %(exc)s"), locals(),
instance=instance)
raise
@@ -635,7 +634,7 @@ class VMOps(object):
_params = {'params': pickle.dumps(params)}
self._session.call_plugin('migration', 'transfer_vhd',
_params)
- except self.XenAPI.Failure:
+ except self._session.XenAPI.Failure:
msg = _("Failed to transfer vhd to new host")
raise exception.MigrationError(reason=msg)
@@ -977,7 +976,7 @@ class VMOps(object):
self._session.call_xenapi('VM.hard_shutdown', vm_ref)
else:
self._session.call_xenapi('VM.clean_shutdown', vm_ref)
- except self.XenAPI.Failure, exc:
+ except self._session.XenAPI.Failure, exc:
LOG.exception(exc)
def _find_root_vdi_ref(self, vm_ref):
@@ -1038,7 +1037,7 @@ class VMOps(object):
"""Destroys a VM record."""
try:
self._session.call_xenapi('VM.destroy', vm_ref)
- except self.XenAPI.Failure, exc:
+ except self._session.XenAPI.Failure, exc:
LOG.exception(exc)
return
@@ -1492,7 +1491,7 @@ class VMOps(object):
args.update(addl_args)
try:
return self._session.call_plugin(plugin, method, args)
- except self.XenAPI.Failure, e:
+ except self._session.XenAPI.Failure, e:
err_msg = e.details[-1].splitlines()[-1]
if 'TIMEOUT:' in err_msg:
LOG.error(_('TIMEOUT: The call to %(method)s timed out. '
diff --git a/nova/virt/xenapi/volumeops.py b/nova/virt/xenapi/volumeops.py
index 412b89be5..587c5877a 100644
--- a/nova/virt/xenapi/volumeops.py
+++ b/nova/virt/xenapi/volumeops.py
@@ -33,7 +33,6 @@ class VolumeOps(object):
"""
def __init__(self, session):
- self.XenAPI = session.get_imported_xenapi()
self._session = session
def create_volume_for_sm(self, volume, sr_uuid):
@@ -42,7 +41,7 @@ class VolumeOps(object):
sm_vol_rec = {}
try:
sr_ref = self._session.call_xenapi("SR.get_by_uuid", sr_uuid)
- except self.XenAPI.Failure, exc:
+ except self._session.XenAPI.Failure, exc:
LOG.exception(exc)
raise volume_utils.StorageError(_('Unable to get SR using uuid'))
#Create VDI
@@ -149,7 +148,7 @@ class VolumeOps(object):
try:
sr_ref = self.introduce_sr(uuid, label, sr_params)
LOG.debug(_('Introduced %(label)s as %(sr_ref)s.') % locals())
- except self.XenAPI.Failure, exc:
+ except self._session.XenAPI.Failure, exc:
LOG.exception(exc)
raise volume_utils.StorageError(
_('Unable to introduce Storage Repository'))
@@ -177,7 +176,7 @@ class VolumeOps(object):
try:
vbd_ref = vm_utils.create_vbd(self._session, vm_ref, vdi_ref,
dev_number, bootable=False)
- except self.XenAPI.Failure, exc:
+ except self._session.XenAPI.Failure, exc:
LOG.exception(exc)
self.forget_sr(uuid)
raise Exception(_('Unable to use SR %(sr_ref)s for'
@@ -185,7 +184,7 @@ class VolumeOps(object):
try:
self._session.call_xenapi("VBD.plug", vbd_ref)
- except self.XenAPI.Failure, exc:
+ except self._session.XenAPI.Failure, exc:
LOG.exception(exc)
self.forget_sr(uuid)
raise Exception(_('Unable to attach volume to instance %s')