diff --git a/tests/unit/plugins/inventory/test_kubevirt.py b/tests/unit/plugins/inventory/test_kubevirt.py index 056231c..fe0f253 100644 --- a/tests/unit/plugins/inventory/test_kubevirt.py +++ b/tests/unit/plugins/inventory/test_kubevirt.py @@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -import copy +from json import dumps import pytest @@ -14,6 +14,7 @@ from addict import Dict from ansible_collections.kubevirt.core.plugins.inventory.kubevirt import ( + DynamicApiError, GetVmiOptions, InventoryModule, KubeVirtInventoryException, @@ -772,7 +773,7 @@ def children_group_with_vmi_create_groups_option(children_group_with_vmi): @pytest.fixture(scope="function") def inventory_groups_create_groups_option(inventory_groups): - inv = copy.deepcopy(inventory_groups) + inv = deepcopy(inventory_groups) inv.append("label_kubevirt_io_domain_test_domain") return inv @@ -1016,7 +1017,7 @@ def loadbalancer(): @pytest.fixture(scope="module") def nodeport(loadbalancer): - np = copy.deepcopy(loadbalancer) + np = deepcopy(loadbalancer) np["test-domain"]["spec"]["type"] = "NodePort" return np @@ -1137,3 +1138,49 @@ def test_set_ansible_host_and_port(monkeypatch, request, inventory, host_vars, v inventory.set_ansible_host_and_port(Dict(vmi), vmi_name, "10.10.10.10", service, opts) assert request.getfixturevalue(result) == host_vars + + +@pytest.fixture(scope="function") +def body_error(mocker): + error = DynamicApiError(e=mocker.Mock()) + error.headers = None + + body = "This is a test error" + error.body = body + + return error + + +@pytest.fixture(scope="function") +def message_error(mocker): + error = DynamicApiError(e=mocker.Mock()) + error.headers = { + "Content-Type": "application/json" + } + + error.body = dumps({"message": "This is a test error"}).encode('utf-8') + + return error + + +@pytest.fixture(scope="function") +def status_reason_error(mocker): + error = DynamicApiError(e=mocker.Mock()) + error.body = None + error.status = 404 + error.reason = "This is a test error" + return error + + +@pytest.mark.parametrize( + "error_object,expected_error_msg", + [ + ("body_error", "This is a test error"), + ("message_error", "This is a test error"), + ("status_reason_error", "404 Reason: This is a test error"), + ], +) +def test_format_dynamic_api_exc(request, inventory, error_object, expected_error_msg): + + result = inventory.format_dynamic_api_exc(request.getfixturevalue(error_object)) + assert expected_error_msg == result diff --git a/tests/unit/plugins/inventory/test_kubevirt_composable_vars.py b/tests/unit/plugins/inventory/test_kubevirt_composable_vars.py new file mode 100644 index 0000000..c087226 --- /dev/null +++ b/tests/unit/plugins/inventory/test_kubevirt_composable_vars.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Red Hat, Inc. +# Apache License 2.0 (see LICENSE or http://www.apache.org/licenses/LICENSE-2.0) + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import pytest + +from ansible_collections.kubevirt.core.plugins.inventory.kubevirt import ( + InventoryModule, +) + +from ansible.template import Templar + + +@pytest.fixture(scope="function") +def inventory_composable_vars(mocker): + inventory = InventoryModule() + + inventory.templar = Templar(loader=None) + + inventory._options = { + "compose": {"block_migratable_vmis": "vmi_migration_method"}, + "strict": True, + "groups": {"vmi_node_groups": "cluster_name"}, + "keyed_groups": [{"prefix": "fedora", "key": "vmi_guest_os_info.version"}], + } + + inventory.inventory = mocker.Mock() + + return inventory + + +@pytest.fixture(scope="function") +def host_vars(monkeypatch, inventory_composable_vars): + host_vars = {} + + def set_variable(host, key, value): + if host not in host_vars: + host_vars[host] = {} + host_vars[host][key] = value + + monkeypatch.setattr( + inventory_composable_vars.inventory, "set_variable", set_variable + ) + return host_vars + + +@pytest.fixture(scope="function") +def add_group(monkeypatch, inventory_composable_vars): + groups = [] + + def add_group(name): + if name not in groups: + groups.append(name) + return name + + monkeypatch.setattr(inventory_composable_vars.inventory, "add_group", add_group) + return groups + + +@pytest.fixture(scope="function") +def add_host(monkeypatch, inventory_composable_vars): + hosts = [] + + def add_host(name, group=None): + if name not in hosts: + hosts.append(name) + if group is not None and group not in hosts: + hosts.append(group) + + monkeypatch.setattr(inventory_composable_vars.inventory, "add_host", add_host) + return hosts + + +@pytest.fixture(scope="function") +def add_child(monkeypatch, inventory_composable_vars): + children = {} + + def add_child(group, name): + if group not in children: + children[group] = [] + if name not in children[group]: + children[group].append(name) + + monkeypatch.setattr(inventory_composable_vars.inventory, "add_child", add_child) + return children + + +@pytest.fixture(scope="module") +def vmi_host_vars(): + return { + "vmi_migration_method": "BlockMigration", + "vmi_guest_os_info": {"id": "fedora", "version": "39"}, + "cluster_name": {"test-cluster"}, + } + + +def test_set_composable_vars( + inventory_composable_vars, + mocker, + host_vars, + add_group, + add_child, + add_host, + vmi_host_vars, +): + get_vars = mocker.patch.object( + inventory_composable_vars.inventory.get_host(), "get_vars" + ) + get_vars.return_value = vmi_host_vars + inventory_composable_vars.set_composable_vars("testvmi") + + assert {"testvmi": {"block_migratable_vmis": "BlockMigration"}} == host_vars + assert ["vmi_node_groups", "fedora_39"] == add_group + assert {"vmi_node_groups": ["testvmi"]} == add_child + assert ["testvmi", "fedora_39"] == add_host