diff mbox

[KVM-AUTOTEST] KVM test: deal with incompatible env files gracefully (using version numbers)

Message ID 1278498738-2622-3-git-send-email-mgoldish@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish July 7, 2010, 10:32 a.m. UTC
None
diff mbox

Patch

diff --git a/client/tests/kvm/kvm.py b/client/tests/kvm/kvm.py
index 9da4c33..f656238 100644
--- a/client/tests/kvm/kvm.py
+++ b/client/tests/kvm/kvm.py
@@ -21,6 +21,7 @@  class kvm(test.test):
             (Online doc - Getting started with KVM testing)
     """
     version = 1
+    env_version = 0
 
     def run_once(self, params):
         # Report the parameters we've received and write them as keyvals
@@ -39,7 +40,7 @@  class kvm(test.test):
         logging.info("Unpickling env. You may see some harmless error "
                      "messages.")
         env_filename = os.path.join(self.bindir, params.get("env", "env"))
-        env = kvm_utils.load_env(env_filename, {})
+        env = kvm_utils.load_env(env_filename, self.env_version)
         logging.debug("Contents of environment: %s", env)
 
         test_passed = False
diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
index 4183f1c..fb2d1c2 100644
--- a/client/tests/kvm/kvm_utils.py
+++ b/client/tests/kvm/kvm_utils.py
@@ -21,17 +21,23 @@  def dump_env(obj, filename):
     file.close()
 
 
-def load_env(filename, default={}):
+def load_env(filename, version):
     """
-    Load KVM test environment from an environment file.
+    Load KVM test environment from an env file.
+    If the version recorded in the file is lower than version, return an empty
+    env.  If some other error occurs during unpickling, return an empty env.
 
-    @param filename: Path to a file where the environment was dumped to.
+    @param filename: Path to an env file.
     """
+    default = {"version": version}
     try:
         file = open(filename, "r")
-        obj = cPickle.load(file)
+        env = cPickle.load(file)
         file.close()
-        return obj
+        if env.get("version", 0) < version:
+            logging.warn("Incompatible env file found. Not using it.")
+            return default
+        return env
     # Almost any exception can be raised during unpickling, so let's catch
     # them all
     except Exception, e: