Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 930e77d1

History | View | Annotate | Download (14 kB)

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

    
4
# Copyright (C) 2007, 2008, 2009, 2010 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
import sys
27
import datetime
28
import optparse
29

    
30
import qa_cluster
31
import qa_config
32
import qa_daemon
33
import qa_env
34
import qa_group
35
import qa_instance
36
import qa_node
37
import qa_os
38
import qa_rapi
39
import qa_tags
40
import qa_utils
41

    
42
from ganeti import utils
43
from ganeti import rapi
44

    
45
import ganeti.rapi.client
46

    
47

    
48
def _FormatHeader(line, end=72):
49
  """Fill a line up to the end column.
50

51
  """
52
  line = "---- " + line + " "
53
  line += "-" * (end-len(line))
54
  line = line.rstrip()
55
  return line
56

    
57

    
58
def _DescriptionOf(fn):
59
  """Computes the description of an item.
60

61
  """
62
  if fn.__doc__:
63
    desc = fn.__doc__.splitlines()[0].strip()
64
  else:
65
    desc = "%r" % fn
66

    
67
  return desc.rstrip(".")
68

    
69
def RunTest(fn, *args):
70
  """Runs a test after printing a header.
71

72
  """
73

    
74
  tstart = datetime.datetime.now()
75

    
76
  desc = _DescriptionOf(fn)
77

    
78
  print
79
  print _FormatHeader("%s start %s" % (tstart, desc))
80

    
81
  try:
82
    retval = fn(*args)
83
    return retval
84
  finally:
85
    tstop = datetime.datetime.now()
86
    tdelta = tstop - tstart
87
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
88

    
89

    
90
def RunTestIf(testnames, fn, *args):
91
  """Runs a test conditionally.
92

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

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

    
105

    
106
def RunEnvTests():
107
  """Run several environment tests.
108

109
  """
110
  RunTestIf("env", qa_env.TestSshConnection)
111
  RunTestIf("env", qa_env.TestIcmpPing)
112
  RunTestIf("env", qa_env.TestGanetiCommands)
113

    
114

    
115
def SetupCluster(rapi_user, rapi_secret):
116
  """Initializes the cluster.
117

118
  @param rapi_user: Login user for RAPI
119
  @param rapi_secret: Login secret for RAPI
120

121
  """
122
  RunTestIf("create-cluster", qa_cluster.TestClusterInit,
123
            rapi_user, rapi_secret)
124

    
125
  # Test on empty cluster
126
  RunTestIf("node-list", qa_node.TestNodeList)
127
  RunTestIf("instance-list", qa_instance.TestInstanceList)
128

    
129
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
130
  if not qa_config.TestEnabled("create-cluster"):
131
    # consider the nodes are already there
132
    qa_node.MarkNodeAddedAll()
133

    
134
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
135

    
136
  # enable the watcher (unconditionally)
137
  RunTest(qa_daemon.TestResumeWatcher)
138

    
139
  RunTestIf("node-list", qa_node.TestNodeList)
140

    
141
  # Test listing fields
142
  RunTestIf("node-list", qa_node.TestNodeListFields)
143
  RunTestIf("instance-list", qa_instance.TestInstanceListFields)
144

    
145
  RunTestIf("node-info", qa_node.TestNodeInfo)
146

    
147

    
148
def RunClusterTests():
149
  """Runs tests related to gnt-cluster.
150

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

    
171

    
172
def RunOsTests():
173
  """Runs all tests related to gnt-os.
174

175
  """
176
  for fn in [
177
    qa_os.TestOsList,
178
    qa_os.TestOsDiagnose,
179
    qa_os.TestOsValid,
180
    qa_os.TestOsInvalid,
181
    qa_os.TestOsPartiallyValid,
182
    qa_os.TestOsModifyValid,
183
    qa_os.TestOsModifyInvalid,
184
    qa_os.TestOsStates,
185
    ]:
186
    RunTestIf("os", fn)
187

    
188

    
189
def RunCommonInstanceTests(instance):
190
  """Runs a few tests that are common to all disk types.
191

192
  """
193
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
194
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
195

    
196
  RunTestIf("instance-list", qa_instance.TestInstanceList)
197

    
198
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
199

    
200
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
201
  RunTestIf(["instance-modify", "rapi"],
202
            qa_rapi.TestRapiInstanceModify, instance)
