From 2aa81e9ef6d04f2fdf1c69e16ceeef156cefedfa Mon Sep 17 00:00:00 2001 From: Jianghua Wang Date: Wed, 24 Feb 2016 07:02:35 +0000 Subject: [PATCH] XenAPI: introduce unit test for XenAPI plugins This is the first patch set to introduce unit test for XenAPI plugins. It contains the common part depended by other plugin tests. In order to make this first patch set to be as simple as possible, it only covers the test framework and the unit test for a simple plugin. Change-Id: Iece439661cc4337e826d0eb15b438d2716214fb5 --- .../unit/virt/xenapi/plugins/__init__.py | 0 .../unit/virt/xenapi/plugins/plugin_test.py | 68 +++++++++++++++++++ .../plugins/test_nova_plugin_version.py | 28 ++++++++ tests-py3.txt | 4 ++ 4 files changed, 100 insertions(+) create mode 100644 nova/tests/unit/virt/xenapi/plugins/__init__.py create mode 100644 nova/tests/unit/virt/xenapi/plugins/plugin_test.py create mode 100644 nova/tests/unit/virt/xenapi/plugins/test_nova_plugin_version.py diff --git a/nova/tests/unit/virt/xenapi/plugins/__init__.py b/nova/tests/unit/virt/xenapi/plugins/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/nova/tests/unit/virt/xenapi/plugins/plugin_test.py b/nova/tests/unit/virt/xenapi/plugins/plugin_test.py new file mode 100644 index 000000000000..d03dcf520d8b --- /dev/null +++ b/nova/tests/unit/virt/xenapi/plugins/plugin_test.py @@ -0,0 +1,68 @@ +# Copyright (c) 2016 OpenStack Foundation +# 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. + +import imp +import mock +import os +import sys + +from nova import test +from nova.virt.xenapi.client import session + + +# both XenAPI and XenAPIPlugin may not exist +# in unit test environment. +sys.modules['XenAPI'] = mock.Mock() +sys.modules['XenAPIPlugin'] = mock.Mock() + + +class PluginTestBase(test.NoDBTestCase): + def setUp(self): + super(PluginTestBase, self).setUp() + self.session = mock.Mock() + session.apply_session_helpers(self.session) + + def mock_patch_object(self, target, attribute, return_val=None): + # utilility function to mock object's attribute + patcher = mock.patch.object(target, attribute, return_value=return_val) + mock_one = patcher.start() + self.addCleanup(patcher.stop) + return mock_one + + def _get_plugin_path(self): + current_path = os.path.realpath(__file__) + rel_path = os.path.join(current_path, + "../../../../../../../plugins/xenserver/xenapi/etc/xapi.d/plugins") + plugin_path = os.path.abspath(rel_path) + return plugin_path + + def load_plugin(self, file_name): + # XAPI plugins run in a py24 environment and may be not compatible with + # py34's syntax. In order to prevent unit test scanning the source file + # under py34 environment, the plugins will be imported with this + # function at run time. + + plugin_path = self._get_plugin_path() + + # add plugin path into search path. + if plugin_path not in sys.path: + sys.path.append(plugin_path) + + # be sure not to create c files next to the plugins + sys.dont_write_bytecode = True + + name = file_name.split('.')[0] + path = os.path.join(plugin_path, file_name) + return imp.load_source(name, path) diff --git a/nova/tests/unit/virt/xenapi/plugins/test_nova_plugin_version.py b/nova/tests/unit/virt/xenapi/plugins/test_nova_plugin_version.py new file mode 100644 index 000000000000..4abc8110696a --- /dev/null +++ b/nova/tests/unit/virt/xenapi/plugins/test_nova_plugin_version.py @@ -0,0 +1,28 @@ +# Copyright (c) 2016 OpenStack Foundation +# 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. + +from nova.tests.unit.virt.xenapi.plugins import plugin_test + + +class NovaPluginVersion(plugin_test.PluginTestBase): + def setUp(self): + super(NovaPluginVersion, self).setUp() + self.nova_plugin_version = self.load_plugin("nova_plugin_version") + + def test_nova_plugin_version(self): + session = 'fake_session' + expected_value = self.nova_plugin_version.PLUGIN_VERSION + return_value = self.nova_plugin_version.get_version(session) + self.assertEqual(expected_value, return_value) diff --git a/tests-py3.txt b/tests-py3.txt index ae7537c413fa..4828d9535cec 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -203,3 +203,7 @@ nova.tests.unit.virt.vmwareapi.test_configdrive.ConfigDriveTestCase nova.tests.unit.virt.vmwareapi.test_driver_api.VMwareAPIVMTestCase nova.tests.unit.virt.xenapi.test_vmops.BootableTestCase nova.tests.unit.virt.xenapi.test_vmops.SpawnTestCase + +# The XenAPI plugins run in a Python 2.4 environment, so avoid attempting +# to run their unit tests in a Python 3 environment. +nova.tests.unit.virt.xenapi.plugins