Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ e81d80bd

History | View | Annotate | Download (33 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 colors
35
import qa_cluster
36
import qa_config
37
import qa_daemon
38
import qa_env
39
import qa_error
40
import qa_group
41
import qa_instance
42
import qa_iptables
43
import qa_monitoring
44
import qa_network
45
import qa_node
46
import qa_os
47
import qa_job
48
import qa_rapi
49
import qa_tags
50
import qa_utils
51

    
52
from ganeti import utils
53
from ganeti import rapi # pylint: disable=W0611
54
from ganeti import constants
55
from ganeti import netutils
56

    
57
import ganeti.rapi.client # pylint: disable=W0611
58
from ganeti.rapi.client import UsesRapiClient
59

    
60

    
61
def _FormatHeader(line, end=72, mark="-", color=None):
62
  """Fill a line up to the end column.
63

64
  """
65
  line = (mark * 4) + " " + line + " "
66
  line += "-" * (end - len(line))
67
  line = line.rstrip()
68
  line = colors.colorize(line, color=color)
69
  return line
70

    
71

    
72
def _DescriptionOf(fn):
73
  """Computes the description of an item.
74

75
  """
76
  if fn.__doc__:
77
    desc = fn.__doc__.splitlines()[0].strip()
78
    desc = desc.rstrip(".")
79
    if fn.__name__:
80
      desc = "[" + fn.__name__ + "] " + desc
81
  else:
82
    desc = "%r" % fn
83

    
84
  return desc
85

    
86

    
87
def RunTest(fn, *args, **kwargs):
88
  """Runs a test after printing a header.
89

90
  """
91

    
92
  tstart = datetime.datetime.now()
93

    
94
  desc = _DescriptionOf(fn)
95

    
96
  print
97
  print _FormatHeader("%s start %s" % (tstart, desc),
98
                      color=colors.YELLOW, mark="<")
99

    
100
  try:
101
    retval = fn(*args, **kwargs)
102
    print _FormatHeader("PASSED %s" % (desc, ), color=colors.GREEN)
103
    return retval
104
  except Exception, e:
105
    print _FormatHeader("FAILED %s: %s" % (desc, e), color=colors.RED)
106
    raise
107
  finally:
108
    tstop = datetime.datetime.now()
109
    tdelta = tstop - tstart
110
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc),
111
                        color=colors.MAGENTA, mark=">")
112

    
113

    
114
def RunTestIf(testnames, fn, *args, **kwargs):
115
  """Runs a test conditionally.
116

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

120
  """
121
  if qa_config.TestEnabled(testnames):
122
    RunTest(fn, *args, **kwargs)
123
  else:
124
    tstart = datetime.datetime.now()
125
    desc = _DescriptionOf(fn)
126
    # TODO: Formatting test names when non-string names are involved
127
    print _FormatHeader("%s skipping %s, test(s) %s disabled" %
128
                        (tstart, desc, testnames),
129
                        color=colors.BLUE, mark="*")
130

    
131

    
132
def RunTestBlock(fn, *args, **kwargs):
133
  """Runs a block of tests after printing a header.
134

135
  """
136
  tstart = datetime.datetime.now()
137

    
138
  desc = _DescriptionOf(fn)
139

    
140
  print
141
  print _FormatHeader("BLOCK %s start %s" % (tstart, desc),
142
                      color=[colors.YELLOW, colors.BOLD], mark="v")
143

    
144
  try:
145
    return fn(*args, **kwargs)
146
  except Exception, e:
147
    print _FormatHeader("BLOCK FAILED %s: %s" % (desc, e),
148
                        color=[colors.RED, colors.BOLD])
149
    raise
150
  finally:
151
    tstop = datetime.datetime.now()
152
    tdelta = tstop - tstart
153
    print _FormatHeader("BLOCK %s time=%s %s" % (tstop, tdelta, desc),
154
                        color=[colors.MAGENTA, colors.BOLD], mark="^")
155

    
156

    
157
def RunEnvTests():
158
  """Run several environment tests.
159

160
  """
161
  RunTestIf("env", qa_env.TestSshConnection)
162
  RunTestIf("env", qa_env.TestIcmpPing)
163
  RunTestIf("env", qa_env.TestGanetiCommands)
164

    
165

    
166
def SetupCluster(rapi_user):
167
  """Initializes the cluster.
168

169
  @param rapi_user: Login user for RAPI
170
  @return: Login secret for RAPI
171

172
  """
173
  rapi_secret = utils.GenerateSecret()
174
  RunTestIf("create-cluster", qa_cluster.TestClusterInit,
175
            rapi_user, rapi_secret)
176
  if not qa_config.TestEnabled("create-cluster"):
177
    # If the cluster is already in place, we assume that exclusive-storage is
