Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ a5877534

History | View | Annotate | Download (27.6 kB)

1
#!/usr/bin/python -u
2
#
3

    
4
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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
# pylint: disable=C0103
27
# due to invalid name
28

    
29
import copy
30
import datetime
31
import optparse
32
import sys
33

    
34
import qa_cluster
35
import qa_config
36
import qa_daemon
37
import qa_env
38
import qa_error
39
import qa_group
40
import qa_instance
41
import qa_monitoring
42
import qa_network
43
import qa_node
44
import qa_os
45
import qa_job
46
import qa_rapi
47
import qa_tags
48
import qa_utils
49

    
50
from ganeti import utils
51
from ganeti import rapi # pylint: disable=W0611
52
from ganeti import constants
53

    
54
import ganeti.rapi.client # pylint: disable=W0611
55
from ganeti.rapi.client import UsesRapiClient
56

    
57

    
58
def _FormatHeader(line, end=72):
59
  """Fill a line up to the end column.
60

61
  """
62
  line = "---- " + line + " "
63
  line += "-" * (end - len(line))
64
  line = line.rstrip()
65
  return line
66

    
67

    
68
def _DescriptionOf(fn):
69
  """Computes the description of an item.
70

71
  """
72
  if fn.__doc__:
73
    desc = fn.__doc__.splitlines()[0].strip()
74
  else:
75
    desc = "%r" % fn
76

    
77
  return desc.rstrip(".")
78

    
79

    
80
def RunTest(fn, *args, **kwargs):
81
  """Runs a test after printing a header.
82

83
  """
84

    
85
  tstart = datetime.datetime.now()
86

    
87
  desc = _DescriptionOf(fn)
88

    
89
  print
90
  print _FormatHeader("%s start %s" % (tstart, desc))
91

    
92
  try:
93
    retval = fn(*args, **kwargs)
94
    return retval
95
  finally:
96
    tstop = datetime.datetime.now()
97
    tdelta = tstop - tstart
98
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
99

    
100

    
101
def RunTestIf(testnames, fn, *args, **kwargs):
102
  """Runs a test conditionally.
103

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

107
  """
108
  if qa_config.TestEnabled(testnames):
109
    RunTest(fn, *args, **kwargs)
110
  else:
111
    tstart = datetime.datetime.now()
112
    desc = _DescriptionOf(fn)
113
    # TODO: Formatting test names when non-string names are involved
114
    print _FormatHeader("%s skipping %s, test(s) %s disabled" %
115
                        (tstart, desc, testnames))
116

    
117

    
118
def RunEnvTests():
119
  """Run several environment tests.
120

121
  """
122
  RunTestIf("env", qa_env.TestSshConnection)
123
  RunTestIf("env", qa_env.TestIcmpPing)
124
  RunTestIf("env", qa_env.TestGanetiCommands)
125

    
126

    
127
def SetupCluster(rapi_user, rapi_secret):
128
  """Initializes the cluster.
129

130
  @param rapi_user: Login user for RAPI
131
  @param rapi_secret: Login secret for RAPI
132

133
  """
134
  RunTestIf("create-cluster", qa_cluster.TestClusterInit,
135
            rapi_user, rapi_secret)
136
  if not qa_config.TestEnabled("create-cluster"):
137
    # If the cluster is already in place, we assume that exclusive-storage is
138
    # already set according to the configuration
139
    qa_config.SetExclusiveStorage(qa_config.get("exclusive-storage", False))
140

    
141
  # Test on empty cluster
142
  RunTestIf("node-list", qa_node.TestNodeList)
143
  RunTestIf("instance-list", qa_instance.TestInstanceList)
144
  RunTestIf("job-list", qa_job.TestJobList)
145

    
146
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
147
  if not qa_config.TestEnabled("create-cluster"):
148
    # consider the nodes are already there
149
    qa_node.MarkNodeAddedAll()
150

    
151
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
152

    
153
  # enable the watcher (unconditionally)
