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