cleanup,test(modules): Cleanup module unit tests

Improve syntax in module unit tests and use only mocker instead of a
combination of mocker and monkeypatch.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2024-06-27 16:08:51 +02:00
parent 52d4b4c731
commit 1630cddc44
2 changed files with 34 additions and 28 deletions

View File

@@ -236,8 +236,8 @@ def k8s_module_params_delete(module_params_delete, vm_definition_running):
} }
def test_module_fails_when_required_args_missing(monkeypatch): def test_module_fails_when_required_args_missing(mocker):
monkeypatch.setattr(AnsibleModule, "fail_json", fail_json) mocker.patch.object(AnsibleModule, "fail_json", fail_json)
with pytest.raises(AnsibleFailJson): with pytest.raises(AnsibleFailJson):
set_module_args({}) set_module_args({})
kubevirt_vm.main() kubevirt_vm.main()
@@ -274,27 +274,29 @@ def test_module_fails_when_required_args_missing(monkeypatch):
) )
def test_module( def test_module(
request, request,
monkeypatch,
mocker, mocker,
module_params, module_params,
k8s_module_params, k8s_module_params,
vm_definition, vm_definition,
method, method,
): ):
monkeypatch.setattr(AnsibleModule, "exit_json", exit_json) mocker.patch.object(AnsibleModule, "exit_json", exit_json)
monkeypatch.setattr(runner, "get_api_client", lambda _: None) mocker.patch.object(runner, "get_api_client")
set_module_args(request.getfixturevalue(module_params)) perform_action = mocker.patch.object(
runner,
perform_action = mocker.patch.object(runner, "perform_action") "perform_action",
perform_action.return_value = { return_value={
"method": method, "method": method,
"changed": True, "changed": True,
"result": "success", "result": "success",
} },
)
with pytest.raises(AnsibleExitJson): with pytest.raises(AnsibleExitJson):
set_module_args(request.getfixturevalue(module_params))
kubevirt_vm.main() kubevirt_vm.main()
perform_action.assert_called_once_with( perform_action.assert_called_once_with(
mocker.ANY, mocker.ANY,
request.getfixturevalue(vm_definition), request.getfixturevalue(vm_definition),
@@ -634,5 +636,6 @@ def vm_template_specs():
], ],
) )
def test_render_template(request, params, rendered_template): def test_render_template(request, params, rendered_template):
result = kubevirt_vm.render_template(request.getfixturevalue(params)) assert kubevirt_vm.render_template(request.getfixturevalue(params)) == dump(
assert result == dump(request.getfixturevalue(rendered_template), sort_keys=False) request.getfixturevalue(rendered_template), sort_keys=False
)

View File

@@ -84,8 +84,8 @@ def find_args_stopped(find_args_default):
{"running": False}, {"running": False},
], ],
) )
def test_module_fails_when_required_args_missing(monkeypatch, module_args): def test_module_fails_when_required_args_missing(mocker, module_args):
monkeypatch.setattr(AnsibleModule, "fail_json", fail_json) mocker.patch.object(AnsibleModule, "fail_json", fail_json)
with pytest.raises(AnsibleFailJson): with pytest.raises(AnsibleFailJson):
set_module_args(module_args) set_module_args(module_args)
kubevirt_vm_info.main() kubevirt_vm_info.main()
@@ -102,19 +102,22 @@ def test_module_fails_when_required_args_missing(monkeypatch, module_args):
({"wait": True, "running": False}, "find_args_stopped"), ({"wait": True, "running": False}, "find_args_stopped"),
], ],
) )
def test_module(request, monkeypatch, mocker, module_args, find_args): def test_module(request, mocker, module_args, find_args):
monkeypatch.setattr(AnsibleModule, "exit_json", exit_json) mocker.patch.object(AnsibleModule, "exit_json", exit_json)
monkeypatch.setattr(kubevirt_vm_info, "get_api_client", lambda _: None) mocker.patch.object(kubevirt_vm_info, "get_api_client")
set_module_args(module_args) find = mocker.patch.object(
K8sService,
find = mocker.patch.object(K8sService, "find") "find",
find.return_value = { return_value={
"api_found": True, "api_found": True,
"failed": False, "failed": False,
"resources": [], "resources": [],
} },
)
with pytest.raises(AnsibleExitJson): with pytest.raises(AnsibleExitJson):
set_module_args(module_args)
kubevirt_vm_info.main() kubevirt_vm_info.main()
find.assert_called_once_with(**request.getfixturevalue(find_args)) find.assert_called_once_with(**request.getfixturevalue(find_args))