From patchwork Wed Sep 9 18:12:05 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 46433 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n89IFjxR019166 for ; Wed, 9 Sep 2009 18:15:45 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753973AbZIISPj (ORCPT ); Wed, 9 Sep 2009 14:15:39 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753965AbZIISPj (ORCPT ); Wed, 9 Sep 2009 14:15:39 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33171 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753969AbZIISPi (ORCPT ); Wed, 9 Sep 2009 14:15:38 -0400 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFfB8014956; Wed, 9 Sep 2009 14:15:41 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFeY3015388; Wed, 9 Sep 2009 14:15:41 -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 n89IFNFX012008; Wed, 9 Sep 2009 14:15:39 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [PATCH 12/19] KVM test: Add new module kvm_test_utils.py Date: Wed, 9 Sep 2009 21:12:05 +0300 Message-Id: <1252519932-30733-12-git-send-email-mgoldish@redhat.com> In-Reply-To: <1252519932-30733-11-git-send-email-mgoldish@redhat.com> References: <1252519932-30733-1-git-send-email-mgoldish@redhat.com> <1252519932-30733-2-git-send-email-mgoldish@redhat.com> <1252519932-30733-3-git-send-email-mgoldish@redhat.com> <1252519932-30733-4-git-send-email-mgoldish@redhat.com> <1252519932-30733-5-git-send-email-mgoldish@redhat.com> <1252519932-30733-6-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> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This module is meant to reduce code size by performing common test procedures. Generally, code here should look like test code. More specifically: - Functions in this module should raise exceptions if things go wrong (unlike functions in kvm_utils.py and kvm_vm.py which report failure via their returned values). - Functions in this module may use logging.info(), in addition to logging.debug() and logging.error(), to log messages the user may be interested in (unlike kvm_utils.py and kvm_vm.py which use logging.debug() for everything that isn't an error). - Functions in this module typically use functions and classes from lower-level modules (e.g. kvm_utils.py, kvm_vm.py, kvm_subprocess.py). - Functions in this module should not be used by lower-level modules. - Functions in this module should be used in the right context. For example, a function should not be used where it may display misleading or inaccurate info or debug messages. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_test_utils.py | 61 ++++++++++++++++++++++++++++++++++++ 1 files changed, 61 insertions(+), 0 deletions(-) create mode 100644 client/tests/kvm/kvm_test_utils.py diff --git a/client/tests/kvm/kvm_test_utils.py b/client/tests/kvm/kvm_test_utils.py new file mode 100644 index 0000000..39e92b9 --- /dev/null +++ b/client/tests/kvm/kvm_test_utils.py @@ -0,0 +1,61 @@ +import time, os, logging, re, commands +from autotest_lib.client.common_lib import utils, error +import kvm_utils, kvm_vm, kvm_subprocess + +""" +High-level KVM test utility functions. + +This module is meant to reduce code size by performing common test procedures. +Generally, code here should look like test code. +More specifically: + - Functions in this module should raise exceptions if things go wrong + (unlike functions in kvm_utils.py and kvm_vm.py which report failure via + their returned values). + - Functions in this module may use logging.info(), in addition to + logging.debug() and logging.error(), to log messages the user may be + interested in (unlike kvm_utils.py and kvm_vm.py which use + logging.debug() for anything that isn't an error). + - Functions in this module typically use functions and classes from + lower-level modules (e.g. kvm_utils.py, kvm_vm.py, kvm_subprocess.py). + - Functions in this module should not be used by lower-level modules. + - Functions in this module should be used in the right context. + For example, a function should not be used where it may display + misleading or inaccurate info or debug messages. + +@copyright: 2008-2009 Red Hat Inc. +""" + + +def get_living_vm(env, vm_name): + """ + Get a VM object from the environment and make sure it's alive. + + @param env: Dictionary with test environment. + @param vm_name: Name of the desired VM object. + @return: A VM object. + """ + vm = kvm_utils.env_get_vm(env, vm_name) + if not vm: + raise error.TestError("VM '%s' not found in environment" % vm_name) + if not vm.is_alive(): + raise error.TestError("VM '%s' seems to be dead; test requires a " + "living VM" % vm_name) + return vm + + +def wait_for_login(vm, nic_index=0, timeout=240): + """ + Try logging into a VM repeatedly. Stop on success or when timeout expires. + + @param vm: VM object. + @param nic_index: Index of NIC to access in the VM. + @param timeout: Time to wait before giving up. + @return: A shell session object. + """ + logging.info("Waiting for guest to be up...") + session = kvm_utils.wait_for(lambda: vm.remote_login(nic_index=nic_index), + timeout, 0, 2) + if not session: + raise error.TestFail("Could not log into guest") + logging.info("Logged in") + return session