KVM test: kvm_subprocess: raise exceptions in kvm_shell_session and kvm_expect
PATCH 1/5 KVM test: kvm_subprocess: raise exceptions in kvm_shell_session and kvm_expect
- Instead of relying solely on returned values, raise exceptions when something goes wrong in any of the variants of get_command_status_output() and read_until_output_matches().
- Introduce cmd() which executes a command and raises an exception if the command's exit status is nonzero (in addition to the exceptions raised by get_command_status_output()). If the exit status is zero, cmd() returns the command's output.
- Modify all tests to account for these changes:
* When cmd() is appropriate, use it.
* When the command is not required to succeed, use get_command_output().
* Otherwise leave code as it is or wrap it in try..except to catch the new exceptions.
- Set auto_close=True by default for kvm_expect (like it is set for kvm_shell_session). Otherwise, an unhandled exception might leave a kvm_expect session open forever.
--- client/tests/kvm/kvm_subprocess.py | 321 +++++++++++++++++-------- client/tests/kvm/kvm_test_utils.py | 75 +++--- client/tests/kvm/kvm_utils.py | 119 +++++----- client/tests/kvm/kvm_vm.py | 10 +- client/tests/kvm/tests/ethtool.py | 17 +- client/tests/kvm/tests/file_transfer.py | 5 +- client/tests/kvm/tests/guest_test.py | 24 +-- client/tests/kvm/tests/iofuzz.py | 18 +- client/tests/kvm/tests/ioquit.py | 9 +- client/tests/kvm/tests/jumbo.py | 6 +- client/tests/kvm/tests/ksm_overcommit.py | 16 +- client/tests/kvm/tests/linux_s3.py | 9 +- client/tests/kvm/tests/mac_change.py | 6 +- client/tests/kvm/tests/migration.py | 8 +- client/tests/kvm/tests/multicast.py | 10 +- client/tests/kvm/tests/netperf.py | 13 +- client/tests/kvm/tests/nic_promisc.py | 16 +- client/tests/kvm/tests/nicdriver_unload.py | 16 +- client/tests/kvm/tests/pci_hotplug.py | 10 +- client/tests/kvm/tests/stress_boot.py | 4 +- client/tests/kvm/tests/virtio_console.py | 4 +- client/tests/kvm/tests/vlan.py | 34 +-- client/tests/kvm/tests/whql_client_install.py | 21 +- client/tests/kvm/tests/whql_submission.py | 22 +- 24 files changed, 426 insertions(+), 367 deletions(-)
client/tests/kvm/kvm_subprocess.py +217 -104
@@ -189,6 +189,88 @@ import subprocess, time, signal, re, threading, logging import common, kvm_utils +class ExpectError(Exception): + def __init__(self, patterns, output): + Exception.__init__(self, patterns, output) + self.patterns = patterns + self.output = output + + def _pattern_str(self): + if len(self.patterns) == 1: + return "pattern %r" % self.patterns[0] + else: + return "patterns %r" % self.patterns + + def __str__(self): + return ("Unknown error occurred while looking for %s (output: %r)" % + (self._pattern_str(), self.output)) + + +class ExpectTimeoutError(ExpectError): + def __str__(self): + return ("Timeout expired while looking for %s (output: %r)" % + (self._pattern_str(), self.output)) + + +class ExpectProcessTerminatedError(ExpectError): + def __init__(self, patterns, status, output): + ExpectError.__init__(self, patterns, output) + self.status = status + + def __str__(self): + return ("Process terminated while looking for %s (status: %s, output: " + "%r)" % (self._pattern_str(), self.status, self.output)) + + +class ShellError(Exception): + def __init__(self, cmd, output): + Exception.__init__(self, cmd, output) + self.cmd = cmd + self.output = output + + def __str__(self): + return ("Could not execute shell command %r (output: %r)" % + (self.cmd, self.output)) + + +class ShellTimeoutError(ShellError): + def __str__(self): + return ("Timeout expired while waiting for shell command %r to " + "complete (output: %r)" % (self.cmd, self.output)) + + +class ShellProcessTerminatedError(ShellError): + # Raised when the shell process itself (e.g. ssh, netcat, telnet) + # terminates unexpectedly + def __init__(self, cmd, status, output): + ShellError.__init__(self, cmd, output) + self.status = status + + def __str__(self): + return ("Shell process terminated while waiting for command %r to " + "complete (status: %s, output: %r)" % + (self.cmd, self.status, self.output)) + + +class ShellCmdError(ShellError): + # Raised when a command executed in a shell terminates with a nonzero + # exit code (status) + def __init__(self, cmd, status, output): + ShellError.__init__(self, cmd, output) + self.status = status + + def __str__(self): + return ("Shell command %r failed with status %d (output: %r)" % + (self.cmd, self.status, self.output)) + + +class ShellStatusError(ShellError): + # Raised when the command's exit status cannot be obtained + def __str__(self): + return ("Could not get exit status of command %r (output: %r)" % + (self.cmd, self.output)) + + def run_bg(command, termination_func=None, output_func=None, output_prefix="", timeout=1.0): """
@@ -773,7 +855,7 @@ class kvm_expect(kvm_tail): It also provides all of kvm_tail's functionality. """ - def __init__(self, command=None, id=None, auto_close=False, echo=False, + def __init__(self, command=None, id=None, auto_close=True, echo=False, linesep="\n", termination_func=None, termination_params=(), output_func=None, output_params=(), output_prefix=""): """
@@ -876,13 +958,14 @@ class kvm_expect(kvm_tail): @param internal_timeout: The timeout to pass to read_nonblocking @param print_func: A function to be used to print the data being read (should take a string parameter) - @return: Tuple containing the match index (or None if no match was - found) and the data read so far. + @return: Tuple containing the match index and the data read so far + @raise ExpectTimeoutError: Raised if timeout expires + @raise ExpectProcessTerminatedError: Raised if the child process + terminates while waiting for output + @raise ExpectError: Raised if an unknown error occurs """ - match = None - data = "" - fd = self._get_fd("expect") + o = "" end_time = time.time() + timeout while True: try:
@@ -890,38 +973,28 @@ class kvm_expect(kvm_tail): max(0, end_time - time.time())) except (select.error, TypeError): break - if fd not in r: - break + if not r: + raise ExpectTimeoutError(patterns, o) # Read data from child - newdata = self.read_nonblocking(internal_timeout) + data = self.read_nonblocking(internal_timeout) + if not data: + break # Print it if necessary - if print_func and newdata: - str = newdata - if str.endswith("\n"): - str = str[:-1] - for line in str.split("\n"): + if print_func: + for line in data.splitlines(): print_func(line) - data += newdata - - done = False # Look for patterns - match = self.match_patterns(filter(data), patterns) + o += data + match = self.match_patterns(filter(o), patterns) if match is not None: - done = True - # Check if child has died - if not self.is_alive(): - logging.debug("Process terminated with status %s" % - self.get_status()) - done = True - # Are we done? - if done: break - - # Print some debugging info - if match is None and (self.is_alive() or self.get_status() != 0): - logging.debug("Timeout elapsed or process terminated. Output:" + - kvm_utils.format_str_for_message(data.strip())) + return match, o - return (match, data) + # Check if the child has terminated + if kvm_utils.wait_for(lambda: not self.is_alive(), 5, 0, 0.1): + raise ExpectProcessTerminatedError(patterns, self.get_status(), o) + else: + # This shouldn't happen + raise ExpectError(patterns, o) def read_until_last_word_matches(self, patterns, timeout=30.0,
@@ -936,8 +1009,11 @@ class kvm_expect(kvm_tail): @param internal_timeout: The timeout to pass to read_nonblocking @param print_func: A function to be used to print the data being read (should take a string parameter) - @return: A tuple containing the match index (or None if no match was - found) and the data read so far. + @return: A tuple containing the match index and the data read so far + @raise ExpectTimeoutError: Raised if timeout expires + @raise ExpectProcessTerminatedError: Raised if the child process + terminates while waiting for output + @raise ExpectError: Raised if an unknown error occurs """ def get_last_word(str): if str:
@@ -967,6 +1043,11 @@ class kvm_expect(kvm_tail): @param internal_timeout: The timeout to pass to read_nonblocking @param print_func: A function to be used to print the data being read (should take a string parameter) + @return: A tuple containing the match index and the data read so far + @raise ExpectTimeoutError: Raised if timeout expires + @raise ExpectProcessTerminatedError: Raised if the child process + terminates while waiting for output + @raise ExpectError: Raised if an unknown error occurs """ def get_last_nonempty_line(str): nonempty_lines = [l for l in str.splitlines() if l.strip()]
@@ -1101,31 +1182,34 @@ class kvm_shell_session(kvm_expect): @param print_func: A function to be used to print the data being read (should take a string parameter) - @return: A tuple containing True/False indicating whether the prompt - was found, and the data read so far. + @return: The data read so far + @raise ExpectTimeoutError: Raised if timeout expires + @raise ExpectProcessTerminatedError: Raised if the shell process + terminates while waiting for output + @raise ExpectError: Raised if an unknown error occurs """ - (match, output) = self.read_until_last_line_matches([self.prompt], - timeout, - internal_timeout, - print_func) - return (match is not None, output) + m, o = self.read_until_last_line_matches([self.prompt], timeout, + internal_timeout, print_func) + return o - def get_command_status_output(self, command, timeout=30.0, - internal_timeout=None, print_func=None): + def get_command_output(self, cmd, timeout=30.0, internal_timeout=None, + print_func=None): """ - Send a command and return its exit status and output. + Send a command and return its output. - @param command: Command to send (must not contain newline characters) - @param timeout: The duration (in seconds) to wait until a match is - found + @param cmd: Command to send (must not contain newline characters) + @param timeout: The duration (in seconds) to wait for the prompt to + return @param internal_timeout: The timeout to pass to read_nonblocking @param print_func: A function to be used to print the data being read (should take a string parameter) - @return: A tuple (status, output) where status is the exit status or - None if no exit status is available (e.g. timeout elapsed), and - output is the output of command. + @return: The output of cmd + @raise ShellTimeoutError: Raised if timeout expires + @raise ShellProcessTerminatedError: Raised if the shell process + terminates while waiting for output + @raise ShellError: Raised if an unknown error occurs """ def remove_command_echo(str, cmd): if str and str.splitlines()[0] == cmd:
@@ -1135,79 +1219,108 @@ class kvm_shell_session(kvm_expect): def remove_last_nonempty_line(str): return "".join(str.rstrip().splitlines(True)[:-1]) - # Print some debugging info - logging.debug("Sending command: %s" % command) - - # Read everything that's waiting to be read + logging.debug("Sending command: %s" % cmd) self.read_nonblocking(timeout=0) + self.sendline(cmd) + try: + o = self.read_up_to_prompt(timeout, internal_timeout, print_func) + except ExpectError, e: + o = remove_command_echo(e.output, cmd) + if isinstance(e, ExpectTimeoutError): + raise ShellTimeoutError(cmd, o) + elif isinstance(e, ExpectProcessTerminatedError): + raise ShellProcessTerminatedError(cmd, e.status, o) + else: + raise ShellError(cmd, o) - # Send the command and get its output - self.sendline(command) - (match, output) = self.read_up_to_prompt(timeout, internal_timeout, - print_func) - # Remove the echoed command from the output - output = remove_command_echo(output, command) - # If the prompt was not found, return the output so far - if not match: - return (None, output) - # Remove the final shell prompt from the output - output = remove_last_nonempty_line(output) - - # Send the 'echo ...' command to get the last exit status - self.sendline(self.status_test_command) - (match, status) = self.read_up_to_prompt(10.0, internal_timeout) - if not match: - return (None, output) - status = remove_command_echo(status, self.status_test_command) - status = remove_last_nonempty_line(status) - # Get the first line consisting of digits only - digit_lines = [l for l in status.splitlines() if l.strip().isdigit()] - if not digit_lines: - return (None, output) - status = int(digit_lines[0].strip()) + # Remove the echoed command and the final shell prompt + return remove_last_nonempty_line(remove_command_echo(o, cmd)) + + + def get_command_status_output(self, cmd, timeout=30.0, + internal_timeout=None, print_func=None): + """ + Send a command and return its exit status and output. + + @param cmd: Command to send (must not contain newline characters) + @param timeout: The duration (in seconds) to wait for the prompt to + return + @param internal_timeout: The timeout to pass to read_nonblocking + @param print_func: A function to be used to print the data being read + (should take a string parameter) - # Print some debugging info - if status != 0: - logging.debug("Command failed; status: %d, output:%s", status, - kvm_utils.format_str_for_message(output.strip())) + @return: A tuple (status, output) where status is the exit status and + output is the output of cmd + @raise ShellTimeoutError: Raised if timeout expires + @raise ShellProcessTerminatedError: Raised if the shell process + terminates while waiting for output + @raise ShellStatusError: Raised if the exit status cannot be obtained + @raise ShellError: Raised if an unknown error occurs + """ + o = self.get_command_output(cmd, timeout, internal_timeout, print_func) + try: + # Send the 'echo $?' (or equivalent) command to get the exit status + s = self.get_command_output(self.status_test_command, 10, + internal_timeout) + except ShellError: + raise ShellStatusError(cmd, o) - return (status, output) + # Get the first line consisting of digits only + digit_lines = [l for l in s.splitlines() if l.strip().isdigit()] + if digit_lines: + return int(digit_lines[0].strip()), o + else: + raise ShellStatusError(cmd, o) - def get_command_status(self, command, timeout=30.0, internal_timeout=None, + def get_command_status(self, cmd, timeout=30.0, internal_timeout=None, print_func=None): """ Send a command and return its exit status. - @param command: Command to send - @param timeout: The duration (in seconds) to wait until a match is - found + @param cmd: Command to send (must not contain newline characters) + @param timeout: The duration (in seconds) to wait for the prompt to + return @param internal_timeout: The timeout to pass to read_nonblocking @param print_func: A function to be used to print the data being read (should take a string parameter) - @return: Exit status or None if no exit status is available (e.g. - timeout elapsed). + @return: The exit status of cmd + @raise ShellTimeoutError: Raised if timeout expires + @raise ShellProcessTerminatedError: Raised if the shell process + terminates while waiting for output + @raise ShellStatusError: Raised if the exit status cannot be obtained + @raise ShellError: Raised if an unknown error occurs """ - (status, output) = self.get_command_status_output(command, timeout, - internal_timeout, - print_func) - return status + s, o = self.get_command_status_output(cmd, timeout, internal_timeout, + print_func) + return s - def get_command_output(self, command, timeout=30.0, internal_timeout=None, - print_func=None): + def cmd(self, cmd, timeout=30.0, internal_timeout=None, print_func=None): """ - Send a command and return its output. + Send a command and return its output. If the command's exit status is + nonzero, raise an exception. - @param command: Command to send - @param timeout: The duration (in seconds) to wait until a match is - found + @param cmd: Command to send (must not contain newline characters) + @param timeout: The duration (in seconds) to wait for the prompt to + return @param internal_timeout: The timeout to pass to read_nonblocking @param print_func: A function to be used to print the data being read (should take a string parameter) - """ - (status, output) = self.get_command_status_output(command, timeout, - internal_timeout, - print_func) - return output + + @return: The output of cmd + @raise ShellTimeoutError: Raised if timeout expires + @raise ShellProcessTerminatedError: Raised if the shell process + terminates while waiting for output + @raise ShellError: Raised if the exit status cannot be obtained or if + an unknown error occurs + @raise ShellStatusError: Raised if the exit status cannot be obtained + @raise ShellError: Raised if an unknown error occurs + @raise ShellCmdError: Raised if the exit status is nonzero + """ + s, o = self.get_command_status_output(cmd, timeout, internal_timeout, + print_func) + if s != 0: + raise ShellCmdError(cmd, s, o) + return o
client/tests/kvm/kvm_test_utils.py +38 -37
114 lines not on this page · raw message
client/tests/kvm/kvm_utils.py +61 -58
143 lines not on this page · raw message
client/tests/kvm/kvm_vm.py +2 -8
@@ -1213,11 +1213,7 @@ class VM: if not session: return None try: - cmd = self.params.get("cpu_chk_cmd") - s, count = session.get_command_status_output(cmd) - if s == 0: - return int(count) - return None + return int(session.cmd(self.params.get("cpu_chk_cmd"))) finally: session.close()
@@ -1235,9 +1231,7 @@ class VM: try: if not cmd: cmd = self.params.get("mem_chk_cmd") - s, mem_str = session.get_command_status_output(cmd) - if s != 0: - return None + mem_str = session.cmd(cmd) mem = re.findall("([0-9]+)", mem_str) mem_size = 0 for m in mem:
client/tests/kvm/tests/ethtool.py +9 -8
@@ -1,7 +1,7 @@ import logging, commands, re from autotest_lib.client.common_lib import error from autotest_lib.client.bin import utils -import kvm_test_utils, kvm_utils +import kvm_test_utils, kvm_utils, kvm_subprocess def run_ethtool(test, params, env): """
@@ -32,7 +32,7 @@ def run_ethtool(test, params, env): 'gro': 'generic.*receive.*offload', 'lro': 'large.*receive.*offload', } - s, o = session.get_command_status_output("ethtool -k %s" % ethname) + o = session.cmd("ethtool -k %s" % ethname) try: return re.findall("%s: (.*)" % feature_pattern.get(type), o)[0] except IndexError:
@@ -92,13 +92,13 @@ def run_ethtool(test, params, env): @param src: Source host of transfer file @return: Tuple (status, error msg/tcpdump result) """ - session2.get_command_status("rm -rf %s" % filename) - dd_cmd = "dd if=/dev/urandom of=%s bs=1M count=%s" % (filename, - params.get("filesize")) + session2.get_command_output("rm -rf %s" % filename) + dd_cmd = ("dd if=/dev/urandom of=%s bs=1M count=%s" % + (filename, params.get("filesize"))) logging.info("Creat file in source host, cmd: %s" % dd_cmd) tcpdump_cmd = "tcpdump -lep -s 0 tcp -vv port ssh" if src == "guest": - s = session.get_command_status(dd_cmd, timeout=360) + session.get_command_output(dd_cmd, timeout=360) tcpdump_cmd += " and src %s" % guest_ip copy_files_fun = vm.copy_files_from else:
@@ -125,8 +125,9 @@ def run_ethtool(test, params, env): logging.info("Transfer file completed") if session.get_command_status("killall tcpdump") != 0: return (False, "Could not kill all tcpdump process") - s, tcpdump_string = session2.read_up_to_prompt(timeout=60) - if not s: + try: + tcpdump_string = session2.read_up_to_prompt(timeout=60) + except kvm_subprocess.ExpectError: return (False, "Fail to read tcpdump's output") if not compare_md5sum(filename):
client/tests/kvm/tests/file_transfer.py +1 -4
@@ -80,10 +80,7 @@ def run_file_transfer(test, params, env): finally: logging.info('Cleaning temp file on guest') clean_cmd += " %s" % guest_path - s, o = session.get_command_status_output(clean_cmd) - if s: - logging.warning("Failed to clean remote file %s, output:%s", - guest_path, o) + session.cmd(clean_cmd) logging.info('Cleaning temp files on host') os.remove('%s/a.out' % dir) os.remove('%s/c.out' % dir)
client/tests/kvm/tests/guest_test.py +8 -16
@@ -48,14 +48,12 @@ def run_guest_test(test, params, env): # Change dir to dst_rsc_dir, and remove the guest script dir there rm_cmd = "cd %s && (rmdir /s /q %s || del /s /q %s)" % \ (dst_rsc_dir, rsc_dir, rsc_dir) - if session.get_command_status(rm_cmd, timeout=test_timeout) != 0: - raise error.TestFail("Remove %s failed." % rsc_dir) + session.cmd(rm_cmd, timeout=test_timeout) logging.debug("Clean directory succeeded.") # then download the resource. rsc_cmd = "cd %s && %s %s" %(dst_rsc_dir, download_cmd, rsc_server) - if session.get_command_status(rsc_cmd, timeout=test_timeout) != 0: - raise error.TestFail("Download test resource failed.") + session.cmd(rsc_cmd, timeout=test_timeout) logging.info("Download resource finished.") else: session.get_command_output("del %s" % dst_rsc_path,
@@ -63,19 +61,13 @@ def run_guest_test(test, params, env): script_path = kvm_utils.get_path(test.bindir, script) vm.copy_files_to(script_path, dst_rsc_path, timeout=60) - command = "cmd /c %s %s %s" %(interpreter, dst_rsc_path, script_params) + cmd = "cmd /c %s %s %s" %(interpreter, dst_rsc_path, script_params) - logging.info("---------------- Script output ----------------") - status = session.get_command_status(command, - print_func=logging.info, - timeout=test_timeout) - logging.info("---------------- End of script output ----------------") - - if status is None: - raise error.TestFail("Timeout expired before script execution " - "completed (or something weird happened)") - if status != 0: - raise error.TestFail("Script execution failed") + try: + logging.info("------------ Script output ------------") + session.cmd(cmd, print_func=logging.info, timeout=test_timeout) + finally: + logging.info("------------ End of script output ------------") if reboot == "yes": logging.debug("Rebooting guest after test ...")
client/tests/kvm/tests/iofuzz.py +8 -10
@@ -33,11 +33,10 @@ def run_iofuzz(test, params, env): logging.debug("outb(0x%x, 0x%x)", port, data) outb_cmd = ("echo -e '\\%s' | dd of=/dev/port seek=%d bs=1 count=1" % (oct(data), port)) - s, o = session.get_command_status_output(outb_cmd) - if s is None: - logging.debug("Command did not return") - if s != 0: - logging.debug("Command returned status %s", s) + try: + session.cmd(outb_cmd) + except kvm_subprocess.ShellError, e: + logging.debug(e) def inb(session, port):
@@ -49,11 +48,10 @@ def run_iofuzz(test, params, env): """ logging.debug("inb(0x%x)", port) inb_cmd = "dd if=/dev/port seek=%d of=/dev/null bs=1 count=1" % port - s, o = session.get_command_status_output(inb_cmd) - if s is None: - logging.debug("Command did not return") - if s != 0: - logging.debug("Command returned status %s", s) + try: + session.cmd(inb_cmd) + except kvm_subprocess.ShellError, e: + logging.debug(e) def fuzz(session, inst_list):
client/tests/kvm/tests/ioquit.py +3 -6
@@ -20,16 +20,13 @@ def run_ioquit(test, params, env): try: bg_cmd = params.get("background_cmd") logging.info("Add IO workload for guest OS.") - (s, o) = session.get_command_status_output(bg_cmd, timeout=60) + session.get_command_output(bg_cmd, timeout=60) check_cmd = params.get("check_cmd") - (s, o) = session2.get_command_status_output(check_cmd, timeout=60) - if s: - raise error.TestError("Fail to add IO workload for Guest OS") + session2.cmd(check_cmd, timeout=60) logging.info("Sleep for a while") time.sleep(random.randrange(30,100)) - (s, o) = session2.get_command_status_output(check_cmd, timeout=60) - if s: + if session2.get_command_status(check_cmd, timeout=60) != 0: logging.info("IO workload finished before the VM was killed") logging.info("Kill the virtual machine") vm.process.close()
client/tests/kvm/tests/jumbo.py +1 -5
@@ -40,11 +40,7 @@ def run_jumbo(test, params, env): logging.info("Changing the MTU of guest ...") guest_mtu_cmd = "ifconfig %s mtu %s" % (ethname , mtu) - s, o = session.get_command_status_output(guest_mtu_cmd) - if s != 0: - logging.error(o) - raise error.TestError("Fail to set the MTU of guest NIC: %s" % - ethname) + session.cmd(guest_mtu_cmd) logging.info("Chaning the MTU of host tap ...") host_mtu_cmd = "ifconfig %s mtu %s" % (ifname, mtu)
client/tests/kvm/tests/ksm_overcommit.py +6 -10
@@ -29,7 +29,7 @@ def run_ksm_overcommit(test, params, env): session.sendline("python /tmp/allocator.py") (match, data) = session.read_until_last_line_matches(["PASS:", "FAIL:"], timeout) - if match == 1 or match is None: + if match != 0: raise error.TestFail("Command allocator.py on guest %s failed.\n" "return code: %s\n output:\n%s" % (vm.name, match, data))
@@ -52,7 +52,7 @@ def run_ksm_overcommit(test, params, env): session.sendline(command) (match, data) = session.read_until_last_line_matches(["PASS:","FAIL:"], timeout) - if match == 1 or match is None: + if match != 0: raise error.TestFail("Failed to execute '%s' on allocator.py, " "vm: %s, output:\n%s" % (command, vm.name, data))
@@ -80,9 +80,7 @@ def run_ksm_overcommit(test, params, env): vm = lvms[lsessions.index(session)] logging.debug("Turning off swap on vm %s" % vm.name) - ret = session.get_command_status("swapoff -a", timeout=300) - if ret is None or ret: - raise error.TestFail("Failed to swapoff on VM %s" % vm.name) + session.cmd("swapoff -a", timeout=300) # Start the allocator _start_allocator(vm, session, 60 * perf_ratio)
@@ -232,7 +230,7 @@ def run_ksm_overcommit(test, params, env): (mem / 200 * 50 * perf_ratio)) logging.debug(kvm_test_utils.get_memory_info([lvms[last_vm]])) - (status, data) = lsessions[i].get_command_status_output("die()", 20) + lsessions[i].get_command_output("die()", 20) lvms[last_vm].destroy(gracefully = False) logging.info("Phase 3b: PASS")
@@ -253,9 +251,7 @@ def run_ksm_overcommit(test, params, env): raise error.TestFail("Could not log into guest %s" % vm.name) - ret = session.get_command_status("swapoff -a", timeout=300) - if ret != 0: - raise error.TestFail("Failed to turn off swap on %s" % vm.name) + session.cmd("swapoff -a", timeout=300) for i in range(0, max_alloc): # Start the allocator
@@ -360,7 +356,7 @@ def run_ksm_overcommit(test, params, env): logging.debug("Cleaning up...") for i in range(0, max_alloc): - lsessions[i].get_command_status_output("die()", 20) + lsessions[i].get_command_output("die()", 20) session.close() vm.destroy(gracefully = False)
client/tests/kvm/tests/linux_s3.py +2 -7
@@ -16,10 +16,7 @@ def run_linux_s3(test, params, env): session = kvm_test_utils.wait_for_login(vm, timeout=timeout) logging.info("Checking that VM supports S3") - status = session.get_command_status("grep -q mem /sys/power/state") - if status == None: - logging.error("Failed to check if S3 exists") - elif status != 0: + if session.get_command_status("grep -q mem /sys/power/state") != 0: raise error.TestFail("Guest does not support S3") logging.info("Waiting for a while for X to start")
@@ -38,9 +35,7 @@ def run_linux_s3(test, params, env): command = "chvt %s && echo mem > /sys/power/state && chvt %s" % (dst_tty, src_tty) suspend_timeout = 120 + int(params.get("smp")) * 60 - status = session.get_command_status(command, timeout=suspend_timeout) - if status != 0: - raise error.TestFail("Suspend to mem failed") + session.cmd(command, timeout=suspend_timeout) logging.info("VM resumed after S3")
client/tests/kvm/tests/mac_change.py +2 -4
@@ -35,13 +35,11 @@ def run_mac_change(test, params, env): 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)) - if session.get_command_status(change_cmd) != 0: - raise error.TestFail("Fail to send mac_change command") + session.cmd(change_cmd) # Verify whether MAC address was changed to the new one logging.info("Verifying the new mac address") - if session.get_command_status("ifconfig | grep -i %s" % new_mac) != 0: - raise error.TestFail("Fail to change MAC address") + session.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")
client/tests/kvm/tests/migration.py +2 -6
@@ -42,9 +42,7 @@ def run_migration(test, params, env): try: check_command = params.get("migration_bg_check_command", "") - if session2.get_command_status(check_command, timeout=30) != 0: - raise error.TestError("Could not start background process '%s'" % - background_command) + session2.cmd(check_command, timeout=30) session2.close() # Migrate the VM
@@ -59,9 +57,7 @@ def run_migration(test, params, env): logging.info("Logged in after migration") # Make sure the background process is still running - if session2.get_command_status(check_command, timeout=30) != 0: - raise error.TestFail("Could not find running background process " - "after migration: '%s'" % background_command) + session2.cmd(check_command, timeout=30) # Get the output of migration_test_command output = session2.get_command_output(test_command)
client/tests/kvm/tests/multicast.py +5 -5
@@ -1,7 +1,7 @@ import logging, os, re from autotest_lib.client.common_lib import error from autotest_lib.client.bin import utils -import kvm_test_utils +import kvm_test_utils, kvm_subprocess def run_multicast(test, params, env):
@@ -23,10 +23,10 @@ def run_multicast(test, params, env): timeout=int(params.get("login_timeout", 360))) def run_guest(cmd): - s, o = session.get_command_status_output(cmd) - if s: - logging.warning('Command %s executed in guest returned exit code ' - '%s, output: %s', cmd, s, o.strip()) + try: + session.cmd(cmd) + except kvm_subprocess.ShellError, e: + logging.warn(e) def run_host_guest(cmd): run_guest(cmd)
client/tests/kvm/tests/netperf.py +6 -7
@@ -1,7 +1,7 @@ import logging, commands, os from autotest_lib.client.common_lib import error from autotest_lib.client.bin import utils -import kvm_test_utils +import kvm_test_utils, kvm_subprocess def run_netperf(test, params, env): """
@@ -32,14 +32,13 @@ def run_netperf(test, params, env): if not vm.copy_files_to(os.path.join(netperf_dir, i), "/tmp"): raise error.TestError("Could not copy file %s to guest" % i) - if session.get_command_status(firewall_flush): + try: + session.cmd(firewall_flush): + except kvm_subprocess.ShellError: logging.warning("Could not flush firewall rules on guest") - if session.get_command_status(setup_cmd % "/tmp", timeout=200): - raise error.TestFail("Fail to setup netperf on guest") - - if session.get_command_status(params.get("netserver_cmd") % "/tmp"): - raise error.TestFail("Fail to start netperf server on guest") + session.cmd(setup_cmd % "/tmp", timeout=200) + session.cmd(params.get("netserver_cmd") % "/tmp") try: logging.info("Setup and run netperf client on host")
client/tests/kvm/tests/nic_promisc.py +4 -12
@@ -29,13 +29,8 @@ def run_nic_promisc(test, params, env): raise error.TestFail("Could not log into guest '%s'" % vm.name) def compare(filename): - cmd = "md5sum %s" % filename md5_host = utils.hash_file(filename, method="md5") - rc_guest, md5_guest = session.get_command_status_output(cmd) - if rc_guest: - logging.debug("Could not get MD5 hash for file %s on guest," - "output: %s", filename, md5_guest) - return False + md5_guest = session.cmd("md5sum %s" % filename) md5_guest = md5_guest.split()[0] if md5_host != md5_guest: logging.error("MD5 hash mismatch between file %s "
@@ -72,10 +67,7 @@ def run_nic_promisc(test, params, env): success_counter += 1 logging.info("Create %s bytes file on guest" % size) - if session.get_command_status(dd_cmd % (filename, int(size)), - timeout=100) != 0: - logging.error("Create file on guest failed") - continue + session.cmd(dd_cmd % (filename, int(size)), timeout=100) logging.info("Transfer file from guest to host") if not vm.copy_files_from(filename, filename):
@@ -90,12 +82,12 @@ def run_nic_promisc(test, params, env): logging.info("Clean temporary files") cmd = "rm -f %s" % filename utils.run(cmd) - session.get_command_status(cmd) + session.get_command_output(cmd) finally: logging.info("Restore the %s to the nonpromisc mode", ethname) session2.close() - session.get_command_status("ip link set %s promisc off" % ethname) + session.get_command_output("ip link set %s promisc off" % ethname) session.close() if success_counter != 2 * len(file_size):
client/tests/kvm/tests/nicdriver_unload.py +6 -10
@@ -28,9 +28,7 @@ def run_nicdriver_unload(test, params, env): ethname = kvm_test_utils.get_linux_ifname(session, vm.get_mac_address(0)) sys_path = "/sys/class/net/%s/device/driver" % (ethname) - s, o = session.get_command_status_output('readlink -e %s' % sys_path) - if s: - raise error.TestError("Could not find driver name") + o = session.cmd("readlink -e %s" % sys_path) driver = os.path.basename(o.strip()) logging.info("driver is %s", driver)
@@ -45,12 +43,8 @@ def run_nicdriver_unload(test, params, env): logging.debug("Failed to transfer file %s", remote_file) def compare(origin_file, receive_file): - cmd = "md5sum %s" check_sum1 = utils.hash_file(origin_file, method="md5") - s, output2 = session.get_command_status_output(cmd % receive_file) - if s != 0: - logging.error("Could not get md5sum of receive_file") - return False + output2 = session.cmd("md5sum %s" % receive_file) check_sum2 = output2.strip().split()[0] logging.debug("original file md5: %s, received file md5: %s", check_sum1, check_sum2)
@@ -77,9 +71,11 @@ def run_nicdriver_unload(test, params, env): logging.info("Unload/load NIC driver repeatedly in guest...") while True: logging.debug("Try to unload/load nic drive once") - if session2.get_command_status(unload_load_cmd, timeout=120) != 0: + try: + session2.cmd(unload_load_cmd, timeout=120) + except: session.get_command_output("rm -rf /tmp/Thread-*") - raise error.TestFail("Unload/load nic driver failed") + raise pid, s = os.waitpid(pid, os.WNOHANG) status = os.WEXITSTATUS(s) if (pid, status) != (0, 0):
client/tests/kvm/tests/pci_hotplug.py +5 -5
@@ -26,8 +26,7 @@ def run_pci_hotplug(test, params, env): # Modprobe the module if specified in config file module = params.get("modprobe_module") if module: - if session.get_command_status("modprobe %s" % module): - raise error.TestError("Modprobe module '%s' failed" % module) + session.cmd("modprobe %s" % module) # Get output of command 'info pci' as reference info_pci_ref = vm.monitor.info("pci")
@@ -152,10 +151,11 @@ def run_pci_hotplug(test, params, env): params.get("find_pci_cmd"))) # Test the newly added device - s, o = session.get_command_status_output(params.get("pci_test_cmd")) - if s != 0: + try: + session.cmd(params.get("pci_test_cmd")) + except kvm_subprocess.ShellError, e: raise error.TestFail("Check for %s device failed after PCI " - "hotplug. Output: %r" % (test_type, o)) + "hotplug. Output: %r" % (test_type, e.output)) session.close()
client/tests/kvm/tests/stress_boot.py +3 -1
@@ -51,7 +51,9 @@ def run_stress_boot(tests, params, env): # check whether all previous shell sessions are responsive for i, se in enumerate(sessions): - if se.get_command_status(params.get("alive_test_cmd")) != 0: + try: + se.cmd(params.get("alive_test_cmd")) + except kvm_subprocess.ShellError: raise error.TestFail("Session #%d is not responsive" % i) num += 1
client/tests/kvm/tests/virtio_console.py +2 -2
@@ -366,7 +366,7 @@ def run_virtio_console(test, params, env): "echo -n 'FAIL: Compile virtio_guest failed'") (match, data) = vm[1].read_until_last_line_matches(["PASS:", "FAIL:"], timeout) - if match == 1 or match is None: + if match != 0: raise error.TestFail("Command console_switch.py on guest %s failed." "\nreturn code: %s\n output:\n%s" % (vm[0].name, match, data))
@@ -376,7 +376,7 @@ def run_virtio_console(test, params, env): "echo -n 'FAIL: virtio_guest failed'") (match, data) = vm[1].read_until_last_line_matches(["PASS:", "FAIL:"], timeout) - if match == 1 or match is None: + if match != 0: raise error.TestFail("Command console_switch.py on guest %s failed." "\nreturn code: %s\n output:\n%s" % (vm[0].name, match, data))
client/tests/kvm/tests/vlan.py +13 -21
@@ -1,6 +1,6 @@ import logging, time, re from autotest_lib.client.common_lib import error -import kvm_test_utils, kvm_utils +import kvm_test_utils, kvm_utils, kvm_subprocess def run_vlan(test, params, env): """
@@ -35,18 +35,15 @@ def run_vlan(test, params, env): vm.append(kvm_test_utils.get_living_vm(env, "vm2")) def add_vlan(session, id, iface="eth0"): - if session.get_command_status("vconfig add %s %s" % (iface, id)) != 0: - raise error.TestError("Fail to add %s.%s" % (iface, id)) + session.cmd("vconfig add %s %s" % (iface, id)) def set_ip_vlan(session, id, ip, iface="eth0"): iface = "%s.%s" % (iface, id) - if session.get_command_status("ifconfig %s %s" % (iface, ip)) != 0: - raise error.TestError("Fail to configure ip for %s" % iface) + session.cmd("ifconfig %s %s" % (iface, ip)) def set_arp_ignore(session, iface="eth0"): ignore_cmd = "echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore" - if session.get_command_status(ignore_cmd) != 0: - raise error.TestError("Fail to set arp_ignore of %s" % session) + session.cmd(ignore_cmd) def rem_vlan(session, id, iface="eth0"): rem_vlan_cmd = "if [[ -e /proc/net/vlan/%s ]];then vconfig rem %s;fi"
@@ -65,11 +62,12 @@ def run_vlan(test, params, env): time.sleep(2) #send file from src to dst send_cmd = send_cmd % (vlan_ip[dst], str(nc_port), "file") - if session[src].get_command_status(send_cmd, timeout = 60) != 0: + if session[src].get_command_status(send_cmd, timeout=60) != 0: raise error.TestFail ("Fail to send file" " from vm%s to vm%s" % (src+1, dst+1)) - s, o = session[dst].read_up_to_prompt(timeout=60) - if s != True: + try: + session[dst].read_up_to_prompt(timeout=60) + except kvm_subprocess.ExpectError: raise error.TestFail ("Fail to receive file" " from vm%s to vm%s" % (src+1, dst+1)) #check MD5 message digest of receive file in dst
@@ -81,7 +79,7 @@ def run_vlan(test, params, env): logging.info("digest_origin is %s" % digest_origin[src]) logging.info("digest_receive is %s" % digest_receive) raise error.TestFail("File transfered differ from origin") - session[dst].get_command_status("rm -f receive") + session[dst].get_command_output("rm -f receive") for i in range(2): session.append(kvm_test_utils.wait_for_login(vm[i],
@@ -97,22 +95,16 @@ def run_vlan(test, params, env): #produce sized file in vm dd_cmd = "dd if=/dev/urandom of=file bs=1024k count=%s" - if session[i].get_command_status(dd_cmd % file_size) != 0: - raise error.TestFail("File producing failed") + session[i].cmd(dd_cmd % file_size) #record MD5 message digest of file - s, output =session[i].get_command_status_output("md5sum file", - timeout=60) - if s != 0: - raise error.TestFail("File MD5_checking failed" ) + output = session[i].cmd("md5sum file", timeout=60) digest_origin.append(re.findall(r'(\w+)', output)[0]) #stop firewall in vm - session[i].get_command_status("/etc/init.d/iptables stop") + session[i].get_command_output("/etc/init.d/iptables stop") #load 8021q module for vconfig - load_8021q_cmd = "modprobe 8021q" - if session[i].get_command_status(load_8021q_cmd) != 0: - raise error.TestError("Fail to load 8021q module on VM%s" % i) + session[i].cmd("modprobe 8021q") try: for i in range(2):
client/tests/kvm/tests/whql_client_install.py +8 -13
@@ -50,6 +50,7 @@ def run_whql_client_install(test, params, env): server_session = kvm_utils.remote_login("nc", server_address, server_shell_port, "", "", session.prompt, session.linesep) + server_session.set_status_test_command(session.status_test_command) # Get server and client information cmd = "echo %computername%"
@@ -67,10 +68,10 @@ def run_whql_client_install(test, params, env): server_dns_suffix = "" # Delete the client machine from the server's data store (if it's there) - server_session.get_command_output("cd %s" % server_studio_path) + server_session.cmd("cd %s" % server_studio_path) cmd = "%s %s %s" % (os.path.basename(dsso_delete_machine_binary), server_name, client_name) - server_session.get_command_output(cmd, print_func=logging.info) + server_session.cmd(cmd, print_func=logging.info) server_session.close() # Rename the client machine
@@ -78,21 +79,18 @@ def run_whql_client_install(test, params, env): logging.info("Renaming client machine to '%s'" % client_name) cmd = ('wmic computersystem where name="%%computername%%" rename name="%s"' % client_name) - if session.get_command_status(cmd, timeout=600) != 0: - raise error.TestError("Could not rename the client machine") + session.cmd(cmd, timeout=600) # Join the server's workgroup logging.info("Joining workgroup '%s'" % server_workgroup) cmd = ('wmic computersystem where name="%%computername%%" call ' 'joindomainorworkgroup name="%s"' % server_workgroup) - if session.get_command_status(cmd, timeout=600) != 0: - raise error.TestError("Could not change the client's workgroup") + session.cmd(cmd, timeout=600) # Set the client machine's DNS suffix logging.info("Setting DNS suffix to '%s'" % server_dns_suffix) cmd = 'reg add %s /v Domain /d "%s" /f' % (regkey, server_dns_suffix) - if session.get_command_status(cmd, timeout=300) != 0: - raise error.TestError("Could not set the client's DNS suffix") + session.cmd(cmd, timeout=300) # Reboot session = kvm_test_utils.reboot(vm, session)
@@ -103,8 +101,7 @@ def run_whql_client_install(test, params, env): server_password) end_time = time.time() + 120 while time.time() < end_time: - s = session.get_command_status(cmd) - if s == 0: + if session.get_command_status(cmd) == 0: break time.sleep(5) else:
@@ -114,7 +111,5 @@ def run_whql_client_install(test, params, env): # Install logging.info("Installing DTM client (timeout=%ds)", install_timeout) install_cmd = r"cmd /c \\%s\%s" % (server_name, install_cmd.lstrip("\\")) - if session.get_command_status(install_cmd, timeout=install_timeout) != 0: - raise error.TestError("Client installation failed") - + session.cmd(install_cmd, timeout=install_timeout) session.close()
client/tests/kvm/tests/whql_submission.py +14 -8
@@ -50,6 +50,7 @@ def run_whql_submission(test, params, env): server_session = kvm_utils.remote_login("nc", server_address, server_shell_port, "", "", session.prompt, session.linesep) + server_session.set_status_test_command(session.status_test_command) # Get the computer names of the server and client cmd = "echo %computername%"
@@ -58,7 +59,7 @@ def run_whql_submission(test, params, env): session.close() # Run the automation program on the server - server_session.get_command_output("cd %s" % server_studio_path) + server_session.cmd("cd %s" % server_studio_path) cmd = "%s %s %s %s %s %s" % (os.path.basename(dsso_test_binary), server_name, client_name,
@@ -78,8 +79,8 @@ def run_whql_submission(test, params, env): if errors: raise error.TestError(errors[0]) else: - raise error.TestError("Error running automation program: could " - "not find '%s' prompt" % prompt) + raise error.TestError("Error running automation program: " + "could not find '%s' prompt" % prompt) # Tell the automation program which device to test find_prompt("Device to test:")
@@ -108,10 +109,15 @@ def run_whql_submission(test, params, env): server_session.sendline() # Wait for the automation program to terminate - m, o = server_session.read_up_to_prompt(print_func=logging.info, - timeout=test_timeout + 300) - # (test_timeout + 300 is used here because the automation program is - # supposed to terminate cleanly on its own when test_timeout expires) + try: + o = server_session.read_up_to_prompt(print_func=logging.info, + timeout=test_timeout + 300) + # (test_timeout + 300 is used here because the automation program is + # supposed to terminate cleanly on its own when test_timeout expires) + done = True + except kvm_subprocess.ExpectError, e: + o = e.output + done = False server_session.close() # Look for test results in the automation program's output
@@ -167,7 +173,7 @@ def run_whql_submission(test, params, env): logging.info("(see logs and HTML reports in %s)" % test.debugdir) # Kill the VM and fail if the automation program did not terminate on time - if not m: + if not done: vm.destroy() raise error.TestFail("The automation program did not terminate " "on time")
Signature · 1 line
1.5.5.6
PATCH 2/5 KVM test: kvm_monitor.py: store attributes in QMPCmdError
For consistency with the exceptions in kvm_subprocess.
--- client/tests/kvm/kvm_monitor.py | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)
client/tests/kvm/kvm_monitor.py +8 -2
@@ -38,9 +38,15 @@ class MonitorNotSupportedError(MonitorError): class QMPCmdError(MonitorError): + def __init__(self, cmd, qmp_args, data): + MonitorError.__init__(self, cmd, qmp_args, data) + self.cmd = cmd + self.qmp_args = qmp_args + self.data = data + def __str__(self): - return ("QMP command '%s' failed (arguments: %r, error message: %r)" % - tuple(self.args)) + return ("QMP command %r failed (arguments: %r, error message: %r)" % + (self.cmd, self.qmp_args, self.data)) class Monitor:
Signature · 1 line
1.5.5.6
PATCH 3/5 KVM test: add utility functions start_windows_service() and stop_windows_service()
These utilities use sc to stop and start windows services. They're used by whql_submission and whql_client_install to stop or restart wttsvc on the client machine.
Resending because this seems never to have been committed.
--- client/tests/kvm/kvm_test_utils.py | 46 ++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-)
client/tests/kvm/kvm_test_utils.py +46 -0
@@ -241,6 +241,52 @@ def migrate(vm, env=None, mig_timeout=3600, mig_protocol="tcp", return dest_vm +def stop_windows_service(session, service, timeout=120): + """ + Stop a Windows service using sc. + If the service is already stopped or is not installed, do nothing. + + @param service: The name of the service + @param timeout: Time duration to wait for service to stop + @raise error.TestError: Raised if the service can't be stopped + """ + end_time = time.time() + timeout + while time.time() < end_time: + o = session.get_command_output("sc stop %s" % service, timeout=60) + # FAILED 1060 means the service isn't installed. + # FAILED 1062 means the service hasn't been started. + if re.search(r"\bFAILED (1060|1062)\b", o, re.I): + break + time.sleep(1) + else: + raise error.TestError("Could not stop service '%s'" % service) + + +def start_windows_service(session, service, timeout=120): + """ + Start a Windows service using sc. + If the service is already running, do nothing. + If the service isn't installed, fail. + + @param service: The name of the service + @param timeout: Time duration to wait for service to start + @raise error.TestError: Raised if the service can't be started + """ + end_time = time.time() + timeout + while time.time() < end_time: + o = session.get_command_output("sc start %s" % service, timeout=60) + # FAILED 1060 means the service isn't installed. + if re.search(r"\bFAILED 1060\b", o, re.I): + raise error.TestError("Could not start service '%s' " + "(service not installed)" % service) + # FAILED 1056 means the service is already running. + if re.search(r"\bFAILED 1056\b", o, re.I): + break + time.sleep(1) + else: + raise error.TestError("Could not start service '%s'" % service) + + def get_time(session, time_command, time_filter_re, time_format): """ Return the host time and guest time. If the guest time cannot be fetched
Signature · 1 line
1.5.5.6
PATCH 4/5 KVM test: kvm_subprocess: rename get_command_status_output() and friends
get_command_status_output() -> cmd_status_output() get_command_output() -> cmd_output() get_command_status() -> cmd_status()
--- client/tests/kvm/kvm_subprocess.py | 27 +++++++++---------- client/tests/kvm/kvm_test_utils.py | 20 +++++++------- client/tests/kvm/tests/ethtool.py | 16 ++++++------ client/tests/kvm/tests/guest_s4.py | 8 +++--- client/tests/kvm/tests/guest_test.py | 3 +- client/tests/kvm/tests/iofuzz.py | 2 +- client/tests/kvm/tests/ioquit.py | 4 +- client/tests/kvm/tests/iozone_windows.py | 4 +- client/tests/kvm/tests/ksm_overcommit.py | 4 +- client/tests/kvm/tests/linux_s3.py | 4 +- client/tests/kvm/tests/migration.py | 7 ++--- client/tests/kvm/tests/multicast.py | 8 +++--- client/tests/kvm/tests/netperf.py | 4 +- client/tests/kvm/tests/nic_promisc.py | 4 +- client/tests/kvm/tests/nicdriver_unload.py | 4 +- client/tests/kvm/tests/pci_hotplug.py | 6 ++-- client/tests/kvm/tests/physical_resources_check.py | 2 +- client/tests/kvm/tests/timedrift.py | 2 +- client/tests/kvm/tests/vlan.py | 11 +++---- client/tests/kvm/tests/whql_client_install.py | 10 +++--- client/tests/kvm/tests/whql_submission.py | 4 +- 21 files changed, 75 insertions(+), 79 deletions(-)
client/tests/kvm/kvm_subprocess.py +13 -14
@@ -1103,7 +1103,7 @@ class kvm_shell_session(kvm_expect): @param prompt: Regular expression describing the shell's prompt line. @param status_test_command: Command to be used for getting the last exit status of commands run inside the shell (used by - get_command_status_output() and friends). + cmd_status_output() and friends). """ # Init the superclass kvm_expect.__init__(self, command, id, auto_close, echo, linesep,
@@ -1193,8 +1193,8 @@ class kvm_shell_session(kvm_expect): return o - def get_command_output(self, cmd, timeout=30.0, internal_timeout=None, - print_func=None): + def cmd_output(self, cmd, timeout=30.0, internal_timeout=None, + print_func=None): """ Send a command and return its output.
@@ -1237,8 +1237,8 @@ class kvm_shell_session(kvm_expect): return remove_last_nonempty_line(remove_command_echo(o, cmd)) - def get_command_status_output(self, cmd, timeout=30.0, - internal_timeout=None, print_func=None): + def cmd_status_output(self, cmd, timeout=30.0, internal_timeout=None, + print_func=None): """ Send a command and return its exit status and output.
@@ -1257,11 +1257,10 @@ class kvm_shell_session(kvm_expect): @raise ShellStatusError: Raised if the exit status cannot be obtained @raise ShellError: Raised if an unknown error occurs """ - o = self.get_command_output(cmd, timeout, internal_timeout, print_func) + o = self.cmd_output(cmd, timeout, internal_timeout, print_func) try: # Send the 'echo $?' (or equivalent) command to get the exit status - s = self.get_command_output(self.status_test_command, 10, - internal_timeout) + s = self.cmd_output(self.status_test_command, 10, internal_timeout) except ShellError: raise ShellStatusError(cmd, o)
@@ -1273,8 +1272,8 @@ class kvm_shell_session(kvm_expect): raise ShellStatusError(cmd, o) - def get_command_status(self, cmd, timeout=30.0, internal_timeout=None, - print_func=None): + def cmd_status(self, cmd, timeout=30.0, internal_timeout=None, + print_func=None): """ Send a command and return its exit status.
@@ -1292,8 +1291,8 @@ class kvm_shell_session(kvm_expect): @raise ShellStatusError: Raised if the exit status cannot be obtained @raise ShellError: Raised if an unknown error occurs """ - s, o = self.get_command_status_output(cmd, timeout, internal_timeout, - print_func) + s, o = self.cmd_status_output(cmd, timeout, internal_timeout, + print_func) return s
@@ -1319,8 +1318,8 @@ class kvm_shell_session(kvm_expect): @raise ShellError: Raised if an unknown error occurs @raise ShellCmdError: Raised if the exit status is nonzero """ - s, o = self.get_command_status_output(cmd, timeout, internal_timeout, - print_func) + s, o = self.cmd_status_output(cmd, timeout, internal_timeout, + print_func) if s != 0: raise ShellCmdError(cmd, s, o) return o
client/tests/kvm/kvm_test_utils.py +10 -10
@@ -252,7 +252,7 @@ def stop_windows_service(session, service, timeout=120): """ end_time = time.time() + timeout while time.time() < end_time: - o = session.get_command_output("sc stop %s" % service, timeout=60) + o = session.cmd_output("sc stop %s" % service, timeout=60) # FAILED 1060 means the service isn't installed. # FAILED 1062 means the service hasn't been started. if re.search(r"\bFAILED (1060|1062)\b", o, re.I):
@@ -274,7 +274,7 @@ def start_windows_service(session, service, timeout=120): """ end_time = time.time() + timeout while time.time() < end_time: - o = session.get_command_output("sc start %s" % service, timeout=60) + o = session.cmd_output("sc start %s" % service, timeout=60) # FAILED 1060 means the service isn't installed. if re.search(r"\bFAILED 1060\b", o, re.I): raise error.TestError("Could not start service '%s' "
@@ -306,7 +306,7 @@ def get_time(session, time_command, time_filter_re, time_format): """ if len(re.findall("ntpdate|w32tm", time_command)) == 0: host_time = time.time() - s = session.get_command_output(time_command) + s = session.cmd_output(time_command) try: s = re.findall(time_filter_re, s)[0]
@@ -398,7 +398,7 @@ def run_autotest(vm, session, control_path, timeout, outputdir): copy = False local_hash = utils.hash_file(local_path) basename = os.path.basename(local_path) - output = session.get_command_output("md5sum %s" % remote_path) + output = session.cmd_output("md5sum %s" % remote_path) if "such file" in output: remote_hash = "0" elif output:
@@ -451,7 +451,7 @@ def run_autotest(vm, session, control_path, timeout, outputdir): Get the status of the tests that were executed on the host and close the session where autotest was being executed. """ - output = session.get_command_output("cat results/*/status") + output = session.cmd_output("cat results/*/status") try: results = scan_results.parse_results(output) # Report test results
@@ -509,8 +509,8 @@ def run_autotest(vm, session, control_path, timeout, outputdir): try: try: logging.info("---------------- Test output ----------------") - session.get_command_output("bin/autotest control", timeout=timeout, - print_func=logging.info) + session.cmd_output("bin/autotest control", timeout=timeout, + print_func=logging.info) finally: logging.info("------------- End of test output ------------") except kvm_subprocess.ShellTimeoutError:
@@ -588,8 +588,8 @@ def raw_ping(command, timeout, session, output_func): return status, output else: try: - output = session.get_command_output(command, timeout=timeout, - print_func=output_func) + output = session.cmd_output(command, timeout=timeout, + print_func=output_func) except kvm_subprocess.ShellTimeoutError: # Send ctrl+c (SIGINT) through ssh session session.send("\003")
@@ -671,7 +671,7 @@ def get_linux_ifname(session, mac_address): @mac_address: the macaddress of nic """ - output = session.get_command_output("ifconfig -a") + output = session.cmd_output("ifconfig -a") try: ethname = re.findall("(\w+)\s+Link.*%s" % mac_address, output,
client/tests/kvm/tests/ethtool.py +8 -8
@@ -51,7 +51,7 @@ def run_ethtool(test, params, env): return False cmd = "ethtool -K %s %s %s" % (ethname, type, status) if ethtool_get(type) != status: - return session.get_command_status(cmd) == 0 + return session.cmd_status(cmd) == 0 if ethtool_get(type) != status: logging.error("Fail to set %s %s" % (type, status)) return False
@@ -74,7 +74,7 @@ def run_ethtool(test, params, env): logging.info("Compare md5sum of the files on guest and host") host_result = utils.hash_file(name, method="md5") try: - o = session.get_command_output("md5sum %s" % name) + o = session.cmd_output("md5sum %s" % name) guest_result = re.findall("\w+", o)[0] except IndexError: logging.error("Could not get file md5sum in guest")
@@ -92,13 +92,13 @@ def run_ethtool(test, params, env): @param src: Source host of transfer file @return: Tuple (status, error msg/tcpdump result) """ - session2.get_command_output("rm -rf %s" % filename) + session2.cmd_output("rm -rf %s" % filename) dd_cmd = ("dd if=/dev/urandom of=%s bs=1M count=%s" % (filename, params.get("filesize"))) logging.info("Creat file in source host, cmd: %s" % dd_cmd) tcpdump_cmd = "tcpdump -lep -s 0 tcp -vv port ssh" if src == "guest": - session.get_command_output(dd_cmd, timeout=360) + session.cmd_output(dd_cmd, timeout=360) tcpdump_cmd += " and src %s" % guest_ip copy_files_fun = vm.copy_files_from else:
@@ -115,15 +115,15 @@ def run_ethtool(test, params, env): tcpdump_cmd += " and not port %s" % i logging.debug("Listen by command: %s" % tcpdump_cmd) session2.sendline(tcpdump_cmd) - if not kvm_utils.wait_for(lambda: session.get_command_status( - "pgrep tcpdump") == 0, 30): + cmd = "pgrep tcpdump" + if not kvm_utils.wait_for(lambda: session.cmd_status(cmd) == 0, 30): return (False, "Tcpdump process wasn't launched") logging.info("Start to transfer file") if not copy_files_fun(filename, filename): return (False, "Child process transfer file failed") logging.info("Transfer file completed") - if session.get_command_status("killall tcpdump") != 0: + if session.cmd_status("killall tcpdump") != 0: return (False, "Could not kill all tcpdump process") try: tcpdump_string = session2.read_up_to_prompt(timeout=60)
@@ -174,7 +174,7 @@ def run_ethtool(test, params, env): session = kvm_test_utils.wait_for_login(vm, timeout=int(params.get("login_timeout", 360))) # Let's just error the test if we identify that there's no ethtool installed - if session.get_command_status("ethtool -h"): + if session.cmd_status("ethtool -h"): raise error.TestError("Command ethtool not installed on guest") session2 = kvm_test_utils.wait_for_login(vm, timeout=int(params.get("login_timeout", 360)))
client/tests/kvm/tests/guest_s4.py +4 -4
@@ -16,7 +16,7 @@ def run_guest_s4(test, params, env): session = kvm_test_utils.wait_for_login(vm, timeout=timeout) logging.info("Checking whether guest OS supports suspend to disk (S4)...") - s, o = session.get_command_status_output(params.get("check_s4_support_cmd")) + s, o = session.cmd_status_output(params.get("check_s4_support_cmd")) if "not enough space" in o: raise error.TestError("Check S4 support failed: %s" % o) elif s != 0:
@@ -36,7 +36,7 @@ def run_guest_s4(test, params, env): # Make sure the background program is running as expected check_s4_cmd = params.get("check_s4_cmd") - if session2.get_command_status(check_s4_cmd) != 0: + if session2.cmd_status(check_s4_cmd) != 0: raise error.TestError("Failed to launch '%s' as a background process" % test_s4_cmd) logging.info("Launched background command in guest: %s" % test_s4_cmd)
@@ -68,11 +68,11 @@ def run_guest_s4(test, params, env): # Check whether the test command is still alive logging.info("Checking if background command is still alive...") - if session2.get_command_status(check_s4_cmd) != 0: + if session2.cmd_status(check_s4_cmd) != 0: raise error.TestFail("Background command '%s' stopped running. S4 " "failed." % test_s4_cmd) logging.info("VM resumed successfuly after suspend to disk") - session2.get_command_output(params.get("kill_test_s4_cmd")) + session2.cmd_output(params.get("kill_test_s4_cmd")) session.close() session2.close()
client/tests/kvm/tests/guest_test.py +1 -2
@@ -56,8 +56,7 @@ def run_guest_test(test, params, env): session.cmd(rsc_cmd, timeout=test_timeout) logging.info("Download resource finished.") else: - session.get_command_output("del %s" % dst_rsc_path, - internal_timeout=0) + session.cmd_output("del %s" % dst_rsc_path, internal_timeout=0) script_path = kvm_utils.get_path(test.bindir, script) vm.copy_files_to(script_path, dst_rsc_path, timeout=60)
client/tests/kvm/tests/iofuzz.py +1 -1
@@ -98,7 +98,7 @@ def run_iofuzz(test, params, env): r = random.SystemRandom() logging.info("Enumerate guest devices through /proc/ioports") - ioports = session.get_command_output("cat /proc/ioports") + ioports = session.cmd_output("cat /proc/ioports") logging.debug(ioports) devices = re.findall("(\w+)-(\w+)\ : (.*)", ioports)
client/tests/kvm/tests/ioquit.py +2 -2
@@ -20,13 +20,13 @@ def run_ioquit(test, params, env): try: bg_cmd = params.get("background_cmd") logging.info("Add IO workload for guest OS.") - session.get_command_output(bg_cmd, timeout=60) + session.cmd_output(bg_cmd, timeout=60) check_cmd = params.get("check_cmd") session2.cmd(check_cmd, timeout=60) logging.info("Sleep for a while") time.sleep(random.randrange(30,100)) - if session2.get_command_status(check_cmd, timeout=60) != 0: + if session2.cmd_status(check_cmd, timeout=60) != 0: logging.info("IO workload finished before the VM was killed") logging.info("Kill the virtual machine") vm.process.close()
client/tests/kvm/tests/iozone_windows.py +2 -2
@@ -28,8 +28,8 @@ def run_iozone_windows(test, params, env): c = params.get("iozone_cmd") t = int(params.get("iozone_timeout")) logging.info("Running IOzone command on guest, timeout %ss", t) - results = session.get_command_output(command=c, timeout=t, - print_func=logging.debug) + results = session.cmd_output(command=c, timeout=t, + print_func=logging.debug) utils.open_write_close(results_path, results) # Postprocess the results using the IOzone postprocessing module
client/tests/kvm/tests/ksm_overcommit.py +2 -2
@@ -230,7 +230,7 @@ def run_ksm_overcommit(test, params, env): (mem / 200 * 50 * perf_ratio)) logging.debug(kvm_test_utils.get_memory_info([lvms[last_vm]])) - lsessions[i].get_command_output("die()", 20) + lsessions[i].cmd_output("die()", 20) lvms[last_vm].destroy(gracefully = False) logging.info("Phase 3b: PASS")
@@ -356,7 +356,7 @@ def run_ksm_overcommit(test, params, env): logging.debug("Cleaning up...") for i in range(0, max_alloc): - lsessions[i].get_command_output("die()", 20) + lsessions[i].cmd_output("die()", 20) session.close() vm.destroy(gracefully = False)
client/tests/kvm/tests/linux_s3.py +2 -2
@@ -16,13 +16,13 @@ def run_linux_s3(test, params, env): session = kvm_test_utils.wait_for_login(vm, timeout=timeout) logging.info("Checking that VM supports S3") - if session.get_command_status("grep -q mem /sys/power/state") != 0: + if session.cmd_status("grep -q mem /sys/power/state") != 0: raise error.TestFail("Guest does not support S3") logging.info("Waiting for a while for X to start") time.sleep(10) - src_tty = session.get_command_output("fgconsole").strip() + src_tty = session.cmd_output("fgconsole").strip() logging.info("Current virtual terminal is %s" % src_tty) if src_tty not in map(str, range(1,10)): raise error.TestFail("Got a strange current vt (%s)" % src_tty)
client/tests/kvm/tests/migration.py +3 -4
@@ -29,7 +29,7 @@ def run_migration(test, params, env): # Get the output of migration_test_command test_command = params.get("migration_test_command") - reference_output = session.get_command_output(test_command) + reference_output = session.cmd_output(test_command) # Start some process in the background (and leave the session open) background_command = params.get("migration_bg_command", "")
@@ -60,7 +60,7 @@ def run_migration(test, params, env): session2.cmd(check_command, timeout=30) # Get the output of migration_test_command - output = session2.get_command_output(test_command) + output = session2.cmd_output(test_command) # Compare output to reference output if output != reference_output:
@@ -77,8 +77,7 @@ def run_migration(test, params, env): finally: # Kill the background process if session2 and session2.is_alive(): - session2.get_command_output(params.get("migration_bg_kill_command", - "")) + session2.cmd_output(params.get("migration_bg_kill_command", "")) session2.close() session.close()
client/tests/kvm/tests/multicast.py +4 -4
@@ -56,8 +56,8 @@ def run_multicast(test, params, env): mcast_path = os.path.join(test.bindir, "scripts/join_mcast.py") if not vm.copy_files_to(mcast_path, "/tmp"): raise error.TestError("Fail to copy %s to guest" % mcast_path) - output = session.get_command_output("python /tmp/join_mcast.py %d %s %d" % - (mgroup_count, prefix, suffix)) + output = session.cmd_output("python /tmp/join_mcast.py %d %s %d" % + (mgroup_count, prefix, suffix)) # if success to join multicast, the process will be paused, and return PID. try:
@@ -86,6 +86,6 @@ def run_multicast(test, params, env): (s, o)) finally: - logging.debug(session.get_command_output("ipmaddr show")) - session.get_command_output("kill -s SIGCONT %s" % pid) + logging.debug(session.cmd_output("ipmaddr show")) + session.cmd_output("kill -s SIGCONT %s" % pid) session.close()
client/tests/kvm/tests/netperf.py +2 -2
@@ -26,7 +26,7 @@ def run_netperf(test, params, env): result_file = os.path.join(test.resultsdir, "output_%s" % test.iteration) firewall_flush = "iptables -F" - session.get_command_output(firewall_flush) + session.cmd_output(firewall_flush) for i in params.get("netperf_files").split(): if not vm.copy_files_to(os.path.join(netperf_dir, i), "/tmp"):
@@ -65,5 +65,5 @@ def run_netperf(test, params, env): ", ".join(list_fail)) finally: - session.get_command_output("killall netserver") + session.cmd_output("killall netserver") session.close()
client/tests/kvm/tests/nic_promisc.py +2 -2
@@ -82,12 +82,12 @@ def run_nic_promisc(test, params, env): logging.info("Clean temporary files") cmd = "rm -f %s" % filename utils.run(cmd) - session.get_command_output(cmd) + session.cmd_output(cmd) finally: logging.info("Restore the %s to the nonpromisc mode", ethname) session2.close() - session.get_command_output("ip link set %s promisc off" % ethname) + session.cmd_output("ip link set %s promisc off" % ethname) session.close() if success_counter != 2 * len(file_size):
client/tests/kvm/tests/nicdriver_unload.py +2 -2
@@ -74,7 +74,7 @@ def run_nicdriver_unload(test, params, env): try: session2.cmd(unload_load_cmd, timeout=120) except: - session.get_command_output("rm -rf /tmp/Thread-*") + session.cmd_output("rm -rf /tmp/Thread-*") raise pid, s = os.waitpid(pid, os.WNOHANG) status = os.WEXITSTATUS(s)
@@ -107,5 +107,5 @@ def run_nicdriver_unload(test, params, env): raise error.TestFail("Test nic function after load/unload fail") finally: - session.get_command_output("rm -rf /tmp/Thread-*") + session.cmd_output("rm -rf /tmp/Thread-*") session.close()
client/tests/kvm/tests/pci_hotplug.py +3 -3
@@ -32,7 +32,7 @@ def run_pci_hotplug(test, params, env): info_pci_ref = vm.monitor.info("pci") # Get output of command as reference - reference = session.get_command_output(params.get("reference_cmd")) + reference = session.cmd_output(params.get("reference_cmd")) tested_model = params.get("pci_model") test_type = params.get("pci_type")
@@ -130,7 +130,7 @@ def run_pci_hotplug(test, params, env): # Define a helper function to compare the output def new_shown(): - o = session.get_command_output(params.get("reference_cmd")) + o = session.cmd_output(params.get("reference_cmd")) return o != reference secs = int(params.get("wait_secs_for_hook_up"))
@@ -141,7 +141,7 @@ def run_pci_hotplug(test, params, env): # Define a helper function to catch PCI device string def find_pci(): - o = session.get_command_output(params.get("find_pci_cmd")) + o = session.cmd_output(params.get("find_pci_cmd")) return params.get("match_string") in o if not kvm_utils.wait_for(find_pci, 30, 3, 3):
client/tests/kvm/tests/physical_resources_check.py +1 -1
@@ -135,7 +135,7 @@ def run_physical_resources_check(test, params, env): def verify_device(expect, name, verify_cmd): f_fail = 0 if verify_cmd: - actual = session.get_command_output(verify_cmd) + actual = session.cmd_output(verify_cmd) if not string.upper(expect) in actual: f_fail += 1 logging.error("%s mismatch:")
client/tests/kvm/tests/timedrift.py +1 -1
@@ -146,7 +146,7 @@ def run_timedrift(test, params, env): restore_cpu_affinity(prev_affinity) # Stop the guest load if guest_load_stop_command: - session.get_command_output(guest_load_stop_command) + session.cmd_output(guest_load_stop_command) # Close all load shell sessions for load_session in guest_load_sessions: load_session.close()
client/tests/kvm/tests/vlan.py +5 -6
@@ -48,8 +48,7 @@ def run_vlan(test, params, env): def rem_vlan(session, id, iface="eth0"): rem_vlan_cmd = "if [[ -e /proc/net/vlan/%s ]];then vconfig rem %s;fi" iface = "%s.%s" % (iface, id) - s = session.get_command_status(rem_vlan_cmd % (iface, iface)) - return s + return session.cmd_status(rem_vlan_cmd % (iface, iface)) def nc_transfer(src, dst): nc_port = kvm_utils.find_free_port(1025, 5334, vm_ip[dst])
@@ -62,7 +61,7 @@ def run_vlan(test, params, env): time.sleep(2) #send file from src to dst send_cmd = send_cmd % (vlan_ip[dst], str(nc_port), "file") - if session[src].get_command_status(send_cmd, timeout=60) != 0: + if session[src].cmd_status(send_cmd, timeout=60) != 0: raise error.TestFail ("Fail to send file" " from vm%s to vm%s" % (src+1, dst+1)) try:
@@ -71,7 +70,7 @@ def run_vlan(test, params, env): raise error.TestFail ("Fail to receive file" " from vm%s to vm%s" % (src+1, dst+1)) #check MD5 message digest of receive file in dst - output = session[dst].get_command_output("md5sum receive").strip() + output = session[dst].cmd_output("md5sum receive").strip() digest_receive = re.findall(r'(\w+)', output)[0] if digest_receive == digest_origin[src]: logging.info("file succeed received in vm %s" % vlan_ip[dst])
@@ -79,7 +78,7 @@ def run_vlan(test, params, env): logging.info("digest_origin is %s" % digest_origin[src]) logging.info("digest_receive is %s" % digest_receive) raise error.TestFail("File transfered differ from origin") - session[dst].get_command_output("rm -f receive") + session[dst].cmd_output("rm -f receive") for i in range(2): session.append(kvm_test_utils.wait_for_login(vm[i],
@@ -101,7 +100,7 @@ def run_vlan(test, params, env): digest_origin.append(re.findall(r'(\w+)', output)[0]) #stop firewall in vm - session[i].get_command_output("/etc/init.d/iptables stop") + session[i].cmd_output("/etc/init.d/iptables stop") #load 8021q module for vconfig session[i].cmd("modprobe 8021q")
client/tests/kvm/tests/whql_client_install.py +5 -5
@@ -54,14 +54,14 @@ def run_whql_client_install(test, params, env): # Get server and client information cmd = "echo %computername%" - server_name = server_session.get_command_output(cmd).strip() - client_name = session.get_command_output(cmd).strip() + server_name = server_session.cmd_output(cmd).strip() + client_name = session.cmd_output(cmd).strip() cmd = "wmic computersystem get domain" - server_workgroup = server_session.get_command_output(cmd).strip() + server_workgroup = server_session.cmd_output(cmd).strip() server_workgroup = server_workgroup.splitlines()[-1] regkey = r"HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" cmd = "reg query %s /v Domain" % regkey - o = server_session.get_command_output(cmd).strip().splitlines()[-1] + o = server_session.cmd_output(cmd).strip().splitlines()[-1] try: server_dns_suffix = o.split(None, 2)[2] except IndexError:
@@ -101,7 +101,7 @@ def run_whql_client_install(test, params, env): server_password) end_time = time.time() + 120 while time.time() < end_time: - if session.get_command_status(cmd) == 0: + if session.cmd_status(cmd) == 0: break time.sleep(5) else:
client/tests/kvm/tests/whql_submission.py +2 -2
@@ -54,8 +54,8 @@ def run_whql_submission(test, params, env): # Get the computer names of the server and client cmd = "echo %computername%" - server_name = server_session.get_command_output(cmd).strip() - client_name = session.get_command_output(cmd).strip() + server_name = server_session.cmd_output(cmd).strip() + client_name = session.cmd_output(cmd).strip() session.close() # Run the automation program on the server
Signature · 1 line
1.5.5.6
RFC PATCH 5/5 KVM test: kvm_subprocess: rename kvm_shell_session and friends
PEP 8 states that class names should use the CapWords convention.
kvm_spawn -> Spawnkvm_tail -> Tailkvm_expect -> Expectkvm_shell_session -> ShellSession
This is an RFC because I wonder if the proposed names are too general, even though they are usually prefixed by 'kvm_subprocess.'.
--- client/tests/kvm/kvm_preprocessing.py | 2 +- client/tests/kvm/kvm_scheduler.py | 2 +- client/tests/kvm/kvm_subprocess.py | 60 ++++++++++++++++---------------- client/tests/kvm/kvm_utils.py | 15 ++++---- client/tests/kvm/kvm_vm.py | 6 ++-- 5 files changed, 42 insertions(+), 43 deletions(-)
client/tests/kvm/kvm_preprocessing.py +1 -1
@@ -204,7 +204,7 @@ def preprocess(test, params, env): if "tcpdump" not in env and params.get("run_tcpdump", "yes") == "yes": cmd = "%s -npvi any 'dst port 68'" % kvm_utils.find_command("tcpdump") logging.debug("Starting tcpdump (%s)...", cmd) - env["tcpdump"] = kvm_subprocess.kvm_tail( + env["tcpdump"] = kvm_subprocess.Tail( command=cmd, output_func=_update_address_cache, output_params=(env["address_cache"],))
client/tests/kvm/kvm_scheduler.py +1 -1
@@ -78,7 +78,7 @@ class scheduler: for obj in env.values(): if isinstance(obj, kvm_vm.VM): obj.destroy() - elif isinstance(obj, kvm_subprocess.kvm_spawn): + elif isinstance(obj, kvm_subprocess.Spawn): obj.close() kvm_utils.dump_env(env, env_filename) w.write("cleanup_done\n")
client/tests/kvm/kvm_subprocess.py +30 -30
150 lines not on this page · raw message
client/tests/kvm/kvm_utils.py +7 -8
@@ -437,7 +437,7 @@ def _remote_login(session, username, password, prompt, timeout=10): @brief: Log into a remote host (guest) using SSH or Telnet. - @param session: A kvm_expect or kvm_shell_session instance to operate on + @param session: An Expect or ShellSession instance to operate on @param username: The username to send in reply to a login prompt @param password: The password to send in reply to a password prompt @param prompt: The shell prompt that indicates a successful login
@@ -509,7 +509,7 @@ def _remote_scp(session, password, transfer_timeout=600, login_timeout=10): @brief: Transfer files using SCP, given a command line. - @param session: A kvm_expect or kvm_shell_session instance to operate on + @param session: An Expect or ShellSession instance to operate on @param password: The password to send in reply to a password prompt. @param transfer_timeout: The time duration (in seconds) to wait for the transfer to complete.
@@ -570,7 +570,7 @@ def remote_login(client, host, port, username, password, prompt, linesep="\n", each step of the login procedure (i.e. the "Are you sure" prompt or the password prompt) - @return: kvm_shell_session object on success and None on failure. + @return: ShellSession object on success and None on failure. """ if client == "ssh": cmd = ("ssh -o UserKnownHostsFile=/dev/null "
@@ -585,8 +585,7 @@ def remote_login(client, host, port, username, password, prompt, linesep="\n", return logging.debug("Trying to login with command '%s'" % cmd) - session = kvm_subprocess.kvm_shell_session(cmd, linesep=linesep, - prompt=prompt) + session = kvm_subprocess.ShellSession(cmd, linesep=linesep, prompt=prompt) if _remote_login(session, username, password, prompt, timeout): if log_filename: session.set_output_func(log_line)
@@ -625,9 +624,9 @@ def remote_scp(command, password, log_filename=None, transfer_timeout=600, output_func = None output_params = () - session = kvm_subprocess.kvm_expect(command, - output_func=output_func, - output_params=output_params) + session = kvm_subprocess.Expect(command, + output_func=output_func, + output_params=output_params) try: return _remote_scp(session, password, transfer_timeout, login_timeout) finally:
client/tests/kvm/kvm_vm.py +3 -3
@@ -733,7 +733,7 @@ class VM: # Establish a session with the serial console -- requires a version # of netcat that supports -U - self.serial_console = kvm_subprocess.kvm_shell_session( + self.serial_console = kvm_subprocess.ShellSession( "nc -U %s" % self.get_serial_console_filename(), auto_close=False, output_func=kvm_utils.log_line,
@@ -1040,7 +1040,7 @@ class VM: @param nic_index: The index of the NIC to connect to. @param timeout: Time (seconds) before giving up logging into the guest. - @return: kvm_spawn object on success and None on failure. + @return: ShellSession object on success and None on failure. """ username = self.params.get("username", "") password = self.params.get("password", "")
@@ -1139,7 +1139,7 @@ class VM: password prompt or a shell prompt) -- fail. @param timeout: Time (seconds) before giving up logging into the guest. - @return: kvm_spawn object on success and None on failure. + @return: ShellSession object on success and None on failure. """ username = self.params.get("username", "") password = self.params.get("password", "")
Signature · 1 line
1.5.5.6
Re: [KVM-AUTOTEST PATCH 5/5] [RFC] KVM test: kvm_subprocess: rename kvm_shell_session and friends
Quoted 9 lines · Michael Goldish
PEP 8 states that class names should use the CapWords convention.
kvm_spawn-> Spawnkvm_tail-> Tailkvm_expect-> Expectkvm_shell_session-> ShellSessionThis is an RFC because I wonder if the proposed names are too general, even though they are usually prefixed by '
kvm_subprocess.'.
No, the names are good. The namespace takes care of disambiguation.
Quoted 287 lines
Signed-off-by Michael Goldish <[email protected]>--- client/tests/kvm/kvm_preprocessing.py | 2 +- client/tests/kvm/kvm_scheduler.py | 2 +- client/tests/kvm/kvm_subprocess.py | 60 ++++++++++++++++---------------- client/tests/kvm/kvm_utils.py | 15 ++++---- client/tests/kvm/kvm_vm.py | 6 ++-- 5 files changed, 42 insertions(+), 43 deletions(-)client/tests/kvm/kvm_preprocessing.py +1
-1@@ -204,7 +204,7 @@ def preprocess(test, params, env): if "tcpdump" not in env and params.get("run_tcpdump", "yes") == "yes": cmd = "%s -npvi any 'dst port 68'" % kvm_utils.find_command("tcpdump") logging.debug("Starting tcpdump (%s)...", cmd) - env["tcpdump"] = kvm_subprocess.kvm_tail( + env["tcpdump"] = kvm_subprocess.Tail( command=cmd, output_func=_update_address_cache, output_params=(env["address_cache"],))client/tests/kvm/kvm_scheduler.py +1
-1@@ -78,7 +78,7 @@ class scheduler: for obj in env.values(): if isinstance(obj, kvm_vm.VM): obj.destroy() - elif isinstance(obj, kvm_subprocess.kvm_spawn): + elif isinstance(obj, kvm_subprocess.Spawn): obj.close() kvm_utils.dump_env(env, env_filename) w.write("cleanup_done\n")client/tests/kvm/kvm_subprocess.py +30
-30@@ -292,12 +292,12 @@ def run_bg(command, termination_func=None, output_func=None, output_prefix="", @param timeout: Time duration (in seconds) to wait for the subprocess to terminate before returning - @return: A kvm_tail object. + @return: A Tail object. """ - process = kvm_tail(command=command, - termination_func=termination_func, - output_func=output_func, - output_prefix=output_prefix) + process = Tail(command=command, + termination_func=termination_func, + output_func=output_func, + output_prefix=output_prefix) end_time = time.time() + timeout while time.time() < end_time and process.is_alive():@@ -338,7 +338,7 @@ def run_fg(command, output_func=None, output_prefix="", timeout=1.0): return (status, output) -class kvm_spawn: +class Spawn: """ This class is used for spawning and controlling a child process.@@ -350,7 +350,7 @@ class kvm_spawn: The text file can be accessed at any time using get_output(). In addition, the server opens as many pipes as requested by the client and writes the output to them. - The pipes are requested and accessed by classes derived from kvm_spawn. + The pipes are requested and accessed by classes derived from Spawn. These pipes are referred to as "readers". The server also receives input from the client and sends it to the child process.@@ -634,7 +634,7 @@ _thread_kill_requested = False def kill_tail_threads(): """ - Kill all kvm_tail threads. + Kill all Tail threads. After calling this function no new threads should be started. """@@ -646,12 +646,12 @@ def kill_tail_threads(): _thread_kill_requested = False -class kvm_tail(kvm_spawn): +class Tail(Spawn): """ This class runs a child process in the background and sends its output in real time, line-by-line, to a callback function. - See kvm_spawn's docstring. + See Spawn's docstring. This class uses a single pipe reader to read data in real time from the child process and report it to a given callback function.@@ -692,10 +692,10 @@ class kvm_tail(kvm_spawn): """ # Add a reader and a close hook self._add_reader("tail") - self._add_close_hook(kvm_tail._join_thread) + self._add_close_hook(Tail._join_thread) # Init the superclass - kvm_spawn.__init__(self, command, id, auto_close, echo, linesep) + Spawn.__init__(self, command, id, auto_close, echo, linesep) # Remember some attributes self.termination_func = termination_func@@ -711,11 +711,11 @@ class kvm_tail(kvm_spawn): def __getinitargs__(self): - return kvm_spawn.__getinitargs__(self) + (self.termination_func, - self.termination_params, - self.output_func, - self.output_params, - self.output_prefix) + return Spawn.__getinitargs__(self) + (self.termination_func, + self.termination_params, + self.output_func, + self.output_params, + self.output_prefix) def set_termination_func(self, termination_func):@@ -847,12 +847,12 @@ class kvm_tail(kvm_spawn): t.join() -class kvm_expect(kvm_tail): +class Expect(Tail): """ This class runs a child process in the background and provides expect-like services. - It also provides all of kvm_tail's functionality. + It also provides all of Tail's functionality. """ def __init__(self, command=None, id=None, auto_close=True, echo=False,@@ -888,13 +888,13 @@ class kvm_expect(kvm_tail): self._add_reader("expect") # Init the superclass - kvm_tail.__init__(self, command, id, auto_close, echo, linesep, - termination_func, termination_params, - output_func, output_params, output_prefix) + Tail.__init__(self, command, id, auto_close, echo, linesep, + termination_func, termination_params, + output_func, output_params, output_prefix) def __getinitargs__(self): - return kvm_tail.__getinitargs__(self) + return Tail.__getinitargs__(self) def read_nonblocking(self, timeout=None):@@ -1061,12 +1061,12 @@ class kvm_expect(kvm_tail): print_func) -class kvm_shell_session(kvm_expect): +class ShellSession(Expect): """ This class runs a child process in the background. It it suited for processes that provide an interactive shell, such as SSH and Telnet. - It provides all services of kvm_expect and kvm_tail. In addition, it + It provides all services of Expect and Tail. In addition, it provides command running services, and a utility function to test the process for responsiveness. """@@ -1106,9 +1106,9 @@ class kvm_shell_session(kvm_expect): cmd_status_output() and friends). """ # Init the superclass - kvm_expect.__init__(self, command, id, auto_close, echo, linesep, - termination_func, termination_params, - output_func, output_params, output_prefix) + Expect.__init__(self, command, id, auto_close, echo, linesep, + termination_func, termination_params, + output_func, output_params, output_prefix) # Remember some attributes self.prompt = prompt@@ -1116,8 +1116,8 @@ class kvm_shell_session(kvm_expect): def __getinitargs__(self): - return kvm_expect.__getinitargs__(self) + (self.prompt, - self.status_test_command) + return Expect.__getinitargs__(self) + (self.prompt, + self.status_test_command) def set_prompt(self, prompt):client/tests/kvm/kvm_utils.py +7
-8@@ -437,7 +437,7 @@ def _remote_login(session, username, password, prompt, timeout=10): @brief: Log into a remote host (guest) using SSH or Telnet. - @param session: A kvm_expect or kvm_shell_session instance to operate on + @param session: An Expect or ShellSession instance to operate on @param username: The username to send in reply to a login prompt @param password: The password to send in reply to a password prompt @param prompt: The shell prompt that indicates a successful login@@ -509,7 +509,7 @@ def _remote_scp(session, password, transfer_timeout=600, login_timeout=10): @brief: Transfer files using SCP, given a command line. - @param session: A kvm_expect or kvm_shell_session instance to operate on + @param session: An Expect or ShellSession instance to operate on @param password: The password to send in reply to a password prompt. @param transfer_timeout: The time duration (in seconds) to wait for the transfer to complete.@@ -570,7 +570,7 @@ def remote_login(client, host, port, username, password, prompt, linesep="\n", each step of the login procedure (i.e. the "Are you sure" prompt or the password prompt) - @return: kvm_shell_session object on success and None on failure. + @return: ShellSession object on success and None on failure. """ if client == "ssh": cmd = ("ssh -o UserKnownHostsFile=/dev/null "@@ -585,8 +585,7 @@ def remote_login(client, host, port, username, password, prompt, linesep="\n", return logging.debug("Trying to login with command '%s'" % cmd) - session = kvm_subprocess.kvm_shell_session(cmd, linesep=linesep, - prompt=prompt) + session = kvm_subprocess.ShellSession(cmd, linesep=linesep, prompt=prompt) if _remote_login(session, username, password, prompt, timeout): if log_filename: session.set_output_func(log_line)@@ -625,9 +624,9 @@ def remote_scp(command, password, log_filename=None, transfer_timeout=600, output_func = None output_params = () - session = kvm_subprocess.kvm_expect(command, - output_func=output_func, - output_params=output_params) + session = kvm_subprocess.Expect(command, + output_func=output_func, + output_params=output_params) try: return _remote_scp(session, password, transfer_timeout, login_timeout) finally:client/tests/kvm/kvm_vm.py +3
-3@@ -733,7 +733,7 @@ class VM: # Establish a session with the serial console -- requires a version # of netcat that supports -U - self.serial_console = kvm_subprocess.kvm_shell_session( + self.serial_console = kvm_subprocess.ShellSession( "nc -U %s" % self.get_serial_console_filename(), auto_close=False, output_func=kvm_utils.log_line,@@ -1040,7 +1040,7 @@ class VM: @param nic_index: The index of the NIC to connect to. @param timeout: Time (seconds) before giving up logging into the guest. - @return: kvm_spawn object on success and None on failure. + @return: ShellSession object on success and None on failure. """ username = self.params.get("username", "") password = self.params.get("password", "")@@ -1139,7 +1139,7 @@ class VM: password prompt or a shell prompt) -- fail. @param timeout: Time (seconds) before giving up logging into the guest. - @return: kvm_spawn object on success and None on failure. + @return: ShellSession object on success and None on failure. """ username = self.params.get("username", "") password = self.params.get("password", "")