Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 7d88f255

History | View | Annotate | Download (12.6 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):
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 _DescriptionOf(fn):
58
  """Computes the description of an item.
59

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

    
66
  return desc.rstrip(".")
67

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

71
  """
72

    
73
  tstart = datetime.datetime.now()
74

    
75
  desc = _DescriptionOf(fn)
76

    
77
  print
78
  print _FormatHeader("%s start %s" % (tstart, desc))
79

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

    
88

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

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

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

    
104

    
105
def RunEnvTests():
106
  """Run several environment tests.
107

108
  """
109
  RunTestIf("env", qa_env.TestSshConnection)
110
  RunTestIf("env", qa_env.TestIcmpPing)
111
  RunTestIf("env", qa_env.TestGanetiCommands)
112

    
113

    
114
def SetupCluster(rapi_user, rapi_secret):
115
  """Initializes the cluster.
116

117
  @param rapi_user: Login user for RAPI
118
  @param rapi_secret: Login secret for RAPI
119

120
  """
121
  RunTestIf("create-cluster", qa_cluster.TestClusterInit,
122
            rapi_user, rapi_secret)
123
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
124
  if not qa_config.TestEnabled("create-cluster"):
125
    # consider the nodes are already there
126
    qa_node.MarkNodeAddedAll()
127

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

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

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

    
135

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

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

    
159

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

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

    
176

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

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

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

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

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

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

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

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

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

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

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

    
215

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

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

    
223

    
224
def RunExportImportTests(instance, pnode, snode):
225
  """Tries to export and import the instance.
226

227
  @param pnode: current primary node of the instance
228
  @param snode: current secondary node of the instance, if any,
229
      otherwise None
230

231
  """
232
  if qa_config.TestEnabled('instance-export'):
233
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
234

    
235
    expnode = qa_config.AcquireNode(exclude=pnode)
236
    try:
237
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
238

    
239
      RunTest(qa_instance.TestBackupList, expnode)
240

    
241
      if qa_config.TestEnabled('instance-import'):
242
        newinst = qa_config.AcquireInstance()
243
        try:
244
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
245
                  expnode, name)
246
          RunTest(qa_instance.TestInstanceRemove, newinst)
247
        finally:
248
          qa_config.ReleaseInstance(newinst)
249
    finally:
250
      qa_config.ReleaseNode(expnode)
251

    
252
  if qa_config.TestEnabled(["rapi", "inter-cluster-instance-move"]):
253
    newinst = qa_config.AcquireInstance()
254
    try:
255
      if snode is None:
256
        excl = [pnode]
257
      else:
258
        excl = [pnode, snode]
259
      tnode = qa_config.AcquireNode(exclude=excl)
260
      try:
261
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
262
                pnode, snode, tnode)
263
      finally:
264
        qa_config.ReleaseNode(tnode)
265
    finally:
266
      qa_config.ReleaseInstance(newinst)
267

    
268

    
269
def RunDaemonTests(instance, pnode):
270
  """Test the ganeti-watcher script.
271

272
  """
273
  RunTest(qa_daemon.TestPauseWatcher)