203

    
204
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
205

    
206
  RunTestIf("instance-reinstall", qa_instance.TestInstanceShutdown, instance)
207
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
208
  RunTestIf("instance-reinstall", qa_instance.TestInstanceStartup, instance)
209

    
210
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
211

    
212
  if qa_config.TestEnabled('instance-rename'):
213
    rename_source = instance["name"]
214
    rename_target = qa_config.get("rename", None)
215
    RunTest(qa_instance.TestInstanceShutdown, instance)
216
    # perform instance rename to the same name
217
    RunTest(qa_instance.TestInstanceRename, rename_source, rename_source)
218
    RunTestIf("rapi", qa_rapi.TestRapiInstanceRename,
219
              rename_source, rename_source)
220
    if rename_target is not None:
221
      # perform instance rename to a different name, if we have one configured
222
      RunTest(qa_instance.TestInstanceRename, rename_source, rename_target)
223
      RunTest(qa_instance.TestInstanceRename, rename_target, rename_source)
224
      RunTestIf("rapi", qa_rapi.TestRapiInstanceRename,
225
                rename_source, rename_target)
226
      RunTestIf("rapi", qa_rapi.TestRapiInstanceRename,
227
                rename_target, rename_source)
228
    RunTest(qa_instance.TestInstanceStartup, instance)
229

    
230
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
231

    
232
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
233

    
234
  RunTestIf("rapi", qa_rapi.TestInstance, instance)
235

    
236
  # Lists instances, too
237
  RunTestIf("node-list", qa_node.TestNodeList)
238

    
239

    
240
def RunCommonNodeTests():
241
  """Run a few common node tests.
242

243
  """
244
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
245
  RunTestIf("node-storage", qa_node.TestNodeStorage)
246
  RunTestIf("node-oob", qa_node.TestOutOfBand)
247

    
248

    
249
def RunGroupListTests():
250
  """Run tests for listing node groups.
251

252
  """
253
  RunTestIf("group-list", qa_group.TestGroupListDefaultFields)
254
  RunTestIf("group-list", qa_group.TestGroupListAllFields)
255

    
256

    
257
def RunGroupRwTests():
258
  """Run tests for adding/removing/renaming groups.
259

260
  """
261
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
262
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
263
  RunTestIf("group-rwops", qa_group.TestGroupModify)
264
  RunTestIf("rapi", qa_rapi.TestRapiNodeGroups)
265

    
266

    
267
def RunExportImportTests(instance, pnode, snode):
268
  """Tries to export and import the instance.
269

270
  @param pnode: current primary node of the instance
271
  @param snode: current secondary node of the instance, if any,
272
      otherwise None
273

274
  """
275
  if qa_config.TestEnabled('instance-export'):
276
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
277

    
278
    expnode = qa_config.AcquireNode(exclude=pnode)
279
    try:
280
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
281

    
282
      RunTest(qa_instance.TestBackupList, expnode)
283

    
284
      if qa_config.TestEnabled('instance-import'):
285
        newinst = qa_config.AcquireInstance()
286
        try:
287
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
288
                  expnode, name)
289
          RunTest(qa_instance.TestInstanceRemove, newinst)
290
        finally:
291
          qa_config.ReleaseInstance(newinst)
292
    finally:
293
      qa_config.ReleaseNode(expnode)
294

    
295
  if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
296
    newinst = qa_config.AcquireInstance()
297
    try:
298
      if snode is None:
299
        excl = [pnode]
300
      else:
301
        excl = [pnode, snode]
302
      tnode = qa_config.AcquireNode(exclude=excl)
303
      try:
304
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
305
                pnode, snode, tnode)
306
      finally:
307
        qa_config.ReleaseNode(tnode)
308
    finally:
309
      qa_config.ReleaseInstance(newinst)
310

    
311

    
312
def RunDaemonTests(instance, pnode):
313
  """Test the ganeti-watcher script.
314

315
  """
316
  RunTest(qa_daemon.TestPauseWatcher)
