Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 3016bc1f

History | View | Annotate | Download (16.4 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_rapi
42
import qa_tags
43
import qa_utils
44

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

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

    
51

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

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

    
61

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

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

    
71
  return desc.rstrip(".")
72

    
73

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

77
  """
78

    
79
  tstart = datetime.datetime.now()
80

    
81
  desc = _DescriptionOf(fn)
82

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

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

    
94

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

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

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

    
110

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

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

    
119

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

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

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

    
130
  # Test on empty cluster
131
  RunTestIf("node-list", qa_node.TestNodeList)
132
  RunTestIf("instance-list", qa_instance.TestInstanceList)
133

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

    
139
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
140

    
141
  # enable the watcher (unconditionally)
142
  RunTest(qa_daemon.TestResumeWatcher)
143

    
144
  RunTestIf("node-list", qa_node.TestNodeList)
145

    
146
  # Test listing fields
147
  RunTestIf("node-list", qa_node.TestNodeListFields)
148
  RunTestIf("instance-list", qa_instance.TestInstanceListFields)
149

    
150
  RunTestIf("node-info", qa_node.TestNodeInfo)
151

    
152

    
153
def RunClusterTests():
154
  """Runs tests related to gnt-cluster.
155

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

    
184

    
185
def RunRepairDiskSizes():
186
  """Run the repair disk-sizes test.
187

188
  """
189
  RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
190

    
191

    
192
def RunOsTests():
193
  """Runs all tests related to gnt-os.
194

195
  """
196
  if qa_config.TestEnabled("rapi"):
197
    rapi_getos = qa_rapi.GetOperatingSystems
198
  else:
199
    rapi_getos = None
200

    
201
  for fn in [
202
    qa_os.TestOsList,
203
    qa_os.TestOsDiagnose,
204
    ]:
205
    RunTestIf("os", fn)
206

    
207
  for fn in [
208
    qa_os.TestOsValid,
209
    qa_os.TestOsInvalid,
210
    qa_os.TestOsPartiallyValid,
211
    ]:
212
    RunTestIf("os", fn, rapi_getos)
213

    
214
  for fn in [
215
    qa_os.TestOsModifyValid,
216
    qa_os.TestOsModifyInvalid,
217
    qa_os.TestOsStatesNonExisting,
218
    ]:
219
    RunTestIf("os", fn)
220

    
221

    
222
def RunCommonInstanceTests(instance):
223
  """Runs a few tests that are common to all disk types.
224

225
  """
226
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
227
  RunTestIf(["instance-shutdown", "instance-console", "rapi"],
228
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
229
  RunTestIf(["instance-shutdown", "instance-modify"],
230
            qa_instance.TestInstanceStoppedModify, instance)
231
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
232

    
233
  # Test shutdown/start via RAPI
234
  RunTestIf(["instance-shutdown", "rapi"],
235
            qa_rapi.TestRapiInstanceShutdown, instance)
236
  RunTestIf(["instance-shutdown", "rapi"],
237
            qa_rapi.TestRapiInstanceStartup, instance)
238

    
239
  RunTestIf("instance-list", qa_instance.TestInstanceList)
240

    
241
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
242

    
243
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
244
  RunTestIf(["instance-modify", "rapi"],
245
            qa_rapi.TestRapiInstanceModify, instance)
246

    
247
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
248
  RunTestIf(["instance-console", "rapi"],
249
            qa_rapi.TestRapiInstanceConsole, instance)
250

    
251
  DOWN_TESTS = ["instance-reinstall", "instance-rename"]
252
  # shutdown instance for any 'down' tests
253
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
254

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

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

    
279
  # and now start the instance again
280
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
281

    
282
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
283

    
284
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
285

    
286
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
287

    
288
  RunTestIf("rapi", qa_rapi.TestInstance, instance)
289

    
290
  # Lists instances, too
291
  RunTestIf("node-list", qa_node.TestNodeList)
292

    
293

    
294
def RunCommonNodeTests():
295
  """Run a few common node tests.
296

297
  """
298
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
299
  RunTestIf("node-storage", qa_node.TestNodeStorage)
300
  RunTestIf("node-oob", qa_node.TestOutOfBand)
301

    
302

    
303
def RunGroupListTests():
304
  """Run tests for listing node groups.
305

306
  """
307
  RunTestIf("group-list", qa_group.TestGroupList)
308
  RunTestIf("group-list", qa_group.TestGroupListFields)
309

    
310

    
311
def RunGroupRwTests():
312
  """Run tests for adding/removing/renaming groups.
313

314
  """
315
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
316
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
317
  RunTestIf("group-rwops", qa_group.TestGroupModify)
318
  RunTestIf(["group-rwops", "rapi"], qa_rapi.TestRapiNodeGroups)
319
  RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
320
            qa_group.GetDefaultGroup())
321

    
322

    
323
def RunExportImportTests(instance, pnode, snode):
324
  """Tries to export and import the instance.
325

326
  @param pnode: current primary node of the instance
327
  @param snode: current secondary node of the instance, if any,
328
      otherwise None
329

330
  """
331
  if qa_config.TestEnabled("instance-export"):
332
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
333

    
334
    expnode = qa_config.AcquireNode(exclude=pnode)
335
    try:
336
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
337

    
338
      RunTest(qa_instance.TestBackupList, expnode)
339

    
340
      if qa_config.TestEnabled("instance-import"):
341
        newinst = qa_config.AcquireInstance()
342
        try:
343
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
344
                  expnode, name)
