4 # Copyright (C) 2007, 2011, 2012 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 """Instance related QA tests.
29 from ganeti import utils
30 from ganeti import constants
31 from ganeti import query
37 from qa_utils import AssertIn, AssertCommand, AssertEqual
40 def _GetDiskStatePath(disk):
41 return "/sys/block/%s/device/state" % disk
44 def _GetGenericAddParameters():
46 params.append("%s=%s,%s=%s" % (constants.BE_MINMEM,
47 qa_config.get(constants.BE_MINMEM),
49 qa_config.get(constants.BE_MAXMEM)))
50 for idx, size in enumerate(qa_config.get("disk")):
51 params.extend(["--disk", "%s:size=%s" % (idx, size)])
55 def _DiskTest(node, disk_template):
56 instance = qa_config.AcquireInstance()
58 cmd = (["gnt-instance", "add",
59 "--os-type=%s" % qa_config.get("os"),
60 "--disk-template=%s" % disk_template,
62 _GetGenericAddParameters())
63 cmd.append(instance["name"])
67 _CheckSsconfInstanceList(instance["name"])
71 qa_config.ReleaseInstance(instance)
75 def TestInstanceAddWithPlainDisk(node):
76 """gnt-instance add -t plain"""
77 return _DiskTest(node["primary"], "plain")
80 def TestInstanceAddWithDrbdDisk(node, node2):
81 """gnt-instance add -t drbd"""
82 return _DiskTest("%s:%s" % (node["primary"], node2["primary"]),
86 def TestInstanceRemove(instance):
87 """gnt-instance remove"""
88 AssertCommand(["gnt-instance", "remove", "-f", instance["name"]])
90 qa_config.ReleaseInstance(instance)
93 def TestInstanceStartup(instance):
94 """gnt-instance startup"""
95 AssertCommand(["gnt-instance", "startup", instance["name"]])
98 def TestInstanceShutdown(instance):
99 """gnt-instance shutdown"""
100 AssertCommand(["gnt-instance", "shutdown", instance["name"]])
103 def TestInstanceReboot(instance):
104 """gnt-instance reboot"""
105 options = qa_config.get("options", {})
106 reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
107 name = instance["name"]
108 for rtype in reboot_types:
109 AssertCommand(["gnt-instance", "reboot", "--type=%s" % rtype, name])
111 AssertCommand(["gnt-instance", "shutdown", name])
112 AssertCommand(["gnt-instance", "reboot", name])
114 master = qa_config.GetMasterNode()
115 cmd = ["gnt-instance", "list", "--no-headers", "-o", "status", name]
116 result_output = qa_utils.GetCommandOutput(master["primary"],
117 utils.ShellQuoteArgs(cmd))
118 AssertEqual(result_output.strip(), constants.INSTST_RUNNING)
121 def TestInstanceReinstall(instance):
122 """gnt-instance reinstall"""
123 AssertCommand(["gnt-instance", "reinstall", "-f", instance["name"]])
126 def _ReadSsconfInstanceList():
127 """Reads ssconf_instance_list from the master node.
130 master = qa_config.GetMasterNode()
132 cmd = ["cat", utils.PathJoin(constants.DATA_DIR,
133 "ssconf_%s" % constants.SS_INSTANCE_LIST)]
135 return qa_utils.GetCommandOutput(master["primary"],
136 utils.ShellQuoteArgs(cmd)).splitlines()
139 def _CheckSsconfInstanceList(instance):
140 """Checks if a certain instance is in the ssconf instance list.
142 @type instance: string
143 @param instance: Instance name
146 AssertIn(qa_utils.ResolveInstanceName(instance),
147 _ReadSsconfInstanceList())
150 def TestInstanceRenameAndBack(rename_source, rename_target):
151 """gnt-instance rename
153 This must leave the instance with the original name, not the target
157 _CheckSsconfInstanceList(rename_source)
158 # first do a rename to a different actual name, expecting it to fail
159 qa_utils.AddToEtcHosts(["meeeeh-not-exists", rename_target])
161 AssertCommand(["gnt-instance", "rename", rename_source, rename_target],
163 _CheckSsconfInstanceList(rename_source)
165 qa_utils.RemoveFromEtcHosts(["meeeeh-not-exists", rename_target])
166 # and now rename instance to rename_target...
167 AssertCommand(["gnt-instance", "rename", rename_source, rename_target])
168 _CheckSsconfInstanceList(rename_target)
170 AssertCommand(["gnt-instance", "rename", rename_target, rename_source])
171 _CheckSsconfInstanceList(rename_source)
174 def TestInstanceFailover(instance):
175 """gnt-instance failover"""
176 cmd = ["gnt-instance", "failover", "--force", instance["name"]]
183 def TestInstanceMigrate(instance):
184 """gnt-instance migrate"""
185 cmd = ["gnt-instance", "migrate", "--force", instance["name"]]
190 AssertCommand(["gnt-instance", "shutdown", instance["name"]])
191 AssertCommand(cmd, fail=True)
192 AssertCommand(["gnt-instance", "migrate", "--force", "--allow-failover",
194 AssertCommand(["gnt-instance", "start", instance["name"]])
196 AssertCommand(["gnt-instance", "modify", "-B",
198 (constants.BE_ALWAYS_FAILOVER, constants.VALUE_TRUE)),
200 AssertCommand(cmd, fail=True)
201 AssertCommand(["gnt-instance", "migrate", "--force", "--allow-failover",
203 AssertCommand(["gnt-instance", "modify", "-B",
205 (constants.BE_ALWAYS_FAILOVER, constants.VALUE_FALSE)),
210 def TestInstanceInfo(instance):
211 """gnt-instance info"""
212 AssertCommand(["gnt-instance", "info", instance["name"]])
215 def TestInstanceModify(instance):
216 """gnt-instance modify"""
217 # Assume /sbin/init exists on all systems
218 test_kernel = "/sbin/init"
219 test_initrd = test_kernel
221 orig_maxmem = qa_config.get(constants.BE_MAXMEM)
222 orig_minmem = qa_config.get(constants.BE_MINMEM)
223 #orig_bridge = qa_config.get("bridge", "xen-br0")
225 ["-B", "%s=128" % constants.BE_MINMEM],
226 ["-B", "%s=128" % constants.BE_MAXMEM],
227 ["-B", "%s=%s,%s=%s" % (constants.BE_MINMEM, orig_minmem,
228 constants.BE_MAXMEM, orig_maxmem)],
229 ["-B", "%s=2" % constants.BE_VCPUS],
230 ["-B", "%s=1" % constants.BE_VCPUS],
231 ["-B", "%s=%s" % (constants.BE_VCPUS, constants.VALUE_DEFAULT)],
232 ["-B", "%s=%s" % (constants.BE_ALWAYS_FAILOVER, constants.VALUE_TRUE)],
233 ["-B", "%s=%s" % (constants.BE_ALWAYS_FAILOVER, constants.VALUE_DEFAULT)],
235 ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, test_kernel)],
236 ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, constants.VALUE_DEFAULT)],
237 ["-H", "%s=%s" % (constants.HV_INITRD_PATH, test_initrd)],
238 ["-H", "no_%s" % (constants.HV_INITRD_PATH, )],
239 ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_DEFAULT)],
242 #["--bridge", "xen-br1"],
243 #["--bridge", orig_bridge],
245 # TODO: Do these tests only with xen-hvm
246 #["-H", "%s=acn" % constants.HV_BOOT_ORDER],
247 #["-H", "%s=%s" % (constants.HV_BOOT_ORDER, constants.VALUE_DEFAULT)],
250 AssertCommand(["gnt-instance", "modify"] + alist + [instance["name"]])
253 AssertCommand(["gnt-instance", "modify", instance["name"]], fail=True)
255 # Marking offline/online while instance is running must fail
256 for arg in ["--online", "--offline"]:
257 AssertCommand(["gnt-instance", "modify", arg, instance["name"]], fail=True)
260 def TestInstanceStoppedModify(instance):
261 """gnt-instance modify (stopped instance)"""
262 name = instance["name"]
264 # Instance was not marked offline; try marking it online once more
265 AssertCommand(["gnt-instance", "modify", "--online", name])
267 # Mark instance as offline
268 AssertCommand(["gnt-instance", "modify", "--offline", name])
271 AssertCommand(["gnt-instance", "modify", "--online", name])
274 def TestInstanceConvertDisk(instance, snode):
275 """gnt-instance modify -t"""
276 name = instance["name"]
277 AssertCommand(["gnt-instance", "modify", "-t", "plain", name])
278 AssertCommand(["gnt-instance", "modify", "-t", "drbd",
279 "-n", snode["primary"], name])
282 def TestInstanceGrowDisk(instance):
283 """gnt-instance grow-disk"""
284 name = instance["name"]
285 all_size = qa_config.get("disk")
286 all_grow = qa_config.get("disk-growth")
288 # missing disk sizes but instance grow disk has been enabled,
289 # let's set fixed/nomimal growth
290 all_grow = ["128M" for _ in all_size]
291 for idx, (size, grow) in enumerate(zip(all_size, all_grow)):
292 # succeed in grow by amount
293 AssertCommand(["gnt-instance", "grow-disk", name, str(idx), grow])
294 # fail in grow to the old size
295 AssertCommand(["gnt-instance", "grow-disk", "--absolute", name, str(idx),
297 # succeed to grow to old size + 2 * growth
298 int_size = utils.ParseUnit(size)
299 int_grow = utils.ParseUnit(grow)
300 AssertCommand(["gnt-instance", "grow-disk", "--absolute", name, str(idx),
301 str(int_size + 2 * int_grow)])
304 def TestInstanceList():
305 """gnt-instance list"""
306 qa_utils.GenericQueryTest("gnt-instance", query.INSTANCE_FIELDS.keys())
309 def TestInstanceListFields():
310 """gnt-instance list-fields"""
311 qa_utils.GenericQueryFieldsTest("gnt-instance", query.INSTANCE_FIELDS.keys())
314 def TestInstanceConsole(instance):
315 """gnt-instance console"""
316 AssertCommand(["gnt-instance", "console", "--show-cmd", instance["name"]])
319 def TestReplaceDisks(instance, pnode, snode, othernode):
320 """gnt-instance replace-disks"""
321 # pylint: disable=W0613
322 # due to unused pnode arg
323 # FIXME: should be removed from the function completely
325 cmd = ["gnt-instance", "replace-disks"]
327 cmd.append(instance["name"])
333 ["--new-secondary=%s" % othernode["primary"]],
335 ["--new-secondary=%s" % snode["primary"]],
337 AssertCommand(buildcmd(data))
339 AssertCommand(buildcmd(["-a"]))
340 AssertCommand(["gnt-instance", "stop", instance["name"]])
341 AssertCommand(buildcmd(["-a"]), fail=True)
342 AssertCommand(["gnt-instance", "activate-disks", instance["name"]])
343 AssertCommand(buildcmd(["-a"]))
344 AssertCommand(["gnt-instance", "start", instance["name"]])
347 def TestInstanceExport(instance, node):
348 """gnt-backup export -n ..."""
349 name = instance["name"]
350 AssertCommand(["gnt-backup", "export", "-n", node["primary"], name])
351 return qa_utils.ResolveInstanceName(name)
354 def TestInstanceExportWithRemove(instance, node):
355 """gnt-backup export --remove-instance"""
356 AssertCommand(["gnt-backup", "export", "-n", node["primary"],
357 "--remove-instance", instance["name"]])
360 def TestInstanceExportNoTarget(instance):
361 """gnt-backup export (without target node, should fail)"""
362 AssertCommand(["gnt-backup", "export", instance["name"]], fail=True)
365 def TestInstanceImport(node, newinst, expnode, name):
366 """gnt-backup import"""
367 cmd = (["gnt-backup", "import",
368 "--disk-template=plain",
370 "--net", "0:mac=generate",
371 "--src-node=%s" % expnode["primary"],
372 "--src-dir=%s/%s" % (constants.EXPORT_DIR, name),
373 "--node=%s" % node["primary"]] +
374 _GetGenericAddParameters())
375 cmd.append(newinst["name"])
379 def TestBackupList(expnode):
380 """gnt-backup list"""
381 AssertCommand(["gnt-backup", "list", "--node=%s" % expnode["primary"]])
383 qa_utils.GenericQueryTest("gnt-backup", query.EXPORT_FIELDS.keys(),
384 namefield=None, test_unknown=False)
387 def TestBackupListFields():
388 """gnt-backup list-fields"""
389 qa_utils.GenericQueryFieldsTest("gnt-backup", query.EXPORT_FIELDS.keys())
392 def _TestInstanceDiskFailure(instance, node, node2, onmaster):
393 """Testing disk failure."""
394 master = qa_config.GetMasterNode()
395 sq = utils.ShellQuoteArgs
397 instance_full = qa_utils.ResolveInstanceName(instance["name"])
398 node_full = qa_utils.ResolveNodeName(node)
399 node2_full = qa_utils.ResolveNodeName(node2)
401 print qa_utils.FormatInfo("Getting physical disk names")
402 cmd = ["gnt-node", "volumes", "--separator=|", "--no-headers",
403 "--output=node,phys,instance",
404 node["primary"], node2["primary"]]
405 output = qa_utils.GetCommandOutput(master["primary"], sq(cmd))
407 # Get physical disk names
408 re_disk = re.compile(r"^/dev/([a-z]+)\d+$")
410 for line in output.splitlines():
411 (node_name, phys, inst) = line.split("|")
412 if inst == instance_full:
413 if node_name not in node2disk:
414 node2disk[node_name] = []
416 m = re_disk.match(phys)
418 raise qa_error.Error("Unknown disk name format: %s" % phys)
421 if name not in node2disk[node_name]:
422 node2disk[node_name].append(name)
424 if [node2_full, node_full][int(onmaster)] not in node2disk:
425 raise qa_error.Error("Couldn't find physical disks used on"
426 " %s node" % ["secondary", "master"][int(onmaster)])
428 print qa_utils.FormatInfo("Checking whether nodes have ability to stop"
430 for node_name, disks in node2disk.iteritems():
433 cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
434 AssertCommand(" && ".join(cmds), node=node_name)
436 print qa_utils.FormatInfo("Getting device paths")
437 cmd = ["gnt-instance", "activate-disks", instance["name"]]
438 output = qa_utils.GetCommandOutput(master["primary"], sq(cmd))
440 for line in output.splitlines():
441 (_, _, tmpdevpath) = line.split(":")
442 devpath.append(tmpdevpath)
445 print qa_utils.FormatInfo("Getting drbd device paths")
446 cmd = ["gnt-instance", "info", instance["name"]]
447 output = qa_utils.GetCommandOutput(master["primary"], sq(cmd))
448 pattern = (r"\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$"
449 r"\s+primary:\s+(/dev/drbd\d+)\s+")
450 drbddevs = re.findall(pattern, output, re.M)
455 print qa_utils.FormatInfo("Deactivating disks")
457 for name in node2disk[[node2_full, node_full][int(onmaster)]]:
458 halted_disks.append(name)
459 cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
460 AssertCommand(" && ".join(cmds), node=[node2, node][int(onmaster)])
462 print qa_utils.FormatInfo("Write to disks and give some time to notice"
463 " to notice the problem")
466 cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
467 "if=%s" % disk, "of=%s" % disk]))
469 AssertCommand(" && ".join(cmds), node=node)
472 print qa_utils.FormatInfo("Debugging info")
473 for name in drbddevs:
474 AssertCommand(["drbdsetup", name, "show"], node=node)
476 AssertCommand(["gnt-instance", "info", instance["name"]])
479 print qa_utils.FormatInfo("Activating disks again")
481 for name in halted_disks:
482 cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
483 AssertCommand("; ".join(cmds), node=[node2, node][int(onmaster)])
486 for name in drbddevs:
487 AssertCommand(["drbdsetup", name, "detach"], node=node)
489 for name in drbddevs:
490 AssertCommand(["drbdsetup", name, "disconnect"], node=node2)
493 #AssertCommand(["vgs"], [node2, node][int(onmaster)])
495 print qa_utils.FormatInfo("Making sure disks are up again")
496 AssertCommand(["gnt-instance", "replace-disks", instance["name"]])
498 print qa_utils.FormatInfo("Restarting instance")
499 AssertCommand(["gnt-instance", "shutdown", instance["name"]])
500 AssertCommand(["gnt-instance", "startup", instance["name"]])
502 AssertCommand(["gnt-cluster", "verify"])
505 def TestInstanceMasterDiskFailure(instance, node, node2):
506 """Testing disk failure on master node."""
507 # pylint: disable=W0613
509 print qa_utils.FormatError("Disk failure on primary node cannot be"
510 " tested due to potential crashes.")
511 # The following can cause crashes, thus it's disabled until fixed
512 #return _TestInstanceDiskFailure(instance, node, node2, True)
515 def TestInstanceSecondaryDiskFailure(instance, node, node2):
516 """Testing disk failure on secondary node."""
517 return _TestInstanceDiskFailure(instance, node, node2, False)