Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 729c4377

History | View | Annotate | Download (12.7 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
    RunTest(qa_cluster.TestJobqueue)
105
  else:
106
    # consider the nodes are already there
107
    qa_node.MarkNodeAddedAll()
108

    
109
  # enable the watcher (unconditionally)
110
  RunTest(qa_daemon.TestResumeWatcher)
111

    
112
  if qa_config.TestEnabled('node-info'):
113
    RunTest(qa_node.TestNodeInfo)
114

    
115

    
116
def RunClusterTests():
117
  """Runs tests related to gnt-cluster.
118

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

    
123
  if qa_config.TestEnabled('cluster-verify'):
124
    RunTest(qa_cluster.TestClusterVerify)
125

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

    
129
  if qa_config.TestEnabled('cluster-rename'):
130
    RunTest(qa_cluster.TestClusterRename)
131

    
132
  if qa_config.TestEnabled('cluster-info'):
133
    RunTest(qa_cluster.TestClusterVersion)
134
    RunTest(qa_cluster.TestClusterInfo)
135
    RunTest(qa_cluster.TestClusterGetmaster)
136

    
137
  if qa_config.TestEnabled('cluster-copyfile'):
138
    RunTest(qa_cluster.TestClusterCopyfile)
139

    
140
  if qa_config.TestEnabled('cluster-command'):
141
    RunTest(qa_cluster.TestClusterCommand)
142

    
143
  if qa_config.TestEnabled('cluster-burnin'):
144
    RunTest(qa_cluster.TestClusterBurnin)
145

    
146
  if qa_config.TestEnabled('cluster-master-failover'):
147
    RunTest(qa_cluster.TestClusterMasterFailover)
148

    
149
  if qa_rapi.Enabled():
150
    RunTest(qa_rapi.TestVersion)
151
    RunTest(qa_rapi.TestEmptyCluster)
152

    
153

    
154
def RunOsTests():
155
  """Runs all tests related to gnt-os.
156

157
  """
158
  if not qa_config.TestEnabled('os'):
159
    return
160

    
161
  RunTest(qa_os.TestOsList)
162
  RunTest(qa_os.TestOsDiagnose)
163
  RunTest(qa_os.TestOsValid)
164
  RunTest(qa_os.TestOsInvalid)
165
  RunTest(qa_os.TestOsPartiallyValid)
166
  RunTest(qa_os.TestOsModifyValid)
167
  RunTest(qa_os.TestOsModifyInvalid)
168
  RunTest(qa_os.TestOsStates)
169

    
170

    
171
def RunCommonInstanceTests(instance):
172
  """Runs a few tests that are common to all disk types.
173

174
  """
175
  if qa_config.TestEnabled('instance-shutdown'):
176
    RunTest(qa_instance.TestInstanceShutdown, instance)
177
    RunTest(qa_instance.TestInstanceStartup, instance)
178

    
179
  if qa_config.TestEnabled('instance-list'):
180
    RunTest(qa_instance.TestInstanceList)
181

    
182
  if qa_config.TestEnabled('instance-info'):
183
    RunTest(qa_instance.TestInstanceInfo, instance)
184

    
185
  if qa_config.TestEnabled('instance-modify'):
186
    RunTest(qa_instance.TestInstanceModify, instance)
187
    if qa_rapi.Enabled():
188
      RunTest(qa_rapi.TestRapiInstanceModify, instance)
189

    
190
  if qa_config.TestEnabled('instance-console'):
191
    RunTest(qa_instance.TestInstanceConsole, instance)
192

    
193
  if qa_config.TestEnabled('instance-reinstall'):
194
    RunTest(qa_instance.TestInstanceShutdown, instance)
195
    RunTest(qa_instance.TestInstanceReinstall, instance)
196
    RunTest(qa_instance.TestInstanceStartup, instance)
197

    
198
  if qa_config.TestEnabled('instance-reboot'):
199
    RunTest(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
      if qa_rapi.Enabled():
210
        RunTest(qa_rapi.TestRapiInstanceRename, instance, rename_target)
211
      RunTest(qa_instance.TestInstanceStartup, instance)
212

    
213
  if qa_config.TestEnabled('tags'):
214
    RunTest(qa_tags.TestInstanceTags, instance)
215

    
216
  if qa_rapi.Enabled():
217
    RunTest(qa_rapi.TestInstance, instance)
218

    
219

    
220
def RunCommonNodeTests():
221
  """Run a few common node tests.
222

223
  """
224
  if qa_config.TestEnabled('node-volumes'):
225
    RunTest(qa_node.TestNodeVolumes)
226

    
227
  if qa_config.TestEnabled("node-storage"):
228
    RunTest(qa_node.TestNodeStorage)
229

    
230

    
231
def RunExportImportTests(instance, pnode, snode):
232
  """Tries to export and import the instance.
233

234
  @param pnode: current primary node of the instance
235
  @param snode: current secondary node of the instance, if any,
236
      otherwise None
237

238
  """
239
  if qa_config.TestEnabled('instance-export'):
240
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
241

    
242
    expnode = qa_config.AcquireNode(exclude=pnode)
243
    try:
244
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
245

    
246
      RunTest(qa_instance.TestBackupList, expnode)
247

    
248
      if qa_config.TestEnabled('instance-import'):
249
        newinst = qa_config.AcquireInstance()
250
        try:
251
          RunTest(qa_instance.TestInstanceImport, pnode, newinst,
252
                  expnode, name)
253
          RunTest(qa_instance.TestInstanceRemove, newinst)
254
        finally:
255
          qa_config.ReleaseInstance(newinst)
256
    finally:
257
      qa_config.ReleaseNode(expnode)
258

    
259
  if (qa_rapi.Enabled() and
260
      qa_config.TestEnabled("inter-cluster-instance-move")):
261
    newinst = qa_config.AcquireInstance()
262
    try:
263
      if snode is None:
264
        excl = [pnode]
265
      else:
266
        excl = [pnode, snode]
267
      tnode = qa_config.AcquireNode(exclude=excl)
268
      try:
269
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
270
                pnode, snode, tnode)
271
      finally:
272
        qa_config.ReleaseNode(tnode)
273
    finally:
274
      qa_config.ReleaseInstance(newinst)
275

    
276

    
277
def RunDaemonTests(instance, pnode):
278
  """Test the ganeti-watcher script.
279

280
  """
281
  automatic_restart = \
282
    qa_config.TestEnabled('instance-automatic-restart')
283
  consecutive_failures = \
284
    qa_config.TestEnabled('instance-consecutive-failures')
285

    
286
  RunTest(qa_daemon.TestPauseWatcher)
287
  if automatic_restart or consecutive_failures:
288

    
289
    if automatic_restart:
290
      RunTest(qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
291

    
292
    if consecutive_failures:
293
      RunTest(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
  if qa_config.TestEnabled('instance-failover'):
303
    RunTest(qa_instance.TestInstanceFailover, instance)
304

    
305
  if qa_config.TestEnabled("instance-migrate"):
306
    RunTest(qa_instance.TestInstanceMigrate, instance)
307

    
308
    if qa_rapi.Enabled():
309
      RunTest(qa_rapi.TestRapiInstanceMigrate, instance)
310

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

    
319
  if qa_config.TestEnabled('node-evacuate'):
320
    RunTest(qa_node.TestNodeEvacuate, pnode, snode)
321

    
322
  if qa_config.TestEnabled('node-failover'):
323
    RunTest(qa_node.TestNodeFailover, pnode, snode)
324

    
325
  if qa_config.TestEnabled('instance-disk-failure'):
326
    RunTest(qa_instance.TestInstanceMasterDiskFailure,
327
            instance, pnode, snode)
328
    RunTest(qa_instance.TestInstanceSecondaryDiskFailure,
329
            instance, pnode, snode)
330

    
331

    
332
@rapi.client.UsesRapiClient
333
def main():
334
  """Main program.
335

336
  """
337
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
338
  parser.add_option('--yes-do-it', dest='yes_do_it',
339
      action="store_true",
340
      help="Really execute the tests")
341
  (qa_config.options, args) = parser.parse_args()
342

    
343
  if len(args) == 1:
344
    (config_file, ) = args
345
  else:
346
    parser.error("Wrong number of arguments.")
347

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

    
354
  qa_config.Load(config_file)
355

    
356
  rapi_user = "ganeti-qa"
357
  rapi_secret = utils.GenerateSecret()
358

    
359
  RunEnvTests()
360
  SetupCluster(rapi_user, rapi_secret)
361

    
362
  # Load RAPI certificate
363
  qa_rapi.Setup(rapi_user, rapi_secret)
364

    
365
  RunClusterTests()
366
  RunOsTests()
367

    
368
  if qa_config.TestEnabled('tags'):
369
    RunTest(qa_tags.TestClusterTags)
370

    
371
  RunCommonNodeTests()
372

    
373
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
374
  try:
375
    if qa_config.TestEnabled('node-readd'):
376
      RunTest(qa_node.TestNodeReadd, pnode)
377

    
378
    if qa_config.TestEnabled("node-modify"):
379
      RunTest(qa_node.TestNodeModify, pnode)
380
  finally:
381
    qa_config.ReleaseNode(pnode)
382

    
383
  pnode = qa_config.AcquireNode()
384
  try:
385
    if qa_config.TestEnabled('tags'):
386
      RunTest(qa_tags.TestNodeTags, pnode)
387

    
388
    if qa_rapi.Enabled():
389
      RunTest(qa_rapi.TestNode, pnode)
390

    
391
      if qa_config.TestEnabled("instance-add-plain-disk"):
392
        for use_client in [True, False]:
393
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
394
                                  use_client)
395
          RunCommonInstanceTests(rapi_instance)
396
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
397
          del rapi_instance
398

    
399
    if qa_config.TestEnabled('instance-add-plain-disk'):
400
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
401
      RunCommonInstanceTests(instance)
402
      RunExportImportTests(instance, pnode, None)
403
      RunDaemonTests(instance, pnode)
404
      RunTest(qa_instance.TestInstanceRemove, instance)
405
      del instance
406

    
407
    multinode_tests = [
408
      ('instance-add-drbd-disk',
409
       qa_instance.TestInstanceAddWithDrbdDisk),
410
    ]
411

    
412
    for name, func in multinode_tests:
413
      if qa_config.TestEnabled(name):
414
        snode = qa_config.AcquireNode(exclude=pnode)
415
        try:
416
          instance = RunTest(func, pnode, snode)
417
          RunCommonInstanceTests(instance)
418
          if qa_config.TestEnabled('instance-convert-disk'):
419
            RunTest(qa_instance.TestInstanceShutdown, instance)
420
            RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
421
            RunTest(qa_instance.TestInstanceStartup, instance)
422
          RunExportImportTests(instance, pnode, snode)
423
          RunHardwareFailureTests(instance, pnode, snode)
424
          RunTest(qa_instance.TestInstanceRemove, instance)
425
          del instance
426
        finally:
427
          qa_config.ReleaseNode(snode)
428

    
429
    if (qa_config.TestEnabled('instance-add-plain-disk') and
430
        qa_config.TestEnabled("instance-export")):
431
      instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
432
      expnode = qa_config.AcquireNode(exclude=pnode)
433
      try:
434
        RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
435
        RunTest(qa_instance.TestBackupList, expnode)
436
      finally:
437
        qa_config.ReleaseNode(expnode)
438
      del expnode
439
      del instance
440

    
441
  finally:
442
    qa_config.ReleaseNode(pnode)
443

    
444
  if qa_config.TestEnabled('create-cluster'):
445
    RunTest(qa_node.TestNodeRemoveAll)
446

    
447
  if qa_config.TestEnabled('cluster-destroy'):
448
    RunTest(qa_cluster.TestClusterDestroy)
449

    
450

    
451
if __name__ == '__main__':
452
  main()