|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +from lib.bond import Bond |
| 8 | +from lib.host import Host |
| 9 | +from lib.network import Network |
| 10 | +from lib.vm import VM |
| 11 | + |
| 12 | +# Requirements: |
| 13 | +# From --hosts parameter: |
| 14 | +# - host(A1): first XCP-ng host, with at least 3 NICs (1 management, 2 unused), with tcpdump |
| 15 | +# From --vm parameter |
| 16 | +# - A VM to import (alpine expected) |
| 17 | + |
| 18 | +@pytest.mark.small_vm |
| 19 | +class TestNetwork: |
| 20 | + @pytest.mark.no_vm |
| 21 | + def test_basic(self, host: Host, empty_network: Network): |
| 22 | + assert empty_network.PIF_uuids() == [], "PIF list must be empty" |
| 23 | + assert empty_network.VIF_uuids() == [], "VIF list must be empty" |
| 24 | + assert empty_network.is_private(), "empty_network must be private" |
| 25 | + assert empty_network.MTU() == 1500, "unexpected MTU" |
| 26 | + |
| 27 | + def test_private_network(self, host: Host, empty_network: Network, imported_vm: VM): |
| 28 | + network = empty_network |
| 29 | + |
| 30 | + try: |
| 31 | + vm1 = imported_vm.clone() |
| 32 | + vm2 = imported_vm.clone() |
| 33 | + |
| 34 | + vif_1_1 = vm1.create_vif(1, network_uuid=network.uuid) |
| 35 | + vif_2_1 = vm2.create_vif(1, network_uuid=network.uuid) |
| 36 | + |
| 37 | + assert len(vm1.vifs()) == 2, "VM1 should have 2 NICs" |
| 38 | + assert len(vm2.vifs()) == 2, "VM2 should have 2 NICs" |
| 39 | + assert len(network.VIF_uuids()) == 2, "unexpected number of VIFs in network" |
| 40 | + |
| 41 | + vm1.start() |
| 42 | + vm2.start() |
| 43 | + |
| 44 | + vm1.wait_for_vm_running_and_ssh_up() |
| 45 | + vm2.wait_for_vm_running_and_ssh_up() |
| 46 | + |
| 47 | + logging.info("Configuring local address on private network") |
| 48 | + vm1.ssh(f"ifconfig eth{vif_1_1.param_get('device')} inet 169.254.1.1 broadcast 169.254.0.0 up") |
| 49 | + vm2.ssh(f"ifconfig eth{vif_2_1.param_get('device')} inet 169.254.2.1 broadcast 169.254.0.0 up") |
| 50 | + |
| 51 | + logging.info("Ping VMs") |
| 52 | + assert vm1.ssh_with_result(["ping", "-c3", "-w5", "169.254.2.1"]).returncode == 0 |
| 53 | + assert vm2.ssh_with_result(["ping", "-c3", "-w5", "169.254.1.1"]).returncode == 0 |
| 54 | + |
| 55 | + finally: |
| 56 | + # VIFs are destroyed by VM.destroy() |
| 57 | + vm2.destroy() |
| 58 | + vm1.destroy() |
| 59 | + |
| 60 | +def _wait_for_packet(host: Host | VM, interface: str, sfilter: str, timeout: int = 30) -> None: |
| 61 | + ret = host.ssh_with_result(f"timeout {timeout} tcpdump -i {interface} -n -c1 '{sfilter}'") |
| 62 | + if ret.returncode != 0: |
| 63 | + pytest.fail(f"tcpdump error: code={ret.returncode} stdout={ret.stdout}") |
| 64 | + return None |
| 65 | + |
| 66 | +@pytest.mark.complex_prerequisites |
| 67 | +@pytest.mark.small_vm |
| 68 | +@pytest.mark.parametrize("bond_devices", [["eth1", "eth2"]]) |
| 69 | +@pytest.mark.parametrize("bond_mode", ["lacp", "active-backup", "balance-slb"]) |
| 70 | +def test_bond(host: Host, imported_vm: VM, bond_devices: list[str], bond_mode: str, bond: Bond): |
| 71 | + # expect host with eth1 and eth2 NICs free for use |
| 72 | + logging.info(f"Bond = {bond.uuid} mode={bond.mode()} slaves={bond.slaves()}") |
| 73 | + |
| 74 | + # disable LACP fallback (make Bond to require LACP negociation first) |
| 75 | + if bond_mode == "lacp": |
| 76 | + bond.param_set("properties", key="lacp-fallback-ab", value=False) |
| 77 | + |
| 78 | + # check bond0 on the host |
| 79 | + ret = host.ssh_with_result("ovs-appctl bond/show bond0") |
| 80 | + if ret.returncode != 0: |
| 81 | + pytest.fail(f"ovs-appctl bond/show failed: exitcode={ret.returncode} stdout={ret.stdout}") |
| 82 | + |
| 83 | + if bond_mode == 'lacp': |
| 84 | + bond_mode_output = 'balance-tcp' |
| 85 | + else: |
| 86 | + bond_mode_output = bond_mode |
| 87 | + |
| 88 | + if f"bond_mode: {bond_mode_output}" not in ret.stdout: |
| 89 | + pytest.fail(f"unexpected bond_mode: {bond_mode}: stdout={ret.stdout}") |
| 90 | + |
| 91 | + if bond_mode == 'lacp': |
| 92 | + if "lacp_fallback_ab: false" not in ret.stdout: |
| 93 | + pytest.fail(f"unexpected lacp_fallback_ab: stdout={ret.stdout}") |
| 94 | + elif "lacp_status: configured" not in ret.stdout: |
| 95 | + pytest.fail(f"unexpected lacp_status: stdout={ret.stdout}") |
| 96 | + else: |
| 97 | + if "lacp_status: off" not in ret.stdout: |
| 98 | + pytest.fail(f"unexpected lacp_status: stdout={ret.stdout}") |
| 99 | + |
| 100 | + try: |
| 101 | + # on the VM, add a new NIC using the bond |
| 102 | + vm = imported_vm.clone() |
| 103 | + vm.create_vif(1, network_uuid=bond.master().network_uuid()) |
| 104 | + vm.start() |
| 105 | + vm.wait_for_vm_running_and_ssh_up() |
| 106 | + |
| 107 | + vm.ssh("apk add tcpdump") |
| 108 | + vm.ssh("ip link set eth1 up") |
| 109 | + |
| 110 | + if bond_mode == 'lacp': |
| 111 | + # we are checking if we are seeing LACP packet on VM side. |
| 112 | + # |
| 113 | + # OpenvSwitch will send LACP negociation on host.eth1 and host.eth2 |
| 114 | + # to etablish LACP link. As we don't have other side, it will keep |
| 115 | + # sending them. |
| 116 | + # On VM side, we could see such packets. So we are checking the VM.eth1 |
| 117 | + # is properly connected to the Bond just created (but we don't check that |
| 118 | + # OpenvSwitch is properly setup) |
| 119 | + logging.info("Waiting for LACP packet") |
| 120 | + _wait_for_packet(vm, "eth1", "ether proto 0x8809") |
| 121 | + |
| 122 | + else: |
| 123 | + # just check if we see a packet on the host side. |
| 124 | + # theVM kernel is expected to send IPv6 packet for Router Solicitation |
| 125 | + # as the VM interface is UP. |
| 126 | + logging.info("Waiting for some IPv6 packet") |
| 127 | + _wait_for_packet(host, "eth1", "ether proto 0x86dd") |
| 128 | + |
| 129 | + finally: |
| 130 | + vm.destroy() |
0 commit comments