154
  RunTest(qa_daemon.TestResumeWatcher)
155

    
156
  RunTestIf("node-list", qa_node.TestNodeList)
157

    
158
  # Test listing fields
159
  RunTestIf("node-list", qa_node.TestNodeListFields)
160
  RunTestIf("instance-list", qa_instance.TestInstanceListFields)
161
  RunTestIf("job-list", qa_job.TestJobListFields)
162
  RunTestIf("instance-export", qa_instance.TestBackupListFields)
163

    
164
  RunTestIf("node-info", qa_node.TestNodeInfo)
165

    
166

    
167
def RunClusterTests():
168
  """Runs tests related to gnt-cluster.
169

170
  """
171
  for test, fn in [
172
    ("create-cluster", qa_cluster.TestClusterInitDisk),
173
    ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
174
    ("cluster-verify", qa_cluster.TestClusterVerify),
175
    ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
176
    # TODO: add more cluster modify tests
177
    ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
178
    ("cluster-modify", qa_cluster.TestClusterModifyIPolicy),
179
    ("cluster-modify", qa_cluster.TestClusterModifyISpecs),
180
    ("cluster-modify", qa_cluster.TestClusterModifyBe),
181
    ("cluster-modify", qa_cluster.TestClusterModifyDisk),
182
    ("cluster-modify", qa_cluster.TestClusterModifyDiskTemplates),
183
    ("cluster-rename", qa_cluster.TestClusterRename),
184
    ("cluster-info", qa_cluster.TestClusterVersion),
185
    ("cluster-info", qa_cluster.TestClusterInfo),
186
    ("cluster-info", qa_cluster.TestClusterGetmaster),
187
    ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
188
    (["cluster-copyfile", qa_config.NoVirtualCluster],
189
     qa_cluster.TestClusterCopyfile),
190
    ("cluster-command", qa_cluster.TestClusterCommand),
191
    ("cluster-burnin", qa_cluster.TestClusterBurnin),
192
    ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
193
    ("cluster-master-failover",
194
     qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
195
    (["cluster-oob", qa_config.NoVirtualCluster],
196
     qa_cluster.TestClusterOob),
197
    (qa_rapi.Enabled, qa_rapi.TestVersion),
198
    (qa_rapi.Enabled, qa_rapi.TestEmptyCluster),
199
    (qa_rapi.Enabled, qa_rapi.TestRapiQuery),
200
    ]:
201
    RunTestIf(test, fn)
202

    
203

    
204
def RunRepairDiskSizes():
205
  """Run the repair disk-sizes test.
206

207
  """
208
  RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
209

    
210

    
211
def RunOsTests():
212
  """Runs all tests related to gnt-os.
213

214
  """
215
  os_enabled = ["os", qa_config.NoVirtualCluster]
216

    
217
  if qa_config.TestEnabled(qa_rapi.Enabled):
218
    rapi_getos = qa_rapi.GetOperatingSystems
219
  else:
220
    rapi_getos = None
221

    
222
  for fn in [
223
    qa_os.TestOsList,
224
    qa_os.TestOsDiagnose,
225
    ]:
226
    RunTestIf(os_enabled, fn)
227

    
228
  for fn in [
229
    qa_os.TestOsValid,
230
    qa_os.TestOsInvalid,
231
    qa_os.TestOsPartiallyValid,
232
    ]:
233
    RunTestIf(os_enabled, fn, rapi_getos)
234

    
235
  for fn in [
236
    qa_os.TestOsModifyValid,
237
    qa_os.TestOsModifyInvalid,
238
    qa_os.TestOsStatesNonExisting,
239
    ]:
240
    RunTestIf(os_enabled, fn)
241

    
242

    
243
def RunCommonInstanceTests(instance):
244
  """Runs a few tests that are common to all disk types.
245

246
  """
247
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
248
  RunTestIf(["instance-shutdown", "instance-console", qa_rapi.Enabled],
249
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
250
  RunTestIf(["instance-shutdown", "instance-modify"],
251
            qa_instance.TestInstanceStoppedModify, instance)
252
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
253

    
254
  # Test shutdown/start via RAPI
255
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
256
            qa_rapi.TestRapiInstanceShutdown, instance)
257
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
258
            qa_rapi.TestRapiInstanceStartup, instance)
