QA: Use instance name for rename test
[ganeti-local] / qa / ganeti-qa.py
1 #!/usr/bin/python -u
2 #
3
4 # Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script for doing QA on Ganeti.
23
24 """
25
26 # pylint: disable=C0103
27 # due to invalid name
28
29 import sys
30 import datetime
31 import optparse
32
33 import qa_cluster
34 import qa_config
35 import qa_daemon
36 import qa_env
37 import qa_group
38 import qa_instance
39 import qa_node
40 import qa_os
41 import qa_job
42 import qa_rapi
43 import qa_tags
44 import qa_utils
45
46 from ganeti import utils
47 from ganeti import rapi # pylint: disable=W0611
48 from ganeti import constants
49
50 import ganeti.rapi.client # pylint: disable=W0611
51 from ganeti.rapi.client import UsesRapiClient
52
53
54 def _FormatHeader(line, end=72):
55   """Fill a line up to the end column.
56
57   """
58   line = "---- " + line + " "
59   line += "-" * (end - len(line))
60   line = line.rstrip()
61   return line
62
63
64 def _DescriptionOf(fn):
65   """Computes the description of an item.
66
67   """
68   if fn.__doc__:
69     desc = fn.__doc__.splitlines()[0].strip()
70   else:
71     desc = "%r" % fn
72
73   return desc.rstrip(".")
74
75
76 def RunTest(fn, *args, **kwargs):
77   """Runs a test after printing a header.
78
79   """
80
81   tstart = datetime.datetime.now()
82
83   desc = _DescriptionOf(fn)
84
85   print
86   print _FormatHeader("%s start %s" % (tstart, desc))
87
88   try:
89     retval = fn(*args, **kwargs)
90     return retval
91   finally:
92     tstop = datetime.datetime.now()
93     tdelta = tstop - tstart
94     print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
95
96
97 def RunTestIf(testnames, fn, *args, **kwargs):
98   """Runs a test conditionally.
99
100   @param testnames: either a single test name in the configuration
101       file, or a list of testnames (which will be AND-ed together)
102
103   """
104   if qa_config.TestEnabled(testnames):
105     RunTest(fn, *args, **kwargs)
106   else:
107     tstart = datetime.datetime.now()
108     desc = _DescriptionOf(fn)
109     print _FormatHeader("%s skipping %s, test(s) %s disabled" %
110                         (tstart, desc, testnames))
111
112
113 def RunEnvTests():
114   """Run several environment tests.
115
116   """
117   RunTestIf("env", qa_env.TestSshConnection)
118   RunTestIf("env", qa_env.TestIcmpPing)
119   RunTestIf("env", qa_env.TestGanetiCommands)
120
121
122 def SetupCluster(rapi_user, rapi_secret):
123   """Initializes the cluster.
124
125   @param rapi_user: Login user for RAPI
126   @param rapi_secret: Login secret for RAPI
127
128   """
129   RunTestIf("create-cluster", qa_cluster.TestClusterInit,
130             rapi_user, rapi_secret)
131
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)
136
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()
141
142   RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
143
144   # enable the watcher (unconditionally)
145   RunTest(qa_daemon.TestResumeWatcher)
146
147   RunTestIf("node-list", qa_node.TestNodeList)
148
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)
154
155   RunTestIf("node-info", qa_node.TestNodeInfo)
156
157
158 def RunClusterTests():
159   """Runs tests related to gnt-cluster.
160
161   """
162   for test, fn in [
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),
186     ]:
187     RunTestIf(test, fn)
188
189
190 def RunRepairDiskSizes():
191   """Run the repair disk-sizes test.
192
193   """
194   RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
195
196
197 def RunOsTests():
198   """Runs all tests related to gnt-os.
199
200   """
201   if qa_config.TestEnabled("rapi"):
202     rapi_getos = qa_rapi.GetOperatingSystems
203   else:
204     rapi_getos = None
205
206   for fn in [
207     qa_os.TestOsList,
208     qa_os.TestOsDiagnose,
209     ]:
210     RunTestIf("os", fn)
211
212   for fn in [
213     qa_os.TestOsValid,
214     qa_os.TestOsInvalid,
215     qa_os.TestOsPartiallyValid,
216     ]:
217     RunTestIf("os", fn, rapi_getos)
218
219   for fn in [
220     qa_os.TestOsModifyValid,
221     qa_os.TestOsModifyInvalid,
222     qa_os.TestOsStatesNonExisting,
223     ]:
224     RunTestIf("os", fn)
225
226
227 def RunCommonInstanceTests(instance):
228   """Runs a few tests that are common to all disk types.
229
230   """
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)
237
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)
243
244   RunTestIf("instance-list", qa_instance.TestInstanceList)
245
246   RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
247
248   RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
249   RunTestIf(["instance-modify", "rapi"],
250             qa_rapi.TestRapiInstanceModify, instance)
251
252   RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
253   RunTestIf(["instance-console", "rapi"],
254             qa_rapi.TestRapiInstanceConsole, instance)
255
256   DOWN_TESTS = qa_config.Either([
257     "instance-reinstall",
258     "instance-rename",
259     "instance-grow-disk",
260     ])
261
262   # shutdown instance for any 'down' tests
263   RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
264
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)
269
270   if qa_config.TestEnabled("instance-rename"):
271     tgt_instance = qa_config.AcquireInstance()
272     try:
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)
286     finally:
287       qa_config.ReleaseInstance(tgt_instance)
288
289   RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
290
291   # and now start the instance again
292   RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
293
294   RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
295
296   RunTestIf("tags", qa_tags.TestInstanceTags, instance)
297
298   RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
299
300   RunTestIf("rapi", qa_rapi.TestInstance, instance)
301
302   # Lists instances, too
303   RunTestIf("node-list", qa_node.TestNodeList)
304
305   # Some jobs have been run, let's test listing them
306   RunTestIf("job-list", qa_job.TestJobList)
307
308
309 def RunCommonNodeTests():
310   """Run a few common node tests.
311
312   """
313   RunTestIf("node-volumes", qa_node.TestNodeVolumes)
314   RunTestIf("node-storage", qa_node.TestNodeStorage)
315   RunTestIf("node-oob", qa_node.TestOutOfBand)
316
317
318 def RunGroupListTests():
319   """Run tests for listing node groups.
320
321   """
322   RunTestIf("group-list", qa_group.TestGroupList)
323   RunTestIf("group-list", qa_group.TestGroupListFields)
324
325
326 def RunGroupRwTests():
327   """Run tests for adding/removing/renaming groups.
328
329   """
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())
336
337
338 def RunExportImportTests(instance, pnode, snode):
339   """Tries to export and import the instance.
340
341   @param pnode: current primary node of the instance
342   @param snode: current secondary node of the instance, if any,
343       otherwise None
344
345   """
346   if qa_config.TestEnabled("instance-export"):
347     RunTest(qa_instance.TestInstanceExportNoTarget, instance)
348
349     expnode = qa_config.AcquireNode(exclude=pnode)
350     try:
351       name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
352
353       RunTest(qa_instance.TestBackupList, expnode)
354
355       if qa_config.TestEnabled("instance-import"):
356         newinst = qa_config.AcquireInstance()
357         try:
358           RunTest(qa_instance.TestInstanceImport, newinst, pnode,
359                   expnode, name)
360           # Check if starting the instance works
361           RunTest(qa_instance.TestInstanceStartup, newinst)
362           RunTest(qa_instance.TestInstanceRemove, newinst)
363         finally:
364           qa_config.ReleaseInstance(newinst)
365     finally:
366       qa_config.ReleaseNode(expnode)
367
368   if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
369     newinst = qa_config.AcquireInstance()
370     try:
371       if snode is None:
372         excl = [pnode]
373       else:
374         excl = [pnode, snode]
375       tnode = qa_config.AcquireNode(exclude=excl)
376       try:
377         RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
378                 pnode, snode, tnode)
379       finally:
380         qa_config.ReleaseNode(tnode)
381     finally:
382       qa_config.ReleaseInstance(newinst)
383
384
385 def RunDaemonTests(instance):
386   """Test the ganeti-watcher script.
387
388   """
389   RunTest(qa_daemon.TestPauseWatcher)
390
391   RunTestIf("instance-automatic-restart",
392             qa_daemon.TestInstanceAutomaticRestart, instance)
393   RunTestIf("instance-consecutive-failures",
394             qa_daemon.TestInstanceConsecutiveFailures, instance)
395
396   RunTest(qa_daemon.TestResumeWatcher)
397
398
399 def RunHardwareFailureTests(instance, pnode, snode):
400   """Test cluster internal hardware failure recovery.
401
402   """
403   RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
404   RunTestIf(["instance-failover", "rapi"],
405             qa_rapi.TestRapiInstanceFailover, instance)
406
407   RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
408   RunTestIf(["instance-migrate", "rapi"],
409             qa_rapi.TestRapiInstanceMigrate, instance)
410
411   if qa_config.TestEnabled("instance-replace-disks"):
412     othernode = qa_config.AcquireNode(exclude=[pnode, snode])
413     try:
414       RunTestIf("rapi", qa_rapi.TestRapiInstanceReplaceDisks, instance)
415       RunTest(qa_instance.TestReplaceDisks,
416               instance, pnode, snode, othernode)
417     finally:
418       qa_config.ReleaseNode(othernode)
419
420   RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
421
422   RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
423
424   RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
425             instance, pnode, snode)
426   RunTestIf("instance-disk-failure",
427             qa_instance.TestInstanceSecondaryDiskFailure, instance,
428             pnode, snode)
429
430
431 def RunQa():
432   """Main QA body.
433
434   """
435   rapi_user = "ganeti-qa"
436   rapi_secret = utils.GenerateSecret()
437
438   RunEnvTests()
439   SetupCluster(rapi_user, rapi_secret)
440
441   # Load RAPI certificate
442   qa_rapi.Setup(rapi_user, rapi_secret)
443
444   RunClusterTests()
445   RunOsTests()
446
447   RunTestIf("tags", qa_tags.TestClusterTags)
448
449   RunCommonNodeTests()
450   RunGroupListTests()
451   RunGroupRwTests()
452
453   pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
454   try:
455     RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
456     RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
457     RunTestIf("delay", qa_cluster.TestDelay, pnode)
458   finally:
459     qa_config.ReleaseNode(pnode)
460
461   pnode = qa_config.AcquireNode()
462   try:
463     RunTestIf("tags", qa_tags.TestNodeTags, pnode)
464
465     if qa_rapi.Enabled():
466       RunTest(qa_rapi.TestNode, pnode)
467
468       if qa_config.TestEnabled("instance-add-plain-disk"):
469         for use_client in [True, False]:
470           rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
471                                   use_client)
472           if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
473             RunCommonInstanceTests(rapi_instance)
474           RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
475           del rapi_instance
476
477     if qa_config.TestEnabled("instance-add-plain-disk"):
478       instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
479       RunCommonInstanceTests(instance)
480       RunGroupListTests()
481       RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
482       RunExportImportTests(instance, pnode, None)
483       RunDaemonTests(instance)
484       RunRepairDiskSizes()
485       RunTest(qa_instance.TestInstanceRemove, instance)
486       del instance
487
488     multinode_tests = [
489       ("instance-add-drbd-disk",
490        qa_instance.TestInstanceAddWithDrbdDisk),
491     ]
492
493     for name, func in multinode_tests:
494       if qa_config.TestEnabled(name):
495         snode = qa_config.AcquireNode(exclude=pnode)
496         try:
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)
501           RunGroupListTests()
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)
511           RunRepairDiskSizes()
512           RunTest(qa_instance.TestInstanceRemove, instance)
513           del instance
514         finally:
515           qa_config.ReleaseNode(snode)
516
517     # Test removing instance with offline drbd secondary
518     if qa_config.TestEnabled("instance-remove-drbd-offline"):
519       snode = qa_config.AcquireNode(exclude=pnode)
520       instance = \
521         qa_instance.TestInstanceAddWithDrbdDisk(pnode, snode)
522       try:
523         qa_node.MakeNodeOffline(snode, "yes")
524         RunTest(qa_instance.TestInstanceRemove, instance)
525       finally:
526         qa_node.MakeNodeOffline(snode, "no")
527         qa_config.ReleaseNode(snode)
528
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)
533         try:
534           if shutdown:
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)
539         finally:
540           qa_config.ReleaseNode(expnode)
541         del expnode
542         del instance
543
544   finally:
545     qa_config.ReleaseNode(pnode)
546
547   RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
548
549   RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
550
551
552 @UsesRapiClient
553 def main():
554   """Main program.
555
556   """
557   parser = optparse.OptionParser(usage="%prog [options] <config-file>")
558   parser.add_option("--yes-do-it", dest="yes_do_it",
559       action="store_true",
560       help="Really execute the tests")
561   (qa_config.options, args) = parser.parse_args()
562
563   if len(args) == 1:
564     (config_file, ) = args
565   else:
566     parser.error("Wrong number of arguments.")
567
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.")
572     sys.exit(1)
573
574   qa_config.Load(config_file)
575
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", "")))
582   try:
583     RunQa()
584   finally:
585     qa_utils.CloseMultiplexers()
586
587 if __name__ == "__main__":
588   main()