From bd5f214b850a814f1d1b7f304c4e5adeff1f080f Mon Sep 17 00:00:00 2001 From: Apollon Oikonomopoulos <apollon@noc.grnet.gr> Date: Thu, 8 Jul 2010 15:04:15 +0300 Subject: [PATCH] _CheckIAllocatorOrNode unit tests 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 | 53 ++++++++++++++++++++++++++++++++++ test/mocks.py | 3 ++ 2 files changed, 56 insertions(+) diff --git a/test/ganeti.cmdlib_unittest.py b/test/ganeti.cmdlib_unittest.py index 0606f8d0c..067eba31b 100755 --- a/test/ganeti.cmdlib_unittest.py +++ b/test/ganeti.cmdlib_unittest.py @@ -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() diff --git a/test/mocks.py b/test/mocks.py index 4c0ae072c..5729ac646 100644 --- a/test/mocks.py +++ b/test/mocks.py @@ -51,6 +51,9 @@ class FakeConfig: def GetMasterNode(self): return utils.HostInfo().name + def GetDefaultIAllocator(Self): + return "testallocator" + class FakeProc: """Fake processor object""" -- GitLab