|
| 1 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | +# |
| 3 | +# Copyright Redhat |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: GPL-2.0 |
| 6 | +# Author: Nannan Li<[email protected]> |
| 7 | +# |
| 8 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 9 | +from virttest import utils_misc |
| 10 | +from virttest import utils_net |
| 11 | +from virttest import virsh |
| 12 | +from virttest.libvirt_xml import vm_xml |
| 13 | +from virttest.utils_libvirt import libvirt_vmxml |
| 14 | + |
| 15 | +from provider.virtual_network import network_base |
| 16 | + |
| 17 | +VIRSH_ARGS = {'ignore_status': False, 'debug': True} |
| 18 | + |
| 19 | + |
| 20 | +def run(test, params, env): |
| 21 | + """ |
| 22 | + Test hotplug ethernet type interface by attach-device |
| 23 | +
|
| 24 | + Steps: |
| 25 | + 1. Start the vm without any interface |
| 26 | + 2. After vm boot successfully, hotplug the interface and check xpath |
| 27 | + 3. Guest ping check |
| 28 | + 4. Detach the interface and check no interface result |
| 29 | + """ |
| 30 | + |
| 31 | + def setup_test(): |
| 32 | + """Setup VM without any interfaces and create required tap devices""" |
| 33 | + test.log.info("TEST_SETUP: Setting up VM without interfaces") |
| 34 | + libvirt_vmxml.remove_vm_devices_by_type(vm, "interface") |
| 35 | + vm.start() |
| 36 | + |
| 37 | + def run_test(): |
| 38 | + """Main test steps: attach interface, check, ping, detach""" |
| 39 | + test.log.info("TEST_STEP1: Attach interface") |
| 40 | + iface_attrs = eval(params.get('iface_attrs', "{}")) |
| 41 | + if device_type == "tap": |
| 42 | + network_base.create_tap(tap_name, default_br, "root") |
| 43 | + elif device_type == "macvtap": |
| 44 | + mac_addr = network_base.create_macvtap(macvtap_name, host_iface, "root") |
| 45 | + iface_attrs["mac_address"] = mac_addr |
| 46 | + |
| 47 | + iface = libvirt_vmxml.create_vm_device_by_type('interface', iface_attrs) |
| 48 | + virsh.attach_device(vm_name, iface.xml, **VIRSH_ARGS) |
| 49 | + test.log.debug("Guest xml before testing:%s", vm_xml.VMXML.new_from_dumpxml(vm_name)) |
| 50 | + |
| 51 | + test.log.info("TEST_STEP2: Checking guest interface added") |
| 52 | + vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) |
| 53 | + libvirt_vmxml.check_guest_xml_by_xpaths(vmxml, expected_xpaths_attach) |
| 54 | + |
| 55 | + session = vm.wait_for_serial_login() |
| 56 | + if not utils_misc.wait_for( |
| 57 | + lambda: len(utils_net.get_linux_iface_info(session=session)) == 2, timeout=15 |
| 58 | + ): |
| 59 | + test.fail(f'Interface should be in guest') |
| 60 | + |
| 61 | + test.log.info("TEST_STEP3: Checking guest connectivity") |
| 62 | + def _ping_check_wrapper(): |
| 63 | + try: |
| 64 | + ips = {'outside_ip': params.get('outside_ip')} |
| 65 | + network_base.ping_check(params, ips, session, force_ipv4=True) |
| 66 | + return True |
| 67 | + except Exception: |
| 68 | + return False |
| 69 | + if not utils_misc.wait_for(_ping_check_wrapper, timeout=10, step=1): |
| 70 | + test.fail("Guest ping outside failed") |
| 71 | + session.close() |
| 72 | + |
| 73 | + test.log.info("TEST_STEP4: Detaching ethernet interface and checking interface removal") |
| 74 | + virsh.detach_device(vm_name, iface.xml, wait_for_event=True, **VIRSH_ARGS) |
| 75 | + |
| 76 | + libvirt_vmxml.check_guest_xml_by_xpaths( |
| 77 | + vm_xml.VMXML.new_from_dumpxml(vm_name), expected_xpaths_detach |
| 78 | + ) |
| 79 | + session = vm.wait_for_serial_login() |
| 80 | + if not utils_misc.wait_for( |
| 81 | + lambda: len(utils_net.get_linux_iface_info(session=session)) == 1, timeout=15 |
| 82 | + ): |
| 83 | + test.fail(f'Interface should be removed from guest') |
| 84 | + test.log.info("Interface successfully removed from guest") |
| 85 | + session.close() |
| 86 | + |
| 87 | + def teardown_test(): |
| 88 | + """Cleanup test environment""" |
| 89 | + test.log.info("TEST_TEARDOWN: Cleaning up test environment") |
| 90 | + bkxml.sync() |
| 91 | + if device_type == "tap": |
| 92 | + test.log.info(f"Cleaning up tap device: {tap_name}") |
| 93 | + network_base.delete_tap(tap_name) |
| 94 | + elif device_type == "macvtap": |
| 95 | + test.log.info(f"Cleaning up macvtap device: {macvtap_name}") |
| 96 | + network_base.delete_tap(macvtap_name) |
| 97 | + |
| 98 | + |
| 99 | + # Get VM parameters |
| 100 | + vm_name = params.get('main_vm') |
| 101 | + vm = env.get_vm(vm_name) |
| 102 | + device_type = params.get('device_type') |
| 103 | + default_br = params.get('default_br') |
| 104 | + tap_name = params.get('tap_name', 'mytap0') |
| 105 | + macvtap_name = params.get('macvtap_name', 'mymacvtap0') |
| 106 | + expected_xpaths_attach = eval(params.get('expected_xpaths_attach')) |
| 107 | + expected_xpaths_detach = eval(params.get('expected_xpaths_detach')) |
| 108 | + if utils_misc.wait_for( |
| 109 | + lambda: utils_net.get_default_gateway( |
| 110 | + iface_name=True, force_dhcp=True, json=True) is None, timeout=15): |
| 111 | + test.fail("Not find host interface in 15s") |
| 112 | + host_iface = params.get("host_iface") |
| 113 | + host_iface = host_iface if host_iface else utils_net.get_default_gateway( |
| 114 | + iface_name=True, force_dhcp=True).split()[0] |
| 115 | + # Backup original VM XML |
| 116 | + vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) |
| 117 | + bkxml = vmxml.copy() |
| 118 | + |
| 119 | + try: |
| 120 | + setup_test() |
| 121 | + run_test() |
| 122 | + finally: |
| 123 | + teardown_test() |
0 commit comments