Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ d27150a9

History | View | Annotate | Download (13.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_instance
35
import qa_node
36
import qa_os
37
import qa_rapi
38
import qa_tags
39
import qa_utils
40

    
41
from ganeti import utils
42
from ganeti import rapi
43

    
44
import ganeti.rapi.client
45

    
46

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

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

    
56

    
57
def RunTest(fn, *args):
58
  """Runs a test after printing a header.
59

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

    
66
  desc = desc.rstrip(".")
67

    
68
  tstart = datetime.datetime.now()
69

    
70
  print
71
  print _FormatHeader("%s start %s" % (tstart, desc))
72

    
73
  try:
74
    retval = fn(*args)
75
    return retval
76
  finally:
77
    tstop = datetime.datetime.now()
78
    tdelta = tstop - tstart
79
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
80

    
81

    
82
def RunEnvTests():
83
  """Run several environment tests.
84

85
  """
86
  if not qa_config.TestEnabled('env'):
87
    return
88

    
89
  RunTest(qa_env.TestSshConnection)
90
  RunTest(qa_env.TestIcmpPing)
91
  RunTest(qa_env.TestGanetiCommands)
92

    
93

    
94
def SetupCluster(rapi_user, rapi_secret):
95
  """Initializes the cluster.
96

97
  @param rapi_user: Login user for RAPI
98
  @param rapi_secret: Login secret for RAPI
99

100
  """
101
  if qa_config.TestEnabled('create-cluster'):
102
    RunTest(qa_cluster.TestClusterInit, rapi_user, rapi_secret)
103
    RunTest(qa_node.TestNodeAddAll)
104
  else:
105
    # consider the nodes are already there
106
    qa_node.MarkNodeAddedAll()
107

    
108
  if qa_config.TestEnabled("test-jobqueue"):
109
    RunTest(qa_cluster.TestJobqueue)
110

    
111
  # enable the watcher (unconditionally)
112
  RunTest(qa_daemon.TestResumeWatcher)
113

    
114
  if qa_config.TestEnabled('node-info'):
115
    RunTest(qa_node.TestNodeInfo)
116

    
117

    
118
def RunClusterTests():
119
  """Runs tests related to gnt-cluster.
120

121
  """
122
  if qa_config.TestEnabled("cluster-renew-crypto"):
123
    RunTest(qa_cluster.TestClusterRenewCrypto)
124

    
125
  if qa_config.TestEnabled('cluster-verify'):
126
    RunTest(qa_cluster.TestClusterVerify)
127

    
128
  if qa_config.TestEnabled('cluster-reserved-lvs'):
129
    RunTest(qa_cluster.TestClusterReservedLvs)
130

    
131
  if qa_config.TestEnabled("cluster-modify"):
132
    RunTest(qa_cluster.TestClusterModifyBe)
133
    # TODO: add more cluster modify tests
134

    
135
  if qa_config.TestEnabled('cluster-rename'):
136
    RunTest(qa_cluster.TestClusterRename)
137

    
138
  if qa_config.TestEnabled('cluster-info'):
139
    RunTest(qa_cluster.TestClusterVersion)
140
    RunTest(qa_cluster.TestClusterInfo)
141
    RunTest(qa_cluster.TestClusterGetmaster)
142

    
143
  if qa_config.TestEnabled('cluster-copyfile'):
144
    RunTest(qa_cluster.TestClusterCopyfile)
145

    
146
  if qa_config.TestEnabled('cluster-command'):
147
    RunTest(qa_cluster.TestClusterCommand)
148

    
149
  if qa_config.TestEnabled('cluster-burnin'):
150
    RunTest(qa_cluster.TestClusterBurnin)
151

    
152
  if qa_config.TestEnabled('cluster-master-failover'):
153
    RunTest(qa_cluster.TestClusterMasterFailover)
154

    
155
  if qa_rapi.Enabled():
156
    RunTest(qa_rapi.TestVersion)
157
    RunTest(qa_rapi.TestEmptyCluster)
158

    
159

    
160
def RunOsTests():
161
  """Runs all tests related to gnt-os.
162

163
  """
164
  if not qa_config.TestEnabled('os'):
165
    return
166

    
167
  RunTest(qa_os.TestOsList)
168
  RunTest(qa_os.TestOsDiagnose)
169
  RunTest(qa_os.TestOsValid)
170
  RunTest(qa_os.TestOsInvalid)
171
  RunTest(qa_os.TestOsPartiallyValid)
172
  RunTest(qa_os.TestOsModifyValid)
173
  RunTest(qa_os.TestOsModifyInvalid)
174
  RunTest(qa_os.TestOsStates)
175

    
176

    
177
def RunCommonInstanceTests(instance):
178
  """Runs a few tests that are common to all disk types.
179

180
  """
181
  if qa_config.TestEnabled('instance-shutdown'):
182
    RunTest(qa_instance.TestInstanceShutdown, instance)
183
    RunTest(qa_instance.TestInstanceStartup, instance)
184

    
185
  if qa_config.TestEnabled('instance-list'):
186
    RunTest(qa_instance.TestInstanceList)
187

    
188
  if qa_config.TestEnabled('instance-info'):
189
    RunTest(qa_instance.TestInstanceInfo, instance)
190

    
191
  if qa_config.TestEnabled('instance-modify'):
192
    RunTest(qa_instance.TestInstanceModify, instance)
193
    if qa_rapi.Enabled():
194
      RunTest(qa_rapi.TestRapiInstanceModify, instance)
195

    
196
  if qa_config.TestEnabled('instance-console'):
197
    RunTest(qa_instance.TestInstanceConsole, instance)
198

    
199
  if qa_config.TestEnabled('instance-reinstall'):
200
    RunTest(qa_instance.TestInstanceShutdown, instance)
201
    RunTest(qa_instance.TestInstanceReinstall, instance)
202
    RunTest(qa_instance.TestInstanceStartup, instance)
203

    
204
  if qa_config.TestEnabled('instance-reboot'):
205
    RunTest(qa_instance.TestInstanceReboot, instance)
206

    
207
  if qa_config.TestEnabled('instance-rename'):
208
    rename_target = qa_config.get("rename", None)
209
    if rename_target is None:
210
      print qa_utils.FormatError("Can not rename instance, 'rename' entry is"
211
                                 " missing from configuration")
212
    else:
213
      RunTest(qa_instance.TestInstanceShutdown, instance)
214
      RunTest(qa_instance.TestInstanceRename, instance, rename_target)
215
      if qa_rapi.Enabled():
216
        RunTest(qa_rapi.TestRapiInstanceRename, instance, rename_target)
217
      RunTest(qa_instance.TestInstanceStartup, instance)
218

    
219
  if qa_config.TestEnabled('tags'):
220
    RunTest(qa_tags.TestInstanceTags, instance)
221

    
222
  if qa_config.TestEnabled("cluster-verify"):
223
    RunTest(qa_cluster.TestClusterVerify)
224

    
225
  if qa_rapi.Enabled():
226
    RunTest(qa_rapi.TestInstance, instance)
227

    
228

    
229
def RunCommonNodeTests():
230
  """Run a few common node tests.
231

232
  """
233
  if qa_config.TestEnabled('node-volumes'):
234
    RunTest(qa_node.TestNodeVolumes)
235

    
236
  if qa_config.TestEnabled("node-storage"):
237
    RunTest(qa_node.TestNodeStorage)
238

    
239

    
240
def RunExportImportTests(instance, pnode, snode):
241
  """Tries to export and import the instance.
242

243
  @param pnode: current primary node of the instance
244
  @param snode: current secondary node of the instance, if any,
245
      otherwise None
246

247
  """
248
  if qa_config.TestEnabled('instance-export'):
249
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
250

    
251
    expnode = qa_config.AcquireNode(exclude=pnode)
252
    try:
253
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
254

    
255
      RunTest(qa_instance.TestBackupList, expnode)
256

    
257
      if qa_config.TestEnabled('instance-import'):
258
        newinst = qa_config.AcquireInstance()
259
        try:
260
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
261
                  expnode, name)
262
          RunTest(qa_instance.TestInstanceRemove, newinst)
263
        finally:
264
          qa_config.ReleaseInstance(newinst)
265
    finally:
266
      qa_config.ReleaseNode(expnode)
267

    
268
  if (qa_rapi.Enabled() and
269
      qa_config.TestEnabled("inter-cluster-instance-move")):
270
    newinst = qa_config.AcquireInstance()
271
    try:
272
      if snode is None:
273
        excl = [pnode]
274
      else:
275
        excl = [pnode, snode]
276
      tnode = qa_config.AcquireNode(exclude=excl)
277
      try:
278
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
279
                pnode, snode, tnode)
280
      finally:
281
        qa_config.ReleaseNode(tnode)
282
    finally:
283
      qa_config.ReleaseInstance(newinst)
284

    
285

    
286
def RunDaemonTests(instance, pnode):
287
  """Test the ganeti-watcher script.
288

289
  """
290
  automatic_restart = \
291
    qa_config.TestEnabled('instance-automatic-restart')
292
  consecutive_failures = \
293
    qa_config.TestEnabled('instance-consecutive-failures')
294

    
295
  RunTest(qa_daemon.TestPauseWatcher)
296
  if automatic_restart or consecutive_failures:
297

    
298
    if automatic_restart:
299
      RunTest(qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
300

    
301
    if consecutive_failures:
302
      RunTest(qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
303

    
304
  RunTest(qa_daemon.TestResumeWatcher)
305

    
306

    
307
def RunHardwareFailureTests(instance, pnode, snode):
308
  """Test cluster internal hardware failure recovery.
309

310
  """
311
  if qa_config.TestEnabled('instance-failover'):
312
    RunTest(qa_instance.TestInstanceFailover, instance)
313

    
314
  if qa_config.TestEnabled("instance-migrate"):
315
    RunTest(qa_instance.TestInstanceMigrate, instance)
316

    
317
    if qa_rapi.Enabled():
318
      RunTest(qa_rapi.TestRapiInstanceMigrate, instance)
319

    
320
  if qa_config.TestEnabled('instance-replace-disks'):
321
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
322
    try:
323
      RunTest(qa_instance.TestReplaceDisks,
324
              instance, pnode, snode, othernode)
325
    finally:
326
      qa_config.ReleaseNode(othernode)
327

    
328
  if qa_config.TestEnabled('node-evacuate'):
329
    RunTest(qa_node.TestNodeEvacuate, pnode, snode)
330

    
331
  if qa_config.TestEnabled('node-failover'):
332
    RunTest(qa_node.TestNodeFailover, pnode, snode)
333

    
334
  if qa_config.TestEnabled('instance-disk-failure'):
335
    RunTest(qa_instance.TestInstanceMasterDiskFailure,
336
            instance, pnode, snode)
337
    RunTest(qa_instance.TestInstanceSecondaryDiskFailure,
338
            instance, pnode, snode)
339

    
340

    
341
@rapi.client.UsesRapiClient
342
def main():
343
  """Main program.
344

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

    
352
  if len(args) == 1:
353
    (config_file, ) = args
354
  else:
355
    parser.error("Wrong number of arguments.")
356

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

    
363
  qa_config.Load(config_file)
364

    
365
  rapi_user = "ganeti-qa"
366
  rapi_secret = utils.GenerateSecret()
367

    
368
  RunEnvTests()
369
  SetupCluster(rapi_user, rapi_secret)
370

    
371
  # Load RAPI certificate
372
  qa_rapi.Setup(rapi_user, rapi_secret)
373

    
374
  RunClusterTests()
375
  RunOsTests()
376

    
377
  if qa_config.TestEnabled('tags'):
378
    RunTest(qa_tags.TestClusterTags)
379

    
380
  RunCommonNodeTests()
381

    
382
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
383
  try:
384
    if qa_config.TestEnabled('node-readd'):
385
      RunTest(qa_node.TestNodeReadd, pnode)
386

    
387
    if qa_config.TestEnabled("node-modify"):
388
      RunTest(qa_node.TestNodeModify, pnode)
389
  finally:
390
    qa_config.ReleaseNode(pnode)
391

    
392
  pnode = qa_config.AcquireNode()
393
  try:
394
    if qa_config.TestEnabled('tags'):
395
      RunTest(qa_tags.TestNodeTags, pnode)
396

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

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

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

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

    
421
    for name, func in multinode_tests:
422
      if qa_config.TestEnabled(name):
423
        snode = qa_config.AcquireNode(exclude=pnode)
424
        try:
425
          instance = RunTest(func, pnode, snode)
426
          RunCommonInstanceTests(instance)
427
          if qa_config.TestEnabled('instance-convert-disk'):
428
            RunTest(qa_instance.TestInstanceShutdown, instance)
429
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
430
            RunTest(qa_instance.TestInstanceStartup, instance)
431
          RunExportImportTests(instance, pnode, snode)
432
          RunHardwareFailureTests(instance, pnode, snode)
433
          RunTest(qa_instance.TestInstanceRemove, instance)
434
          del instance
435
        finally:
436
          qa_config.ReleaseNode(snode)
437

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

    
454
  finally:
455
    qa_config.ReleaseNode(pnode)
456

    
457
  if qa_config.TestEnabled('create-cluster'):
458
    RunTest(qa_node.TestNodeRemoveAll)
459

    
460
  if qa_config.TestEnabled('cluster-destroy'):
461
    RunTest(qa_cluster.TestClusterDestroy)
462

    
463

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