|
| 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_misc |
| 11 | +from virttest import utils_net |
| 12 | + |
| 13 | +from virttest.libvirt_xml import vm_xml |
| 14 | +from virttest.utils_test import libvirt |
| 15 | + |
| 16 | +from provider.virtual_network import network_base |
| 17 | + |
| 18 | +virsh_opt = {"debug": True, "ignore_status": False} |
| 19 | + |
| 20 | + |
| 21 | +def run_sub_test(test, vm, params, plug_tag): |
| 22 | + """ |
| 23 | + Run subtest before/after hotplug/unplug device. |
| 24 | +
|
| 25 | + :param test: test object |
| 26 | + :param vm: VM object |
| 27 | + :param params: test parameters |
| 28 | + :param plug_tag: identify when to run subtest, ex, after_plug |
| 29 | + :return: whether vm was successfully shut-down if needed |
| 30 | + """ |
| 31 | + sub_type = params.get("sub_type_%s" % plug_tag) |
| 32 | + login_timeout = params.get_numeric("login_timeout", 360) |
| 33 | + vm.cleanup_serial_console() |
| 34 | + vm.create_serial_console() |
| 35 | + session = vm.wait_for_serial_login(timeout=login_timeout) |
| 36 | + shutdown_method = params.get("shutdown_method", "shell") |
| 37 | + shutdown_command = params.get("shutdown_command") |
| 38 | + if sub_type == "reboot": |
| 39 | + test.log.info("Running sub test '%s' %s", sub_type, plug_tag) |
| 40 | + if params.get("reboot_method"): |
| 41 | + vm.reboot(session, params["reboot_method"], 0, login_timeout, serial=True) |
| 42 | + elif sub_type == "shutdown": |
| 43 | + test.log.info("Running sub test '%s' %s", sub_type, plug_tag) |
| 44 | + if shutdown_method == "shell": |
| 45 | + session.sendline(shutdown_command) |
| 46 | + if not utils_misc.wait_for(lambda: vm.state() == "shut off", 120): |
| 47 | + test.fail("VM failed to shutdown within timeout") |
| 48 | + |
| 49 | + |
| 50 | +def run(test, params, env): |
| 51 | + """ |
| 52 | + Test hotplug of virtio-net NIC devices with shutdown/reboot operations. |
| 53 | +
|
| 54 | + Transferred from tp-qemu nic_hotplug.one_pci.nic_virtio.with_shutdown/with_reboot |
| 55 | + and adapted to tp-libvirt structure and function usage. |
| 56 | +
|
| 57 | + Test procedure: |
| 58 | + 1) Boot a guest with interface |
| 59 | + 2) Check connectivity |
| 60 | + 3) Pause/resume VM and verify connectivity |
| 61 | + 4) Hotunplug the interface |
| 62 | + 5) Perform shutdown or reboot operation based on test variant |
| 63 | +
|
| 64 | + :param test: QEMU test object |
| 65 | + :param params: Dictionary with the test parameters |
| 66 | + :param env: Dictionary with test environment |
| 67 | + """ |
| 68 | + vm_name = params.get("main_vm") |
| 69 | + vm = env.get_vm(vm_name) |
| 70 | + login_timeout = params.get_numeric("login_timeout", 360) |
| 71 | + repeat_times = params.get_numeric("repeat_times", 1) |
| 72 | + ping_params = {'vm_ping_host': 'pass'} |
| 73 | + ips = {'host_ip': utils_net.get_host_ip_address(params)} |
| 74 | + |
| 75 | + def run_test(): |
| 76 | + """ |
| 77 | + Execute the main virtio-net hotplug with shutdown/reboot test. |
| 78 | + """ |
| 79 | + for iteration in range(repeat_times): |
| 80 | + test.log.info("TEST_STEP 1:Start test iteration %s, guest xml", iteration + 1) |
| 81 | + vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) |
| 82 | + test.log.debug("Prepare guest xml:%s\n", vmxml) |
| 83 | + iface = libvirt.get_vm_device(vmxml, "interface", index=-1)[0] |
| 84 | + |
| 85 | + test.log.info("TEST_STEP 2: Ping host from guest") |
| 86 | + session = vm.wait_for_login(timeout=login_timeout) |
| 87 | + network_base.ping_check(ping_params, ips, session, force_ipv4=True) |
| 88 | + |
| 89 | + test.log.info("TEST_STEP 3: Pause vm and resume and check connectivity again") |
| 90 | + virsh.suspend(vm_name, **virsh_opt) |
| 91 | + virsh.resume(vm_name, **virsh_opt) |
| 92 | + network_base.ping_check(ping_params, ips, session, force_ipv4=True) |
| 93 | + |
| 94 | + test.log.info("TEST_STEP 4: Ping host from guest after resume") |
| 95 | + network_base.ping_check(ping_params, ips, session, force_ipv4=True) |
| 96 | + session.close() |
| 97 | + |
| 98 | + test.log.info("TEST_STEP 5: Hot-unplug the interface") |
| 99 | + virsh.detach_device(vm_name, iface.xml, wait_for_event=True, **virsh_opt) |
| 100 | + run_sub_test(test, vm, params, "after_unplug") |
| 101 | + |
| 102 | + run_test() |
0 commit comments