Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/module_utils/_xenserver/FakeXenAPI.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 FAKE_API_VERSION = "1.1"
9
10
11 class Failure(Exception):
12 def __init__(self, details):
13 self.details = details
14
15 def __str__(self):
16 return str(self.details)
17
18
19 class Session:
20 def __init__(self, uri, transport=None, encoding=None, verbose=0, allow_none=1, ignore_ssl=False):
21 self.transport = transport
22 self._session = None
23 self.last_login_method = None
24 self.last_login_params = None
25 self.API_version = FAKE_API_VERSION
26
27 def _get_api_version(self):
28 return FAKE_API_VERSION
29
30 def _login(self, method, params):
31 self._session = "OpaqueRef:fake-xenapi-session-ref"
32 self.last_login_method = method
33 self.last_login_params = params
34 self.API_version = self._get_api_version()
35
36 def _logout(self):
37 self._session = None
38 self.last_login_method = None
39 self.last_login_params = None
40 self.API_version = FAKE_API_VERSION
41
42 def xenapi_request(self, methodname, params):
43 if methodname.startswith("login"):
44 self._login(methodname, params)
45 return None
46 elif methodname == "logout" or methodname == "session.logout":
47 self._logout()
48 return None
49 else:
50 # Should be patched with mocker.patch().
51 return None
52
53 def __getattr__(self, name):
54 if name == "handle":
55 return self._session
56 elif name == "xenapi":
57 # Should be patched with mocker.patch().
58 return None
59 elif name.startswith("login") or name.startswith("slave_local"):
60 return lambda *params: self._login(name, params)
61 elif name == "logout":
62 return self._logout
63
64
65 def xapi_local():
66 return Session("http://_var_lib_xcp_xapi/")