4 # Copyright (C) 2011 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.client.gnt_cluster"""
27 from ganeti.client import gnt_cluster
28 from ganeti import utils
29 from ganeti import compat
30 from ganeti import constants
35 class TestEpoUtilities(unittest.TestCase):
37 self.nodes2ip = dict(("node%s" % i, "192.0.2.%s" % i) for i in range(1, 10))
38 self.nodes = set(self.nodes2ip.keys())
39 self.ips2node = dict((v, k) for (k, v) in self.nodes2ip.items())
41 def _FakeAction(*args):
44 def _FakePing(ip, port, live_port_needed=False):
45 self.assert_(live_port_needed)
46 self.assertEqual(port, 0)
50 self.assert_(secs >= 0 and secs <= 5)
53 def _NoopFeedback(self, text):
56 def testPingFnRemoveHostsUp(self):
58 def _FakeSeenPing(ip, *args, **kwargs):
59 node = self.ips2node[ip]
60 self.assertFalse(node in seen)
64 helper = gnt_cluster._RunWhenNodesReachableHelper(self.nodes,
68 _ping_fn=_FakeSeenPing,
69 _sleep_fn=self._FakeSleep)
71 nodes_len = len(self.nodes)
72 for (num, _) in enumerate(self.nodes):
74 if num < nodes_len - 1:
75 self.assertRaises(utils.RetryAgain, helper)
79 self.assertEqual(seen, self.nodes)
80 self.assertFalse(helper.down)
81 self.assertEqual(helper.up, self.nodes)
83 def testActionReturnFalseSetsHelperFalse(self):
85 def _FalseAction(*args):
88 helper = gnt_cluster._RunWhenNodesReachableHelper(self.nodes, _FalseAction,
91 _ping_fn=self._FakePing,
92 _sleep_fn=self._FakeSleep)
96 except utils.RetryAgain:
99 self.assertFalse(helper.success)
101 def testMaybeInstanceStartup(self):
103 def _FakeInstanceStart(opts, instances, start):
104 instances_arg.append(set(instances))
108 "inst1": set(["node1", "node2"]),
109 "inst2": set(["node1", "node3"]),
110 "inst3": set(["node2", "node1"]),
111 "inst4": set(["node2", "node1", "node3"]),
112 "inst5": set(["node4"]),
115 fn = _FakeInstanceStart
116 self.assert_(gnt_cluster._MaybeInstanceStartup(None, inst_map, set(),
117 _instance_start_fn=fn))
118 self.assertFalse(instances_arg)
119 result = gnt_cluster._MaybeInstanceStartup(None, inst_map, set(["node1"]),
120 _instance_start_fn=fn)
122 self.assertFalse(instances_arg)
123 result = gnt_cluster._MaybeInstanceStartup(None, inst_map,
124 set(["node1", "node3"]),
125 _instance_start_fn=fn)
126 self.assert_(result is None)
127 self.assertEqual(instances_arg.pop(0), set(["inst2"]))
128 self.assertFalse("inst2" in inst_map)
129 result = gnt_cluster._MaybeInstanceStartup(None, inst_map,
130 set(["node1", "node3"]),
131 _instance_start_fn=fn)
133 self.assertFalse(instances_arg)
134 result = gnt_cluster._MaybeInstanceStartup(None, inst_map,
135 set(["node1", "node3", "node2"]),
136 _instance_start_fn=fn)
137 self.assertEqual(instances_arg.pop(0), set(["inst1", "inst3", "inst4"]))
138 self.assert_(result is None)
139 result = gnt_cluster._MaybeInstanceStartup(None, inst_map,
140 set(["node1", "node3", "node2",
142 _instance_start_fn=fn)
143 self.assert_(result is None)
144 self.assertEqual(instances_arg.pop(0), set(["inst5"]))
145 self.assertFalse(inst_map)
149 def __init__(self, groups, nodes):
150 self._groups = groups
153 def QueryGroups(self, names, fields, use_locking):
154 assert not use_locking
155 assert fields == ["node_list"]
158 def QueryNodes(self, names, fields, use_locking):
159 assert not use_locking
160 assert fields == ["name", "master", "pinst_list", "sinst_list", "powered",
165 class TestEpo(unittest.TestCase):
169 def _ConfirmForce(self, *args):
170 self.fail("Shouldn't need confirmation")
172 def _Confirm(self, exp_names, result, names, ltype, text):
173 self.assertEqual(names, exp_names)
174 self.assertFalse(result is NotImplemented)
177 def _Off(self, exp_node_list, opts, node_list, inst_map):
178 self.assertEqual(node_list, exp_node_list)
179 self.assertFalse(inst_map)
180 return self._OFF_EXITCODE
182 def _Test(self, *args, **kwargs):
183 defaults = dict(cl=NotImplemented, _on_fn=NotImplemented,
184 _off_fn=NotImplemented,
185 _stdout_fn=lambda *args: None,
186 _stderr_fn=lambda *args: None)
187 defaults.update(kwargs)
188 return gnt_cluster.Epo(*args, **defaults)
190 def testShowAllWithGroups(self):
191 opts = optparse.Values(dict(groups=True, show_all=True))
192 result = self._Test(opts, NotImplemented)
193 self.assertEqual(result, constants.EXIT_FAILURE)
195 def testShowAllWithArgs(self):
196 opts = optparse.Values(dict(groups=False, show_all=True))
197 result = self._Test(opts, ["a", "b", "c"])
198 self.assertEqual(result, constants.EXIT_FAILURE)
200 def testNoArgumentsNoParameters(self):
201 for (force, confirm_result) in [(True, NotImplemented), (False, False),
203 opts = optparse.Values(dict(groups=False, show_all=False, force=force,
205 client = _ClientForEpo(NotImplemented, [
206 ("node1.example.com", False, [], [], True, False),
210 confirm_fn = self._ConfirmForce
212 confirm_fn = compat.partial(self._Confirm, ["node1.example.com"],
215 off_fn = compat.partial(self._Off, ["node1.example.com"])
217 result = self._Test(opts, [], cl=client, _off_fn=off_fn,
218 _confirm_fn=confirm_fn)
219 if force or confirm_result:
220 self.assertEqual(result, self._OFF_EXITCODE)
222 self.assertEqual(result, constants.EXIT_FAILURE)
224 def testPowerOn(self):
225 for master in [False, True]:
226 opts = optparse.Values(dict(groups=False, show_all=True,
227 force=True, on=True))
228 client = _ClientForEpo(NotImplemented, [
229 ("node1.example.com", False, [], [], True, False),
230 ("node2.example.com", False, [], [], False, False),
231 ("node3.example.com", False, [], [], True, True),
232 ("node4.example.com", False, [], [], None, True),
233 ("node5.example.com", master, [], [], False, False),
236 def _On(_, all_nodes, node_list, inst_map):
237 self.assertEqual(all_nodes,
238 ["node%s.example.com" % i for i in range(1, 6)])
240 self.assertEqual(node_list, ["node2.example.com"])
242 self.assertEqual(node_list, ["node2.example.com",
243 "node5.example.com"])
244 self.assertFalse(inst_map)
245 return self._ON_EXITCODE
247 result = self._Test(opts, [], cl=client, _on_fn=_On,
248 _confirm_fn=self._ConfirmForce)
249 self.assertEqual(result, self._ON_EXITCODE)
251 def testMasterWithoutShowAll(self):
252 opts = optparse.Values(dict(groups=False, show_all=False,
253 force=True, on=False))
254 client = _ClientForEpo(NotImplemented, [
255 ("node1.example.com", True, [], [], True, False),
257 result = self._Test(opts, [], cl=client, _confirm_fn=self._ConfirmForce)
258 self.assertEqual(result, constants.EXIT_FAILURE)
261 if __name__ == "__main__":
262 testutils.GanetiTestProgram()