mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-03-27 03:13:10 +00:00
test(kubevirt): add inventory unit tests
It adds unit tests for the following functions: - set_composable_vars - format_dynamic_api_exc Signed-off-by: Javier Cano Cano <jcanocan@redhat.com>
This commit is contained in:
@@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function
|
|||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import copy
|
from json import dumps
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -14,6 +14,7 @@ from addict import Dict
|
|||||||
|
|
||||||
|
|
||||||
from ansible_collections.kubevirt.core.plugins.inventory.kubevirt import (
|
from ansible_collections.kubevirt.core.plugins.inventory.kubevirt import (
|
||||||
|
DynamicApiError,
|
||||||
GetVmiOptions,
|
GetVmiOptions,
|
||||||
InventoryModule,
|
InventoryModule,
|
||||||
KubeVirtInventoryException,
|
KubeVirtInventoryException,
|
||||||
@@ -772,7 +773,7 @@ def children_group_with_vmi_create_groups_option(children_group_with_vmi):
|
|||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
def inventory_groups_create_groups_option(inventory_groups):
|
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")
|
inv.append("label_kubevirt_io_domain_test_domain")
|
||||||
return inv
|
return inv
|
||||||
|
|
||||||
@@ -1016,7 +1017,7 @@ def loadbalancer():
|
|||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def nodeport(loadbalancer):
|
def nodeport(loadbalancer):
|
||||||
np = copy.deepcopy(loadbalancer)
|
np = deepcopy(loadbalancer)
|
||||||
np["test-domain"]["spec"]["type"] = "NodePort"
|
np["test-domain"]["spec"]["type"] = "NodePort"
|
||||||
return np
|
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)
|
inventory.set_ansible_host_and_port(Dict(vmi), vmi_name, "10.10.10.10", service, opts)
|
||||||
|
|
||||||
assert request.getfixturevalue(result) == host_vars
|
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
|
||||||
|
|||||||
119
tests/unit/plugins/inventory/test_kubevirt_composable_vars.py
Normal file
119
tests/unit/plugins/inventory/test_kubevirt_composable_vars.py
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user