4 # Copyright (C) 2007, 2008, 2009, 2010, 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 """Script for doing QA on Ganeti.
26 # pylint: disable=C0103
47 from ganeti import utils
48 from ganeti import rapi # pylint: disable=W0611
49 from ganeti import constants
51 import ganeti.rapi.client # pylint: disable=W0611
52 from ganeti.rapi.client import UsesRapiClient
55 def _FormatHeader(line, end=72):
56 """Fill a line up to the end column.
59 line = "---- " + line + " "
60 line += "-" * (end - len(line))
65 def _DescriptionOf(fn):
66 """Computes the description of an item.
70 desc = fn.__doc__.splitlines()[0].strip()
74 return desc.rstrip(".")
77 def RunTest(fn, *args, **kwargs):
78 """Runs a test after printing a header.
82 tstart = datetime.datetime.now()
84 desc = _DescriptionOf(fn)
87 print _FormatHeader("%s start %s" % (tstart, desc))
90 retval = fn(*args, **kwargs)
93 tstop = datetime.datetime.now()
94 tdelta = tstop - tstart
95 print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
98 def RunTestIf(testnames, fn, *args, **kwargs):
99 """Runs a test conditionally.
101 @param testnames: either a single test name in the configuration
102 file, or a list of testnames (which will be AND-ed together)
105 if qa_config.TestEnabled(testnames):
106 RunTest(fn, *args, **kwargs)
108 tstart = datetime.datetime.now()
109 desc = _DescriptionOf(fn)
110 print _FormatHeader("%s skipping %s, test(s) %s disabled" %
111 (tstart, desc, testnames))
115 """Run several environment tests.
118 RunTestIf("env", qa_env.TestSshConnection)
119 RunTestIf("env", qa_env.TestIcmpPing)
120 RunTestIf("env", qa_env.TestGanetiCommands)
123 def SetupCluster(rapi_user, rapi_secret):
124 """Initializes the cluster.
126 @param rapi_user: Login user for RAPI
127 @param rapi_secret: Login secret for RAPI
130 RunTestIf("create-cluster", qa_cluster.TestClusterInit,
131 rapi_user, rapi_secret)
133 # Test on empty cluster
134 RunTestIf("node-list", qa_node.TestNodeList)
135 RunTestIf("instance-list", qa_instance.TestInstanceList)
136 RunTestIf("job-list", qa_job.TestJobList)
138 RunTestIf("create-cluster", qa_node.TestNodeAddAll)
139 if not qa_config.TestEnabled("create-cluster"):
140 # consider the nodes are already there
141 qa_node.MarkNodeAddedAll()
143 RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
145 # enable the watcher (unconditionally)
146 RunTest(qa_daemon.TestResumeWatcher)
148 RunTestIf("node-list", qa_node.TestNodeList)
150 # Test listing fields
151 RunTestIf("node-list", qa_node.TestNodeListFields)
152 RunTestIf("instance-list", qa_instance.TestInstanceListFields)
153 RunTestIf("job-list", qa_job.TestJobListFields)
154 RunTestIf("instance-export", qa_instance.TestBackupListFields)
156 RunTestIf("node-info", qa_node.TestNodeInfo)
159 def RunClusterTests():
160 """Runs tests related to gnt-cluster.
164 ("create-cluster", qa_cluster.TestClusterInitDisk),
165 ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
166 ("cluster-verify", qa_cluster.TestClusterVerify),
167 ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
168 # TODO: add more cluster modify tests
169 ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
170 ("cluster-modify", qa_cluster.TestClusterModifyBe),
171 ("cluster-modify", qa_cluster.TestClusterModifyDisk),
172 ("cluster-rename", qa_cluster.TestClusterRename),
173 ("cluster-info", qa_cluster.TestClusterVersion),
174 ("cluster-info", qa_cluster.TestClusterInfo),
175 ("cluster-info", qa_cluster.TestClusterGetmaster),
176 ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
177 ("cluster-copyfile", qa_cluster.TestClusterCopyfile),
178 ("cluster-command", qa_cluster.TestClusterCommand),
179 ("cluster-burnin", qa_cluster.TestClusterBurnin),
180 ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
181 ("cluster-master-failover",
182 qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
183 ("cluster-oob", qa_cluster.TestClusterOob),
184 ("rapi", qa_rapi.TestVersion),
185 ("rapi", qa_rapi.TestEmptyCluster),
186 ("rapi", qa_rapi.TestRapiQuery),
191 def RunRepairDiskSizes():
192 """Run the repair disk-sizes test.
195 RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
199 """Runs all tests related to gnt-os.
202 if qa_config.TestEnabled("rapi"):
203 rapi_getos = qa_rapi.GetOperatingSystems
209 qa_os.TestOsDiagnose,
216 qa_os.TestOsPartiallyValid,
218 RunTestIf("os", fn, rapi_getos)
221 qa_os.TestOsModifyValid,
222 qa_os.TestOsModifyInvalid,
223 qa_os.TestOsStatesNonExisting,
228 def RunCommonInstanceTests(instance):
229 """Runs a few tests that are common to all disk types.
232 RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
233 RunTestIf(["instance-shutdown", "instance-console", "rapi"],
234 qa_rapi.TestRapiStoppedInstanceConsole, instance)
235 RunTestIf(["instance-shutdown", "instance-modify"],
236 qa_instance.TestInstanceStoppedModify, instance)
237 RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
239 # Test shutdown/start via RAPI
240 RunTestIf(["instance-shutdown", "rapi"],
241 qa_rapi.TestRapiInstanceShutdown, instance)
242 RunTestIf(["instance-shutdown", "rapi"],
243 qa_rapi.TestRapiInstanceStartup, instance)
245 RunTestIf("instance-list", qa_instance.TestInstanceList)
247 RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
249 RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
250 RunTestIf(["instance-modify", "rapi"],
251 qa_rapi.TestRapiInstanceModify, instance)
253 RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
254 RunTestIf(["instance-console", "rapi"],
255 qa_rapi.TestRapiInstanceConsole, instance)
257 DOWN_TESTS = qa_config.Either([
258 "instance-reinstall",
260 "instance-grow-disk",
263 # shutdown instance for any 'down' tests
264 RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
266 # now run the 'down' state tests
267 RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
268 RunTestIf(["instance-reinstall", "rapi"],
269 qa_rapi.TestRapiInstanceReinstall, instance)
271 if qa_config.TestEnabled("instance-rename"):
272 rename_source = instance["name"]
273 rename_target = qa_config.get("rename", None)
274 # perform instance rename to the same name
275 RunTest(qa_instance.TestInstanceRenameAndBack,
276 rename_source, rename_source)
277 RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
278 rename_source, rename_source)
279 if rename_target is not None:
280 # perform instance rename to a different name, if we have one configured
281 RunTest(qa_instance.TestInstanceRenameAndBack,
282 rename_source, rename_target)
283 RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
284 rename_source, rename_target)
286 RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
288 # and now start the instance again
289 RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
291 RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
293 RunTestIf("tags", qa_tags.TestInstanceTags, instance)
295 RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
297 RunTestIf("rapi", qa_rapi.TestInstance, instance)
299 # Lists instances, too
300 RunTestIf("node-list", qa_node.TestNodeList)
302 # Some jobs have been run, let's test listing them
303 RunTestIf("job-list", qa_job.TestJobList)
306 def RunCommonNodeTests():
307 """Run a few common node tests.
310 RunTestIf("node-volumes", qa_node.TestNodeVolumes)
311 RunTestIf("node-storage", qa_node.TestNodeStorage)
312 RunTestIf("node-oob", qa_node.TestOutOfBand)
315 def RunGroupListTests():
316 """Run tests for listing node groups.
319 RunTestIf("group-list", qa_group.TestGroupList)
320 RunTestIf("group-list", qa_group.TestGroupListFields)
323 def RunGroupRwTests():
324 """Run tests for adding/removing/renaming groups.
327 RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
328 RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
329 RunTestIf("group-rwops", qa_group.TestGroupModify)
330 RunTestIf(["group-rwops", "rapi"], qa_rapi.TestRapiNodeGroups)
331 RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
332 qa_group.GetDefaultGroup())
335 def RunExportImportTests(instance, pnode, snode):
336 """Tries to export and import the instance.
338 @param pnode: current primary node of the instance
339 @param snode: current secondary node of the instance, if any,
343 if qa_config.TestEnabled("instance-export"):
344 RunTest(qa_instance.TestInstanceExportNoTarget, instance)
346 expnode = qa_config.AcquireNode(exclude=pnode)
348 name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
350 RunTest(qa_instance.TestBackupList, expnode)
352 if qa_config.TestEnabled("instance-import"):
353 newinst = qa_config.AcquireInstance()
355 RunTest(qa_instance.TestInstanceImport, newinst, pnode,
357 # Check if starting the instance works
358 RunTest(qa_instance.TestInstanceStartup, newinst)
359 RunTest(qa_instance.TestInstanceRemove, newinst)
361 qa_config.ReleaseInstance(newinst)
363 qa_config.ReleaseNode(expnode)
365 if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
366 newinst = qa_config.AcquireInstance()
371 excl = [pnode, snode]
372 tnode = qa_config.AcquireNode(exclude=excl)
374 RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
377 qa_config.ReleaseNode(tnode)
379 qa_config.ReleaseInstance(newinst)
382 def RunDaemonTests(instance):
383 """Test the ganeti-watcher script.
386 RunTest(qa_daemon.TestPauseWatcher)
388 RunTestIf("instance-automatic-restart",
389 qa_daemon.TestInstanceAutomaticRestart, instance)
390 RunTestIf("instance-consecutive-failures",
391 qa_daemon.TestInstanceConsecutiveFailures, instance)
393 RunTest(qa_daemon.TestResumeWatcher)
396 def RunSingleHomedHardwareFailureTests(instance, pnode):
397 """Test hardware failure recovery for single-homed instances.
400 if qa_config.TestEnabled("instance-recreate-disks"):
401 othernode = qa_config.AcquireNode(exclude=[pnode])
403 RunTest(qa_instance.TestRecreateDisks,
404 instance, pnode, None, [othernode])
406 qa_config.ReleaseNode(othernode)
409 def RunHardwareFailureTests(instance, pnode, snode):
410 """Test cluster internal hardware failure recovery.
413 RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
414 RunTestIf(["instance-failover", "rapi"],
415 qa_rapi.TestRapiInstanceFailover, instance)
417 RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
418 RunTestIf(["instance-migrate", "rapi"],
419 qa_rapi.TestRapiInstanceMigrate, instance)
421 if qa_config.TestEnabled("instance-replace-disks"):
422 othernode = qa_config.AcquireNode(exclude=[pnode, snode])
424 RunTestIf("rapi", qa_rapi.TestRapiInstanceReplaceDisks, instance)
425 RunTest(qa_instance.TestReplaceDisks,
426 instance, pnode, snode, othernode)
428 qa_config.ReleaseNode(othernode)
430 if qa_config.TestEnabled("instance-recreate-disks"):
431 othernode1 = qa_config.AcquireNode(exclude=[pnode, snode])
433 othernode2 = qa_config.AcquireNode(exclude=[pnode, snode, othernode1])
434 except qa_error.OutOfNodesError:
435 # Let's reuse one of the nodes if the cluster is not big enough
438 RunTest(qa_instance.TestRecreateDisks,
439 instance, pnode, snode, [othernode1, othernode2])
441 qa_config.ReleaseNode(othernode1)
442 if othernode2 != pnode:
443 qa_config.ReleaseNode(othernode2)
445 RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
447 RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
449 RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
450 instance, pnode, snode)
451 RunTestIf("instance-disk-failure",
452 qa_instance.TestInstanceSecondaryDiskFailure, instance,
460 rapi_user = "ganeti-qa"
461 rapi_secret = utils.GenerateSecret()
464 SetupCluster(rapi_user, rapi_secret)
466 # Load RAPI certificate
467 qa_rapi.Setup(rapi_user, rapi_secret)
472 RunTestIf("tags", qa_tags.TestClusterTags)
478 # The master shouldn't be readded or put offline; "delay" needs a non-master
480 pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
482 RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
483 RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
484 RunTestIf("delay", qa_cluster.TestDelay, pnode)
486 qa_config.ReleaseNode(pnode)
488 pnode = qa_config.AcquireNode()
490 RunTestIf("tags", qa_tags.TestNodeTags, pnode)
492 if qa_rapi.Enabled():
493 RunTest(qa_rapi.TestNode, pnode)
495 if qa_config.TestEnabled("instance-add-plain-disk"):
496 for use_client in [True, False]:
497 rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
499 if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
500 RunCommonInstanceTests(rapi_instance)
501 RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
504 if qa_config.TestEnabled("instance-add-plain-disk"):
505 instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
506 RunCommonInstanceTests(instance)
508 RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
509 RunExportImportTests(instance, pnode, None)
510 RunDaemonTests(instance)
512 RunSingleHomedHardwareFailureTests(instance, pnode)
513 RunTest(qa_instance.TestInstanceRemove, instance)
517 ("instance-add-drbd-disk",
518 qa_instance.TestInstanceAddWithDrbdDisk),
521 for name, func in multinode_tests:
522 if qa_config.TestEnabled(name):
523 snode = qa_config.AcquireNode(exclude=pnode)
525 instance = RunTest(func, pnode, snode)
526 RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, pnode)
527 RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, snode)
528 RunCommonInstanceTests(instance)
530 RunTestIf("group-rwops", qa_group.TestAssignNodesIncludingSplit,
531 constants.INITIAL_NODE_GROUP_NAME,
532 pnode["primary"], snode["primary"])
533 if qa_config.TestEnabled("instance-convert-disk"):
534 RunTest(qa_instance.TestInstanceShutdown, instance)
535 RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
536 RunTest(qa_instance.TestInstanceStartup, instance)
537 RunExportImportTests(instance, pnode, snode)
538 RunHardwareFailureTests(instance, pnode, snode)
540 RunTest(qa_instance.TestInstanceRemove, instance)
543 qa_config.ReleaseNode(snode)
546 qa_config.ReleaseNode(pnode)
548 # Test removing instance with offline drbd secondary
549 if qa_config.TestEnabled("instance-remove-drbd-offline"):
550 # Make sure the master is not put offline
551 snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
553 pnode = qa_config.AcquireNode(exclude=snode)
555 instance = qa_instance.TestInstanceAddWithDrbdDisk(pnode, snode)
556 qa_node.MakeNodeOffline(snode, "yes")
558 RunTest(qa_instance.TestInstanceRemove, instance)
560 qa_node.MakeNodeOffline(snode, "no")
562 qa_config.ReleaseNode(pnode)
564 qa_config.ReleaseNode(snode)
566 pnode = qa_config.AcquireNode()
568 if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
569 for shutdown in [False, True]:
570 instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
571 expnode = qa_config.AcquireNode(exclude=pnode)
574 # Stop instance before exporting and removing it
575 RunTest(qa_instance.TestInstanceShutdown, instance)
576 RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
577 RunTest(qa_instance.TestBackupList, expnode)
579 qa_config.ReleaseNode(expnode)
584 qa_config.ReleaseNode(pnode)
586 RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
588 RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
596 parser = optparse.OptionParser(usage="%prog [options] <config-file>")
597 parser.add_option("--yes-do-it", dest="yes_do_it",
599 help="Really execute the tests")
600 (qa_config.options, args) = parser.parse_args()
603 (config_file, ) = args
605 parser.error("Wrong number of arguments.")
607 if not qa_config.options.yes_do_it:
608 print ("Executing this script irreversibly destroys any Ganeti\n"
609 "configuration on all nodes involved. If you really want\n"
610 "to start testing, supply the --yes-do-it option.")
613 qa_config.Load(config_file)
615 primary = qa_config.GetMasterNode()["primary"]
616 qa_utils.StartMultiplexer(primary)
617 print ("SSH command for primary node: %s" %
618 utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
619 print ("SSH command for other nodes: %s" %
620 utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
624 qa_utils.CloseMultiplexers()
626 if __name__ == "__main__":