_CheckIAllocatorOrNode unit tests
authorApollon Oikonomopoulos <apollon@noc.grnet.gr>
Thu, 8 Jul 2010 12:04:15 +0000 (15:04 +0300)
committerIustin Pop <iustin@google.com>
Thu, 8 Jul 2010 13:45:14 +0000 (15:45 +0200)
Add unit tests to check the function of _CheckIAllocatorOrNode

Signed-off-by: Apollon Oikonomopoulos <apollon@noc.grnet.gr>
Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

test/ganeti.cmdlib_unittest.py
test/mocks.py

index 0606f8d..067eba3 100755 (executable)
@@ -35,6 +35,7 @@ from ganeti import errors
 from ganeti import utils
 
 import testutils
+import mocks
 
 
 class TestCertVerification(testutils.GanetiTestCase):
@@ -94,5 +95,57 @@ class TestOpcodeParams(testutils.GanetiTestCase):
                   (lu_name, utils.CommaJoin(extra)))
 
 
+class TestIAllocatorChecks(testutils.GanetiTestCase):
+  def testFunction(self):
+    class TestLU(object):
+      def __init__(self, opcode):
+        self.cfg = mocks.FakeConfig()
+        self.op = opcode
+
+    class TestOpcode(opcodes.OpCode):
+      OP_ID = "OP_TEST"
+      __slots__ = ["iallocator", "node"]
+
+    default_iallocator = mocks.FakeConfig().GetDefaultIAllocator()
+    other_iallocator = default_iallocator + "_not"
+
+    op = TestOpcode()
+    lu = TestLU(op)
+
+    c_i = lambda: cmdlib._CheckIAllocatorOrNode(lu, "iallocator", "node")
+
+    # Neither node nor iallocator given
+    op.iallocator = None
+    op.node = None
+    c_i()
+    self.assertEqual(lu.op.iallocator, default_iallocator)
+    self.assertEqual(lu.op.node, None)
+
+    # Both, iallocator and node given
+    op.iallocator = "test"
+    op.node = "test"
+    self.assertRaises(errors.OpPrereqError, c_i)
+
+    # Only iallocator given
+    op.iallocator = other_iallocator
+    op.node = None
+    c_i()
+    self.assertEqual(lu.op.iallocator, other_iallocator)
+    self.assertEqual(lu.op.node, None)
+
+    # Only node given
+    op.iallocator = None
+    op.node = "node"
+    c_i()
+    self.assertEqual(lu.op.iallocator, None)
+    self.assertEqual(lu.op.node, "node")
+
+    # No node, iallocator or default iallocator
+    op.iallocator = None
+    op.node = None
+    lu.cfg.GetDefaultIAllocator = lambda: None
+    self.assertRaises(errors.OpPrereqError, c_i)
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()
index 4c0ae07..5729ac6 100644 (file)
@@ -51,6 +51,9 @@ class FakeConfig:
     def GetMasterNode(self):
         return utils.HostInfo().name
 
+    def GetDefaultIAllocator(Self):
+        return "testallocator"
+
 
 class FakeProc:
     """Fake processor object"""