Skip to content

Commit cf11c35

Browse files
committed
Manual cherry-pick 4.21: Drop cluster net stack fixtures (#4365)
Both ipv4_supported_cluster and ipv6_supported_cluster fixtures are used frequently as parameters. For cleaner data usage, these fixtures can be removed and replaced with cached helper functions that retrieve the cluster network stack once. Signed-off-by: Asia Khromov <azhivovk@redhat.com>
1 parent 9fa8eb6 commit cf11c35

File tree

9 files changed

+71
-97
lines changed

9 files changed

+71
-97
lines changed

conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from _pytest.reports import CollectReport, TestReport
2222
from _pytest.runner import CallInfo
2323
from kubernetes.dynamic.exceptions import ConflictError
24+
from ocp_resources.network_config_openshift_io import Network
2425
from pyhelper_utils.shell import run_command
2526
from pytest import Item
2627
from pytest_testconfig import config as py_config
@@ -830,6 +831,9 @@ def _update_os_related_config():
830831
if not skip_if_pytest_flags_exists(pytest_config=session.config):
831832
admin_client = utilities.cluster.cache_admin_client()
832833
py_config["version_explorer_url"] = get_cnv_version_explorer_url(pytest_config=session.config)
834+
py_config["cluster_service_network"] = Network(
835+
client=admin_client, name="cluster"
836+
).instance.status.serviceNetwork
833837
if not session.config.getoption("--skip-artifactory-check"):
834838
py_config["server_url"] = py_config["server_url"] or get_artifactory_server_url(
835839
cluster_host_url=admin_client.configuration.host, session=session

libs/net/cluster.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import ipaddress
2+
import logging
3+
from functools import cache
4+
5+
from pytest_testconfig import py_config
6+
7+
LOGGER = logging.getLogger(__name__)
8+
9+
10+
@cache
11+
def is_ipv6_single_stack_cluster() -> bool:
12+
ipv4_supported = ipv4_supported_cluster()
13+
ipv6_supported = ipv6_supported_cluster()
14+
15+
is_ipv6_only = ipv6_supported and not ipv4_supported
16+
LOGGER.info(f"Cluster network detection: IPv4={ipv4_supported}, IPv6={ipv6_supported}, IPv6-only={is_ipv6_only}")
17+
return is_ipv6_only
18+
19+
20+
@cache
21+
def ipv4_supported_cluster() -> bool:
22+
return _cluster_ip_family_supported(ip_family=4)
23+
24+
25+
@cache
26+
def ipv6_supported_cluster() -> bool:
27+
return _cluster_ip_family_supported(ip_family=6)
28+
29+
30+
def _cluster_ip_family_supported(ip_family: int) -> bool:
31+
return any(ipaddress.ip_network(ip).version == ip_family for ip in py_config.get("cluster_service_network"))

tests/conftest.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import copy
6-
import ipaddress
76
import logging
87
import os
98
import os.path
@@ -42,7 +41,6 @@
4241
from ocp_resources.mutating_webhook_config import MutatingWebhookConfiguration
4342
from ocp_resources.namespace import Namespace
4443
from ocp_resources.network_addons_config import NetworkAddonsConfig
45-
from ocp_resources.network_config_openshift_io import Network
4644
from ocp_resources.node import Node
4745
from ocp_resources.node_network_state import NodeNetworkState
4846
from ocp_resources.oauth import OAuth
@@ -71,6 +69,7 @@
7169
from timeout_sampler import TimeoutSampler
7270

7371
import utilities.hco
72+
from libs.net.cluster import ipv4_supported_cluster, ipv6_supported_cluster
7473
from tests.utils import download_and_extract_tar
7574
from utilities.artifactory import get_artifactory_header, get_http_image_url, get_test_artifact_server_url
7675
from utilities.bitwarden import get_cnv_tests_secret_by_name
@@ -1437,8 +1436,6 @@ def cluster_info(
14371436
hco_image,
14381437
ocs_current_version,
14391438
kubevirt_resource_scope_session,
1440-
ipv6_supported_cluster,
1441-
ipv4_supported_cluster,
14421439
workers_type,
14431440
nodes_cpu_architecture,
14441441
):
@@ -1458,8 +1455,8 @@ def cluster_info(
14581455
f"\tCNI type: {get_cluster_cni_type(admin_client=admin_client)}\n"
14591456
f"\tWorkers type: {workers_type}\n"
14601457
f"\tCluster CPU Architecture: {nodes_cpu_architecture}\n"
1461-
f"\tIPv4 cluster: {ipv4_supported_cluster}\n"
1462-
f"\tIPv6 cluster: {ipv6_supported_cluster}\n"
1458+
f"\tIPv4 cluster: {ipv4_supported_cluster()}\n"
1459+
f"\tIPv6 cluster: {ipv6_supported_cluster()}\n"
14631460
f"\tVirtctl version: \n\t{virtctl_client_version}\n\t{virtctl_server_version}\n"
14641461
)
14651462

@@ -1794,23 +1791,6 @@ def ssp_resource_scope_function(admin_client, hco_namespace):
17941791
return get_ssp_resource(admin_client=admin_client, namespace=hco_namespace)
17951792

17961793

1797-
@pytest.fixture(scope="session")
1798-
def cluster_service_network(admin_client):
1799-
return Network(client=admin_client, name="cluster").instance.status.serviceNetwork
1800-
1801-
1802-
@pytest.fixture(scope="session")
1803-
def ipv4_supported_cluster(cluster_service_network):
1804-
if cluster_service_network:
1805-
return any([ipaddress.ip_network(ip).version == 4 for ip in cluster_service_network])
1806-
1807-
1808-
@pytest.fixture(scope="session")
1809-
def ipv6_supported_cluster(cluster_service_network):
1810-
if cluster_service_network:
1811-
return any([ipaddress.ip_network(ip).version == 6 for ip in cluster_service_network])
1812-
1813-
18141794
@pytest.fixture()
18151795
def disabled_common_boot_image_import_hco_spec_scope_function(
18161796
admin_client,
@@ -2574,11 +2554,6 @@ def nmstate_namespace(admin_client):
25742554
return None
25752555

25762556

2577-
@pytest.fixture()
2578-
def ipv6_single_stack_cluster(ipv4_supported_cluster, ipv6_supported_cluster):
2579-
return ipv6_supported_cluster and not ipv4_supported_cluster
2580-
2581-
25822557
@pytest.fixture(scope="class")
25832558
def ping_process_in_rhel_os():
25842559
def _start_ping(vm):

tests/network/conftest.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pytest_testconfig import config as py_config
1717
from timeout_sampler import TimeoutExpiredError
1818

19+
from libs.net.cluster import ipv4_supported_cluster, ipv6_supported_cluster
1920
from tests.network.utils import get_vlan_index_number
2021
from utilities.constants import (
2122
CLUSTER,
@@ -59,22 +60,15 @@ def virt_handler_pod(admin_client):
5960
raise ResourceNotFoundError(f"No {VIRT_HANDLER} Pod found.")
6061

6162

62-
@pytest.fixture(scope="session")
63-
def dual_stack_cluster(ipv4_supported_cluster, ipv6_supported_cluster):
64-
return ipv4_supported_cluster and ipv6_supported_cluster
65-
66-
6763
@pytest.fixture(scope="module")
68-
def ipv6_primary_interface_cloud_init_data(
69-
ipv4_supported_cluster: bool, ipv6_supported_cluster: bool
70-
) -> dict[str, dict] | None:
71-
if ipv6_supported_cluster:
64+
def ipv6_primary_interface_cloud_init_data() -> dict[str, dict] | None:
65+
if ipv6_supported_cluster():
7266
return {
7367
"ethernets": {
7468
"eth0": {
7569
"addresses": ["fd10:0:2::2/120"],
7670
"gateway6": "fd10:0:2::1",
77-
"dhcp4": ipv4_supported_cluster,
71+
"dhcp4": ipv4_supported_cluster(),
7872
"dhcp6": False,
7973
},
8074
},
@@ -174,8 +168,6 @@ def network_sanity(
174168
cluster_network_mtu,
175169
network_overhead,
176170
sriov_workers,
177-
ipv4_supported_cluster,
178-
ipv6_supported_cluster,
179171
conformance_tests,
180172
nmstate_namespace,
181173
mtv_namespace_scope_session,
@@ -313,8 +305,8 @@ def _verify_mtv_installed():
313305
_verify_service_mesh()
314306
_verify_jumbo_frame()
315307
_verify_sriov()
316-
_verify_ip_family(family="ipv4", is_supported_in_cluster=ipv4_supported_cluster)
317-
_verify_ip_family(family="ipv6", is_supported_in_cluster=ipv6_supported_cluster)
308+
_verify_ip_family(family="ipv4", is_supported_in_cluster=ipv4_supported_cluster())
309+
_verify_ip_family(family="ipv6", is_supported_in_cluster=ipv6_supported_cluster())
318310
_verify_nmstate_running_pods(_admin_client=admin_client, namespace=nmstate_namespace)
319311
_verify_mtv_installed()
320312

tests/network/connectivity/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from libs.net.cluster import ipv6_supported_cluster
34
from tests.network.connectivity.utils import create_running_vm
45
from utilities.constants import LINUX_BRIDGE, OVS_BRIDGE
56
from utilities.data_utils import name_prefix
@@ -28,8 +29,8 @@ def vlan_id_3(vlan_index_number):
2829

2930

3031
@pytest.fixture()
31-
def fail_if_not_ipv6_supported_cluster(ipv6_supported_cluster):
32-
if not ipv6_supported_cluster:
32+
def fail_if_not_ipv6_supported_cluster():
33+
if not ipv6_supported_cluster():
3334
pytest.fail(reason="IPv6 is not supported in this cluster")
3435

3536

tests/network/general/test_ip_family_services.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88

9+
from libs.net.cluster import ipv4_supported_cluster, ipv6_supported_cluster
910
from tests.network.utils import basic_expose_command, get_service
1011
from utilities.constants import SSH_PORT_22
1112
from utilities.infra import get_node_selector_dict, run_virtctl_command
@@ -75,10 +76,11 @@ def virtctl_expose_service(
7576
request,
7677
unprivileged_client,
7778
running_vm_for_exposure,
78-
dual_stack_cluster,
7979
):
8080
ip_family_policy = request.param
81-
if ip_family_policy == SERVICE_IP_FAMILY_POLICY_REQUIRE_DUAL_STACK and not dual_stack_cluster:
81+
if ip_family_policy == SERVICE_IP_FAMILY_POLICY_REQUIRE_DUAL_STACK and not (
82+
ipv4_supported_cluster() and ipv6_supported_cluster()
83+
):
8284
pytest.skip(
8385
f"{SERVICE_IP_FAMILY_POLICY_REQUIRE_DUAL_STACK} service cannot be created in a non-dual-stack cluster."
8486
)
@@ -98,9 +100,11 @@ def virtctl_expose_service(
98100

99101

100102
@pytest.fixture()
101-
def expected_num_families_in_service(request, dual_stack_cluster):
103+
def expected_num_families_in_service(request):
102104
ip_family_policy = request.param
103-
if ip_family_policy != SERVICE_IP_FAMILY_POLICY_SINGLE_STACK and dual_stack_cluster:
105+
if ip_family_policy != SERVICE_IP_FAMILY_POLICY_SINGLE_STACK and (
106+
ipv4_supported_cluster() and ipv6_supported_cluster()
107+
):
104108
return 2
105109
return 1
106110

@@ -165,7 +169,6 @@ def test_vitrctl_expose_services(
165169
expected_num_families_in_service,
166170
running_vm_for_exposure,
167171
virtctl_expose_service,
168-
dual_stack_cluster,
169172
ip_family_policy,
170173
):
171174
assert_svc_ip_params(

tests/network/l2_bridge/vmi_interfaces_stability/conftest.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
@pytest.fixture(scope="class")
1818
def running_linux_bridge_vm(
19-
ipv4_supported_cluster: bool,
20-
ipv6_supported_cluster: bool,
2119
unprivileged_client: DynamicClient,
2220
namespace: Namespace,
2321
bridge_nad: NetworkAttachmentDefinition,
@@ -27,8 +25,6 @@ def running_linux_bridge_vm(
2725
name="vm-iface-stability",
2826
client=unprivileged_client,
2927
bridge_network_name=bridge_nad.name,
30-
ipv4_supported_cluster=ipv4_supported_cluster,
31-
ipv6_supported_cluster=ipv6_supported_cluster,
3228
) as vm:
3329
vm.start(wait=True)
3430
vm.wait_for_agent_connected()

tests/network/l2_bridge/vmi_interfaces_stability/lib_helpers.py

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from kubernetes.dynamic.resource import ResourceField
88
from ocp_resources.virtual_machine_instance import VirtualMachineInstance
99

10+
from libs.net.cluster import ipv4_supported_cluster, ipv6_supported_cluster
1011
from libs.net.ip import random_ipv4_address, random_ipv6_address
1112
from libs.net.vmspec import lookup_iface_status, lookup_primary_network
1213
from libs.vm.factory import base_vmspec, fedora_vm
@@ -26,8 +27,6 @@ def secondary_network_vm(
2627
name: str,
2728
client: DynamicClient,
2829
bridge_network_name: str,
29-
ipv4_supported_cluster: bool,
30-
ipv6_supported_cluster: bool,
3130
) -> BaseVirtualMachine:
3231
spec = base_vmspec()
3332
spec.template.spec.domain.devices.interfaces = [ # type: ignore
@@ -42,24 +41,13 @@ def secondary_network_vm(
4241
]
4342

4443
ethernets = {}
45-
primary = primary_iface_cloud_init(
46-
ipv4_supported_cluster=ipv4_supported_cluster,
47-
ipv6_supported_cluster=ipv6_supported_cluster,
48-
)
44+
primary = primary_iface_cloud_init()
4945
if primary:
5046
ethernets["eth1"] = primary
5147

52-
ethernets["eth0"] = secondary_iface_cloud_init(
53-
ipv4_supported_cluster=ipv4_supported_cluster,
54-
ipv6_supported_cluster=ipv6_supported_cluster,
55-
host_address=1,
56-
)
48+
ethernets["eth0"] = secondary_iface_cloud_init(host_address=1)
5749

58-
ethernets["eth2"] = secondary_iface_cloud_init(
59-
ipv4_supported_cluster=ipv4_supported_cluster,
60-
ipv6_supported_cluster=ipv6_supported_cluster,
61-
host_address=2,
62-
)
50+
ethernets["eth2"] = secondary_iface_cloud_init(host_address=2)
6351

6452
userdata = cloudinit.UserData(users=[])
6553
disk, volume = cloudinitdisk_storage(
@@ -73,50 +61,33 @@ def secondary_network_vm(
7361
return fedora_vm(namespace=namespace, name=name, client=client, spec=spec)
7462

7563

76-
def primary_iface_cloud_init(
77-
ipv4_supported_cluster: bool,
78-
ipv6_supported_cluster: bool,
79-
) -> cloudinit.EthernetDevice | None:
80-
if not ipv6_supported_cluster:
64+
def primary_iface_cloud_init() -> cloudinit.EthernetDevice | None:
65+
if not ipv6_supported_cluster():
8166
return None
8267
return cloudinit.EthernetDevice(
8368
addresses=["fd10:0:2::2/120"],
8469
gateway6="fd10:0:2::1",
85-
dhcp4=ipv4_supported_cluster,
70+
dhcp4=ipv4_supported_cluster(),
8671
dhcp6=False,
8772
)
8873

8974

90-
def secondary_iface_cloud_init(
91-
ipv4_supported_cluster: bool,
92-
ipv6_supported_cluster: bool,
93-
host_address: int,
94-
) -> cloudinit.EthernetDevice:
95-
ips = secondary_iface_ips(
96-
ipv4_supported_cluster=ipv4_supported_cluster,
97-
ipv6_supported_cluster=ipv6_supported_cluster,
98-
host_address=host_address,
99-
)
75+
def secondary_iface_cloud_init(host_address: int) -> cloudinit.EthernetDevice:
76+
ips = secondary_iface_ips(host_address=host_address)
10077
addresses = [f"{ip}/64" if ipaddress.ip_address(ip).version == 6 else f"{ip}/24" for ip in ips]
10178
return cloudinit.EthernetDevice(addresses=addresses)
10279

10380

104-
def secondary_iface_ips(
105-
ipv4_supported_cluster: bool,
106-
ipv6_supported_cluster: bool,
107-
host_address: int,
108-
) -> list[str]:
81+
def secondary_iface_ips(host_address: int) -> list[str]:
10982
ips = []
110-
if ipv4_supported_cluster:
83+
if ipv4_supported_cluster():
11184
ips.append(random_ipv4_address(net_seed=0, host_address=host_address))
112-
if ipv6_supported_cluster:
85+
if ipv6_supported_cluster():
11386
ips.append(random_ipv6_address(net_seed=0, host_address=host_address))
11487
return ips
11588

11689

117-
def wait_for_stable_ifaces(
118-
vm: BaseVirtualMachine,
119-
) -> None:
90+
def wait_for_stable_ifaces(vm: BaseVirtualMachine) -> None:
12091
primary_network = lookup_primary_network(vm=vm)
12192

12293
secondary_iface_to_ips = {

tests/observability/metrics/test_general_metrics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ocp_resources.resource import Resource
55
from ocp_resources.virtual_machine import VirtualMachine
66

7+
from libs.net.cluster import is_ipv6_single_stack_cluster
78
from tests.observability.metrics.constants import KUBEVIRT_VMI_NODE_CPU_AFFINITY
89
from tests.observability.metrics.utils import validate_vmi_node_cpu_affinity_with_prometheus
910
from tests.observability.utils import validate_metrics_value
@@ -90,9 +91,9 @@ class TestVirtHCOSingleStackIpv6:
9091
@pytest.mark.ipv6
9192
@pytest.mark.polarion("CNV-11740")
9293
@pytest.mark.s390x
93-
def test_metric_kubevirt_hco_single_stack_ipv6(self, prometheus, ipv6_single_stack_cluster):
94+
def test_metric_kubevirt_hco_single_stack_ipv6(self, prometheus):
9495
validate_metrics_value(
9596
prometheus=prometheus,
9697
metric_name="kubevirt_hco_single_stack_ipv6",
97-
expected_value="1" if ipv6_single_stack_cluster else "0",
98+
expected_value="1" if is_ipv6_single_stack_cluster() else "0",
9899
)

0 commit comments

Comments
 (0)