178
    # already set according to the configuration
179
    qa_config.SetExclusiveStorage(qa_config.get("exclusive-storage", False))
180
    if qa_rapi.Enabled():
181
      # To support RAPI on an existing cluster we have to find out the secret
182
      rapi_secret = qa_rapi.LookupRapiSecret(rapi_user)
183

    
184
  qa_group.ConfigureGroups()
185

    
186
  # Test on empty cluster
187
  RunTestIf("node-list", qa_node.TestNodeList)
188
  RunTestIf("instance-list", qa_instance.TestInstanceList)
189
  RunTestIf("job-list", qa_job.TestJobList)
190

    
191
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
192
  if not qa_config.TestEnabled("create-cluster"):
193
    # consider the nodes are already there
194
    qa_node.MarkNodeAddedAll()
195

    
196
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
197
  RunTestIf("test-jobqueue", qa_job.TestJobCancellation)
198

    
199
  # enable the watcher (unconditionally)
200
  RunTest(qa_daemon.TestResumeWatcher)
201

    
202
  RunTestIf("node-list", qa_node.TestNodeList)
203

    
204
  # Test listing fields
205
  RunTestIf("node-list", qa_node.TestNodeListFields)
206
  RunTestIf("instance-list", qa_instance.TestInstanceListFields)
207
  RunTestIf("job-list", qa_job.TestJobListFields)
208
  RunTestIf("instance-export", qa_instance.TestBackupListFields)
209

    
210
  RunTestIf("node-info", qa_node.TestNodeInfo)
211

    
212
  return rapi_secret
213

    
214

    
215
def RunClusterTests():
216
  """Runs tests related to gnt-cluster.
217

218
  """
219
  for test, fn in [
220
    ("create-cluster", qa_cluster.TestClusterInitDisk),
221
    ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
222
    ("cluster-verify", qa_cluster.TestClusterVerify),
223
    ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
224
    # TODO: add more cluster modify tests
225
    ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
226
    ("cluster-modify", qa_cluster.TestClusterModifyIPolicy),
227
    ("cluster-modify", qa_cluster.TestClusterModifyISpecs),
228
    ("cluster-modify", qa_cluster.TestClusterModifyBe),
229
    ("cluster-modify", qa_cluster.TestClusterModifyDisk),
230
    ("cluster-modify", qa_cluster.TestClusterModifyDiskTemplates),
231
    ("cluster-modify", qa_cluster.TestClusterModifyFileStorageDir),
232
    ("cluster-modify", qa_cluster.TestClusterModifySharedFileStorageDir),
233
    ("cluster-rename", qa_cluster.TestClusterRename),
234
    ("cluster-info", qa_cluster.TestClusterVersion),
235
    ("cluster-info", qa_cluster.TestClusterInfo),
236
    ("cluster-info", qa_cluster.TestClusterGetmaster),
237
    ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
238
    (["cluster-copyfile", qa_config.NoVirtualCluster],
239
     qa_cluster.TestClusterCopyfile),
240
    ("cluster-command", qa_cluster.TestClusterCommand),
241
    ("cluster-burnin", qa_cluster.TestClusterBurnin),
242
    ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
243
    ("cluster-master-failover",
244
     qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
245
    (["cluster-oob", qa_config.NoVirtualCluster],
246
     qa_cluster.TestClusterOob),
247
    ("cluster-instance-communication", qa_cluster.TestInstanceCommunication),
248
    (qa_rapi.Enabled, qa_rapi.TestVersion),
249
    (qa_rapi.Enabled, qa_rapi.TestEmptyCluster),
250
    (qa_rapi.Enabled, qa_rapi.TestRapiQuery),
251
    ]:
252
    RunTestIf(test, fn)
253

    
254

    
255
def RunRepairDiskSizes():
256
  """Run the repair disk-sizes test.
257

258
  """
259
  RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
260

    
261

    
262
def RunOsTests():
263
  """Runs all tests related to gnt-os.
264

265
  """
266
  os_enabled = ["os", qa_config.NoVirtualCluster]
267

    
268
  if qa_config.TestEnabled(qa_rapi.Enabled):
269
    rapi_getos = qa_rapi.GetOperatingSystems
270
  else:
271
    rapi_getos = None
272

    
273
  for fn in [
274
    qa_os.TestOsList,
275
    qa_os.TestOsDiagnose,
276
    ]:
277
    RunTestIf(os_enabled, fn)
278

    
279
  for fn in [
280
    qa_os.TestOsValid,
281
    qa_os.TestOsInvalid,
282
    qa_os.TestOsPartiallyValid,
283
    ]:
284
    RunTestIf(os_enabled, fn, rapi_getos)
285

    
286
  for fn in [
287
    qa_os.TestOsModifyValid,
288
    qa_os.TestOsModifyInvalid,
289
    qa_os.TestOsStatesNonExisting,
290
    ]:
291
    RunTestIf(os_enabled, fn)
292

    
293

    
294
def RunCommonInstanceTests(instance, inst_nodes):
295
  """Runs a few tests that are common to all disk types.
296

297
  """
298
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
299
  RunTestIf(["instance-shutdown", "instance-console", qa_rapi.Enabled],
300
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
301
  RunTestIf(["instance-shutdown", "instance-modify"],
302
            qa_instance.TestInstanceStoppedModify, instance)
303
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
304

    
305
  # Test shutdown/start via RAPI
306
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
307
            qa_rapi.TestRapiInstanceShutdown, instance)
308
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
309
            qa_rapi.TestRapiInstanceStartup, instance)