259

    
260
  RunTestIf("instance-list", qa_instance.TestInstanceList)
261

    
262
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
263

    
264
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
265
  RunTestIf(["instance-modify", qa_rapi.Enabled],
266
            qa_rapi.TestRapiInstanceModify, instance)
267

    
268
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
269
  RunTestIf(["instance-console", qa_rapi.Enabled],
270
            qa_rapi.TestRapiInstanceConsole, instance)
271

    
272
  RunTestIf("instance-device-names", qa_instance.TestInstanceDeviceNames,
273
            instance)
274
  DOWN_TESTS = qa_config.Either([
275
    "instance-reinstall",
276
    "instance-rename",
277
    "instance-grow-disk",
278
    ])
279

    
280
  # shutdown instance for any 'down' tests
281
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
282

    
283
  # now run the 'down' state tests
284
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
285
  RunTestIf(["instance-reinstall", qa_rapi.Enabled],
286
            qa_rapi.TestRapiInstanceReinstall, instance)
287

    
288
  if qa_config.TestEnabled("instance-rename"):
289
    tgt_instance = qa_config.AcquireInstance()
290
    try:
291
      rename_source = instance.name
292
      rename_target = tgt_instance.name
293
      # perform instance rename to the same name
294
      RunTest(qa_instance.TestInstanceRenameAndBack,
295
              rename_source, rename_source)
296
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
297
                rename_source, rename_source)
298
      if rename_target is not None:
299
        # perform instance rename to a different name, if we have one configured
300
        RunTest(qa_instance.TestInstanceRenameAndBack,
301
                rename_source, rename_target)
302
        RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
303
                  rename_source, rename_target)
304
    finally:
305
      tgt_instance.Release()
306

    
307
  RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
308

    
309
  # and now start the instance again
310
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
311

    
312
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
313

    
314
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
315

    
316
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
317

    
318
  RunTestIf(qa_rapi.Enabled, qa_rapi.TestInstance, instance)
319

    
320
  # Lists instances, too
321
  RunTestIf("node-list", qa_node.TestNodeList)
322

    
323
  # Some jobs have been run, let's test listing them
324
  RunTestIf("job-list", qa_job.TestJobList)
325

    
326

    
327
def RunCommonNodeTests():
328
  """Run a few common node tests.
329

330
  """
331
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
332
  RunTestIf("node-storage", qa_node.TestNodeStorage)
333
  RunTestIf(["node-oob", qa_config.NoVirtualCluster], qa_node.TestOutOfBand)
334

    
335

    
336
def RunGroupListTests():
337
  """Run tests for listing node groups.
338

339
  """
340
  RunTestIf("group-list", qa_group.TestGroupList)
341
  RunTestIf("group-list", qa_group.TestGroupListFields)
342

    
343

    
344
def RunNetworkTests():
345
  """Run tests for network management.
346

347
  """
348
  RunTestIf("network", qa_network.TestNetworkAddRemove)
349
  RunTestIf("network", qa_network.TestNetworkConnect)
350

    
351

    
352
def RunGroupRwTests():
353
  """Run tests for adding/removing/renaming groups.
354

355
  """
356
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
357
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
358
  RunTestIf("group-rwops", qa_group.TestGroupModify)
359
  RunTestIf(["group-rwops", qa_rapi.Enabled], qa_rapi.TestRapiNodeGroups)
360
  RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
361
            qa_group.GetDefaultGroup())
