Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/module_utils/_xenserver/conftest.py

raw
1 #
2 # Copyright (c) 2019, Bojan Vitnik <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 import importlib
9 import json
10 import os
11 import sys
12 from unittest.mock import MagicMock
13
14 import pytest
15
16 from .FakeAnsibleModule import FakeAnsibleModule
17
18
19 @pytest.fixture
20 def fake_ansible_module(request):
21 """Returns fake AnsibleModule with fake module params."""
22 if hasattr(request, "param"):
23 return FakeAnsibleModule(request.param)
24 else:
25 params = {
26 "hostname": "somehost",
27 "username": "someuser",
28 "password": "somepwd",
29 "validate_certs": True,
30 }
31
32 return FakeAnsibleModule(params)
33
34
35 @pytest.fixture(autouse=True)
36 def XenAPI():
37 """Imports and returns fake XenAPI module."""
38
39 # Import of fake XenAPI module is wrapped by fixture so that it does not
40 # affect other unit tests which could potentially also use XenAPI module.
41
42 # First we use importlib.import_module() to import the module and assign
43 # it to a local symbol.
44 fake_xenapi = importlib.import_module(
45 "ansible_collections.community.general.tests.unit.plugins.module_utils._xenserver.FakeXenAPI"
46 )
47
48 # Now we populate Python module cache with imported fake module using the
49 # original module name (XenAPI). That way, any 'import XenAPI' statement
50 # will just load already imported fake module from the cache.
51 sys.modules["XenAPI"] = fake_xenapi
52
53 return fake_xenapi
54
55
56 @pytest.fixture(autouse=True)
57 def xenserver(XenAPI):
58 """Imports and returns xenserver module util."""
59
60 # Since we are wrapping fake XenAPI module inside a fixture, all modules
61 # that depend on it have to be imported inside a test function. To make
62 # this easier to handle and remove some code repetition, we wrap the import
63 # of xenserver module util with a fixture.
64 from ansible_collections.community.general.plugins.module_utils import _xenserver as xenserver
65
66 return xenserver
67
68
69 @pytest.fixture
70 def mock_xenapi_failure(XenAPI, mocker):
71 """
72 Returns mock object that raises XenAPI.Failure on any XenAPI
73 method call.
74 """
75 fake_error_msg = "Fake XAPI method call error!"
76
77 # We need to use our MagicMock based class that passes side_effect to its
78 # children because calls to xenapi methods can generate an arbitrary
79 # hierarchy of mock objects. Any such object when called should use the
80 # same side_effect as its parent mock object.
81 class MagicMockSideEffect(MagicMock):
82 def _get_child_mock(self, **kw):
83 child_mock = super()._get_child_mock(**kw)
84 child_mock.side_effect = self.side_effect
85 return child_mock
86
87 mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", new=MagicMockSideEffect(), create=True)
88 mocked_xenapi.side_effect = XenAPI.Failure(fake_error_msg)
89
90 return mocked_xenapi, fake_error_msg
91
92
93 @pytest.fixture
94 def fixture_data_from_file(request):
95 """Loads fixture data from files."""
96 if not hasattr(request, "param"):
97 return {}
98
99 fixture_path = os.path.join(os.path.dirname(__file__), "fixtures")
100 fixture_data = {}
101
102 if isinstance(request.param, str):
103 request.param = [request.param]
104
105 for fixture_name in request.param:
106 path = os.path.join(fixture_path, fixture_name)
107
108 with open(path) as f:
109 data = f.read()
110
111 try:
112 data = json.loads(data)
113 except Exception:
114 pass
115
116 fixture_data[fixture_name] = data
117
118 return fixture_data