Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 66787da5

History | View | Annotate | Download (13 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
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
125
  if not qa_config.TestEnabled("create-cluster"):
126
    # consider the nodes are already there
127
    qa_node.MarkNodeAddedAll()
128

    
129
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
130

    
131
  # enable the watcher (unconditionally)
132
  RunTest(qa_daemon.TestResumeWatcher)
133

    
134
  RunTestIf("node-info", qa_node.TestNodeInfo)
135

    
136

    
137
def RunClusterTests():
138
  """Runs tests related to gnt-cluster.
139

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

    
160

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

164
  """
165
  for fn in [
166
    qa_os.TestOsList,
167
    qa_os.TestOsDiagnose,
168
    qa_os.TestOsValid,
169
    qa_os.TestOsInvalid,
170
    qa_os.TestOsPartiallyValid,
171
    qa_os.TestOsModifyValid,
172
    qa_os.TestOsModifyInvalid,
173
    qa_os.TestOsStates,
174
    ]:
175
    RunTestIf("os", fn)
176

    
177

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

181
  """
182
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
183
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
184

    
185
  RunTestIf("instance-list", qa_instance.TestInstanceList)
186

    
187
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
188

    
189
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
190
  RunTestIf(["instance-modify", "rapi"],
191
            qa_rapi.TestRapiInstanceModify, instance)
192

    
193
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
194

    
195
  RunTestIf("instance-reinstall", qa_instance.TestInstanceShutdown, instance)
196
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
197
  RunTestIf("instance-reinstall", qa_instance.TestInstanceStartup, instance)
198

    
199
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
200

    
201
  if qa_config.TestEnabled('instance-rename'):
202
    rename_target = qa_config.get("rename", None)
203
    if rename_target is None:
204
      print qa_utils.FormatError("Can rename instance, 'rename' entry is"
205
                                 " missing from configuration")
206
    else:
207
      RunTest(qa_instance.TestInstanceShutdown, instance)
208
      RunTest(qa_instance.TestInstanceRename, instance, rename_target)
209
      RunTestIf("rapi", qa_rapi.TestRapiInstanceRename, instance, rename_target)
210
      RunTest(qa_instance.TestInstanceStartup, instance)
211

    
212
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
213

    
214
  RunTestIf("rapi", qa_rapi.TestInstance, instance)
215

    
216

    
217
def RunCommonNodeTests():
218
  """Run a few common node tests.
219

220
  """
221
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
222
  RunTestIf("node-storage", qa_node.TestNodeStorage)
223

    
224

    
225
def RunGroupListTests():
226
  """Run tests for listing node groups.
227

228
  """
229
  RunTestIf("group-list", qa_group.TestGroupListDefaultFields)
230
  RunTestIf("group-list", qa_group.TestGroupListAllFields)
231

    
232

    
233
def RunGroupRwTests():
234
  """Run tests for adding/removing/renaming groups.
235

236
  """
237
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
238

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

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

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

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

    
254
      RunTest(qa_instance.TestBackupList, expnode)
255

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

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

    
283

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

287
  """
288
  RunTest(qa_daemon.TestPauseWatcher)
289

    
290
  RunTestIf("instance-automatic-restart",
291
            qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
292
  RunTestIf("instance-consecutive-failures",
293
            qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
294

    
295
  RunTest(qa_daemon.TestResumeWatcher)
296

    
297

    
298
def RunHardwareFailureTests(instance, pnode, snode):
299
  """Test cluster internal hardware failure recovery.
300

301
  """
302
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
303

    
304
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
305
  RunTestIf(["instance-migrate", "rapi"],
306
            qa_rapi.TestRapiInstanceMigrate, instance)
307

    
308
  if qa_config.TestEnabled('instance-replace-disks'):
309
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
310
    try:
311
      RunTest(qa_instance.TestReplaceDisks,
312
              instance, pnode, snode, othernode)
313
    finally:
314
      qa_config.ReleaseNode(othernode)
315

    
316
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
317

    
318
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
319

    
320
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
321
            instance, pnode, snode)
322
  RunTestIf("instance-disk-failure",
323
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
324
            pnode, snode)
325

    
326

    
327
@rapi.client.UsesRapiClient
328
def main():
329
  """Main program.
330

331
  """
332
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
333
  parser.add_option('--yes-do-it', dest='yes_do_it',
334
      action="store_true",
335
      help="Really execute the tests")
336
  (qa_config.options, args) = parser.parse_args()
337

    
338
  if len(args) == 1:
339
    (config_file, ) = args
340
  else:
341
    parser.error("Wrong number of arguments.")
342

    
343
  if not qa_config.options.yes_do_it:
344
    print ("Executing this script irreversibly destroys any Ganeti\n"
345
           "configuration on all nodes involved. If you really want\n"
346
           "to start testing, supply the --yes-do-it option.")
347
    sys.exit(1)
348

    
349
  qa_config.Load(config_file)
350

    
351
  rapi_user = "ganeti-qa"
352
  rapi_secret = utils.GenerateSecret()
353

    
354
  RunEnvTests()
355
  SetupCluster(rapi_user, rapi_secret)
356

    
357
  # Load RAPI certificate
358
  qa_rapi.Setup(rapi_user, rapi_secret)
359

    
360
  RunClusterTests()
361
  RunOsTests()
362

    
363
  RunTestIf("tags", qa_tags.TestClusterTags)
364

    
365
  RunCommonNodeTests()
366
  RunGroupListTests()
367
  RunGroupRwTests()
368

    
369
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
370
  try:
371
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
372
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
373
  finally:
374
    qa_config.ReleaseNode(pnode)
375

    
376
  pnode = qa_config.AcquireNode()
377
  try:
378
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
379

    
380
    if qa_rapi.Enabled():
381
      RunTest(qa_rapi.TestNode, pnode)
382

    
383
      if qa_config.TestEnabled("instance-add-plain-disk"):
384
        for use_client in [True, False]:
385
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
386
                                  use_client)
387
          RunCommonInstanceTests(rapi_instance)
388
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
389
          del rapi_instance
390

    
391
    if qa_config.TestEnabled('instance-add-plain-disk'):
392
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
393
      RunCommonInstanceTests(instance)
394
      RunGroupListTests()
395
      RunExportImportTests(instance, pnode, None)
396
      RunDaemonTests(instance, pnode)
397
      RunTest(qa_instance.TestInstanceRemove, instance)
398
      del instance
399

    
400
    multinode_tests = [
401
      ('instance-add-drbd-disk',
402
       qa_instance.TestInstanceAddWithDrbdDisk),
403
    ]
404

    
405
    for name, func in multinode_tests:
406
      if qa_config.TestEnabled(name):
407
        snode = qa_config.AcquireNode(exclude=pnode)
408
        try:
409
          instance = RunTest(func, pnode, snode)
410
          RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
411
          RunCommonInstanceTests(instance)
412
          RunGroupListTests()
413
          if qa_config.TestEnabled('instance-convert-disk'):
414
            RunTest(qa_instance.TestInstanceShutdown, instance)
415
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
416
            RunTest(qa_instance.TestInstanceStartup, instance)
417
          RunExportImportTests(instance, pnode, snode)
418
          RunHardwareFailureTests(instance, pnode, snode)
419
          RunTest(qa_instance.TestInstanceRemove, instance)
420
          del instance
421
        finally:
422
          qa_config.ReleaseNode(snode)
423

    
424
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
425
      for shutdown in [False, True]:
426
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
427
        expnode = qa_config.AcquireNode(exclude=pnode)
428
        try:
429
          if shutdown:
430
            # Stop instance before exporting and removing it
431
            RunTest(qa_instance.TestInstanceShutdown, instance)
432
          RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
433
          RunTest(qa_instance.TestBackupList, expnode)
434
        finally:
435
          qa_config.ReleaseNode(expnode)
436
        del expnode
437
        del instance
438

    
439
  finally:
440
    qa_config.ReleaseNode(pnode)
441

    
442
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
443

    
444
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
445

    
446

    
447
if __name__ == '__main__':
448
  main()