Revision e969a81f test/py/cmdlib/test_unittest.py

b/test/py/cmdlib/test_unittest.py
21 21

  
22 22
"""Tests for LUTest*"""
23 23

  
24
import mock
24 25

  
25 26
from ganeti import constants
26
from ganeti import errors
27 27
from ganeti import opcodes
28 28

  
29 29
from testsupport import *
......
47 47
  def testInvalidDuration(self):
48 48
    op = opcodes.OpTestDelay(duration=-1)
49 49

  
50
    self.assertRaises(errors.OpExecError, self.ExecOpCode, op)
50
    self.ExecOpCodeExpectOpExecError(op)
51 51

  
52 52
  def testOnNodeUuid(self):
53 53
    node_uuids = [self.cfg.GetMasterNode()]
......
87 87
        .AddFailedNode(self.cfg.GetMasterNode()) \
88 88
        .Build()
89 89

  
90
    self.assertRaises(errors.OpExecError, self.ExecOpCode, op)
90
    self.ExecOpCodeExpectOpExecError(op)
91 91

  
92 92
  def testMultipleNodes(self):
93 93
    node1 = self.cfg.AddNewNode()
......
107 107
                                                     DELAY_DURATION)
108 108

  
109 109

  
110
class TestLUTestAllocator(CmdlibTestCase):
111
  def setUp(self):
112
    super(TestLUTestAllocator, self).setUp()
113

  
114
    self.base_op = opcodes.OpTestAllocator(
115
                      name="new-instance.example.com",
116
                      nics=[],
117
                      disks=[],
118
                      disk_template=constants.DT_DISKLESS,
119
                      direction=constants.IALLOCATOR_DIR_OUT,
120
                      iallocator="test")
121

  
122
    self.valid_alloc_op = \
123
      self.CopyOpCode(self.base_op,
124
                      mode=constants.IALLOCATOR_MODE_ALLOC,
125
                      memory=0,
126
                      disk_template=constants.DT_DISKLESS,
127
                      os="mock_os",
128
                      vcpus=1)
129
    self.valid_multi_alloc_op = \
130
      self.CopyOpCode(self.base_op,
131
                      mode=constants.IALLOCATOR_MODE_MULTI_ALLOC,
132
                      instances=["new-instance.example.com"],
133
                      memory=0,
134
                      disk_template=constants.DT_DISKLESS,
135
                      os="mock_os",
136
                      vcpus=1)
137
    self.valid_reloc_op = \
138
      self.CopyOpCode(self.base_op,
139
                      mode=constants.IALLOCATOR_MODE_RELOC)
140
    self.valid_chg_group_op = \
141
      self.CopyOpCode(self.base_op,
142
                      mode=constants.IALLOCATOR_MODE_CHG_GROUP,
143
                      instances=["new-instance.example.com"],
144
                      target_groups=["default"])
145
    self.valid_node_evac_op = \
146
      self.CopyOpCode(self.base_op,
147
                      mode=constants.IALLOCATOR_MODE_NODE_EVAC,
148
                      instances=["new-instance.example.com"],
149
                      evac_mode=constants.IALLOCATOR_NEVAC_PRI)
150

  
151
    self.iallocator_cls.return_value.in_text = "mock in text"
152
    self.iallocator_cls.return_value.out_text = "mock out text"
153

  
154
  def testMissingDirection(self):
155
    op = self.CopyOpCode(self.base_op,
156
                         direction=self.REMOVE)
157

  
158
    self.ExecOpCodeExpectOpPrereqError(
159
      op, "'OP_TEST_ALLOCATOR.direction' fails validation")
160

  
161
  def testAllocWrongDisks(self):
162
    op = self.CopyOpCode(self.valid_alloc_op,
163
                         disks=[0, "test"])
164

  
165
    self.ExecOpCodeExpectOpPrereqError(op, "Invalid contents")
166

  
167
  def testAllocWithExistingInstance(self):
168
    inst = self.cfg.AddNewInstance()
169
    op = self.CopyOpCode(self.valid_alloc_op, name=inst.name)
170

  
171
    self.ExecOpCodeExpectOpPrereqError(op, "already in the cluster")
172

  
173
  def testAllocMultiAllocMissingIAllocator(self):
174
    for mode in [constants.IALLOCATOR_MODE_ALLOC,
175
                 constants.IALLOCATOR_MODE_MULTI_ALLOC]:
176
      op = self.CopyOpCode(self.base_op,
177
                           mode=mode,
178
                           iallocator=None)
179

  
180
      self.ResetMocks()
181
      self.ExecOpCodeExpectOpPrereqError(op, "Missing allocator name")
182

  
183
  def testChgGroupNodeEvacMissingInstances(self):
184
    for mode in [constants.IALLOCATOR_MODE_CHG_GROUP,
185
                 constants.IALLOCATOR_MODE_NODE_EVAC]:
186
      op = self.CopyOpCode(self.base_op,
187
                           mode=mode)
188

  
189
      self.ResetMocks()
190
      self.ExecOpCodeExpectOpPrereqError(op, "Missing instances")
191

  
192
  def testAlloc(self):
193
    op = self.valid_alloc_op
194

  
195
    self.ExecOpCode(op)
196

  
197
    assert self.iallocator_cls.call_count == 1
198
    self.iallocator_cls.return_value.Run \
199
      .assert_called_once_with("test", validate=False)
200

  
201
  def testReloc(self):
202
    op = self.valid_reloc_op
203
    self.cfg.AddNewInstance(name=op.name)
204

  
205
    self.ExecOpCode(op)
206

  
207
    assert self.iallocator_cls.call_count == 1
208
    self.iallocator_cls.return_value.Run \
209
      .assert_called_once_with("test", validate=False)
210

  
211
  def testChgGroup(self):
212
    op = self.valid_chg_group_op
213
    for inst_name in op.instances:
214
      self.cfg.AddNewInstance(name=inst_name)
215

  
216
    self.ExecOpCode(op)
217

  
218
    assert self.iallocator_cls.call_count == 1
219
    self.iallocator_cls.return_value.Run \
220
      .assert_called_once_with("test", validate=False)
221

  
222
  def testNodeEvac(self):
223
    op = self.valid_node_evac_op
224
    for inst_name in op.instances:
225
      self.cfg.AddNewInstance(name=inst_name)
226

  
227
    self.ExecOpCode(op)
228

  
229
    assert self.iallocator_cls.call_count == 1
230
    self.iallocator_cls.return_value.Run \
231
      .assert_called_once_with("test", validate=False)
232

  
233
  def testMultiAlloc(self):
234
    op = self.valid_multi_alloc_op
235

  
236
    self.ExecOpCode(op)
237

  
238
    assert self.iallocator_cls.call_count == 1
239
    self.iallocator_cls.return_value.Run \
240
      .assert_called_once_with("test", validate=False)
241

  
242
  def testAllocDirectionIn(self):
243
    op = self.CopyOpCode(self.valid_alloc_op,
244
                         direction=constants.IALLOCATOR_DIR_IN)
245

  
246
    self.ExecOpCode(op)
247

  
248

  
110 249
if __name__ == "__main__":
111 250
  testutils.GanetiTestProgram()

Also available in: Unified diff