Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ f3fd2c9d

History | View | Annotate | Download (14.2 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
from ganeti import constants
45

    
46
import ganeti.rapi.client
47

    
48

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

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

    
58

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

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

    
68
  return desc.rstrip(".")
69

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

73
  """
74

    
75
  tstart = datetime.datetime.now()
76

    
77
  desc = _DescriptionOf(fn)
78

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

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

    
90

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

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

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

    
106

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

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

    
115

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

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

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

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

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

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

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

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

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

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

    
148

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

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

    
172

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

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

    
189

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

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

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

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

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

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

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

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

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

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

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

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

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

    
240

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

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

    
249

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

253
  """
254
  RunTestIf("group-list", qa_group.TestGroupList)
255
  RunTestIf("group-list", qa_group.TestGroupListFields)
256

    
257

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

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

    
267

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

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

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

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

    
283
      RunTest(qa_instance.TestBackupList, expnode)
284

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

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

    
312

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

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

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

    
324
  RunTest(qa_daemon.TestResumeWatcher)
325

    
326

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

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

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

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

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

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

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

    
355

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

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

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

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

    
378
  qa_config.Load(config_file)
379

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

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

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

    
389
  RunClusterTests()
390
  RunOsTests()
391

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

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

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

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

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

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

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

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

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

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

    
470
  finally:
471
    qa_config.ReleaseNode(pnode)
472

    
473
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
474

    
475
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
476

    
477

    
478
if __name__ == '__main__':
479
  main()