File: plugin_test.py

package info (click to toggle)
python-os-xenapi 0.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,012 kB
  • sloc: python: 8,137; sh: 2,154; makefile: 45
file content (68 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright (c) 2016 Citrix Systems
# 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 os
import sys

import mock

from os_xenapi.client import session
from os_xenapi.tests import base

# both XenAPI and XenAPIPlugin may not exist
# in unit test environment.
sys.modules['XenAPI'] = mock.Mock()
sys.modules['XenAPIPlugin'] = mock.Mock()


class PluginTestBase(base.TestCase):
    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):
        # utility 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,
                                "../../../dom0/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)