Statistics
| Branch: | Tag: | Revision:

root / test / py / cmdlib / test_unittest.py @ b02063fe

History | View | Annotate | Download (7.9 kB)

1 3efa7659 Thomas Thrainer
#!/usr/bin/python
2 3efa7659 Thomas Thrainer
#
3 3efa7659 Thomas Thrainer
4 3efa7659 Thomas Thrainer
# Copyright (C) 2013 Google Inc.
5 3efa7659 Thomas Thrainer
#
6 3efa7659 Thomas Thrainer
# This program is free software; you can redistribute it and/or modify
7 3efa7659 Thomas Thrainer
# it under the terms of the GNU General Public License as published by
8 3efa7659 Thomas Thrainer
# the Free Software Foundation; either version 2 of the License, or
9 3efa7659 Thomas Thrainer
# (at your option) any later version.
10 3efa7659 Thomas Thrainer
#
11 3efa7659 Thomas Thrainer
# This program is distributed in the hope that it will be useful, but
12 3efa7659 Thomas Thrainer
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 3efa7659 Thomas Thrainer
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 3efa7659 Thomas Thrainer
# General Public License for more details.
15 3efa7659 Thomas Thrainer
#
16 3efa7659 Thomas Thrainer
# You should have received a copy of the GNU General Public License
17 3efa7659 Thomas Thrainer
# along with this program; if not, write to the Free Software
18 3efa7659 Thomas Thrainer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 3efa7659 Thomas Thrainer
# 02110-1301, USA.
20 3efa7659 Thomas Thrainer
21 3efa7659 Thomas Thrainer
22 bd39b6bb Thomas Thrainer
"""Tests for LUTest*"""
23 3efa7659 Thomas Thrainer
24 e969a81f Thomas Thrainer
import mock
25 3efa7659 Thomas Thrainer
26 3efa7659 Thomas Thrainer
from ganeti import constants
27 3efa7659 Thomas Thrainer
from ganeti import opcodes
28 3efa7659 Thomas Thrainer
29 3efa7659 Thomas Thrainer
from testsupport import *
30 3efa7659 Thomas Thrainer
31 3efa7659 Thomas Thrainer
import testutils
32 3efa7659 Thomas Thrainer
33 3efa7659 Thomas Thrainer
DELAY_DURATION = 0.01
34 3efa7659 Thomas Thrainer
35 3efa7659 Thomas Thrainer
36 3efa7659 Thomas Thrainer
class TestLUTestDelay(CmdlibTestCase):
37 3efa7659 Thomas Thrainer
  def testRepeatedInvocation(self):
38 3efa7659 Thomas Thrainer
    op = opcodes.OpTestDelay(duration=DELAY_DURATION,
39 3efa7659 Thomas Thrainer
                             repeat=3)
40 3efa7659 Thomas Thrainer
    self.ExecOpCode(op)
41 3efa7659 Thomas Thrainer
42 3efa7659 Thomas Thrainer
    self.assertLogContainsMessage(" - INFO: Test delay iteration 0/2")
43 3efa7659 Thomas Thrainer
    self.mcpu.assertLogContainsEntry(constants.ELOG_MESSAGE,
44 3efa7659 Thomas Thrainer
                                     " - INFO: Test delay iteration 1/2")
45 3efa7659 Thomas Thrainer
    self.assertLogContainsRegex("2/2$")
46 3efa7659 Thomas Thrainer
47 3efa7659 Thomas Thrainer
  def testInvalidDuration(self):
48 3efa7659 Thomas Thrainer
    op = opcodes.OpTestDelay(duration=-1)
49 3efa7659 Thomas Thrainer
50 e969a81f Thomas Thrainer
    self.ExecOpCodeExpectOpExecError(op)
51 3efa7659 Thomas Thrainer
52 3efa7659 Thomas Thrainer
  def testOnNodeUuid(self):
53 f02733cc Thomas Thrainer
    node_uuids = [self.master_uuid]