362

    
363

    
364
def RunExportImportTests(instance, inodes):
365
  """Tries to export and import the instance.
366

367
  @type inodes: list of nodes
368
  @param inodes: current nodes of the instance
369

370
  """
371
  # FIXME: export explicitly bails out on file based storage. other non-lvm
372
  # based storage types are untested, though. Also note that import could still
373
  # work, but is deeply embedded into the "export" case.
374
  if (qa_config.TestEnabled("instance-export") and
375
      instance.disk_template != constants.DT_FILE):
376
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
377

    
378
    pnode = inodes[0]
379
    expnode = qa_config.AcquireNode(exclude=pnode)
380
    try:
381
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
382

    
383
      RunTest(qa_instance.TestBackupList, expnode)
384

    
385
      if qa_config.TestEnabled("instance-import"):
386
        newinst = qa_config.AcquireInstance()
387
        try:
388
          RunTest(qa_instance.TestInstanceImport, newinst, pnode,
389
                  expnode, name)
390
          # Check if starting the instance works
391
          RunTest(qa_instance.TestInstanceStartup, newinst)
392
          RunTest(qa_instance.TestInstanceRemove, newinst)
393
        finally:
394
          newinst.Release()
395
    finally:
396
      expnode.Release()
397

    
398
  # FIXME: inter-cluster-instance-move crashes on file based instances :/
399
  # See Issue 414.
400
  if (qa_config.TestEnabled([qa_rapi.Enabled, "inter-cluster-instance-move"])
401
      and instance.disk_template != constants.DT_FILE):
402
    newinst = qa_config.AcquireInstance()
403
    try:
404
      tnode = qa_config.AcquireNode(exclude=inodes)
405
      try:
406
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
407
                inodes, tnode)
408
      finally:
409
        tnode.Release()
410
    finally:
411
      newinst.Release()
412

    
413

    
414
def RunDaemonTests(instance):
415
  """Test the ganeti-watcher script.
416

417
  """
418
  RunTest(qa_daemon.TestPauseWatcher)
419

    
420
  RunTestIf("instance-automatic-restart",
421
            qa_daemon.TestInstanceAutomaticRestart, instance)
422
  RunTestIf("instance-consecutive-failures",
423
            qa_daemon.TestInstanceConsecutiveFailures, instance)
424

    
425
  RunTest(qa_daemon.TestResumeWatcher)
426

    
427

    
428
def RunHardwareFailureTests(instance, inodes):
429
  """Test cluster internal hardware failure recovery.
430

431
  """
432
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
433
  RunTestIf(["instance-failover", qa_rapi.Enabled],
434
            qa_rapi.TestRapiInstanceFailover, instance)
435

    
436
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
437
  RunTestIf(["instance-migrate", qa_rapi.Enabled],
438
            qa_rapi.TestRapiInstanceMigrate, instance)
439

    
440
  if qa_config.TestEnabled("instance-replace-disks"):
441
    # We just need alternative secondary nodes, hence "- 1"
442
    othernodes = qa_config.AcquireManyNodes(len(inodes) - 1, exclude=inodes)
443
    try:
444
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceReplaceDisks, instance)
445
      RunTest(qa_instance.TestReplaceDisks,
446
              instance, inodes, othernodes)
447
    finally:
448
      qa_config.ReleaseManyNodes(othernodes)
449
    del othernodes
450

    
451
  if qa_config.TestEnabled("instance-recreate-disks"):
452
    try:
453
      acquirednodes = qa_config.AcquireManyNodes(len(inodes), exclude=inodes)
454
      othernodes = acquirednodes
455
    except qa_error.OutOfNodesError:
456
      if len(inodes) > 1:
457
        # If the cluster is not big enough, let's reuse some of the nodes, but
458
        # with different roles. In this way, we can test a DRBD instance even on
459
        # a 3-node cluster.
460
        acquirednodes = [qa_config.AcquireNode(exclude=inodes)]
461
        othernodes = acquirednodes + inodes[:-1]
462
      else:
463
        raise
464
    try:
465
      RunTest(qa_instance.TestRecreateDisks,
466
              instance, inodes, othernodes)
467
    finally:
468
      qa_config.ReleaseManyNodes(acquirednodes)
469

    
470
  if len(inodes) >= 2:
471
    RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, inodes[0], inodes[1])
472
    RunTestIf("node-failover", qa_node.TestNodeFailover, inodes[0], inodes[1])
473

    
474

    
475
def RunExclusiveStorageTests():
476
  """Test exclusive storage."""
477
  if not qa_config.TestEnabled("cluster-exclusive-storage"):
478
    return
479

    
480
  node = qa_config.AcquireNode()
481
  try:
482
    old_es = qa_cluster.TestSetExclStorCluster(False)
483
    qa_node.TestExclStorSingleNode(node)
484

    
485
    qa_cluster.TestSetExclStorCluster(True)
486
    qa_cluster.TestExclStorSharedPv(node)
487

    
488
    if qa_config.TestEnabled("instance-add-plain-disk"):
489
      # Make sure that the cluster doesn't have any pre-existing problem
490
      qa_cluster.AssertClusterVerify()
491

    
492
      # Create and allocate instances
493
      instance1 = qa_instance.TestInstanceAddWithPlainDisk([node])
494
      try:
495
        instance2 = qa_instance.TestInstanceAddWithPlainDisk([node])
496
        try:
497
          # cluster-verify checks that disks are allocated correctly
498
          qa_cluster.AssertClusterVerify()
499

    
500
          # Remove instances
501
          qa_instance.TestInstanceRemove(instance2)
502
          qa_instance.TestInstanceRemove(instance1)
503
        finally:
504
          instance2.Release()
505
      finally:
506
        instance1.Release()
507

    
508
    if qa_config.TestEnabled("instance-add-drbd-disk"):
509
      snode = qa_config.AcquireNode()
510
      try:
511
        qa_cluster.TestSetExclStorCluster(False)
512
        instance = qa_instance.TestInstanceAddWithDrbdDisk([node, snode])
513
        try:
514
          qa_cluster.TestSetExclStorCluster(True)
515
          exp_err = [constants.CV_EINSTANCEUNSUITABLENODE]
516
          qa_cluster.AssertClusterVerify(fail=True, errors=exp_err)
517
          qa_instance.TestInstanceRemove(instance)
518
        finally:
519
          instance.Release()
520
      finally:
521
        snode.Release()
522
    qa_cluster.TestSetExclStorCluster(old_es)
523
  finally:
524
    node.Release()
525

    
526

    
527
def _BuildSpecDict(par, mn, st, mx):
528
  return {
529
    constants.ISPECS_MINMAX: [{
530
      constants.ISPECS_MIN: {par: mn},
531
      constants.ISPECS_MAX: {par: mx},
532
      }],
533
    constants.ISPECS_STD: {par: st},
534
    }
535

    
536

    
537
def _BuildDoubleSpecDict(index, par, mn, st, mx):
538
  new_spec = {
539
    constants.ISPECS_MINMAX: [{}, {}],
540
    }
541
  if st is not None:
542
    new_spec[constants.ISPECS_STD] = {par: st}
543
  new_spec[constants.ISPECS_MINMAX][index] = {
544
    constants.ISPECS_MIN: {par: mn},
545
    constants.ISPECS_MAX: {par: mx},
546
    }
547
  return new_spec
548

    
549

    
550
def TestIPolicyPlainInstance():
551
  """Test instance policy interaction with instances"""
552
  params = ["memory-size", "cpu-count", "disk-count", "disk-size", "nic-count"]
553
  if not qa_config.IsTemplateSupported(constants.DT_PLAIN):
554
    print "Template %s not supported" % constants.DT_PLAIN
555
    return
556

    
557
  # This test assumes that the group policy is empty
558
  (_, old_specs) = qa_cluster.TestClusterSetISpecs()