310

    
311
  RunTestIf("instance-list", qa_instance.TestInstanceList)
312

    
313
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
314

    
315
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
316
  RunTestIf(["instance-modify", qa_rapi.Enabled],
317
            qa_rapi.TestRapiInstanceModify, instance)
318

    
319
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
320
  RunTestIf(["instance-console", qa_rapi.Enabled],
321
            qa_rapi.TestRapiInstanceConsole, instance)
322

    
323
  RunTestIf("instance-device-names", qa_instance.TestInstanceDeviceNames,
324
            instance)
325
  DOWN_TESTS = qa_config.Either([
326
    "instance-reinstall",
327
    "instance-rename",
328
    "instance-grow-disk",
329
    ])
330

    
331
  # shutdown instance for any 'down' tests
332
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
333

    
334
  # now run the 'down' state tests
335
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
336
  RunTestIf(["instance-reinstall", qa_rapi.Enabled],
337
            qa_rapi.TestRapiInstanceReinstall, instance)
338

    
339
  if qa_config.TestEnabled("instance-rename"):
340
    tgt_instance = qa_config.AcquireInstance()
341
    try:
342
      rename_source = instance.name
343
      rename_target = tgt_instance.name
344
      # perform instance rename to the same name
345
      RunTest(qa_instance.TestInstanceRenameAndBack,
346
              rename_source, rename_source)
347
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
348
                rename_source, rename_source)
349
      if rename_target is not None:
350
        # perform instance rename to a different name, if we have one configured
351
        RunTest(qa_instance.TestInstanceRenameAndBack,
352
                rename_source, rename_target)
353
        RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
354
                  rename_source, rename_target)
355
    finally:
356
      tgt_instance.Release()
357

    
358
  RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
359

    
360
  # and now start the instance again
361
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
362

    
363
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
364

    
365
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
366

    
367
  if instance.disk_template == constants.DT_DRBD8:
368
    RunTestIf("cluster-verify",
369
              qa_cluster.TestClusterVerifyDisksBrokenDRBD, instance, inst_nodes)
370
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
371

    
372
  RunTestIf(qa_rapi.Enabled, qa_rapi.TestInstance, instance)
373

    
374
  # Lists instances, too
375
  RunTestIf("node-list", qa_node.TestNodeList)
376

    
377
  # Some jobs have been run, let's test listing them
378
  RunTestIf("job-list", qa_job.TestJobList)
379

    
380

    
381
def RunCommonNodeTests():
382
  """Run a few common node tests.
383

384
  """
385
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
386
  RunTestIf("node-storage", qa_node.TestNodeStorage)
387
  RunTestIf(["node-oob", qa_config.NoVirtualCluster], qa_node.TestOutOfBand)
388

    
389

    
390
def RunGroupListTests():
391
  """Run tests for listing node groups.
392

393
  """
394
  RunTestIf("group-list", qa_group.TestGroupList)
395
  RunTestIf("group-list", qa_group.TestGroupListFields)
396

    
397

    
398
def RunNetworkTests():
399
  """Run tests for network management.
400

401
  """
402
  RunTestIf("network", qa_network.TestNetworkAddRemove)
403
  RunTestIf("network", qa_network.TestNetworkConnect)
404
  RunTestIf(["network", "tags"], qa_network.TestNetworkTags)
405

    
406

    
407
def RunGroupRwTests():
408
  """Run tests for adding/removing/renaming groups.
409

410
  """
411
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
412
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
413
  RunTestIf("group-rwops", qa_group.TestGroupModify)
414
  RunTestIf(["group-rwops", qa_rapi.Enabled], qa_rapi.TestRapiNodeGroups)
415
  RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
416
            qa_group.GetDefaultGroup())
417

    
418

    
419
def RunExportImportTests(instance, inodes):
420
  """Tries to export and import the instance.
421

422
  @type inodes: list of nodes
423
  @param inodes: current nodes of the instance
424

425
  """
