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
This commit is contained in:
Jianghua Wang 2016-02-24 07:02:35 +00:00
parent d51c5670d8
commit 2aa81e9ef6
4 changed files with 100 additions and 0 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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