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