Revision 1ca7b773 test/ganeti.client.gnt_cluster_unittest.py

b/test/ganeti.client.gnt_cluster_unittest.py
22 22
"""Script for testing ganeti.client.gnt_cluster"""
23 23

  
24 24
import unittest
25
import optparse
25 26

  
26 27
from ganeti.client import gnt_cluster
27 28
from ganeti import utils
28 29
from ganeti import compat
30
from ganeti import constants
29 31

  
30 32
import testutils
31 33

  
32 34

  
33
class TestEpo(unittest.TestCase):
35
class TestEpoUtilities(unittest.TestCase):
34 36
  def setUp(self):
35 37
    self.nodes2ip = dict(("node%s" % i, "192.0.2.%s" % i) for i in range(1, 10))
36 38
    self.nodes = set(self.nodes2ip.keys())
......
143 145
    self.assertFalse(inst_map)
144 146

  
145 147

  
148
class _ClientForEpo:
149
  def __init__(self, groups, nodes):
150
    self._groups = groups
151
    self._nodes = nodes
152

  
153
  def QueryGroups(self, names, fields, use_locking):
154
    assert not use_locking
155
    assert fields == ["node_list"]
156
    return self._groups
157

  
158
  def QueryNodes(self, names, fields, use_locking):
159
    assert not use_locking
160
    assert fields == ["name", "master", "pinst_list", "sinst_list", "powered",
161
                      "offline"]
162
    return self._nodes
163

  
164

  
165
class TestEpo(unittest.TestCase):
166
  _ON_EXITCODE = 253
167
  _OFF_EXITCODE = 254
168

  
169
  def _ConfirmForce(self, *args):
170
    self.fail("Shouldn't need confirmation")
171

  
172
  def _Confirm(self, exp_names, result, names, ltype, text):
173
    self.assertEqual(names, exp_names)
174
    self.assertFalse(result is NotImplemented)
175
    return result
176

  
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
181

  
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)
189

  
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)
194

  
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)
199

  
200
  def testNoArgumentsNoParameters(self):
201
    for (force, confirm_result) in [(True, NotImplemented), (False, False),
202
                                    (False, True)]:
203
      opts = optparse.Values(dict(groups=False, show_all=False, force=force,
204
                                  on=False))
205
      client = _ClientForEpo(NotImplemented, [
206
        ("node1.example.com", False, [], [], True, False),
207
        ])
208

  
209
      if force:
210
        confirm_fn = self._ConfirmForce
211
      else:
212
        confirm_fn = compat.partial(self._Confirm, ["node1.example.com"],
213
                                    confirm_result)
214

  
215
      off_fn = compat.partial(self._Off, ["node1.example.com"])
216

  
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)
221
      else:
222
        self.assertEqual(result, constants.EXIT_FAILURE)
223

  
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),
234
        ])
235

  
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)])
239
        if master:
240
          self.assertEqual(node_list, ["node2.example.com"])
241
        else:
242
          self.assertEqual(node_list, ["node2.example.com",
243
                                       "node5.example.com"])
244
        self.assertFalse(inst_map)
245
        return self._ON_EXITCODE
246

  
247
      result = self._Test(opts, [], cl=client, _on_fn=_On,
248
                          _confirm_fn=self._ConfirmForce)
249
      self.assertEqual(result, self._ON_EXITCODE)
250

  
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),
256
      ])
257
    result = self._Test(opts, [], cl=client, _confirm_fn=self._ConfirmForce)
258
    self.assertEqual(result, constants.EXIT_FAILURE)
259

  
260

  
146 261
if __name__ == "__main__":
147 262
  testutils.GanetiTestProgram()

Also available in: Unified diff