Stricter check for OS modifications passed to OpClusterSetParams
authorMichael Hanselmann <hansmi@google.com>
Tue, 31 Jan 2012 07:56:35 +0000 (08:56 +0100)
committerMichael Hanselmann <hansmi@google.com>
Wed, 1 Feb 2012 06:24:15 +0000 (07:24 +0100)
Don't just check the first element of each item, but also make sure the
OS name is a string and not empty.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

lib/opcodes.py
test/ganeti.opcodes_unittest.py

index cabee0b..e0ace9b 100644 (file)
@@ -36,7 +36,6 @@ opcodes.
 import logging
 import re
 
-from ganeti import compat
 from ganeti import constants
 from ganeti import errors
 from ganeti import ht
@@ -165,11 +164,13 @@ _PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
 _OPID_RE = re.compile("([a-z])([A-Z])")
 
 #: Utility function for L{OpClusterSetParams}
-_TestClusterOsList = ht.TOr(ht.TNone,
-  ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
-    ht.TMap(ht.WithDesc("GetFirstItem")(compat.fst),
-            ht.TElemOf(constants.DDMS_VALUES)))))
+_TestClusterOsListItem = \
+  ht.TAnd(ht.TIsLength(2), ht.TItems([
+    ht.TElemOf(constants.DDMS_VALUES),
+    ht.TNonEmptyString,
+    ]))
 
+_TestClusterOsList = ht.TOr(ht.TNone, ht.TListOf(_TestClusterOsListItem))
 
 # TODO: Generate check from constants.INIC_PARAMS_TYPES
 #: Utility function for testing NIC definitions
index e3ba1cc..59c3ae7 100755 (executable)
@@ -338,5 +338,31 @@ class TestResultChecks(unittest.TestCase):
       }))
 
 
+class TestClusterOsList(unittest.TestCase):
+  def test(self):
+    good = [
+      None,
+      [],
+      [(constants.DDM_ADD, "dos"),
+       (constants.DDM_REMOVE, "linux")],
+      ]
+
+    for i in good:
+      self.assertTrue(opcodes._TestClusterOsList(i))
+
+    wrong = ["", 0, "xy", ["Hello World"], object(),
+      [("foo", "bar")],
+      [("", "")],
+      [[constants.DDM_ADD]],
+      [(constants.DDM_ADD, "")],
+      [(constants.DDM_REMOVE, "")],
+      [(constants.DDM_ADD, None)],
+      [(constants.DDM_REMOVE, None)],
+      ]
+
+    for i in wrong:
+      self.assertFalse(opcodes._TestClusterOsList(i))
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()