426
  # FIXME: export explicitly bails out on file based storage. other non-lvm
427
  # based storage types are untested, though. Also note that import could still
428
  # work, but is deeply embedded into the "export" case.
429
  if (qa_config.TestEnabled("instance-export") and
430
      instance.disk_template not in constants.DTS_FILEBASED):
431
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
432

    
433
    pnode = inodes[0]
434
    expnode = qa_config.AcquireNode(exclude=pnode)
435
    try:
436
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
437

    
438
      RunTest(qa_instance.TestBackupList, expnode)
439

    
440
      if qa_config.TestEnabled("instance-import"):
441
        newinst = qa_config.AcquireInstance()
442
        try:
443
          RunTest(qa_instance.TestInstanceImport, newinst, pnode,
444
                  expnode, name)
445
          # Check if starting the instance works
446
          RunTest(qa_instance.TestInstanceStartup, newinst)
447
          RunTest(qa_instance.TestInstanceRemove, newinst)
448
        finally:
449
          newinst.Release()
450
    finally:
451
      expnode.Release()
452

    
453
  # FIXME: inter-cluster-instance-move crashes on file based instances :/
454
  # See Issue 414.
455
  if (qa_config.TestEnabled([qa_rapi.Enabled, "inter-cluster-instance-move"])
456
      and (instance.disk_template not in constants.DTS_FILEBASED)):
457
    newinst = qa_config.AcquireInstance()
458
    try:
459
      tnode = qa_config.AcquireNode(exclude=inodes)
460
      try:
461
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
462
                inodes, tnode)
463
      finally:
464
        tnode.Release()
465
    finally:
466
      newinst.Release()
467

    
468

    
469
def RunDaemonTests(instance):
470
  """Test the ganeti-watcher script.
471

472
  """
473
  RunTest(qa_daemon.TestPauseWatcher)
474

    
475
  RunTestIf("instance-automatic-restart",
476
            qa_daemon.TestInstanceAutomaticRestart, instance)
477
  RunTestIf("instance-consecutive-failures",
478
            qa_daemon.TestInstanceConsecutiveFailures, instance)
479

    
480
  RunTest(qa_daemon.TestResumeWatcher)
481

    
482

    
483
def RunHardwareFailureTests(instance, inodes):
484
  """Test cluster internal hardware failure recovery.
485

486
  """
487
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
488
  RunTestIf(["instance-failover", qa_rapi.Enabled],
489
            qa_rapi.TestRapiInstanceFailover, instance)
490

    
491
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
492
  RunTestIf(["instance-migrate", qa_rapi.Enabled],
493
            qa_rapi.TestRapiInstanceMigrate, instance)
494

    
495
  if qa_config.TestEnabled("instance-replace-disks"):
496
    # We just need alternative secondary nodes, hence "- 1"
497
    othernodes = qa_config.AcquireManyNodes(len(inodes) - 1, exclude=inodes)
498
    try:
499
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceReplaceDisks, instance)
500
      RunTest(qa_instance.TestReplaceDisks,
501
              instance, inodes, othernodes)
502
    finally:
503
      qa_config.ReleaseManyNodes(othernodes)
504
    del othernodes
505

    
506
  if qa_config.TestEnabled("instance-recreate-disks"):
507
    try:
508
      acquirednodes = qa_config.AcquireManyNodes(len(inodes), exclude=inodes)
509
      othernodes = acquirednodes
510
    except qa_error.OutOfNodesError:
511
      if len(inodes) > 1:
512
        # If the cluster is not big enough, let's reuse some of the nodes, but
513
        # with different roles. In this way, we can test a DRBD instance even on
514
        # a 3-node cluster.
515
        acquirednodes = [qa_config.AcquireNode(exclude=inodes)]
516
        othernodes = acquirednodes + inodes[:-1]
517
      else:
518
        raise
519
    try:
520
      RunTest(qa_instance.TestRecreateDisks,
521
              instance, inodes, othernodes)
522
    finally:
523
      qa_config.ReleaseManyNodes(acquirednodes)
524

    
525
  if len(inodes) >= 2:
526
    RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, inodes[0], inodes[1])
527
    RunTestIf("node-failover", qa_node.TestNodeFailover, inodes[0], inodes[1])
528
    RunTestIf("node-migrate", qa_node.TestNodeMigrate, inodes[0], inodes[1])
529

    
530

    
531
def RunExclusiveStorageTests():
532
  """Test exclusive storage."""
533
  if not qa_config.TestEnabled("cluster-exclusive-storage"):
534
    return
535

    
536
  node = qa_config.AcquireNode()
537
  try:
538
    old_es = qa_cluster.TestSetExclStorCluster(False)
