|
| 1 | +import pytest |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from lib.common import Defer, wait_for |
| 6 | +from lib.vif import VIF |
| 7 | +from lib.vm import VM |
| 8 | +from lib.windows import ( |
| 9 | + vif_add_manual_configuration, |
| 10 | + vif_execute_powershell_script, |
| 11 | + vif_exists, |
| 12 | + vif_has_static_configuration, |
| 13 | + vif_uses_dhcp, |
| 14 | +) |
| 15 | + |
| 16 | +from typing import Generator |
| 17 | + |
| 18 | +# Requirements: |
| 19 | +# - Same as TestGuestToolsWindowsNondestructive. |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.multi_vms |
| 23 | +@pytest.mark.usefixtures("windows_vm") |
| 24 | +class TestVifConfigure: |
| 25 | + @pytest.fixture |
| 26 | + def temporary_vif(self, vm_install_test_tools_per_test_class: VM, defer: Defer) -> Generator[VIF, None, None]: |
| 27 | + vm = vm_install_test_tools_per_test_class |
| 28 | + existing_vifs = vm.vifs() |
| 29 | + network_uuid = existing_vifs[0].param_get("network-uuid") |
| 30 | + assert network_uuid is not None |
| 31 | + |
| 32 | + logging.info("Create temporary VIF") |
| 33 | + vif = vm.create_vif(1, network_uuid=network_uuid) |
| 34 | + defer(lambda: vif.destroy()) |
| 35 | + vif.plug() |
| 36 | + defer(lambda: vif.unplug()) |
| 37 | + |
| 38 | + wait_for(lambda: vif_exists(vif), "Wait for temporary VIF network adapter") |
| 39 | + # A static default route on this test-only interface must never take precedence over the management VIF. |
| 40 | + vif_execute_powershell_script( |
| 41 | + vif, |
| 42 | + r"""Set-NetIPInterface -InterfaceIndex $adapter.ifIndex ` |
| 43 | +-AddressFamily IPv4 ` |
| 44 | +-AutomaticMetric Disabled ` |
| 45 | +-InterfaceMetric 9999; |
| 46 | +Set-NetIPInterface -InterfaceIndex $adapter.ifIndex ` |
| 47 | +-AddressFamily IPv6 ` |
| 48 | +-AutomaticMetric Disabled ` |
| 49 | +-InterfaceMetric 9999""", |
| 50 | + ) |
| 51 | + yield vif |
| 52 | + |
| 53 | + def test_vif_configure_ipv4(self, temporary_vif: VIF) -> None: |
| 54 | + vif = temporary_vif |
| 55 | + address1 = "192.0.2.2" |
| 56 | + prefix = 24 |
| 57 | + gateway1 = "192.0.2.1" |
| 58 | + address2 = "198.51.100.2" |
| 59 | + gateway2 = "198.51.100.1" |
| 60 | + |
| 61 | + logging.info("Configure DHCP IPv4") |
| 62 | + vif.configure_ipv4("dhcp") |
| 63 | + wait_for(lambda: vif_uses_dhcp(vif)) |
| 64 | + |
| 65 | + logging.info("Configure static IPv4") |
| 66 | + vif.configure_ipv4("static", f"{address1}/{prefix}", gateway1) |
| 67 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv4", address1, prefix, gateway1)) |
| 68 | + |
| 69 | + logging.info("Configure noop IPv4") |
| 70 | + vif.configure_ipv4("none") |
| 71 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv4", address1, prefix, gateway1)) |
| 72 | + |
| 73 | + logging.info("Reconfigure static IPv4") |
| 74 | + vif.configure_ipv4("static", f"{address2}/{prefix}", gateway2) |
| 75 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv4", address1, prefix, gateway1, present=False)) |
| 76 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv4", address2, prefix, gateway2)) |
| 77 | + |
| 78 | + logging.info("Configure DHCP IPv4") |
| 79 | + vif.configure_ipv4("dhcp") |
| 80 | + wait_for(lambda: vif_uses_dhcp(vif)) |
| 81 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv4", address2, prefix, gateway2, present=False)) |
| 82 | + |
| 83 | + def test_vif_configure_ipv4_dhcp_to_static(self, temporary_vif: VIF) -> None: |
| 84 | + vif = temporary_vif |
| 85 | + address = "203.0.113.2" |
| 86 | + prefix = 24 |
| 87 | + gateway = "203.0.113.1" |
| 88 | + |
| 89 | + logging.info("Configure DHCP IPv4") |
| 90 | + vif.configure_ipv4("dhcp") |
| 91 | + wait_for(lambda: vif_uses_dhcp(vif)) |
| 92 | + |
| 93 | + logging.info("Add manual configuration") |
| 94 | + vif_add_manual_configuration(vif, address, prefix, gateway) |
| 95 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv4", address, prefix, gateway)) |
| 96 | + |
| 97 | + logging.info("Configure static IPv4 while keeping address %s/%s and gateway %s", address, prefix, gateway) |
| 98 | + vif.configure_ipv4("static", f"{address}/{prefix}", gateway) |
| 99 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv4", address, prefix, gateway)) |
| 100 | + |
| 101 | + def test_vif_configure_ipv6(self, temporary_vif: VIF) -> None: |
| 102 | + vif = temporary_vif |
| 103 | + address1 = "2001:db8:1::2" |
| 104 | + prefix = 64 |
| 105 | + gateway1 = "2001:db8:1::1" |
| 106 | + address2 = "2001:db8:2::3" |
| 107 | + gateway2 = "2001:db8:2::254" |
| 108 | + |
| 109 | + logging.info("Configure autoconf IPv6") |
| 110 | + vif.configure_ipv6("autoconf") |
| 111 | + |
| 112 | + logging.info("Configure static IPv6") |
| 113 | + vif.configure_ipv6("static", f"{address1}/{prefix}", gateway1) |
| 114 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv6", address1, prefix, gateway1)) |
| 115 | + |
| 116 | + logging.info("Reconfigure noop IPv6") |
| 117 | + vif.configure_ipv6("none") |
| 118 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv6", address1, prefix, gateway1)) |
| 119 | + |
| 120 | + logging.info("Reconfigure static IPv6") |
| 121 | + vif.configure_ipv6("static", f"{address2}/{prefix}", gateway2) |
| 122 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv6", address1, prefix, gateway1, present=False)) |
| 123 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv6", address2, prefix, gateway2)) |
| 124 | + |
| 125 | + logging.info("Configure autoconf IPv6") |
| 126 | + vif.configure_ipv6("autoconf") |
| 127 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv6", address2, prefix, gateway2, present=False)) |
| 128 | + |
| 129 | + def test_vif_configure_ipv6_autoconf_to_static(self, temporary_vif: VIF) -> None: |
| 130 | + vif = temporary_vif |
| 131 | + address = "2001:db8:3::2" |
| 132 | + prefix = 64 |
| 133 | + gateway = "2001:db8:3::1" |
| 134 | + |
| 135 | + logging.info("Configure autoconf IPv6") |
| 136 | + vif.configure_ipv6("autoconf") |
| 137 | + |
| 138 | + logging.info("Add manual configuration") |
| 139 | + vif_add_manual_configuration(vif, address, prefix, gateway) |
| 140 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv6", address, prefix, gateway)) |
| 141 | + |
| 142 | + logging.info( |
| 143 | + "Configure static IPv6 while keeping address %s/%s and gateway %s", |
| 144 | + address, |
| 145 | + prefix, |
| 146 | + gateway, |
| 147 | + ) |
| 148 | + vif.configure_ipv6("static", f"{address}/{prefix}", gateway) |
| 149 | + wait_for(lambda: vif_has_static_configuration(vif, "IPv6", address, prefix, gateway)) |
0 commit comments