Files
kubernetes.core/tests/unit/module_utils/test_runner.py
T
Yuriy Novostavskiyandpatchback[bot] cd631923aa fix(k8s): emit warnings via AnsibleModule.warn instead of exit_json (#1267)
SUMMARY
runner.py put warnings into the result dict that run_module() and k8s_service unpack into exit_json()/fail_json(). Passing warnings that way is deprecated since ansible-core 2.19 and goes away in 2.23, so every k8s or k8s_service run that produced a warning also emitted a deprecation warning. All five sites now use AnsibleModule.warn.
This also fixes a quieter bug in the same code. For a multi-document manifest run_module() returns {"changed": ..., "result": {"results": results}}, so warnings ended up nested at result.results[*].warnings, where ansible-core never looked. They were silently dropped whenever a task handled more than one resource, and are now shown.
#1033 fixed this class of bug in k8s_drain, k8s_rollback and k8s_scale for #1031, but never touched runner.py.
Fixes #1264
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
k8s
k8s_service
ADDITIONAL INFORMATION
Verified on kind (Kubernetes v1.37.0) with ansible-core 2.21.4
AI Assistance Disclosure
Generated: unit test is AI-generated,
Human Verified: test on the kind cluster performed by a human

Reviewed-by: Don Naro <dnaro@redhat.com>
Reviewed-by: Bikouo Aubin
(cherry picked from commit 11b861f77e)
2026-11-16 17:23:12 +00:00

211 lines
6.1 KiB
Python

from copy import deepcopy
from unittest.mock import Mock
import pytest
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.runner import (
perform_action,
)
from kubernetes.dynamic.resource import ResourceInstance
definition = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "foo",
"labels": {"environment": "production", "app": "nginx"},
"namespace": "foo",
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx:1.14.2",
"command": ["/bin/sh", "-c", "sleep 10"],
}
]
},
}
modified_def = deepcopy(definition)
modified_def["metadata"]["labels"]["environment"] = "testing"
# Differs from the above only in metadata.resourceVersion, which diff_objects()
# treats as a match while still producing a diff.
versioned_def = deepcopy(definition)
versioned_def["metadata"]["resourceVersion"] = "1"
rereconciled_def = deepcopy(definition)
rereconciled_def["metadata"]["resourceVersion"] = "2"
NO_MEANINGFUL_DIFF = (
"No meaningful diff was generated, but the API may not be idempotent "
"(only metadata.generation or metadata.resourceVersion were changed)"
)
@pytest.mark.parametrize(
"action, params, existing, instance_warnings, expected, expected_warnings",
[
(
"delete",
{"state": "absent"},
{},
{},
{"changed": False, "method": "delete", "result": {}},
[],
),
(
"delete",
{"state": "absent"},
definition,
{"kind": "Status"},
{"changed": True, "method": "delete", "result": {"kind": "Status"}},
[],
),
(
"apply",
{"apply": "yes"},
{},
(definition, []),
{"changed": True, "method": "apply", "result": definition},
[],
),
(
"apply",
{"apply": "yes"},
{},
(definition, ["test warning"]),
{"changed": True, "method": "apply", "result": definition},
["test warning"],
),
(
"create",
{"state": "patched"},
{},
({}, []),
{"changed": False, "result": {}},
[
"resource 'kind=Pod,name=foo' was not found but will not be created as 'state' parameter has been set to 'patched'"
],
),
(
"create",
{},
{},
(definition, []),
{"changed": True, "method": "create", "result": definition},
[],
),
(
"create",
{},
{},
(definition, ["test warning"]),
{"changed": True, "method": "create", "result": definition},
["test warning"],
),
(
"replace",
{"force": "yes"},
definition,
(definition, []),
{"changed": False, "method": "replace", "result": definition},
[],
),
(
"replace",
{"force": "yes"},
definition,
(modified_def, []),
{"changed": True, "method": "replace", "result": modified_def},
[],
),
(
"replace",
{"force": "yes"},
definition,
(modified_def, ["test warning"]),
{"changed": True, "method": "replace", "result": modified_def},
["test warning"],
),
(
"update",
{},
definition,
(definition, []),
{"changed": False, "method": "update", "result": definition},
[],
),
(
"update",
{},
definition,
(modified_def, []),
{"changed": True, "method": "update", "result": modified_def},
[],
),
(
"update",
{},
definition,
(modified_def, ["test warning"]),
{"changed": True, "method": "update", "result": modified_def},
["test warning"],
),
(
"update",
{},
versioned_def,
(rereconciled_def, []),
{"changed": False, "method": "update", "result": rereconciled_def},
[NO_MEANINGFUL_DIFF],
),
(
"update",
{},
versioned_def,
(rereconciled_def, ["test warning"]),
{"changed": False, "method": "update", "result": rereconciled_def},
["test warning", NO_MEANINGFUL_DIFF],
),
(
"create",
{"label_selectors": ["app=foo"]},
{},
(definition, []),
{
"changed": False,
"msg": "resource 'kind=Pod,name=foo,namespace=foo' filtered by label_selectors.",
},
[],
),
(
"create",
{"label_selectors": ["app=nginx"]},
{},
(definition, []),
{"changed": True, "method": "create", "result": definition},
[],
),
],
)
def test_perform_action(
action, params, existing, instance_warnings, expected, expected_warnings
):
svc = Mock()
svc.find_resource.return_value = Mock(
kind=definition["kind"], group_version=definition["apiVersion"]
)
svc.retrieve.return_value = ResourceInstance(None, existing) if existing else None
spec = {action + ".return_value": instance_warnings}
svc.configure_mock(**spec)
result = perform_action(svc, definition, params)
assert expected.items() <= result.items()
# Warnings must be emitted through AnsibleModule.warn, never smuggled into the
# result dict: run_module()/k8s_service unpack that dict into exit_json(), and
# passing `warnings` to exit_json() is deprecated since ansible-core 2.19
# (https://github.com/ansible-collections/kubernetes.core/issues/1264).
assert "warnings" not in result
assert [c.args[0] for c in svc.module.warn.call_args_list] == expected_warnings