539
    qa_node.TestExclStorSingleNode(node)
540

    
541
    qa_cluster.TestSetExclStorCluster(True)
542
    qa_cluster.TestExclStorSharedPv(node)
543

    
544
    if qa_config.TestEnabled("instance-add-plain-disk"):
545
      # Make sure that the cluster doesn't have any pre-existing problem
546
      qa_cluster.AssertClusterVerify()
547

    
548
      # Create and allocate instances
549
      instance1 = qa_instance.TestInstanceAddWithPlainDisk([node])
550
      try:
551
        instance2 = qa_instance.TestInstanceAddWithPlainDisk([node])
552
        try:
553
          # cluster-verify checks that disks are allocated correctly
554
          qa_cluster.AssertClusterVerify()
555

    
556
          # Remove instances
557
          qa_instance.TestInstanceRemove(instance2)
558
          qa_instance.TestInstanceRemove(instance1)
559
        finally:
560
          instance2.Release()
561
      finally:
562
        instance1.Release()
563

    
564
    if qa_config.TestEnabled("instance-add-drbd-disk"):
565
      snode = qa_config.AcquireNode()
566
      try:
567
        qa_cluster.TestSetExclStorCluster(False)
568
        instance = qa_instance.TestInstanceAddWithDrbdDisk([node, snode])
569
        try:
570
          qa_cluster.TestSetExclStorCluster(True)
571
          exp_err = [constants.CV_EINSTANCEUNSUITABLENODE]
572
          qa_cluster.AssertClusterVerify(fail=True, errors=exp_err)
573
          qa_instance.TestInstanceRemove(instance)
574
        finally:
575
          instance.Release()
576
      finally:
577
        snode.Release()
578
    qa_cluster.TestSetExclStorCluster(old_es)
579
  finally:
580
    node.Release()
581

    
582

    
583
def RunCustomSshPortTests():
584
  """Test accessing nodes with custom SSH ports.
585

586
  This requires removing nodes, adding them to a new group, and then undoing
587
  the change.
588
  """
589
  if not qa_config.TestEnabled("group-custom-ssh-port"):
590
    return
591

    
592
  std_port = netutils.GetDaemonPort(constants.SSH)
593
  port = 211
594
  master = qa_config.GetMasterNode()
595
  with qa_config.AcquireManyNodesCtx(1, exclude=master) as nodes:
596
    # Checks if the node(s) could be contacted through IPv6.
597
    # If yes, better skip the whole test.
598

    
599
    for node in nodes:
600
      if qa_utils.UsesIPv6Connection(node.primary, std_port):
601
        print ("Node %s is likely to be reached using IPv6,"
602
               "skipping the test" % (node.primary, ))
603
        return
604

    
605
    for node in nodes:
606
      qa_node.NodeRemove(node)
607
    with qa_iptables.RulesContext(nodes) as r:
608
      with qa_group.NewGroupCtx() as group:
609
        qa_group.ModifyGroupSshPort(r, group, nodes, port)
610

    
611
        for node in nodes:
612
          qa_node.NodeAdd(node, group=group)
613

    
614
        # Make sure that the cluster doesn't have any pre-existing problem
615
        qa_cluster.AssertClusterVerify()
616

    
617
        # Create and allocate instances
618
        instance1 = qa_instance.TestInstanceAddWithPlainDisk(nodes)
619
        try:
620
          instance2 = qa_instance.TestInstanceAddWithPlainDisk(nodes)
621
          try:
622
            # cluster-verify checks that disks are allocated correctly
623
            qa_cluster.AssertClusterVerify()
624

    
625
            # Remove instances
626
            qa_instance.TestInstanceRemove(instance2)
627
            qa_instance.TestInstanceRemove(instance1)
628
          finally:
629
            instance2.Release()
630
        finally:
631
          instance1.Release()
632

    
633
        for node in nodes:
634
          qa_node.NodeRemove(node)
635

    
636
    for node in nodes:
637
      qa_node.NodeAdd(node)
638

    
639
    qa_cluster.AssertClusterVerify()
640

    
641

    
642
def _BuildSpecDict(par, mn, st, mx):
643
  return {
644
    constants.ISPECS_MINMAX: [{
645
      constants.ISPECS_MIN: {par: mn},
646
      constants.ISPECS_MAX: {par: mx},
647
      }],
648
    constants.ISPECS_STD: {par: st},
649
    }
650

    
651

    
652
def _BuildDoubleSpecDict(index, par, mn, st, mx):
653
  new_spec = {
654
    constants.ISPECS_MINMAX: [{}, {}],
655
    }
656
  if st is not None:
657
    new_spec[constants.ISPECS_STD] = {par: st}
658
  new_spec[constants.ISPECS_MINMAX][index] = {
659
    constants.ISPECS_MIN: {par: mn},
660
    constants.ISPECS_MAX: {par: mx},
661
    }
662
  return new_spec
663

    
664

    
665
def TestIPolicyPlainInstance():
666
  """Test instance policy interaction with instances"""
667
  params = ["memory-size", "cpu-count", "disk-count", "disk-size", "nic-count"]
668
  if not qa_config.IsTemplateSupported(constants.DT_PLAIN):
669
    print "Template %s not supported" % constants.DT_PLAIN
670
    return
671

    
672
  # This test assumes that the group policy is empty
673
  (_, old_specs) = qa_cluster.TestClusterSetISpecs()
674
  # We also assume to have only one min/max bound
675
  assert len(old_specs[constants.ISPECS_MINMAX]) == 1
676
  node = qa_config.AcquireNode()
677
  try:
678
    # Log of policy changes, list of tuples:
679
    # (full_change, incremental_change, policy_violated)
680
    history = []
681
    instance = qa_instance.TestInstanceAddWithPlainDisk([node])
682
    try:
683
      policyerror = [constants.CV_EINSTANCEPOLICY]
684
      for par in params:
685
        (iminval, imaxval) = qa_instance.GetInstanceSpec(instance.name, par)
686
        # Some specs must be multiple of 4
687
        new_spec = _BuildSpecDict(par, imaxval + 4, imaxval + 4, imaxval + 4)
688
        history.append((None, new_spec, True))
689
        if iminval > 0:
690
          # Some specs must be multiple of 4
691
          if iminval >= 4:
692
            upper = iminval - 4
693
          else:
694
            upper = iminval - 1
695
          new_spec = _BuildSpecDict(par, 0, upper, upper)
696
          history.append((None, new_spec, True))
697
        history.append((old_specs, None, False))
698

    
699
      # Test with two instance specs
700
      double_specs = copy.deepcopy(old_specs)
701
      double_specs[constants.ISPECS_MINMAX] = \
702
          double_specs[constants.ISPECS_MINMAX] * 2
703
      (par1, par2) = params[0:2]
704
      (_, imaxval1) = qa_instance.GetInstanceSpec(instance.name, par1)
705
      (_, imaxval2) = qa_instance.GetInstanceSpec(instance.name, par2)
706
      old_minmax = old_specs[constants.ISPECS_MINMAX][0]
707
      history.extend([
708
        (double_specs, None, False),
709
        # The first min/max limit is being violated
710
        (None,
711
         _BuildDoubleSpecDict(0, par1, imaxval1 + 4, imaxval1 + 4,
712
                              imaxval1 + 4),
713
         False),
714
        # Both min/max limits are being violated
715
        (None,
716
         _BuildDoubleSpecDict(1, par2, imaxval2 + 4, None, imaxval2 + 4),
717
         True),
718
        # The second min/max limit is being violated
719
        (None,
720
         _BuildDoubleSpecDict(0, par1,
721
                              old_minmax[constants.ISPECS_MIN][par1],
722
                              old_specs[constants.ISPECS_STD][par1],
723
                              old_minmax[constants.ISPECS_MAX][par1]),
724
         False),
725
        (old_specs, None, False),
726
        ])
727

    
728
      # Apply the changes, and check policy violations after each change
729
      qa_cluster.AssertClusterVerify()
730
      for (new_specs, diff_specs, failed) in history:
731
        qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
732
                                        diff_specs=diff_specs)
733
        if failed:
734
          qa_cluster.AssertClusterVerify(warnings=policyerror)
735
        else:
736
          qa_cluster.AssertClusterVerify()
737

    
738
      qa_instance.TestInstanceRemove(instance)
739
    finally:
740
      instance.Release()
741

    
742
    # Now we replay the same policy changes, and we expect that the instance
743
    # cannot be created for the cases where we had a policy violation above
744
    for (new_specs, diff_specs, failed) in history:
745
      qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
746
                                      diff_specs=diff_specs)
747
      if failed:
748
        qa_instance.TestInstanceAddWithPlainDisk([node], fail=True)
749
      # Instance creation with no policy violation has been tested already
750
  finally:
751
    node.Release()
752

    
753

    
754
def IsExclusiveStorageInstanceTestEnabled():
755
  test_name = "exclusive-storage-instance-tests"
756
  if qa_config.TestEnabled(test_name):
757
    vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
758
    vgscmd = utils.ShellQuoteArgs([
759
      "vgs", "--noheadings", "-o", "pv_count", vgname,
760
      ])
761
    nodes = qa_config.GetConfig()["nodes"]
762
    for node in nodes:
763
      try:
764
        pvnum = int(qa_utils.GetCommandOutput(node.primary, vgscmd))
765
      except Exception, e:
766
        msg = ("Cannot get the number of PVs on %s, needed by '%s': %s" %
767
               (node.primary, test_name, e))
768
        raise qa_error.Error(msg)
769
      if pvnum < 2:
770
        raise qa_error.Error("Node %s has not enough PVs (%s) to run '%s'" %
771
                             (node.primary, pvnum, test_name))
772
    res = True
773
  else:
774
    res = False
775
  return res
776

    
777

    
778
def RunInstanceTests():
779
  """Create and exercise instances."""
780

    
781
  for (test_name, templ, create_fun, num_nodes) in \
782
      qa_instance.available_instance_tests:
783
    if (qa_config.TestEnabled(test_name) and
784
        qa_config.IsTemplateSupported(templ)):
785
      inodes = qa_config.AcquireManyNodes(num_nodes)
786
      try:
787
        instance = RunTest(create_fun, inodes)
788
        try:
789
          RunTestIf("instance-user-down", qa_instance.TestInstanceUserDown,
790
                    instance, qa_config.GetMasterNode())
791
          RunTestIf("instance-communication",
792
                    qa_instance.TestInstanceCommunication,
793
                    instance,
794
                    qa_config.GetMasterNode())
795
          RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
796
          RunDaemonTests(instance)
797
          for node in inodes:
798
            RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, node)
799
          if len(inodes) > 1:
800
            RunTestIf("group-rwops", qa_group.TestAssignNodesIncludingSplit,
801
                      constants.INITIAL_NODE_GROUP_NAME,
802
                      inodes[0].primary, inodes[1].primary)
803
          if qa_config.TestEnabled("instance-convert-disk"):
804
            RunTest(qa_instance.TestInstanceShutdown, instance)
805
            RunTest(qa_instance.TestInstanceConvertDiskToPlain,
806
                    instance, inodes)
807
            RunTest(qa_instance.TestInstanceStartup, instance)
808
          RunTestIf("instance-modify-disks",
809
                    qa_instance.TestInstanceModifyDisks, instance)
810
          RunCommonInstanceTests(instance, inodes)
811
          if qa_config.TestEnabled("instance-modify-primary"):
812
            othernode = qa_config.AcquireNode()
813
            RunTest(qa_instance.TestInstanceModifyPrimaryAndBack,
814
                    instance, inodes[0], othernode)
815
            othernode.Release()
816
          RunGroupListTests()
817
          RunExportImportTests(instance, inodes)
818
          RunHardwareFailureTests(instance, inodes)
819
          RunRepairDiskSizes()
820
          RunTest(qa_instance.TestInstanceRemove, instance)
821
        finally:
822
          instance.Release()
823
        del instance
824
      finally:
825
        qa_config.ReleaseManyNodes(inodes)
826
      qa_cluster.AssertClusterVerify()
827

    
828

    
829
def RunMonitoringTests():
830
  if qa_config.TestEnabled("mon-collector"):
831
    RunTest(qa_monitoring.TestInstStatusCollector)
832

    
833

    
834
def RunQa():
835
  """Main QA body.
836

837
  """
838
  rapi_user = "ganeti-qa"
839

    
840
  RunTestBlock(RunEnvTests)
841
  rapi_secret = SetupCluster(rapi_user)
842

    
843
  if qa_rapi.Enabled():
844
    # Load RAPI certificate
845
    qa_rapi.Setup(rapi_user, rapi_secret)
846

    
847
  RunTestBlock(RunClusterTests)
848
  RunTestBlock(RunOsTests)
849

    
850
  RunTestIf("tags", qa_tags.TestClusterTags)
851

    
852
  RunTestBlock(RunCommonNodeTests)
853
  RunTestBlock(RunGroupListTests)
854
  RunTestBlock(RunGroupRwTests)
855
  RunTestBlock(RunNetworkTests)
856

    
857
  # The master shouldn't be readded or put offline; "delay" needs a non-master
858
  # node to test
859
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
860
  try:
861
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
862
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
863
    RunTestIf("delay", qa_cluster.TestDelay, pnode)
864
  finally:
865
    pnode.Release()
866

    
867
  # Make sure the cluster is clean before running instance tests
868
  qa_cluster.AssertClusterVerify()
869

    
870
  pnode = qa_config.AcquireNode()
871
  try:
872
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
873

    
874
    if qa_rapi.Enabled():
875
      RunTest(qa_rapi.TestNode, pnode)
876

    
877
      if (qa_config.TestEnabled("instance-add-plain-disk")
878
          and qa_config.IsTemplateSupported(constants.DT_PLAIN)):
879
        # Normal instance allocation via RAPI
880
        for use_client in [True, False]:
881
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
882
                                  use_client)
883
          try:
884
            if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
885
              RunCommonInstanceTests(rapi_instance, [pnode])
886
            RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
887
          finally:
888
            rapi_instance.Release()
889
          del rapi_instance
890

    
891
        # Multi-instance allocation
892
        rapi_instance_one, rapi_instance_two = \
893
          RunTest(qa_rapi.TestRapiInstanceMultiAlloc, pnode)
894

    
895
        try:
896
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance_one, True)
897
          RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance_two, True)
898
        finally:
899
          rapi_instance_one.Release()
900
          rapi_instance_two.Release()
901
  finally:
902
    pnode.Release()
903

    
904
  config_list = [
905
    ("default-instance-tests", lambda: None, lambda _: None),
906
    (IsExclusiveStorageInstanceTestEnabled,
907
     lambda: qa_cluster.TestSetExclStorCluster(True),
908
     qa_cluster.TestSetExclStorCluster),
909
  ]
910
  for (conf_name, setup_conf_f, restore_conf_f) in config_list:
911
    if qa_config.TestEnabled(conf_name):
912
      oldconf = setup_conf_f()
913
      RunTestBlock(RunInstanceTests)
914
      restore_conf_f(oldconf)
915

    
916
  pnode = qa_config.AcquireNode()
917
  try:
918
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
919
      for shutdown in [False, True]:
920
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, [pnode])
921
        try:
922
          expnode = qa_config.AcquireNode(exclude=pnode)
923
          try:
924
            if shutdown:
925
              # Stop instance before exporting and removing it
926
              RunTest(qa_instance.TestInstanceShutdown, instance)
927
            RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
928
            RunTest(qa_instance.TestBackupList, expnode)
929
          finally:
930
            expnode.Release()
931
        finally:
932
          instance.Release()
933
        del expnode
934
        del instance
935
      qa_cluster.AssertClusterVerify()
936

    
937
  finally:
938
    pnode.Release()
939

    
940
  RunTestIf("cluster-upgrade", qa_cluster.TestUpgrade)
941

    
942
  RunTestBlock(RunExclusiveStorageTests)
943
  RunTestIf(["cluster-instance-policy", "instance-add-plain-disk"],
944
            TestIPolicyPlainInstance)
945

    
946
  RunTestBlock(RunCustomSshPortTests)
947

    
948
  RunTestIf(
949
    "instance-add-restricted-by-disktemplates",
950
    qa_instance.TestInstanceCreationRestrictedByDiskTemplates)
951

    
952
  # Test removing instance with offline drbd secondary
953
  if qa_config.TestEnabled(["instance-remove-drbd-offline",
954
                            "instance-add-drbd-disk"]):
955
    # Make sure the master is not put offline
956
    snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
957
    try:
958
      pnode = qa_config.AcquireNode(exclude=snode)
959
      try:
960
        instance = qa_instance.TestInstanceAddWithDrbdDisk([pnode, snode])
961
        set_offline = lambda node: qa_node.MakeNodeOffline(node, "yes")
962
        set_online = lambda node: qa_node.MakeNodeOffline(node, "no")
963
        RunTest(qa_instance.TestRemoveInstanceOfflineNode, instance, snode,
964
                set_offline, set_online)
965
      finally:
966
        pnode.Release()
967
    finally:
968
      snode.Release()
969
    qa_cluster.AssertClusterVerify()
970

    
971
  RunTestBlock(RunMonitoringTests)
972

    
973
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
974

    
975
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
976

    
977

    
978
@UsesRapiClient
979
def main():
980
  """Main program.
981

982
  """
983
  colors.check_for_colors()
984

    
985
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
986
  parser.add_option("--yes-do-it", dest="yes_do_it",
987
                    action="store_true",
988
                    help="Really execute the tests")
989
  (opts, args) = parser.parse_args()
990

    
991
  if len(args) == 1:
992
    (config_file, ) = args
993
  else:
994
    parser.error("Wrong number of arguments.")
995

    
996
  if not opts.yes_do_it:
997
    print ("Executing this script irreversibly destroys any Ganeti\n"
998
           "configuration on all nodes involved. If you really want\n"
999
           "to start testing, supply the --yes-do-it option.")
1000
    sys.exit(1)
1001

    
1002
  qa_config.Load(config_file)
1003

    
1004
  primary = qa_config.GetMasterNode().primary
1005
  qa_utils.StartMultiplexer(primary)
1006
  print ("SSH command for primary node: %s" %
1007
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
1008
  print ("SSH command for other nodes: %s" %
1009
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
1010
  try:
1011
    RunQa()
1012
  finally:
1013
    qa_utils.CloseMultiplexers()
1014

    
1015
if __name__ == "__main__":
1016
  main()