Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions tests/network/l2_bridge/libl2bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Final

from kubernetes.dynamic import DynamicClient
from kubernetes.dynamic.client import ResourceField
Comment thread
yossisegev marked this conversation as resolved.
from ocp_resources.resource import ResourceEditor
from timeout_sampler import TimeoutExpiredError, TimeoutSampler

Expand Down Expand Up @@ -202,21 +203,21 @@ def create_bridge_interface_for_hot_plug(
yield br


def set_secondary_static_ip_address(vm, ipv4_address, vmi_interface):
guest_vm_interface = get_guest_vm_interface_name_by_vmi_interface_name(
Comment thread
yossisegev marked this conversation as resolved.
vm=vm,
vm_interface_name=vmi_interface,
def set_secondary_static_ip_address(
vm: VirtualMachineForTests, ipv4_address: str, vmi_interface: ResourceField
) -> None:
console_command = (
f"sudo ip addr add {ipv4_address}/{IPV4_ADDRESS_SUBNET_PREFIX_LENGTH} dev {vmi_interface.interfaceName}"
)
console_command = f"sudo ip addr add {ipv4_address}/{IPV4_ADDRESS_SUBNET_PREFIX_LENGTH} dev {guest_vm_interface}"
LOGGER.info(f"Sending command to {vm.name} console: '{console_command}'")
with console.Console(vm=vm) as vm_console:
vm_console.sendline(console_command)

# Verify the IP address was set successfully.
# The function fails on timeout if the interface or its address are not found,
# so there's no need to check its return code.
hot_plugged_interface_ip = lookup_iface_status_ip(vm=vm, iface_name=vmi_interface, ip_family=4)
LOGGER.info(f"{vm.name}/{vmi_interface} set with IP address {hot_plugged_interface_ip}")
hot_plugged_interface_ip = lookup_iface_status_ip(vm=vm, iface_name=vmi_interface.name, ip_family=4)
LOGGER.info(f"{vm.name}/{vmi_interface.name} set with IP address {hot_plugged_interface_ip}")


def hot_plug_interface_and_set_address(
Expand All @@ -236,20 +237,12 @@ def hot_plug_interface_and_set_address(
set_secondary_static_ip_address(
vm=vm,
ipv4_address=ipv4_address,
vmi_interface=iface.name,
vmi_interface=iface,
)

return iface


def get_guest_vm_interface_name_by_vmi_interface_name(vm, vm_interface_name):
vmi_interfaces = vm.vmi.interfaces
for interface in vmi_interfaces:
if interface["name"] == vm_interface_name:
return interface["interfaceName"]
raise VMInterfaceStatusNotFoundError(f"Interface {vm_interface_name} not found in VM {vm.name} status")


@contextlib.contextmanager
def create_vm_for_hot_plug(
namespace_name,
Expand Down
4 changes: 2 additions & 2 deletions tests/network/l2_bridge/test_bridge_nic_hot_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def hot_plugged_interface_with_address(running_vm_for_nic_hot_plug, index_number
set_secondary_static_ip_address(
vm=running_vm_for_nic_hot_plug,
ipv4_address=random_ipv4_address(net_seed=0, host_address=next(index_number)),
vmi_interface=hot_plugged_interface.name,
vmi_interface=hot_plugged_interface,
)


Expand Down Expand Up @@ -151,7 +151,7 @@ def hot_plugged_second_interface_with_address(
set_secondary_static_ip_address(
vm=running_vm_with_secondary_and_hot_plugged_interfaces,
ipv4_address=random_ipv4_address(net_seed=0, host_address=next(index_number)),
vmi_interface=hot_plugged_interface_on_vm_created_with_secondary_interface.name,
vmi_interface=hot_plugged_interface_on_vm_created_with_secondary_interface,
)


Expand Down