54 3efa7659 Thomas Thrainer
    op = opcodes.OpTestDelay(duration=DELAY_DURATION,
55 3efa7659 Thomas Thrainer
                             on_node_uuids=node_uuids)
56 3efa7659 Thomas Thrainer
    self.ExecOpCode(op)
57 3efa7659 Thomas Thrainer
58 3efa7659 Thomas Thrainer
    self.rpc.call_test_delay.assert_called_once_with(node_uuids, DELAY_DURATION)
59 3efa7659 Thomas Thrainer
60 3efa7659 Thomas Thrainer
  def testOnNodeName(self):
61 3efa7659 Thomas Thrainer
    op = opcodes.OpTestDelay(duration=DELAY_DURATION,
62 f02733cc Thomas Thrainer
                             on_nodes=[self.master.name])
63 3efa7659 Thomas Thrainer
    self.ExecOpCode(op)
64 3efa7659 Thomas Thrainer
65 f02733cc Thomas Thrainer
    self.rpc.call_test_delay.assert_called_once_with([self.master_uuid],
66 3efa7659 Thomas Thrainer
                                                     DELAY_DURATION)
67 3efa7659 Thomas Thrainer
68 3efa7659 Thomas Thrainer
  def testSuccessfulRpc(self):
69 3efa7659 Thomas Thrainer
    op = opcodes.OpTestDelay(duration=DELAY_DURATION,
70 f02733cc Thomas Thrainer
                             on_nodes=[self.master.name])
71 3efa7659 Thomas Thrainer
72 3efa7659 Thomas Thrainer
    self.rpc.call_test_delay.return_value = \
73 a794b8d7 Thomas Thrainer
      self.RpcResultsBuilder() \
74 f02733cc Thomas Thrainer
        .AddSuccessfulNode(self.master) \
75 3efa7659 Thomas Thrainer
        .Build()
76 3efa7659 Thomas Thrainer
77 3efa7659 Thomas Thrainer
    self.ExecOpCode(op)
78 3efa7659 Thomas Thrainer
79 3efa7659 Thomas Thrainer
    self.rpc.call_test_delay.assert_called_once()
80 3efa7659 Thomas Thrainer
81 3efa7659 Thomas Thrainer
  def testFailingRpc(self):
82 3efa7659 Thomas Thrainer
    op = opcodes.OpTestDelay(duration=DELAY_DURATION,
83 f02733cc Thomas Thrainer
                             on_nodes=[self.master.name])
84 3efa7659 Thomas Thrainer
85 3efa7659 Thomas Thrainer
    self.rpc.call_test_delay.return_value = \
86 a794b8d7 Thomas Thrainer
      self.RpcResultsBuilder() \
87 f02733cc Thomas Thrainer
        .AddFailedNode(self.master) \
88 3efa7659 Thomas Thrainer
        .Build()
89 3efa7659 Thomas Thrainer
90 e969a81f Thomas Thrainer
    self.ExecOpCodeExpectOpExecError(op)
91 3efa7659 Thomas Thrainer
92 3efa7659 Thomas Thrainer
  def testMultipleNodes(self):
93 3efa7659 Thomas Thrainer
    node1 = self.cfg.AddNewNode()
94 3efa7659 Thomas Thrainer
    node2 = self.cfg.AddNewNode()
95 3efa7659 Thomas Thrainer
    op = opcodes.OpTestDelay(duration=DELAY_DURATION,
96 3efa7659 Thomas Thrainer
                             on_nodes=[node1.name, node2.name])
97 3efa7659 Thomas Thrainer
98 3efa7659 Thomas Thrainer
    self.rpc.call_test_delay.return_value = \
99 a794b8d7 Thomas Thrainer
      self.RpcResultsBuilder() \
100 3efa7659 Thomas Thrainer
        .AddSuccessfulNode(node1) \
101 3efa7659 Thomas Thrainer
        .AddSuccessfulNode(node2) \