317

    
318
  RunTestIf("instance-automatic-restart",
319
            qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
320
  RunTestIf("instance-consecutive-failures",
321
            qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
322

    
323
  RunTest(qa_daemon.TestResumeWatcher)
324

    
325

    
326
def RunHardwareFailureTests(instance, pnode, snode):
327
  """Test cluster internal hardware failure recovery.
328

329
  """
330
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
331

    
332
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
333
  RunTestIf(["instance-migrate", "rapi"],
334
            qa_rapi.TestRapiInstanceMigrate, instance)
335

    
336
  if qa_config.TestEnabled('instance-replace-disks'):
337
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
338
    try:
339
      RunTest(qa_instance.TestReplaceDisks,
340
              instance, pnode, snode, othernode)
341
    finally:
342
      qa_config.ReleaseNode(othernode)
343

    
344
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
345

    
346
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
347

    
348
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
349
            instance, pnode, snode)
350
  RunTestIf("instance-disk-failure",
351
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
352
            pnode, snode)
353

    
354

    
355
@rapi.client.UsesRapiClient
356
def main():
357
  """Main program.
358

359
  """
360
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
361
  parser.add_option('--yes-do-it', dest='yes_do_it',
362
      action="store_true",
363
      help="Really execute the tests")
364
  (qa_config.options, args) = parser.parse_args()
365

    
366
  if len(args) == 1:
367
    (config_file, ) = args
368
  else:
369
    parser.error("Wrong number of arguments.")
370

    
371
  if not qa_config.options.yes_do_it:
372
    print ("Executing this script irreversibly destroys any Ganeti\n"
373
           "configuration on all nodes involved. If you really want\n"
374
           "to start testing, supply the --yes-do-it option.")
375
    sys.exit(1)
376

    
377
  qa_config.Load(config_file)
378

    
379
  rapi_user = "ganeti-qa"
380
  rapi_secret = utils.GenerateSecret()
381

    
382
  RunEnvTests()
383
  SetupCluster(rapi_user, rapi_secret)
384

    
385
  # Load RAPI certificate
386
  qa_rapi.Setup(rapi_user, rapi_secret)
387

    
388
  RunClusterTests()
389
  RunOsTests()
390

    
391
  RunTestIf("tags", qa_tags.TestClusterTags)
392

    
393
  RunCommonNodeTests()
394
  RunGroupListTests()
395
  RunGroupRwTests()
396

    
397
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
398
  try:
399
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
400
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
401
  finally:
402
    qa_config.ReleaseNode(pnode)
403

    
404
  pnode = qa_config.AcquireNode()
405
  try:
406
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
407

    
408
    if qa_rapi.Enabled():
409
      RunTest(qa_rapi.TestNode, pnode)
410

    
411
      if qa_config.TestEnabled("instance-add-plain-disk"):
412
        for use_client in [True, False]:
413
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
414
                                  use_client)
415
          RunCommonInstanceTests(rapi_instance)
416
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
417
          del rapi_instance
418

    
419
    if qa_config.TestEnabled('instance-add-plain-disk'):
420
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
421
      RunCommonInstanceTests(instance)
422
      RunGroupListTests()
423
      RunExportImportTests(instance, pnode, None)
424
      RunDaemonTests(instance, pnode)
425
      RunTest(qa_instance.TestInstanceRemove, instance)
426
      del instance
427

    
428
    multinode_tests = [
429
      ('instance-add-drbd-disk',
430
       qa_instance.TestInstanceAddWithDrbdDisk),
431
    ]
432

    
433
    for name, func in multinode_tests:
434
      if qa_config.TestEnabled(name):
435
        snode = qa_config.AcquireNode(exclude=pnode)
436
        try:
437
          instance = RunTest(func, pnode, snode)
438
          RunCommonInstanceTests(instance)
439
          RunGroupListTests()
440
          if qa_config.TestEnabled('instance-convert-disk'):
441
            RunTest(qa_instance.TestInstanceShutdown, instance)
442
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
443
            RunTest(qa_instance.TestInstanceStartup, instance)
444
          RunExportImportTests(instance, pnode, snode)
445
          RunHardwareFailureTests(instance, pnode, snode)
446
          RunTest(qa_instance.TestInstanceRemove, instance)
447
          del instance
448
        finally:
449
          qa_config.ReleaseNode(snode)
450

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

    
466
  finally:
467
    qa_config.ReleaseNode(pnode)
468

    
469
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
470

    
471
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
472

    
473

    
474
if __name__ == '__main__':
475
  main()