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
46 from ganeti import utils
47 from ganeti import rapi # pylint: disable=W0611
48 from ganeti import constants
50 import ganeti.rapi.client # pylint: disable=W0611
51 from ganeti.rapi.client import UsesRapiClient
54 def _FormatHeader(line, end=72):
55 """Fill a line up to the end column.
58 line = "---- " + line + " "
59 line += "-" * (end - len(line))
64 def _DescriptionOf(fn):
65 """Computes the description of an item.
69 desc = fn.__doc__.splitlines()[0].strip()
73 return desc.rstrip(".")
76 def RunTest(fn, *args, **kwargs):
77 """Runs a test after printing a header.
81 tstart = datetime.datetime.now()
83 desc = _DescriptionOf(fn)
86 print _FormatHeader("%s start %s" % (tstart, desc))
89 retval = fn(*args, **kwargs)
92 tstop = datetime.datetime.now()
93 tdelta = tstop - tstart
94 print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
97 def RunTestIf(testnames, fn, *args, **kwargs):
98 """Runs a test conditionally.
100 @param testnames: either a single test name in the configuration
101 file, or a list of testnames (which will be AND-ed together)
104 if qa_config.TestEnabled(testnames):
105 RunTest(fn, *args, **kwargs)
107 tstart = datetime.datetime.now()
108 desc = _DescriptionOf(fn)
109 print _FormatHeader("%s skipping %s, test(s) %s disabled" %
110 (tstart, desc, testnames))
114 """Run several environment tests.
117 RunTestIf("env", qa_env.TestSshConnection)
118 RunTestIf("env", qa_env.TestIcmpPing)
119 RunTestIf("env", qa_env.TestGanetiCommands)
122 def SetupCluster(rapi_user, rapi_secret):
123 """Initializes the cluster.
125 @param rapi_user: Login user for RAPI
126 @param rapi_secret: Login secret for RAPI
129 RunTestIf("create-cluster", qa_cluster.TestClusterInit,
130 rapi_user, rapi_secret)
132 # Test on empty cluster
133 RunTestIf("node-list", qa_node.TestNodeList)
134 RunTestIf("instance-list", qa_instance.TestInstanceList)
135 RunTestIf("job-list", qa_job.TestJobList)
137 RunTestIf("create-cluster", qa_node.TestNodeAddAll)
138 if not qa_config.TestEnabled("create-cluster"):
139 # consider the nodes are already there
140 qa_node.MarkNodeAddedAll()
142 RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
144 # enable the watcher (unconditionally)
145 RunTest(qa_daemon.TestResumeWatcher)
147 RunTestIf("node-list", qa_node.TestNodeList)
149 # Test listing fields
150 RunTestIf("node-list", qa_node.TestNodeListFields)
151 RunTestIf("instance-list", qa_instance.TestInstanceListFields)
152 RunTestIf("job-list", qa_job.TestJobListFields)
153 RunTestIf("instance-export", qa_instance.TestBackupListFields)
155 RunTestIf("node-info", qa_node.TestNodeInfo)
158 def RunClusterTests():
159 """Runs tests related to gnt-cluster.
163 ("create-cluster", qa_cluster.TestClusterInitDisk),
164 ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
165 ("cluster-verify", qa_cluster.TestClusterVerify),
166 ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
167 # TODO: add more cluster modify tests
168 ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
169 ("cluster-modify", qa_cluster.TestClusterModifyBe),
170 ("cluster-modify", qa_cluster.TestClusterModifyDisk),
171 ("cluster-rename", qa_cluster.TestClusterRename),
172 ("cluster-info", qa_cluster.TestClusterVersion),
173 ("cluster-info", qa_cluster.TestClusterInfo),
174 ("cluster-info", qa_cluster.TestClusterGetmaster),
175 ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
176 ("cluster-copyfile", qa_cluster.TestClusterCopyfile),
177 ("cluster-command", qa_cluster.TestClusterCommand),
178 ("cluster-burnin", qa_cluster.TestClusterBurnin),
179 ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
180 ("cluster-master-failover",
181 qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
182 ("cluster-oob", qa_cluster.TestClusterOob),
183 ("rapi", qa_rapi.TestVersion),
184 ("rapi", qa_rapi.TestEmptyCluster),
185 ("rapi", qa_rapi.TestRapiQuery),
190 def RunRepairDiskSizes():
191 """Run the repair disk-sizes test.
194 RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
198 """Runs all tests related to gnt-os.
201 if qa_config.TestEnabled("rapi"):
202 rapi_getos = qa_rapi.GetOperatingSystems
208 qa_os.TestOsDiagnose,
215 qa_os.TestOsPartiallyValid,
217 RunTestIf("os", fn, rapi_getos)
220 qa_os.TestOsModifyValid,
221 qa_os.TestOsModifyInvalid,
222 qa_os.TestOsStatesNonExisting,
227 def RunCommonInstanceTests(instance):
228 """Runs a few tests that are common to all disk types.
231 RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
232 RunTestIf(["instance-shutdown", "instance-console", "rapi"],
233 qa_rapi.TestRapiStoppedInstanceConsole, instance)
234 RunTestIf(["instance-shutdown", "instance-modify"],
235 qa_instance.TestInstanceStoppedModify, instance)
236 RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
238 # Test shutdown/start via RAPI
239 RunTestIf(["instance-shutdown", "rapi"],
240 qa_rapi.TestRapiInstanceShutdown, instance)
241 RunTestIf(["instance-shutdown", "rapi"],
242 qa_rapi.TestRapiInstanceStartup, instance)
244 RunTestIf("instance-list", qa_instance.TestInstanceList)
246 RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
248 RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
249 RunTestIf(["instance-modify", "rapi"],
250 qa_rapi.TestRapiInstanceModify, instance)
252 RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
253 RunTestIf(["instance-console", "rapi"],
254 qa_rapi.TestRapiInstanceConsole, instance)
256 DOWN_TESTS = qa_config.Either([
257 "instance-reinstall",
259 "instance-grow-disk",
262 # shutdown instance for any 'down' tests
263 RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
265 # now run the 'down' state tests
266 RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
267 RunTestIf(["instance-reinstall", "rapi"],
268 qa_rapi.TestRapiInstanceReinstall, instance)
270 if qa_config.TestEnabled("instance-rename"):
271 tgt_instance = qa_config.AcquireInstance()
273 rename_source = instance["name"]
274 rename_target = tgt_instance["name"]
275 # perform instance rename to the same name
276 RunTest(qa_instance.TestInstanceRenameAndBack,
277 rename_source, rename_source)
278 RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
279 rename_source, rename_source)
280 if rename_target is not None:
281 # perform instance rename to a different name, if we have one configured
282 RunTest(qa_instance.TestInstanceRenameAndBack,
283 rename_source, rename_target)
284 RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
285 rename_source, rename_target)
287 qa_config.ReleaseInstance(tgt_instance)
289 RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
291 # and now start the instance again
292 RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
294 RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
296 RunTestIf("tags", qa_tags.TestInstanceTags, instance)
298 RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
300 RunTestIf("rapi", qa_rapi.TestInstance, instance)
302 # Lists instances, too
303 RunTestIf("node-list", qa_node.TestNodeList)
305 # Some jobs have been run, let's test listing them
306 RunTestIf("job-list", qa_job.TestJobList)
309 def RunCommonNodeTests():
310 """Run a few common node tests.
313 RunTestIf("node-volumes", qa_node.TestNodeVolumes)
314 RunTestIf("node-storage", qa_node.TestNodeStorage)
315 RunTestIf("node-oob", qa_node.TestOutOfBand)
318 def RunGroupListTests():
319 """Run tests for listing node groups.
322 RunTestIf("group-list", qa_group.TestGroupList)
323 RunTestIf("group-list", qa_group.TestGroupListFields)
326 def RunGroupRwTests():
327 """Run tests for adding/removing/renaming groups.
330 RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
331 RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
332 RunTestIf("group-rwops", qa_group.TestGroupModify)
333 RunTestIf(["group-rwops", "rapi"], qa_rapi.TestRapiNodeGroups)
334 RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
335 qa_group.GetDefaultGroup())
338 def RunExportImportTests(instance, pnode, snode):
339 """Tries to export and import the instance.
341 @param pnode: current primary node of the instance
342 @param snode: current secondary node of the instance, if any,
346 if qa_config.TestEnabled("instance-export"):
347 RunTest(qa_instance.TestInstanceExportNoTarget, instance)
349 expnode = qa_config.AcquireNode(exclude=pnode)
351 name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
353 RunTest(qa_instance.TestBackupList, expnode)
355 if qa_config.TestEnabled("instance-import"):
356 newinst = qa_config.AcquireInstance()
358 RunTest(qa_instance.TestInstanceImport, newinst, pnode,
360 # Check if starting the instance works
361 RunTest(qa_instance.TestInstanceStartup, newinst)
362 RunTest(qa_instance.TestInstanceRemove, newinst)
364 qa_config.ReleaseInstance(newinst)
366 qa_config.ReleaseNode(expnode)
368 if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
369 newinst = qa_config.AcquireInstance()
374 excl = [pnode, snode]
375 tnode = qa_config.AcquireNode(exclude=excl)
377 RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
380 qa_config.ReleaseNode(tnode)
382 qa_config.ReleaseInstance(newinst)
385 def RunDaemonTests(instance):
386 """Test the ganeti-watcher script.
389 RunTest(qa_daemon.TestPauseWatcher)
391 RunTestIf("instance-automatic-restart",
392 qa_daemon.TestInstanceAutomaticRestart, instance)
393 RunTestIf("instance-consecutive-failures",
394 qa_daemon.TestInstanceConsecutiveFailures, instance)
396 RunTest(qa_daemon.TestResumeWatcher)
399 def RunHardwareFailureTests(instance, pnode, snode):
400 """Test cluster internal hardware failure recovery.
403 RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
404 RunTestIf(["instance-failover", "rapi"],
405 qa_rapi.TestRapiInstanceFailover, instance)
407 RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
408 RunTestIf(["instance-migrate", "rapi"],
409 qa_rapi.TestRapiInstanceMigrate, instance)
411 if qa_config.TestEnabled("instance-replace-disks"):
412 othernode = qa_config.AcquireNode(exclude=[pnode, snode])
414 RunTestIf("rapi", qa_rapi.TestRapiInstanceReplaceDisks, instance)
415 RunTest(qa_instance.TestReplaceDisks,
416 instance, pnode, snode, othernode)
418 qa_config.ReleaseNode(othernode)
420 RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
422 RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
424 RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
425 instance, pnode, snode)
426 RunTestIf("instance-disk-failure",
427 qa_instance.TestInstanceSecondaryDiskFailure, instance,
435 rapi_user = "ganeti-qa"
436 rapi_secret = utils.GenerateSecret()
439 SetupCluster(rapi_user, rapi_secret)
441 # Load RAPI certificate
442 qa_rapi.Setup(rapi_user, rapi_secret)
447 RunTestIf("tags", qa_tags.TestClusterTags)
453 pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
455 RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
456 RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
457 RunTestIf("delay", qa_cluster.TestDelay, pnode)
459 qa_config.ReleaseNode(pnode)
461 pnode = qa_config.AcquireNode()
463 RunTestIf("tags", qa_tags.TestNodeTags, pnode)
465 if qa_rapi.Enabled():
466 RunTest(qa_rapi.TestNode, pnode)
468 if qa_config.TestEnabled("instance-add-plain-disk"):
469 for use_client in [True, False]:
470 rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
472 if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
473 RunCommonInstanceTests(rapi_instance)
474 RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
477 if qa_config.TestEnabled("instance-add-plain-disk"):
478 instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
479 RunCommonInstanceTests(instance)
481 RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
482 RunExportImportTests(instance, pnode, None)
483 RunDaemonTests(instance)
485 RunTest(qa_instance.TestInstanceRemove, instance)
489 ("instance-add-drbd-disk",
490 qa_instance.TestInstanceAddWithDrbdDisk),
493 for name, func in multinode_tests:
494 if qa_config.TestEnabled(name):
495 snode = qa_config.AcquireNode(exclude=pnode)
497 instance = RunTest(func, pnode, snode)
498 RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, pnode)
499 RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, snode)
500 RunCommonInstanceTests(instance)
502 RunTest(qa_group.TestAssignNodesIncludingSplit,
503 constants.INITIAL_NODE_GROUP_NAME,
504 pnode["primary"], snode["primary"])
505 if qa_config.TestEnabled("instance-convert-disk"):
506 RunTest(qa_instance.TestInstanceShutdown, instance)
507 RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
508 RunTest(qa_instance.TestInstanceStartup, instance)
509 RunExportImportTests(instance, pnode, snode)
510 RunHardwareFailureTests(instance, pnode, snode)
512 RunTest(qa_instance.TestInstanceRemove, instance)
515 qa_config.ReleaseNode(snode)
517 # Test removing instance with offline drbd secondary
518 if qa_config.TestEnabled("instance-remove-drbd-offline"):
519 snode = qa_config.AcquireNode(exclude=pnode)
521 qa_instance.TestInstanceAddWithDrbdDisk(pnode, snode)
523 qa_node.MakeNodeOffline(snode, "yes")
524 RunTest(qa_instance.TestInstanceRemove, instance)
526 qa_node.MakeNodeOffline(snode, "no")
527 qa_config.ReleaseNode(snode)
529 if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
530 for shutdown in [False, True]:
531 instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
532 expnode = qa_config.AcquireNode(exclude=pnode)
535 # Stop instance before exporting and removing it
536 RunTest(qa_instance.TestInstanceShutdown, instance)
537 RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
538 RunTest(qa_instance.TestBackupList, expnode)
540 qa_config.ReleaseNode(expnode)
545 qa_config.ReleaseNode(pnode)
547 RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
549 RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
557 parser = optparse.OptionParser(usage="%prog [options] <config-file>")
558 parser.add_option("--yes-do-it", dest="yes_do_it",
560 help="Really execute the tests")
561 (qa_config.options, args) = parser.parse_args()
564 (config_file, ) = args
566 parser.error("Wrong number of arguments.")
568 if not qa_config.options.yes_do_it:
569 print ("Executing this script irreversibly destroys any Ganeti\n"
570 "configuration on all nodes involved. If you really want\n"
571 "to start testing, supply the --yes-do-it option.")
574 qa_config.Load(config_file)
576 primary = qa_config.GetMasterNode()["primary"]
577 qa_utils.StartMultiplexer(primary)
578 print ("SSH command for primary node: %s" %
579 utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
580 print ("SSH command for other nodes: %s" %
581 utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
585 qa_utils.CloseMultiplexers()
587 if __name__ == "__main__":