Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ c7e54e1d

History | View | Annotate | Download (17.5 kB)

1
#!/usr/bin/python -u
2
#
3

    
4
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Script for doing QA on Ganeti.
23

24
"""
25

    
26
# pylint: disable=C0103
27
# due to invalid name
28

    
29
import sys
30
import datetime
31
import optparse
32

    
33
import qa_cluster
34
import qa_config
35
import qa_daemon
36
import qa_env
37
import qa_group
38
import qa_instance
39
import qa_node
40
import qa_os
41
import qa_job
42
import qa_rapi
43
import qa_tags
44
import qa_utils
45

    
46
from ganeti import utils
47
from ganeti import rapi
48
from ganeti import constants
49

    
50
import ganeti.rapi.client # pylint: disable=W0611
51

    
52

    
53
def _FormatHeader(line, end=72):
54
  """Fill a line up to the end column.
55

56
  """
57
  line = "---- " + line + " "
58
  line += "-" * (end - len(line))
59
  line = line.rstrip()
60
  return line
61

    
62

    
63
def _DescriptionOf(fn):
64
  """Computes the description of an item.
65

66
  """
67
  if fn.__doc__:
68
    desc = fn.__doc__.splitlines()[0].strip()
69
  else:
70
    desc = "%r" % fn
71

    
72
  return desc.rstrip(".")
73

    
74

    
75
def RunTest(fn, *args, **kwargs):
76
  """Runs a test after printing a header.
77

78
  """
79

    
80
  tstart = datetime.datetime.now()
81

    
82
  desc = _DescriptionOf(fn)
83

    
84
  print
85
  print _FormatHeader("%s start %s" % (tstart, desc))
86

    
87
  try:
88
    retval = fn(*args, **kwargs)
89
    return retval
90
  finally:
91
    tstop = datetime.datetime.now()
92
    tdelta = tstop - tstart
93
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
94

    
95

    
96
def RunTestIf(testnames, fn, *args, **kwargs):
97
  """Runs a test conditionally.
98

99
  @param testnames: either a single test name in the configuration
100
      file, or a list of testnames (which will be AND-ed together)
101

102
  """
103
  if qa_config.TestEnabled(testnames):
104
    RunTest(fn, *args, **kwargs)
105
  else:
106
    tstart = datetime.datetime.now()
107
    desc = _DescriptionOf(fn)
108
    print _FormatHeader("%s skipping %s, test(s) %s disabled" %
109
                        (tstart, desc, testnames))
110

    
111

    
112
def RunEnvTests():
113
  """Run several environment tests.
114

115
  """
116
  RunTestIf("env", qa_env.TestSshConnection)
117
  RunTestIf("env", qa_env.TestIcmpPing)
118
  RunTestIf("env", qa_env.TestGanetiCommands)
119

    
120

    
121
def SetupCluster(rapi_user, rapi_secret):
122
  """Initializes the cluster.
123

124
  @param rapi_user: Login user for RAPI
125
  @param rapi_secret: Login secret for RAPI
126

127
  """
128
  RunTestIf("create-cluster", qa_cluster.TestClusterInit,
129
            rapi_user, rapi_secret)
130

    
131
  # Test on empty cluster
132
  RunTestIf("node-list", qa_node.TestNodeList)
133
  RunTestIf("instance-list", qa_instance.TestInstanceList)
134
  RunTestIf("job-list", qa_job.TestJobList)
135

    
136
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
137
  if not qa_config.TestEnabled("create-cluster"):
138
    # consider the nodes are already there
139
    qa_node.MarkNodeAddedAll()
140

    
141
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
142

    
143
  # enable the watcher (unconditionally)
144
  RunTest(qa_daemon.TestResumeWatcher)
145

    
146
  RunTestIf("node-list", qa_node.TestNodeList)
147

    
148
  # Test listing fields
149
  RunTestIf("node-list", qa_node.TestNodeListFields)
150
  RunTestIf("instance-list", qa_instance.TestInstanceListFields)
151
  RunTestIf("job-list", qa_job.TestJobListFields)
152
  RunTestIf("instance-export", qa_instance.TestBackupListFields)
153

    
154
  RunTestIf("node-info", qa_node.TestNodeInfo)
155

    
156

    
157
def RunClusterTests():
158
  """Runs tests related to gnt-cluster.
159

