mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-14 16:05:04 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -17,9 +17,7 @@ class Failure(Exception):
|
|||
|
||||
|
||||
class Session:
|
||||
def __init__(self, uri, transport=None, encoding=None, verbose=0,
|
||||
allow_none=1, ignore_ssl=False):
|
||||
|
||||
def __init__(self, uri, transport=None, encoding=None, verbose=0, allow_none=1, ignore_ssl=False):
|
||||
self.transport = transport
|
||||
self._session = None
|
||||
self.last_login_method = None
|
||||
|
|
@ -42,10 +40,10 @@ class Session:
|
|||
self.API_version = FAKE_API_VERSION
|
||||
|
||||
def xenapi_request(self, methodname, params):
|
||||
if methodname.startswith('login'):
|
||||
if methodname.startswith("login"):
|
||||
self._login(methodname, params)
|
||||
return None
|
||||
elif methodname == 'logout' or methodname == 'session.logout':
|
||||
elif methodname == "logout" or methodname == "session.logout":
|
||||
self._logout()
|
||||
return None
|
||||
else:
|
||||
|
|
@ -53,14 +51,14 @@ class Session:
|
|||
return None
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'handle':
|
||||
if name == "handle":
|
||||
return self._session
|
||||
elif name == 'xenapi':
|
||||
elif name == "xenapi":
|
||||
# Should be patched with mocker.patch().
|
||||
return None
|
||||
elif name.startswith('login') or name.startswith('slave_local'):
|
||||
elif name.startswith("login") or name.startswith("slave_local"):
|
||||
return lambda *params: self._login(name, params)
|
||||
elif name == 'logout':
|
||||
elif name == "logout":
|
||||
return self._logout
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -13,12 +13,12 @@ def fake_xenapi_ref(xenapi_class):
|
|||
testcase_bad_xenapi_refs = {
|
||||
"params": [
|
||||
None,
|
||||
'',
|
||||
'OpaqueRef:NULL',
|
||||
"",
|
||||
"OpaqueRef:NULL",
|
||||
],
|
||||
"ids": [
|
||||
'none',
|
||||
'empty',
|
||||
'ref-null',
|
||||
"none",
|
||||
"empty",
|
||||
"ref-null",
|
||||
],
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ from .FakeAnsibleModule import FakeAnsibleModule
|
|||
@pytest.fixture
|
||||
def fake_ansible_module(request):
|
||||
"""Returns fake AnsibleModule with fake module params."""
|
||||
if hasattr(request, 'param'):
|
||||
if hasattr(request, "param"):
|
||||
return FakeAnsibleModule(request.param)
|
||||
else:
|
||||
params = {
|
||||
|
|
@ -42,12 +42,14 @@ def XenAPI():
|
|||
|
||||
# First we use importlib.import_module() to import the module and assign
|
||||
# it to a local symbol.
|
||||
fake_xenapi = importlib.import_module('ansible_collections.community.general.tests.unit.plugins.module_utils.xenserver.FakeXenAPI')
|
||||
fake_xenapi = importlib.import_module(
|
||||
"ansible_collections.community.general.tests.unit.plugins.module_utils.xenserver.FakeXenAPI"
|
||||
)
|
||||
|
||||
# Now we populate Python module cache with imported fake module using the
|
||||
# original module name (XenAPI). That way, any 'import XenAPI' statement
|
||||
# will just load already imported fake module from the cache.
|
||||
sys.modules['XenAPI'] = fake_xenapi
|
||||
sys.modules["XenAPI"] = fake_xenapi
|
||||
|
||||
return fake_xenapi
|
||||
|
||||
|
|
@ -83,7 +85,7 @@ def mock_xenapi_failure(XenAPI, mocker):
|
|||
child_mock.side_effect = self.side_effect
|
||||
return child_mock
|
||||
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', new=MagicMockSideEffect(), create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", new=MagicMockSideEffect(), create=True)
|
||||
mocked_xenapi.side_effect = XenAPI.Failure(fake_error_msg)
|
||||
|
||||
return mocked_xenapi, fake_error_msg
|
||||
|
|
@ -92,10 +94,10 @@ def mock_xenapi_failure(XenAPI, mocker):
|
|||
@pytest.fixture
|
||||
def fixture_data_from_file(request):
|
||||
"""Loads fixture data from files."""
|
||||
if not hasattr(request, 'param'):
|
||||
if not hasattr(request, "param"):
|
||||
return {}
|
||||
|
||||
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
|
||||
fixture_path = os.path.join(os.path.dirname(__file__), "fixtures")
|
||||
fixture_data = {}
|
||||
|
||||
if isinstance(request.param, str):
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ testcase_gather_vm_params_and_facts = {
|
|||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('vm_ref', testcase_bad_xenapi_refs['params'], ids=testcase_bad_xenapi_refs['ids']) # type: ignore
|
||||
@pytest.mark.parametrize("vm_ref", testcase_bad_xenapi_refs["params"], ids=testcase_bad_xenapi_refs["ids"]) # type: ignore
|
||||
def test_gather_vm_params_bad_vm_ref(fake_ansible_module, xenserver, vm_ref):
|
||||
"""Tests return of empty dict on bad vm_ref."""
|
||||
assert xenserver.gather_vm_params(fake_ansible_module, vm_ref) == {}
|
||||
|
|
@ -37,13 +37,15 @@ def test_gather_vm_facts_no_vm_params(fake_ansible_module, xenserver):
|
|||
assert xenserver.gather_vm_facts(fake_ansible_module, {}) == {}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fixture_data_from_file',
|
||||
testcase_gather_vm_params_and_facts['params'], # type: ignore
|
||||
ids=testcase_gather_vm_params_and_facts['ids'], # type: ignore
|
||||
indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
"fixture_data_from_file",
|
||||
testcase_gather_vm_params_and_facts["params"], # type: ignore
|
||||
ids=testcase_gather_vm_params_and_facts["ids"], # type: ignore
|
||||
indirect=True,
|
||||
)
|
||||
def test_gather_vm_params_and_facts(mocker, fake_ansible_module, XenAPI, xenserver, fixture_data_from_file):
|
||||
"""Tests proper parsing of VM parameters and facts."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
if "params" in list(fixture_data_from_file.keys())[0]:
|
||||
params_file = list(fixture_data_from_file.keys())[0]
|
||||
|
|
@ -53,21 +55,29 @@ def test_gather_vm_params_and_facts(mocker, fake_ansible_module, XenAPI, xenserv
|
|||
facts_file = list(fixture_data_from_file.keys())[0]
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['VM'][obj_ref],
|
||||
"VM_metrics.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['VM_metrics'][obj_ref],
|
||||
"VM_guest_metrics.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['VM_guest_metrics'][obj_ref],
|
||||
"VBD.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['VBD'][obj_ref],
|
||||
"VDI.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['VDI'][obj_ref],
|
||||
"SR.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['SR'][obj_ref],
|
||||
"VIF.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['VIF'][obj_ref],
|
||||
"network.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['network'][obj_ref],
|
||||
"host.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]['host'][obj_ref],
|
||||
"VM.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["VM"][obj_ref],
|
||||
"VM_metrics.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["VM_metrics"][obj_ref],
|
||||
"VM_guest_metrics.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file][
|
||||
"VM_guest_metrics"
|
||||
][obj_ref],
|
||||
"VBD.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["VBD"][obj_ref],
|
||||
"VDI.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["VDI"][obj_ref],
|
||||
"SR.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["SR"][obj_ref],
|
||||
"VIF.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["VIF"][obj_ref],
|
||||
"network.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["network"][obj_ref],
|
||||
"host.get_record.side_effect": lambda obj_ref: fixture_data_from_file[params_file]["host"][obj_ref],
|
||||
}
|
||||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
mocker.patch('ansible_collections.community.general.plugins.module_utils.xenserver.get_xenserver_version', return_value=[7, 2, 0])
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.module_utils.xenserver.get_xenserver_version",
|
||||
return_value=[7, 2, 0],
|
||||
)
|
||||
|
||||
vm_ref = list(fixture_data_from_file[params_file]['VM'].keys())[0]
|
||||
vm_ref = list(fixture_data_from_file[params_file]["VM"].keys())[0]
|
||||
|
||||
assert xenserver.gather_vm_facts(fake_ansible_module, xenserver.gather_vm_params(fake_ansible_module, vm_ref)) == fixture_data_from_file[facts_file]
|
||||
assert (
|
||||
xenserver.gather_vm_facts(fake_ansible_module, xenserver.gather_vm_params(fake_ansible_module, vm_ref))
|
||||
== fixture_data_from_file[facts_file]
|
||||
)
|
||||
|
|
|
|||
|
|
@ -14,59 +14,67 @@ from .common import fake_xenapi_ref
|
|||
|
||||
def test_get_object_ref_xenapi_failure(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests catching of XenAPI failures."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi_request', side_effect=XenAPI.Failure('Fake XAPI method call error!'))
|
||||
mocked_xenapi = mocker.patch.object(
|
||||
XenAPI.Session, "xenapi_request", side_effect=XenAPI.Failure("Fake XAPI method call error!")
|
||||
)
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name")
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "XAPI ERROR: Fake XAPI method call error!"
|
||||
assert exc_info.value.kwargs["msg"] == "XAPI ERROR: Fake XAPI method call error!"
|
||||
|
||||
|
||||
def test_get_object_ref_bad_uuid_and_name(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests failure on bad object uuid and/or name."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi_request')
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi_request")
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, None, msg_prefix="Test: ")
|
||||
|
||||
mocked_xenapi.xenapi_request.assert_not_called()
|
||||
assert exc_info.value.kwargs['msg'] == "Test: no valid name or UUID supplied for VM!"
|
||||
assert exc_info.value.kwargs["msg"] == "Test: no valid name or UUID supplied for VM!"
|
||||
|
||||
|
||||
def test_get_object_ref_uuid_not_found(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests when object is not found by uuid."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi_request', side_effect=XenAPI.Failure('Fake XAPI not found error!'))
|
||||
mocked_xenapi = mocker.patch.object(
|
||||
XenAPI.Session, "xenapi_request", side_effect=XenAPI.Failure("Fake XAPI not found error!")
|
||||
)
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name", uuid="fake-uuid", msg_prefix="Test: ")
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "Test: VM with UUID 'fake-uuid' not found!"
|
||||
assert xenserver.get_object_ref(fake_ansible_module, "name", uuid="fake-uuid", fail=False, msg_prefix="Test: ") is None
|
||||
assert exc_info.value.kwargs["msg"] == "Test: VM with UUID 'fake-uuid' not found!"
|
||||
assert (
|
||||
xenserver.get_object_ref(fake_ansible_module, "name", uuid="fake-uuid", fail=False, msg_prefix="Test: ") is None
|
||||
)
|
||||
|
||||
|
||||
def test_get_object_ref_name_not_found(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests when object is not found by name."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi_request', return_value=[])
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi_request", return_value=[])
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name", msg_prefix="Test: ")
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "Test: VM with name 'name' not found!"
|
||||
assert exc_info.value.kwargs["msg"] == "Test: VM with name 'name' not found!"
|
||||
assert xenserver.get_object_ref(fake_ansible_module, "name", fail=False, msg_prefix="Test: ") is None
|
||||
|
||||
|
||||
def test_get_object_ref_name_multiple_found(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests when multiple objects are found by name."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi_request', return_value=[fake_xenapi_ref('VM'), fake_xenapi_ref('VM')])
|
||||
mocked_xenapi = mocker.patch.object(
|
||||
XenAPI.Session, "xenapi_request", return_value=[fake_xenapi_ref("VM"), fake_xenapi_ref("VM")]
|
||||
)
|
||||
|
||||
error_msg = "Test: multiple VMs with name 'name' found! Please use UUID."
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name", msg_prefix="Test: ")
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == error_msg
|
||||
assert exc_info.value.kwargs["msg"] == error_msg
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name", fail=False, msg_prefix="Test: ")
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == error_msg
|
||||
assert exc_info.value.kwargs["msg"] == error_msg
|
||||
|
|
|
|||
|
|
@ -11,171 +11,179 @@ import pytest
|
|||
from ansible.module_utils.common.network import is_mac
|
||||
|
||||
testcase_is_valid_mac_addr = [
|
||||
('A4-23-8D-F8-C9-E5', True),
|
||||
('35:71:F4:11:0B:D8', True),
|
||||
('b3-bd-20-59-0c-cf', True),
|
||||
('32:61:ca:65:f1:f4', True),
|
||||
('asdf', False),
|
||||
('A4-23-8D-G8-C9-E5', False),
|
||||
('A4-3-8D-F8-C9-E5', False),
|
||||
('A4-23-88D-F8-C9-E5', False),
|
||||
('A4-23-8D-F8-C9_E5', False),
|
||||
('A4-23--8D-F8-C9-E5', False),
|
||||
("A4-23-8D-F8-C9-E5", True),
|
||||
("35:71:F4:11:0B:D8", True),
|
||||
("b3-bd-20-59-0c-cf", True),
|
||||
("32:61:ca:65:f1:f4", True),
|
||||
("asdf", False),
|
||||
("A4-23-8D-G8-C9-E5", False),
|
||||
("A4-3-8D-F8-C9-E5", False),
|
||||
("A4-23-88D-F8-C9-E5", False),
|
||||
("A4-23-8D-F8-C9_E5", False),
|
||||
("A4-23--8D-F8-C9-E5", False),
|
||||
]
|
||||
|
||||
testcase_is_valid_ip_addr = [
|
||||
('0.0.0.0', True),
|
||||
('10.0.0.1', True),
|
||||
('192.168.0.1', True),
|
||||
('255.255.255.255', True),
|
||||
('asdf', False),
|
||||
('a.b.c.d', False),
|
||||
('345.345.345.345', False),
|
||||
('-10.0.0.1', False),
|
||||
("0.0.0.0", True),
|
||||
("10.0.0.1", True),
|
||||
("192.168.0.1", True),
|
||||
("255.255.255.255", True),
|
||||
("asdf", False),
|
||||
("a.b.c.d", False),
|
||||
("345.345.345.345", False),
|
||||
("-10.0.0.1", False),
|
||||
]
|
||||
|
||||
testcase_is_valid_ip_netmask = [
|
||||
('240.0.0.0', True),
|
||||
('255.224.0.0', True),
|
||||
('255.255.248.0', True),
|
||||
('255.255.255.255', True),
|
||||
('asdf', False),
|
||||
('a.b.c.d', False),
|
||||
('192.168.0.1', False),
|
||||
('255.0.248.0', False),
|
||||
("240.0.0.0", True),
|
||||
("255.224.0.0", True),
|
||||
("255.255.248.0", True),
|
||||
("255.255.255.255", True),
|
||||
("asdf", False),
|
||||
("a.b.c.d", False),
|
||||
("192.168.0.1", False),
|
||||
("255.0.248.0", False),
|
||||
]
|
||||
|
||||
testcase_is_valid_ip_prefix = [
|
||||
('0', True),
|
||||
('16', True),
|
||||
('24', True),
|
||||
('32', True),
|
||||
('asdf', False),
|
||||
('-10', False),
|
||||
('60', False),
|
||||
('60s', False),
|
||||
("0", True),
|
||||
("16", True),
|
||||
("24", True),
|
||||
("32", True),
|
||||
("asdf", False),
|
||||
("-10", False),
|
||||
("60", False),
|
||||
("60s", False),
|
||||
]
|
||||
|
||||
testcase_ip_prefix_to_netmask = {
|
||||
"params": [
|
||||
('0', '0.0.0.0'),
|
||||
('8', '255.0.0.0'),
|
||||
('11', '255.224.0.0'),
|
||||
('16', '255.255.0.0'),
|
||||
('21', '255.255.248.0'),
|
||||
('24', '255.255.255.0'),
|
||||
('26', '255.255.255.192'),
|
||||
('32', '255.255.255.255'),
|
||||
('a', ''),
|
||||
('60', ''),
|
||||
("0", "0.0.0.0"),
|
||||
("8", "255.0.0.0"),
|
||||
("11", "255.224.0.0"),
|
||||
("16", "255.255.0.0"),
|
||||
("21", "255.255.248.0"),
|
||||
("24", "255.255.255.0"),
|
||||
("26", "255.255.255.192"),
|
||||
("32", "255.255.255.255"),
|
||||
("a", ""),
|
||||
("60", ""),
|
||||
],
|
||||
"ids": [
|
||||
'0',
|
||||
'8',
|
||||
'11',
|
||||
'16',
|
||||
'21',
|
||||
'24',
|
||||
'26',
|
||||
'32',
|
||||
'a',
|
||||
'60',
|
||||
"0",
|
||||
"8",
|
||||
"11",
|
||||
"16",
|
||||
"21",
|
||||
"24",
|
||||
"26",
|
||||
"32",
|
||||
"a",
|
||||
"60",
|
||||
],
|
||||
}
|
||||
|
||||
testcase_ip_netmask_to_prefix = {
|
||||
"params": [
|
||||
('0.0.0.0', '0'),
|
||||
('255.0.0.0', '8'),
|
||||
('255.224.0.0', '11'),
|
||||
('255.255.0.0', '16'),
|
||||
('255.255.248.0', '21'),
|
||||
('255.255.255.0', '24'),
|
||||
('255.255.255.192', '26'),
|
||||
('255.255.255.255', '32'),
|
||||
('a', ''),
|
||||
('60', ''),
|
||||
("0.0.0.0", "0"),
|
||||
("255.0.0.0", "8"),
|
||||
("255.224.0.0", "11"),
|
||||
("255.255.0.0", "16"),
|
||||
("255.255.248.0", "21"),
|
||||
("255.255.255.0", "24"),
|
||||
("255.255.255.192", "26"),
|
||||
("255.255.255.255", "32"),
|
||||
("a", ""),
|
||||
("60", ""),
|
||||
],
|
||||
"ids": [
|
||||
'0.0.0.0',
|
||||
'255.0.0.0',
|
||||
'255.224.0.0',
|
||||
'255.255.0.0',
|
||||
'255.255.248.0',
|
||||
'255.255.255.0',
|
||||
'255.255.255.192',
|
||||
'255.255.255.255',
|
||||
'a',
|
||||
'60',
|
||||
"0.0.0.0",
|
||||
"255.0.0.0",
|
||||
"255.224.0.0",
|
||||
"255.255.0.0",
|
||||
"255.255.248.0",
|
||||
"255.255.255.0",
|
||||
"255.255.255.192",
|
||||
"255.255.255.255",
|
||||
"a",
|
||||
"60",
|
||||
],
|
||||
}
|
||||
|
||||
testcase_is_valid_ip6_addr = [
|
||||
('::1', True),
|
||||
('2001:DB8:0:0:8:800:200C:417A', True),
|
||||
('2001:DB8::8:800:200C:417A', True),
|
||||
('FF01::101', True),
|
||||
('asdf', False),
|
||||
('2001:DB8:0:0:8:800:200C:417A:221', False),
|
||||
('FF01::101::2', False),
|
||||
('2001:db8:85a3::8a2e:370k:7334', False),
|
||||
("::1", True),
|
||||
("2001:DB8:0:0:8:800:200C:417A", True),
|
||||
("2001:DB8::8:800:200C:417A", True),
|
||||
("FF01::101", True),
|
||||
("asdf", False),
|
||||
("2001:DB8:0:0:8:800:200C:417A:221", False),
|
||||
("FF01::101::2", False),
|
||||
("2001:db8:85a3::8a2e:370k:7334", False),
|
||||
]
|
||||
|
||||
testcase_is_valid_ip6_prefix = [
|
||||
('0', True),
|
||||
('56', True),
|
||||
('78', True),
|
||||
('128', True),
|
||||
('asdf', False),
|
||||
('-10', False),
|
||||
('345', False),
|
||||
('60s', False),
|
||||
("0", True),
|
||||
("56", True),
|
||||
("78", True),
|
||||
("128", True),
|
||||
("asdf", False),
|
||||
("-10", False),
|
||||
("345", False),
|
||||
("60s", False),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mac_addr, result', testcase_is_valid_mac_addr)
|
||||
@pytest.mark.parametrize("mac_addr, result", testcase_is_valid_mac_addr)
|
||||
def test_is_valid_mac_addr(xenserver, mac_addr, result):
|
||||
"""Tests against examples of valid and invalid mac addresses."""
|
||||
assert is_mac(mac_addr) is result
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ip_addr, result', testcase_is_valid_ip_addr)
|
||||
@pytest.mark.parametrize("ip_addr, result", testcase_is_valid_ip_addr)
|
||||
def test_is_valid_ip_addr(xenserver, ip_addr, result):
|
||||
"""Tests against examples of valid and invalid ip addresses."""
|
||||
assert xenserver.is_valid_ip_addr(ip_addr) is result
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ip_netmask, result', testcase_is_valid_ip_netmask)
|
||||
@pytest.mark.parametrize("ip_netmask, result", testcase_is_valid_ip_netmask)
|
||||
def test_is_valid_ip_netmask(xenserver, ip_netmask, result):
|
||||
"""Tests against examples of valid and invalid ip netmasks."""
|
||||
assert xenserver.is_valid_ip_netmask(ip_netmask) is result
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ip_prefix, result', testcase_is_valid_ip_prefix)
|
||||
@pytest.mark.parametrize("ip_prefix, result", testcase_is_valid_ip_prefix)
|
||||
def test_is_valid_ip_prefix(xenserver, ip_prefix, result):
|
||||
"""Tests against examples of valid and invalid ip prefixes."""
|
||||
assert xenserver.is_valid_ip_prefix(ip_prefix) is result
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ip_prefix, ip_netmask', testcase_ip_prefix_to_netmask['params'], ids=testcase_ip_prefix_to_netmask['ids']) # type: ignore
|
||||
@pytest.mark.parametrize(
|
||||
"ip_prefix, ip_netmask",
|
||||
testcase_ip_prefix_to_netmask["params"], # type: ignore
|
||||
ids=testcase_ip_prefix_to_netmask["ids"], # type: ignore
|
||||
)
|
||||
def test_ip_prefix_to_netmask(xenserver, ip_prefix, ip_netmask):
|
||||
"""Tests ip prefix to netmask conversion."""
|
||||
assert xenserver.ip_prefix_to_netmask(ip_prefix) == ip_netmask
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ip_netmask, ip_prefix', testcase_ip_netmask_to_prefix['params'], ids=testcase_ip_netmask_to_prefix['ids']) # type: ignore
|
||||
@pytest.mark.parametrize(
|
||||
"ip_netmask, ip_prefix",
|
||||
testcase_ip_netmask_to_prefix["params"], # type: ignore
|
||||
ids=testcase_ip_netmask_to_prefix["ids"], # type: ignore
|
||||
)
|
||||
def test_ip_netmask_to_prefix(xenserver, ip_netmask, ip_prefix):
|
||||
"""Tests ip netmask to prefix conversion."""
|
||||
assert xenserver.ip_netmask_to_prefix(ip_netmask) == ip_prefix
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ip6_addr, result', testcase_is_valid_ip6_addr)
|
||||
@pytest.mark.parametrize("ip6_addr, result", testcase_is_valid_ip6_addr)
|
||||
def test_is_valid_ip6_addr(xenserver, ip6_addr, result):
|
||||
"""Tests against examples of valid and invalid ip6 addresses."""
|
||||
assert xenserver.is_valid_ip6_addr(ip6_addr) is result
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ip6_prefix, result', testcase_is_valid_ip6_prefix)
|
||||
@pytest.mark.parametrize("ip6_prefix, result", testcase_is_valid_ip6_prefix)
|
||||
def test_is_valid_ip6_prefix(xenserver, ip6_prefix, result):
|
||||
"""Tests against examples of valid and invalid ip6 prefixes."""
|
||||
assert xenserver.is_valid_ip6_prefix(ip6_prefix) is result
|
||||
|
|
|
|||
|
|
@ -14,16 +14,16 @@ from .common import fake_xenapi_ref, testcase_bad_xenapi_refs
|
|||
|
||||
testcase_set_vm_power_state_bad_transitions = {
|
||||
"params": [
|
||||
('restarted', 'Halted', "Cannot restart VM in state 'poweredoff'!"),
|
||||
('restarted', 'Suspended', "Cannot restart VM in state 'suspended'!"),
|
||||
('suspended', 'Halted', "Cannot suspend VM in state 'poweredoff'!"),
|
||||
('suspended', 'Paused', "Cannot suspend VM in state 'paused'!"),
|
||||
('shutdownguest', 'Halted', "Cannot shutdown guest when VM is in state 'poweredoff'!"),
|
||||
('shutdownguest', 'Suspended', "Cannot shutdown guest when VM is in state 'suspended'!"),
|
||||
('shutdownguest', 'Paused', "Cannot shutdown guest when VM is in state 'paused'!"),
|
||||
('rebootguest', 'Halted', "Cannot reboot guest when VM is in state 'poweredoff'!"),
|
||||
('rebootguest', 'Suspended', "Cannot reboot guest when VM is in state 'suspended'!"),
|
||||
('rebootguest', 'Paused', "Cannot reboot guest when VM is in state 'paused'!"),
|
||||
("restarted", "Halted", "Cannot restart VM in state 'poweredoff'!"),
|
||||
("restarted", "Suspended", "Cannot restart VM in state 'suspended'!"),
|
||||
("suspended", "Halted", "Cannot suspend VM in state 'poweredoff'!"),
|
||||
("suspended", "Paused", "Cannot suspend VM in state 'paused'!"),
|
||||
("shutdownguest", "Halted", "Cannot shutdown guest when VM is in state 'poweredoff'!"),
|
||||
("shutdownguest", "Suspended", "Cannot shutdown guest when VM is in state 'suspended'!"),
|
||||
("shutdownguest", "Paused", "Cannot shutdown guest when VM is in state 'paused'!"),
|
||||
("rebootguest", "Halted", "Cannot reboot guest when VM is in state 'poweredoff'!"),
|
||||
("rebootguest", "Suspended", "Cannot reboot guest when VM is in state 'suspended'!"),
|
||||
("rebootguest", "Paused", "Cannot reboot guest when VM is in state 'paused'!"),
|
||||
],
|
||||
"ids": [
|
||||
"poweredoff->restarted",
|
||||
|
|
@ -41,8 +41,8 @@ testcase_set_vm_power_state_bad_transitions = {
|
|||
|
||||
testcase_set_vm_power_state_task_timeout = {
|
||||
"params": [
|
||||
('shutdownguest', "Guest shutdown task failed: 'timeout'!"),
|
||||
('rebootguest', "Guest reboot task failed: 'timeout'!"),
|
||||
("shutdownguest", "Guest shutdown task failed: 'timeout'!"),
|
||||
("rebootguest", "Guest reboot task failed: 'timeout'!"),
|
||||
],
|
||||
"ids": [
|
||||
"shutdownguest-timeout",
|
||||
|
|
@ -52,16 +52,16 @@ testcase_set_vm_power_state_task_timeout = {
|
|||
|
||||
testcase_set_vm_power_state_no_transitions = {
|
||||
"params": [
|
||||
('poweredon', "Running"),
|
||||
('Poweredon', "Running"),
|
||||
('powered-on', "Running"),
|
||||
('Powered_on', "Running"),
|
||||
('poweredoff', "Halted"),
|
||||
('Poweredoff', "Halted"),
|
||||
('powered-off', "Halted"),
|
||||
('powered_off', "Halted"),
|
||||
('suspended', "Suspended"),
|
||||
('Suspended', "Suspended"),
|
||||
("poweredon", "Running"),
|
||||
("Poweredon", "Running"),
|
||||
("powered-on", "Running"),
|
||||
("Powered_on", "Running"),
|
||||
("poweredoff", "Halted"),
|
||||
("Poweredoff", "Halted"),
|
||||
("powered-off", "Halted"),
|
||||
("powered_off", "Halted"),
|
||||
("suspended", "Suspended"),
|
||||
("Suspended", "Suspended"),
|
||||
],
|
||||
"ids": [
|
||||
"poweredon",
|
||||
|
|
@ -79,44 +79,44 @@ testcase_set_vm_power_state_no_transitions = {
|
|||
|
||||
testcase_set_vm_power_state_transitions = {
|
||||
"params": [
|
||||
('poweredon', 'Halted', 'running', 'VM.start'),
|
||||
('Poweredon', 'Halted', 'running', 'VM.start'),
|
||||
('powered-on', 'Halted', 'running', 'VM.start'),
|
||||
('Powered_on', 'Halted', 'running', 'VM.start'),
|
||||
('poweredon', 'Suspended', 'running', 'VM.resume'),
|
||||
('Poweredon', 'Suspended', 'running', 'VM.resume'),
|
||||
('powered-on', 'Suspended', 'running', 'VM.resume'),
|
||||
('Powered_on', 'Suspended', 'running', 'VM.resume'),
|
||||
('poweredon', 'Paused', 'running', 'VM.unpause'),
|
||||
('Poweredon', 'Paused', 'running', 'VM.unpause'),
|
||||
('powered-on', 'Paused', 'running', 'VM.unpause'),
|
||||
('Powered_on', 'Paused', 'running', 'VM.unpause'),
|
||||
('poweredoff', 'Running', 'halted', 'VM.hard_shutdown'),
|
||||
('Poweredoff', 'Running', 'halted', 'VM.hard_shutdown'),
|
||||
('powered-off', 'Running', 'halted', 'VM.hard_shutdown'),
|
||||
('powered_off', 'Running', 'halted', 'VM.hard_shutdown'),
|
||||
('poweredoff', 'Suspended', 'halted', 'VM.hard_shutdown'),
|
||||
('Poweredoff', 'Suspended', 'halted', 'VM.hard_shutdown'),
|
||||
('powered-off', 'Suspended', 'halted', 'VM.hard_shutdown'),
|
||||
('powered_off', 'Suspended', 'halted', 'VM.hard_shutdown'),
|
||||
('poweredoff', 'Paused', 'halted', 'VM.hard_shutdown'),
|
||||
('Poweredoff', 'Paused', 'halted', 'VM.hard_shutdown'),
|
||||
('powered-off', 'Paused', 'halted', 'VM.hard_shutdown'),
|
||||
('powered_off', 'Paused', 'halted', 'VM.hard_shutdown'),
|
||||
('restarted', 'Running', 'running', 'VM.hard_reboot'),
|
||||
('Restarted', 'Running', 'running', 'VM.hard_reboot'),
|
||||
('restarted', 'Paused', 'running', 'VM.hard_reboot'),
|
||||
('Restarted', 'Paused', 'running', 'VM.hard_reboot'),
|
||||
('suspended', 'Running', 'suspended', 'VM.suspend'),
|
||||
('Suspended', 'Running', 'suspended', 'VM.suspend'),
|
||||
('shutdownguest', 'Running', 'halted', 'VM.clean_shutdown'),
|
||||
('Shutdownguest', 'Running', 'halted', 'VM.clean_shutdown'),
|
||||
('shutdown-guest', 'Running', 'halted', 'VM.clean_shutdown'),
|
||||
('shutdown_guest', 'Running', 'halted', 'VM.clean_shutdown'),
|
||||
('rebootguest', 'Running', 'running', 'VM.clean_reboot'),
|
||||
('rebootguest', 'Running', 'running', 'VM.clean_reboot'),
|
||||
('reboot-guest', 'Running', 'running', 'VM.clean_reboot'),
|
||||
('reboot_guest', 'Running', 'running', 'VM.clean_reboot'),
|
||||
("poweredon", "Halted", "running", "VM.start"),
|
||||
("Poweredon", "Halted", "running", "VM.start"),
|
||||
("powered-on", "Halted", "running", "VM.start"),
|
||||
("Powered_on", "Halted", "running", "VM.start"),
|
||||
("poweredon", "Suspended", "running", "VM.resume"),
|
||||
("Poweredon", "Suspended", "running", "VM.resume"),
|
||||
("powered-on", "Suspended", "running", "VM.resume"),
|
||||
("Powered_on", "Suspended", "running", "VM.resume"),
|
||||
("poweredon", "Paused", "running", "VM.unpause"),
|
||||
("Poweredon", "Paused", "running", "VM.unpause"),
|
||||
("powered-on", "Paused", "running", "VM.unpause"),
|
||||
("Powered_on", "Paused", "running", "VM.unpause"),
|
||||
("poweredoff", "Running", "halted", "VM.hard_shutdown"),
|
||||
("Poweredoff", "Running", "halted", "VM.hard_shutdown"),
|
||||
("powered-off", "Running", "halted", "VM.hard_shutdown"),
|
||||
("powered_off", "Running", "halted", "VM.hard_shutdown"),
|
||||
("poweredoff", "Suspended", "halted", "VM.hard_shutdown"),
|
||||
("Poweredoff", "Suspended", "halted", "VM.hard_shutdown"),
|
||||
("powered-off", "Suspended", "halted", "VM.hard_shutdown"),
|
||||
("powered_off", "Suspended", "halted", "VM.hard_shutdown"),
|
||||
("poweredoff", "Paused", "halted", "VM.hard_shutdown"),
|
||||
("Poweredoff", "Paused", "halted", "VM.hard_shutdown"),
|
||||
("powered-off", "Paused", "halted", "VM.hard_shutdown"),
|
||||
("powered_off", "Paused", "halted", "VM.hard_shutdown"),
|
||||
("restarted", "Running", "running", "VM.hard_reboot"),
|
||||
("Restarted", "Running", "running", "VM.hard_reboot"),
|
||||
("restarted", "Paused", "running", "VM.hard_reboot"),
|
||||
("Restarted", "Paused", "running", "VM.hard_reboot"),
|
||||
("suspended", "Running", "suspended", "VM.suspend"),
|
||||
("Suspended", "Running", "suspended", "VM.suspend"),
|
||||
("shutdownguest", "Running", "halted", "VM.clean_shutdown"),
|
||||
("Shutdownguest", "Running", "halted", "VM.clean_shutdown"),
|
||||
("shutdown-guest", "Running", "halted", "VM.clean_shutdown"),
|
||||
("shutdown_guest", "Running", "halted", "VM.clean_shutdown"),
|
||||
("rebootguest", "Running", "running", "VM.clean_reboot"),
|
||||
("rebootguest", "Running", "running", "VM.clean_reboot"),
|
||||
("reboot-guest", "Running", "running", "VM.clean_reboot"),
|
||||
("reboot_guest", "Running", "running", "VM.clean_reboot"),
|
||||
],
|
||||
"ids": [
|
||||
"poweredoff->poweredon",
|
||||
|
|
@ -162,14 +162,14 @@ testcase_set_vm_power_state_transitions = {
|
|||
|
||||
testcase_set_vm_power_state_transitions_async = {
|
||||
"params": [
|
||||
('shutdownguest', 'Running', 'halted', 'Async.VM.clean_shutdown'),
|
||||
('Shutdownguest', 'Running', 'halted', 'Async.VM.clean_shutdown'),
|
||||
('shutdown-guest', 'Running', 'halted', 'Async.VM.clean_shutdown'),
|
||||
('shutdown_guest', 'Running', 'halted', 'Async.VM.clean_shutdown'),
|
||||
('rebootguest', 'Running', 'running', 'Async.VM.clean_reboot'),
|
||||
('rebootguest', 'Running', 'running', 'Async.VM.clean_reboot'),
|
||||
('reboot-guest', 'Running', 'running', 'Async.VM.clean_reboot'),
|
||||
('reboot_guest', 'Running', 'running', 'Async.VM.clean_reboot'),
|
||||
("shutdownguest", "Running", "halted", "Async.VM.clean_shutdown"),
|
||||
("Shutdownguest", "Running", "halted", "Async.VM.clean_shutdown"),
|
||||
("shutdown-guest", "Running", "halted", "Async.VM.clean_shutdown"),
|
||||
("shutdown_guest", "Running", "halted", "Async.VM.clean_shutdown"),
|
||||
("rebootguest", "Running", "running", "Async.VM.clean_reboot"),
|
||||
("rebootguest", "Running", "running", "Async.VM.clean_reboot"),
|
||||
("reboot-guest", "Running", "running", "Async.VM.clean_reboot"),
|
||||
("reboot_guest", "Running", "running", "Async.VM.clean_reboot"),
|
||||
],
|
||||
"ids": [
|
||||
"poweredon->shutdownguest",
|
||||
|
|
@ -184,26 +184,26 @@ testcase_set_vm_power_state_transitions_async = {
|
|||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('vm_ref', testcase_bad_xenapi_refs['params'], ids=testcase_bad_xenapi_refs['ids']) # type: ignore
|
||||
@pytest.mark.parametrize("vm_ref", testcase_bad_xenapi_refs["params"], ids=testcase_bad_xenapi_refs["ids"]) # type: ignore
|
||||
def test_set_vm_power_state_bad_vm_ref(fake_ansible_module, xenserver, vm_ref):
|
||||
"""Tests failure on bad vm_ref."""
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.set_vm_power_state(fake_ansible_module, vm_ref, None)
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "Cannot set VM power state. Invalid VM reference supplied!"
|
||||
assert exc_info.value.kwargs["msg"] == "Cannot set VM power state. Invalid VM reference supplied!"
|
||||
|
||||
|
||||
def test_set_vm_power_state_xenapi_failure(mock_xenapi_failure, fake_ansible_module, xenserver):
|
||||
"""Tests catching of XenAPI failures."""
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), "poweredon")
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), "poweredon")
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
assert exc_info.value.kwargs["msg"] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
|
||||
|
||||
def test_set_vm_power_state_bad_power_state(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests failure on unsupported power state."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": "Running",
|
||||
|
|
@ -212,21 +212,25 @@ def test_set_vm_power_state_bad_power_state(mocker, fake_ansible_module, XenAPI,
|
|||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), "bad")
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), "bad")
|
||||
|
||||
# Beside VM.get_power_state() no other method should have been
|
||||
# called additionally.
|
||||
assert len(mocked_xenapi.method_calls) == 1
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "Requested VM power state 'bad' is unsupported!"
|
||||
assert exc_info.value.kwargs["msg"] == "Requested VM power state 'bad' is unsupported!"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, error_msg',
|
||||
testcase_set_vm_power_state_bad_transitions['params'], # type: ignore
|
||||
ids=testcase_set_vm_power_state_bad_transitions['ids']) # type: ignore
|
||||
def test_set_vm_power_state_bad_transition(mocker, fake_ansible_module, XenAPI, xenserver, power_state_desired, power_state_current, error_msg):
|
||||
@pytest.mark.parametrize(
|
||||
"power_state_desired, power_state_current, error_msg",
|
||||
testcase_set_vm_power_state_bad_transitions["params"], # type: ignore
|
||||
ids=testcase_set_vm_power_state_bad_transitions["ids"], # type: ignore
|
||||
)
|
||||
def test_set_vm_power_state_bad_transition(
|
||||
mocker, fake_ansible_module, XenAPI, xenserver, power_state_desired, power_state_current, error_msg
|
||||
):
|
||||
"""Tests failure on bad power state transition."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": power_state_current,
|
||||
|
|
@ -235,48 +239,56 @@ def test_set_vm_power_state_bad_transition(mocker, fake_ansible_module, XenAPI,
|
|||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), power_state_desired)
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), power_state_desired)
|
||||
|
||||
# Beside VM.get_power_state() no other method should have been
|
||||
# called additionally.
|
||||
assert len(mocked_xenapi.method_calls) == 1
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == error_msg
|
||||
assert exc_info.value.kwargs["msg"] == error_msg
|
||||
|
||||
|
||||
@pytest.mark.parametrize('power_state, error_msg',
|
||||
testcase_set_vm_power_state_task_timeout['params'], # type: ignore
|
||||
ids=testcase_set_vm_power_state_task_timeout['ids']) # type: ignore
|
||||
@pytest.mark.parametrize(
|
||||
"power_state, error_msg",
|
||||
testcase_set_vm_power_state_task_timeout["params"], # type: ignore
|
||||
ids=testcase_set_vm_power_state_task_timeout["ids"], # type: ignore
|
||||
)
|
||||
def test_set_vm_power_state_task_timeout(mocker, fake_ansible_module, XenAPI, xenserver, power_state, error_msg):
|
||||
"""Tests failure on async task timeout."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": "Running",
|
||||
"Async.VM.clean_shutdown.return_value": fake_xenapi_ref('task'),
|
||||
"Async.VM.clean_reboot.return_value": fake_xenapi_ref('task'),
|
||||
"Async.VM.clean_shutdown.return_value": fake_xenapi_ref("task"),
|
||||
"Async.VM.clean_reboot.return_value": fake_xenapi_ref("task"),
|
||||
}
|
||||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
mocker.patch('ansible_collections.community.general.plugins.module_utils.xenserver.wait_for_task', return_value="timeout")
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.module_utils.xenserver.wait_for_task", return_value="timeout"
|
||||
)
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), power_state, timeout=1)
|
||||
xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), power_state, timeout=1)
|
||||
|
||||
# Beside VM.get_power_state() only one of Async.VM.clean_shutdown or
|
||||
# Async.VM.clean_reboot should have been called additionally.
|
||||
assert len(mocked_xenapi.method_calls) == 2
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == error_msg
|
||||
assert exc_info.value.kwargs["msg"] == error_msg
|
||||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current',
|
||||
testcase_set_vm_power_state_no_transitions['params'], # type: ignore
|
||||
ids=testcase_set_vm_power_state_no_transitions['ids']) # type: ignore
|
||||
def test_set_vm_power_state_no_transition(mocker, fake_ansible_module, XenAPI, xenserver, power_state_desired, power_state_current):
|
||||
@pytest.mark.parametrize(
|
||||
"power_state_desired, power_state_current",
|
||||
testcase_set_vm_power_state_no_transitions["params"], # type: ignore
|
||||
ids=testcase_set_vm_power_state_no_transitions["ids"], # type: ignore
|
||||
)
|
||||
def test_set_vm_power_state_no_transition(
|
||||
mocker, fake_ansible_module, XenAPI, xenserver, power_state_desired, power_state_current
|
||||
):
|
||||
"""Tests regular invocation without power state transition."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": power_state_current,
|
||||
|
|
@ -284,7 +296,7 @@ def test_set_vm_power_state_no_transition(mocker, fake_ansible_module, XenAPI, x
|
|||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), power_state_desired)
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), power_state_desired)
|
||||
|
||||
# Beside VM.get_power_state() no other method should have been
|
||||
# called additionally.
|
||||
|
|
@ -294,19 +306,23 @@ def test_set_vm_power_state_no_transition(mocker, fake_ansible_module, XenAPI, x
|
|||
assert result[1] == power_state_current.lower()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method',
|
||||
testcase_set_vm_power_state_transitions['params'], # type: ignore
|
||||
ids=testcase_set_vm_power_state_transitions['ids']) # type: ignore
|
||||
def test_set_vm_power_state_transition(mocker,
|
||||
fake_ansible_module,
|
||||
XenAPI,
|
||||
xenserver,
|
||||
power_state_desired,
|
||||
power_state_current,
|
||||
power_state_resulting,
|
||||
activated_xenapi_method):
|
||||
@pytest.mark.parametrize(
|
||||
"power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method",
|
||||
testcase_set_vm_power_state_transitions["params"], # type: ignore
|
||||
ids=testcase_set_vm_power_state_transitions["ids"], # type: ignore
|
||||
)
|
||||
def test_set_vm_power_state_transition(
|
||||
mocker,
|
||||
fake_ansible_module,
|
||||
XenAPI,
|
||||
xenserver,
|
||||
power_state_desired,
|
||||
power_state_current,
|
||||
power_state_resulting,
|
||||
activated_xenapi_method,
|
||||
):
|
||||
"""Tests regular invocation with power state transition."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": power_state_current,
|
||||
|
|
@ -314,11 +330,11 @@ def test_set_vm_power_state_transition(mocker,
|
|||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), power_state_desired, timeout=0)
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), power_state_desired, timeout=0)
|
||||
|
||||
mocked_xenapi_method = mocked_xenapi
|
||||
|
||||
for activated_xenapi_class in activated_xenapi_method.split('.'):
|
||||
for activated_xenapi_class in activated_xenapi_method.split("."):
|
||||
mocked_xenapi_method = getattr(mocked_xenapi_method, activated_xenapi_class)
|
||||
|
||||
mocked_xenapi_method.assert_called_once()
|
||||
|
|
@ -331,37 +347,41 @@ def test_set_vm_power_state_transition(mocker,
|
|||
assert result[1] == power_state_resulting
|
||||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method',
|
||||
testcase_set_vm_power_state_transitions_async['params'], # type: ignore
|
||||
ids=testcase_set_vm_power_state_transitions_async['ids']) # type: ignore
|
||||
def test_set_vm_power_state_transition_async(mocker,
|
||||
fake_ansible_module,
|
||||
XenAPI,
|
||||
xenserver,
|
||||
power_state_desired,
|
||||
power_state_current,
|
||||
power_state_resulting,
|
||||
activated_xenapi_method):
|
||||
@pytest.mark.parametrize(
|
||||
"power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method",
|
||||
testcase_set_vm_power_state_transitions_async["params"], # type: ignore
|
||||
ids=testcase_set_vm_power_state_transitions_async["ids"], # type: ignore
|
||||
)
|
||||
def test_set_vm_power_state_transition_async(
|
||||
mocker,
|
||||
fake_ansible_module,
|
||||
XenAPI,
|
||||
xenserver,
|
||||
power_state_desired,
|
||||
power_state_current,
|
||||
power_state_resulting,
|
||||
activated_xenapi_method,
|
||||
):
|
||||
"""
|
||||
Tests regular invocation with async power state transition
|
||||
(shutdownguest and rebootguest only).
|
||||
"""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": power_state_current,
|
||||
f"{activated_xenapi_method}.return_value": fake_xenapi_ref('task'),
|
||||
f"{activated_xenapi_method}.return_value": fake_xenapi_ref("task"),
|
||||
}
|
||||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
mocker.patch('ansible_collections.community.general.plugins.module_utils.xenserver.wait_for_task', return_value="")
|
||||
mocker.patch("ansible_collections.community.general.plugins.module_utils.xenserver.wait_for_task", return_value="")
|
||||
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), power_state_desired, timeout=1)
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), power_state_desired, timeout=1)
|
||||
|
||||
mocked_xenapi_method = mocked_xenapi
|
||||
|
||||
for activated_xenapi_class in activated_xenapi_method.split('.'):
|
||||
for activated_xenapi_class in activated_xenapi_method.split("."):
|
||||
mocked_xenapi_method = getattr(mocked_xenapi_method, activated_xenapi_class)
|
||||
|
||||
mocked_xenapi_method.assert_called_once()
|
||||
|
|
@ -374,19 +394,23 @@ def test_set_vm_power_state_transition_async(mocker,
|
|||
assert result[1] == power_state_resulting
|
||||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method',
|
||||
testcase_set_vm_power_state_transitions['params'], # type: ignore
|
||||
ids=testcase_set_vm_power_state_transitions['ids']) # type: ignore
|
||||
def test_set_vm_power_state_transition_check_mode(mocker,
|
||||
fake_ansible_module,
|
||||
XenAPI,
|
||||
xenserver,
|
||||
power_state_desired,
|
||||
power_state_current,
|
||||
power_state_resulting,
|
||||
activated_xenapi_method):
|
||||
@pytest.mark.parametrize(
|
||||
"power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method",
|
||||
testcase_set_vm_power_state_transitions["params"], # type: ignore
|
||||
ids=testcase_set_vm_power_state_transitions["ids"], # type: ignore
|
||||
)
|
||||
def test_set_vm_power_state_transition_check_mode(
|
||||
mocker,
|
||||
fake_ansible_module,
|
||||
XenAPI,
|
||||
xenserver,
|
||||
power_state_desired,
|
||||
power_state_current,
|
||||
power_state_resulting,
|
||||
activated_xenapi_method,
|
||||
):
|
||||
"""Tests regular invocation with power state transition in check mode."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": power_state_current,
|
||||
|
|
@ -395,11 +419,11 @@ def test_set_vm_power_state_transition_check_mode(mocker,
|
|||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
fake_ansible_module.check_mode = True
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref('VM'), power_state_desired, timeout=0)
|
||||
result = xenserver.set_vm_power_state(fake_ansible_module, fake_xenapi_ref("VM"), power_state_desired, timeout=0)
|
||||
|
||||
mocked_xenapi_method = mocked_xenapi
|
||||
|
||||
for activated_xenapi_class in activated_xenapi_method.split('.'):
|
||||
for activated_xenapi_class in activated_xenapi_method.split("."):
|
||||
mocked_xenapi_method = getattr(mocked_xenapi_method, activated_xenapi_class)
|
||||
|
||||
mocked_xenapi_method.assert_not_called()
|
||||
|
|
|
|||
|
|
@ -14,71 +14,73 @@ from .common import fake_xenapi_ref, testcase_bad_xenapi_refs
|
|||
|
||||
testcase_wait_for_vm_ip_address_bad_power_states = {
|
||||
"params": [
|
||||
'Halted',
|
||||
'Paused',
|
||||
'Suspended',
|
||||
'Other',
|
||||
"Halted",
|
||||
"Paused",
|
||||
"Suspended",
|
||||
"Other",
|
||||
],
|
||||
"ids": [
|
||||
'state-halted',
|
||||
'state-paused',
|
||||
'state-suspended',
|
||||
'state-other',
|
||||
]
|
||||
"state-halted",
|
||||
"state-paused",
|
||||
"state-suspended",
|
||||
"state-other",
|
||||
],
|
||||
}
|
||||
|
||||
testcase_wait_for_vm_ip_address_bad_guest_metrics = {
|
||||
"params": [
|
||||
('OpaqueRef:NULL', {"networks": {}}),
|
||||
(fake_xenapi_ref('VM_guest_metrics'), {"networks": {}}),
|
||||
("OpaqueRef:NULL", {"networks": {}}),
|
||||
(fake_xenapi_ref("VM_guest_metrics"), {"networks": {}}),
|
||||
],
|
||||
"ids": [
|
||||
'vm_guest_metrics_ref-null, no-ip',
|
||||
'vm_guest_metrics_ref-ok, no-ip',
|
||||
"vm_guest_metrics_ref-null, no-ip",
|
||||
"vm_guest_metrics_ref-ok, no-ip",
|
||||
],
|
||||
}
|
||||
|
||||
testcase_wait_for_task_all_statuses = {
|
||||
"params": [
|
||||
('Success', ''),
|
||||
('Failure', 'failure'),
|
||||
('Cancelling', 'cancelling'),
|
||||
('Cancelled', 'cancelled'),
|
||||
('Other', 'other'),
|
||||
("Success", ""),
|
||||
("Failure", "failure"),
|
||||
("Cancelling", "cancelling"),
|
||||
("Cancelled", "cancelled"),
|
||||
("Other", "other"),
|
||||
],
|
||||
"ids": [
|
||||
'task-success',
|
||||
'task-failure',
|
||||
'task-cancelling',
|
||||
'task-cancelled',
|
||||
'task-other',
|
||||
]
|
||||
"task-success",
|
||||
"task-failure",
|
||||
"task-cancelling",
|
||||
"task-cancelled",
|
||||
"task-other",
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('vm_ref', testcase_bad_xenapi_refs['params'], ids=testcase_bad_xenapi_refs['ids']) # type: ignore
|
||||
@pytest.mark.parametrize("vm_ref", testcase_bad_xenapi_refs["params"], ids=testcase_bad_xenapi_refs["ids"]) # type: ignore
|
||||
def test_wait_for_vm_ip_address_bad_vm_ref(fake_ansible_module, xenserver, vm_ref):
|
||||
"""Tests failure on bad vm_ref."""
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.wait_for_vm_ip_address(fake_ansible_module, vm_ref)
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "Cannot wait for VM IP address. Invalid VM reference supplied!"
|
||||
assert exc_info.value.kwargs["msg"] == "Cannot wait for VM IP address. Invalid VM reference supplied!"
|
||||
|
||||
|
||||
def test_wait_for_vm_ip_address_xenapi_failure(mock_xenapi_failure, xenserver, fake_ansible_module):
|
||||
"""Tests catching of XenAPI failures."""
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref('VM'))
|
||||
xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref("VM"))
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
assert exc_info.value.kwargs["msg"] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('bad_power_state',
|
||||
testcase_wait_for_vm_ip_address_bad_power_states['params'],
|
||||
ids=testcase_wait_for_vm_ip_address_bad_power_states['ids'])
|
||||
@pytest.mark.parametrize(
|
||||
"bad_power_state",
|
||||
testcase_wait_for_vm_ip_address_bad_power_states["params"],
|
||||
ids=testcase_wait_for_vm_ip_address_bad_power_states["ids"],
|
||||
)
|
||||
def test_wait_for_vm_ip_address_bad_power_state(mocker, fake_ansible_module, XenAPI, xenserver, bad_power_state):
|
||||
"""Tests failure on bad power state."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": bad_power_state,
|
||||
|
|
@ -87,18 +89,23 @@ def test_wait_for_vm_ip_address_bad_power_state(mocker, fake_ansible_module, Xen
|
|||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref('VM'))
|
||||
xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref("VM"))
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == (
|
||||
f"Cannot wait for VM IP address when VM is in state '{xenserver.xapi_to_module_vm_power_state(bad_power_state.lower())}'!")
|
||||
assert exc_info.value.kwargs["msg"] == (
|
||||
f"Cannot wait for VM IP address when VM is in state '{xenserver.xapi_to_module_vm_power_state(bad_power_state.lower())}'!"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('bad_guest_metrics_ref, bad_guest_metrics',
|
||||
testcase_wait_for_vm_ip_address_bad_guest_metrics['params'], # type: ignore
|
||||
ids=testcase_wait_for_vm_ip_address_bad_guest_metrics['ids']) # type: ignore
|
||||
def test_wait_for_vm_ip_address_timeout(mocker, fake_ansible_module, XenAPI, xenserver, bad_guest_metrics_ref, bad_guest_metrics):
|
||||
@pytest.mark.parametrize(
|
||||
"bad_guest_metrics_ref, bad_guest_metrics",
|
||||
testcase_wait_for_vm_ip_address_bad_guest_metrics["params"], # type: ignore
|
||||
ids=testcase_wait_for_vm_ip_address_bad_guest_metrics["ids"], # type: ignore
|
||||
)
|
||||
def test_wait_for_vm_ip_address_timeout(
|
||||
mocker, fake_ansible_module, XenAPI, xenserver, bad_guest_metrics_ref, bad_guest_metrics
|
||||
):
|
||||
"""Tests timeout."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": "Running",
|
||||
|
|
@ -108,17 +115,17 @@ def test_wait_for_vm_ip_address_timeout(mocker, fake_ansible_module, XenAPI, xen
|
|||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
mocker.patch('time.sleep')
|
||||
mocker.patch("time.sleep")
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref('VM'), timeout=1)
|
||||
xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref("VM"), timeout=1)
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "Timed out waiting for VM IP address!"
|
||||
assert exc_info.value.kwargs["msg"] == "Timed out waiting for VM IP address!"
|
||||
|
||||
|
||||
def test_wait_for_vm_ip_address(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests regular invocation."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
# This mock simulates regular VM IP acquirement lifecycle:
|
||||
#
|
||||
|
|
@ -130,9 +137,9 @@ def test_wait_for_vm_ip_address(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
mocked_returns = {
|
||||
"VM.get_power_state.return_value": "Running",
|
||||
"VM.get_guest_metrics.side_effect": [
|
||||
'OpaqueRef:NULL',
|
||||
fake_xenapi_ref('VM_guest_metrics'),
|
||||
fake_xenapi_ref('VM_guest_metrics'),
|
||||
"OpaqueRef:NULL",
|
||||
fake_xenapi_ref("VM_guest_metrics"),
|
||||
fake_xenapi_ref("VM_guest_metrics"),
|
||||
],
|
||||
"VM_guest_metrics.get_record.side_effect": [
|
||||
{
|
||||
|
|
@ -149,33 +156,33 @@ def test_wait_for_vm_ip_address(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
mocker.patch('time.sleep')
|
||||
mocker.patch("time.sleep")
|
||||
|
||||
fake_guest_metrics = xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref('VM'))
|
||||
fake_guest_metrics = xenserver.wait_for_vm_ip_address(fake_ansible_module, fake_xenapi_ref("VM"))
|
||||
|
||||
assert fake_guest_metrics == mocked_returns['VM_guest_metrics.get_record.side_effect'][1]
|
||||
assert fake_guest_metrics == mocked_returns["VM_guest_metrics.get_record.side_effect"][1]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('task_ref', testcase_bad_xenapi_refs['params'], ids=testcase_bad_xenapi_refs['ids']) # type: ignore
|
||||
@pytest.mark.parametrize("task_ref", testcase_bad_xenapi_refs["params"], ids=testcase_bad_xenapi_refs["ids"]) # type: ignore
|
||||
def test_wait_for_task_bad_task_ref(fake_ansible_module, xenserver, task_ref):
|
||||
"""Tests failure on bad task_ref."""
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.wait_for_task(fake_ansible_module, task_ref)
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == "Cannot wait for task. Invalid task reference supplied!"
|
||||
assert exc_info.value.kwargs["msg"] == "Cannot wait for task. Invalid task reference supplied!"
|
||||
|
||||
|
||||
def test_wait_for_task_xenapi_failure(mock_xenapi_failure, fake_ansible_module, xenserver):
|
||||
"""Tests catching of XenAPI failures."""
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.wait_for_task(fake_ansible_module, fake_xenapi_ref('task'))
|
||||
xenserver.wait_for_task(fake_ansible_module, fake_xenapi_ref("task"))
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
assert exc_info.value.kwargs["msg"] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
|
||||
|
||||
def test_wait_for_task_timeout(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests timeout."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"task.get_status.return_value": "Pending",
|
||||
|
|
@ -184,26 +191,28 @@ def test_wait_for_task_timeout(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
mocker.patch('time.sleep')
|
||||
mocker.patch("time.sleep")
|
||||
|
||||
fake_result = xenserver.wait_for_task(fake_ansible_module, fake_xenapi_ref('task'), timeout=1)
|
||||
fake_result = xenserver.wait_for_task(fake_ansible_module, fake_xenapi_ref("task"), timeout=1)
|
||||
|
||||
mocked_xenapi.task.destroy.assert_called_once()
|
||||
assert fake_result == "timeout"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('task_status, result',
|
||||
testcase_wait_for_task_all_statuses['params'], # type: ignore
|
||||
ids=testcase_wait_for_task_all_statuses['ids']) # type: ignore
|
||||
@pytest.mark.parametrize(
|
||||
"task_status, result",
|
||||
testcase_wait_for_task_all_statuses["params"], # type: ignore
|
||||
ids=testcase_wait_for_task_all_statuses["ids"], # type: ignore
|
||||
)
|
||||
def test_wait_for_task(mocker, fake_ansible_module, XenAPI, xenserver, task_status, result):
|
||||
"""Tests regular invocation."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
# Mock will first return Pending status and on second invocation it will
|
||||
# return one of possible final statuses.
|
||||
mocked_returns = {
|
||||
"task.get_status.side_effect": [
|
||||
'Pending',
|
||||
"Pending",
|
||||
task_status,
|
||||
],
|
||||
"task.destroy.return_value": None,
|
||||
|
|
@ -211,9 +220,9 @@ def test_wait_for_task(mocker, fake_ansible_module, XenAPI, xenserver, task_stat
|
|||
|
||||
mocked_xenapi.configure_mock(**mocked_returns)
|
||||
|
||||
mocker.patch('time.sleep')
|
||||
mocker.patch("time.sleep")
|
||||
|
||||
fake_result = xenserver.wait_for_task(fake_ansible_module, fake_xenapi_ref('task'))
|
||||
fake_result = xenserver.wait_for_task(fake_ansible_module, fake_xenapi_ref("task"))
|
||||
|
||||
mocked_xenapi.task.destroy.assert_called_once()
|
||||
assert fake_result == result
|
||||
|
|
|
|||
|
|
@ -63,24 +63,34 @@ testcase_module_remote_conn_scheme = {
|
|||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fake_ansible_module', testcase_module_local_conn['params'], ids=testcase_module_local_conn['ids'], indirect=True) # type: ignore
|
||||
@pytest.mark.parametrize(
|
||||
"fake_ansible_module",
|
||||
testcase_module_local_conn["params"], # type: ignore
|
||||
ids=testcase_module_local_conn["ids"], # type: ignore
|
||||
indirect=True,
|
||||
)
|
||||
def test_xapi_connect_local_session(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests that connection to localhost uses XenAPI.xapi_local() function."""
|
||||
mocker.patch('XenAPI.xapi_local')
|
||||
mocker.patch("XenAPI.xapi_local")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
XenAPI.xapi_local.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fake_ansible_module', testcase_module_local_conn['params'], ids=testcase_module_local_conn['ids'], indirect=True) # type: ignore
|
||||
@pytest.mark.parametrize(
|
||||
"fake_ansible_module",
|
||||
testcase_module_local_conn["params"], # type: ignore
|
||||
ids=testcase_module_local_conn["ids"], # type: ignore
|
||||
indirect=True,
|
||||
)
|
||||
def test_xapi_connect_local_login(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests that connection to localhost uses empty username and password."""
|
||||
mocker.patch.object(XenAPI.Session, 'login_with_password', create=True)
|
||||
mocker.patch.object(XenAPI.Session, "login_with_password", create=True)
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
XenAPI.Session.login_with_password.assert_called_once_with('', '', ANSIBLE_VERSION, 'Ansible')
|
||||
XenAPI.Session.login_with_password.assert_called_once_with("", "", ANSIBLE_VERSION, "Ansible")
|
||||
|
||||
|
||||
def test_xapi_connect_login(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
|
|
@ -88,80 +98,88 @@ def test_xapi_connect_login(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
Tests that username and password are properly propagated to
|
||||
XenAPI.Session.login_with_password() function.
|
||||
"""
|
||||
mocker.patch.object(XenAPI.Session, 'login_with_password', create=True)
|
||||
mocker.patch.object(XenAPI.Session, "login_with_password", create=True)
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
username = fake_ansible_module.params['username']
|
||||
password = fake_ansible_module.params['password']
|
||||
username = fake_ansible_module.params["username"]
|
||||
password = fake_ansible_module.params["password"]
|
||||
|
||||
XenAPI.Session.login_with_password.assert_called_once_with(username, password, ANSIBLE_VERSION, 'Ansible')
|
||||
XenAPI.Session.login_with_password.assert_called_once_with(username, password, ANSIBLE_VERSION, "Ansible")
|
||||
|
||||
|
||||
def test_xapi_connect_login_failure(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests that login failure is properly handled."""
|
||||
fake_error_msg = "Fake XAPI login error!"
|
||||
|
||||
mocked_login = mocker.patch.object(XenAPI.Session, 'login_with_password', create=True)
|
||||
mocked_login = mocker.patch.object(XenAPI.Session, "login_with_password", create=True)
|
||||
mocked_login.side_effect = XenAPI.Failure(fake_error_msg)
|
||||
|
||||
hostname = fake_ansible_module.params['hostname']
|
||||
username = fake_ansible_module.params['username']
|
||||
hostname = fake_ansible_module.params["hostname"]
|
||||
username = fake_ansible_module.params["username"]
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == f"Unable to log on to XenServer at http://{hostname} as {username}: {fake_error_msg}"
|
||||
assert (
|
||||
exc_info.value.kwargs["msg"]
|
||||
== f"Unable to log on to XenServer at http://{hostname} as {username}: {fake_error_msg}"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'fake_ansible_module',
|
||||
testcase_module_remote_conn_scheme['params'], # type: ignore
|
||||
ids=testcase_module_remote_conn_scheme['ids'], # type: ignore
|
||||
"fake_ansible_module",
|
||||
testcase_module_remote_conn_scheme["params"], # type: ignore
|
||||
ids=testcase_module_remote_conn_scheme["ids"], # type: ignore
|
||||
indirect=True,
|
||||
)
|
||||
def test_xapi_connect_remote_scheme(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests that explicit scheme in hostname param is preserved."""
|
||||
mocker.patch('XenAPI.Session')
|
||||
mocker.patch("XenAPI.Session")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
hostname = fake_ansible_module.params['hostname']
|
||||
ignore_ssl = not fake_ansible_module.params['validate_certs']
|
||||
hostname = fake_ansible_module.params["hostname"]
|
||||
ignore_ssl = not fake_ansible_module.params["validate_certs"]
|
||||
|
||||
XenAPI.Session.assert_called_once_with(hostname, ignore_ssl=ignore_ssl)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fake_ansible_module', testcase_module_remote_conn['params'], ids=testcase_module_remote_conn['ids'], indirect=True) # type: ignore
|
||||
@pytest.mark.parametrize(
|
||||
"fake_ansible_module",
|
||||
testcase_module_remote_conn["params"], # type: ignore
|
||||
ids=testcase_module_remote_conn["ids"], # type: ignore
|
||||
indirect=True,
|
||||
)
|
||||
def test_xapi_connect_remote_no_scheme(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests that proper scheme is prepended to hostname without scheme."""
|
||||
mocker.patch('XenAPI.Session')
|
||||
mocker.patch("XenAPI.Session")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
hostname = fake_ansible_module.params['hostname']
|
||||
ignore_ssl = not fake_ansible_module.params['validate_certs']
|
||||
hostname = fake_ansible_module.params["hostname"]
|
||||
ignore_ssl = not fake_ansible_module.params["validate_certs"]
|
||||
|
||||
XenAPI.Session.assert_called_once_with(f"http://{hostname}", ignore_ssl=ignore_ssl)
|
||||
|
||||
|
||||
def test_xapi_connect_support_ignore_ssl(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests proper handling of ignore_ssl support."""
|
||||
mocked_session = mocker.patch('XenAPI.Session')
|
||||
mocked_session = mocker.patch("XenAPI.Session")
|
||||
mocked_session.side_effect = TypeError()
|
||||
|
||||
with pytest.raises(TypeError) as exc_info:
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
hostname = fake_ansible_module.params['hostname']
|
||||
ignore_ssl = not fake_ansible_module.params['validate_certs']
|
||||
hostname = fake_ansible_module.params["hostname"]
|
||||
ignore_ssl = not fake_ansible_module.params["validate_certs"]
|
||||
|
||||
XenAPI.Session.assert_called_with(f"http://{hostname}")
|
||||
|
||||
|
||||
def test_xapi_connect_no_disconnect_atexit(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests skipping registration of atexit disconnect handler."""
|
||||
mocker.patch('atexit.register')
|
||||
mocker.patch("atexit.register")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module, disconnect_atexit=False)
|
||||
|
||||
|
|
@ -170,7 +188,7 @@ def test_xapi_connect_no_disconnect_atexit(mocker, fake_ansible_module, XenAPI,
|
|||
|
||||
def test_xapi_connect_singleton(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests if XAPI.connect() returns singleton."""
|
||||
mocker.patch('XenAPI.Session')
|
||||
mocker.patch("XenAPI.Session")
|
||||
|
||||
xapi_session1 = xenserver.XAPI.connect(fake_ansible_module)
|
||||
xapi_session2 = xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
|
|
|||
|
|
@ -14,12 +14,12 @@ from .common import fake_xenapi_ref
|
|||
|
||||
def test_xenserverobject_xenapi_lib_detection(mocker, fake_ansible_module, xenserver):
|
||||
"""Tests XenAPI lib detection code."""
|
||||
mocker.patch('ansible_collections.community.general.plugins.module_utils.xenserver.HAS_XENAPI', new=False)
|
||||
mocker.patch("ansible_collections.community.general.plugins.module_utils.xenserver.HAS_XENAPI", new=False)
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.XenServerObject(fake_ansible_module)
|
||||
|
||||
assert 'Failed to import the required Python library (XenAPI) on' in exc_info.value.kwargs['msg']
|
||||
assert "Failed to import the required Python library (XenAPI) on" in exc_info.value.kwargs["msg"]
|
||||
|
||||
|
||||
def test_xenserverobject_xenapi_failure(mock_xenapi_failure, fake_ansible_module, xenserver):
|
||||
|
|
@ -27,17 +27,17 @@ def test_xenserverobject_xenapi_failure(mock_xenapi_failure, fake_ansible_module
|
|||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.XenServerObject(fake_ansible_module)
|
||||
|
||||
assert exc_info.value.kwargs['msg'] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
assert exc_info.value.kwargs["msg"] == f"XAPI ERROR: {mock_xenapi_failure[1]}"
|
||||
|
||||
|
||||
def test_xenserverobject(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests successful creation of XenServerObject."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, 'xenapi', create=True)
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi", create=True)
|
||||
|
||||
mocked_returns = {
|
||||
"pool.get_all.return_value": [fake_xenapi_ref('pool')],
|
||||
"pool.get_default_SR.return_value": fake_xenapi_ref('SR'),
|
||||
"session.get_this_host.return_value": fake_xenapi_ref('host'),
|
||||
"pool.get_all.return_value": [fake_xenapi_ref("pool")],
|
||||
"pool.get_default_SR.return_value": fake_xenapi_ref("SR"),
|
||||
"session.get_this_host.return_value": fake_xenapi_ref("host"),
|
||||
"host.get_software_version.return_value": {"product_version": "7.2.0"},
|
||||
}
|
||||
|
||||
|
|
@ -45,5 +45,5 @@ def test_xenserverobject(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
|
||||
xso = xenserver.XenServerObject(fake_ansible_module)
|
||||
|
||||
assert xso.pool_ref == fake_xenapi_ref('pool')
|
||||
assert xso.pool_ref == fake_xenapi_ref("pool")
|
||||
assert xso.xenserver_version == [7, 2, 0]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue