|
| 1 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | +# |
| 3 | +# Copyright Redhat |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: GPL-2.0 |
| 6 | +# Author: Transferred from tp-qemu nic_hotplug test case |
| 7 | +# |
| 8 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 9 | +from virttest import virsh |
| 10 | +from virttest import utils_net |
| 11 | + |
| 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_opt = {"debug": True, "ignore_status": False} |
| 18 | + |
| 19 | + |
| 20 | +def run_sub_test(test, vm, params, plug_tag): |
| 21 | + """ |
| 22 | + Run subtest before/after hotplug/unplug device. |
| 23 | +
|
| 24 | + :param test: test object |
| 25 | + :param vm: VM object |
| 26 | + :param params: test parameters |
| 27 | + :param plug_tag: identify when to run subtest, ex, after_plug |
| 28 | + :return: whether vm was successfully shut-down if needed |
| 29 | + """ |
| 30 | + sub_type = params.get("sub_type_%s" % plug_tag) |
| 31 | + login_timeout = params.get_numeric("login_timeout", 360) |
| 32 | + vm.cleanup_serial_console() |
| 33 | + vm.create_serial_console() |
| 34 | + session = vm.wait_for_serial_login(timeout=login_timeout) |
| 35 | + shutdown_method = params.get("shutdown_method", "shell") |
| 36 | + shutdown_command = params.get("shutdown_command") |
| 37 | + if sub_type == "reboot": |
| 38 | + test.log.info("Running sub test '%s' %s", sub_type, plug_tag) |
| 39 | + if params.get("reboot_method"): |
| 40 | + vm.reboot(session, params["reboot_method"], 0, login_timeout, serial=True) |
| 41 | + elif sub_type == "shutdown": |
| 42 | + test.log.info("Running sub test '%s' %s", sub_type, plug_tag) |
| 43 | + if shutdown_method == "shell": |
| 44 | + # Send a shutdown command to the guest's shell |
| 45 | + session.sendline(shutdown_command) |
| 46 | + return True |
| 47 | + |
| 48 | + return False |
| 49 | + |
| 50 | + |
| 51 | +def run(test, params, env): |
| 52 | + """ |
| 53 | + Test hotplug of virtio-net NIC devices with shutdown/reboot operations. |
| 54 | +
|
| 55 | + Transferred from tp-qemu nic_hotplug.one_pci.nic_virtio.with_shutdown/with_reboot |
| 56 | + and adapted to tp-libvirt structure and function usage. |
| 57 | +
|
| 58 | + Test procedure: |
| 59 | + 1) Boot up guest without interface |
| 60 | + 2) Hotplug virtio-net interface through libvirt |
| 61 | + 3) Check if new interface gets ip address |
| 62 | + 4) Ping guest new ip from host |
| 63 | + 5) Pause/resume VM and verify connectivity |
| 64 | + 6) Hotunplug the interface |
| 65 | + 7) Perform shutdown or reboot operation based on test variant |
| 66 | +
|
| 67 | + :param test: QEMU test object |
| 68 | + :param params: Dictionary with the test parameters |
| 69 | + :param env: Dictionary with test environment |
| 70 | + """ |
| 71 | + vm_name = params.get("main_vm") |
| 72 | + vm = env.get_vm(vm_name) |
| 73 | + iface_attrs = eval(params.get("iface_attrs", "{}")) |
| 74 | + login_timeout = params.get_numeric("login_timeout", 360) |
| 75 | + repeat_times = params.get_numeric("repeat_times", 1) |
| 76 | + vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) |
| 77 | + mac = vm_xml.VMXML.get_first_mac_by_name(vm_name) |
| 78 | + ping_params = {'guest_ping_host': 'pass'} |
| 79 | + ips = {'host_ip': utils_net.get_host_ip_address(params)} |
| 80 | + |
| 81 | + def setup_test(): |
| 82 | + """ |
| 83 | + Setup test environment for virtio-net hotplug test. |
| 84 | + """ |
| 85 | + test.log.info("TEST_SETUP: Prepare guest without interface") |
| 86 | + vmxml.remove_all_device_by_type('interface') |
| 87 | + vmxml.sync() |
| 88 | + |
| 89 | + def run_test(): |
| 90 | + """ |
| 91 | + Execute the main virtio-net hotplug with shutdown/reboot test. |
| 92 | + """ |
| 93 | + for iteration in range(repeat_times): |
| 94 | + test.log.info("Start test iteration %s", iteration + 1) |
| 95 | + if not vm.is_alive(): |
| 96 | + vm.start() |
| 97 | + |
| 98 | + test.log.info("TEST_STEP 1: Hotplug virtio-net interface") |
| 99 | + iface = libvirt_vmxml.create_vm_device_by_type('interface', iface_attrs) |
| 100 | + iface.mac_address = mac |
| 101 | + virsh.attach_device(vm_name, iface.xml, wait_for_event=True, |
| 102 | + flagstr='--live', **virsh_opt) |
| 103 | + session = vm.wait_for_login(timeout=login_timeout) |
| 104 | + |
| 105 | + test.log.info("TEST_STEP 2: Ping host from guest") |
| 106 | + network_base.ping_check(ping_params, ips, session, force_ipv4=True) |
| 107 | + |
| 108 | + test.log.info("TEST_STEP 3: Pause vm and resume") |
| 109 | + virsh.suspend(vm_name, **virsh_opt) |
| 110 | + virsh.resume(vm_name, **virsh_opt) |
| 111 | + |
| 112 | + test.log.info("TEST_STEP 4: Ping host from guest after resume") |
| 113 | + network_base.ping_check(ping_params, ips, session, force_ipv4=True) |
| 114 | + session.close() |
| 115 | + |
| 116 | + vm_switched_off = run_sub_test(test, vm, params, "after_plug") |
| 117 | + if vm_switched_off: |
| 118 | + return |
| 119 | + |
| 120 | + test.log.info("TEST_STEP 5: Hot-unplug the virtio-net interface") |
| 121 | + virsh.detach_device(vm_name, iface.xml, wait_for_event=True, **virsh_opt) |
| 122 | + run_sub_test(test, vm, params, "after_unplug") |
| 123 | + |
| 124 | + setup_test() |
| 125 | + run_test() |
0 commit comments