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

View File

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