Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 26a5056d

History | View | Annotate | Download (16.7 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):
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)
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):
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)
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 = ["instance-reinstall", "instance-rename", "instance-grow-disk"]
256
  # shutdown instance for any 'down' tests
257
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
258

    
259
  # now run the 'down' state tests
260
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
261
  RunTestIf(["instance-reinstall", "rapi"],
262
            qa_rapi.TestRapiInstanceReinstall, instance)
263
  # RAPI reinstall will leave the instance up by default, so we have
264
  # to stop it again
265
  RunTestIf(["instance-reinstall", "rapi"],
266
            qa_rapi.TestRapiInstanceShutdown, instance)
267

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

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

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

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

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

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

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

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

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

    
302

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

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

    
311

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

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

    
319

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

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

    
331

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

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

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

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

    
347
      RunTest(qa_instance.TestBackupList, expnode)
348

    
349
      if qa_config.TestEnabled("instance-import"):
350
        newinst = qa_config.AcquireInstance()
351
        try:
352
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
353
                  expnode, name)
354
          RunTest(qa_instance.TestInstanceRemove, newinst)
355
        finally:
356
          qa_config.ReleaseInstance(newinst)
357
    finally:
358
      qa_config.ReleaseNode(expnode)
359

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

    
376

    
377
def RunDaemonTests(instance):
378
  """Test the ganeti-watcher script.
379

380
  """
381
  RunTest(qa_daemon.TestPauseWatcher)
382

    
383
  RunTestIf("instance-automatic-restart",
384
            qa_daemon.TestInstanceAutomaticRestart, instance)
385
  RunTestIf("instance-consecutive-failures",
386
            qa_daemon.TestInstanceConsecutiveFailures, instance)
387

    
388
  RunTest(qa_daemon.TestResumeWatcher)
389

    
390

    
391
def RunHardwareFailureTests(instance, pnode, snode):
392
  """Test cluster internal hardware failure recovery.
393

394
  """
395
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
396
  RunTestIf(["instance-failover", "rapi"],
397
            qa_rapi.TestRapiInstanceFailover, instance)
398

    
399
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
400
  RunTestIf(["instance-migrate", "rapi"],
401
            qa_rapi.TestRapiInstanceMigrate, instance)
402

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

    
412
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
413

    
414
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
415

    
416
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
417
            instance, pnode, snode)
418
  RunTestIf("instance-disk-failure",
419
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
420
            pnode, snode)
421

    
422

    
423
def RunQa():
424
  """Main QA body.
425

426
  """
427
  rapi_user = "ganeti-qa"
428
  rapi_secret = utils.GenerateSecret()
429

    
430
  RunEnvTests()
431
  SetupCluster(rapi_user, rapi_secret)
432

    
433
  # Load RAPI certificate
434
  qa_rapi.Setup(rapi_user, rapi_secret)
435

    
436
  RunClusterTests()
437
  RunOsTests()
438

    
439
  RunTestIf("tags", qa_tags.TestClusterTags)
440

    
441
  RunCommonNodeTests()
442
  RunGroupListTests()
443
  RunGroupRwTests()
444

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

    
453
  pnode = qa_config.AcquireNode()
454
  try:
455
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
456

    
457
    if qa_rapi.Enabled():
458
      RunTest(qa_rapi.TestNode, pnode)
459

    
460
      if qa_config.TestEnabled("instance-add-plain-disk"):
461
        for use_client in [True, False]:
462
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
463
                                  use_client)
464
          RunCommonInstanceTests(rapi_instance)
465
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
466
          del rapi_instance
467

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

    
479
    multinode_tests = [
480
      ("instance-add-drbd-disk",
481
       qa_instance.TestInstanceAddWithDrbdDisk),
482
    ]
483

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

    
506
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
507
      for shutdown in [False, True]:
508
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
509
        expnode = qa_config.AcquireNode(exclude=pnode)
510
        try:
511
          if shutdown:
512
            # Stop instance before exporting and removing it
513
            RunTest(qa_instance.TestInstanceShutdown, instance)
514
          RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
515
          RunTest(qa_instance.TestBackupList, expnode)
516
        finally:
517
          qa_config.ReleaseNode(expnode)
518
        del expnode
519
        del instance
520

    
521
  finally:
522
    qa_config.ReleaseNode(pnode)
523

    
524
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
525

    
526
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
527

    
528

    
529
@rapi.client.UsesRapiClient
530
def main():
531
  """Main program.
532

533
  """
534
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
535
  parser.add_option("--yes-do-it", dest="yes_do_it",
536
      action="store_true",
537
      help="Really execute the tests")
538
  (qa_config.options, args) = parser.parse_args()
539

    
540
  if len(args) == 1:
541
    (config_file, ) = args
542
  else:
543
    parser.error("Wrong number of arguments.")
544

    
545
  if not qa_config.options.yes_do_it:
546
    print ("Executing this script irreversibly destroys any Ganeti\n"
547
           "configuration on all nodes involved. If you really want\n"
548
           "to start testing, supply the --yes-do-it option.")
549
    sys.exit(1)
550

    
551
  qa_config.Load(config_file)
552

    
553
  qa_utils.StartMultiplexer(qa_config.GetMasterNode()["primary"])
554
  try:
555
    RunQa()
556
  finally:
557
    qa_utils.CloseMultiplexers()
558

    
559
if __name__ == "__main__":
560
  main()