Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 4b10fb65

History | View | Annotate | Download (13.8 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
  RunTestIf("node-info", qa_node.TestNodeInfo)
142

    
143

    
144
def RunClusterTests():
145
  """Runs tests related to gnt-cluster.
146

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

    
167

    
168
def RunOsTests():
169
  """Runs all tests related to gnt-os.
170

171
  """
172
  for fn in [
173
    qa_os.TestOsList,
174
    qa_os.TestOsDiagnose,
175
    qa_os.TestOsValid,
176
    qa_os.TestOsInvalid,
177
    qa_os.TestOsPartiallyValid,
178
    qa_os.TestOsModifyValid,
179
    qa_os.TestOsModifyInvalid,
180
    qa_os.TestOsStates,
181
    ]:
182
    RunTestIf("os", fn)
183

    
184

    
185
def RunCommonInstanceTests(instance):
186
  """Runs a few tests that are common to all disk types.
187

188
  """
189
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
190
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
191

    
192
  RunTestIf("instance-list", qa_instance.TestInstanceList)
193

    
194
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
195

    
196
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
197
  RunTestIf(["instance-modify", "rapi"],
198
            qa_rapi.TestRapiInstanceModify, instance)
199

    
200
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
201

    
202
  RunTestIf("instance-reinstall", qa_instance.TestInstanceShutdown, instance)
203
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
204
  RunTestIf("instance-reinstall", qa_instance.TestInstanceStartup, instance)
205

    
206
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
207

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

    
223
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
224

    
225
  RunTestIf("rapi", qa_rapi.TestInstance, instance)
226

    
227
  # Lists instances, too
228
  RunTestIf("node-list", qa_node.TestNodeList)
229

    
230

    
231
def RunCommonNodeTests():
232
  """Run a few common node tests.
233

234
  """
235
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
236
  RunTestIf("node-storage", qa_node.TestNodeStorage)
237

    
238

    
239
def RunGroupListTests():
240
  """Run tests for listing node groups.
241

242
  """
243
  RunTestIf("group-list", qa_group.TestGroupListDefaultFields)
244
  RunTestIf("group-list", qa_group.TestGroupListAllFields)
245

    
246

    
247
def RunGroupRwTests():
248
  """Run tests for adding/removing/renaming groups.
249

250
  """
251
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
252
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
253
  RunTestIf("group-rwops", qa_group.TestGroupModify)
254
  RunTestIf("rapi", qa_rapi.TestRapiNodeGroups)
255

    
256

    
257
def RunExportImportTests(instance, pnode, snode):
258
  """Tries to export and import the instance.
259

260
  @param pnode: current primary node of the instance
261
  @param snode: current secondary node of the instance, if any,
262
      otherwise None
263

264
  """
265
  if qa_config.TestEnabled('instance-export'):
266
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
267

    
268
    expnode = qa_config.AcquireNode(exclude=pnode)
269
    try:
270
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
271

    
272
      RunTest(qa_instance.TestBackupList, expnode)
273

    
274
      if qa_config.TestEnabled('instance-import'):
275
        newinst = qa_config.AcquireInstance()
276
        try:
277
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
278
                  expnode, name)
279
          RunTest(qa_instance.TestInstanceRemove, newinst)
280
        finally:
281
          qa_config.ReleaseInstance(newinst)
282
    finally:
283
      qa_config.ReleaseNode(expnode)
284

    
285
  if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
286
    newinst = qa_config.AcquireInstance()
287
    try:
288
      if snode is None:
289
        excl = [pnode]
290
      else:
291
        excl = [pnode, snode]
292
      tnode = qa_config.AcquireNode(exclude=excl)
293
      try:
294
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
295
                pnode, snode, tnode)
296
      finally:
297
        qa_config.ReleaseNode(tnode)
298
    finally:
299
      qa_config.ReleaseInstance(newinst)
300

    
301

    
302
def RunDaemonTests(instance, pnode):
303
  """Test the ganeti-watcher script.
304

305
  """
306
  RunTest(qa_daemon.TestPauseWatcher)
307

    
308
  RunTestIf("instance-automatic-restart",
309
            qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
310
  RunTestIf("instance-consecutive-failures",
311
            qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
312

    
313
  RunTest(qa_daemon.TestResumeWatcher)
314

    
315

    
316
def RunHardwareFailureTests(instance, pnode, snode):
317
  """Test cluster internal hardware failure recovery.
318

319
  """
320
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
321

    
322
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
323
  RunTestIf(["instance-migrate", "rapi"],
324
            qa_rapi.TestRapiInstanceMigrate, instance)
325

    
326
  if qa_config.TestEnabled('instance-replace-disks'):
327
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
328
    try:
329
      RunTest(qa_instance.TestReplaceDisks,
330
              instance, pnode, snode, othernode)
331
    finally:
332
      qa_config.ReleaseNode(othernode)
333

    
334
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
335

    
336
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
337

    
338
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
339
            instance, pnode, snode)
340
  RunTestIf("instance-disk-failure",
341
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
342
            pnode, snode)
343

    
344

    
345
@rapi.client.UsesRapiClient
346
def main():
347
  """Main program.
348

349
  """
350
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
351
  parser.add_option('--yes-do-it', dest='yes_do_it',
352
      action="store_true",
353
      help="Really execute the tests")
354
  (qa_config.options, args) = parser.parse_args()
355

    
356
  if len(args) == 1:
357
    (config_file, ) = args
358
  else:
359
    parser.error("Wrong number of arguments.")
360

    
361
  if not qa_config.options.yes_do_it:
362
    print ("Executing this script irreversibly destroys any Ganeti\n"
363
           "configuration on all nodes involved. If you really want\n"
364
           "to start testing, supply the --yes-do-it option.")
365
    sys.exit(1)
366

    
367
  qa_config.Load(config_file)
368

    
369
  rapi_user = "ganeti-qa"
370
  rapi_secret = utils.GenerateSecret()
371

    
372
  RunEnvTests()
373
  SetupCluster(rapi_user, rapi_secret)
374

    
375
  # Load RAPI certificate
376
  qa_rapi.Setup(rapi_user, rapi_secret)
377

    
378
  RunClusterTests()
379
  RunOsTests()
380

    
381
  RunTestIf("tags", qa_tags.TestClusterTags)
382

    
383
  RunCommonNodeTests()
384
  RunGroupListTests()
385
  RunGroupRwTests()
386

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

    
394
  pnode = qa_config.AcquireNode()
395
  try:
396
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
397

    
398
    if qa_rapi.Enabled():
399
      RunTest(qa_rapi.TestNode, pnode)
400

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

    
409
    if qa_config.TestEnabled('instance-add-plain-disk'):
410
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
411
      RunCommonInstanceTests(instance)
412
      RunGroupListTests()
413
      RunExportImportTests(instance, pnode, None)
414
      RunDaemonTests(instance, pnode)
415
      RunTest(qa_instance.TestInstanceRemove, instance)
416
      del instance
417

    
418
    multinode_tests = [
419
      ('instance-add-drbd-disk',
420
       qa_instance.TestInstanceAddWithDrbdDisk),
421
    ]
422

    
423
    for name, func in multinode_tests:
424
      if qa_config.TestEnabled(name):
425
        snode = qa_config.AcquireNode(exclude=pnode)
426
        try:
427
          instance = RunTest(func, pnode, snode)
428
          RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
429
          RunCommonInstanceTests(instance)
430
          RunGroupListTests()
431
          if qa_config.TestEnabled('instance-convert-disk'):
432
            RunTest(qa_instance.TestInstanceShutdown, instance)
433
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
434
            RunTest(qa_instance.TestInstanceStartup, instance)
435
          RunExportImportTests(instance, pnode, snode)
436
          RunHardwareFailureTests(instance, pnode, snode)
437
          RunTest(qa_instance.TestInstanceRemove, instance)
438
          del instance
439
        finally:
440
          qa_config.ReleaseNode(snode)
441

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

    
457
  finally:
458
    qa_config.ReleaseNode(pnode)
459

    
460
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
461

    
462
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
463

    
464

    
465
if __name__ == '__main__':
466
  main()