From patchwork Wed Sep 9 18:12:08 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 46441 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n89IFxk7019233 for ; Wed, 9 Sep 2009 18:15:59 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753995AbZIISPs (ORCPT ); Wed, 9 Sep 2009 14:15:48 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753988AbZIISPq (ORCPT ); Wed, 9 Sep 2009 14:15:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:10207 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753978AbZIISPn (ORCPT ); Wed, 9 Sep 2009 14:15:43 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFjb9030578; Wed, 9 Sep 2009 14:15:45 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFiZ4004979; Wed, 9 Sep 2009 14:15:45 -0400 Received: from localhost.localdomain (dhcp-1-188.tlv.redhat.com [10.35.1.188]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFNFa012008; Wed, 9 Sep 2009 14:15:43 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [PATCH 15/19] KVM test: Autotest test wrapper cleanup Date: Wed, 9 Sep 2009 21:12:08 +0300 Message-Id: <1252519932-30733-15-git-send-email-mgoldish@redhat.com> In-Reply-To: <1252519932-30733-14-git-send-email-mgoldish@redhat.com> References: <1252519932-30733-1-git-send-email-mgoldish@redhat.com> <1252519932-30733-2-git-send-email-mgoldish@redhat.com> <1252519932-30733-3-git-send-email-mgoldish@redhat.com> <1252519932-30733-4-git-send-email-mgoldish@redhat.com> <1252519932-30733-5-git-send-email-mgoldish@redhat.com> <1252519932-30733-6-git-send-email-mgoldish@redhat.com> <1252519932-30733-7-git-send-email-mgoldish@redhat.com> <1252519932-30733-8-git-send-email-mgoldish@redhat.com> <1252519932-30733-9-git-send-email-mgoldish@redhat.com> <1252519932-30733-10-git-send-email-mgoldish@redhat.com> <1252519932-30733-11-git-send-email-mgoldish@redhat.com> <1252519932-30733-12-git-send-email-mgoldish@redhat.com> <1252519932-30733-13-git-send-email-mgoldish@redhat.com> <1252519932-30733-14-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Make the code shorter and simpler. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_tests.py | 153 +++++++++++++++++++---------------------- 1 files changed, 72 insertions(+), 81 deletions(-) diff --git a/client/tests/kvm/kvm_tests.py b/client/tests/kvm/kvm_tests.py index 35666cf..8c785c1 100644 --- a/client/tests/kvm/kvm_tests.py +++ b/client/tests/kvm/kvm_tests.py @@ -205,10 +205,44 @@ def run_autotest(test, params, env): @param params: Dictionary with test parameters. @param env: Dictionary with the test environment. """ + # Helper functions + def copy_if_size_differs(vm, local_path, remote_path): + """ + Copy a file to a guest if it doesn't exist or if its size differs. + + @param vm: VM object + @param local_path: Local path + @param remote_path: Remote path + """ + copy = False + output = session.get_command_output("ls -l %s" % remote_path) + if ("such file" in output or + int(output.split()[4]) != os.path.getsize(local_path)): + basename = os.path.basename(local_path) + logging.info("Copying %s to guest (file is missing or has a " + "different size)..." % basename) + if not vm.copy_files_to(local_path, remote_path): + raise error.TestFail("Could not copy %s to guest" % basename) + + def extract(vm, remote_path, dest_dir="."): + """ + Extract a .tar.bz2 file on the guest. + + @param vm: VM object + @param remote_path: Remote file path + @param dest_dir: Destination dir for the contents + """ + basename = os.path.basename(remote_path) + logging.info("Extracting %s..." % basename) + status = session.get_command_status("tar xfj %s -C %s" % + (remote_path, dest_dir)) + if status != 0: + raise error.TestFail("Could not extract %s" % basename) + vm = kvm_test_utils.get_living_vm(env, params.get("main_vm")) session = kvm_test_utils.wait_for_login(vm) - # Collect some info + # Collect test parameters test_name = params.get("test_name") test_timeout = int(params.get("test_timeout", 300)) test_control_file = params.get("test_control_file", "control") @@ -235,104 +269,47 @@ def run_autotest(test, params, env): (os.path.join(test.bindir, "autotest", "tests"), tarred_test_path, test_name), timeout=30) - # Check if we need to copy autotest.tar.bz2 - copy = False - output = session.get_command_output("ls -l autotest.tar.bz2") - if "such file" in output: - copy = True - else: - size = int(output.split()[4]) - if size != os.path.getsize(tarred_autotest_path): - copy = True - # Perform the copy - if copy: - logging.info("Copying autotest.tar.bz2 to guest" - " (file is missing or has a different size)...") - if not vm.copy_files_to(tarred_autotest_path, ""): - raise error.TestFail("Could not copy autotest.tar.bz2 to guest") - - # Check if we need to copy .tar.bz2 - copy = False - output = session.get_command_output("ls -l %s.tar.bz2" % test_name) - if "such file" in output: - copy = True - else: - size = int(output.split()[4]) - if size != os.path.getsize(tarred_test_path): - copy = True - # Perform the copy - if copy: - logging.info("Copying %s.tar.bz2 to guest (file is missing or has a" - " different size)..." % test_name) - if not vm.copy_files_to(tarred_test_path, ""): - raise error.TestFail("Could not copy %s.tar.bz2 to guest" % - test_name) + # Copy autotest.tar.bz2 + copy_if_size_differs(vm, tarred_autotest_path, "autotest.tar.bz2") + + # Copy .tar.bz2 + copy_if_size_differs(vm, tarred_test_path, test_name + ".tar.bz2") # Extract autotest.tar.bz2 - logging.info("Extracting autotest.tar.bz2...") - status = session.get_command_status("tar xvfj autotest.tar.bz2") - if status != 0: - raise error.TestFail("Could not extract autotest.tar.bz2") + extract(vm, "autotest.tar.bz2") # mkdir autotest/tests session.sendline("mkdir autotest/tests") # Extract .tar.bz2 into autotest/tests - logging.info("Extracting %s.tar.bz2..." % test_name) - status = session.get_command_status("tar xvfj %s.tar.bz2 -C " - "autotest/tests" % test_name) - if status != 0: - raise error.TestFail("Could not extract %s.tar.bz2" % test_name) + extract(vm, test_name + ".tar.bz2", "autotest/tests") - # Cleaning up old remaining results - session.sendline("rm -rf autotest/results/*") - # Copying the selected control file (located inside - # test.bindir/autotest_control to the autotest dir + # Copy the selected control file (located inside + # test.bindir/autotest_control) to the autotest dir control_file_path = os.path.join(test.bindir, "autotest_control", test_control_file) if not vm.copy_files_to(control_file_path, "autotest/control"): raise error.TestFail("Could not copy the test control file to guest") + # Run the test logging.info("Running test '%s'..." % test_name) session.sendline("cd autotest") session.sendline("rm -f control.state") + session.sendline("rm -rf results/*") session.read_up_to_prompt() - session.sendline("bin/autotest control") logging.info("---------------- Test output ----------------") - match = session.read_up_to_prompt(timeout=test_timeout, - print_func=logging.info)[0] + status = session.get_command_status("bin/autotest control", + timeout=test_timeout, + print_func=logging.info) logging.info("---------------- End of test output ----------------") - if not match: + if status is None: raise error.TestFail("Timeout elapsed while waiting for test to " "complete") + # Get the results generated by autotest output = session.get_command_output("cat results/*/status") - - # Parse test results - result_list = scan_results.parse_results(output) - - # Report test results and check for FAIL/ERROR status - logging.info("Results (test, status, duration, info):") - status_error = False - status_fail = False - if result_list == []: - status_fail = True - message_fail = ("Test '%s' did not produce any recognizable " - "results" % test_name) - for result in result_list: - logging.info(str(result)) - if result[1] == "FAIL": - status_fail = True - message_fail = ("Test '%s' ended with FAIL " - "(info: '%s')" % (result[0], result[3])) - if result[1] == "ERROR": - status_error = True - message_error = ("Test '%s' ended with ERROR " - "(info: '%s')" % (result[0], result[3])) - if result[1] == "ABORT": - status_error = True - message_error = ("Test '%s' ended with ABORT " - "(info: '%s')" % (result[0], result[3])) + results = scan_results.parse_results(output) + session.close # Copy test results to the local bindir/guest_results logging.info("Copying results back from guest...") @@ -342,11 +319,25 @@ def run_autotest(test, params, env): if not vm.copy_files_from("autotest/results/default/*", guest_results_dir): logging.error("Could not copy results back from guest") + # Report test results + logging.info("Results (test, status, duration, info):") + for result in results: + logging.info(str(result)) + + # Make a list of FAIL/ERROR/ABORT results (make sure FAIL results appear + # before ERROR results, and ERROR results appear before ABORT results) + bad_results = [r for r in results if r[1] == "FAIL"] + bad_results += [r for r in results if r[1] == "ERROR"] + bad_results += [r for r in results if r[1] == "ABORT"] + # Fail the test if necessary - if status_fail: - raise error.TestFail(message_fail) - elif status_error: - raise error.TestError(message_error) + if not results: + raise Error.TestFail("Test '%s' did not produce any recognizable " + "results" % test_name) + if bad_results: + result = bad_results[0] + raise Error.TestFail("Test '%s' ended with %s (reason: '%s')" + % (result[0], result[1], result[3])) def internal_yum_update(session, command, prompt, timeout):