From patchwork Wed Apr 7 08:49:15 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Yang X-Patchwork-Id: 90977 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o378nSjo032393 for ; Wed, 7 Apr 2010 08:49:28 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752133Ab0DGIt0 (ORCPT ); Wed, 7 Apr 2010 04:49:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:6721 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750790Ab0DGItX (ORCPT ); Wed, 7 Apr 2010 04:49:23 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o378nMtb026182 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 7 Apr 2010 04:49:22 -0400 Received: from localhost.localdomain ([10.66.91.72]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o378nGdw009801; Wed, 7 Apr 2010 04:49:20 -0400 From: Feng Yang To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, Feng Yang Subject: [PATCH 2/3] KVM Test: Add function run_autotest_background and wait_autotest_background. Date: Wed, 7 Apr 2010 16:49:15 +0800 Message-Id: <1270630156-9904-2-git-send-email-fyang@redhat.com> In-Reply-To: <1270630156-9904-1-git-send-email-fyang@redhat.com> References: <1270630156-9904-1-git-send-email-fyang@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Wed, 07 Apr 2010 08:49:29 +0000 (UTC) diff --git a/client/tests/kvm/kvm_test_utils.py b/client/tests/kvm/kvm_test_utils.py index f512044..2a1054e 100644 --- a/client/tests/kvm/kvm_test_utils.py +++ b/client/tests/kvm/kvm_test_utils.py @@ -21,7 +21,7 @@ More specifically: @copyright: 2008-2009 Red Hat Inc. """ -import time, os, logging, re, commands +import time, os, logging, re, commands, sys from autotest_lib.client.common_lib import error from autotest_lib.client.bin import utils import kvm_utils, kvm_vm, kvm_subprocess, scan_results @@ -402,3 +402,69 @@ def run_autotest(vm, session, control_path, timeout, test_name, outputdir): result = bad_results[0] raise error.TestFail("Test '%s' ended with %s (reason: '%s')" % (result[0], result[1], result[3])) + + +def run_autotest_background(vm, session, control_path, timeout, test_name, + outputdir): + """ + Wrapper of run_autotest() and make it run in the background through fork() + and let it run in the child process. + 1) Flush the stdio. + 2) Build test params which is recevied from arguments and used by + run_autotest() + 3) Fork the process and let the run_autotest() run in the child + 4) Catch the exception raise by run_autotest() and exit the child with + non-zero return code. + 5) If no exception catched, reutrn 0 + + @param vm: VM object. + @param session: A shell session on the VM provided. + @param control: An autotest control file. + @param timeout: Timeout under which the autotest test must complete. + @param test_name: Autotest client test name. + @param outputdir: Path on host where we should copy the guest autotest + results to. + """ + + def flush(): + sys.stdout.flush() + sys.stderr.flush() + + logging.info("Running autotest background ...") + flush() + pid = os.fork() + if pid: + # Parent process + return pid + + try: + # Launch autotest + logging.info("child process of run_autotest_background") + run_autotest(vm, session, control_path, timeout, test_name, outputdir) + except error.TestFail, message_fail: + logging.info("[Autotest Background FAIL] %s" % message_fail) + os._exit(1) + except error.TestError, message_error: + logging.info("[Autotest Background ERROR] %s" % message_error) + os._exit(2) + except: + os._exit(3) + + logging.info("[Auototest Background GOOD]") + os._exit(0) + + +def wait_autotest_background(pid): + """ + Wait for background autotest finish. + + @param pid: Pid of the child process executing background autotest + """ + logging.info("Waiting for background autotest to finish ...") + + (pid, s) = os.waitpid(pid,0) + status = os.WEXITSTATUS(s) + if status != 0: + return False + return True +