|
| 1 | +import ipaddress |
| 2 | +from collections.abc import Generator |
| 3 | + |
| 4 | +import pytest |
| 5 | +from kubernetes.dynamic import DynamicClient |
| 6 | +from ocp_resources.namespace import Namespace |
| 7 | +from ocp_resources.node import Node |
| 8 | + |
| 9 | +import tests.network.libs.cluster_user_defined_network as libcudn |
| 10 | +import tests.network.libs.nodenetworkconfigurationpolicy as libnncp |
| 11 | +from libs.net.ip import cidr_addresses_by_family |
| 12 | +from libs.net.traffic_generator import TcpServer, VMTcpClient, active_tcp_connections |
| 13 | +from libs.net.vmspec import wait_for_ifaces_status |
| 14 | +from libs.vm.spec import Interface, Multus, Network |
| 15 | +from libs.vm.vm import BaseVirtualMachine |
| 16 | +from tests.network.libs import cloudinit |
| 17 | +from tests.network.libs.cloudinit import EthernetDevice |
| 18 | +from tests.network.localnet.liblocalnet import LOCALNET_BR_EX_INTERFACE, localnet_vm |
| 19 | + |
| 20 | +_LOCALNET_IFACE_GUEST_NAME = "eth1" |
| 21 | +_SERVER_HOST_ADDRESS = 1 |
| 22 | +_CLIENT_HOST_ADDRESS = 2 |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="module") |
| 26 | +def localnet_server_vm( |
| 27 | + unprivileged_client: DynamicClient, |
| 28 | + namespace_localnet_1: Namespace, |
| 29 | + cudn_localnet: libcudn.ClusterUserDefinedNetwork, |
| 30 | + nncp_localnet: libnncp.NodeNetworkConfigurationPolicy, |
| 31 | + rhcos9_node: Node, |
| 32 | +) -> Generator[BaseVirtualMachine]: |
| 33 | + addresses = cidr_addresses_by_family(net_seed=0, host_address=_SERVER_HOST_ADDRESS) |
| 34 | + with localnet_vm( |
| 35 | + namespace=namespace_localnet_1.name, |
| 36 | + name="server-vm", |
| 37 | + client=unprivileged_client, |
| 38 | + networks=[ |
| 39 | + Network(name="default", pod={}), |
| 40 | + Network(name=LOCALNET_BR_EX_INTERFACE, multus=Multus(networkName=cudn_localnet.name)), |
| 41 | + ], |
| 42 | + interfaces=[ |
| 43 | + Interface(name="default", masquerade={}), |
| 44 | + Interface(name=LOCALNET_BR_EX_INTERFACE, bridge={}), |
| 45 | + ], |
| 46 | + network_data=cloudinit.NetworkData(ethernets={_LOCALNET_IFACE_GUEST_NAME: EthernetDevice(addresses=addresses)}), |
| 47 | + node=rhcos9_node, |
| 48 | + ) as vm: |
| 49 | + vm.start(wait=True) |
| 50 | + vm.wait_for_agent_connected() |
| 51 | + wait_for_ifaces_status( |
| 52 | + vm=vm, |
| 53 | + ip_addresses_by_spec_net_name={ |
| 54 | + LOCALNET_BR_EX_INTERFACE: [str(ipaddress.ip_interface(addr).ip) for addr in addresses] |
| 55 | + }, |
| 56 | + ) |
| 57 | + yield vm |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture(scope="module") |
| 61 | +def localnet_client_vm( |
| 62 | + unprivileged_client: DynamicClient, |
| 63 | + namespace_localnet_1: Namespace, |
| 64 | + cudn_localnet: libcudn.ClusterUserDefinedNetwork, |
| 65 | + nncp_localnet: libnncp.NodeNetworkConfigurationPolicy, |
| 66 | + rhcos9_node: Node, |
| 67 | +) -> Generator[BaseVirtualMachine]: |
| 68 | + addresses = cidr_addresses_by_family(net_seed=0, host_address=_CLIENT_HOST_ADDRESS) |
| 69 | + with localnet_vm( |
| 70 | + namespace=namespace_localnet_1.name, |
| 71 | + name="client-vm", |
| 72 | + client=unprivileged_client, |
| 73 | + networks=[ |
| 74 | + Network(name="default", pod={}), |
| 75 | + Network(name=LOCALNET_BR_EX_INTERFACE, multus=Multus(networkName=cudn_localnet.name)), |
| 76 | + ], |
| 77 | + interfaces=[ |
| 78 | + Interface(name="default", masquerade={}), |
| 79 | + Interface(name=LOCALNET_BR_EX_INTERFACE, bridge={}), |
| 80 | + ], |
| 81 | + network_data=cloudinit.NetworkData(ethernets={_LOCALNET_IFACE_GUEST_NAME: EthernetDevice(addresses=addresses)}), |
| 82 | + node=rhcos9_node, |
| 83 | + ) as vm: |
| 84 | + vm.start(wait=True) |
| 85 | + vm.wait_for_agent_connected() |
| 86 | + wait_for_ifaces_status( |
| 87 | + vm=vm, |
| 88 | + ip_addresses_by_spec_net_name={ |
| 89 | + LOCALNET_BR_EX_INTERFACE: [str(ipaddress.ip_interface(addr).ip) for addr in addresses] |
| 90 | + }, |
| 91 | + ) |
| 92 | + yield vm |
| 93 | + |
| 94 | + |
| 95 | +@pytest.fixture(scope="module") |
| 96 | +def localnet_active_tcp_connection( |
| 97 | + localnet_client_vm: BaseVirtualMachine, |
| 98 | + localnet_server_vm: BaseVirtualMachine, |
| 99 | +) -> Generator[list[tuple[VMTcpClient, TcpServer]]]: |
| 100 | + with active_tcp_connections( |
| 101 | + client_vm=localnet_client_vm, |
| 102 | + server_vm=localnet_server_vm, |
| 103 | + iface_name=LOCALNET_BR_EX_INTERFACE, |
| 104 | + ) as connections: |
| 105 | + yield connections |
0 commit comments