102 3efa7659 Thomas Thrainer
        .Build()
103 3efa7659 Thomas Thrainer
104 3efa7659 Thomas Thrainer
    self.ExecOpCode(op)
105 3efa7659 Thomas Thrainer
106 3efa7659 Thomas Thrainer
    self.rpc.call_test_delay.assert_called_once_with([node1.uuid, node2.uuid],
107 3efa7659 Thomas Thrainer
                                                     DELAY_DURATION)
108 3efa7659 Thomas Thrainer
109 3efa7659 Thomas Thrainer
110 e969a81f Thomas Thrainer
class TestLUTestAllocator(CmdlibTestCase):
111 e969a81f Thomas Thrainer
  def setUp(self):
112 e969a81f Thomas Thrainer
    super(TestLUTestAllocator, self).setUp()
113 e969a81f Thomas Thrainer
114 e969a81f Thomas Thrainer
    self.base_op = opcodes.OpTestAllocator(
115 e969a81f Thomas Thrainer
                      name="new-instance.example.com",
116 e969a81f Thomas Thrainer
                      nics=[],
117 e969a81f Thomas Thrainer
                      disks=[],
118 e969a81f Thomas Thrainer
                      disk_template=constants.DT_DISKLESS,
119 e969a81f Thomas Thrainer
                      direction=constants.IALLOCATOR_DIR_OUT,
120 e969a81f Thomas Thrainer
                      iallocator="test")
121 e969a81f Thomas Thrainer
122 e969a81f Thomas Thrainer
    self.valid_alloc_op = \
123 e969a81f Thomas Thrainer
      self.CopyOpCode(self.base_op,
124 e969a81f Thomas Thrainer
                      mode=constants.IALLOCATOR_MODE_ALLOC,
125 e969a81f Thomas Thrainer
                      memory=0,
126 e969a81f Thomas Thrainer
                      disk_template=constants.DT_DISKLESS,
127 e969a81f Thomas Thrainer
                      os="mock_os",
128 e969a81f Thomas Thrainer
                      vcpus=1)
129 e969a81f Thomas Thrainer
    self.valid_multi_alloc_op = \
130 e969a81f Thomas Thrainer
      self.CopyOpCode(self.base_op,
131 e969a81f Thomas Thrainer
                      mode=constants.IALLOCATOR_MODE_MULTI_ALLOC,
132 e969a81f Thomas Thrainer
                      instances=["new-instance.example.com"],
133 e969a81f Thomas Thrainer
                      memory=0,
134 e969a81f Thomas Thrainer
                      disk_template=constants.DT_DISKLESS,
135 e969a81f Thomas Thrainer
                      os="mock_os",
136 e969a81f Thomas Thrainer
                      vcpus=1)
137 e969a81f Thomas Thrainer
    self.valid_reloc_op = \
138 e969a81f Thomas Thrainer
      self.CopyOpCode(self.base_op,
139 e969a81f Thomas Thrainer
                      mode=constants.IALLOCATOR_MODE_RELOC)
140 e969a81f Thomas Thrainer
    self.valid_chg_group_op = \
141 e969a81f Thomas Thrainer
      self.CopyOpCode(self.base_op,
142 e969a81f Thomas Thrainer
                      mode=constants.IALLOCATOR_MODE_CHG_GROUP,
143 e969a81f Thomas Thrainer
                      instances=["new-instance.example.com"],
144 e969a81f Thomas Thrainer
                      target_groups=["default"])
145 e969a81f Thomas Thrainer
    self.valid_node_evac_op = \
146 e969a81f Thomas Thrainer
      self.CopyOpCode(self.base_op,
147 e969a81f Thomas Thrainer
                      mode=constants.IALLOCATOR_MODE_NODE_EVAC,
148 e969a81f Thomas Thrainer
                      instances=["new-instance.example.com"],
149 e969a81f Thomas Thrainer
                      evac_mode=constants.IALLOCATOR_NEVAC_PRI)
