Add predicate whether to call cfgupgrade --downgrade
authorKlaus Aehlig <aehlig@google.com>
Thu, 26 Sep 2013 14:15:22 +0000 (16:15 +0200)
committerKlaus Aehlig <aehlig@google.com>
Fri, 4 Oct 2013 14:10:21 +0000 (16:10 +0200)
Provide a predicate that, given the current version and the
version to go to, tells whether it is appropriate to call
cfgupgrade --downgrade.

Signed-off-by: Klaus Aehlig <aehlig@google.com>
Reviewed-by: Jose Lopes <jabolopes@google.com>

lib/utils/version.py
test/py/ganeti.utils.version_unittest.py

index d601499..2867f14 100644 (file)
@@ -119,3 +119,21 @@ def UpgradeRange(target, current=CURRENT_VERSION):
     return "can only downgrade one minor version at a time"
 
   return None
+
+
+def ShouldCfgdowngrade(version, current=CURRENT_VERSION):
+  """Decide whether cfgupgrade --downgrade should be called.
+
+  Given the current version and the version to change to, decide
+  if in the transition process cfgupgrade --downgrade should
+  be called
+
+  @param version: The version to upgrade to as (major, minor, revision)
+  @type version: tuple
+  @param current: The versino to upgrade from as (major, minor, revision)
+  @type current: tuple
+  @rtype: bool
+  @return: True, if cfgupgrade --downgrade should be called.
+
+  """
+  return version[0] == current[0] and version[1] == current[1] - 1
index 8e175b1..64eea71 100755 (executable)
@@ -56,6 +56,14 @@ class UpgradeRangeTest(unittest.TestCase):
         self.assertEquals(version.UpgradeRange((2,10,0), current=(2,9,0)),
                           "automatic upgrades only supported from 2.10 onwards")
 
+class ShouldCfgdowngradeTest(unittest.TestCase):
+    def testShouldCfgDowngrade(self):
+        self.assertTrue(version.ShouldCfgdowngrade((2,9,3), current=(2,10,0)))
+        self.assertTrue(version.ShouldCfgdowngrade((2,9,0), current=(2,10,4)))
+        self.assertFalse(version.ShouldCfgdowngrade((2,9,0), current=(2,11,0)))
+        self.assertFalse(version.ShouldCfgdowngrade((2,9,0), current=(3,10,0)))
+        self.assertFalse(version.ShouldCfgdowngrade((2,10,0), current=(3,10,0)))
+
 
 if __name__ == "__main__":
   testutils.GanetiTestProgram()