From patchwork Tue Dec 28 15:43:07 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 436601 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id oBSFiIgr022292 for ; Tue, 28 Dec 2010 15:44:18 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751952Ab0L1PoK (ORCPT ); Tue, 28 Dec 2010 10:44:10 -0500 Received: from mx1.redhat.com ([209.132.183.28]:11514 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754076Ab0L1PoJ (ORCPT ); Tue, 28 Dec 2010 10:44:09 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oBSFi7W2032105 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 28 Dec 2010 10:44:07 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id oBSFi75b028708; Tue, 28 Dec 2010 10:44:07 -0500 Received: from moof.tlv.redhat.com (dhcp-1-185.tlv.redhat.com [10.35.1.185]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id oBSFi39e019837; Tue, 28 Dec 2010 10:44:05 -0500 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish , Eduardo Habkost Subject: [KVM-AUTOTEST PATCH 1/4] KVM test: introduce the Env class Date: Tue, 28 Dec 2010 17:43:07 +0200 Message-Id: <1293550990-10669-2-git-send-email-mgoldish@redhat.com> In-Reply-To: <1293550990-10669-1-git-send-email-mgoldish@redhat.com> References: <1293550990-10669-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 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 (demeter1.kernel.org [140.211.167.41]); Tue, 28 Dec 2010 15:44:18 +0000 (UTC) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 2e4ba92..2423dd9 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -62,6 +62,91 @@ def load_env(filename, version): return default +class Env(UserDict.IterableUserDict): + """ + A dict-like object containing global objects used by tests. + """ + def __init__(self, filename=None, version=0): + """ + Create an empty Env object or load an existing one from a file. + + If the version recorded in the file is lower than version, or if some + error occurs during unpickling, or if filename is not supplied, + create an empty Env object. + + @param filename: Path to an env file. + @param version: Required env version (int). + """ + UserDict.IterableUserDict.__init__(self) + empty = {"version": version} + if filename: + self._filename = filename + try: + f = open(filename, "r") + env = cPickle.load(f) + f.close() + if env.get("version", 0) >= version: + self.data = env + else: + logging.warn("Incompatible env file found. Not using it.") + self.data = empty + # Almost any exception can be raised during unpickling, so let's + # catch them all + except Exception, e: + logging.warn(e) + self.data = empty + else: + self.data = empty + + + def save(self, filename=None): + """ + Pickle the contents of the Env object into a file. + + @param filename: Filename to pickle the dict into. If not supplied, + use the filename from which the dict was loaded. + """ + filename = filename or self._filename + f = open(filename, "w") + cPickle.dump(self.data, f) + f.close() + + + def get_all_vms(self): + """ + Return a list of all VM objects in this Env object. + """ + return [o for o in self.values() if is_vm(o)] + + + def get_vm(self, name): + """ + Return a VM object by its name. + + @param name: VM name. + """ + return self.get("vm__%s" % name) + + + def register_vm(self, name, vm): + """ + Register a VM in this Env object. + + @param name: VM name. + @param vm: VM object. + """ + self["vm__%s" % name] = vm + + + def unregister_vm(self, name): + """ + Remove a given VM. + + @param name: VM name. + """ + del self["vm__%s" % name] + + class Params(UserDict.IterableUserDict): """ A dict-like object passed to every test.