345
          RunTest(qa_instance.TestInstanceRemove, newinst)
346
        finally:
347
          qa_config.ReleaseInstance(newinst)
348
    finally:
349
      qa_config.ReleaseNode(expnode)
350

    
351
  if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
352
    newinst = qa_config.AcquireInstance()
353
    try:
354
      if snode is None:
355
        excl = [pnode]
356
      else:
357
        excl = [pnode, snode]
358
      tnode = qa_config.AcquireNode(exclude=excl)
359
      try:
360
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
361
                pnode, snode, tnode)
362
      finally:
363
        qa_config.ReleaseNode(tnode)
364
    finally:
365
      qa_config.ReleaseInstance(newinst)
366

    
367

    
368
def RunDaemonTests(instance):
369
  """Test the ganeti-watcher script.
370

371
  """
372
  RunTest(qa_daemon.TestPauseWatcher)
373

    
374
  RunTestIf("instance-automatic-restart",
375
            qa_daemon.TestInstanceAutomaticRestart, instance)
376
  RunTestIf("instance-consecutive-failures",
377
            qa_daemon.TestInstanceConsecutiveFailures, instance)
378

    
379
  RunTest(qa_daemon.TestResumeWatcher)
380

    
381

    
382
def RunHardwareFailureTests(instance, pnode, snode):
383
  """Test cluster internal hardware failure recovery.
384

385
  """
386
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
387
  RunTestIf(["instance-failover", "rapi"],
388
            qa_rapi.TestRapiInstanceFailover, instance)
389

    
390
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
391
  RunTestIf(["instance-migrate", "rapi"],
392
            qa_rapi.TestRapiInstanceMigrate, instance)
393

    
394
  if qa_config.TestEnabled("instance-replace-disks"):
395
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
396
    try:
397
      RunTestIf("rapi", qa_rapi.TestRapiInstanceReplaceDisks, instance)
398
      RunTest(qa_instance.TestReplaceDisks,
399
              instance, pnode, snode, othernode)
400
    finally:
401
      qa_config.ReleaseNode(othernode)
402

    
403
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
404

    
405
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
406

    
407
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
408
            instance, pnode, snode)
409
  RunTestIf("instance-disk-failure",
410
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
411
            pnode, snode)
412

    
413

    
414
def RunQa():
415
  """Main QA body.
416

417
  """
418
  rapi_user = "ganeti-qa"
419
  rapi_secret = utils.GenerateSecret()
420

    
421
  RunEnvTests()
422
  SetupCluster(rapi_user, rapi_secret)
423

    
424
  # Load RAPI certificate
425
  qa_rapi.Setup(rapi_user, rapi_secret)
426

    
427
  RunClusterTests()
428
  RunOsTests()
