4 # Copyright (C) 2007, 2011, 2012, 2013 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 """Node-related QA tests.
26 from ganeti import utils
27 from ganeti import constants
28 from ganeti import query
29 from ganeti import serializer
35 from qa_utils import AssertCommand, AssertEqual
38 def _NodeAdd(node, readd=False):
39 if not readd and node.added:
40 raise qa_error.Error("Node %s already in cluster" % node.primary)
41 elif readd and not node.added:
42 raise qa_error.Error("Node %s not yet in cluster" % node.primary)
44 cmd = ["gnt-node", "add", "--no-ssh-key-check"]
46 cmd.append("--secondary-ip=%s" % node.secondary)
49 cmd.append(node.primary)
59 def _NodeRemove(node):
60 AssertCommand(["gnt-node", "remove", node.primary])
64 def MakeNodeOffline(node, value):
65 """gnt-node modify --offline=value"""
66 # value in ["yes", "no"]
67 AssertCommand(["gnt-node", "modify", "--offline", value, node.primary])
71 """Adding all nodes to cluster."""
72 master = qa_config.GetMasterNode()
73 for node in qa_config.get("nodes"):
75 _NodeAdd(node, readd=False)
78 def MarkNodeAddedAll():
79 """Mark all nodes as added.
81 This is useful if we don't create the cluster ourselves (in qa).
84 master = qa_config.GetMasterNode()
85 for node in qa_config.get("nodes"):
90 def TestNodeRemoveAll():
91 """Removing all nodes from cluster."""
92 master = qa_config.GetMasterNode()
93 for node in qa_config.get("nodes"):
98 def TestNodeReadd(node):
99 """gnt-node add --readd"""
100 _NodeAdd(node, readd=True)
105 AssertCommand(["gnt-node", "info"])
108 def TestNodeVolumes():
109 """gnt-node volumes"""
110 AssertCommand(["gnt-node", "volumes"])
113 def TestNodeStorage():
114 """gnt-node storage"""
115 master = qa_config.GetMasterNode()
117 # FIXME: test all storage_types in constants.VALID_STORAGE_TYPES
118 # as soon as they are implemented.
119 for storage_type in [constants.ST_FILE, constants.ST_LVM_VG,
120 constants.ST_LVM_PV]:
122 cmd = ["gnt-node", "list-storage", "--storage-type", storage_type]
124 # Skip file storage if not enabled, otherwise QA will fail; we
125 # just test for basic failure, but otherwise skip the rest of the
127 if storage_type == constants.ST_FILE and not constants.ENABLE_FILE_STORAGE:
128 AssertCommand(cmd, fail=True)
134 # Test all storage fields
135 cmd = ["gnt-node", "list-storage", "--storage-type", storage_type,
136 "--output=%s" % ",".join(list(constants.VALID_STORAGE_FIELDS) +
137 [constants.SF_NODE, constants.SF_TYPE])]
140 # Get list of valid storage devices
141 cmd = ["gnt-node", "list-storage", "--storage-type", storage_type,
142 "--output=node,name,allocatable", "--separator=|",
144 output = qa_utils.GetCommandOutput(master.primary,
145 utils.ShellQuoteArgs(cmd))
147 # Test with up to two devices
150 for line in output.splitlines()[:testdevcount]:
151 (node_name, st_name, st_allocatable) = line.split("|")
153 # Dummy modification without any changes
154 cmd = ["gnt-node", "modify-storage", node_name, storage_type, st_name]
157 # Make sure we end up with the same value as before
158 if st_allocatable.lower() == "y":
159 test_allocatable = ["no", "yes"]
161 test_allocatable = ["yes", "no"]
163 fail = (constants.SF_ALLOCATABLE not in
164 constants.MODIFIABLE_STORAGE_FIELDS.get(storage_type, []))
166 for i in test_allocatable:
167 AssertCommand(["gnt-node", "modify-storage", "--allocatable", i,
168 node_name, storage_type, st_name], fail=fail)
171 cmd = ["gnt-node", "list-storage", "--storage-type", storage_type,
172 "--output=name,allocatable", "--separator=|",
173 "--no-headers", node_name]
174 listout = qa_utils.GetCommandOutput(master.primary,
175 utils.ShellQuoteArgs(cmd))
176 for line in listout.splitlines():
177 (vfy_name, vfy_allocatable) = line.split("|")
178 if vfy_name == st_name and not fail:
179 AssertEqual(vfy_allocatable, i[0].upper())
181 AssertEqual(vfy_allocatable, st_allocatable)
183 # Test repair functionality
184 fail = (constants.SO_FIX_CONSISTENCY not in
185 constants.VALID_STORAGE_OPERATIONS.get(storage_type, []))
186 AssertCommand(["gnt-node", "repair-storage", node_name,
187 storage_type, st_name], fail=fail)
190 def TestNodeFailover(node, node2):
191 """gnt-node failover"""
192 if qa_utils.GetNodeInstances(node2, secondaries=False):
193 raise qa_error.UnusableNodeError("Secondary node has at least one"
194 " primary instance. This test requires"
195 " it to have no primary instances.")
197 # Fail over to secondary node
198 AssertCommand(["gnt-node", "failover", "-f", node.primary])
200 # ... and back again.
201 AssertCommand(["gnt-node", "failover", "-f", node2.primary])
204 def TestNodeMigrate(node, node2):
205 """gnt-node migrate"""
206 if qa_utils.GetNodeInstances(node2, secondaries=False):
207 raise qa_error.UnusableNodeError("Secondary node has at least one"
208 " primary instance. This test requires"
209 " it to have no primary instances.")
211 # Migrate to secondary node
212 AssertCommand(["gnt-node", "migrate", "-f", node.primary])
214 # ... and back again.
215 AssertCommand(["gnt-node", "migrate", "-f", node2.primary])
218 def TestNodeEvacuate(node, node2):
219 """gnt-node evacuate"""
220 node3 = qa_config.AcquireNode(exclude=[node, node2])
222 if qa_utils.GetNodeInstances(node3, secondaries=True):
223 raise qa_error.UnusableNodeError("Evacuation node has at least one"
224 " secondary instance. This test requires"
225 " it to have no secondary instances.")
227 # Evacuate all secondary instances
228 AssertCommand(["gnt-node", "evacuate", "-f",
229 "--new-secondary=%s" % node3.primary, node2.primary])
231 # ... and back again.
232 AssertCommand(["gnt-node", "evacuate", "-f",
233 "--new-secondary=%s" % node2.primary, node3.primary])
238 def TestNodeModify(node):
239 """gnt-node modify"""
241 # make sure enough master candidates will be available by disabling the
242 # master candidate role first with --auto-promote
243 AssertCommand(["gnt-node", "modify", "--master-candidate=no",
244 "--auto-promote", node.primary])
246 # now it's save to force-remove the master candidate role
247 for flag in ["master-candidate", "drained", "offline"]:
248 for value in ["yes", "no"]:
249 AssertCommand(["gnt-node", "modify", "--force",
250 "--%s=%s" % (flag, value), node.primary])
252 AssertCommand(["gnt-node", "modify", "--master-candidate=yes", node.primary])
254 # Test setting secondary IP address
255 AssertCommand(["gnt-node", "modify", "--secondary-ip=%s" % node.secondary,
259 def _CreateOobScriptStructure():
260 """Create a simple OOB handling script and its structure."""
261 master = qa_config.GetMasterNode()
263 data_path = qa_utils.UploadData(master.primary, "")
264 verify_path = qa_utils.UploadData(master.primary, "")
265 exit_code_path = qa_utils.UploadData(master.primary, "")
267 oob_script = (("#!/bin/bash\n"
271 (utils.ShellQuote(verify_path), utils.ShellQuote(data_path),
272 utils.ShellQuote(exit_code_path)))
273 oob_path = qa_utils.UploadData(master.primary, oob_script, mode=0700)
275 return [oob_path, verify_path, data_path, exit_code_path]
278 def _UpdateOobFile(path, data):
279 """Updates the data file with data."""
280 master = qa_config.GetMasterNode()
281 qa_utils.UploadData(master.primary, data, filename=path)
284 def _AssertOobCall(verify_path, expected_args):
285 """Assert the OOB call was performed with expetected args."""
286 master = qa_config.GetMasterNode()
288 verify_output_cmd = utils.ShellQuoteArgs(["cat", verify_path])
289 output = qa_utils.GetCommandOutput(master.primary, verify_output_cmd,
292 AssertEqual(expected_args, output.strip())
297 master = qa_config.GetMasterNode()
299 node = qa_config.AcquireNode(exclude=master)
301 master_name = master.primary
302 node_name = node.primary
303 full_node_name = qa_utils.ResolveNodeName(node)
305 (oob_path, verify_path,
306 data_path, exit_code_path) = _CreateOobScriptStructure()
309 AssertCommand(["gnt-cluster", "modify", "--node-parameters",
310 "oob_program=%s" % oob_path])
313 _UpdateOobFile(exit_code_path, "0")
315 AssertCommand(["gnt-node", "power", "on", node_name])
316 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
318 AssertCommand(["gnt-node", "power", "-f", "off", node_name])
319 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
321 # Power off on master without options should fail
322 AssertCommand(["gnt-node", "power", "-f", "off", master_name], fail=True)
323 # With force master it should still fail
324 AssertCommand(["gnt-node", "power", "-f", "--ignore-status", "off",
328 # Verify we can't transform back to online when not yet powered on
329 AssertCommand(["gnt-node", "modify", "-O", "no", node_name],
332 AssertCommand(["gnt-node", "modify", "-O", "no", "--node-powered", "yes",
335 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name])
336 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
338 # Those commands should fail as they expect output which isn't provided yet
339 # But they should have called the oob helper nevermind
340 AssertCommand(["gnt-node", "power", "status", node_name],
342 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
344 AssertCommand(["gnt-node", "health", node_name],
346 _AssertOobCall(verify_path, "health %s" % full_node_name)
348 AssertCommand(["gnt-node", "health"], fail=True)
350 # Correct Data, exit 0
351 _UpdateOobFile(data_path, serializer.DumpJson({"powered": True}))
353 AssertCommand(["gnt-node", "power", "status", node_name])
354 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
356 _UpdateOobFile(data_path, serializer.DumpJson([["temp", "OK"],
357 ["disk0", "CRITICAL"]]))
359 AssertCommand(["gnt-node", "health", node_name])
360 _AssertOobCall(verify_path, "health %s" % full_node_name)
362 AssertCommand(["gnt-node", "health"])
364 # Those commands should fail as they expect no data regardless of exit 0
365 AssertCommand(["gnt-node", "power", "on", node_name], fail=True)
366 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
369 AssertCommand(["gnt-node", "power", "-f", "off", node_name], fail=True)
370 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
372 AssertCommand(["gnt-node", "modify", "-O", "no", node_name])
374 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name], fail=True)
375 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
377 # Data, exit 1 (all should fail)
378 _UpdateOobFile(exit_code_path, "1")
380 AssertCommand(["gnt-node", "power", "on", node_name], fail=True)
381 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
384 AssertCommand(["gnt-node", "power", "-f", "off", node_name], fail=True)
385 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
387 AssertCommand(["gnt-node", "modify", "-O", "no", node_name])
389 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name], fail=True)
390 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
392 AssertCommand(["gnt-node", "power", "status", node_name],
394 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
396 AssertCommand(["gnt-node", "health", node_name],
398 _AssertOobCall(verify_path, "health %s" % full_node_name)
400 AssertCommand(["gnt-node", "health"], fail=True)
402 # No data, exit 1 (all should fail)
403 _UpdateOobFile(data_path, "")
404 AssertCommand(["gnt-node", "power", "on", node_name], fail=True)
405 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
408 AssertCommand(["gnt-node", "power", "-f", "off", node_name], fail=True)
409 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
411 AssertCommand(["gnt-node", "modify", "-O", "no", node_name])
413 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name], fail=True)
414 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
416 AssertCommand(["gnt-node", "power", "status", node_name],
418 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
420 AssertCommand(["gnt-node", "health", node_name],
422 _AssertOobCall(verify_path, "health %s" % full_node_name)
424 AssertCommand(["gnt-node", "health"], fail=True)
426 # Different OOB script for node
427 verify_path2 = qa_utils.UploadData(master.primary, "")
428 oob_script = ("#!/bin/sh\n"
429 "echo \"$@\" > %s\n") % verify_path2
430 oob_path2 = qa_utils.UploadData(master.primary, oob_script, mode=0700)
433 AssertCommand(["gnt-node", "modify", "--node-parameters",
434 "oob_program=%s" % oob_path2, node_name])
435 AssertCommand(["gnt-node", "power", "on", node_name])
436 _AssertOobCall(verify_path2, "power-on %s" % full_node_name)
438 AssertCommand(["gnt-node", "modify", "--node-parameters",
439 "oob_program=default", node_name])
440 AssertCommand(["rm", "-f", oob_path2, verify_path2])
442 AssertCommand(["gnt-cluster", "modify", "--node-parameters",
444 AssertCommand(["rm", "-f", oob_path, verify_path, data_path,
450 qa_utils.GenericQueryTest("gnt-node", query.NODE_FIELDS.keys())
453 def TestNodeListFields():
454 """gnt-node list-fields"""
455 qa_utils.GenericQueryFieldsTest("gnt-node", query.NODE_FIELDS.keys())
458 def TestNodeListDrbd(node):
459 """gnt-node list-drbd"""
460 AssertCommand(["gnt-node", "list-drbd", node.primary])
463 def _BuildSetESCmd(action, value, node_name):
466 cmd.extend(["add", "--readd"])
469 cmd.extend(["--node-parameters", "exclusive_storage=%s" % value, node_name])
473 def TestExclStorSingleNode(node):
474 """gnt-node add/modify cannot change the exclusive_storage flag.
477 for action in ["add", "modify"]:
478 for value in (True, False, "default"):
479 AssertCommand(_BuildSetESCmd(action, value, node.primary), fail=True)