From: Michael Goldish Subject: [KVM-AUTOTEST PATCH v2 3/4] KVM test: add wrapper for RHEL-6 style unittests Date: Wed, 9 Jun 2010 00:50:06 +0300 Message-ID: <1276033807-22718-3-git-send-email-mgoldish@redhat.com> References: <1276033807-22718-1-git-send-email-mgoldish@redhat.com> <1276033807-22718-2-git-send-email-mgoldish@redhat.com> Cc: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:49476 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754528Ab0FHVuo (ORCPT ); Tue, 8 Jun 2010 17:50:44 -0400 In-Reply-To: <1276033807-22718-2-git-send-email-mgoldish@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Based on Naphtali Sprei's patches. Changes from v1: - Determine success/failure by exit status instead of output - Restructure loop so that vm.is_dead() is called less often - Copy test log to debugdir/unittest.log - Change parameters passed to wait_for() Signed-off-by: Michael Goldish --- client/tests/kvm/tests/unittest.py | 65 ++++++++++++++++++++++++++++++++++++ 1 files changed, 65 insertions(+), 0 deletions(-) create mode 100644 client/tests/kvm/tests/unittest.py diff --git a/client/tests/kvm/tests/unittest.py b/client/tests/kvm/tests/unittest.py new file mode 100644 index 0000000..4570096 --- /dev/null +++ b/client/tests/kvm/tests/unittest.py @@ -0,0 +1,65 @@ +import logging, time, os, shutil +from autotest_lib.client.common_lib import error +import kvm_subprocess, kvm_test_utils, kvm_utils + + +def run_unittest(test, params, env): + """ + KVM RHEL-6 style unit test: + 1) Resume a stopped VM + 2) Wait for VM to terminate + 3) Make sure qemu's exit status is 0 + + @param test: kvm test object + @param params: Dictionary with the test parameters + @param env: Dictionary with test environment + """ + # Get VM + vm = kvm_test_utils.get_living_vm(env, params.get("main_vm")) + # Resume VM if stopped + vm.send_monitor_cmd("cont") + + timeout = float(params.get("unittest_timeout", 60)) + + try: + if params.get("log_output") == "yes": + # Log test output + f = open(vm.testlog_file_name) + logging.info("-------- Test output --------") + try: + end_time = time.time() + timeout + while time.time() < end_time: + line = f.readline() + if line: + logging.info(line.rstrip()) + elif vm.is_dead(): + break + else: + time.sleep(1) + else: + raise error.TestFail("Timeout elapsed (%ss)" % timeout) + # Make sure everything has been read and logged + while True: + line = f.readline() + if line: + logging.info(line.rstrip()) + else: + break + finally: + logging.info("-------- End of test output --------") + f.close() + else: + # Just wait for the VM to terminate + logging.info("Waiting for VM to terminate...") + if not kvm_utils.wait_for(vm.is_dead, timeout): + raise error.TestFail("Timeout elapsed (%ss)" % timeout) + finally: + # Copy test log to debugdir/unittest.log + testlog_path = os.path.join(test.debugdir, "unittest.log") + shutil.copy(vm.testlog_file_name, testlog_path) + + # Check qemu's exit status + status = vm.process.get_status() + if status != 0: + raise error.TestFail("qemu exited with status %s (see unittest " + "output at %s)" % (status, testlog_path)) -- 1.5.4.1