summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevananda van der Veen <devananda.vdv@gmail.com>2014-04-24 11:10:32 -0700
committerDevananda van der Veen <devananda.vdv@gmail.com>2014-06-09 17:08:23 +0000
commit990d6545fb7b3488769f7e2d15382a6f895a67b9 (patch)
tree9c874cccca1e9457215190155214674a351aa7ec
parent63c10301245b5b07fe3224e1dc537bc07c94aa9c (diff)
downloadironic-990d6545fb7b3488769f7e2d15382a6f895a67b9.tar.gz
Mock seamicroclient lib in unit tests if not present
Mock the python-seamicroclient library from within ironic/tests/drivers so that test_seamicro.py can import ironic/drivers/modules/seamicro.py and run the unit tests on it, even when the external library is not present. Change-Id: I4f2cd75b2c64a5a280a5d45d5cea058fd432815b Closes-bug: #1312321
-rw-r--r--ironic/tests/drivers/__init__.py21
-rw-r--r--ironic/tests/drivers/test_seamicro.py8
-rw-r--r--ironic/tests/drivers/third_party_driver_mocks.py49
3 files changed, 71 insertions, 7 deletions
diff --git a/ironic/tests/drivers/__init__.py b/ironic/tests/drivers/__init__.py
index e69de29bb..0640dde8d 100644
--- a/ironic/tests/drivers/__init__.py
+++ b/ironic/tests/drivers/__init__.py
@@ -0,0 +1,21 @@
+# Copyright 2014 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# NOTE(deva): since __init__ is loaded before the files in the same directory,
+# and some third-party driver tests may need to have their
+# external libraries mocked, we load the file which does that
+# mocking here -- in the __init__.
+
+from ironic.tests.drivers import third_party_driver_mocks # noqa
diff --git a/ironic/tests/drivers/test_seamicro.py b/ironic/tests/drivers/test_seamicro.py
index cc47bbaa8..6620ed6f8 100644
--- a/ironic/tests/drivers/test_seamicro.py
+++ b/ironic/tests/drivers/test_seamicro.py
@@ -15,6 +15,7 @@
import uuid
import mock
+from seamicroclient import exceptions as seamicro_client_exception
from ironic.common import driver_factory
from ironic.common import exception
@@ -23,7 +24,6 @@ from ironic.conductor import task_manager
from ironic.db import api as dbapi
from ironic.drivers.modules import seamicro
from ironic.openstack.common import context
-from ironic.openstack.common import importutils
from ironic.tests import base
from ironic.tests.conductor import utils as mgr_utils
from ironic.tests.db import base as db_base
@@ -32,10 +32,6 @@ from ironic.tests.objects import utils as obj_utils
INFO_DICT = db_utils.get_test_seamicro_info()
-seamicroclient = importutils.try_import("seamicroclient")
-if seamicroclient:
- from seamicroclient import exceptions as seamicro_client_exception
-
class Fake_Server():
def __init__(self, active=False, *args, **kwargs):
@@ -264,8 +260,6 @@ class SeaMicroPowerDriverTestCase(db_base.DbTestCase):
def setUp(self):
super(SeaMicroPowerDriverTestCase, self).setUp()
- if not seamicroclient:
- self.skipTest("Seamicroclient library not found")
mgr_utils.mock_the_extension_manager(driver='fake_seamicro')
self.driver = driver_factory.get_driver('fake_seamicro')
self.node = obj_utils.create_test_node(self.context,
diff --git a/ironic/tests/drivers/third_party_driver_mocks.py b/ironic/tests/drivers/third_party_driver_mocks.py
new file mode 100644
index 000000000..a1b5d808e
--- /dev/null
+++ b/ironic/tests/drivers/third_party_driver_mocks.py
@@ -0,0 +1,49 @@
+# Copyright 2014 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""This module detects whether third-party libraries, utilized by third-party
+drivers, are present on the system. If they are not, it mocks them and tinkers
+with sys.modules so that the drivers can be loaded by unit tests, and the unit
+tests can continue to test the functionality of those drivers without the
+respective external libraries' actually being present.
+
+Any external library required by a third-party driver should be mocked here.
+Current list of mocked libraries:
+ seamicroclient
+"""
+
+import sys
+
+import mock
+
+from ironic.openstack.common import importutils
+
+
+# attempt to load the external 'seamicroclient' library, which is
+# required by the optional drivers.modules.seamicro module
+seamicroclient = importutils.try_import("seamicroclient")
+if not seamicroclient:
+ smc = mock.Mock()
+ smc.client = mock.Mock()
+ smc.exceptions = mock.Mock()
+ smc.exceptions.ClientException = Exception
+ sys.modules['seamicroclient'] = smc
+ sys.modules['seamicroclient.client'] = smc.client
+ sys.modules['seamicroclient.exceptions'] = smc.exceptions
+
+# if anything has loaded the seamicro driver yet, reload it now that
+# the external library has been mocked
+if 'ironic.drivers.modules.seamicro' in sys.modules:
+ reload(sys.modules['ironic.drivers.modules.seamicro'])