mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-07-29 02:44:35 +00:00
Merge pull request #107 from jcanocan/fix-pr-96-issues
tests: Address comments of #96
This commit is contained in:
@@ -6,10 +6,12 @@ from __future__ import absolute_import, division, print_function
|
|||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
import copy
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from addict import Dict
|
from addict import Dict
|
||||||
import copy
|
|
||||||
|
|
||||||
from ansible_collections.kubevirt.core.plugins.inventory.kubevirt import (
|
from ansible_collections.kubevirt.core.plugins.inventory.kubevirt import (
|
||||||
GetVmiOptions,
|
GetVmiOptions,
|
||||||
@@ -41,8 +43,16 @@ BASE_VMI = {
|
|||||||
"spec": {
|
"spec": {
|
||||||
"domain": {"devices": {}},
|
"domain": {"devices": {}},
|
||||||
},
|
},
|
||||||
|
"status": {
|
||||||
|
"interfaces": [{"ipAddress": "10.10.10.10"}],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
# Note, if "interfaces": [{}] or "interfaces": [{"ipAddress": {}}] the if on line 517, problem?
|
NO_STATUS_VMI = merge_dicts(
|
||||||
|
BASE_VMI,
|
||||||
|
{
|
||||||
|
"status": None
|
||||||
|
}
|
||||||
|
)
|
||||||
VMI_WITH_INTERFACE_NO_IPADDRESS = merge_dicts(
|
VMI_WITH_INTERFACE_NO_IPADDRESS = merge_dicts(
|
||||||
BASE_VMI,
|
BASE_VMI,
|
||||||
{
|
{
|
||||||
@@ -51,16 +61,8 @@ VMI_WITH_INTERFACE_NO_IPADDRESS = merge_dicts(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
BASIC_VMI = merge_dicts(
|
|
||||||
BASE_VMI,
|
|
||||||
{
|
|
||||||
"status": {
|
|
||||||
"interfaces": [{"ipAddress": "10.10.10.10"}],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
WINDOWS_VMI_1 = merge_dicts(
|
WINDOWS_VMI_1 = merge_dicts(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
{
|
{
|
||||||
"status": {
|
"status": {
|
||||||
"guestOSInfo": {"id": "mswindows"},
|
"guestOSInfo": {"id": "mswindows"},
|
||||||
@@ -68,7 +70,7 @@ WINDOWS_VMI_1 = merge_dicts(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
WINDOWS_VMI_2 = merge_dicts(
|
WINDOWS_VMI_2 = merge_dicts(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"annotations": {"kubevirt.io/cluster-preference-name": "windows.2k22"}
|
"annotations": {"kubevirt.io/cluster-preference-name": "windows.2k22"}
|
||||||
@@ -76,22 +78,21 @@ WINDOWS_VMI_2 = merge_dicts(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
WINDOWS_VMI_3 = merge_dicts(
|
WINDOWS_VMI_3 = merge_dicts(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
{
|
{
|
||||||
"metadata": {"annotations": {"kubevirt.io/preference-name": "windows.2k22"}},
|
"metadata": {"annotations": {"kubevirt.io/preference-name": "windows.2k22"}},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
WINDOWS_VMI_4 = merge_dicts(
|
WINDOWS_VMI_4 = merge_dicts(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
{
|
{
|
||||||
"metadata": {"annotations": {"vm.kubevirt.io/os": "windows2k22"}},
|
"metadata": {"annotations": {"vm.kubevirt.io/os": "windows2k22"}},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
COMPLETE_VMI = merge_dicts(
|
COMPLETE_VMI = merge_dicts(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
# {"test-label": "test-label"} does not work
|
|
||||||
"annotations": {"test-annotation": "test-annotation"},
|
"annotations": {"test-annotation": "test-annotation"},
|
||||||
"clusterName": {"test-cluster"},
|
"clusterName": {"test-cluster"},
|
||||||
"resourceVersion": {"42"},
|
"resourceVersion": {"42"},
|
||||||
@@ -146,7 +147,7 @@ BASE_SERVICE = {
|
|||||||
},
|
},
|
||||||
"spec": {}
|
"spec": {}
|
||||||
}
|
}
|
||||||
LOADBALANCER_SERVICE_WITHOUT_AND_SELECTOR_PORTS = merge_dicts(
|
BASE_LOADBALANCER_SERVICE = merge_dicts(
|
||||||
BASE_SERVICE,
|
BASE_SERVICE,
|
||||||
{
|
{
|
||||||
"spec": {
|
"spec": {
|
||||||
@@ -155,7 +156,7 @@ LOADBALANCER_SERVICE_WITHOUT_AND_SELECTOR_PORTS = merge_dicts(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
LOADBALANCER_SERVICE_WITHOUT_SELECTOR_AND_SSH_PORT = merge_dicts(
|
LOADBALANCER_SERVICE_WITHOUT_SELECTOR_AND_SSH_PORT = merge_dicts(
|
||||||
LOADBALANCER_SERVICE_WITHOUT_AND_SELECTOR_PORTS,
|
BASE_LOADBALANCER_SERVICE,
|
||||||
{
|
{
|
||||||
"spec": {
|
"spec": {
|
||||||
"ports": [
|
"ports": [
|
||||||
@@ -170,7 +171,7 @@ LOADBALANCER_SERVICE_WITHOUT_SELECTOR_AND_SSH_PORT = merge_dicts(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
LOADBALANCER_SERVICE_WITHOUT_SELECTOR = merge_dicts(
|
LOADBALANCER_SERVICE_WITHOUT_SELECTOR = merge_dicts(
|
||||||
LOADBALANCER_SERVICE_WITHOUT_AND_SELECTOR_PORTS,
|
BASE_LOADBALANCER_SERVICE,
|
||||||
{
|
{
|
||||||
"spec": {
|
"spec": {
|
||||||
"ports": [
|
"ports": [
|
||||||
@@ -184,26 +185,16 @@ LOADBALANCER_SERVICE_WITHOUT_SELECTOR = merge_dicts(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
LOADBALANCER_SERVICE = {
|
LOADBALANCER_SERVICE = merge_dicts(
|
||||||
"apiVersion": "v1",
|
LOADBALANCER_SERVICE_WITHOUT_SELECTOR,
|
||||||
"kind": "Service",
|
{
|
||||||
"metadata": {
|
"spec": {
|
||||||
"name": "test-service"
|
"selector": {
|
||||||
},
|
"kubevirt.io/domain": "test-domain"
|
||||||
"spec": {
|
|
||||||
"selector": {
|
|
||||||
"kubevirt.io/domain": "test-domain"
|
|
||||||
},
|
|
||||||
"ports": [
|
|
||||||
{
|
|
||||||
"protocol": "TCP",
|
|
||||||
"port": 22,
|
|
||||||
"targetPort": 22
|
|
||||||
}
|
}
|
||||||
],
|
}
|
||||||
"type": "LoadBalancer"
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
NODEPORT_SERVICE = merge_dicts(
|
NODEPORT_SERVICE = merge_dicts(
|
||||||
LOADBALANCER_SERVICE,
|
LOADBALANCER_SERVICE,
|
||||||
{
|
{
|
||||||
@@ -404,7 +395,7 @@ def test_is_windows(inventory, guest_os_info, annotations, expected):
|
|||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"client,vmi,expected",
|
"client,vmi,expected",
|
||||||
[
|
[
|
||||||
({"vmis": [BASIC_VMI]}, BASIC_VMI, False),
|
({"vmis": [BASE_VMI]}, BASE_VMI, False),
|
||||||
({"vmis": [WINDOWS_VMI_1]}, WINDOWS_VMI_1, True),
|
({"vmis": [WINDOWS_VMI_1]}, WINDOWS_VMI_1, True),
|
||||||
({"vmis": [WINDOWS_VMI_2]}, WINDOWS_VMI_2, True),
|
({"vmis": [WINDOWS_VMI_2]}, WINDOWS_VMI_2, True),
|
||||||
({"vmis": [WINDOWS_VMI_3]}, WINDOWS_VMI_3, True),
|
({"vmis": [WINDOWS_VMI_3]}, WINDOWS_VMI_3, True),
|
||||||
@@ -544,24 +535,11 @@ def connection_none():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
|
||||||
def connection_not_list():
|
|
||||||
return "test"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
|
||||||
def connection_list_not_dict():
|
|
||||||
return [
|
|
||||||
"test",
|
|
||||||
"test"
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def connection_list():
|
def connection_list():
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
"name": "test",
|
"name": "connection-list",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -570,7 +548,7 @@ def connection_list():
|
|||||||
def connection_list_namespace():
|
def connection_list_namespace():
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
"name": "test",
|
"name": "connection-list-namespace",
|
||||||
"namespaces": ["test"]
|
"namespaces": ["test"]
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -580,7 +558,7 @@ def connection_list_namespace():
|
|||||||
def connection_with_base_values():
|
def connection_with_base_values():
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
"name": "test",
|
"name": "connection-with-base-values",
|
||||||
"namespaces": ["test"],
|
"namespaces": ["test"],
|
||||||
"use_service": True,
|
"use_service": True,
|
||||||
"create_groups": True,
|
"create_groups": True,
|
||||||
@@ -594,7 +572,7 @@ def connection_with_base_values():
|
|||||||
def connection_with_network():
|
def connection_with_network():
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
"name": "test",
|
"name": "connection-with-network",
|
||||||
"namespaces": ["test"],
|
"namespaces": ["test"],
|
||||||
"use_service": True,
|
"use_service": True,
|
||||||
"create_groups": True,
|
"create_groups": True,
|
||||||
@@ -609,7 +587,7 @@ def connection_with_network():
|
|||||||
def connection_with_interface():
|
def connection_with_interface():
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
"name": "test",
|
"name": "connection-with-interface",
|
||||||
"namespaces": ["test"],
|
"namespaces": ["test"],
|
||||||
"use_service": True,
|
"use_service": True,
|
||||||
"create_groups": True,
|
"create_groups": True,
|
||||||
@@ -650,11 +628,11 @@ def vmi_options_with_interface():
|
|||||||
"connections,result,default_namespace",
|
"connections,result,default_namespace",
|
||||||
[
|
[
|
||||||
|
|
||||||
("connection_list", {"name": "test", "namespace": DEFAULT_NAMESPACE, "opts": GetVmiOptions()}, True),
|
("connection_list", {"name": "connection-list", "namespace": DEFAULT_NAMESPACE, "opts": GetVmiOptions()}, True),
|
||||||
("connection_list_namespace", {"name": "test", "namespace": "test", "opts": GetVmiOptions()}, False),
|
("connection_list_namespace", {"name": "connection-list-namespace", "namespace": "test", "opts": GetVmiOptions()}, False),
|
||||||
("connection_with_base_values", {"name": "test", "namespace": "test", "opts": vmi_options_base_values()}, False),
|
("connection_with_base_values", {"name": "connection-with-base-values", "namespace": "test", "opts": vmi_options_base_values()}, False),
|
||||||
("connection_with_network", {"name": "test", "namespace": "test", "opts": vmi_options_with_network()}, False),
|
("connection_with_network", {"name": "connection-with-network", "namespace": "test", "opts": vmi_options_with_network()}, False),
|
||||||
("connection_with_interface", {"name": "test", "namespace": "test", "opts": vmi_options_with_interface()}, False),
|
("connection_with_interface", {"name": "connection-with-interface", "namespace": "test", "opts": vmi_options_with_interface()}, False),
|
||||||
("connection_none", {"name": "default-hostname", "namespace": DEFAULT_NAMESPACE, "opts": GetVmiOptions()}, True),
|
("connection_none", {"name": "default-hostname", "namespace": DEFAULT_NAMESPACE, "opts": GetVmiOptions()}, True),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -680,6 +658,19 @@ def test_fetch_objects(request, mocker, monkeypatch, inventory, connections, res
|
|||||||
result["opts"])
|
result["opts"])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def connection_not_list():
|
||||||
|
return "test"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def connection_list_not_dict():
|
||||||
|
return [
|
||||||
|
"test",
|
||||||
|
"test"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"connections,result",
|
"connections,result",
|
||||||
[
|
[
|
||||||
@@ -720,7 +711,6 @@ def get_multiple_namespace_list():
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# Exceptions?
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"client,result",
|
"client,result",
|
||||||
[
|
[
|
||||||
@@ -786,12 +776,12 @@ def inventory_groups_create_groups_option(inventory_groups):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def base_vmi_host_vars():
|
def empty_host_vars():
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def basic_vmi_host_vars():
|
def base_vmi_host_vars():
|
||||||
return {
|
return {
|
||||||
"default-testvmi": {
|
"default-testvmi": {
|
||||||
"object_type": "vmi",
|
"object_type": "vmi",
|
||||||
@@ -818,8 +808,8 @@ def basic_vmi_host_vars():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def complete_vmi_host_vars(basic_vmi_host_vars):
|
def complete_vmi_host_vars(base_vmi_host_vars):
|
||||||
vmi = basic_vmi_host_vars["default-testvmi"] | {
|
vmi = base_vmi_host_vars["default-testvmi"] | {
|
||||||
|
|
||||||
"labels": {"kubevirt.io/domain": "test-domain"},
|
"labels": {"kubevirt.io/domain": "test-domain"},
|
||||||
"annotations": {"test-annotation": "test-annotation"},
|
"annotations": {"test-annotation": "test-annotation"},
|
||||||
@@ -867,8 +857,8 @@ def complete_vmi_host_vars(basic_vmi_host_vars):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def windows_vmi_host_vars(basic_vmi_host_vars):
|
def windows_vmi_host_vars(base_vmi_host_vars):
|
||||||
vmi = basic_vmi_host_vars["default-testvmi"] | {
|
vmi = base_vmi_host_vars["default-testvmi"] | {
|
||||||
"ansible_connection": "winrm",
|
"ansible_connection": "winrm",
|
||||||
"vmi_guest_os_info": {"id": "mswindows"},
|
"vmi_guest_os_info": {"id": "mswindows"},
|
||||||
}
|
}
|
||||||
@@ -882,12 +872,13 @@ def windows_vmi_host_vars(basic_vmi_host_vars):
|
|||||||
"client,vmi,groups,vmi_group,child_group,create_groups,expected_host_vars,call_functions,windows",
|
"client,vmi,groups,vmi_group,child_group,create_groups,expected_host_vars,call_functions,windows",
|
||||||
[
|
[
|
||||||
(
|
(
|
||||||
{"vmis": [BASE_VMI]},
|
{"vmis": [NO_STATUS_VMI]},
|
||||||
BASE_VMI,
|
NO_STATUS_VMI,
|
||||||
"inventory_groups",
|
"inventory_groups",
|
||||||
"inventory_empty",
|
"inventory_empty",
|
||||||
"children_group_without_vmi",
|
"children_group_without_vmi",
|
||||||
False, "base_vmi_host_vars",
|
False,
|
||||||
|
"empty_host_vars",
|
||||||
False,
|
False,
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
@@ -898,18 +889,18 @@ def windows_vmi_host_vars(basic_vmi_host_vars):
|
|||||||
"inventory_empty",
|
"inventory_empty",
|
||||||
"children_group_without_vmi",
|
"children_group_without_vmi",
|
||||||
False,
|
False,
|
||||||
"base_vmi_host_vars",
|
"empty_host_vars",
|
||||||
False,
|
False,
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
{"vmis": [BASIC_VMI], "services": [LOADBALANCER_SERVICE]},
|
{"vmis": [BASE_VMI], "services": [LOADBALANCER_SERVICE]},
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
"inventory_groups",
|
"inventory_groups",
|
||||||
"inventory_vmi_group",
|
"inventory_vmi_group",
|
||||||
"children_group_with_vmi",
|
"children_group_with_vmi",
|
||||||
False,
|
False,
|
||||||
"basic_vmi_host_vars",
|
"base_vmi_host_vars",
|
||||||
True,
|
True,
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
@@ -990,6 +981,9 @@ def test_get_vmis_for_namespace(mocker,
|
|||||||
GetVmiOptions(create_groups=create_groups)
|
GetVmiOptions(create_groups=create_groups)
|
||||||
)
|
)
|
||||||
set_composable_vars.assert_called_once_with(vmi_name)
|
set_composable_vars.assert_called_once_with(vmi_name)
|
||||||
|
else:
|
||||||
|
set_composable_vars.assert_not_called()
|
||||||
|
set_ansible_host_and_port.asser_not_called()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
@@ -1046,7 +1040,7 @@ def empty_service():
|
|||||||
"empty_service",
|
"empty_service",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
{"services": [LOADBALANCER_SERVICE_WITHOUT_AND_SELECTOR_PORTS], "namespaces": [{"metadata": {"name": DEFAULT_NAMESPACE}}]},
|
{"services": [BASE_LOADBALANCER_SERVICE], "namespaces": [{"metadata": {"name": DEFAULT_NAMESPACE}}]},
|
||||||
"empty_service",
|
"empty_service",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@@ -1108,24 +1102,24 @@ def ansible_host_and_port_service_ip():
|
|||||||
"vmi,use_service,opts,result",
|
"vmi,use_service,opts,result",
|
||||||
[
|
[
|
||||||
(
|
(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
None,
|
None,
|
||||||
GetVmiOptions(kube_secondary_dns=True, network_name="test-network"),
|
GetVmiOptions(kube_secondary_dns=True, network_name="test-network"),
|
||||||
"ansible_host_and_port_network",
|
"ansible_host_and_port_network",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
None,
|
None,
|
||||||
GetVmiOptions(kube_secondary_dns=True, network_name="test-network", base_domain=DEFAULT_BASE_DOMAIN),
|
GetVmiOptions(kube_secondary_dns=True, network_name="test-network", base_domain=DEFAULT_BASE_DOMAIN),
|
||||||
"ansible_host_and_port_network_with_base_domain",
|
"ansible_host_and_port_network_with_base_domain",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
{"host": "test-host", "port": "8080"}, GetVmiOptions(use_service=True),
|
{"host": "test-host", "port": "8080"}, GetVmiOptions(use_service=True),
|
||||||
"ansible_host_and_port_service",
|
"ansible_host_and_port_service",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
BASIC_VMI,
|
BASE_VMI,
|
||||||
{"host": None, "port": None}, GetVmiOptions(use_service=True),
|
{"host": None, "port": None}, GetVmiOptions(use_service=True),
|
||||||
"ansible_host_and_port_service_ip",
|
"ansible_host_and_port_service_ip",
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user