From ca3010ac1dbd79b53d93b4d04823774903a39a39 Mon Sep 17 00:00:00 2001 From: Geetika Kapoor Date: Wed, 10 Jun 2026 11:31:54 +0100 Subject: [PATCH] tests(inventory): cover existing ansible_connection and service selection - Assert default_win_ansible_connection is not applied when the host already has ansible_connection. - Matching Service is used when SSH and WinRM LoadBalancer services co-exist. Signed-off-by: Geetika Kapoor --- .../test_kubevirt_win_ansible_connection.py | 70 +++++++++++++++++++ .../test_kubevirt_set_vars_from_vmi.py | 50 +++++++++++++ 2 files changed, 120 insertions(+) diff --git a/tests/unit/plugins/inventory/blackbox/test_kubevirt_win_ansible_connection.py b/tests/unit/plugins/inventory/blackbox/test_kubevirt_win_ansible_connection.py index 3735c22..1dcbb13 100644 --- a/tests/unit/plugins/inventory/blackbox/test_kubevirt_win_ansible_connection.py +++ b/tests/unit/plugins/inventory/blackbox/test_kubevirt_win_ansible_connection.py @@ -96,6 +96,34 @@ SVC_LB_SSH = merge_dicts( }, }, ) +SVC_LB_SSH_2222 = merge_dicts( + SVC_LB_SSH, + { + "spec": { + "ports": [ + { + "protocol": "TCP", + "port": 2222, + "targetPort": 22, + }, + ], + }, + }, +) +SVC_LB_WINRM_HTTPS_59861 = merge_dicts( + SVC_LB_WINRM_HTTPS, + { + "spec": { + "ports": [ + { + "protocol": "TCP", + "port": 59861, + "targetPort": 5986, + }, + ], + }, + }, +) @pytest.mark.parametrize( @@ -155,3 +183,45 @@ def test_default_win_ansible_connection( else: assert hosts[host]["ansible_host"] == "10.10.10.10" assert hosts[host]["ansible_port"] is None + + +@pytest.mark.parametrize( + "default_win_ansible_connection,expected_port", + [ + ("winrm", 59861), + ("ssh", 2222), + ], +) +def test_default_win_ansible_connection_picks_service_by_protocol( + inventory, + hosts, + default_win_ansible_connection, + expected_port, +): + inventory._populate_inventory( + { + "default_hostname": "test", + "cluster_domain": "test.com", + "namespaces": { + "default": { + "vms": [], + "vmis": [WINDOWS_VMI_1], + "services": { + "testdomain": [ + SVC_LB_SSH_2222, + SVC_LB_WINRM_HTTPS_59861, + ] + }, + } + }, + }, + InventoryOptions( + use_service=True, + default_win_ansible_connection=default_win_ansible_connection, + ), + ) + + host = f"{DEFAULT_NAMESPACE}-{WINDOWS_VMI_1['metadata']['name']}" + assert hosts[host]["ansible_connection"] == default_win_ansible_connection + assert hosts[host]["ansible_host"] == "192.168.1.100" + assert hosts[host]["ansible_port"] == expected_port diff --git a/tests/unit/plugins/inventory/test_kubevirt_set_vars_from_vmi.py b/tests/unit/plugins/inventory/test_kubevirt_set_vars_from_vmi.py index 8845ecd..58977df 100644 --- a/tests/unit/plugins/inventory/test_kubevirt_set_vars_from_vmi.py +++ b/tests/unit/plugins/inventory/test_kubevirt_set_vars_from_vmi.py @@ -120,6 +120,56 @@ def test_set_connection_if_windows(mocker, inventory, default_win_ansible_connec ) +@pytest.mark.parametrize( + "existing_ansible_connection,default_win_ansible_connection,target_port", + [ + ("ssh", "winrm", 22), + ("winrm", "ssh", 5986), + ], +) +def test_respect_existing_ansible_connection( + mocker, + inventory, + existing_ansible_connection, + default_win_ansible_connection, + target_port, +): + mocker.patch.object(inventory, "_set_common_vars") + mocker.patch.object(inventory, "_is_windows", return_value=True) + set_ansible_host_and_port = mocker.patch.object( + inventory, "_set_ansible_host_and_port" + ) + mocker.patch.object( + inventory.inventory, + "get_host", + return_value=mocker.Mock( + get_vars=mocker.Mock( + return_value={"ansible_connection": existing_ansible_connection} + ) + ), + ) + set_variable = mocker.patch.object(inventory.inventory, "set_variable") + + hostname = "default-testvm" + vmi = { + "metadata": {"labels": {LABEL_KUBEVIRT_IO_DOMAIN: "testdomain"}}, + "status": {"interfaces": [{"ipAddress": "1.1.1.1"}]}, + } + service = { + "metadata": {"name": "testsvc"}, + "spec": {"ports": [{"targetPort": target_port}]}, + } + opts = InventoryOptions( + default_win_ansible_connection=default_win_ansible_connection + ) + inventory._set_vars_from_vmi(hostname, vmi, {"testdomain": [service]}, opts) + + set_variable.assert_not_called() + set_ansible_host_and_port.assert_called_once_with( + vmi, hostname, "1.1.1.1", service, opts + ) + + @pytest.mark.parametrize( "is_windows,target_port", [