diff mbox

[2/3] KVM Test: Add function run_autotest_background and wait_autotest_background.

Message ID 1270630156-9904-2-git-send-email-fyang@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Feng Yang April 7, 2010, 8:49 a.m. UTC
None
diff mbox

Patch

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
+