559
  # We also assume to have only one min/max bound
560
  assert len(old_specs[constants.ISPECS_MINMAX]) == 1
561
  node = qa_config.AcquireNode()
562
  try:
563
    # Log of policy changes, list of tuples:
564
    # (full_change, incremental_change, policy_violated)
565
    history = []
566
    instance = qa_instance.TestInstanceAddWithPlainDisk([node])
567
    try:
568
      policyerror = [constants.CV_EINSTANCEPOLICY]
569
      for par in params:
570
        (iminval, imaxval) = qa_instance.GetInstanceSpec(instance.name, par)
571
        # Some specs must be multiple of 4
572
        new_spec = _BuildSpecDict(par, imaxval + 4, imaxval + 4, imaxval + 4)
573
        history.append((None, new_spec, True))
574
        if iminval > 0:
575
          # Some specs must be multiple of 4
576
          if iminval >= 4:
577
            upper = iminval - 4
578
          else:
579
            upper = iminval - 1
580
          new_spec = _BuildSpecDict(par, 0, upper, upper)
581
          history.append((None, new_spec, True))
582
        history.append((old_specs, None, False))
583

    
584
      # Test with two instance specs
585
      double_specs = copy.deepcopy(old_specs)
586
      double_specs[constants.ISPECS_MINMAX] = \
587
          double_specs[constants.ISPECS_MINMAX] * 2
588
      (par1, par2) = params[0:2]
589
      (_, imaxval1) = qa_instance.GetInstanceSpec(instance.name, par1)
590
      (_, imaxval2) = qa_instance.GetInstanceSpec(instance.name, par2)
591
      old_minmax = old_specs[constants.ISPECS_MINMAX][0]
592
      history.extend([
593
        (double_specs, None, False),
594
        # The first min/max limit is being violated
595
        (None,
596
         _BuildDoubleSpecDict(0, par1, imaxval1 + 4, imaxval1 + 4,
597
                              imaxval1 + 4),
598
         False),
599
        # Both min/max limits are being violated
600
        (None,
601
         _BuildDoubleSpecDict(1, par2, imaxval2 + 4, None, imaxval2 + 4),
602
         True),
603
        # The second min/max limit is being violated
604
        (None,
605
         _BuildDoubleSpecDict(0, par1,
606
                              old_minmax[constants.ISPECS_MIN][par1],
607
                              old_specs[constants.ISPECS_STD][par1],
608
                              old_minmax[constants.ISPECS_MAX][par1]),
609
         False),
610
        (old_specs, None, False),
611
        ])
612

    
613
      # Apply the changes, and check policy violations after each change
614
      qa_cluster.AssertClusterVerify()
615
      for (new_specs, diff_specs, failed) in history:
616
        qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
617
                                        diff_specs=diff_specs)
618
        if failed:
619
          qa_cluster.AssertClusterVerify(warnings=policyerror)
620
        else:
621
          qa_cluster.AssertClusterVerify()
622

    
623
      qa_instance.TestInstanceRemove(instance)
624
    finally:
625
      instance.Release()
626

    
627
    # Now we replay the same policy changes, and we expect that the instance
628
    # cannot be created for the cases where we had a policy violation above
629
    for (new_specs, diff_specs, failed) in history:
630
      qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
631
                                      diff_specs=diff_specs)
632
      if failed:
633
        qa_instance.TestInstanceAddWithPlainDisk([node], fail=True)
634
      # Instance creation with no policy violation has been tested already
635
  finally:
636
    node.Release()
637

    
638

    
639
def RunInstanceTests():
640
  """Create and exercise instances."""
