cleanup(tests): Major rework of inventory unit tests

Rework the inventory unit tests by splitting up
tests/unit/plugins/inventory/test_kubevirt.py into multiple files,
by trying to simplify the test code and making it more robust and by
using appropriate fixtures. This also adds new tests or test cases to
improve code coverage. Tests that work from the black box perspective
are now located in a subdirectory.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2024-06-27 13:34:07 +02:00
parent a94eda613f
commit be65833724
18 changed files with 1927 additions and 1087 deletions

View File

@@ -0,0 +1,80 @@
# -*- 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 (
InventoryOptions,
)
from ansible_collections.kubevirt.core.tests.unit.plugins.inventory.constants import (
DEFAULT_NAMESPACE,
merge_dicts,
)
BASE_VMI = {
"metadata": {
"name": "testvmi",
"namespace": "default",
},
"status": {
"interfaces": [{"ipAddress": "10.10.10.10"}],
},
}
WINDOWS_VMI_1 = merge_dicts(
BASE_VMI,
{
"status": {
"guestOSInfo": {"id": "mswindows"},
}
},
)
WINDOWS_VMI_2 = merge_dicts(
BASE_VMI,
{
"metadata": {
"annotations": {"kubevirt.io/cluster-preference-name": "windows.2k22"}
},
},
)
WINDOWS_VMI_3 = merge_dicts(
BASE_VMI,
{
"metadata": {"annotations": {"kubevirt.io/preference-name": "windows.2k22"}},
},
)
WINDOWS_VMI_4 = merge_dicts(
BASE_VMI,
{
"metadata": {"annotations": {"vm.kubevirt.io/os": "windows2k22"}},
},
)
@pytest.mark.parametrize(
"client,vmi,expected",
[
({"vmis": [BASE_VMI]}, BASE_VMI, False),
({"vmis": [WINDOWS_VMI_1]}, WINDOWS_VMI_1, True),
({"vmis": [WINDOWS_VMI_2]}, WINDOWS_VMI_2, True),
({"vmis": [WINDOWS_VMI_3]}, WINDOWS_VMI_3, True),
({"vmis": [WINDOWS_VMI_4]}, WINDOWS_VMI_4, True),
],
indirect=["client"],
)
def test_ansible_connection_winrm(inventory, hosts, client, vmi, expected):
inventory.populate_inventory_from_namespace(
client, "", DEFAULT_NAMESPACE, InventoryOptions()
)
host = f"{DEFAULT_NAMESPACE}-{vmi['metadata']['name']}"
if expected:
assert hosts[host]["ansible_connection"] == "winrm"
else:
assert "ansible_connection" not in hosts[host]

View File

@@ -0,0 +1,63 @@
# -*- 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 (
InventoryOptions,
)
from ansible_collections.kubevirt.core.tests.unit.plugins.inventory.constants import (
DEFAULT_NAMESPACE,
)
VMI = {
"metadata": {
"name": "testvmi",
"namespace": "default",
},
"status": {
"interfaces": [{"ipAddress": "10.10.10.10"}],
"migrationMethod": "BlockMigration",
"nodeName": "test-node",
"guestOSInfo": {
"id": "fedora",
"versionId": "40",
},
},
}
@pytest.mark.parametrize(
"client",
[{"vmis": [VMI]}],
indirect=["client"],
)
def test_set_composable_vars(
inventory,
groups,
hosts,
client,
):
inventory._options = {
"compose": {"set_from_another_var": "vmi_node_name"},
"groups": {"block_migratable_vmis": "vmi_migration_method == 'BlockMigration'"},
"keyed_groups": [{"prefix": "fedora", "key": "vmi_guest_os_info.versionId"}],
"strict": True,
}
inventory.populate_inventory_from_namespace(
client, "", DEFAULT_NAMESPACE, InventoryOptions()
)
host = f"{DEFAULT_NAMESPACE}-testvmi"
assert hosts[host]["set_from_another_var"] == "test-node"
assert "block_migratable_vmis" in groups
assert host in groups["block_migratable_vmis"]["children"]
assert "fedora_40" in groups
assert host in groups["fedora_40"]["children"]