150 e969a81f Thomas Thrainer
151 e969a81f Thomas Thrainer
    self.iallocator_cls.return_value.in_text = "mock in text"
152 e969a81f Thomas Thrainer
    self.iallocator_cls.return_value.out_text = "mock out text"
153 e969a81f Thomas Thrainer
154 e969a81f Thomas Thrainer
  def testMissingDirection(self):
155 e969a81f Thomas Thrainer
    op = self.CopyOpCode(self.base_op,
156 e969a81f Thomas Thrainer
                         direction=self.REMOVE)
157 e969a81f Thomas Thrainer
158 e969a81f Thomas Thrainer
    self.ExecOpCodeExpectOpPrereqError(
159 e969a81f Thomas Thrainer
      op, "'OP_TEST_ALLOCATOR.direction' fails validation")
160 e969a81f Thomas Thrainer
161 e969a81f Thomas Thrainer
  def testAllocWrongDisks(self):
162 e969a81f Thomas Thrainer
    op = self.CopyOpCode(self.valid_alloc_op,
163 e969a81f Thomas Thrainer
                         disks=[0, "test"])
164 e969a81f Thomas Thrainer
165 e969a81f Thomas Thrainer
    self.ExecOpCodeExpectOpPrereqError(op, "Invalid contents")
166 e969a81f Thomas Thrainer
167 e969a81f Thomas Thrainer
  def testAllocWithExistingInstance(self):
168 e969a81f Thomas Thrainer
    inst = self.cfg.AddNewInstance()
169 e969a81f Thomas Thrainer
    op = self.CopyOpCode(self.valid_alloc_op, name=inst.name)
170 e969a81f Thomas Thrainer
171 e969a81f Thomas Thrainer
    self.ExecOpCodeExpectOpPrereqError(op, "already in the cluster")
172 e969a81f Thomas Thrainer
173 e969a81f Thomas Thrainer
  def testAllocMultiAllocMissingIAllocator(self):
174 e969a81f Thomas Thrainer
    for mode in [constants.IALLOCATOR_MODE_ALLOC,
175 e969a81f Thomas Thrainer
                 constants.IALLOCATOR_MODE_MULTI_ALLOC]:
176 e969a81f Thomas Thrainer
      op = self.CopyOpCode(self.base_op,
177 e969a81f Thomas Thrainer
                           mode=mode,
178 e969a81f Thomas Thrainer
                           iallocator=None)
179 e969a81f Thomas Thrainer
180 e969a81f Thomas Thrainer
      self.ResetMocks()
181 e969a81f Thomas Thrainer
      self.ExecOpCodeExpectOpPrereqError(op, "Missing allocator name")
182 e969a81f Thomas Thrainer
183 e969a81f Thomas Thrainer
  def testChgGroupNodeEvacMissingInstances(self):
184 e969a81f Thomas Thrainer
    for mode in [constants.IALLOCATOR_MODE_CHG_GROUP,
185 e969a81f Thomas Thrainer
                 constants.IALLOCATOR_MODE_NODE_EVAC]:
186 e969a81f Thomas Thrainer
      op = self.CopyOpCode(self.base_op,
187 e969a81f Thomas Thrainer
                           mode=mode)
188 e969a81f Thomas Thrainer
189 e969a81f Thomas Thrainer
      self.ResetMocks()
190 e969a81f Thomas Thrainer
      self.ExecOpCodeExpectOpPrereqError(op, "Missing instances")
191 e969a81f Thomas Thrainer
192 e969a81f Thomas Thrainer
  def testAlloc(self):
193 e969a81f Thomas Thrainer
    op = self.valid_alloc_op
194 e969a81f Thomas Thrainer
195 e969a81f Thomas Thrainer
    self.ExecOpCode(op)
196 e969a81f Thomas Thrainer
197 e969a81f Thomas Thrainer
    assert self.iallocator_cls.call_count == 1
