From: Lucas Meneghel Rodrigues Subject: Re: [Autotest] [PATCH 15/19] KVM test: Autotest test wrapper cleanup Date: Thu, 10 Sep 2009 00:16:09 -0300 Message-ID: <6ac58f4f0909092016g444d4c2cu5eb7827dbdcc801c@mail.gmail.com> References: <1252519932-30733-1-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> <1252519932-30733-15-git-send-email-mgoldish@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: autotest@test.kernel.org, kvm@vger.kernel.org To: Michael Goldish Return-path: Received: from mail-vw0-f195.google.com ([209.85.212.195]:35513 "EHLO mail-vw0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751917AbZIJDQI convert rfc822-to-8bit (ORCPT ); Wed, 9 Sep 2009 23:16:08 -0400 Received: by vws33 with SMTP id 33so2652346vws.33 for ; Wed, 09 Sep 2009 20:16:10 -0700 (PDT) In-Reply-To: <1252519932-30733-15-git-send-email-mgoldish@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On Wed, Sep 9, 2009 at 3:12 PM, Michael Goldish w= rote: > Make the code shorter and simpler. > > Signed-off-by: Michael Goldish > --- > =A0client/tests/kvm/kvm_tests.py | =A0153 +++++++++++++++++++--------= -------------- > =A01 files changed, 72 insertions(+), 81 deletions(-) > > diff --git a/client/tests/kvm/kvm_tests.py b/client/tests/kvm/kvm_tes= ts.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): > =A0 =A0 @param params: Dictionary with test parameters. > =A0 =A0 @param env: Dictionary with the test environment. > =A0 =A0 """ > + =A0 =A0# Helper functions > + =A0 =A0def copy_if_size_differs(vm, local_path, remote_path): > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0Copy a file to a guest if it doesn't exist or if its= size differs. > + > + =A0 =A0 =A0 =A0@param vm: VM object > + =A0 =A0 =A0 =A0@param local_path: Local path > + =A0 =A0 =A0 =A0@param remote_path: Remote path > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0copy =3D False > + =A0 =A0 =A0 =A0output =3D session.get_command_output("ls -l %s" % r= emote_path) > + =A0 =A0 =A0 =A0if ("such file" in output or > + =A0 =A0 =A0 =A0 =A0 =A0int(output.split()[4]) !=3D os.path.getsize(= local_path)): > + =A0 =A0 =A0 =A0 =A0 =A0basename =3D os.path.basename(local_path) > + =A0 =A0 =A0 =A0 =A0 =A0logging.info("Copying %s to guest (file is m= issing or has a " > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "different size)...= " % basename) > + =A0 =A0 =A0 =A0 =A0 =A0if not vm.copy_files_to(local_path, remote_p= ath): > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0raise error.TestFail("Could not copy= %s to guest" % basename) > + > + =A0 =A0def extract(vm, remote_path, dest_dir=3D"."): > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0Extract a .tar.bz2 file on the guest. > + > + =A0 =A0 =A0 =A0@param vm: VM object > + =A0 =A0 =A0 =A0@param remote_path: Remote file path > + =A0 =A0 =A0 =A0@param dest_dir: Destination dir for the contents > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0basename =3D os.path.basename(remote_path) > + =A0 =A0 =A0 =A0logging.info("Extracting %s..." % basename) > + =A0 =A0 =A0 =A0status =3D session.get_command_status("tar xfj %s -C= %s" % > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0(remote_path, dest_dir)) > + =A0 =A0 =A0 =A0if status !=3D 0: > + =A0 =A0 =A0 =A0 =A0 =A0raise error.TestFail("Could not extract %s" = % basename) > + > =A0 =A0 vm =3D kvm_test_utils.get_living_vm(env, params.get("main_vm"= )) > =A0 =A0 session =3D kvm_test_utils.wait_for_login(vm) > > - =A0 =A0# Collect some info > + =A0 =A0# Collect test parameters > =A0 =A0 test_name =3D params.get("test_name") > =A0 =A0 test_timeout =3D int(params.get("test_timeout", 300)) > =A0 =A0 test_control_file =3D params.get("test_control_file", "contro= l") > @@ -235,104 +269,47 @@ def run_autotest(test, params, env): > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (os.path.join(tes= t.bindir, "autotest", "tests"), > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0tarred_test_pa= th, test_name), timeout=3D30) > > - =A0 =A0# Check if we need to copy autotest.tar.bz2 > - =A0 =A0copy =3D False > - =A0 =A0output =3D session.get_command_output("ls -l autotest.tar.bz= 2") > - =A0 =A0if "such file" in output: > - =A0 =A0 =A0 =A0copy =3D True > - =A0 =A0else: > - =A0 =A0 =A0 =A0size =3D int(output.split()[4]) > - =A0 =A0 =A0 =A0if size !=3D os.path.getsize(tarred_autotest_path): > - =A0 =A0 =A0 =A0 =A0 =A0copy =3D True > - =A0 =A0# Perform the copy > - =A0 =A0if copy: > - =A0 =A0 =A0 =A0logging.info("Copying autotest.tar.bz2 to guest" > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 " (file is missing or has a= different size)...") > - =A0 =A0 =A0 =A0if not vm.copy_files_to(tarred_autotest_path, ""): > - =A0 =A0 =A0 =A0 =A0 =A0raise error.TestFail("Could not copy autotes= t.tar.bz2 to guest") > - > - =A0 =A0# Check if we need to copy .tar.bz2 > - =A0 =A0copy =3D False > - =A0 =A0output =3D session.get_command_output("ls -l %s.tar.bz2" % t= est_name) > - =A0 =A0if "such file" in output: > - =A0 =A0 =A0 =A0copy =3D True > - =A0 =A0else: > - =A0 =A0 =A0 =A0size =3D int(output.split()[4]) > - =A0 =A0 =A0 =A0if size !=3D os.path.getsize(tarred_test_path): > - =A0 =A0 =A0 =A0 =A0 =A0copy =3D True > - =A0 =A0# Perform the copy > - =A0 =A0if copy: > - =A0 =A0 =A0 =A0logging.info("Copying %s.tar.bz2 to guest (file is m= issing or has a" > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 " different size)..." % tes= t_name) > - =A0 =A0 =A0 =A0if not vm.copy_files_to(tarred_test_path, ""): > - =A0 =A0 =A0 =A0 =A0 =A0raise error.TestFail("Could not copy %s.tar.= bz2 to guest" % > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 tes= t_name) > + =A0 =A0# Copy autotest.tar.bz2 > + =A0 =A0copy_if_size_differs(vm, tarred_autotest_path, "autotest.tar= =2Ebz2") > + > + =A0 =A0# Copy .tar.bz2 > + =A0 =A0copy_if_size_differs(vm, tarred_test_path, test_name + ".tar= =2Ebz2") > > =A0 =A0 # Extract autotest.tar.bz2 > - =A0 =A0logging.info("Extracting autotest.tar.bz2...") > - =A0 =A0status =3D session.get_command_status("tar xvfj autotest.tar= =2Ebz2") > - =A0 =A0if status !=3D 0: > - =A0 =A0 =A0 =A0raise error.TestFail("Could not extract autotest.tar= =2Ebz2") > + =A0 =A0extract(vm, "autotest.tar.bz2") > > =A0 =A0 # mkdir autotest/tests > =A0 =A0 session.sendline("mkdir autotest/tests") > > =A0 =A0 # Extract .tar.bz2 into autotest/tests > - =A0 =A0logging.info("Extracting %s.tar.bz2..." % test_name) > - =A0 =A0status =3D session.get_command_status("tar xvfj %s.tar.bz2 -= C " > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0"autotest/tests" % test_name) > - =A0 =A0if status !=3D 0: > - =A0 =A0 =A0 =A0raise error.TestFail("Could not extract %s.tar.bz2" = % test_name) > + =A0 =A0extract(vm, test_name + ".tar.bz2", "autotest/tests") > > - =A0 =A0# Cleaning up old remaining results > - =A0 =A0session.sendline("rm -rf autotest/results/*") > - =A0 =A0# Copying the selected control file (located inside > - =A0 =A0# test.bindir/autotest_control to the autotest dir > + =A0 =A0# Copy the selected control file (located inside > + =A0 =A0# test.bindir/autotest_control) to the autotest dir > =A0 =A0 control_file_path =3D os.path.join(test.bindir, "autotest_con= trol", > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0test_control_file) > =A0 =A0 if not vm.copy_files_to(control_file_path, "autotest/control"= ): > =A0 =A0 =A0 =A0 raise error.TestFail("Could not copy the test control= file to guest") > + > =A0 =A0 # Run the test > =A0 =A0 logging.info("Running test '%s'..." % test_name) > =A0 =A0 session.sendline("cd autotest") > =A0 =A0 session.sendline("rm -f control.state") > + =A0 =A0session.sendline("rm -rf results/*") > =A0 =A0 session.read_up_to_prompt() > - =A0 =A0session.sendline("bin/autotest control") > =A0 =A0 logging.info("---------------- Test output ----------------") > - =A0 =A0match =3D session.read_up_to_prompt(timeout=3Dtest_timeout, > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0print_func=3Dlogging.info)[0] > + =A0 =A0status =3D session.get_command_status("bin/autotest control"= , > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0timeout=3Dtest_timeout, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0print_func=3Dlogging.info) > =A0 =A0 logging.info("---------------- End of test output -----------= -----") > - =A0 =A0if not match: > + =A0 =A0if status is None: > =A0 =A0 =A0 =A0 raise error.TestFail("Timeout elapsed while waiting f= or test to " > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"complete"= ) > + > =A0 =A0 # Get the results generated by autotest > =A0 =A0 output =3D session.get_command_output("cat results/*/status") > - > - =A0 =A0# Parse test results > - =A0 =A0result_list =3D scan_results.parse_results(output) > - > - =A0 =A0# Report test results and check for FAIL/ERROR status > - =A0 =A0logging.info("Results (test, status, duration, info):") > - =A0 =A0status_error =3D False > - =A0 =A0status_fail =3D False > - =A0 =A0if result_list =3D=3D []: > - =A0 =A0 =A0 =A0status_fail =3D True > - =A0 =A0 =A0 =A0message_fail =3D ("Test '%s' did not produce any rec= ognizable " > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"results" % test_nam= e) > - =A0 =A0for result in result_list: > - =A0 =A0 =A0 =A0logging.info(str(result)) > - =A0 =A0 =A0 =A0if result[1] =3D=3D "FAIL": > - =A0 =A0 =A0 =A0 =A0 =A0status_fail =3D True > - =A0 =A0 =A0 =A0 =A0 =A0message_fail =3D ("Test '%s' ended with FAIL= " > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"(info: '%s'= )" % (result[0], result[3])) > - =A0 =A0 =A0 =A0if result[1] =3D=3D "ERROR": > - =A0 =A0 =A0 =A0 =A0 =A0status_error =3D True > - =A0 =A0 =A0 =A0 =A0 =A0message_error =3D ("Test '%s' ended with ERR= OR " > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "(info: '%s= ')" % (result[0], result[3])) > - =A0 =A0 =A0 =A0if result[1] =3D=3D "ABORT": > - =A0 =A0 =A0 =A0 =A0 =A0status_error =3D True > - =A0 =A0 =A0 =A0 =A0 =A0message_error =3D ("Test '%s' ended with ABO= RT " > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "(info: '%s= ')" % (result[0], result[3])) > + =A0 =A0results =3D scan_results.parse_results(output) > + =A0 =A0session.close > > =A0 =A0 # Copy test results to the local bindir/guest_results > =A0 =A0 logging.info("Copying results back from guest...") > @@ -342,11 +319,25 @@ def run_autotest(test, params, env): > =A0 =A0 if not vm.copy_files_from("autotest/results/default/*", guest= _results_dir): > =A0 =A0 =A0 =A0 logging.error("Could not copy results back from guest= ") > > + =A0 =A0# Report test results > + =A0 =A0logging.info("Results (test, status, duration, info):") > + =A0 =A0for result in results: > + =A0 =A0 =A0 =A0logging.info(str(result)) > + > + =A0 =A0# Make a list of FAIL/ERROR/ABORT results (make sure FAIL re= sults appear > + =A0 =A0# before ERROR results, and ERROR results appear before ABOR= T results) > + =A0 =A0bad_results =3D [r for r in results if r[1] =3D=3D "FAIL"] > + =A0 =A0bad_results +=3D [r for r in results if r[1] =3D=3D "ERROR"] > + =A0 =A0bad_results +=3D [r for r in results if r[1] =3D=3D "ABORT"] > + > =A0 =A0 # Fail the test if necessary > - =A0 =A0if status_fail: > - =A0 =A0 =A0 =A0raise error.TestFail(message_fail) > - =A0 =A0elif status_error: > - =A0 =A0 =A0 =A0raise error.TestError(message_error) Caught 2 typos below, should be error.TestFail, not Error.TestFail. Nevertheless, excellent cleanup. Applied with those minimal changes. > + =A0 =A0if not results: > + =A0 =A0 =A0 =A0raise Error.TestFail("Test '%s' did not produce any = recognizable " > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "results" %= test_name) > + =A0 =A0if bad_results: > + =A0 =A0 =A0 =A0result =3D bad_results[0] > + =A0 =A0 =A0 =A0raise Error.TestFail("Test '%s' ended with %s (reaso= n: '%s')" > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 % (result[0= ], result[1], result[3])) > > > =A0def internal_yum_update(session, command, prompt, timeout): > -- > 1.5.4.1 > > _______________________________________________ > Autotest mailing list > Autotest@test.kernel.org > http://test.kernel.org/cgi-bin/mailman/listinfo/autotest > --=20 Lucas