def reboot(self, session=None, method="shell", nic_index=0, timeout=240): """ Reboot the VM and wait for it to come back up by trying to log in until timeout expires. @param session: A shell session object or None. @param method: Reboot method. Can be "shell" (send a shell reboot command). @param nic_index: Index of NIC to access in the VM, when logging in after rebooting. @param timeout: Time to wait for login to succeed (after rebooting). @return: A new shell session object. """ error.base_context("rebooting '%s'" % self.name, logging.info) error.context("before reboot") session = session or self.login() error.context() if method == "shell": session.sendline(self.params.get("reboot_command")) else: raise virt_vm.VMRebootError("Unknown reboot method: %s" % method) error.context("waiting for guest to go down", logging.info) if not virt_utils.wait_for( lambda: not session.is_responsive(timeout=30), 120, 0, 1): raise virt_vm.VMRebootError("Guest refuses to go down") session.close() error.context("logging in after reboot", logging.info) return self.wait_for_login(nic_index, timeout=timeout)
def reboot(self, session=None, method="shell", nic_index=0, timeout=240): """ Reboot the VM and wait for it to come back up by trying to log in until timeout expires. @param session: A shell session object or None. @param method: Reboot method. Can be "shell" (send a shell reboot command). @param nic_index: Index of NIC to access in the VM, when logging in after rebooting. @param timeout: Time to wait for login to succeed (after rebooting). @return: A new shell session object. """ error.base_context("rebooting '%s'" % self.name, logging.info) error.context("before reboot") session = session or self.login() error.context() if method == "shell": session.sendline(self.params.get("reboot_command")) else: raise virt_vm.VMRebootError("Unknown reboot method: %s" % method) error.context("waiting for guest to go down", logging.info) if not virt_utils.wait_for(lambda: not session.is_responsive(timeout=30), 120, 0, 1): raise virt_vm.VMRebootError("Guest refuses to go down") session.close() error.context("logging in after reboot", logging.info) return self.wait_for_login(nic_index, timeout=timeout)
def run_stress_boot(test, params, env): """ Boots VMs until one of them becomes unresponsive, and records the maximum number of VMs successfully started: 1) boot the first vm 2) boot the second vm cloned from the first vm, check whether it boots up and all booted vms respond to shell commands 3) go on until cannot create VM anymore or cannot allocate memory for VM @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment. """ error.base_context("waiting for the first guest to be up", logging.info) vm = env.get_vm(params["main_vm"]) vm.verify_alive() login_timeout = float(params.get("login_timeout", 240)) session = vm.wait_for_login(timeout=login_timeout) num = 2 sessions = [session] # Boot the VMs try: while num <= int(params.get("max_vms")): # Clone vm according to the first one error.base_context("booting guest #%d" % num, logging.info) vm_name = "vm%d" % num vm_params = vm.params.copy() curr_vm = vm.clone(vm_name, vm_params) env.register_vm(vm_name, curr_vm) kvm_preprocessing.preprocess_vm(test, vm_params, env, vm_name) params["vms"] += " " + vm_name sessions.append(curr_vm.wait_for_login(timeout=login_timeout)) logging.info("Guest #%d booted up successfully", num) # Check whether all previous shell sessions are responsive for i, se in enumerate(sessions): error.context("checking responsiveness of guest #%d" % (i + 1), logging.debug) se.cmd(params.get("alive_test_cmd")) num += 1 finally: for se in sessions: se.close() logging.info("Total number booted: %d" % (num - 1))
def run_stress_boot(test, params, env): """ Boots VMs until one of them becomes unresponsive, and records the maximum number of VMs successfully started: 1) boot the first vm 2) boot the second vm cloned from the first vm, check whether it boots up and all booted vms respond to shell commands 3) go on until cannot create VM anymore or cannot allocate memory for VM @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment. """ error.base_context("waiting for the first guest to be up", logging.info) vm = env.get_vm(params["main_vm"]) vm.verify_alive() login_timeout = float(params.get("login_timeout", 240)) session = vm.wait_for_login(timeout=login_timeout) num = 2 sessions = [session] # Boot the VMs try: while num <= int(params.get("max_vms")): # Clone vm according to the first one error.base_context("booting guest #%d" % num, logging.info) vm_name = "vm%d" % num vm_params = vm.params.copy() curr_vm = vm.clone(vm_name, vm_params) env.register_vm(vm_name, curr_vm) virt_env_process.preprocess_vm(test, vm_params, env, vm_name) params["vms"] += " " + vm_name sessions.append(curr_vm.wait_for_login(timeout=login_timeout)) logging.info("Guest #%d booted up successfully", num) # Check whether all previous shell sessions are responsive for i, se in enumerate(sessions): error.context("checking responsiveness of guest #%d" % (i + 1), logging.debug) se.cmd(params.get("alive_test_cmd")) num += 1 finally: for se in sessions: se.close() logging.info("Total number booted: %d" % (num -1))
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()
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 kvm_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 render_answer_file(self): """ Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey provided for this test and replace the KVM_TEST_MEDIUM with the tree url or nfs address provided for this test. @return: Answer file contents """ error.base_context('Rendering final answer file') error.context('Reading answer file %s' % self.unattended_file) unattended_contents = open(self.unattended_file).read() dummy_cdkey_re = r'\bKVM_TEST_CDKEY\b' real_cdkey = os.environ.get('KVM_TEST_cdkey') if re.search(dummy_cdkey_re, unattended_contents): if real_cdkey: unattended_contents = re.sub(dummy_cdkey_re, real_cdkey, unattended_contents) else: print("WARNING: 'cdkey' required but not specified for " "this unattended installation") dummy_medium_re = r'\bKVM_TEST_MEDIUM\b' if self.medium == "cdrom": content = "cdrom" elif self.medium == "url": content = "url --url %s" % self.url elif self.medium == "nfs": content = "nfs --server=%s --dir=%s" % (self.nfs_server, self.nfs_dir) else: raise ValueError("Unexpected installation medium %s" % self.url) unattended_contents = re.sub(dummy_medium_re, content, unattended_contents) def replace_virtio_key(contents, dummy_re, env): """ Replace a virtio dummy string with contents. If install_virtio is not set, replace it with a dummy string. @param contents: Contents of the unattended file @param dummy_re: Regular expression used to search on the. unattended file contents. @param env: Name of the environment variable. """ dummy_path = "C:" driver = os.environ.get(env, '') if re.search(dummy_re, contents): if self.install_virtio == "yes": if driver.endswith("msi"): driver = 'msiexec /passive /package ' + driver else: try: # Let's escape windows style paths properly drive, path = driver.split(":") driver = drive + ":" + re.escape(path) except: pass contents = re.sub(dummy_re, driver, contents) else: contents = re.sub(dummy_re, dummy_path, contents) return contents vdict = { r'\bKVM_TEST_STORAGE_DRIVER_PATH\b': 'KVM_TEST_virtio_storage_path', r'\bKVM_TEST_NETWORK_DRIVER_PATH\b': 'KVM_TEST_virtio_network_path', r'\bKVM_TEST_VIRTIO_NETWORK_INSTALLER\b': 'KVM_TEST_virtio_network_installer_path' } for vkey in vdict: unattended_contents = replace_virtio_key(unattended_contents, vkey, vdict[vkey]) logging.debug("Unattended install contents:") for line in unattended_contents.splitlines(): logging.debug(line) return unattended_contents
def render_answer_file(self): """ Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey provided for this test and replace the KVM_TEST_MEDIUM with the tree url or nfs address provided for this test. @return: Answer file contents """ error.base_context("Rendering final answer file") error.context("Reading answer file %s" % self.unattended_file) unattended_contents = open(self.unattended_file).read() dummy_cdkey_re = r"\bKVM_TEST_CDKEY\b" if re.search(dummy_cdkey_re, unattended_contents): if self.cdkey: unattended_contents = re.sub(dummy_cdkey_re, self.cdkey, unattended_contents) else: print("WARNING: 'cdkey' required but not specified for " "this unattended installation") dummy_medium_re = r"\bKVM_TEST_MEDIUM\b" if self.medium == "cdrom": content = "cdrom" elif self.medium == "url": content = "url --url %s" % self.url elif self.medium == "nfs": content = "nfs --server=%s --dir=%s" % (self.nfs_server, self.nfs_dir) else: raise ValueError("Unexpected installation medium %s" % self.url) unattended_contents = re.sub(dummy_medium_re, content, unattended_contents) def replace_virtio_key(contents, dummy_re, attribute_name): """ Replace a virtio dummy string with contents. If install_virtio is not set, replace it with a dummy string. @param contents: Contents of the unattended file @param dummy_re: Regular expression used to search on the. unattended file contents. @param env: Name of the environment variable. """ dummy_path = "C:" driver = getattr(self, attribute_name, "") if re.search(dummy_re, contents): if self.install_virtio == "yes": if driver.endswith("msi"): driver = "msiexec /passive /package " + driver else: try: # Let's escape windows style paths properly drive, path = driver.split(":") driver = drive + ":" + re.escape(path) except: pass contents = re.sub(dummy_re, driver, contents) else: contents = re.sub(dummy_re, dummy_path, contents) return contents vdict = { r"\bKVM_TEST_STORAGE_DRIVER_PATH\b": "virtio_storage_path", r"\bKVM_TEST_NETWORK_DRIVER_PATH\b": "virtio_network_path", r"\bKVM_TEST_VIRTIO_NETWORK_INSTALLER\b": "virtio_network_installer_path", } for vkey in vdict: unattended_contents = replace_virtio_key( contents=unattended_contents, dummy_re=vkey, attribute_name=vdict[vkey] ) logging.debug("Unattended install contents:") for line in unattended_contents.splitlines(): logging.debug(line) return unattended_contents