Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 69df9d2b

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
    ("cluster-oob", qa_cluster.TestClusterOob),
168
    ("rapi", qa_rapi.TestVersion),
169
    ("rapi", qa_rapi.TestEmptyCluster),
170
    ]:
171
    RunTestIf(test, fn)
172

    
173

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

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

    
190

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

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

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

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

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

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

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

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

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

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

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

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

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

    
241

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

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

    
250

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

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

    
258

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

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

    
268

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

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

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

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

    
284
      RunTest(qa_instance.TestBackupList, expnode)
285

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

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

    
313

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

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

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

    
325
  RunTest(qa_daemon.TestResumeWatcher)
326

    
327

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

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

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

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

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

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

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

    
356

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

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

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

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

    
379
  qa_config.Load(config_file)
380

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

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

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

    
390
  RunClusterTests()
391
  RunOsTests()
392

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

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

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

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

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

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

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

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

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

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

    
471
  finally:
472
    qa_config.ReleaseNode(pnode)
473

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

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

    
478

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