4 # Copyright (C) 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for testing ganeti.masterd.iallocator"""
26 from ganeti import compat
27 from ganeti import constants
28 from ganeti import errors
29 from ganeti import objects
31 from ganeti.masterd import iallocator
36 class _StubIAllocator(object):
37 def __init__(self, success):
38 self.success = success
41 class TestIAReqMultiInstanceAlloc(unittest.TestCase):
44 # First result (all instances "allocate")
51 # Second result (partial "allocate", partial "fail")
57 # Third result (all instances "fail")
71 result_fn = iallocator.IAReqMultiInstanceAlloc.REQ_RESULT
73 self.assertTrue(compat.all(map(result_fn, good_results)))
74 self.assertFalse(compat.any(map(result_fn, bad_results)))
77 class TestIARequestBase(unittest.TestCase):
78 def testValidateResult(self):
79 class _StubReqBase(iallocator.IARequestBase):
80 MODE = constants.IALLOCATOR_MODE_ALLOC
84 stub.ValidateResult(_StubIAllocator(True), True)
85 self.assertRaises(errors.ResultValidationError, stub.ValidateResult,
86 _StubIAllocator(True), "foo")
87 stub.ValidateResult(_StubIAllocator(False), True)
88 # We don't validate the result if the iallocation request was not successful
89 stub.ValidateResult(_StubIAllocator(False), "foo")
92 class _FakeConfigWithNdParams:
93 def GetNdParams(self, _):
97 class TestComputeBasicNodeData(unittest.TestCase):
99 self.fn = compat.partial(iallocator.IAllocator._ComputeBasicNodeData,
100 _FakeConfigWithNdParams())
103 self.assertEqual(self.fn({}, None), {})
105 def testSimple(self):
106 node1 = objects.Node(name="node1",
107 primary_ip="192.0.2.1",
108 secondary_ip="192.0.2.2",
111 master_candidate=True,
116 node2 = objects.Node(name="node2",
117 primary_ip="192.0.2.3",
118 secondary_ip="192.0.2.4",
121 master_candidate=False,
122 master_capable=False,
126 assert node1 != node2
133 self.assertEqual(self.fn(ninfo, None), {
136 "primary_ip": "192.0.2.1",
137 "secondary_ip": "192.0.2.2",
140 "master_candidate": True,
142 "master_capable": True,
148 "primary_ip": "192.0.2.3",
149 "secondary_ip": "192.0.2.4",
152 "master_candidate": False,
154 "master_capable": False,
160 def testOfflineNode(self):
161 for whitelist in [None, [], set(), ["node1"], ["node2"]]:
163 "node1": objects.Node(name="node1", offline=True)
165 self.assertEqual(len(result), 1)
166 self.assertTrue(result["node1"]["offline"])
168 def testWhitelist(self):
169 for whitelist in [None, [], set(), ["node1"], ["node2"]]:
171 "node1": objects.Node(name="node1", offline=False)
173 self.assertEqual(len(result), 1)
175 if whitelist is None or "node1" in whitelist:
176 self.assertFalse(result["node1"]["offline"])
178 self.assertTrue(result["node1"]["offline"])
181 if __name__ == "__main__":
182 testutils.GanetiTestProgram()