mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-03-26 21:33:02 +00:00
* Cleanup gha * test by removing matrix excludes * Rename sanity tests * trigger integration tests * Fix ansible-lint workflow * Fix concurrency * Add ansible-lint config * Add ansible-lint config * Fix integration and lint issues * integration wf * fix yamllint issues * fix yamllint issues * update readme and add ignore-2.16.txt * fix ansible-doc * Add version * Use /dev/random to generate random data The GHA environment has difficultly generating entropy. Trying to read from /dev/urandom just blocks forever. We don't care if the random data is cryptographically secure; it's just garbage data for the test. Read from /dev/random, instead. This is only used during the k8s_copy test target. This also removes the custom test module that was being used to generate the files. It's not worth maintaining this for two task that can be replaced with some simple command/shell tasks. * Fix saniry errors * test github_action fix * Address review comments * Remove default types * review comments * isort fixes * remove tags * Add setuptools to venv * Test gh changes * update changelog * update ignore-2.16 * Fix indentation in inventory plugin example * Update .github/workflows/integration-tests.yaml * Update integration-tests.yaml --------- Co-authored-by: Mike Graves <mgraves@redhat.com> Co-authored-by: Bikouo Aubin <79859644+abikouo@users.noreply.github.com>
135 lines
3.6 KiB
Python
135 lines
3.6 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"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"action, params, existing, instance, expected",
|
|
[
|
|
(
|
|
"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},
|
|
),
|
|
(
|
|
"create",
|
|
{"state": "patched"},
|
|
{},
|
|
{},
|
|
{
|
|
"changed": False,
|
|
"result": {},
|
|
"warnings": [
|
|
"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},
|
|
),
|
|
(
|
|
"replace",
|
|
{"force": "yes"},
|
|
definition,
|
|
definition,
|
|
{"changed": False, "method": "replace", "result": definition},
|
|
),
|
|
(
|
|
"replace",
|
|
{"force": "yes"},
|
|
definition,
|
|
modified_def,
|
|
{"changed": True, "method": "replace", "result": modified_def},
|
|
),
|
|
(
|
|
"update",
|
|
{},
|
|
definition,
|
|
definition,
|
|
{"changed": False, "method": "update", "result": definition},
|
|
),
|
|
(
|
|
"update",
|
|
{},
|
|
definition,
|
|
modified_def,
|
|
{"changed": True, "method": "update", "result": modified_def},
|
|
),
|
|
(
|
|
"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, expected):
|
|
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}
|
|
svc.configure_mock(**spec)
|
|
|
|
result = perform_action(svc, definition, params)
|
|
assert expected.items() <= result.items()
|