4 # Copyright (C) 2007, 2008, 2009, 2010, 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 """Script for doing QA on Ganeti.
26 # pylint: disable=C0103
48 from ganeti import utils
49 from ganeti import rapi # pylint: disable=W0611
50 from ganeti import constants
52 import ganeti.rapi.client # pylint: disable=W0611
53 from ganeti.rapi.client import UsesRapiClient
56 def _FormatHeader(line, end=72):
57 """Fill a line up to the end column.
60 line = "---- " + line + " "
61 line += "-" * (end - len(line))
66 def _DescriptionOf(fn):
67 """Computes the description of an item.
71 desc = fn.__doc__.splitlines()[0].strip()
75 return desc.rstrip(".")
78 def RunTest(fn, *args, **kwargs):
79 """Runs a test after printing a header.
83 tstart = datetime.datetime.now()
85 desc = _DescriptionOf(fn)
88 print _FormatHeader("%s start %s" % (tstart, desc))
91 retval = fn(*args, **kwargs)
94 tstop = datetime.datetime.now()
95 tdelta = tstop - tstart
96 print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
99 def RunTestIf(testnames, fn, *args, **kwargs):
100 """Runs a test conditionally.
102 @param testnames: either a single test name in the configuration
103 file, or a list of testnames (which will be AND-ed together)
106 if qa_config.TestEnabled(testnames):
107 RunTest(fn, *args, **kwargs)
109 tstart = datetime.datetime.now()
110 desc = _DescriptionOf(fn)
111 # TODO: Formatting test names when non-string names are involved
112 print _FormatHeader("%s skipping %s, test(s) %s disabled" %
113 (tstart, desc, testnames))
117 """Run several environment tests.
120 RunTestIf("env", qa_env.TestSshConnection)
121 RunTestIf("env", qa_env.TestIcmpPing)
122 RunTestIf("env", qa_env.TestGanetiCommands)
125 def SetupCluster(rapi_user, rapi_secret):
126 """Initializes the cluster.
128 @param rapi_user: Login user for RAPI
129 @param rapi_secret: Login secret for RAPI
132 RunTestIf("create-cluster", qa_cluster.TestClusterInit,
133 rapi_user, rapi_secret)
134 if not qa_config.TestEnabled("create-cluster"):
135 # If the cluster is already in place, we assume that exclusive-storage is
136 # already set according to the configuration
137 qa_config.SetExclusiveStorage(qa_config.get("exclusive-storage", False))
139 # Test on empty cluster
140 RunTestIf("node-list", qa_node.TestNodeList)
141 RunTestIf("instance-list", qa_instance.TestInstanceList)
142 RunTestIf("job-list", qa_job.TestJobList)
144 RunTestIf("create-cluster", qa_node.TestNodeAddAll)
145 if not qa_config.TestEnabled("create-cluster"):
146 # consider the nodes are already there
147 qa_node.MarkNodeAddedAll()
149 RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
151 # enable the watcher (unconditionally)
152 RunTest(qa_daemon.TestResumeWatcher)
154 RunTestIf("node-list", qa_node.TestNodeList)
156 # Test listing fields
157 RunTestIf("node-list", qa_node.TestNodeListFields)
158 RunTestIf("instance-list", qa_instance.TestInstanceListFields)
159 RunTestIf("job-list", qa_job.TestJobListFields)
160 RunTestIf("instance-export", qa_instance.TestBackupListFields)
162 RunTestIf("node-info", qa_node.TestNodeInfo)
165 def RunClusterTests():
166 """Runs tests related to gnt-cluster.
170 ("create-cluster", qa_cluster.TestClusterInitDisk),
171 ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
172 ("cluster-verify", qa_cluster.TestClusterVerify),
173 ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
174 # TODO: add more cluster modify tests
175 ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
176 ("cluster-modify", qa_cluster.TestClusterModifyBe),
177 ("cluster-modify", qa_cluster.TestClusterModifyDisk),
178 ("cluster-rename", qa_cluster.TestClusterRename),
179 ("cluster-info", qa_cluster.TestClusterVersion),
180 ("cluster-info", qa_cluster.TestClusterInfo),
181 ("cluster-info", qa_cluster.TestClusterGetmaster),
182 ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
183 ("cluster-copyfile", qa_cluster.TestClusterCopyfile),
184 ("cluster-command", qa_cluster.TestClusterCommand),
185 ("cluster-burnin", qa_cluster.TestClusterBurnin),
186 ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
187 ("cluster-master-failover",
188 qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
189 ("cluster-oob", qa_cluster.TestClusterOob),
190 ("rapi", qa_rapi.TestVersion),
191 ("rapi", qa_rapi.TestEmptyCluster),
192 ("rapi", qa_rapi.TestRapiQuery),
197 def RunRepairDiskSizes():
198 """Run the repair disk-sizes test.
201 RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
205 """Runs all tests related to gnt-os.
208 if qa_config.TestEnabled("rapi"):
209 rapi_getos = qa_rapi.GetOperatingSystems
215 qa_os.TestOsDiagnose,
222 qa_os.TestOsPartiallyValid,
224 RunTestIf("os", fn, rapi_getos)
227 qa_os.TestOsModifyValid,
228 qa_os.TestOsModifyInvalid,
229 qa_os.TestOsStatesNonExisting,
234 def RunCommonInstanceTests(instance):
235 """Runs a few tests that are common to all disk types.
238 RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
239 RunTestIf(["instance-shutdown", "instance-console", "rapi"],
240 qa_rapi.TestRapiStoppedInstanceConsole, instance)
241 RunTestIf(["instance-shutdown", "instance-modify"],
242 qa_instance.TestInstanceStoppedModify, instance)
243 RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
245 # Test shutdown/start via RAPI
246 RunTestIf(["instance-shutdown", "rapi"],
247 qa_rapi.TestRapiInstanceShutdown, instance)
248 RunTestIf(["instance-shutdown", "rapi"],
249 qa_rapi.TestRapiInstanceStartup, instance)
251 RunTestIf("instance-list", qa_instance.TestInstanceList)
253 RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
255 RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
256 RunTestIf(["instance-modify", "rapi"],
257 qa_rapi.TestRapiInstanceModify, instance)
259 RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
260 RunTestIf(["instance-console", "rapi"],
261 qa_rapi.TestRapiInstanceConsole, instance)
263 DOWN_TESTS = qa_config.Either([
264 "instance-reinstall",
266 "instance-grow-disk",
269 # shutdown instance for any 'down' tests
270 RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
272 # now run the 'down' state tests
273 RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
274 RunTestIf(["instance-reinstall", "rapi"],
275 qa_rapi.TestRapiInstanceReinstall, instance)
277 if qa_config.TestEnabled("instance-rename"):
278 tgt_instance = qa_config.AcquireInstance()
280 rename_source = instance.name
281 rename_target = tgt_instance.name
282 # perform instance rename to the same name
283 RunTest(qa_instance.TestInstanceRenameAndBack,
284 rename_source, rename_source)
285 RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
286 rename_source, rename_source)
287 if rename_target is not None:
288 # perform instance rename to a different name, if we have one configured
289 RunTest(qa_instance.TestInstanceRenameAndBack,
290 rename_source, rename_target)
291 RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
292 rename_source, rename_target)
294 tgt_instance.Release()
296 RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
298 # and now start the instance again
299 RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
301 RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
303 RunTestIf("tags", qa_tags.TestInstanceTags, instance)
305 RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
307 RunTestIf("rapi", qa_rapi.TestInstance, instance)
309 # Lists instances, too
310 RunTestIf("node-list", qa_node.TestNodeList)
312 # Some jobs have been run, let's test listing them
313 RunTestIf("job-list", qa_job.TestJobList)
316 def RunCommonNodeTests():
317 """Run a few common node tests.
320 RunTestIf("node-volumes", qa_node.TestNodeVolumes)
321 RunTestIf("node-storage", qa_node.TestNodeStorage)
322 RunTestIf("node-oob", qa_node.TestOutOfBand)
325 def RunGroupListTests():
326 """Run tests for listing node groups.
329 RunTestIf("group-list", qa_group.TestGroupList)
330 RunTestIf("group-list", qa_group.TestGroupListFields)
333 def RunNetworkTests():
334 """Run tests for network management.
337 RunTestIf("network", qa_network.TestNetworkAddRemove)
338 RunTestIf("network", qa_network.TestNetworkConnect)
341 def RunGroupRwTests():
342 """Run tests for adding/removing/renaming groups.
345 RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
346 RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
347 RunTestIf("group-rwops", qa_group.TestGroupModify)
348 RunTestIf(["group-rwops", "rapi"], qa_rapi.TestRapiNodeGroups)
349 RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
350 qa_group.GetDefaultGroup())
353 def RunExportImportTests(instance, inodes):
354 """Tries to export and import the instance.
356 @type inodes: list of nodes
357 @param inodes: current nodes of the instance
360 if qa_config.TestEnabled("instance-export"):
361 RunTest(qa_instance.TestInstanceExportNoTarget, instance)
364 expnode = qa_config.AcquireNode(exclude=pnode)
366 name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
368 RunTest(qa_instance.TestBackupList, expnode)
370 if qa_config.TestEnabled("instance-import"):
371 newinst = qa_config.AcquireInstance()
373 RunTest(qa_instance.TestInstanceImport, newinst, pnode,
375 # Check if starting the instance works
376 RunTest(qa_instance.TestInstanceStartup, newinst)
377 RunTest(qa_instance.TestInstanceRemove, newinst)
383 if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
384 newinst = qa_config.AcquireInstance()
386 tnode = qa_config.AcquireNode(exclude=inodes)
388 RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
396 def RunDaemonTests(instance):
397 """Test the ganeti-watcher script.
400 RunTest(qa_daemon.TestPauseWatcher)
402 RunTestIf("instance-automatic-restart",
403 qa_daemon.TestInstanceAutomaticRestart, instance)
404 RunTestIf("instance-consecutive-failures",
405 qa_daemon.TestInstanceConsecutiveFailures, instance)
407 RunTest(qa_daemon.TestResumeWatcher)
410 def RunHardwareFailureTests(instance, inodes):
411 """Test cluster internal hardware failure recovery.
414 RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
415 RunTestIf(["instance-failover", "rapi"],
416 qa_rapi.TestRapiInstanceFailover, instance)
418 RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
419 RunTestIf(["instance-migrate", "rapi"],
420 qa_rapi.TestRapiInstanceMigrate, instance)
422 if qa_config.TestEnabled("instance-replace-disks"):
423 # We just need alternative secondary nodes, hence "- 1"
424 othernodes = qa_config.AcquireManyNodes(len(inodes) - 1, exclude=inodes)
426 RunTestIf("rapi", qa_rapi.TestRapiInstanceReplaceDisks, instance)
427 RunTest(qa_instance.TestReplaceDisks,
428 instance, inodes, othernodes)
430 qa_config.ReleaseManyNodes(othernodes)
433 if qa_config.TestEnabled("instance-recreate-disks"):
435 acquirednodes = qa_config.AcquireManyNodes(len(inodes), exclude=inodes)
436 othernodes = acquirednodes
437 except qa_error.OutOfNodesError:
439 # If the cluster is not big enough, let's reuse some of the nodes, but
440 # with different roles. In this way, we can test a DRBD instance even on
442 acquirednodes = [qa_config.AcquireNode(exclude=inodes)]
443 othernodes = acquirednodes + inodes[:-1]
447 RunTest(qa_instance.TestRecreateDisks,
448 instance, inodes, othernodes)
450 qa_config.ReleaseManyNodes(acquirednodes)
453 RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, inodes[0], inodes[1])
454 RunTestIf("node-failover", qa_node.TestNodeFailover, inodes[0], inodes[1])
457 def RunExclusiveStorageTests():
458 """Test exclusive storage."""
459 if not qa_config.TestEnabled("cluster-exclusive-storage"):
462 node = qa_config.AcquireNode()
464 old_es = qa_cluster.TestSetExclStorCluster(False)
465 qa_node.TestExclStorSingleNode(node)
467 qa_cluster.TestSetExclStorCluster(True)
468 qa_cluster.TestExclStorSharedPv(node)
470 if qa_config.TestEnabled("instance-add-plain-disk"):
471 # Make sure that the cluster doesn't have any pre-existing problem
472 qa_cluster.AssertClusterVerify()
474 # Create and allocate instances
475 instance1 = qa_instance.TestInstanceAddWithPlainDisk([node])
477 instance2 = qa_instance.TestInstanceAddWithPlainDisk([node])
479 # cluster-verify checks that disks are allocated correctly
480 qa_cluster.AssertClusterVerify()
483 qa_instance.TestInstanceRemove(instance2)
484 qa_instance.TestInstanceRemove(instance1)
490 if qa_config.TestEnabled("instance-add-drbd-disk"):
491 snode = qa_config.AcquireNode()
493 qa_cluster.TestSetExclStorCluster(False)
494 instance = qa_instance.TestInstanceAddWithDrbdDisk([node, snode])
496 qa_cluster.TestSetExclStorCluster(True)
497 exp_err = [constants.CV_EINSTANCEUNSUITABLENODE]
498 qa_cluster.AssertClusterVerify(fail=True, errors=exp_err)
499 qa_instance.TestInstanceRemove(instance)
504 qa_cluster.TestSetExclStorCluster(old_es)
509 def RunInstanceTests():
510 """Create and exercise instances."""
512 ("instance-add-plain-disk", constants.DT_PLAIN,
513 qa_instance.TestInstanceAddWithPlainDisk, 1),
514 ("instance-add-drbd-disk", constants.DT_DRBD8,
515 qa_instance.TestInstanceAddWithDrbdDisk, 2),
518 for (test_name, templ, create_fun, num_nodes) in instance_tests:
519 if (qa_config.TestEnabled(test_name) and
520 qa_config.IsTemplateSupported(templ)):
521 inodes = qa_config.AcquireManyNodes(num_nodes)
523 instance = RunTest(create_fun, inodes)
525 RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
526 RunDaemonTests(instance)
528 RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, node)
530 RunTestIf("group-rwops", qa_group.TestAssignNodesIncludingSplit,
531 constants.INITIAL_NODE_GROUP_NAME,
532 inodes[0].primary, inodes[1].primary)
533 if qa_config.TestEnabled("instance-convert-disk"):
534 RunTest(qa_instance.TestInstanceShutdown, instance)
535 RunTest(qa_instance.TestInstanceConvertDiskToPlain,
537 RunTest(qa_instance.TestInstanceStartup, instance)
538 RunCommonInstanceTests(instance)
540 RunExportImportTests(instance, inodes)
541 RunHardwareFailureTests(instance, inodes)
543 RunTest(qa_instance.TestInstanceRemove, instance)
548 qa_config.ReleaseManyNodes(inodes)
549 qa_cluster.AssertClusterVerify()
556 rapi_user = "ganeti-qa"
557 rapi_secret = utils.GenerateSecret()
560 SetupCluster(rapi_user, rapi_secret)
562 # Load RAPI certificate
563 qa_rapi.Setup(rapi_user, rapi_secret)
568 RunTestIf("tags", qa_tags.TestClusterTags)
575 # The master shouldn't be readded or put offline; "delay" needs a non-master
577 pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
579 RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
580 RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
581 RunTestIf("delay", qa_cluster.TestDelay, pnode)
585 # Make sure the cluster is clean before running instance tests
586 qa_cluster.AssertClusterVerify()
588 pnode = qa_config.AcquireNode()
590 RunTestIf("tags", qa_tags.TestNodeTags, pnode)
592 if qa_rapi.Enabled():
593 RunTest(qa_rapi.TestNode, pnode)
595 if qa_config.TestEnabled("instance-add-plain-disk"):
596 for use_client in [True, False]:
597 rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
600 if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
601 RunCommonInstanceTests(rapi_instance)
602 RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
604 rapi_instance.Release()
611 ("default-instance-tests", lambda: None, lambda _: None),
612 ("exclusive-storage-instance-tests",
613 lambda: qa_cluster.TestSetExclStorCluster(True),
614 qa_cluster.TestSetExclStorCluster),
616 for (conf_name, setup_conf_f, restore_conf_f) in config_list:
617 if qa_config.TestEnabled(conf_name):
618 oldconf = setup_conf_f()
620 restore_conf_f(oldconf)
622 pnode = qa_config.AcquireNode()
624 if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
625 for shutdown in [False, True]:
626 instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, [pnode])
628 expnode = qa_config.AcquireNode(exclude=pnode)
631 # Stop instance before exporting and removing it
632 RunTest(qa_instance.TestInstanceShutdown, instance)
633 RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
634 RunTest(qa_instance.TestBackupList, expnode)
641 qa_cluster.AssertClusterVerify()
646 RunExclusiveStorageTests()
648 # Test removing instance with offline drbd secondary
649 if qa_config.TestEnabled("instance-remove-drbd-offline"):
650 # Make sure the master is not put offline
651 snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
653 pnode = qa_config.AcquireNode(exclude=snode)
655 instance = qa_instance.TestInstanceAddWithDrbdDisk([pnode, snode])
656 set_offline = lambda node: qa_node.MakeNodeOffline(node, "yes")
657 set_online = lambda node: qa_node.MakeNodeOffline(node, "no")
658 RunTest(qa_instance.TestRemoveInstanceOfflineNode, instance, snode,
659 set_offline, set_online)
664 qa_cluster.AssertClusterVerify()
666 RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
668 RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
676 parser = optparse.OptionParser(usage="%prog [options] <config-file>")
677 parser.add_option("--yes-do-it", dest="yes_do_it",
679 help="Really execute the tests")
680 (opts, args) = parser.parse_args()
683 (config_file, ) = args
685 parser.error("Wrong number of arguments.")
687 if not opts.yes_do_it:
688 print ("Executing this script irreversibly destroys any Ganeti\n"
689 "configuration on all nodes involved. If you really want\n"
690 "to start testing, supply the --yes-do-it option.")
693 qa_config.Load(config_file)
695 primary = qa_config.GetMasterNode().primary
696 qa_utils.StartMultiplexer(primary)
697 print ("SSH command for primary node: %s" %
698 utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
699 print ("SSH command for other nodes: %s" %
700 utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
704 qa_utils.CloseMultiplexers()
706 if __name__ == "__main__":