Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/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 sys
10
11 import pytest
12
13 from .FakeAnsibleModule import FakeAnsibleModule
14
15
16 @pytest.fixture
17 def fake_ansible_module(request):
18 """Returns fake AnsibleModule with fake module params."""
19 if hasattr(request, "param"):
20 return FakeAnsibleModule(request.param)
21 else:
22 params = {
23 "hostname": "somehost",
24 "username": "someuser",
25 "password": "somepwd",
26 "validate_certs": True,
27 }
28
29 return FakeAnsibleModule(params)
30
31
32 @pytest.fixture(autouse=True)
33 def XenAPI():
34 """Imports and returns fake XenAPI module."""
35
36 # Import of fake XenAPI module is wrapped by fixture so that it does not
37 # affect other unit tests which could potentially also use XenAPI module.
38
39 # First we use importlib.import_module() to import the module and assign
40 # it to a local symbol.
41 fake_xenapi = importlib.import_module("ansible_collections.community.general.tests.unit.plugins.modules.FakeXenAPI")
42
43 # Now we populate Python module cache with imported fake module using the
44 # original module name (XenAPI). That way, any 'import XenAPI' statement
45 # will just load already imported fake module from the cache.
46 sys.modules["XenAPI"] = fake_xenapi
47
48 return fake_xenapi
49
50
51 @pytest.fixture
52 def xenserver_guest_info(XenAPI):
53 """Imports and returns xenserver_guest_info module."""
54
55 # Since we are wrapping fake XenAPI module inside a fixture, all modules
56 # that depend on it have to be imported inside a test function. To make
57 # this easier to handle and remove some code repetition, we wrap the import
58 # of xenserver_guest_info module with a fixture.
59 from ansible_collections.community.general.plugins.modules import xenserver_guest_info
60
61 return xenserver_guest_info
62
63
64 @pytest.fixture
65 def xenserver_guest_powerstate(XenAPI):
66 """Imports and returns xenserver_guest_powerstate module."""
67
68 # Since we are wrapping fake XenAPI module inside a fixture, all modules
69 # that depend on it have to be imported inside a test function. To make
70 # this easier to handle and remove some code repetition, we wrap the import
71 # of xenserver_guest_powerstate module with a fixture.
72 from ansible_collections.community.general.plugins.modules import xenserver_guest_powerstate
73
74 return xenserver_guest_powerstate