|
| 1 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | +# |
| 3 | +# Copyright Redhat |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: GPL-2.0 |
| 6 | +# Author: Nannan Li<[email protected]> |
| 7 | +# |
| 8 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 9 | +import os |
| 10 | +import time |
| 11 | + |
| 12 | +from avocado.utils import process |
| 13 | + |
| 14 | +from virttest import utils_package |
| 15 | +from virttest import virsh |
| 16 | +from virttest import utils_net |
| 17 | +from virttest import utils_misc |
| 18 | +from virttest.libvirt_xml import vm_xml |
| 19 | +from virttest.utils_libvirt import libvirt_vmxml |
| 20 | + |
| 21 | +from provider.virtual_network import network_base |
| 22 | + |
| 23 | +virsh_opt = {"debug": True, "ignore_status": False} |
| 24 | + |
| 25 | + |
| 26 | +def transfer_file(vm_object, source_path): |
| 27 | + """ |
| 28 | + Transfer stress file to guest. |
| 29 | +
|
| 30 | + :params vm_object, vm object |
| 31 | + :params source_path, the source path that's transferred to guest. |
| 32 | + :return dest_path, destination path in guest. |
| 33 | + """ |
| 34 | + if not vm_object.is_alive(): |
| 35 | + virsh.start(vm_object.name) |
| 36 | + session = vm_object.wait_for_login() |
| 37 | + |
| 38 | + dest_path = "/var/%s" % os.path.basename(source_path) |
| 39 | + vm_object.copy_files_to( |
| 40 | + host_path=source_path, guest_path=dest_path) |
| 41 | + |
| 42 | + def _file_exists(): |
| 43 | + if not session.cmd("cat %s" % dest_path, ignore_all_errors=True): |
| 44 | + return True |
| 45 | + utils_misc.wait_for(lambda: _file_exists(), 15) |
| 46 | + |
| 47 | + session.close() |
| 48 | + |
| 49 | + return dest_path |
| 50 | + |
| 51 | + |
| 52 | +def run(test, params, env): |
| 53 | + """ |
| 54 | + Packet buffer stress test for user - only for linux. |
| 55 | + """ |
| 56 | + def setup_test(): |
| 57 | + """ |
| 58 | + Prepare the test environment with required guest type and interface. |
| 59 | +
|
| 60 | + :return vm_list: list of vm names. |
| 61 | + """ |
| 62 | + test.log.info("TEST_SETUP: Prepare specific guest os version and interface.") |
| 63 | + vm_list = network_base.prepare_vms_with_iface( |
| 64 | + params, vm_image_path=vm_image_paths, iface_list=iface_list) |
| 65 | + |
| 66 | + return vm_list |
| 67 | + |
| 68 | + def run_test(vm_list): |
| 69 | + """ |
| 70 | + Set MTU value in guest interface and run stress test. |
| 71 | + """ |
| 72 | + for vm_name in vm_list: |
| 73 | + test.log.debug("TEST_STEP: Transfer stress file to guest: %s", vm_name) |
| 74 | + env.create_vm(vm_type='libvirt', target="", name=vm_name, params=params, bindir=test.bindir) |
| 75 | + vm = env.get_vm(vm_name) |
| 76 | + dest_path = transfer_file(vm, stress_file) |
| 77 | + |
| 78 | + test.log.debug("TEST_STEP: Replace interface with %s type" % iface_type) |
| 79 | + session = vm.wait_for_login(timeout=360) |
| 80 | + iface_name = utils_net.get_linux_ifname(session=session)[0] |
| 81 | + iface_mac = utils_net.get_linux_mac(session, iface_name) |
| 82 | + virsh.detach_interface(vm_name, "--type %s --mac %s" % (basic_iface_type, iface_mac), |
| 83 | + flagstr='--live', **virsh_opt) |
| 84 | + |
| 85 | + iface = libvirt_vmxml.create_vm_device_by_type('interface', iface_list_2) |
| 86 | + virsh.attach_device(vm.name, iface.xml, flagstr='--config', **virsh_opt) |
| 87 | + test.log.debug("Test with vm xml:%s", vm_xml.VMXML.new_from_dumpxml(vm_name)) |
| 88 | + session.close() |
| 89 | + |
| 90 | + test.log.debug("TEST_STEP: Set MTU value in guest: %s", vm_name) |
| 91 | + virsh.destroy(vm_name, **virsh_opt) |
| 92 | + virsh.start(vm_name, **virsh_opt) |
| 93 | + session = vm.wait_for_login(timeout=360) |
| 94 | + network_base.set_guest_iface_mtu(test, session, mtu=net_mtu) |
| 95 | + |
| 96 | + test.log.debug("TEST_STEP: Compile and execute the stress script.") |
| 97 | + utils_package.package_install(["gcc"], session, 360) |
| 98 | + session.cmd("gcc %s -o %s" % (dest_path, tmp_exe_file)) |
| 99 | + session.sendline("%s &" % tmp_exe_file) |
| 100 | + time.sleep(60) |
| 101 | + |
| 102 | + result = process.getstatusoutput( |
| 103 | + "ps -aux|grep /usr/bin/gnome-calculator |grep -v grep", |
| 104 | + timeout=60, |
| 105 | + shell=True, |
| 106 | + )[0] |
| 107 | + if result == 0: |
| 108 | + test.fail("Virtual machine has security issues") |
| 109 | + session.send_ctrl("^c") |
| 110 | + test.log.info("send ctrl+c command to exit the current process.") |
| 111 | + |
| 112 | + test.log.debug("TEST_STEP: Check guest work normally and no crash occurred.") |
| 113 | + vm.verify_kernel_crash() |
| 114 | + vm.verify_dmesg() |
| 115 | + |
| 116 | + def teardown_test(vm_list): |
| 117 | + """ |
| 118 | + Clean data. |
| 119 | + """ |
| 120 | + test.log.debug("TEST_TEARDOWN: Recover the env.") |
| 121 | + if vm_list: |
| 122 | + for vm_name in vm_list: |
| 123 | + if vm_name != params.get("main_vm"): |
| 124 | + virsh.destroy(vm_name) |
| 125 | + virsh.undefine(vm_name, options='--nvram') |
| 126 | + |
| 127 | + iface_type = params.get("iface_type") |
| 128 | + basic_iface_type = params.get("basic_iface_type", "network") |
| 129 | + |
| 130 | + iface_list = eval(params.get('iface_list', '[]')) |
| 131 | + iface_list_2 = eval(params.get('iface_list_2', '{}')) |
| 132 | + vm_image_paths = eval(params.get('vm_image_paths', '{}')) |
| 133 | + net_mtu = params.get('net_mtu') |
| 134 | + stress_file = params.get('stress_file') |
| 135 | + stress_file = os.path.join(os.path.dirname(__file__), stress_file) |
| 136 | + tmp_exe_file = params.get("tmp_exe_file", "/tmp/test") |
| 137 | + vm_list = [] |
| 138 | + |
| 139 | + try: |
| 140 | + |
| 141 | + vm_list = setup_test() |
| 142 | + run_test(vm_list) |
| 143 | + |
| 144 | + finally: |
| 145 | + teardown_test(vm_list) |
0 commit comments