429

    
430
  RunTestIf("tags", qa_tags.TestClusterTags)
431

    
432
  RunCommonNodeTests()
433
  RunGroupListTests()
434
  RunGroupRwTests()
435

    
436
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
437
  try:
438
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
439
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
440
    RunTestIf("delay", qa_cluster.TestDelay, pnode)
441
  finally:
442
    qa_config.ReleaseNode(pnode)
443

    
444
  pnode = qa_config.AcquireNode()
445
  try:
446
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
447

    
448
    if qa_rapi.Enabled():
449
      RunTest(qa_rapi.TestNode, pnode)
450

    
451
      if qa_config.TestEnabled("instance-add-plain-disk"):
452
        for use_client in [True, False]:
453
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
454
                                  use_client)
455
          RunCommonInstanceTests(rapi_instance)
456
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
457
          del rapi_instance
458

    
459
    if qa_config.TestEnabled("instance-add-plain-disk"):
460
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
461
      RunCommonInstanceTests(instance)
462
      RunGroupListTests()
463
      RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
464
      RunExportImportTests(instance, pnode, None)
465
      RunDaemonTests(instance)
466
      RunRepairDiskSizes()
467
      RunTest(qa_instance.TestInstanceRemove, instance)
468
      del instance
469

    
470
    multinode_tests = [
471
      ("instance-add-drbd-disk",
472
       qa_instance.TestInstanceAddWithDrbdDisk),
473
    ]
474

    
475
    for name, func in multinode_tests:
476
      if qa_config.TestEnabled(name):
477
        snode = qa_config.AcquireNode(exclude=pnode)
478
        try:
479
          instance = RunTest(func, pnode, snode)
480
          RunCommonInstanceTests(instance)
481
          RunGroupListTests()
482
          RunTest(qa_group.TestAssignNodesIncludingSplit,
483
                  constants.INITIAL_NODE_GROUP_NAME,
484
                  pnode["primary"], snode["primary"])
485
          if qa_config.TestEnabled("instance-convert-disk"):
486
            RunTest(qa_instance.TestInstanceShutdown, instance)
487
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
488
            RunTest(qa_instance.TestInstanceStartup, instance)
489
          RunExportImportTests(instance, pnode, snode)
490
          RunHardwareFailureTests(instance, pnode, snode)
491
          RunRepairDiskSizes()
492
          RunTest(qa_instance.TestInstanceRemove, instance)
493
          del instance
494
        finally:
495
          qa_config.ReleaseNode(snode)
496

    
497
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
498
      for shutdown in [False, True]:
499
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
500
        expnode = qa_config.AcquireNode(exclude=pnode)
501
        try:
502
          if shutdown:
503
            # Stop instance before exporting and removing it
504
            RunTest(qa_instance.TestInstanceShutdown, instance)
505
          RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
506
          RunTest(qa_instance.TestBackupList, expnode)
507
        finally:
508
          qa_config.ReleaseNode(expnode)
509
        del expnode
510
        del instance
511

    
512
  finally:
513
    qa_config.ReleaseNode(pnode)
514

    
515
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
516

    
517
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
518

    
519

    
520
@rapi.client.UsesRapiClient
521
def main():
522
  """Main program.
523

524
  """
525
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
526
  parser.add_option("--yes-do-it", dest="yes_do_it",
527
      action="store_true",
528
      help="Really execute the tests")
529
  (qa_config.options, args) = parser.parse_args()
530

    
531
  if len(args) == 1:
532
    (config_file, ) = args
533
  else:
534
    parser.error("Wrong number of arguments.")
535

    
536
  if not qa_config.options.yes_do_it:
537
    print ("Executing this script irreversibly destroys any Ganeti\n"
538
           "configuration on all nodes involved. If you really want\n"
539
           "to start testing, supply the --yes-do-it option.")
540
    sys.exit(1)
541

    
542
  qa_config.Load(config_file)
543

    
544
  qa_utils.StartMultiplexer(qa_config.GetMasterNode()["primary"])
545
  try:
546
    RunQa()
547
  finally:
548
    qa_utils.CloseMultiplexers()
549

    
550
if __name__ == "__main__":
551
  main()