hv_xen._GetCommand: retrieve xen command from hvparams
[ganeti-local] / qa / ganeti-qa.py
1 #!/usr/bin/python -u
2 #
3
4 # Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 copy
30 import datetime
31 import optparse
32 import sys
33
34 import qa_cluster
35 import qa_config
36 import qa_daemon
37 import qa_env
38 import qa_error
39 import qa_group
40 import qa_instance
41 import qa_monitoring
42 import qa_network
43 import qa_node
44 import qa_os
45 import qa_job
46 import qa_rapi
47 import qa_tags
48 import qa_utils
49
50 from ganeti import utils
51 from ganeti import rapi # pylint: disable=W0611
52 from ganeti import constants
53
54 import ganeti.rapi.client # pylint: disable=W0611
55 from ganeti.rapi.client import UsesRapiClient
56
57
58 def _FormatHeader(line, end=72):
59   """Fill a line up to the end column.
60
61   """
62   line = "---- " + line + " "
63   line += "-" * (end - len(line))
64   line = line.rstrip()
65   return line
66
67
68 def _DescriptionOf(fn):
69   """Computes the description of an item.
70
71   """
72   if fn.__doc__:
73     desc = fn.__doc__.splitlines()[0].strip()
74   else:
75     desc = "%r" % fn
76
77   return desc.rstrip(".")
78
79
80 def RunTest(fn, *args, **kwargs):
81   """Runs a test after printing a header.
82
83   """
84
85   tstart = datetime.datetime.now()
86
87   desc = _DescriptionOf(fn)
88
89   print
90   print _FormatHeader("%s start %s" % (tstart, desc))
91
92   try:
93     retval = fn(*args, **kwargs)
94     return retval
95   finally:
96     tstop = datetime.datetime.now()
97     tdelta = tstop - tstart
98     print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
99
100
101 def RunTestIf(testnames, fn, *args, **kwargs):
102   """Runs a test conditionally.
103
104   @param testnames: either a single test name in the configuration
105       file, or a list of testnames (which will be AND-ed together)
106
107   """
108   if qa_config.TestEnabled(testnames):
109     RunTest(fn, *args, **kwargs)
110   else:
111     tstart = datetime.datetime.now()
112     desc = _DescriptionOf(fn)
113     # TODO: Formatting test names when non-string names are involved
114     print _FormatHeader("%s skipping %s, test(s) %s disabled" %
115                         (tstart, desc, testnames))
116
117
118 def RunEnvTests():
119   """Run several environment tests.
120
121   """
122   RunTestIf("env", qa_env.TestSshConnection)
123   RunTestIf("env", qa_env.TestIcmpPing)
124   RunTestIf("env", qa_env.TestGanetiCommands)
125
126
127 def SetupCluster(rapi_user, rapi_secret):
128   """Initializes the cluster.
129
130   @param rapi_user: Login user for RAPI
131   @param rapi_secret: Login secret for RAPI
132
133   """
134   RunTestIf("create-cluster", qa_cluster.TestClusterInit,
135             rapi_user, rapi_secret)
136   if not qa_config.TestEnabled("create-cluster"):
137     # If the cluster is already in place, we assume that exclusive-storage is
138     # already set according to the configuration
139     qa_config.SetExclusiveStorage(qa_config.get("exclusive-storage", False))
140
141   # Test on empty cluster
142   RunTestIf("node-list", qa_node.TestNodeList)
143   RunTestIf("instance-list", qa_instance.TestInstanceList)
144   RunTestIf("job-list", qa_job.TestJobList)
145
146   RunTestIf("create-cluster", qa_node.TestNodeAddAll)
147   if not qa_config.TestEnabled("create-cluster"):
148     # consider the nodes are already there
149     qa_node.MarkNodeAddedAll()
150
151   RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
152
153   # enable the watcher (unconditionally)
154   RunTest(qa_daemon.TestResumeWatcher)
155
156   RunTestIf("node-list", qa_node.TestNodeList)
157
158   # Test listing fields
159   RunTestIf("node-list", qa_node.TestNodeListFields)
160   RunTestIf("instance-list", qa_instance.TestInstanceListFields)
161   RunTestIf("job-list", qa_job.TestJobListFields)
162   RunTestIf("instance-export", qa_instance.TestBackupListFields)
163
164   RunTestIf("node-info", qa_node.TestNodeInfo)
165
166
167 def RunClusterTests():
168   """Runs tests related to gnt-cluster.
169
170   """
171   for test, fn in [
172     ("create-cluster", qa_cluster.TestClusterInitDisk),
173     ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
174     ("cluster-verify", qa_cluster.TestClusterVerify),
175     ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
176     # TODO: add more cluster modify tests
177     ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
178     ("cluster-modify", qa_cluster.TestClusterModifyIPolicy),
179     ("cluster-modify", qa_cluster.TestClusterModifyISpecs),
180     ("cluster-modify", qa_cluster.TestClusterModifyBe),
181     ("cluster-modify", qa_cluster.TestClusterModifyDisk),
182     ("cluster-modify", qa_cluster.TestClusterModifyDiskTemplates),
183     ("cluster-rename", qa_cluster.TestClusterRename),
184     ("cluster-info", qa_cluster.TestClusterVersion),
185     ("cluster-info", qa_cluster.TestClusterInfo),
186     ("cluster-info", qa_cluster.TestClusterGetmaster),
187     ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
188     (["cluster-copyfile", qa_config.NoVirtualCluster],
189      qa_cluster.TestClusterCopyfile),
190     ("cluster-command", qa_cluster.TestClusterCommand),
191     ("cluster-burnin", qa_cluster.TestClusterBurnin),
192     ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
193     ("cluster-master-failover",
194      qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
195     (["cluster-oob", qa_config.NoVirtualCluster],
196      qa_cluster.TestClusterOob),
197     (qa_rapi.Enabled, qa_rapi.TestVersion),
198     (qa_rapi.Enabled, qa_rapi.TestEmptyCluster),
199     (qa_rapi.Enabled, qa_rapi.TestRapiQuery),
200     ]:
201     RunTestIf(test, fn)
202
203
204 def RunRepairDiskSizes():
205   """Run the repair disk-sizes test.
206
207   """
208   RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
209
210
211 def RunOsTests():
212   """Runs all tests related to gnt-os.
213
214   """
215   os_enabled = ["os", qa_config.NoVirtualCluster]
216
217   if qa_config.TestEnabled(qa_rapi.Enabled):
218     rapi_getos = qa_rapi.GetOperatingSystems
219   else:
220     rapi_getos = None
221
222   for fn in [
223     qa_os.TestOsList,
224     qa_os.TestOsDiagnose,
225     ]:
226     RunTestIf(os_enabled, fn)
227
228   for fn in [
229     qa_os.TestOsValid,
230     qa_os.TestOsInvalid,
231     qa_os.TestOsPartiallyValid,
232     ]:
233     RunTestIf(os_enabled, fn, rapi_getos)
234
235   for fn in [
236     qa_os.TestOsModifyValid,
237     qa_os.TestOsModifyInvalid,
238     qa_os.TestOsStatesNonExisting,
239     ]:
240     RunTestIf(os_enabled, fn)
241
242
243 def RunCommonInstanceTests(instance):
244   """Runs a few tests that are common to all disk types.
245
246   """
247   RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
248   RunTestIf(["instance-shutdown", "instance-console", qa_rapi.Enabled],
249             qa_rapi.TestRapiStoppedInstanceConsole, instance)
250   RunTestIf(["instance-shutdown", "instance-modify"],
251             qa_instance.TestInstanceStoppedModify, instance)
252   RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
253
254   # Test shutdown/start via RAPI
255   RunTestIf(["instance-shutdown", qa_rapi.Enabled],
256             qa_rapi.TestRapiInstanceShutdown, instance)
257   RunTestIf(["instance-shutdown", qa_rapi.Enabled],
258             qa_rapi.TestRapiInstanceStartup, instance)
259
260   RunTestIf("instance-list", qa_instance.TestInstanceList)
261
262   RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
263
264   RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
265   RunTestIf(["instance-modify", qa_rapi.Enabled],
266             qa_rapi.TestRapiInstanceModify, instance)
267
268   RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
269   RunTestIf(["instance-console", qa_rapi.Enabled],
270             qa_rapi.TestRapiInstanceConsole, instance)
271
272   RunTestIf("instance-device-names", qa_instance.TestInstanceDeviceNames,
273             instance)
274   DOWN_TESTS = qa_config.Either([
275     "instance-reinstall",
276     "instance-rename",
277     "instance-grow-disk",
278     ])
279
280   # shutdown instance for any 'down' tests
281   RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
282
283   # now run the 'down' state tests
284   RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
285   RunTestIf(["instance-reinstall", qa_rapi.Enabled],
286             qa_rapi.TestRapiInstanceReinstall, instance)
287
288   if qa_config.TestEnabled("instance-rename"):
289     tgt_instance = qa_config.AcquireInstance()
290     try:
291       rename_source = instance.name
292       rename_target = tgt_instance.name
293       # perform instance rename to the same name
294       RunTest(qa_instance.TestInstanceRenameAndBack,
295               rename_source, rename_source)
296       RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
297                 rename_source, rename_source)
298       if rename_target is not None:
299         # perform instance rename to a different name, if we have one configured
300         RunTest(qa_instance.TestInstanceRenameAndBack,
301                 rename_source, rename_target)
302         RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
303                   rename_source, rename_target)
304     finally:
305       tgt_instance.Release()
306
307   RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
308
309   # and now start the instance again
310   RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
311
312   RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
313
314   RunTestIf("tags", qa_tags.TestInstanceTags, instance)
315
316   RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
317
318   RunTestIf(qa_rapi.Enabled, qa_rapi.TestInstance, instance)
319
320   # Lists instances, too
321   RunTestIf("node-list", qa_node.TestNodeList)
322
323   # Some jobs have been run, let's test listing them
324   RunTestIf("job-list", qa_job.TestJobList)
325
326
327 def RunCommonNodeTests():
328   """Run a few common node tests.
329
330   """
331   RunTestIf("node-volumes", qa_node.TestNodeVolumes)
332   RunTestIf("node-storage", qa_node.TestNodeStorage)
333   RunTestIf(["node-oob", qa_config.NoVirtualCluster], qa_node.TestOutOfBand)
334
335
336 def RunGroupListTests():
337   """Run tests for listing node groups.
338
339   """
340   RunTestIf("group-list", qa_group.TestGroupList)
341   RunTestIf("group-list", qa_group.TestGroupListFields)
342
343
344 def RunNetworkTests():
345   """Run tests for network management.
346
347   """
348   RunTestIf("network", qa_network.TestNetworkAddRemove)
349   RunTestIf("network", qa_network.TestNetworkConnect)
350
351
352 def RunGroupRwTests():
353   """Run tests for adding/removing/renaming groups.
354
355   """
356   RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
357   RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
358   RunTestIf("group-rwops", qa_group.TestGroupModify)
359   RunTestIf(["group-rwops", qa_rapi.Enabled], qa_rapi.TestRapiNodeGroups)
360   RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
361             qa_group.GetDefaultGroup())
362
363
364 def RunExportImportTests(instance, inodes):
365   """Tries to export and import the instance.
366
367   @type inodes: list of nodes
368   @param inodes: current nodes of the instance
369
370   """
371   # FIXME: export explicitly bails out on file based storage. other non-lvm
372   # based storage types are untested, though. Also note that import could still
373   # work, but is deeply embedded into the "export" case.
374   if (qa_config.TestEnabled("instance-export") and
375       instance.disk_template != constants.DT_FILE):
376     RunTest(qa_instance.TestInstanceExportNoTarget, instance)
377
378     pnode = inodes[0]
379     expnode = qa_config.AcquireNode(exclude=pnode)
380     try:
381       name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
382
383       RunTest(qa_instance.TestBackupList, expnode)
384
385       if qa_config.TestEnabled("instance-import"):
386         newinst = qa_config.AcquireInstance()
387         try:
388           RunTest(qa_instance.TestInstanceImport, newinst, pnode,
389                   expnode, name)
390           # Check if starting the instance works
391           RunTest(qa_instance.TestInstanceStartup, newinst)
392           RunTest(qa_instance.TestInstanceRemove, newinst)
393         finally:
394           newinst.Release()
395     finally:
396       expnode.Release()
397
398   # FIXME: inter-cluster-instance-move crashes on file based instances :/
399   # See Issue 414.
400   if (qa_config.TestEnabled([qa_rapi.Enabled, "inter-cluster-instance-move"])
401       and instance.disk_template != constants.DT_FILE):
402     newinst = qa_config.AcquireInstance()
403     try:
404       tnode = qa_config.AcquireNode(exclude=inodes)
405       try:
406         RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
407                 inodes, tnode)
408       finally:
409         tnode.Release()
410     finally:
411       newinst.Release()
412
413
414 def RunDaemonTests(instance):
415   """Test the ganeti-watcher script.
416
417   """
418   RunTest(qa_daemon.TestPauseWatcher)
419
420   RunTestIf("instance-automatic-restart",
421             qa_daemon.TestInstanceAutomaticRestart, instance)
422   RunTestIf("instance-consecutive-failures",
423             qa_daemon.TestInstanceConsecutiveFailures, instance)
424
425   RunTest(qa_daemon.TestResumeWatcher)
426
427
428 def RunHardwareFailureTests(instance, inodes):
429   """Test cluster internal hardware failure recovery.
430
431   """
432   RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
433   RunTestIf(["instance-failover", qa_rapi.Enabled],
434             qa_rapi.TestRapiInstanceFailover, instance)
435
436   RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
437   RunTestIf(["instance-migrate", qa_rapi.Enabled],
438             qa_rapi.TestRapiInstanceMigrate, instance)
439
440   if qa_config.TestEnabled("instance-replace-disks"):
441     # We just need alternative secondary nodes, hence "- 1"
442     othernodes = qa_config.AcquireManyNodes(len(inodes) - 1, exclude=inodes)
443     try:
444       RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceReplaceDisks, instance)
445       RunTest(qa_instance.TestReplaceDisks,
446               instance, inodes, othernodes)
447     finally:
448       qa_config.ReleaseManyNodes(othernodes)
449     del othernodes
450
451   if qa_config.TestEnabled("instance-recreate-disks"):
452     try:
453       acquirednodes = qa_config.AcquireManyNodes(len(inodes), exclude=inodes)
454       othernodes = acquirednodes
455     except qa_error.OutOfNodesError:
456       if len(inodes) > 1:
457         # If the cluster is not big enough, let's reuse some of the nodes, but
458         # with different roles. In this way, we can test a DRBD instance even on
459         # a 3-node cluster.
460         acquirednodes = [qa_config.AcquireNode(exclude=inodes)]
461         othernodes = acquirednodes + inodes[:-1]
462       else:
463         raise
464     try:
465       RunTest(qa_instance.TestRecreateDisks,
466               instance, inodes, othernodes)
467     finally:
468       qa_config.ReleaseManyNodes(acquirednodes)
469
470   if len(inodes) >= 2:
471     RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, inodes[0], inodes[1])
472     RunTestIf("node-failover", qa_node.TestNodeFailover, inodes[0], inodes[1])
473
474
475 def RunExclusiveStorageTests():
476   """Test exclusive storage."""
477   if not qa_config.TestEnabled("cluster-exclusive-storage"):
478     return
479
480   node = qa_config.AcquireNode()
481   try:
482     old_es = qa_cluster.TestSetExclStorCluster(False)
483     qa_node.TestExclStorSingleNode(node)
484
485     qa_cluster.TestSetExclStorCluster(True)
486     qa_cluster.TestExclStorSharedPv(node)
487
488     if qa_config.TestEnabled("instance-add-plain-disk"):
489       # Make sure that the cluster doesn't have any pre-existing problem
490       qa_cluster.AssertClusterVerify()
491
492       # Create and allocate instances
493       instance1 = qa_instance.TestInstanceAddWithPlainDisk([node])
494       try:
495         instance2 = qa_instance.TestInstanceAddWithPlainDisk([node])
496         try:
497           # cluster-verify checks that disks are allocated correctly
498           qa_cluster.AssertClusterVerify()
499
500           # Remove instances
501           qa_instance.TestInstanceRemove(instance2)
502           qa_instance.TestInstanceRemove(instance1)
503         finally:
504           instance2.Release()
505       finally:
506         instance1.Release()
507
508     if qa_config.TestEnabled("instance-add-drbd-disk"):
509       snode = qa_config.AcquireNode()
510       try:
511         qa_cluster.TestSetExclStorCluster(False)
512         instance = qa_instance.TestInstanceAddWithDrbdDisk([node, snode])
513         try:
514           qa_cluster.TestSetExclStorCluster(True)
515           exp_err = [constants.CV_EINSTANCEUNSUITABLENODE]
516           qa_cluster.AssertClusterVerify(fail=True, errors=exp_err)
517           qa_instance.TestInstanceRemove(instance)
518         finally:
519           instance.Release()
520       finally:
521         snode.Release()
522     qa_cluster.TestSetExclStorCluster(old_es)
523   finally:
524     node.Release()
525
526
527 def _BuildSpecDict(par, mn, st, mx):
528   return {
529     constants.ISPECS_MINMAX: [{
530       constants.ISPECS_MIN: {par: mn},
531       constants.ISPECS_MAX: {par: mx},
532       }],
533     constants.ISPECS_STD: {par: st},
534     }
535
536
537 def _BuildDoubleSpecDict(index, par, mn, st, mx):
538   new_spec = {
539     constants.ISPECS_MINMAX: [{}, {}],
540     }
541   if st is not None:
542     new_spec[constants.ISPECS_STD] = {par: st}
543   new_spec[constants.ISPECS_MINMAX][index] = {
544     constants.ISPECS_MIN: {par: mn},
545     constants.ISPECS_MAX: {par: mx},
546     }
547   return new_spec
548
549
550 def TestIPolicyPlainInstance():
551   """Test instance policy interaction with instances"""
552   params = ["memory-size", "cpu-count", "disk-count", "disk-size", "nic-count"]
553   if not qa_config.IsTemplateSupported(constants.DT_PLAIN):
554     print "Template %s not supported" % constants.DT_PLAIN
555     return
556
557   # This test assumes that the group policy is empty
558   (_, old_specs) = qa_cluster.TestClusterSetISpecs()
559   # We also assume to have only one min/max bound
560   assert len(old_specs[constants.ISPECS_MINMAX]) == 1
561   node = qa_config.AcquireNode()
562   try:
563     # Log of policy changes, list of tuples:
564     # (full_change, incremental_change, policy_violated)
565     history = []
566     instance = qa_instance.TestInstanceAddWithPlainDisk([node])
567     try:
568       policyerror = [constants.CV_EINSTANCEPOLICY]
569       for par in params:
570         (iminval, imaxval) = qa_instance.GetInstanceSpec(instance.name, par)
571         # Some specs must be multiple of 4
572         new_spec = _BuildSpecDict(par, imaxval + 4, imaxval + 4, imaxval + 4)
573         history.append((None, new_spec, True))
574         if iminval > 0:
575           # Some specs must be multiple of 4
576           if iminval >= 4:
577             upper = iminval - 4
578           else:
579             upper = iminval - 1
580           new_spec = _BuildSpecDict(par, 0, upper, upper)
581           history.append((None, new_spec, True))
582         history.append((old_specs, None, False))
583
584       # Test with two instance specs
585       double_specs = copy.deepcopy(old_specs)
586       double_specs[constants.ISPECS_MINMAX] = \
587           double_specs[constants.ISPECS_MINMAX] * 2
588       (par1, par2) = params[0:2]
589       (_, imaxval1) = qa_instance.GetInstanceSpec(instance.name, par1)
590       (_, imaxval2) = qa_instance.GetInstanceSpec(instance.name, par2)
591       old_minmax = old_specs[constants.ISPECS_MINMAX][0]
592       history.extend([
593         (double_specs, None, False),
594         # The first min/max limit is being violated
595         (None,
596          _BuildDoubleSpecDict(0, par1, imaxval1 + 4, imaxval1 + 4,
597                               imaxval1 + 4),
598          False),
599         # Both min/max limits are being violated
600         (None,
601          _BuildDoubleSpecDict(1, par2, imaxval2 + 4, None, imaxval2 + 4),
602          True),
603         # The second min/max limit is being violated
604         (None,
605          _BuildDoubleSpecDict(0, par1,
606                               old_minmax[constants.ISPECS_MIN][par1],
607                               old_specs[constants.ISPECS_STD][par1],
608                               old_minmax[constants.ISPECS_MAX][par1]),
609          False),
610         (old_specs, None, False),
611         ])
612
613       # Apply the changes, and check policy violations after each change
614       qa_cluster.AssertClusterVerify()
615       for (new_specs, diff_specs, failed) in history:
616         qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
617                                         diff_specs=diff_specs)
618         if failed:
619           qa_cluster.AssertClusterVerify(warnings=policyerror)
620         else:
621           qa_cluster.AssertClusterVerify()
622
623       qa_instance.TestInstanceRemove(instance)
624     finally:
625       instance.Release()
626
627     # Now we replay the same policy changes, and we expect that the instance
628     # cannot be created for the cases where we had a policy violation above
629     for (new_specs, diff_specs, failed) in history:
630       qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
631                                       diff_specs=diff_specs)
632       if failed:
633         qa_instance.TestInstanceAddWithPlainDisk([node], fail=True)
634       # Instance creation with no policy violation has been tested already
635   finally:
636     node.Release()
637
638
639 def IsExclusiveStorageInstanceTestEnabled():
640   test_name = "exclusive-storage-instance-tests"
641   if qa_config.TestEnabled(test_name):
642     vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
643     vgscmd = utils.ShellQuoteArgs([
644       "vgs", "--noheadings", "-o", "pv_count", vgname,
645       ])
646     nodes = qa_config.GetConfig()["nodes"]
647     for node in nodes:
648       try:
649         pvnum = int(qa_utils.GetCommandOutput(node.primary, vgscmd))
650       except Exception, e:
651         msg = ("Cannot get the number of PVs on %s, needed by '%s': %s" %
652                (node.primary, test_name, e))
653         raise qa_error.Error(msg)
654       if pvnum < 2:
655         raise qa_error.Error("Node %s has not enough PVs (%s) to run '%s'" %
656                              (node.primary, pvnum, test_name))
657     res = True
658   else:
659     res = False
660   return res
661
662
663 def RunInstanceTests():
664   """Create and exercise instances."""
665   instance_tests = [
666     ("instance-add-plain-disk", constants.DT_PLAIN,
667      qa_instance.TestInstanceAddWithPlainDisk, 1),
668     ("instance-add-drbd-disk", constants.DT_DRBD8,
669      qa_instance.TestInstanceAddWithDrbdDisk, 2),
670     ("instance-add-diskless", constants.DT_DISKLESS,
671      qa_instance.TestInstanceAddDiskless, 1),
672     ("instance-add-file", constants.DT_FILE,
673      qa_instance.TestInstanceAddFile, 1)
674     ]
675
676   for (test_name, templ, create_fun, num_nodes) in instance_tests:
677     if (qa_config.TestEnabled(test_name) and
678         qa_config.IsTemplateSupported(templ)):
679       inodes = qa_config.AcquireManyNodes(num_nodes)
680       try:
681         instance = RunTest(create_fun, inodes)
682         try:
683           RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
684           RunDaemonTests(instance)
685           for node in inodes:
686             RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, node)
687           if len(inodes) > 1:
688             RunTestIf("group-rwops", qa_group.TestAssignNodesIncludingSplit,
689                       constants.INITIAL_NODE_GROUP_NAME,
690                       inodes[0].primary, inodes[1].primary)
691           if qa_config.TestEnabled("instance-convert-disk"):
692             RunTest(qa_instance.TestInstanceShutdown, instance)
693             RunTest(qa_instance.TestInstanceConvertDiskToPlain,
694                     instance, inodes)
695             RunTest(qa_instance.TestInstanceStartup, instance)
696           RunTestIf("instance-modify-disks",
697                     qa_instance.TestInstanceModifyDisks, instance)
698           RunCommonInstanceTests(instance)
699           if qa_config.TestEnabled("instance-modify-primary"):
700             othernode = qa_config.AcquireNode()
701             RunTest(qa_instance.TestInstanceModifyPrimaryAndBack,
702                     instance, inodes[0], othernode)
703             othernode.Release()
704           RunGroupListTests()
705           RunExportImportTests(instance, inodes)
706           RunHardwareFailureTests(instance, inodes)
707           RunRepairDiskSizes()
708           RunTest(qa_instance.TestInstanceRemove, instance)
709         finally:
710           instance.Release()
711         del instance
712       finally:
713         qa_config.ReleaseManyNodes(inodes)
714       qa_cluster.AssertClusterVerify()
715
716
717 def RunMonitoringTests():
718   if qa_config.TestEnabled("mon-collector"):
719     RunTest(qa_monitoring.TestInstStatusCollector)
720
721
722 def RunQa():
723   """Main QA body.
724
725   """
726   rapi_user = "ganeti-qa"
727   rapi_secret = utils.GenerateSecret()
728
729   RunEnvTests()
730   SetupCluster(rapi_user, rapi_secret)
731
732   if qa_rapi.Enabled():
733     # Load RAPI certificate
734     qa_rapi.Setup(rapi_user, rapi_secret)
735
736   RunClusterTests()
737   RunOsTests()
738
739   RunTestIf("tags", qa_tags.TestClusterTags)
740
741   RunCommonNodeTests()
742   RunGroupListTests()
743   RunGroupRwTests()
744   RunNetworkTests()
745
746   # The master shouldn't be readded or put offline; "delay" needs a non-master
747   # node to test
748   pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
749   try:
750     RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
751     RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
752     RunTestIf("delay", qa_cluster.TestDelay, pnode)
753   finally:
754     pnode.Release()
755
756   # Make sure the cluster is clean before running instance tests
757   qa_cluster.AssertClusterVerify()
758
759   pnode = qa_config.AcquireNode()
760   try:
761     RunTestIf("tags", qa_tags.TestNodeTags, pnode)
762
763     if qa_rapi.Enabled():
764       RunTest(qa_rapi.TestNode, pnode)
765
766       if qa_config.TestEnabled("instance-add-plain-disk"):
767         for use_client in [True, False]:
768           rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
769                                   use_client)
770           try:
771             if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
772               RunCommonInstanceTests(rapi_instance)
773             RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
774           finally:
775             rapi_instance.Release()
776           del rapi_instance
777
778   finally:
779     pnode.Release()
780
781   config_list = [
782     ("default-instance-tests", lambda: None, lambda _: None),
783     (IsExclusiveStorageInstanceTestEnabled,
784      lambda: qa_cluster.TestSetExclStorCluster(True),
785      qa_cluster.TestSetExclStorCluster),
786   ]
787   for (conf_name, setup_conf_f, restore_conf_f) in config_list:
788     if qa_config.TestEnabled(conf_name):
789       oldconf = setup_conf_f()
790       RunInstanceTests()
791       restore_conf_f(oldconf)
792
793   pnode = qa_config.AcquireNode()
794   try:
795     if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
796       for shutdown in [False, True]:
797         instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, [pnode])
798         try:
799           expnode = qa_config.AcquireNode(exclude=pnode)
800           try:
801             if shutdown:
802               # Stop instance before exporting and removing it
803               RunTest(qa_instance.TestInstanceShutdown, instance)
804             RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
805             RunTest(qa_instance.TestBackupList, expnode)
806           finally:
807             expnode.Release()
808         finally:
809           instance.Release()
810         del expnode
811         del instance
812       qa_cluster.AssertClusterVerify()
813
814   finally:
815     pnode.Release()
816
817   RunExclusiveStorageTests()
818   RunTestIf(["cluster-instance-policy", "instance-add-plain-disk"],
819             TestIPolicyPlainInstance)
820
821   RunTestIf(
822     "instance-add-restricted-by-disktemplates",
823     qa_instance.TestInstanceCreationRestrictedByDiskTemplates)
824
825   # Test removing instance with offline drbd secondary
826   if qa_config.TestEnabled(["instance-remove-drbd-offline",
827                             "instance-add-drbd-disk"]):
828     # Make sure the master is not put offline
829     snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
830     try:
831       pnode = qa_config.AcquireNode(exclude=snode)
832       try:
833         instance = qa_instance.TestInstanceAddWithDrbdDisk([pnode, snode])
834         set_offline = lambda node: qa_node.MakeNodeOffline(node, "yes")
835         set_online = lambda node: qa_node.MakeNodeOffline(node, "no")
836         RunTest(qa_instance.TestRemoveInstanceOfflineNode, instance, snode,
837                 set_offline, set_online)
838       finally:
839         pnode.Release()
840     finally:
841       snode.Release()
842     qa_cluster.AssertClusterVerify()
843
844   RunMonitoringTests()
845
846   RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
847
848   RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
849
850
851 @UsesRapiClient
852 def main():
853   """Main program.
854
855   """
856   parser = optparse.OptionParser(usage="%prog [options] <config-file>")
857   parser.add_option("--yes-do-it", dest="yes_do_it",
858                     action="store_true",
859                     help="Really execute the tests")
860   (opts, args) = parser.parse_args()
861
862   if len(args) == 1:
863     (config_file, ) = args
864   else:
865     parser.error("Wrong number of arguments.")
866
867   if not opts.yes_do_it:
868     print ("Executing this script irreversibly destroys any Ganeti\n"
869            "configuration on all nodes involved. If you really want\n"
870            "to start testing, supply the --yes-do-it option.")
871     sys.exit(1)
872
873   qa_config.Load(config_file)
874
875   primary = qa_config.GetMasterNode().primary
876   qa_utils.StartMultiplexer(primary)
877   print ("SSH command for primary node: %s" %
878          utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
879   print ("SSH command for other nodes: %s" %
880          utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
881   try:
882     RunQa()
883   finally:
884     qa_utils.CloseMultiplexers()
885
886 if __name__ == "__main__":
887   main()