mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-24 04:39:15 +00:00
CI: add type checking (#10997)
* Set up type checking with mypy. * Make mypy pass. * Use list() instead of sorted().
This commit is contained in:
parent
831787619a
commit
6088b0cff5
73 changed files with 442 additions and 175 deletions
|
|
@ -36,7 +36,7 @@ class SecretVariablesTestCase(unittest.TestCase):
|
|||
|
||||
self.assertEqual(SecretVariables.list_to_dict(source, hashed=True), expect)
|
||||
|
||||
def test_list_to_dict(self):
|
||||
def test_list_to_dict_2(self):
|
||||
source = [
|
||||
dict(key="secret1", value="value1"),
|
||||
dict(key="secret2", value="value2")
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import typing as t
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils import csv
|
||||
|
|
@ -112,7 +114,7 @@ INVALID_CSV = [
|
|||
),
|
||||
]
|
||||
|
||||
INVALID_DIALECT = [
|
||||
INVALID_DIALECT: list[tuple[str, t.Any, t.Any, str]] = [
|
||||
(
|
||||
'invalid',
|
||||
{},
|
||||
|
|
@ -153,7 +155,7 @@ def test_invalid_csv(data, dialect, dialect_params, fieldnames):
|
|||
|
||||
|
||||
@pytest.mark.parametrize("dialect,dialect_params,fieldnames,data", INVALID_DIALECT)
|
||||
def test_invalid_dialect(data, dialect, dialect_params, fieldnames):
|
||||
def test_invalid_dialect(data: str, dialect: t.Any, dialect_params: t.Any, fieldnames: str) -> None:
|
||||
result = False
|
||||
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -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'])
|
||||
@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) == {}
|
||||
|
|
@ -38,8 +38,8 @@ def test_gather_vm_facts_no_vm_params(fake_ansible_module, xenserver):
|
|||
|
||||
|
||||
@pytest.mark.parametrize('fixture_data_from_file',
|
||||
testcase_gather_vm_params_and_facts['params'],
|
||||
ids=testcase_gather_vm_params_and_facts['ids'],
|
||||
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."""
|
||||
|
|
|
|||
|
|
@ -157,13 +157,13 @@ def test_is_valid_ip_prefix(xenserver, ip_prefix, result):
|
|||
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'])
|
||||
@pytest.mark.parametrize('ip_prefix, ip_netmask', testcase_ip_prefix_to_netmask['params'], 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'])
|
||||
@pytest.mark.parametrize('ip_netmask, ip_prefix', testcase_ip_netmask_to_prefix['params'], 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
|
||||
|
|
|
|||
|
|
@ -184,7 +184,7 @@ testcase_set_vm_power_state_transitions_async = {
|
|||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('vm_ref', testcase_bad_xenapi_refs['params'], ids=testcase_bad_xenapi_refs['ids'])
|
||||
@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:
|
||||
|
|
@ -222,8 +222,8 @@ def test_set_vm_power_state_bad_power_state(mocker, fake_ansible_module, XenAPI,
|
|||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, error_msg',
|
||||
testcase_set_vm_power_state_bad_transitions['params'],
|
||||
ids=testcase_set_vm_power_state_bad_transitions['ids'])
|
||||
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)
|
||||
|
|
@ -245,8 +245,8 @@ def test_set_vm_power_state_bad_transition(mocker, fake_ansible_module, XenAPI,
|
|||
|
||||
|
||||
@pytest.mark.parametrize('power_state, error_msg',
|
||||
testcase_set_vm_power_state_task_timeout['params'],
|
||||
ids=testcase_set_vm_power_state_task_timeout['ids'])
|
||||
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)
|
||||
|
|
@ -272,8 +272,8 @@ def test_set_vm_power_state_task_timeout(mocker, fake_ansible_module, XenAPI, xe
|
|||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current',
|
||||
testcase_set_vm_power_state_no_transitions['params'],
|
||||
ids=testcase_set_vm_power_state_no_transitions['ids'])
|
||||
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)
|
||||
|
|
@ -295,8 +295,8 @@ def test_set_vm_power_state_no_transition(mocker, fake_ansible_module, XenAPI, x
|
|||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method',
|
||||
testcase_set_vm_power_state_transitions['params'],
|
||||
ids=testcase_set_vm_power_state_transitions['ids'])
|
||||
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,
|
||||
|
|
@ -332,8 +332,8 @@ def test_set_vm_power_state_transition(mocker,
|
|||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method',
|
||||
testcase_set_vm_power_state_transitions_async['params'],
|
||||
ids=testcase_set_vm_power_state_transitions_async['ids'])
|
||||
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,
|
||||
|
|
@ -375,8 +375,8 @@ def test_set_vm_power_state_transition_async(mocker,
|
|||
|
||||
|
||||
@pytest.mark.parametrize('power_state_desired, power_state_current, power_state_resulting, activated_xenapi_method',
|
||||
testcase_set_vm_power_state_transitions['params'],
|
||||
ids=testcase_set_vm_power_state_transitions['ids'])
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ testcase_wait_for_task_all_statuses = {
|
|||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('vm_ref', testcase_bad_xenapi_refs['params'], ids=testcase_bad_xenapi_refs['ids'])
|
||||
@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:
|
||||
|
|
@ -94,8 +94,8 @@ def test_wait_for_vm_ip_address_bad_power_state(mocker, fake_ansible_module, Xen
|
|||
|
||||
|
||||
@pytest.mark.parametrize('bad_guest_metrics_ref, bad_guest_metrics',
|
||||
testcase_wait_for_vm_ip_address_bad_guest_metrics['params'],
|
||||
ids=testcase_wait_for_vm_ip_address_bad_guest_metrics['ids'])
|
||||
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)
|
||||
|
|
@ -156,7 +156,7 @@ def test_wait_for_vm_ip_address(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
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'])
|
||||
@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:
|
||||
|
|
@ -193,8 +193,8 @@ def test_wait_for_task_timeout(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
|
||||
|
||||
@pytest.mark.parametrize('task_status, result',
|
||||
testcase_wait_for_task_all_statuses['params'],
|
||||
ids=testcase_wait_for_task_all_statuses['ids'])
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ testcase_module_remote_conn_scheme = {
|
|||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fake_ansible_module', testcase_module_local_conn['params'], ids=testcase_module_local_conn['ids'], indirect=True)
|
||||
@pytest.mark.parametrize('fake_ansible_module', testcase_module_local_conn['params'], ids=testcase_module_local_conn['ids'], indirect=True) # type: ignore
|
||||
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')
|
||||
|
|
@ -73,7 +73,7 @@ def test_xapi_connect_local_session(mocker, fake_ansible_module, XenAPI, xenserv
|
|||
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)
|
||||
@pytest.mark.parametrize('fake_ansible_module', testcase_module_local_conn['params'], ids=testcase_module_local_conn['ids'], indirect=True) # type: ignore
|
||||
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)
|
||||
|
|
@ -114,7 +114,12 @@ def test_xapi_connect_login_failure(mocker, fake_ansible_module, XenAPI, xenserv
|
|||
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'], ids=testcase_module_remote_conn_scheme['ids'], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
'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')
|
||||
|
|
@ -127,7 +132,7 @@ def test_xapi_connect_remote_scheme(mocker, fake_ansible_module, XenAPI, xenserv
|
|||
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)
|
||||
@pytest.mark.parametrize('fake_ansible_module', testcase_module_remote_conn['params'], ids=testcase_module_remote_conn['ids'], indirect=True) # type: ignore
|
||||
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')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue