Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ ff699aa9

History | View | Annotate | Download (14.9 kB)

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

    
4
# Copyright (C) 2007, 2008, 2009, 2010, 2011 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-msg=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-msg=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
def RunTest(fn, *args):
74
  """Runs a test after printing a header.
75

76
  """
77

    
78
  tstart = datetime.datetime.now()
79

    
80
  desc = _DescriptionOf(fn)
81

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

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

    
93

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

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

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

    
109

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

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

    
118

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

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

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

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

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

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

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

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

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

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

    
151

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

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

    
178

    
179
def RunOsTests():
180
  """Runs all tests related to gnt-os.
181

182
  """
183
  for fn in [
184
    qa_os.TestOsList,
185
    qa_os.TestOsDiagnose,
186
    qa_os.TestOsValid,
187
    qa_os.TestOsInvalid,
188
    qa_os.TestOsPartiallyValid,
189
    qa_os.TestOsModifyValid,
190
    qa_os.TestOsModifyInvalid,
191
    qa_os.TestOsStates,
192
    ]:
193
    RunTestIf("os", fn)
194

    
195

    
196
def RunCommonInstanceTests(instance):
197
  """Runs a few tests that are common to all disk types.
198

199
  """
200
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
201
  RunTestIf(["instance-shutdown", "instance-console", "rapi"],
202
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
203
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
204

    
205
  RunTestIf("instance-list", qa_instance.TestInstanceList)
206

    
207
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
208

    
209
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
210
  RunTestIf(["instance-modify", "rapi"],
211
            qa_rapi.TestRapiInstanceModify, instance)
212

    
213
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
214
  RunTestIf(["instance-console", "rapi"],
215
            qa_rapi.TestRapiInstanceConsole, instance)
216

    
217
  RunTestIf("instance-reinstall", qa_instance.TestInstanceShutdown, instance)
218
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
219
  RunTestIf(["instance-reinstall", "rapi"],
220
            qa_rapi.TestRapiInstanceReinstall, instance)
221
  RunTestIf("instance-reinstall", qa_instance.TestInstanceStartup, instance)
222

    
223
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
224

    
225
  if qa_config.TestEnabled('instance-rename'):
226
    rename_source = instance["name"]
227
    rename_target = qa_config.get("rename", None)
228
    RunTest(qa_instance.TestInstanceShutdown, instance)
229
    # perform instance rename to the same name
230
    RunTest(qa_instance.TestInstanceRename, rename_source, rename_source)
231
    RunTestIf("rapi", qa_rapi.TestRapiInstanceRename,
232
              rename_source, rename_source)
233
    if rename_target is not None:
234
      # perform instance rename to a different name, if we have one configured
235
      RunTest(qa_instance.TestInstanceRename, rename_source, rename_target)
236
      RunTest(qa_instance.TestInstanceRename, rename_target, rename_source)
237
      RunTestIf("rapi", qa_rapi.TestRapiInstanceRename,
238
                rename_source, rename_target)
239
      RunTestIf("rapi", qa_rapi.TestRapiInstanceRename,
240
                rename_target, rename_source)
241
    RunTest(qa_instance.TestInstanceStartup, instance)
242

    
243
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
244

    
245
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
246

    
247
  RunTestIf("rapi", qa_rapi.TestInstance, instance)
248

    
249
  # Lists instances, too
250
  RunTestIf("node-list", qa_node.TestNodeList)
251

    
252

    
253
def RunCommonNodeTests():
254
  """Run a few common node tests.
255

256
  """
257
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
258
  RunTestIf("node-storage", qa_node.TestNodeStorage)
259
  RunTestIf("node-oob", qa_node.TestOutOfBand)
260

    
261

    
262
def RunGroupListTests():
263
  """Run tests for listing node groups.
264

265
  """
266
  RunTestIf("group-list", qa_group.TestGroupList)
267
  RunTestIf("group-list", qa_group.TestGroupListFields)
268

    
269

    
270
def RunGroupRwTests():
271
  """Run tests for adding/removing/renaming groups.
272

273
  """
274
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
275
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
276
  RunTestIf("group-rwops", qa_group.TestGroupModify)
277
  RunTestIf(["group-rwops", "rapi"], qa_rapi.TestRapiNodeGroups)
278

    
279

    
280
def RunExportImportTests(instance, pnode, snode):
281
  """Tries to export and import the instance.
282

283
  @param pnode: current primary node of the instance
284
  @param snode: current secondary node of the instance, if any,
285
      otherwise None
286

287
  """
288
  if qa_config.TestEnabled('instance-export'):
289
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
290

    
291
    expnode = qa_config.AcquireNode(exclude=pnode)
292
    try:
293
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
294

    
295
      RunTest(qa_instance.TestBackupList, expnode)
296

    
297
      if qa_config.TestEnabled('instance-import'):
298
        newinst = qa_config.AcquireInstance()
299
        try:
300
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
301
                  expnode, name)
302
          RunTest(qa_instance.TestInstanceRemove, newinst)
303
        finally:
304
          qa_config.ReleaseInstance(newinst)
305
    finally:
306
      qa_config.ReleaseNode(expnode)
307

    
308
  if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
309
    newinst = qa_config.AcquireInstance()
310
    try:
311
      if snode is None:
312
        excl = [pnode]
313
      else:
314
        excl = [pnode, snode]
315
      tnode = qa_config.AcquireNode(exclude=excl)
316
      try:
317
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
318
                pnode, snode, tnode)
319
      finally:
320
        qa_config.ReleaseNode(tnode)
321
    finally:
322
      qa_config.ReleaseInstance(newinst)
