mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-03-26 21:33:02 +00:00
Fix unit tests (#939)
Some unit tests are broken with ansible-core 2.19, this PR aims to fix them. Reviewed-by: Bianca Henderson <beeankha@gmail.com>
This commit is contained in:
3
changelogs/fragments/20250503-fix-unit-tests.yml
Normal file
3
changelogs/fragments/20250503-fix-unit-tests.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
trivial:
|
||||||
|
- fix unit tests with ``ansible-core 2.19``.
|
||||||
@@ -2,50 +2,57 @@ from __future__ import absolute_import, division, print_function
|
|||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import json
|
import re
|
||||||
|
|
||||||
import kubernetes
|
import kubernetes
|
||||||
import pytest
|
import pytest
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
|
||||||
AnsibleK8SModule,
|
AnsibleK8SModule,
|
||||||
)
|
)
|
||||||
|
from mock import MagicMock, patch
|
||||||
|
|
||||||
MINIMAL_K8S_VERSION = "24.2.0"
|
MINIMAL_K8S_VERSION = "24.2.0"
|
||||||
UNSUPPORTED_K8S_VERSION = "11.0.0"
|
UNSUPPORTED_K8S_VERSION = "11.0.0"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("stdin", [{}], indirect=["stdin"])
|
class FakeAnsibleModule:
|
||||||
def test_no_warn(monkeypatch, stdin, capfd):
|
def __init__(self, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def exit_json(self):
|
||||||
|
raise SystemExit(0)
|
||||||
|
|
||||||
|
|
||||||
|
@patch.object(AnsibleK8SModule, "warn")
|
||||||
|
def test_no_warn(m_ansible_k8s_module_warn, monkeypatch, capfd):
|
||||||
monkeypatch.setattr(kubernetes, "__version__", MINIMAL_K8S_VERSION)
|
monkeypatch.setattr(kubernetes, "__version__", MINIMAL_K8S_VERSION)
|
||||||
|
|
||||||
module = AnsibleK8SModule(argument_spec={})
|
m_ansible_k8s_module_warn.side_effect = print
|
||||||
|
module = AnsibleK8SModule(argument_spec={}, module_class=FakeAnsibleModule)
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
module.exit_json()
|
module.exit_json()
|
||||||
out, err = capfd.readouterr()
|
out, err = capfd.readouterr()
|
||||||
|
m_ansible_k8s_module_warn.assert_not_called()
|
||||||
return_value = json.loads(out)
|
|
||||||
|
|
||||||
assert return_value.get("exception") is None
|
|
||||||
assert return_value.get("warnings") is None
|
|
||||||
assert return_value.get("failed") is None
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("stdin", [{}], indirect=["stdin"])
|
@patch.object(AnsibleK8SModule, "warn")
|
||||||
def test_warn_on_k8s_version(monkeypatch, stdin, capfd):
|
def test_warn_on_k8s_version(m_ansible_k8s_module_warn, monkeypatch, capfd):
|
||||||
monkeypatch.setattr(kubernetes, "__version__", UNSUPPORTED_K8S_VERSION)
|
monkeypatch.setattr(kubernetes, "__version__", UNSUPPORTED_K8S_VERSION)
|
||||||
|
|
||||||
module = AnsibleK8SModule(argument_spec={})
|
m_ansible_k8s_module_warn.side_effect = print
|
||||||
|
module = AnsibleK8SModule(argument_spec={}, module_class=FakeAnsibleModule)
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
module.exit_json()
|
module.exit_json()
|
||||||
|
|
||||||
|
m_ansible_k8s_module_warn.assert_called_once()
|
||||||
out, err = capfd.readouterr()
|
out, err = capfd.readouterr()
|
||||||
|
assert (
|
||||||
return_value = json.loads(out)
|
re.search(
|
||||||
|
r"kubernetes<([0-9]+\.[0-9]+\.[0-9]+) is not supported or tested. Some features may not work.",
|
||||||
assert return_value.get("warnings") is not None
|
out,
|
||||||
warnings = return_value["warnings"]
|
)
|
||||||
assert len(warnings) == 1
|
is not None
|
||||||
assert "kubernetes" in str(warnings[0])
|
)
|
||||||
assert MINIMAL_K8S_VERSION in str(warnings[0])
|
|
||||||
|
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
@@ -58,9 +65,17 @@ dependencies = [
|
|||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"stdin,desired,actual,result", [({}, *d) for d in dependencies], indirect=["stdin"]
|
"stdin,desired,actual,result", [({}, *d) for d in dependencies], indirect=["stdin"]
|
||||||
)
|
)
|
||||||
def test_has_at_least(monkeypatch, stdin, desired, actual, result, capfd):
|
@patch.object(AnsibleK8SModule, "warn")
|
||||||
|
def test_has_at_least(
|
||||||
|
m_ansible_k8s_module_warn, monkeypatch, stdin, desired, actual, result, capfd
|
||||||
|
):
|
||||||
monkeypatch.setattr(kubernetes, "__version__", actual)
|
monkeypatch.setattr(kubernetes, "__version__", actual)
|
||||||
|
|
||||||
|
def fake_warn(x):
|
||||||
|
print(x)
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
m_ansible_k8s_module_warn.side_effect = fake_warn
|
||||||
module = AnsibleK8SModule(argument_spec={})
|
module = AnsibleK8SModule(argument_spec={})
|
||||||
|
|
||||||
assert module.has_at_least("kubernetes", desired) is result
|
assert module.has_at_least("kubernetes", desired) is result
|
||||||
@@ -80,11 +95,18 @@ def test_requires_fails_with_message(
|
|||||||
monkeypatch, stdin, dependency, version, msg, capfd
|
monkeypatch, stdin, dependency, version, msg, capfd
|
||||||
):
|
):
|
||||||
monkeypatch.setattr(kubernetes, "__version__", "24.2.0")
|
monkeypatch.setattr(kubernetes, "__version__", "24.2.0")
|
||||||
module = AnsibleK8SModule(argument_spec={})
|
module = AnsibleK8SModule(argument_spec={}, module_class=FakeAnsibleModule)
|
||||||
|
|
||||||
|
def fake_fail_json(**kwargs):
|
||||||
|
print(f"Printing message => {kwargs}")
|
||||||
|
print(kwargs.get("msg"))
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
module.fail_json = MagicMock()
|
||||||
|
module.fail_json.side_effect = fake_fail_json
|
||||||
|
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
module.requires(dependency, version)
|
module.requires(dependency, version)
|
||||||
|
module.fail_json.assert_called_once()
|
||||||
out, err = capfd.readouterr()
|
out, err = capfd.readouterr()
|
||||||
return_value = json.loads(out)
|
assert msg in out
|
||||||
|
|
||||||
assert return_value.get("failed")
|
|
||||||
assert msg in return_value.get("msg")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user