From patchwork Thu Jun 3 11:03:16 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 104074 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 o53B3ahj004881 for ; Thu, 3 Jun 2010 11:03:36 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752567Ab0FCLDe (ORCPT ); Thu, 3 Jun 2010 07:03:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:23940 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752524Ab0FCLDc (ORCPT ); Thu, 3 Jun 2010 07:03:32 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o53B3VaK021018 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 3 Jun 2010 07:03:31 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o53B3VhY028010; Thu, 3 Jun 2010 07:03:31 -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 o53B3QFd025307; Thu, 3 Jun 2010 07:03:30 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH 3/4] KVM test: add wrapper for RHEL-6 style unittests Date: Thu, 3 Jun 2010 14:03:16 +0300 Message-Id: <1275562997-26311-3-git-send-email-mgoldish@redhat.com> In-Reply-To: <1275562997-26311-2-git-send-email-mgoldish@redhat.com> References: <1275562997-26311-1-git-send-email-mgoldish@redhat.com> <1275562997-26311-2-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 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]); Thu, 03 Jun 2010 11:03:36 +0000 (UTC) diff --git a/client/tests/kvm/tests/unittest.py b/client/tests/kvm/tests/unittest.py new file mode 100644 index 0000000..c073d04 --- /dev/null +++ b/client/tests/kvm/tests/unittest.py @@ -0,0 +1,66 @@ +import logging, time, select, os, difflib +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) Compare output with reference output + + @param test: kvm test object + @param params: Dictionary with the test parameters + @param env: Dictionary with test environment. + """ + # Get params + ref_output_filename = params.get("reference_output") + if not ref_output_filename: + raise error.TestError("reference_output not specified") + ref_output_filename = kvm_utils.get_path(test.bindir, ref_output_filename) + if not os.path.exists(ref_output_filename): + raise error.TestError("Reference file not found: %s" % + ref_output_filename) + timeout = float(params.get("unittest_timeout", 60)) + + # Get VM + vm = kvm_test_utils.get_living_vm(env, params.get("main_vm")) + # Resume VM if stopped + vm.send_monitor_cmd("cont") + + 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: + dead = vm.is_dead() + line = f.readline() + if line: + logging.info(line.rstrip()) + elif dead: + break + else: + time.sleep(1) + else: + raise error.TestFail("Timeout elapsed (%ss)" % timeout) + 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, 0, 1, timeout): + raise error.TestFail("Timeout elapsed (%ss)" % timeout) + + # Compare output with reference_output + logging.info("Comparing test output with reference output") + test_output = open(vm.testlog_file_name, "U").readlines() + ref_output = open(ref_output_filename, "U").readlines() + if test_output != ref_output: + logging.error("Difference between reference output and test output:") + for line in difflib.unified_diff(ref_output, test_output): + logging.error(line.rstrip()) + raise error.TestFail("Test output differs from reference output")