160
  """
161
  for test, fn in [
162
    ("create-cluster", qa_cluster.TestClusterInitDisk),
163
    ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
164
    ("cluster-verify", qa_cluster.TestClusterVerify),
165
    ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
166
    # TODO: add more cluster modify tests
167
    ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
168
    ("cluster-modify", qa_cluster.TestClusterModifyBe),
169
    ("cluster-modify", qa_cluster.TestClusterModifyDisk),
170
    ("cluster-rename", qa_cluster.TestClusterRename),
171
    ("cluster-info", qa_cluster.TestClusterVersion),
172
    ("cluster-info", qa_cluster.TestClusterInfo),
173
    ("cluster-info", qa_cluster.TestClusterGetmaster),
174
    ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
175
    ("cluster-copyfile", qa_cluster.TestClusterCopyfile),
176
    ("cluster-command", qa_cluster.TestClusterCommand),
177
    ("cluster-burnin", qa_cluster.TestClusterBurnin),
178
    ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
179
    ("cluster-master-failover",
180
     qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
181
    ("cluster-oob", qa_cluster.TestClusterOob),
182
    ("rapi", qa_rapi.TestVersion),
183
    ("rapi", qa_rapi.TestEmptyCluster),
184
    ("rapi", qa_rapi.TestRapiQuery),
185
    ]:
186
    RunTestIf(test, fn)
187

    
188

    
189
def RunRepairDiskSizes():
190
  """Run the repair disk-sizes test.
191

192
  """
193
  RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
194

    
195

    
196
def RunOsTests():
197
  """Runs all tests related to gnt-os.
198

199
  """
200
  if qa_config.TestEnabled("rapi"):
201
    rapi_getos = qa_rapi.GetOperatingSystems
202
  else:
203
    rapi_getos = None
204

    
205
  for fn in [
206
    qa_os.TestOsList,
207
    qa_os.TestOsDiagnose,
208
    ]:
209
    RunTestIf("os", fn)
210

    
211
  for fn in [
212
    qa_os.TestOsValid,
213
    qa_os.TestOsInvalid,
214
    qa_os.TestOsPartiallyValid,
215
    ]:
216
    RunTestIf("os", fn, rapi_getos)
217

    
218
  for fn in [
219
    qa_os.TestOsModifyValid,
220
    qa_os.TestOsModifyInvalid,
221
    qa_os.TestOsStatesNonExisting,
222
    ]:
223
    RunTestIf("os", fn)
224

    
225

    
226
def RunCommonInstanceTests(instance):
227
  """Runs a few tests that are common to all disk types.
228

229
  """
230
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
231
  RunTestIf(["instance-shutdown", "instance-console", "rapi"],
232
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
233
  RunTestIf(["instance-shutdown", "instance-modify"],
234
            qa_instance.TestInstanceStoppedModify, instance)
235
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
236

    
237
  # Test shutdown/start via RAPI
238
  RunTestIf(["instance-shutdown", "rapi"],
239
            qa_rapi.TestRapiInstanceShutdown, instance)
240
  RunTestIf(["instance-shutdown", "rapi"],
241
            qa_rapi.TestRapiInstanceStartup, instance)
242

    
243
  RunTestIf("instance-list", qa_instance.TestInstanceList)
244

    
245
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
246

    
247
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
248
  RunTestIf(["instance-modify", "rapi"],
249
            qa_rapi.TestRapiInstanceModify, instance)
250

    
251
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
252
  RunTestIf(["instance-console", "rapi"],
253
            qa_rapi.TestRapiInstanceConsole, instance)
254

    
255
  DOWN_TESTS = qa_config.Either([
256
    "instance-reinstall",
257
    "instance-rename",
258
    "instance-grow-disk",
259
    ])
260

    
261
  # shutdown instance for any 'down' tests
262
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
263

    
264
  # now run the 'down' state tests
265
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
266
  RunTestIf(["instance-reinstall", "rapi"],
267
            qa_rapi.TestRapiInstanceReinstall, instance)
268

    
269
  if qa_config.TestEnabled("instance-rename"):
270
    rename_source = instance["name"]
271
    rename_target = qa_config.get("rename", None)
272
    # perform instance rename to the same name
273
    RunTest(qa_instance.TestInstanceRenameAndBack,
274
            rename_source, rename_source)
275
    RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
276
              rename_source, rename_source)
277
    if rename_target is not None:
278
      # perform instance rename to a different name, if we have one configured
279
      RunTest(qa_instance.TestInstanceRenameAndBack,
280
              rename_source, rename_target)
281
      RunTestIf("rapi", qa_rapi.TestRapiInstanceRenameAndBack,
282
                rename_source, rename_target)
283

    
284
  RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
285

    
286
  # and now start the instance again
287
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
288

    
289
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
290

    
291
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
292

    
293
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
294

    
295
  RunTestIf("rapi", qa_rapi.TestInstance, instance)
296

    
297
  # Lists instances, too
298
  RunTestIf("node-list", qa_node.TestNodeList)
299

    
300
  # Some jobs have been run, let's test listing them
301
  RunTestIf("job-list", qa_job.TestJobList)
302

    
303

    
304
def RunCommonNodeTests():
305
  """Run a few common node tests.