198 e969a81f Thomas Thrainer
    self.iallocator_cls.return_value.Run \
199 e969a81f Thomas Thrainer
      .assert_called_once_with("test", validate=False)
200 e969a81f Thomas Thrainer
201 e969a81f Thomas Thrainer
  def testReloc(self):
202 e969a81f Thomas Thrainer
    op = self.valid_reloc_op
203 e969a81f Thomas Thrainer
    self.cfg.AddNewInstance(name=op.name)
204 e969a81f Thomas Thrainer
205 e969a81f Thomas Thrainer
    self.ExecOpCode(op)
206 e969a81f Thomas Thrainer
207 e969a81f Thomas Thrainer
    assert self.iallocator_cls.call_count == 1
208 e969a81f Thomas Thrainer
    self.iallocator_cls.return_value.Run \
209 e969a81f Thomas Thrainer
      .assert_called_once_with("test", validate=False)
210 e969a81f Thomas Thrainer
211 e969a81f Thomas Thrainer
  def testChgGroup(self):
212 e969a81f Thomas Thrainer
    op = self.valid_chg_group_op
213 e969a81f Thomas Thrainer
    for inst_name in op.instances:
214 e969a81f Thomas Thrainer
      self.cfg.AddNewInstance(name=inst_name)
215 e969a81f Thomas Thrainer
216 e969a81f Thomas Thrainer
    self.ExecOpCode(op)
217 e969a81f Thomas Thrainer
218 e969a81f Thomas Thrainer
    assert self.iallocator_cls.call_count == 1
219 e969a81f Thomas Thrainer
    self.iallocator_cls.return_value.Run \
220 e969a81f Thomas Thrainer
      .assert_called_once_with("test", validate=False)
221 e969a81f Thomas Thrainer
222 e969a81f Thomas Thrainer
  def testNodeEvac(self):
223 e969a81f Thomas Thrainer
    op = self.valid_node_evac_op
224 e969a81f Thomas Thrainer
    for inst_name in op.instances:
225 e969a81f Thomas Thrainer
      self.cfg.AddNewInstance(name=inst_name)
226 e969a81f Thomas Thrainer
227 e969a81f Thomas Thrainer
    self.ExecOpCode(op)
228 e969a81f Thomas Thrainer
229 e969a81f Thomas Thrainer
    assert self.iallocator_cls.call_count == 1
230 e969a81f Thomas Thrainer
    self.iallocator_cls.return_value.Run \
231 e969a81f Thomas Thrainer
      .assert_called_once_with("test", validate=False)
232 e969a81f Thomas Thrainer
233 e969a81f Thomas Thrainer
  def testMultiAlloc(self):
234 e969a81f Thomas Thrainer
    op = self.valid_multi_alloc_op
235 e969a81f Thomas Thrainer
236 e969a81f Thomas Thrainer
    self.ExecOpCode(op)
237 e969a81f Thomas Thrainer
238 e969a81f Thomas Thrainer
    assert self.iallocator_cls.call_count == 1
239 e969a81f Thomas Thrainer
    self.iallocator_cls.return_value.Run \
240 e969a81f Thomas Thrainer
      .assert_called_once_with("test", validate=False)
241 e969a81f Thomas Thrainer
242 e969a81f Thomas Thrainer
  def testAllocDirectionIn(self):
243 e969a81f Thomas Thrainer
    op = self.CopyOpCode(self.valid_alloc_op,
244 e969a81f Thomas Thrainer
                         direction=constants.IALLOCATOR_DIR_IN)
245 e969a81f Thomas Thrainer
246 e969a81f Thomas Thrainer
    self.ExecOpCode(op)
247 e969a81f Thomas Thrainer
248 e969a81f Thomas Thrainer
249 3efa7659 Thomas Thrainer
if __name__ == "__main__":
250 3efa7659 Thomas Thrainer
  testutils.GanetiTestProgram()