641
  instance_tests = [
642
    ("instance-add-plain-disk", constants.DT_PLAIN,
643
     qa_instance.TestInstanceAddWithPlainDisk, 1),
644
    ("instance-add-drbd-disk", constants.DT_DRBD8,
645
     qa_instance.TestInstanceAddWithDrbdDisk, 2),
646
    ("instance-add-diskless", constants.DT_DISKLESS,
647
     qa_instance.TestInstanceAddDiskless, 1),
648
    ("instance-add-file", constants.DT_FILE,
649
     qa_instance.TestInstanceAddFile, 1)
650
    ]
651

    
652
  for (test_name, templ, create_fun, num_nodes) in instance_tests:
653
    if (qa_config.TestEnabled(test_name) and
654
        qa_config.IsTemplateSupported(templ)):
655
      inodes = qa_config.AcquireManyNodes(num_nodes)
656
      try:
657
        instance = RunTest(create_fun, inodes)
658
        try:
659
          RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
660
          RunDaemonTests(instance)
661
          for node in inodes:
662
            RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, node)
663
          if len(inodes) > 1:
664
            RunTestIf("group-rwops", qa_group.TestAssignNodesIncludingSplit,
665
                      constants.INITIAL_NODE_GROUP_NAME,
666
                      inodes[0].primary, inodes[1].primary)
667
          if qa_config.TestEnabled("instance-convert-disk"):
668
            RunTest(qa_instance.TestInstanceShutdown, instance)
669
            RunTest(qa_instance.TestInstanceConvertDiskToPlain,
670
                    instance, inodes)
671
            RunTest(qa_instance.TestInstanceStartup, instance)
672
          RunCommonInstanceTests(instance)
673
          if qa_config.TestEnabled("instance-modify-primary"):
674
            othernode = qa_config.AcquireNode()
675
            RunTest(qa_instance.TestInstanceModifyPrimaryAndBack,
676
                    instance, inodes[0], othernode)
677
            othernode.Release()
678
          RunGroupListTests()
679
          RunExportImportTests(instance, inodes)
680
          RunHardwareFailureTests(instance, inodes)
681
          RunRepairDiskSizes()
682
          RunTest(qa_instance.TestInstanceRemove, instance)
683
        finally:
684
          instance.Release()
685
        del instance
686
      finally:
687
        qa_config.ReleaseManyNodes(inodes)
688
      qa_cluster.AssertClusterVerify()
689

    
690

    
691
def RunMonitoringTests():
692
  if qa_config.TestEnabled("mon-collector"):
693
    RunTest(qa_monitoring.TestInstStatusCollector)
694

    
695

    
696
def RunQa():
697
  """Main QA body.
698

699
  """
700
  rapi_user = "ganeti-qa"
701
  rapi_secret = utils.GenerateSecret()
702

    
703
  RunEnvTests()
704
  SetupCluster(rapi_user, rapi_secret)
705

    
706
  if qa_rapi.Enabled():
707
    # Load RAPI certificate
708
    qa_rapi.Setup(rapi_user, rapi_secret)
709

    
710
  RunClusterTests()
711
  RunOsTests()
712

    
713
  RunTestIf("tags", qa_tags.TestClusterTags)
714

    
715
  RunCommonNodeTests()
716
  RunGroupListTests()
717
  RunGroupRwTests()
718
  RunNetworkTests()
719

    
720
  # The master shouldn't be readded or put offline; "delay" needs a non-master
721
  # node to test
722
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
723
  try:
724
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
725
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
726
    RunTestIf("delay", qa_cluster.TestDelay, pnode)
727
  finally:
728
    pnode.Release()
729

    
730
  # Make sure the cluster is clean before running instance tests
731
  qa_cluster.AssertClusterVerify()
732

    
733
  pnode = qa_config.AcquireNode()
734
  try:
735
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
736

    
737
    if qa_rapi.Enabled():
738
      RunTest(qa_rapi.TestNode, pnode)
739

    
740
      if qa_config.TestEnabled("instance-add-plain-disk"):
741
        for use_client in [True, False]:
742
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
743
                                  use_client)
744
          try:
745
            if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
746
              RunCommonInstanceTests(rapi_instance)
747
            RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