306

307
  """
308
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
309
  RunTestIf("node-storage", qa_node.TestNodeStorage)
310
  RunTestIf("node-oob", qa_node.TestOutOfBand)
311

    
312

    
313
def RunGroupListTests():
314
  """Run tests for listing node groups.
315

316
  """
317
  RunTestIf("group-list", qa_group.TestGroupList)
318
  RunTestIf("group-list", qa_group.TestGroupListFields)
319

    
320

    
321
def RunGroupRwTests():
322
  """Run tests for adding/removing/renaming groups.
323

324
  """
325
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
326
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
327
  RunTestIf("group-rwops", qa_group.TestGroupModify)
328
  RunTestIf(["group-rwops", "rapi"], qa_rapi.TestRapiNodeGroups)
329
  RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
330
            qa_group.GetDefaultGroup())
331

    
332

    
333
def RunExportImportTests(instance, pnode, snode):
334
  """Tries to export and import the instance.
335

336
  @param pnode: current primary node of the instance
337
  @param snode: current secondary node of the instance, if any,
338
      otherwise None
339

340
  """
341
  if qa_config.TestEnabled("instance-export"):
342
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
343

    
344
    expnode = qa_config.AcquireNode(exclude=pnode)
345
    try:
346
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
347

    
348
      RunTest(qa_instance.TestBackupList, expnode)
349

    
350
      if qa_config.TestEnabled("instance-import"):
351
        newinst = qa_config.AcquireInstance()
352
        try:
353
          RunTest(qa_instance.TestInstanceImport, newinst, pnode,
354
                  expnode, name)
355
          # Check if starting the instance works
356
          RunTest(qa_instance.TestInstanceStartup, newinst)
357
          RunTest(qa_instance.TestInstanceRemove, newinst)
358
        finally:
359
          qa_config.ReleaseInstance(newinst)
360
    finally:
361
      qa_config.ReleaseNode(expnode)
362

    
363
  if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
364
    newinst = qa_config.AcquireInstance()
365
    try:
366
      if snode is None:
367
        excl = [pnode]
368
      else:
369
        excl = [pnode, snode]
370
      tnode = qa_config.AcquireNode(exclude=excl)
371
      try:
372
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
373
                pnode, snode, tnode)
374
      finally:
375
        qa_config.ReleaseNode(tnode)
376
    finally:
377
      qa_config.ReleaseInstance(newinst)
378

    
379

    
380
def RunDaemonTests(instance):
381
  """Test the ganeti-watcher script.
382

383
  """
384
  RunTest(qa_daemon.TestPauseWatcher)
385

    
386
  RunTestIf("instance-automatic-restart",
387
            qa_daemon.TestInstanceAutomaticRestart, instance)
388
  RunTestIf("instance-consecutive-failures",
389
            qa_daemon.TestInstanceConsecutiveFailures, instance)
390

    
391
  RunTest(qa_daemon.TestResumeWatcher)
392

    
393

    
394
def RunHardwareFailureTests(instance, pnode, snode):
395
  """Test cluster internal hardware failure recovery.
396

397
  """
398
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
399
  RunTestIf(["instance-failover", "rapi"],
400
            qa_rapi.TestRapiInstanceFailover, instance)
401

    
402
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
403
  RunTestIf(["instance-migrate", "rapi"],
404
            qa_rapi.TestRapiInstanceMigrate, instance)
405

    
406
  if qa_config.TestEnabled("instance-replace-disks"):
407
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
408
    try:
409
      RunTestIf("rapi", qa_rapi.TestRapiInstanceReplaceDisks, instance)
410
      RunTest(qa_instance.TestReplaceDisks,
411
              instance, pnode, snode, othernode)
412
    finally:
413
      qa_config.ReleaseNode(othernode)
414

    
415
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
416

    
417
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
418

    
419
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
420
            instance, pnode, snode)
421
  RunTestIf("instance-disk-failure",
422
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
423
            pnode, snode)
424

    
425

    
426
def RunQa():
427
  """Main QA body.
428