274

    
275
  RunTestIf("instance-automatic-restart",
276
            qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
277
  RunTestIf("instance-consecutive-failures",
278
            qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
279

    
280
  RunTest(qa_daemon.TestResumeWatcher)
281

    
282

    
283
def RunHardwareFailureTests(instance, pnode, snode):
284
  """Test cluster internal hardware failure recovery.
285

286
  """
287
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
288

    
289
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
290
  RunTestIf(["instance-migrate", "rapi"],
291
            qa_rapi.TestRapiInstanceMigrate, instance)
292

    
293
  if qa_config.TestEnabled('instance-replace-disks'):
294
    othernode = qa_config.AcquireNode(exclude=[pnode, snode])
295
    try:
296
      RunTest(qa_instance.TestReplaceDisks,
297
              instance, pnode, snode, othernode)
298
    finally:
299
      qa_config.ReleaseNode(othernode)
300

    
301
  RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, pnode, snode)
302

    
303
  RunTestIf("node-failover", qa_node.TestNodeFailover, pnode, snode)
304

    
305
  RunTestIf("instance-disk-failure", qa_instance.TestInstanceMasterDiskFailure,
306
            instance, pnode, snode)
307
  RunTestIf("instance-disk-failure",
308
            qa_instance.TestInstanceSecondaryDiskFailure, instance,
309
            pnode, snode)
310

    
311

    
312
@rapi.client.UsesRapiClient
313
def main():
314
  """Main program.
315

316
  """
317
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
318
  parser.add_option('--yes-do-it', dest='yes_do_it',
319
      action="store_true",
320
      help="Really execute the tests")
321
  (qa_config.options, args) = parser.parse_args()
322

    
323
  if len(args) == 1:
324
    (config_file, ) = args
325
  else:
326
    parser.error("Wrong number of arguments.")
327

    
328
  if not qa_config.options.yes_do_it:
329
    print ("Executing this script irreversibly destroys any Ganeti\n"
330
           "configuration on all nodes involved. If you really want\n"
331
           "to start testing, supply the --yes-do-it option.")
332
    sys.exit(1)
333

    
334
  qa_config.Load(config_file)
335

    
336
  rapi_user = "ganeti-qa"
337
  rapi_secret = utils.GenerateSecret()
338

    
339
  RunEnvTests()
340
  SetupCluster(rapi_user, rapi_secret)
341

    
342
  # Load RAPI certificate
343
  qa_rapi.Setup(rapi_user, rapi_secret)
344

    
345
  RunClusterTests()
346
  RunOsTests()
347

    
348
  RunTestIf("tags", qa_tags.TestClusterTags)
349

    
350
  RunCommonNodeTests()
351

    
352
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
353
  try:
354
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
355
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
356
  finally:
357
    qa_config.ReleaseNode(pnode)
358

    
359
  pnode = qa_config.AcquireNode()
360
  try:
361
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
362

    
363
    if qa_rapi.Enabled():
364
      RunTest(qa_rapi.TestNode, pnode)
365

    
366
      if qa_config.TestEnabled("instance-add-plain-disk"):
367
        for use_client in [True, False]:
368
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
369
                                  use_client)
370
          RunCommonInstanceTests(rapi_instance)
371
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
372
          del rapi_instance
373

    
374
    if qa_config.TestEnabled('instance-add-plain-disk'):
375
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
376
      RunCommonInstanceTests(instance)
377
      RunExportImportTests(instance, pnode, None)
378
      RunDaemonTests(instance, pnode)
379
      RunTest(qa_instance.TestInstanceRemove, instance)
380
      del instance
381

    
382
    multinode_tests = [
383
      ('instance-add-drbd-disk',
384
       qa_instance.TestInstanceAddWithDrbdDisk),
385
    ]
386

    
387
    for name, func in multinode_tests:
388
      if qa_config.TestEnabled(name):
389
        snode = qa_config.AcquireNode(exclude=pnode)
390
        try:
391
          instance = RunTest(func, pnode, snode)
392
          RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
393
          RunCommonInstanceTests(instance)
394
          if qa_config.TestEnabled('instance-convert-disk'):
395
            RunTest(qa_instance.TestInstanceShutdown, instance)
396
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
397
            RunTest(qa_instance.TestInstanceStartup, instance)
398
          RunExportImportTests(instance, pnode, snode)
399
          RunHardwareFailureTests(instance, pnode, snode)
400
          RunTest(qa_instance.TestInstanceRemove, instance)
401
          del instance
402
        finally:
403
          qa_config.ReleaseNode(snode)
404

    
405
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
406
      for shutdown in [False, True]:
407
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
408
        expnode = qa_config.AcquireNode(exclude=pnode)
409
        try:
410
          if shutdown:
411
            # Stop instance before exporting and removing it
412
            RunTest(qa_instance.TestInstanceShutdown, instance)
413
          RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
414
          RunTest(qa_instance.TestBackupList, expnode)
415
        finally:
416
          qa_config.ReleaseNode(expnode)
417
        del expnode
418
        del instance
419

    
420
  finally:
421
    qa_config.ReleaseNode(pnode)
422

    
423
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
424

    
425
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
426

    
427

    
428
if __name__ == '__main__':
429
  main()