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 TestNodeEvacuate(node, node2):
205 """gnt-node evacuate"""
206 node3 = qa_config.AcquireNode(exclude=[node, node2])
208 if qa_utils.GetNodeInstances(node3, secondaries=True):
209 raise qa_error.UnusableNodeError("Evacuation node has at least one"
210 " secondary instance. This test requires"
211 " it to have no secondary instances.")
213 # Evacuate all secondary instances
214 AssertCommand(["gnt-node", "evacuate", "-f",
215 "--new-secondary=%s" % node3.primary, node2.primary])
217 # ... and back again.
218 AssertCommand(["gnt-node", "evacuate", "-f",
219 "--new-secondary=%s" % node2.primary, node3.primary])
224 def TestNodeModify(node):
225 """gnt-node modify"""
226 for flag in ["master-candidate", "drained", "offline"]:
227 for value in ["yes", "no"]:
228 AssertCommand(["gnt-node", "modify", "--force",
229 "--%s=%s" % (flag, value), node.primary])
231 AssertCommand(["gnt-node", "modify", "--master-candidate=yes",
232 "--auto-promote", node.primary])
234 # Test setting secondary IP address
235 AssertCommand(["gnt-node", "modify", "--secondary-ip=%s" % node.secondary,
239 def _CreateOobScriptStructure():
240 """Create a simple OOB handling script and its structure."""
241 master = qa_config.GetMasterNode()
243 data_path = qa_utils.UploadData(master.primary, "")
244 verify_path = qa_utils.UploadData(master.primary, "")
245 exit_code_path = qa_utils.UploadData(master.primary, "")
247 oob_script = (("#!/bin/bash\n"
251 (utils.ShellQuote(verify_path), utils.ShellQuote(data_path),
252 utils.ShellQuote(exit_code_path)))
253 oob_path = qa_utils.UploadData(master.primary, oob_script, mode=0700)
255 return [oob_path, verify_path, data_path, exit_code_path]
258 def _UpdateOobFile(path, data):
259 """Updates the data file with data."""
260 master = qa_config.GetMasterNode()
261 qa_utils.UploadData(master.primary, data, filename=path)
264 def _AssertOobCall(verify_path, expected_args):
265 """Assert the OOB call was performed with expetected args."""
266 master = qa_config.GetMasterNode()
268 verify_output_cmd = utils.ShellQuoteArgs(["cat", verify_path])
269 output = qa_utils.GetCommandOutput(master.primary, verify_output_cmd,
272 AssertEqual(expected_args, output.strip())
277 master = qa_config.GetMasterNode()
279 node = qa_config.AcquireNode(exclude=master)
281 master_name = master.primary
282 node_name = node.primary
283 full_node_name = qa_utils.ResolveNodeName(node)
285 (oob_path, verify_path,
286 data_path, exit_code_path) = _CreateOobScriptStructure()
289 AssertCommand(["gnt-cluster", "modify", "--node-parameters",
290 "oob_program=%s" % oob_path])
293 _UpdateOobFile(exit_code_path, "0")
295 AssertCommand(["gnt-node", "power", "on", node_name])
296 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
298 AssertCommand(["gnt-node", "power", "-f", "off", node_name])
299 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
301 # Power off on master without options should fail
302 AssertCommand(["gnt-node", "power", "-f", "off", master_name], fail=True)
303 # With force master it should still fail
304 AssertCommand(["gnt-node", "power", "-f", "--ignore-status", "off",
308 # Verify we can't transform back to online when not yet powered on
309 AssertCommand(["gnt-node", "modify", "-O", "no", node_name],
312 AssertCommand(["gnt-node", "modify", "-O", "no", "--node-powered", "yes",
315 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name])
316 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
318 # Those commands should fail as they expect output which isn't provided yet
319 # But they should have called the oob helper nevermind
320 AssertCommand(["gnt-node", "power", "status", node_name],
322 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
324 AssertCommand(["gnt-node", "health", node_name],
326 _AssertOobCall(verify_path, "health %s" % full_node_name)
328 AssertCommand(["gnt-node", "health"], fail=True)
330 # Correct Data, exit 0
331 _UpdateOobFile(data_path, serializer.DumpJson({"powered": True}))
333 AssertCommand(["gnt-node", "power", "status", node_name])
334 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
336 _UpdateOobFile(data_path, serializer.DumpJson([["temp", "OK"],
337 ["disk0", "CRITICAL"]]))
339 AssertCommand(["gnt-node", "health", node_name])
340 _AssertOobCall(verify_path, "health %s" % full_node_name)
342 AssertCommand(["gnt-node", "health"])
344 # Those commands should fail as they expect no data regardless of exit 0
345 AssertCommand(["gnt-node", "power", "on", node_name], fail=True)
346 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
349 AssertCommand(["gnt-node", "power", "-f", "off", node_name], fail=True)
350 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
352 AssertCommand(["gnt-node", "modify", "-O", "no", node_name])
354 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name], fail=True)
355 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
357 # Data, exit 1 (all should fail)
358 _UpdateOobFile(exit_code_path, "1")
360 AssertCommand(["gnt-node", "power", "on", node_name], fail=True)
361 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
364 AssertCommand(["gnt-node", "power", "-f", "off", node_name], fail=True)
365 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
367 AssertCommand(["gnt-node", "modify", "-O", "no", node_name])
369 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name], fail=True)
370 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
372 AssertCommand(["gnt-node", "power", "status", node_name],
374 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
376 AssertCommand(["gnt-node", "health", node_name],
378 _AssertOobCall(verify_path, "health %s" % full_node_name)
380 AssertCommand(["gnt-node", "health"], fail=True)
382 # No data, exit 1 (all should fail)
383 _UpdateOobFile(data_path, "")
384 AssertCommand(["gnt-node", "power", "on", node_name], fail=True)
385 _AssertOobCall(verify_path, "power-on %s" % full_node_name)
388 AssertCommand(["gnt-node", "power", "-f", "off", node_name], fail=True)
389 _AssertOobCall(verify_path, "power-off %s" % full_node_name)
391 AssertCommand(["gnt-node", "modify", "-O", "no", node_name])
393 AssertCommand(["gnt-node", "power", "-f", "cycle", node_name], fail=True)
394 _AssertOobCall(verify_path, "power-cycle %s" % full_node_name)
396 AssertCommand(["gnt-node", "power", "status", node_name],
398 _AssertOobCall(verify_path, "power-status %s" % full_node_name)
400 AssertCommand(["gnt-node", "health", node_name],
402 _AssertOobCall(verify_path, "health %s" % full_node_name)
404 AssertCommand(["gnt-node", "health"], fail=True)
406 # Different OOB script for node
407 verify_path2 = qa_utils.UploadData(master.primary, "")
408 oob_script = ("#!/bin/sh\n"
409 "echo \"$@\" > %s\n") % verify_path2
410 oob_path2 = qa_utils.UploadData(master.primary, oob_script, mode=0700)
413 AssertCommand(["gnt-node", "modify", "--node-parameters",
414 "oob_program=%s" % oob_path2, node_name])
415 AssertCommand(["gnt-node", "power", "on", node_name])
416 _AssertOobCall(verify_path2, "power-on %s" % full_node_name)
418 AssertCommand(["gnt-node", "modify", "--node-parameters",
419 "oob_program=default", node_name])
420 AssertCommand(["rm", "-f", oob_path2, verify_path2])
422 AssertCommand(["gnt-cluster", "modify", "--node-parameters",
424 AssertCommand(["rm", "-f", oob_path, verify_path, data_path,
430 qa_utils.GenericQueryTest("gnt-node", query.NODE_FIELDS.keys())
433 def TestNodeListFields():
434 """gnt-node list-fields"""
435 qa_utils.GenericQueryFieldsTest("gnt-node", query.NODE_FIELDS.keys())
438 def TestNodeListDrbd(node):
439 """gnt-node list-drbd"""
440 AssertCommand(["gnt-node", "list-drbd", node.primary])
443 def _BuildSetESCmd(action, value, node_name):
446 cmd.extend(["add", "--readd"])
449 cmd.extend(["--node-parameters", "exclusive_storage=%s" % value, node_name])
453 def TestExclStorSingleNode(node):
454 """gnt-node add/modify cannot change the exclusive_storage flag.
457 for action in ["add", "modify"]:
458 for value in (True, False, "default"):
459 AssertCommand(_BuildSetESCmd(action, value, node.primary), fail=True)