429
  """
430
  rapi_user = "ganeti-qa"
431
  rapi_secret = utils.GenerateSecret()
432

    
433
  RunEnvTests()
434
  SetupCluster(rapi_user, rapi_secret)
435

    
436
  # Load RAPI certificate
437
  qa_rapi.Setup(rapi_user, rapi_secret)
438

    
439
  RunClusterTests()
440
  RunOsTests()
441

    
442
  RunTestIf("tags", qa_tags.TestClusterTags)
443

    
444
  RunCommonNodeTests()
445
  RunGroupListTests()
446
  RunGroupRwTests()
447

    
448
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
449
  try:
450
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
451
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
452
    RunTestIf("delay", qa_cluster.TestDelay, pnode)
453
  finally:
454
    qa_config.ReleaseNode(pnode)
455

    
456
  pnode = qa_config.AcquireNode()
457
  try:
458
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
459

    
460
    if qa_rapi.Enabled():
461
      RunTest(qa_rapi.TestNode, pnode)
462

    
463
      if qa_config.TestEnabled("instance-add-plain-disk"):
464
        for use_client in [True, False]:
465
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
466
                                  use_client)
467
          if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
468
            RunCommonInstanceTests(rapi_instance)
469
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
470
          del rapi_instance
471

    
472
    if qa_config.TestEnabled("instance-add-plain-disk"):
473
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
474
      RunCommonInstanceTests(instance)
475
      RunGroupListTests()
476
      RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
477
      RunExportImportTests(instance, pnode, None)
478
      RunDaemonTests(instance)
479
      RunRepairDiskSizes()
480
      RunTest(qa_instance.TestInstanceRemove, instance)
481
      del instance
482

    
483
    multinode_tests = [
484
      ("instance-add-drbd-disk",
485
       qa_instance.TestInstanceAddWithDrbdDisk),
486
    ]
487

    
488
    for name, func in multinode_tests:
489
      if qa_config.TestEnabled(name):
490
        snode = qa_config.AcquireNode(exclude=pnode)
491
        try:
492
          instance = RunTest(func, pnode, snode)
493
          RunCommonInstanceTests(instance)
494
          RunGroupListTests()
495
          RunTest(qa_group.TestAssignNodesIncludingSplit,
496
                  constants.INITIAL_NODE_GROUP_NAME,
497
                  pnode["primary"], snode["primary"])
498
          if qa_config.TestEnabled("instance-convert-disk"):
499
            RunTest(qa_instance.TestInstanceShutdown, instance)
500
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
501
            RunTest(qa_instance.TestInstanceStartup, instance)
502
          RunExportImportTests(instance, pnode, snode)
503
          RunHardwareFailureTests(instance, pnode, snode)
504
          RunRepairDiskSizes()
505
          RunTest(qa_instance.TestInstanceRemove, instance)
506
          del instance
507
        finally:
508
          qa_config.ReleaseNode(snode)
509

    
510
    # Test removing instance with offline drbd secondary
511
    if qa_config.TestEnabled("instance-remove-drbd-offline"):
512
      snode = qa_config.AcquireNode(exclude=pnode)
513
      instance = \
514
        qa_instance.TestInstanceAddWithDrbdDisk(pnode, snode)
515
      try:
516
        qa_node.MakeNodeOffline(snode, "yes")
517
        RunTest(qa_instance.TestInstanceRemove, instance)
518
      finally:
519
        qa_node.MakeNodeOffline(snode, "no")
520
        qa_config.ReleaseNode(snode)
521

    
522
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
523
      for shutdown in [False, True]:
524
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
525
        expnode = qa_config.AcquireNode(exclude=pnode)
526
        try:
527
          if shutdown:
528
            # Stop instance before exporting and removing it
529
            RunTest(qa_instance.TestInstanceShutdown, instance)
530
          RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
531
          RunTest(qa_instance.TestBackupList, expnode)
532
        finally:
533
          qa_config.ReleaseNode(expnode)
534
        del expnode
535
        del instance
536

    
537
  finally:
538
    qa_config.ReleaseNode(pnode)
539

    
540
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
541

    
542
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
543

    
544

    
545
@rapi.client.UsesRapiClient
546
def main():
547
  """Main program.
548

549
  """
550
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
551
  parser.add_option("--yes-do-it", dest="yes_do_it",
552
      action="store_true",
553
      help="Really execute the tests")
554
  (qa_config.options, args) = parser.parse_args()
555

    
556
  if len(args) == 1:
557
    (config_file, ) = args
558
  else:
559
    parser.error("Wrong number of arguments.")
560

    
561
  if not qa_config.options.yes_do_it:
562
    print ("Executing this script irreversibly destroys any Ganeti\n"
563
           "configuration on all nodes involved. If you really want\n"
564
           "to start testing, supply the --yes-do-it option.")
565
    sys.exit(1)
566

    
567
  qa_config.Load(config_file)
568

    
569
  primary = qa_config.GetMasterNode()["primary"]
570
  qa_utils.StartMultiplexer(primary)
571
  print ("SSH command for primary node: %s" %
572
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
573
  print ("SSH command for other nodes: %s" %
574
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
575
  try:
576
    RunQa()
577
  finally:
578
    qa_utils.CloseMultiplexers()
579

    
580
if __name__ == "__main__":
581
  main()