323

    
324

    
325
def RunDaemonTests(instance, pnode):
326
  """Test the ganeti-watcher script.
327

328
  """
329
  RunTest(qa_daemon.TestPauseWatcher)
330

    
331
  RunTestIf("instance-automatic-restart",
332
            qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
333
  RunTestIf("instance-consecutive-failures",
334
            qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
335

    
336
  RunTest(qa_daemon.TestResumeWatcher)
337

    
338

    
339
def RunHardwareFailureTests(instance, pnode, snode):
340
  """Test cluster internal hardware failure recovery.
341

342
  """
343
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
344

    
345
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
346
  RunTestIf(["instance-migrate", "rapi"],
347
            qa_rapi.TestRapiInstanceMigrate, instance)
348

    
349
  if qa_config.TestEnabled('instance-replace-disks'):
350
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
351
    try:
352
      RunTest(qa_instance.TestReplaceDisks,
353
              instance, pnode, snode, othernode)
354
    finally:
355
      qa_config.ReleaseNode(othernode)
356

    
357
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
358

    
359
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
360

    
361
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
362
            instance, pnode, snode)
363
  RunTestIf("instance-disk-failure",
364
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
365
            pnode, snode)
366

    
367

    
368
def RunQa():
369
  """Main QA body.
370

371
  """
372
  rapi_user = "ganeti-qa"
373
  rapi_secret = utils.GenerateSecret()
374

    
375
  RunEnvTests()
376
  SetupCluster(rapi_user, rapi_secret)
377

    
378
  # Load RAPI certificate
379
  qa_rapi.Setup(rapi_user, rapi_secret)
380

    
381
  RunClusterTests()
382
  RunOsTests()
383

    
384
  RunTestIf("tags", qa_tags.TestClusterTags)
385

    
386
  RunCommonNodeTests()
387
  RunGroupListTests()
388
  RunGroupRwTests()
389

    
390
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
391
  try:
392
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
393
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
394
  finally:
395
    qa_config.ReleaseNode(pnode)
396

    
397
  pnode = qa_config.AcquireNode()
398
  try:
399
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
400

    
401
    if qa_rapi.Enabled():
402
      RunTest(qa_rapi.TestNode, pnode)
403

    
404
      if qa_config.TestEnabled("instance-add-plain-disk"):
405
        for use_client in [True, False]:
406
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
407
                                  use_client)
408
          RunCommonInstanceTests(rapi_instance)
409
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
410
          del rapi_instance
411

    
412
    if qa_config.TestEnabled('instance-add-plain-disk'):
413
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
414
      RunCommonInstanceTests(instance)
415
      RunGroupListTests()
416
      RunTest(qa_cluster.TestClusterEpo)
417
      RunExportImportTests(instance, pnode, None)
418
      RunDaemonTests(instance, pnode)
419
      RunTest(qa_instance.TestInstanceRemove, instance)
420
      del instance
421

    
422
    multinode_tests = [
423
      ('instance-add-drbd-disk',
424
       qa_instance.TestInstanceAddWithDrbdDisk),
425
    ]
426

    
427
    for name, func in multinode_tests:
428
      if qa_config.TestEnabled(name):
429
        snode = qa_config.AcquireNode(exclude=pnode)
430
        try:
431
          instance = RunTest(func, pnode, snode)
432
          RunCommonInstanceTests(instance)
433
          RunGroupListTests()
434
          RunTest(qa_group.TestAssignNodesIncludingSplit,
435
                  constants.INITIAL_NODE_GROUP_NAME,
436
                  pnode["primary"], snode["primary"])
437
          if qa_config.TestEnabled('instance-convert-disk'):
438
            RunTest(qa_instance.TestInstanceShutdown, instance)
439
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
440
            RunTest(qa_instance.TestInstanceStartup, instance)
441
          RunExportImportTests(instance, pnode, snode)
442
          RunHardwareFailureTests(instance, pnode, snode)
443
          RunTest(qa_instance.TestInstanceRemove, instance)
444
          del instance
445
        finally:
446
          qa_config.ReleaseNode(snode)
447

    
448
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
449
      for shutdown in [False, True]:
450
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
451
        expnode = qa_config.AcquireNode(exclude=pnode)
452
        try:
453
          if shutdown:
454
            # Stop instance before exporting and removing it
455
            RunTest(qa_instance.TestInstanceShutdown, instance)
456
          RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
457
          RunTest(qa_instance.TestBackupList, expnode)
458
        finally:
459
          qa_config.ReleaseNode(expnode)
460
        del expnode
461
        del instance
462

    
463
  finally:
464
    qa_config.ReleaseNode(pnode)
465

    
466
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
467

    
468
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
469

    
470

    
471
@rapi.client.UsesRapiClient
472
def main():
473
  """Main program.
474

475
  """
476
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
477
  parser.add_option('--yes-do-it', dest='yes_do_it',
478
      action="store_true",
479
      help="Really execute the tests")
480
  (qa_config.options, args) = parser.parse_args()
481

    
482
  if len(args) == 1:
483
    (config_file, ) = args
484
  else:
485
    parser.error("Wrong number of arguments.")
486

    
487
  if not qa_config.options.yes_do_it:
488
    print ("Executing this script irreversibly destroys any Ganeti\n"
489
           "configuration on all nodes involved. If you really want\n"
490
           "to start testing, supply the --yes-do-it option.")
491
    sys.exit(1)
492

    
493
  qa_config.Load(config_file)
494

    
495
  qa_utils.StartMultiplexer(qa_config.GetMasterNode()["primary"])
496
  try:
497
    RunQa()
498
  finally:
499
    qa_utils.CloseMultiplexers()
500

    
501
if __name__ == '__main__':
502
  main()