748
          finally:
749
            rapi_instance.Release()
750
          del rapi_instance
751

    
752
  finally:
753
    pnode.Release()
754

    
755
  config_list = [
756
    ("default-instance-tests", lambda: None, lambda _: None),
757
    ("exclusive-storage-instance-tests",
758
     lambda: qa_cluster.TestSetExclStorCluster(True),
759
     qa_cluster.TestSetExclStorCluster),
760
  ]
761
  for (conf_name, setup_conf_f, restore_conf_f) in config_list:
762
    if qa_config.TestEnabled(conf_name):
763
      oldconf = setup_conf_f()
764
      RunInstanceTests()
765
      restore_conf_f(oldconf)
766

    
767
  pnode = qa_config.AcquireNode()
768
  try:
769
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
770
      for shutdown in [False, True]:
771
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, [pnode])
772
        try:
773
          expnode = qa_config.AcquireNode(exclude=pnode)
774
          try:
775
            if shutdown:
776
              # Stop instance before exporting and removing it
777
              RunTest(qa_instance.TestInstanceShutdown, instance)
778
            RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
779
            RunTest(qa_instance.TestBackupList, expnode)
780
          finally:
781
            expnode.Release()
782
        finally:
783
          instance.Release()
784
        del expnode
785
        del instance
786
      qa_cluster.AssertClusterVerify()
787

    
788
  finally:
789
    pnode.Release()
790

    
791
  RunExclusiveStorageTests()
792
  RunTestIf(["cluster-instance-policy", "instance-add-plain-disk"],
793
            TestIPolicyPlainInstance)
794

    
795
  RunTestIf(
796
    "instance-add-restricted-by-disktemplates",
797
    qa_instance.TestInstanceCreationRestrictedByDiskTemplates)
798

    
799
  # Test removing instance with offline drbd secondary
800
  if qa_config.TestEnabled(["instance-remove-drbd-offline",
801
                            "instance-add-drbd-disk"]):
802
    # Make sure the master is not put offline
803
    snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
804
    try:
805
      pnode = qa_config.AcquireNode(exclude=snode)
806
      try:
807
        instance = qa_instance.TestInstanceAddWithDrbdDisk([pnode, snode])
808
        set_offline = lambda node: qa_node.MakeNodeOffline(node, "yes")
809
        set_online = lambda node: qa_node.MakeNodeOffline(node, "no")
810
        RunTest(qa_instance.TestRemoveInstanceOfflineNode, instance, snode,
811
                set_offline, set_online)
812
      finally:
813
        pnode.Release()
814
    finally:
815
      snode.Release()
816
    qa_cluster.AssertClusterVerify()
817

    
818
  RunMonitoringTests()
819

    
820
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
821

    
822
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
823

    
824

    
825
@UsesRapiClient
826
def main():
827
  """Main program.
828

829
  """
830
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
831
  parser.add_option("--yes-do-it", dest="yes_do_it",
832
                    action="store_true",
833
                    help="Really execute the tests")
834
  (opts, args) = parser.parse_args()
835

    
836
  if len(args) == 1:
837
    (config_file, ) = args
838
  else:
839
    parser.error("Wrong number of arguments.")
840

    
841
  if not opts.yes_do_it:
842
    print ("Executing this script irreversibly destroys any Ganeti\n"
843
           "configuration on all nodes involved. If you really want\n"
844
           "to start testing, supply the --yes-do-it option.")
845
    sys.exit(1)
846

    
847
  qa_config.Load(config_file)
848

    
849
  primary = qa_config.GetMasterNode().primary
850
  qa_utils.StartMultiplexer(primary)
851
  print ("SSH command for primary node: %s" %
852
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
853
  print ("SSH command for other nodes: %s" %
854
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
855
  try:
856
    RunQa()
857
  finally:
858
    qa_utils.CloseMultiplexers()
859

    
860
if __name__ == "__main__":
861
  main()