def receiver(): """ Receive side """ logging.info("Starting receiver process on %s", receiver_addr) if vm_receiver: session = vm_receiver.wait_for_login(timeout=login_timeout) else: username = params.get("username", "") password = params.get("password", "") prompt = params.get("shell_prompt", "[\#\$]") linesep = eval("'%s'" % params.get("shell_linesep", r"\n")) client = params.get("shell_client") port = int(params.get("shell_port")) log_filename = "session-%s-%s.log" % (receiver_addr, virt_utils.generate_random_string(4)) session = virt_utils.remote_login( client, receiver_addr, port, username, password, prompt, linesep, log_filename, timeout ) session.set_status_test_command("echo %errorlevel%") install_ntttcp(session) ntttcp_receiver_cmd = params.get("ntttcp_receiver_cmd") global _receiver_ready f = open(results_path + ".receiver", "a") for b in buffers: virt_utils.wait_for(lambda: not _wait(), timeout) _receiver_ready = True rbuf = params.get("fixed_rbuf", b) cmd = ntttcp_receiver_cmd % (session_num, receiver_addr, rbuf, buf_num) r = session.cmd_output(cmd, timeout=timeout, print_func=logging.debug) f.write("Send buffer size: %s\n%s\n%s" % (b, cmd, r)) f.close() session.close()
def crash_test(vcpu): """ Trigger a crash dump through sysrq-trigger @param vcpu: vcpu which is used to trigger a crash """ session = vm.wait_for_login(timeout=timeout) session.cmd_output("rm -rf /var/crash/*") if crash_cmd == "nmi": session.cmd("echo 1 > /proc/sys/kernel/unknown_nmi_panic") vm.monitor.cmd('nmi') else: logging.info("Triggering crash on vcpu %d ...", vcpu) session.sendline("taskset -c %d %s" % (vcpu, crash_cmd)) if not virt_utils.wait_for(lambda: not session.is_responsive(), 240, 0, 1): raise error.TestFail("Could not trigger crash on vcpu %d" % vcpu) logging.info("Waiting for kernel crash dump to complete") session = vm.wait_for_login(timeout=crash_timeout) logging.info("Probing vmcore file...") session.cmd("ls -R /var/crash | grep vmcore") logging.info("Found vmcore.") session.cmd_output("rm -rf /var/crash/*")
def sender(): """ Send side """ logging.info("Sarting sender process ...") session = vm_sender.wait_for_login(timeout=login_timeout) install_ntttcp(session) ntttcp_sender_cmd = params.get("ntttcp_sender_cmd") f = open(results_path + ".sender", "a") try: global _receiver_ready for b in buffers: cmd = ntttcp_sender_cmd % (session_num, receiver_addr, b, buf_num) # Wait until receiver ready virt_utils.wait_for(_wait, timeout) r = session.cmd_output(cmd, timeout=timeout, print_func=logging.debug) _receiver_ready = False f.write("Send buffer size: %s\n%s\n%s" % (b, cmd, r)) finally: f.close() session.close()
def run_mac_change(test, params, env): """ Change MAC address of guest. 1) Get a new mac from pool, and the old mac addr of guest. 2) Set new mac in guest and regain new IP. 3) Re-log into guest with new MAC. @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 360)) session_serial = vm.wait_for_serial_login(timeout=timeout) # This session will be used to assess whether the IP change worked session = vm.wait_for_login(timeout=timeout) old_mac = vm.get_mac_address(0) while True: vm.free_mac_address(0) new_mac = virt_utils.generate_mac_address(vm.instance, 0) if old_mac != new_mac: break logging.info("The initial MAC address is %s", old_mac) interface = virt_test_utils.get_linux_ifname(session_serial, old_mac) # Start change MAC address logging.info("Changing MAC address to %s", new_mac) change_cmd = ("ifconfig %s down && ifconfig %s hw ether %s && " "ifconfig %s up" % (interface, interface, new_mac, interface)) session_serial.cmd(change_cmd) # Verify whether MAC address was changed to the new one logging.info("Verifying the new mac address") session_serial.cmd("ifconfig | grep -i %s" % new_mac) # Restart `dhclient' to regain IP for new mac address logging.info("Restart the network to gain new IP") dhclient_cmd = "dhclient -r && dhclient %s" % interface session_serial.sendline(dhclient_cmd) # Re-log into the guest after changing mac address if virt_utils.wait_for(session.is_responsive, 120, 20, 3): # Just warning when failed to see the session become dead, # because there is a little chance the ip does not change. logging.warn("The session is still responsive, settings may fail.") session.close() # Re-log into guest and check if session is responsive logging.info("Re-log into the guest") session = vm.wait_for_login(timeout=timeout) if not session.is_responsive(): raise error.TestFail("The new session is not responsive.") session.close()
def watchdog_action_reset(): """ Trigger a crash dump through sysrq-trigger Ensure watchdog_action(reset) occur. """ session = vm.wait_for_login(timeout=timeout) logging.info("Triggering crash on vm") crash_cmd = "echo c > /proc/sysrq-trigger" session.sendline(crash_cmd) if not virt_utils.wait_for(lambda: not session.is_responsive(), 240, 0, 1): raise error.TestFail("Could not trigger crash") logging.info("Waiting for kernel watchdog_action to take place") session = vm.wait_for_login(timeout=relogin_timeout)
def nic_del(vm, nic_id, wait=True): """ Remove the nic from pci tree. @vm: VM object @id: the nic id @wait: Whether need to wait for the guest to unplug the device """ nic_del_cmd = "device_del %s" % nic_id vm.monitor.cmd(nic_del_cmd) if wait: logging.info("waiting for the guest to finish the unplug") if not virt_utils.wait_for(lambda: nic_id not in vm.monitor.info("qtree"), guest_delay, 5 ,1): logging.error(vm.monitor.info("qtree")) raise error.TestError("Device is not unplugged by " "guest, please check whether the " "hotplug module was loaded in guest")
def pci_del(ignore_failure=False): if cmd_type == "pci_add": result_domain, bus, slot, function = add_output.split(',') domain = int(result_domain.split()[2]) bus = int(bus.split()[1]) slot = int(slot.split()[1]) pci_addr = "%x:%x:%x" % (domain, bus, slot) cmd = "pci_del pci_addr=%s" % pci_addr elif cmd_type == "device_add": cmd = "device_del %s" % device_id # This should be replaced by a proper monitor method call vm.monitor.cmd(cmd) def device_removed(): after_del = vm.monitor.info("pci") return after_del != after_add if (not virt_utils.wait_for(device_removed, 10, 0, 1) and not ignore_failure): raise error.TestFail("Failed to hot remove PCI device: %s. " "Monitor command: %s" % (tested_model, cmd))
def run_shutdown(test, params, env): """ KVM shutdown test: 1) Log into a guest 2) Send a shutdown command to the guest, or issue a system_powerdown monitor command (depending on the value of shutdown_method) 3) Wait until the guest is down @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 360)) session = vm.wait_for_login(timeout=timeout) try: error.base_context("shutting down the VM") if params.get("shutdown_method") == "shell": # Send a shutdown command to the guest's shell session.sendline(vm.get_params().get("shutdown_command")) error.context("waiting VM to go down (shutdown shell cmd)") elif params.get("shutdown_method") == "system_powerdown": # Sleep for a while -- give the guest a chance to finish booting time.sleep(float(params.get("sleep_before_powerdown", 10))) # Send a system_powerdown monitor command vm.monitor.cmd("system_powerdown") error.context("waiting VM to go down " "(system_powerdown monitor cmd)") if not virt_utils.wait_for(vm.is_dead, 240, 0, 1): raise error.TestFail("Guest refuses to go down") finally: session.close()
else: tcpdump_cmd += " and dst %s" % guest_ip copy_files_from = vm.copy_files_to try: utils.system(dd_cmd) except error.CmdError, e: return failure # only capture the new tcp port after offload setup original_tcp_ports = re.findall("tcp.*:(\d+).*%s" % guest_ip, utils.system_output("/bin/netstat -nap")) for i in original_tcp_ports: tcpdump_cmd += " and not port %s" % i logging.debug("Listen using command: %s", tcpdump_cmd) session2.sendline(tcpdump_cmd) if not virt_utils.wait_for( lambda:session.cmd_status("pgrep tcpdump") == 0, 30): return (False, "Tcpdump process wasn't launched") logging.info("Start to transfer file") try: copy_files_from(filename, filename) except virt_utils.SCPError, e: return (False, "File transfer failed (%s)" % e) logging.info("Transfer file completed") session.cmd("killall tcpdump") try: tcpdump_string = session2.read_up_to_prompt(timeout=60) except aexpect.ExpectError: return (False, "Fail to read tcpdump's output") if not compare_md5sum(filename):
def run_cdrom(test, params, env): """ KVM cdrom test: 1) Boot up a VM with one iso. 2) Check if VM identifies correctly the iso file. 3) Eject cdrom and change with another iso several times. 4) Try to format cdrom and check the return string. 5) Mount cdrom device. 6) Copy file from cdrom and compare files using diff. 7) Umount and mount several times. @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment. """ def master_cdroms(params): error.context("creating test cdrom") os.chdir(test.tmpdir) cdrom_cd1 = params.get("cdrom_cd1") cdrom_dir = os.path.dirname(cdrom_cd1) utils.run("dd if=/dev/urandom of=orig bs=10M count=1") utils.run("dd if=/dev/urandom of=new bs=10M count=1") utils.run("mkisofs -o %s/orig.iso orig" % cdrom_dir) utils.run("mkisofs -o %s/new.iso new" % cdrom_dir) return "%s/new.iso" % cdrom_dir def cleanup_cdroms(cdrom_dir): error.context("cleaning up temp cdrom images") os.remove("%s/orig.iso" % cdrom_dir) os.remove("%s/new.iso" % cdrom_dir) def get_cdrom_info(): blocks = vm.monitor.info("block") (device, file) = (None, None) if isinstance(blocks, str): try: device = re.findall("(ide\d+-cd\d+): .*", blocks)[0] except IndexError: device = None try: file = re.findall("ide\d+-cd\d+: .*file=(\S*) ", blocks)[0] except IndexError: file = None else: for block in blocks: d = block['device'] try: device = re.findall("(ide\d+-cd\d+)", d)[0] except IndexError: device = None continue try: file = block['inserted']['file'] except KeyError: file = None break logging.debug("Device name: %s, ISO: %s" % (device, file)) return (device, file) def check_cdrom_locked(cdrom): blocks = vm.monitor.info("block") if isinstance(blocks, str): lock_str = "locked=1" for block in blocks.splitlines(): if cdrom in block and lock_str in block: return True else: for block in blocks: if ('inserted' in block.keys() and block['inserted']['file'] == cdrom): return block['locked'] return False def eject_cdrom(device, monitor): if isinstance(monitor, kvm_monitor.HumanMonitor): monitor.cmd("eject %s" % device) elif isinstance(monitor, kvm_monitor.QMPMonitor): monitor.cmd("eject", args={'device': device}) def change_cdrom(device, target, monitor): if isinstance(monitor, kvm_monitor.HumanMonitor): monitor.cmd("change %s %s" % (device, target)) elif isinstance(monitor, kvm_monitor.QMPMonitor): monitor.cmd("change", args={'device': device, 'target': target}) cdrom_new = master_cdroms(params) cdrom_dir = os.path.dirname(cdrom_new) vm = env.get_vm(params["main_vm"]) vm.create() session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360))) cdrom_orig = params.get("cdrom_cd1") cdrom = cdrom_orig output = session.get_command_output("ls /dev/cdrom*") cdrom_dev_list = re.findall("/dev/cdrom-\w+|/dev/cdrom\d*", output) logging.debug("cdrom_dev_list: %s" % cdrom_dev_list) cdrom_dev = "" test_cmd = "dd if=%s of=/dev/null bs=1 count=1" for d in cdrom_dev_list: try: output = session.cmd(test_cmd % d) cdrom_dev = d break except aexpect.ShellError: logging.error(output) if not cdrom_dev: raise error.TestFail("Could not find a valid cdrom device") error.context("Detecting the existence of a cdrom") (device, file) = get_cdrom_info() if file != cdrom: raise error.TestError("Could not find a valid cdrom device") session.get_command_output("umount %s" % cdrom_dev) if not virt_utils.wait_for(lambda: not check_cdrom_locked(file), 300): raise error.TestError("Device %s could not be unlocked" % device) max_times = int(params.get("max_times", 100)) error.context("Eject the cdrom for %s times" % max_times) for i in range(1, max_times): eject_cdrom(device, vm.monitor) (device, file) = get_cdrom_info() if file is not None: raise error.TestFail("Device %s was not ejected" % cdrom) cdrom = cdrom_new # On even attempts, try to change the cdrom if i % 2 == 0: cdrom = cdrom_orig change_cdrom(device, cdrom, vm.monitor) time.sleep(10) (device, file) = get_cdrom_info() if file != cdrom: raise error.TestError("It wasn't possible to change cdrom %s" % cdrom) error.context("Check whether the cdrom is read-only") try: output = session.cmd("echo y | mkfs %s" % cdrom_dev) raise error.TestFail("Attempt to format cdrom %s succeeded" % cdrom_dev) except aexpect.ShellError: pass error.context("Mounting the cdrom under /mnt") session.cmd("mount %s %s" % (cdrom_dev, "/mnt"), timeout=30) filename = "new" error.context("File copying test") session.cmd("rm -f /tmp/%s" % filename) session.cmd("cp -f /mnt/%s /tmp/" % filename) error.context("Compare file on disk and on cdrom") f1_hash = session.cmd("md5sum /mnt/%s" % filename).split()[0].strip() f2_hash = session.cmd("md5sum /tmp/%s" % filename).split()[0].strip() if f1_hash != f2_hash: raise error.TestFail("On disk and on cdrom files are different, " "md5 mismatch") error.context("Mount/Unmount cdrom for %s times" % max_times) for i in range(1, max_times): try: session.cmd("umount %s" % cdrom_dev) session.cmd("mount %s /mnt" % cdrom_dev) except aexpect.ShellError: logging.debug(session.cmd("cat /etc/mtab")) raise session.cmd("umount %s" % cdrom_dev) (device, file) = get_cdrom_info() if device is not None: eject_cdrom(device, vm.monitor) session.close() cleanup_cdroms(cdrom_dir)
def run_nic_hotplug(test, params, env): """ Test hotplug of NIC devices 1) Boot up guest with one nic 2) Add a host network device through monitor cmd and check if it's added 3) Add nic device through monitor cmd and check if it's added 4) Check if new interface gets ip address 5) Disable primary link of guest 6) Ping guest new ip from host 7) Delete nic device and netdev 8) Re-enable primary link of guest @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ vm = virt_test_utils.get_living_vm(env, params.get("main_vm")) timeout = int(params.get("login_timeout", 360)) guest_delay = int(params.get("guest_delay", 20)) session = virt_test_utils.wait_for_login(vm, timeout=timeout) romfile = params.get("romfile") # Modprobe the module if specified in config file module = params.get("modprobe_module") if module: session.get_command_output("modprobe %s" % module) def netdev_add(vm): netdev_id = virt_utils.generate_random_id() attach_cmd = ("netdev_add tap,id=%s" % netdev_id) nic_script = params.get("nic_script") if nic_script: attach_cmd += ",script=%s" % virt_utils.get_path(vm.root_dir, nic_script) netdev_extra_params = params.get("netdev_extra_params") if netdev_extra_params: attach_cmd += ",%s" % netdev_extra_params logging.info("Adding netdev through %s", attach_cmd) vm.monitor.cmd(attach_cmd) network = vm.monitor.info("network") if netdev_id not in network: logging.error(network) raise error.TestError("Fail to add netdev: %s" % netdev_id) else: return netdev_id def netdev_del(vm, n_id): vm.monitor.cmd("netdev_del %s" % n_id) network = vm.monitor.info("network") if n_id in network: logging.error(network) raise error.TestError("Fail to remove netdev %s" % n_id) def nic_add(vm, model, netdev_id, mac, rom=None): """ Add a nic to virtual machine @vm: VM object @model: nic model @netdev_id: id of netdev @mac: Mac address of new nic @rom: Rom file """ nic_id = virt_utils.generate_random_id() if model == "virtio": model = "virtio-net-pci" device_add_cmd = "device_add %s,netdev=%s,mac=%s,id=%s" % (model, netdev_id, mac, nic_id) if rom: device_add_cmd += ",romfile=%s" % rom logging.info("Adding nic through %s", device_add_cmd) vm.monitor.cmd(device_add_cmd) qdev = vm.monitor.info("qtree") if not nic_id in qdev: logging.error(qdev) raise error.TestFail("Device %s was not plugged into qdev" "tree" % nic_id) else: return nic_id def nic_del(vm, nic_id, wait=True): """ Remove the nic from pci tree. @vm: VM object @id: the nic id @wait: Whether need to wait for the guest to unplug the device """ nic_del_cmd = "device_del %s" % nic_id vm.monitor.cmd(nic_del_cmd) if wait: logging.info("waiting for the guest to finish the unplug") if not virt_utils.wait_for(lambda: nic_id not in vm.monitor.info("qtree"), guest_delay, 5 ,1): logging.error(vm.monitor.info("qtree")) raise error.TestError("Device is not unplugged by " "guest, please check whether the " "hotplug module was loaded in guest") logging.info("Attach a virtio nic to vm") mac = virt_utils.generate_mac_address(vm.instance, 1) if not mac: mac = "00:00:02:00:00:02" netdev_id = netdev_add(vm) device_id = nic_add(vm, "virtio", netdev_id, mac, romfile) if "Win" not in params.get("guest_name", ""): session.sendline("dhclient %s &" % virt_test_utils.get_linux_ifname(session, mac)) logging.info("Shutting down the primary link") vm.monitor.cmd("set_link %s down" % vm.netdev_id[0]) try: logging.info("Waiting for new nic's ip address acquisition...") if not virt_utils.wait_for(lambda: (vm.address_cache.get(mac) is not None), 10, 1): raise error.TestFail("Could not get ip address of new nic") ip = vm.address_cache.get(mac) if not virt_utils.verify_ip_address_ownership(ip, mac): raise error.TestFail("Could not verify the ip address of new nic") else: logging.info("Got the ip address of new nic: %s", ip) logging.info("Ping test the new nic ...") s, o = virt_test_utils.ping(ip, 100) if s != 0: logging.error(o) raise error.TestFail("New nic failed ping test") logging.info("Detaching a virtio nic from vm") nic_del(vm, device_id) netdev_del(vm, netdev_id) finally: vm.free_mac_address(1) logging.info("Re-enabling the primary link") vm.monitor.cmd("set_link %s up" % vm.netdev_id[0])
def run_pci_hotplug(test, params, env): """ Test hotplug of PCI devices. (Elements between [] are configurable test parameters) 1) PCI add a deivce (NIC / block) 2) Compare output of monitor command 'info pci'. 3) Compare output of guest command [reference_cmd]. 4) Verify whether pci_model is shown in [pci_find_cmd]. 5) Check whether the newly added PCI device works fine. 6) PCI delete the device, verify whether could remove the PCI device. @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 360)) session = vm.wait_for_login(timeout=timeout) # Modprobe the module if specified in config file module = params.get("modprobe_module") if module: session.cmd("modprobe %s" % module) # Get output of command 'info pci' as reference info_pci_ref = vm.monitor.info("pci") # Get output of command as reference reference = session.cmd_output(params.get("reference_cmd")) tested_model = params.get("pci_model") test_type = params.get("pci_type") image_format = params.get("image_format_stg") # Probe qemu to verify what is the supported syntax for PCI hotplug cmd_output = vm.monitor.cmd("?") if len(re.findall("\ndevice_add", cmd_output)) > 0: cmd_type = "device_add" elif len(re.findall("\npci_add", cmd_output)) > 0: cmd_type = "pci_add" else: raise error.TestError("Unknow version of qemu") # Determine syntax of drive hotplug # __com.redhat_drive_add == qemu-kvm-0.12 on RHEL 6 if len(re.findall("\n__com.redhat_drive_add", cmd_output)) > 0: drive_cmd_type = "__com.redhat_drive_add" # drive_add == qemu-kvm-0.13 onwards elif len(re.findall("\ndrive_add", cmd_output)) > 0: drive_cmd_type = "drive_add" else: raise error.TestError("Unknow version of qemu") # Probe qemu for a list of supported devices devices_support = vm.monitor.cmd("%s ?" % cmd_type) if cmd_type == "pci_add": if test_type == "nic": pci_add_cmd = "pci_add pci_addr=auto nic model=%s" % tested_model elif test_type == "block": image_params = params.object_params("stg") image_filename = virt_vm.get_image_filename( image_params, test.bindir) pci_add_cmd = ("pci_add pci_addr=auto storage file=%s,if=%s" % (image_filename, tested_model)) # Execute pci_add (should be replaced by a proper monitor method call) add_output = vm.monitor.cmd(pci_add_cmd) if not "OK domain" in add_output: raise error.TestFail("Add PCI device failed. " "Monitor command is: %s, Output: %r" % (pci_add_cmd, add_output)) after_add = vm.monitor.info("pci") elif cmd_type == "device_add": driver_id = test_type + "-" + virt_utils.generate_random_id() device_id = test_type + "-" + virt_utils.generate_random_id() if test_type == "nic": if tested_model == "virtio": tested_model = "virtio-net-pci" pci_add_cmd = "device_add id=%s,driver=%s" % (device_id, tested_model) elif test_type == "block": image_params = params.object_params("stg") image_filename = virt_vm.get_image_filename( image_params, test.bindir) controller_model = None if tested_model == "virtio": tested_model = "virtio-blk-pci" if tested_model == "scsi": tested_model = "scsi-disk" controller_model = "lsi53c895a" if len(re.findall(controller_model, devices_support)) == 0: raise error.TestError("scsi controller device (%s) not " "supported by qemu" % controller_model) if controller_model is not None: controller_id = "controller-" + device_id controller_add_cmd = ("device_add %s,id=%s" % (controller_model, controller_id)) vm.monitor.cmd(controller_add_cmd) if drive_cmd_type == "drive_add": driver_add_cmd = ("drive_add auto " "file=%s,if=none,id=%s,format=%s" % (image_filename, driver_id, image_format)) elif drive_cmd_type == "__com.redhat_drive_add": driver_add_cmd = ("__com.redhat_drive_add " "file=%s,format=%s,id=%s" % (image_filename, image_format, driver_id)) pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" % (device_id, tested_model, driver_id)) vm.monitor.cmd(driver_add_cmd) # Check if the device is support in qemu if len(re.findall(tested_model, devices_support)) > 0: add_output = vm.monitor.cmd(pci_add_cmd) else: raise error.TestError("%s doesn't support device: %s" % (cmd_type, tested_model)) after_add = vm.monitor.info("pci") if not device_id in after_add: raise error.TestFail("Add device failed. Monitor command is: %s" ". Output: %r" % (pci_add_cmd, add_output)) # Define a helper function to delete the device def pci_del(ignore_failure=False): if cmd_type == "pci_add": result_domain, bus, slot, function = add_output.split(',') domain = int(result_domain.split()[2]) bus = int(bus.split()[1]) slot = int(slot.split()[1]) pci_addr = "%x:%x:%x" % (domain, bus, slot) cmd = "pci_del pci_addr=%s" % pci_addr elif cmd_type == "device_add": cmd = "device_del %s" % device_id # This should be replaced by a proper monitor method call vm.monitor.cmd(cmd) def device_removed(): after_del = vm.monitor.info("pci") return after_del != after_add if (not virt_utils.wait_for(device_removed, 10, 0, 1) and not ignore_failure): raise error.TestFail("Failed to hot remove PCI device: %s. " "Monitor command: %s" % (tested_model, cmd)) try: # Compare the output of 'info pci' if after_add == info_pci_ref: raise error.TestFail("No new PCI device shown after executing " "monitor command: 'info pci'") # Define a helper function to compare the output def new_shown(): o = session.cmd_output(params.get("reference_cmd")) return o != reference secs = int(params.get("wait_secs_for_hook_up")) if not virt_utils.wait_for(new_shown, 30, secs, 3): raise error.TestFail("No new device shown in output of command " "executed inside the guest: %s" % params.get("reference_cmd")) # Define a helper function to catch PCI device string def find_pci(): o = session.cmd_output(params.get("find_pci_cmd")) return params.get("match_string") in o if not virt_utils.wait_for(find_pci, 30, 3, 3): raise error.TestFail( "PCI %s %s device not found in guest. " "Command was: %s" % (tested_model, test_type, params.get("find_pci_cmd"))) # Test the newly added device try: session.cmd(params.get("pci_test_cmd")) except aexpect.ShellError, e: raise error.TestFail("Check for %s device failed after PCI " "hotplug. Output: %r" % (test_type, e.output)) session.close()
global _url_auto_content_server_thread global _url_auto_content_server_thread_event if _url_auto_content_server_thread is not None: _url_auto_content_server_thread_event.set() _url_auto_content_server_thread.join(3) _url_auto_content_server_thread = None cleanup(unattended_install_config.cdrom_cd1_mount) global _unattended_server_thread global _unattended_server_thread_event if _unattended_server_thread is not None: _unattended_server_thread_event.set() _unattended_server_thread.join(3) _unattended_server_thread = None time_elapsed = time.time() - start_time logging.info("Guest reported successful installation after %d s (%d min)", time_elapsed, time_elapsed / 60) if params.get("shutdown_cleanly", "yes") == "yes": shutdown_cleanly_timeout = int( params.get("shutdown_cleanly_timeout", 120)) logging.info("Wait for guest to shutdown cleanly") try: if virt_utils.wait_for(vm.is_dead, shutdown_cleanly_timeout, 1, 1): logging.info("Guest managed to shutdown cleanly") except kvm_monitor.MonitorError, e: logging.warning( "Guest apparently shut down, but got a " "monitor error: %s", e)
def run_guest_s4(test, params, env): """ Suspend guest to disk, supports both Linux & Windows OSes. @param test: kvm test object. @param params: Dictionary with test parameters. @param env: Dictionary with the test environment. """ error.base_context("before S4") vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 360)) session = vm.wait_for_login(timeout=timeout) error.context("checking whether guest OS supports S4", logging.info) session.cmd(params.get("check_s4_support_cmd")) error.context() logging.info("Waiting until all guest OS services are fully started...") time.sleep(float(params.get("services_up_timeout", 30))) # Start up a program (tcpdump for linux & ping for Windows), as a flag. # If the program died after suspend, then fails this testcase. test_s4_cmd = params.get("test_s4_cmd") session.sendline(test_s4_cmd) time.sleep(5) # Get the second session to start S4 session2 = vm.wait_for_login(timeout=timeout) # Make sure the background program is running as expected error.context("making sure background program is running") check_s4_cmd = params.get("check_s4_cmd") session2.cmd(check_s4_cmd) logging.info("Launched background command in guest: %s", test_s4_cmd) error.context() error.base_context() # Suspend to disk logging.info("Starting suspend to disk now...") session2.sendline(params.get("set_s4_cmd")) # Make sure the VM goes down error.base_context("after S4") suspend_timeout = 240 + int(params.get("smp")) * 60 if not virt_utils.wait_for(vm.is_dead, suspend_timeout, 2, 2): raise error.TestFail("VM refuses to go down. Suspend failed.") logging.info("VM suspended successfully. Sleeping for a while before " "resuming it.") time.sleep(10) # Start vm, and check whether the program is still running logging.info("Resuming suspended VM...") vm.create() # Log into the resumed VM relogin_timeout = int(params.get("relogin_timeout", 240)) logging.info("Logging into resumed VM, timeout %s", relogin_timeout) session2 = vm.wait_for_login(timeout=relogin_timeout) # Check whether the test command is still alive error.context("making sure background program is still running", logging.info) session2.cmd(check_s4_cmd) error.context() logging.info("VM resumed successfuly after suspend to disk") session2.cmd_output(params.get("kill_test_s4_cmd")) session.close() session2.close()
def run_jumbo(test, params, env): """ Test the RX jumbo frame function of vnics: 1) Boot the VM. 2) Change the MTU of guest nics and host taps depending on the NIC model. 3) Add the static ARP entry for guest NIC. 4) Wait for the MTU ok. 5) Verify the path MTU using ping. 6) Ping the guest with large frames. 7) Increment size ping. 8) Flood ping the guest with large frames. 9) Verify the path MTU. 10) Recover the MTU. @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360))) mtu = params.get("mtu", "1500") flood_time = params.get("flood_time", "300") max_icmp_pkt_size = int(mtu) - 28 ifname = vm.get_ifname(0) ip = vm.get_address(0) if ip is None: raise error.TestError("Could not get the IP address") try: # Environment preparation ethname = virt_test_utils.get_linux_ifname(session, vm.get_mac_address(0)) logging.info("Changing the MTU of guest ...") guest_mtu_cmd = "ifconfig %s mtu %s" % (ethname, mtu) session.cmd(guest_mtu_cmd) logging.info("Chaning the MTU of host tap ...") host_mtu_cmd = "ifconfig %s mtu %s" % (ifname, mtu) utils.run(host_mtu_cmd) logging.info("Add a temporary static ARP entry ...") arp_add_cmd = "arp -s %s %s -i %s" % (ip, vm.get_mac_address(0), ifname) utils.run(arp_add_cmd) def is_mtu_ok(): s, o = virt_test_utils.ping(ip, 1, interface=ifname, packetsize=max_icmp_pkt_size, hint="do", timeout=2) return s == 0 def verify_mtu(): logging.info("Verify the path MTU") s, o = virt_test_utils.ping(ip, 10, interface=ifname, packetsize=max_icmp_pkt_size, hint="do", timeout=15) if s != 0: logging.error(o) raise error.TestFail("Path MTU is not as expected") if virt_test_utils.get_loss_ratio(o) != 0: logging.error(o) raise error.TestFail("Packet loss ratio during MTU " "verification is not zero") def flood_ping(): logging.info("Flood with large frames") virt_test_utils.ping( ip, interface=ifname, packetsize=max_icmp_pkt_size, flood=True, timeout=float(flood_time) ) def large_frame_ping(count=100): logging.info("Large frame ping") s, o = virt_test_utils.ping( ip, count, interface=ifname, packetsize=max_icmp_pkt_size, timeout=float(count) * 2 ) ratio = virt_test_utils.get_loss_ratio(o) if ratio != 0: raise error.TestFail("Loss ratio of large frame ping is %s" % ratio) def size_increase_ping(step=random.randrange(90, 110)): logging.info("Size increase ping") for size in range(0, max_icmp_pkt_size + 1, step): logging.info("Ping %s with size %s", ip, size) s, o = virt_test_utils.ping(ip, 1, interface=ifname, packetsize=size, hint="do", timeout=1) if s != 0: s, o = virt_test_utils.ping( ip, 10, interface=ifname, packetsize=size, adaptive=True, hint="do", timeout=20 ) if virt_test_utils.get_loss_ratio(o) > int(params.get("fail_ratio", 50)): raise error.TestFail("Ping loss ratio is greater " "than 50% for size %s" % size) logging.info("Waiting for the MTU to be OK") wait_mtu_ok = 10 if not virt_utils.wait_for(is_mtu_ok, wait_mtu_ok, 0, 1): logging.debug(commands.getoutput("ifconfig -a")) raise error.TestError("MTU is not as expected even after %s " "seconds" % wait_mtu_ok) # Functional Test verify_mtu() large_frame_ping() size_increase_ping() # Stress test flood_ping() verify_mtu() finally: # Environment clean session.close() logging.info("Removing the temporary ARP entry") utils.run("arp -d %s -i %s" % (ip, ifname))
def run_nic_hotplug(test, params, env): """ Test hotplug of NIC devices 1) Boot up guest with one nic 2) Add a host network device through monitor cmd and check if it's added 3) Add nic device through monitor cmd and check if it's added 4) Check if new interface gets ip address 5) Disable primary link of guest 6) Ping guest new ip from host 7) Delete nic device and netdev 8) Re-enable primary link of guest BEWARE OF THE NETWORK BRIDGE DEVICE USED FOR THIS TEST ("bridge" param). The KVM autotest default bridge virbr0, leveraging libvirt, works fine for the purpose of this test. When using other bridges, the timeouts which usually happen when the bridge topology changes (that is, devices get added and removed) may cause random failures. @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ vm = virt_test_utils.get_living_vm(env, params.get("main_vm")) login_timeout = int(params.get("login_timeout", 360)) guest_delay = int(params.get("guest_delay", 20)) romfile = params.get("romfile", "") pci_model = params.get("pci_model", "rtl8139") run_dhclient = params.get("run_dhclient", "no") guest_is_not_windows = "Win" not in params.get("guest_name", "") session = virt_test_utils.wait_for_login(vm, timeout=login_timeout) if guest_is_not_windows: # Disable udev rules that usually mess up with device naming mv_stat, o = session.cmd_status_output("mv /etc/udev/rules.d/" "70-persistent-net.rules /tmp") logging.debug("command to disable udev rules returned status: %s" % mv_stat) # Modprobe the module if specified in config file module = params.get("modprobe_module") if module: session.get_command_output("modprobe %s" % module) # hot-add the nic nic_info = vm.add_nic(model=pci_model) # Only run dhclient if explicitly set and guest is not running Windows. # Most modern Linux guests run NetworkManager, and thus do not need this. if run_dhclient == "yes" and guest_is_not_windows: session_serial = vm.wait_for_serial_login(timeout=login_timeout) ifname = virt_test_utils.get_linux_ifname(session, nic_info['mac']) session_serial.cmd("dhclient %s &" % ifname) logging.info("Shutting down the primary link") vm.monitor.cmd("set_link %s off" % vm.netdev_id[0]) try: logging.info("Waiting for new nic's ip address acquisition...") if not virt_utils.wait_for(lambda: (vm.address_cache.get(nic_info['mac']) is not None), 10, 1): raise error.TestFail("Could not get ip address of new nic") ip = vm.address_cache.get(nic_info['mac']) if not virt_utils.verify_ip_address_ownership(ip, nic_info['mac']): raise error.TestFail("Could not verify the ip address of new nic") else: logging.info("Got the ip address of new nic: %s", ip) logging.info("Ping test the new nic ...") s, o = virt_test_utils.ping(ip, 100) if s != 0: logging.error(o) raise error.TestFail("New nic failed ping test") logging.info("Detaching the previously attached nic from vm") vm.del_nic(nic_info, guest_delay) finally: vm.free_mac_address(1) logging.info("Re-enabling the primary link") vm.monitor.cmd("set_link %s on" % vm.netdev_id[0]) # Attempt to put back udev network naming rules, even if the command to # disable the rules failed. We may be undoing what was done in a previous # (failed) test that never reached this point. if guest_is_not_windows: mv_stat, o = session.cmd_status_output("mv /tmp/70-persistent-net" ".rules /etc/udev/rules.d/") logging.debug("command to revert udev rules returned status: %s" % mv_stat)
def run_nic_hotplug(test, params, env): """ Test hotplug of NIC devices 1) Boot up guest with one nic 2) Add a host network device through monitor cmd and check if it's added 3) Add nic device through monitor cmd and check if it's added 4) Check if new interface gets ip address 5) Disable primary link of guest 6) Ping guest new ip from host 7) Delete nic device and netdev 8) Re-enable primary link of guest @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ vm = virt_test_utils.get_living_vm(env, params.get("main_vm")) timeout = int(params.get("login_timeout", 360)) guest_delay = int(params.get("guest_delay", 20)) session = virt_test_utils.wait_for_login(vm, timeout=timeout) romfile = params.get("romfile") # Modprobe the module if specified in config file module = params.get("modprobe_module") if module: session.get_command_output("modprobe %s" % module) def netdev_add(vm): netdev_id = virt_utils.generate_random_id() attach_cmd = ("netdev_add tap,id=%s" % netdev_id) nic_script = params.get("nic_script") if nic_script: attach_cmd += ",script=%s" % virt_utils.get_path( vm.root_dir, nic_script) netdev_extra_params = params.get("netdev_extra_params") if netdev_extra_params: attach_cmd += ",%s" % netdev_extra_params logging.info("Adding netdev through %s", attach_cmd) vm.monitor.cmd(attach_cmd) network = vm.monitor.info("network") if netdev_id not in network: logging.error(network) raise error.TestError("Fail to add netdev: %s" % netdev_id) else: return netdev_id def netdev_del(vm, n_id): vm.monitor.cmd("netdev_del %s" % n_id) network = vm.monitor.info("network") if n_id in network: logging.error(network) raise error.TestError("Fail to remove netdev %s" % n_id) def nic_add(vm, model, netdev_id, mac, rom=None): """ Add a nic to virtual machine @vm: VM object @model: nic model @netdev_id: id of netdev @mac: Mac address of new nic @rom: Rom file """ nic_id = virt_utils.generate_random_id() if model == "virtio": model = "virtio-net-pci" device_add_cmd = "device_add %s,netdev=%s,mac=%s,id=%s" % ( model, netdev_id, mac, nic_id) if rom: device_add_cmd += ",romfile=%s" % rom logging.info("Adding nic through %s", device_add_cmd) vm.monitor.cmd(device_add_cmd) qdev = vm.monitor.info("qtree") if not nic_id in qdev: logging.error(qdev) raise error.TestFail("Device %s was not plugged into qdev" "tree" % nic_id) else: return nic_id def nic_del(vm, nic_id, wait=True): """ Remove the nic from pci tree. @vm: VM object @id: the nic id @wait: Whether need to wait for the guest to unplug the device """ nic_del_cmd = "device_del %s" % nic_id vm.monitor.cmd(nic_del_cmd) if wait: logging.info("waiting for the guest to finish the unplug") if not virt_utils.wait_for( lambda: nic_id not in vm.monitor.info("qtree"), guest_delay, 5, 1): logging.error(vm.monitor.info("qtree")) raise error.TestError("Device is not unplugged by " "guest, please check whether the " "hotplug module was loaded in guest") logging.info("Attach a virtio nic to vm") mac = virt_utils.generate_mac_address(vm.instance, 1) if not mac: mac = "00:00:02:00:00:02" netdev_id = netdev_add(vm) device_id = nic_add(vm, "virtio", netdev_id, mac, romfile) if "Win" not in params.get("guest_name", ""): session.sendline("dhclient %s &" % virt_test_utils.get_linux_ifname(session, mac)) logging.info("Shutting down the primary link") vm.monitor.cmd("set_link %s down" % vm.netdev_id[0]) try: logging.info("Waiting for new nic's ip address acquisition...") if not virt_utils.wait_for( lambda: (vm.address_cache.get(mac) is not None), 10, 1): raise error.TestFail("Could not get ip address of new nic") ip = vm.address_cache.get(mac) if not virt_utils.verify_ip_address_ownership(ip, mac): raise error.TestFail("Could not verify the ip address of new nic") else: logging.info("Got the ip address of new nic: %s", ip) logging.info("Ping test the new nic ...") s, o = virt_test_utils.ping(ip, 100) if s != 0: logging.error(o) raise error.TestFail("New nic failed ping test") logging.info("Detaching a virtio nic from vm") nic_del(vm, device_id) netdev_del(vm, netdev_id) finally: vm.free_mac_address(1) logging.info("Re-enabling the primary link") vm.monitor.cmd("set_link %s up" % vm.netdev_id[0])
def run_pci_hotplug(test, params, env): """ Test hotplug of PCI devices. (Elements between [] are configurable test parameters) 1) PCI add a deivce (NIC / block) 2) Compare output of monitor command 'info pci'. 3) Compare output of guest command [reference_cmd]. 4) Verify whether pci_model is shown in [pci_find_cmd]. 5) Check whether the newly added PCI device works fine. 6) PCI delete the device, verify whether could remove the PCI device. @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 360)) session = vm.wait_for_login(timeout=timeout) # Modprobe the module if specified in config file module = params.get("modprobe_module") if module: session.cmd("modprobe %s" % module) # Get output of command 'info pci' as reference info_pci_ref = vm.monitor.info("pci") # Get output of command as reference reference = session.cmd_output(params.get("reference_cmd")) tested_model = params.get("pci_model") test_type = params.get("pci_type") image_format = params.get("image_format_stg") # Probe qemu to verify what is the supported syntax for PCI hotplug cmd_output = vm.monitor.cmd("?") if len(re.findall("\ndevice_add", cmd_output)) > 0: cmd_type = "device_add" elif len(re.findall("\npci_add", cmd_output)) > 0: cmd_type = "pci_add" else: raise error.TestError("Unknow version of qemu") # Determine syntax of drive hotplug # __com.redhat_drive_add == qemu-kvm-0.12 on RHEL 6 if len(re.findall("\n__com.redhat_drive_add", cmd_output)) > 0: drive_cmd_type = "__com.redhat_drive_add" # drive_add == qemu-kvm-0.13 onwards elif len(re.findall("\ndrive_add", cmd_output)) > 0: drive_cmd_type = "drive_add" else: raise error.TestError("Unknow version of qemu") # Probe qemu for a list of supported devices devices_support = vm.monitor.cmd("%s ?" % cmd_type) if cmd_type == "pci_add": if test_type == "nic": pci_add_cmd = "pci_add pci_addr=auto nic model=%s" % tested_model elif test_type == "block": image_params = params.object_params("stg") image_filename = virt_vm.get_image_filename(image_params, test.bindir) pci_add_cmd = ("pci_add pci_addr=auto storage file=%s,if=%s" % (image_filename, tested_model)) # Execute pci_add (should be replaced by a proper monitor method call) add_output = vm.monitor.cmd(pci_add_cmd) if not "OK domain" in add_output: raise error.TestFail("Add PCI device failed. " "Monitor command is: %s, Output: %r" % (pci_add_cmd, add_output)) after_add = vm.monitor.info("pci") elif cmd_type == "device_add": driver_id = test_type + "-" + virt_utils.generate_random_id() device_id = test_type + "-" + virt_utils.generate_random_id() if test_type == "nic": if tested_model == "virtio": tested_model = "virtio-net-pci" pci_add_cmd = "device_add id=%s,driver=%s" % (device_id, tested_model) elif test_type == "block": image_params = params.object_params("stg") image_filename = virt_vm.get_image_filename(image_params, test.bindir) controller_model = None if tested_model == "virtio": tested_model = "virtio-blk-pci" if tested_model == "scsi": tested_model = "scsi-disk" controller_model = "lsi53c895a" if len(re.findall(controller_model, devices_support)) == 0: raise error.TestError("scsi controller device (%s) not " "supported by qemu" % controller_model) if controller_model is not None: controller_id = "controller-" + device_id controller_add_cmd = ("device_add %s,id=%s" % (controller_model, controller_id)) vm.monitor.cmd(controller_add_cmd) if drive_cmd_type == "drive_add": driver_add_cmd = ("drive_add auto " "file=%s,if=none,id=%s,format=%s" % (image_filename, driver_id, image_format)) elif drive_cmd_type == "__com.redhat_drive_add": driver_add_cmd = ("__com.redhat_drive_add " "file=%s,format=%s,id=%s" % (image_filename, image_format, driver_id)) pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" % (device_id, tested_model, driver_id)) vm.monitor.cmd(driver_add_cmd) # Check if the device is support in qemu if len(re.findall(tested_model, devices_support)) > 0: add_output = vm.monitor.cmd(pci_add_cmd) else: raise error.TestError("%s doesn't support device: %s" % (cmd_type, tested_model)) after_add = vm.monitor.info("pci") if not device_id in after_add: raise error.TestFail("Add device failed. Monitor command is: %s" ". Output: %r" % (pci_add_cmd, add_output)) # Define a helper function to delete the device def pci_del(ignore_failure=False): if cmd_type == "pci_add": result_domain, bus, slot, function = add_output.split(',') domain = int(result_domain.split()[2]) bus = int(bus.split()[1]) slot = int(slot.split()[1]) pci_addr = "%x:%x:%x" % (domain, bus, slot) cmd = "pci_del pci_addr=%s" % pci_addr elif cmd_type == "device_add": cmd = "device_del %s" % device_id # This should be replaced by a proper monitor method call vm.monitor.cmd(cmd) def device_removed(): after_del = vm.monitor.info("pci") return after_del != after_add if (not virt_utils.wait_for(device_removed, 10, 0, 1) and not ignore_failure): raise error.TestFail("Failed to hot remove PCI device: %s. " "Monitor command: %s" % (tested_model, cmd)) try: # Compare the output of 'info pci' if after_add == info_pci_ref: raise error.TestFail("No new PCI device shown after executing " "monitor command: 'info pci'") # Define a helper function to compare the output def new_shown(): o = session.cmd_output(params.get("reference_cmd")) return o != reference secs = int(params.get("wait_secs_for_hook_up")) if not virt_utils.wait_for(new_shown, 30, secs, 3): raise error.TestFail("No new device shown in output of command " "executed inside the guest: %s" % params.get("reference_cmd")) # Define a helper function to catch PCI device string def find_pci(): o = session.cmd_output(params.get("find_pci_cmd")) return params.get("match_string") in o if not virt_utils.wait_for(find_pci, 30, 3, 3): raise error.TestFail("PCI %s %s device not found in guest. " "Command was: %s" % (tested_model, test_type, params.get("find_pci_cmd"))) # Test the newly added device try: session.cmd(params.get("pci_test_cmd")) except aexpect.ShellError, e: raise error.TestFail("Check for %s device failed after PCI " "hotplug. Output: %r" % (test_type, e.output)) session.close()
def run_trans_hugepage_swapping(test, params, env): """ KVM khugepage user side test: 1) Verify that the hugepages can be swapped in/out. @param test: KVM test object. @param params: Dictionary with test parameters. @param env: Dictionary with the test environment. """ def get_args(args_list): """ Get the memory arguments from system """ args_list_tmp = args_list.copy() for line in file('/proc/meminfo', 'r').readlines(): for key in args_list_tmp.keys(): if line.startswith("%s" % args_list_tmp[key]): args_list_tmp[key] = int(re.split('\s+', line)[1]) return args_list_tmp test_config = virt_test_setup.TransparentHugePageConfig(test, params) try: test_config.setup() # Swapping test logging.info("Swapping test start") # Parameters of memory information # @total: Memory size # @free: Free memory size # @swap_size: Swap size # @swap_free: Free swap size # @hugepage_size: Page size of one hugepage # @page_size: The biggest page size that app can ask for args_dict_check = {"free" : "MemFree", "swap_size" : "SwapTotal", "swap_free" : "SwapFree", "total" : "MemTotal", "hugepage_size" : "Hugepagesize",} args_dict = get_args(args_dict_check) swap_free = [] total = int(args_dict['total']) / 1024 free = int(args_dict['free']) / 1024 swap_size = int(args_dict['swap_size']) / 1024 swap_free.append(int(args_dict['swap_free'])/1024) hugepage_size = int(args_dict['hugepage_size']) / 1024 dd_timeout = float(params.get("dd_timeout", 900)) login_timeout = float(params.get("login_timeout", 360)) check_cmd_timeout = float(params.get("check_cmd_timeout", 900)) mem_path = os.path.join(test.tmpdir, 'thp_space') tmpfs_path = "/space" # If swap is enough fill all memory with dd if swap_free > (total - free): count = total / hugepage_size tmpfs_size = total else: count = free / hugepage_size tmpfs_size = free if swap_size <= 0: raise logging.info("Host does not have swap enabled") session = None try: if not os.path.isdir(mem_path): os.makedirs(mem_path) utils.run("mount -t tmpfs -o size=%sM none %s" % (tmpfs_size, mem_path)) # Set the memory size of vm # To ignore the oom killer set it to the free swap size vm = virt_test_utils.get_living_vm(env, params.get("main_vm")) if int(params['mem']) > swap_free[0]: vm.destroy() vm_name = 'vmsw' vm0 = params.get("main_vm") vm0_key = virt_utils.env_get_vm(env, vm0) params['vms'] = params['vms'] + " " + vm_name params['mem'] = str(swap_free[0]) vm_key = vm0_key.clone(vm0, params) virt_utils.env_register_vm(env, vm_name, vm_key) virt_env_process.preprocess_vm(test, params, env, vm_name) vm_key.create() session = virt_utils.wait_for(vm_key.remote_login, timeout=login_timeout) else: session = virt_test_utils.wait_for_login(vm, timeout=login_timeout) error.context("making guest to swap memory") cmd = ("dd if=/dev/zero of=%s/zero bs=%s000000 count=%s" % (mem_path, hugepage_size, count)) utils.run(cmd) args_dict = get_args(args_dict_check) swap_free.append(int(args_dict['swap_free'])/1024) if swap_free[1] - swap_free[0] >= 0: raise error.TestFail("No data was swapped to memory") # Try harder to make guest memory to be swapped session.cmd("find / -name \"*\"", timeout=check_cmd_timeout) finally: if session is not None: utils.run("umount %s" % mem_path) logging.info("Swapping test succeed") finally: session.close() test_config.cleanup()
def run_nfs_corrupt(test, params, env): """ Test if VM paused when image NFS shutdown, the drive option 'werror' should be stop, the drive option 'cache' should be none. 1) Setup NFS service on host 2) Boot up a VM using another disk on NFS server and write the disk by dd 3) Check if VM status is 'running' 4) Reject NFS connection on host 5) Check if VM status is 'paused' 6) Accept NFS connection on host and continue VM by monitor command 7) Check if VM status is 'running' @param test: kvm test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ def get_nfs_devname(params, session): """ Get the possbile name of nfs storage dev name in guest. @param params: Test params dictionary. @param session: An SSH session object. """ image1_type = params.object_params("image1").get("drive_format") stg_type = params.object_params("stg").get("drive_format") cmd = "" # Seems we can get correct 'stg' devname even if the 'stg' image # has a different type from main image (we call it 'image1' in # config file) with these 'if' sentences. if image1_type == stg_type: cmd = "ls /dev/[hsv]d[a-z]" elif stg_type == "virtio": cmd = "ls /dev/vd[a-z]" else: cmd = "ls /dev/[sh]d[a-z]" cmd += " | tail -n 1" return session.cmd_output(cmd) def check_vm_status(vm, status): """ Check if VM has the given status or not. @param vm: VM object. @param status: String with desired status. @return: True if VM status matches our desired status. @return: False if VM status does not match our desired status. """ try: vm.verify_status(status) except: return False else: return True config = NFSCorruptConfig(test, params) config.setup() params["image_name_stg"] = os.path.join(config.mnt_dir, 'nfs_corrupt') params["force_create_image_stg"] = "yes" params["create_image_stg"] = "yes" stg_params = params.object_params("stg") virt_env_process.preprocess_image(test, stg_params) vm = env.get_vm(params["main_vm"]) vm.create(params=params) session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360))) nfs_devname = get_nfs_devname(params, session) # Write disk on NFS server write_disk_cmd = "dd if=/dev/urandom of=%s" % nfs_devname logging.info("Write disk on NFS server, cmd: %s" % write_disk_cmd) session.sendline(write_disk_cmd) try: # Read some command output, it will timeout session.read_up_to_prompt(timeout=30) except: pass try: error.context("Make sure guest is running before test") vm.resume() vm.verify_status("running") try: cmd = "iptables" cmd += " -t filter" cmd += " -A INPUT" cmd += " -s localhost" cmd += " -m state" cmd += " --state NEW" cmd += " -p tcp" cmd += " --dport 2049" cmd += " -j REJECT" error.context("Reject NFS connection on host") utils.system(cmd) error.context("Check if VM status is 'paused'") if not virt_utils.wait_for( lambda: check_vm_status(vm, "paused"), int(params.get('wait_paused_timeout', 120))): raise error.TestError("Guest is not paused after stop NFS") finally: error.context("Accept NFS connection on host") cmd = "iptables" cmd += " -t filter" cmd += " -D INPUT" cmd += " -s localhost" cmd += " -m state" cmd += " --state NEW" cmd += " -p tcp" cmd += " --dport 2049" cmd += " -j REJECT" utils.system(cmd) error.context("Continue guest") vm.resume() error.context("Check if VM status is 'running'") if not virt_utils.wait_for(lambda: check_vm_status(vm, "running"), 20): raise error.TestError("Guest does not restore to 'running' status") finally: session.close() vm.destroy(gracefully=True) config.cleanup()
break else: raise e vm.verify_kernel_crash() if params.get("wait_no_ack", "no") == "no": client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: client.connect((vm.get_address(), port)) if client.recv(1024) == "done": break except (socket.error, virt_vm.VMAddressError): pass if migrate_background: vm.migrate(timeout=mig_timeout, protocol=mig_protocol) else: time.sleep(1) if params.get("wait_no_ack", "no") == "no": client.close() else: raise error.TestFail("Timeout elapsed while waiting for install to " "finish") time_elapsed = time.time() - start_time logging.info("Guest reported successful installation after %d s (%d min)", time_elapsed, time_elapsed / 60) if params.get("shutdown_cleanly", "yes") == "yes": shutdown_cleanly_timeout = int(params.get("shutdown_cleanly_timeout", 120)) logging.info("Wait for guest to shutdown cleanly") if virt_utils.wait_for(vm.is_dead, shutdown_cleanly_timeout, 1, 1): logging.info("Guest managed to shutdown cleanly")
def run_unittest(test, params, env): """ KVM RHEL-6 style unit test: 1) Resume a stopped VM 2) Wait for VM to terminate 3) If qemu exited with code = 0, the unittest passed. Otherwise, it failed 4) Collect all logs generated @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment """ unittest_dir = os.path.join(test.bindir, 'unittests') if not os.path.isdir(unittest_dir): raise error.TestError("No unittest dir %s available (did you run the " "build test first?)" % unittest_dir) os.chdir(unittest_dir) unittest_list = glob.glob('*.flat') if not unittest_list: raise error.TestError("No unittest files available (did you run the " "build test first?)") logging.debug('Flat file list: %s', unittest_list) unittest_cfg = os.path.join(unittest_dir, 'unittests.cfg') parser = ConfigParser.ConfigParser() parser.read(unittest_cfg) test_list = parser.sections() if not test_list: raise error.TestError("No tests listed on config file %s" % unittest_cfg) logging.debug('Unit test list: %s', test_list) if params.get('test_list'): test_list = params.get('test_list').split() logging.info('Original test list overriden by user') logging.info('User defined unit test list: %s', test_list) nfail = 0 tests_failed = [] timeout = int(params.get('unittest_timeout', 600)) extra_params_original = params['extra_params'] for t in test_list: logging.info('Running %s', t) flat_file = None if parser.has_option(t, 'file'): flat_file = parser.get(t, 'file') if flat_file is None: nfail += 1 tests_failed.append(t) logging.error('Unittest config file %s has section %s but no ' 'mandatory option file', unittest_cfg, t) continue if flat_file not in unittest_list: nfail += 1 tests_failed.append(t) logging.error('Unittest file %s referenced in config file %s but ' 'was not find under the unittest dir', flat_file, unittest_cfg) continue smp = None if parser.has_option(t, 'smp'): smp = int(parser.get(t, 'smp')) params['smp'] = smp extra_params = None if parser.has_option(t, 'extra_params'): extra_params = parser.get(t, 'extra_params') params['extra_params'] += ' %s' % extra_params vm_name = params.get("main_vm") params['kernel'] = os.path.join(unittest_dir, flat_file) testlog_path = os.path.join(test.debugdir, "%s.log" % t) try: try: vm_name = params.get('main_vm') virt_env_process.preprocess_vm(test, params, env, vm_name) vm = env.get_vm(vm_name) vm.create() vm.monitor.cmd("cont") logging.info("Waiting for unittest %s to complete, timeout %s, " "output in %s", t, timeout, vm.get_testlog_filename()) if not virt_utils.wait_for(vm.is_dead, timeout): raise error.TestFail("Timeout elapsed (%ss)" % timeout) # Check qemu's exit status status = vm.process.get_status() if status != 0: nfail += 1 tests_failed.append(t) logging.error("Unit test %s failed", t) except Exception, e: nfail += 1 tests_failed.append(t) logging.error('Exception happened during %s: %s', t, str(e)) finally: try: shutil.copy(vm.get_testlog_filename(), testlog_path) logging.info("Unit test log collected and available under %s", testlog_path) except (NameError, IOError): logging.error("Not possible to collect logs") # Restore the extra params so other tests can run normally params['extra_params'] = extra_params_original if nfail != 0: raise error.TestFail("Unit tests failed: %s" % " ".join(tests_failed))