Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ a02dbfca

History | View | Annotate | Download (33.1 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
from ganeti import pathutils
57

    
58
from ganeti.http.auth import ParsePasswordFile
59
import ganeti.rapi.client # pylint: disable=W0611
60
from ganeti.rapi.client import UsesRapiClient
61

    
62

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

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

    
73

    
74
def _DescriptionOf(fn):
75
  """Computes the description of an item.
76

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

    
86
  return desc
87

    
88

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

92
  """
93

    
94
  tstart = datetime.datetime.now()
95

    
96
  desc = _DescriptionOf(fn)
97

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

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

    
115

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

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

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

    
133

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

137
  """
138
  tstart = datetime.datetime.now()
139

    
140
  desc = _DescriptionOf(fn)
141

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

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

    
158

    
159
def RunEnvTests():
160
  """Run several environment tests.
161

162
  """
163
  RunTestIf("env", qa_env.TestSshConnection)
164
  RunTestIf("env", qa_env.TestIcmpPing)
165
  RunTestIf("env", qa_env.TestGanetiCommands)
166

    
167

    
168
def _LookupRapiSecret(rapi_user):
169
  """Find the RAPI secret for the given user.
170

171
  @param rapi_user: Login user
172
  @return: Login secret for the user
173

174
  """
175
  CTEXT = "{CLEARTEXT}"
176
  master = qa_config.GetMasterNode()
177
  cmd = ["cat", qa_utils.MakeNodePath(master, pathutils.RAPI_USERS_FILE)]
178
  file_content = qa_utils.GetCommandOutput(master.primary,
179
                                           utils.ShellQuoteArgs(cmd))
180
  users = ParsePasswordFile(file_content)
181
  entry = users.get(rapi_user)
182
  if not entry:
183
    raise qa_error.Error("User %s not found in RAPI users file" % rapi_user)
184
  secret = entry.password
185
  if secret.upper().startswith(CTEXT):
186
    secret = secret[len(CTEXT):]
187
  elif secret.startswith("{"):
188
    raise qa_error.Error("Unsupported password schema for RAPI user %s:"
189
                         " not a clear text password" % rapi_user)
190
  return secret
191

    
192

    
193
def SetupCluster(rapi_user):
194
  """Initializes the cluster.
195

196
  @param rapi_user: Login user for RAPI
197
  @return: Login secret for RAPI
198

199
  """
200
  rapi_secret = utils.GenerateSecret()
201
  RunTestIf("create-cluster", qa_cluster.TestClusterInit,
202
            rapi_user, rapi_secret)
203
  if not qa_config.TestEnabled("create-cluster"):
204
    # If the cluster is already in place, we assume that exclusive-storage is
205
    # already set according to the configuration
206
    qa_config.SetExclusiveStorage(qa_config.get("exclusive-storage", False))
207
    if qa_rapi.Enabled():
208
      # To support RAPI on an existing cluster we have to find out the secret
209
      rapi_secret = _LookupRapiSecret(rapi_user)
210

    
211
  qa_group.ConfigureGroups()
212

    
213
  # Test on empty cluster
214
  RunTestIf("node-list", qa_node.TestNodeList)
215
  RunTestIf("instance-list", qa_instance.TestInstanceList)
216
  RunTestIf("job-list", qa_job.TestJobList)
217

    
218
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
219
  if not qa_config.TestEnabled("create-cluster"):
220
    # consider the nodes are already there
221
    qa_node.MarkNodeAddedAll()
222

    
223
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
224

    
225
  # enable the watcher (unconditionally)
226
  RunTest(qa_daemon.TestResumeWatcher)
227

    
228
  RunTestIf("node-list", qa_node.TestNodeList)
229

    
230
  # Test listing fields
231
  RunTestIf("node-list", qa_node.TestNodeListFields)
232
  RunTestIf("instance-list", qa_instance.TestInstanceListFields)
233
  RunTestIf("job-list", qa_job.TestJobListFields)
234
  RunTestIf("instance-export", qa_instance.TestBackupListFields)
235

    
236
  RunTestIf("node-info", qa_node.TestNodeInfo)
237

    
238
  return rapi_secret
239

    
240

    
241
def RunClusterTests():
242
  """Runs tests related to gnt-cluster.
243

244
  """
245
  for test, fn in [
246
    ("create-cluster", qa_cluster.TestClusterInitDisk),
247
    ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
248
    ("cluster-verify", qa_cluster.TestClusterVerify),
249
    ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
250
    # TODO: add more cluster modify tests
251
    ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
252
    ("cluster-modify", qa_cluster.TestClusterModifyIPolicy),
253
    ("cluster-modify", qa_cluster.TestClusterModifyISpecs),
254
    ("cluster-modify", qa_cluster.TestClusterModifyBe),
255
    ("cluster-modify", qa_cluster.TestClusterModifyDisk),
256
    ("cluster-modify", qa_cluster.TestClusterModifyDiskTemplates),
257
    ("cluster-modify", qa_cluster.TestClusterModifyFileStorageDir),
258
    ("cluster-modify", qa_cluster.TestClusterModifySharedFileStorageDir),
259
    ("cluster-rename", qa_cluster.TestClusterRename),
260
    ("cluster-info", qa_cluster.TestClusterVersion),
261
    ("cluster-info", qa_cluster.TestClusterInfo),
262
    ("cluster-info", qa_cluster.TestClusterGetmaster),
263
    ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
264
    (["cluster-copyfile", qa_config.NoVirtualCluster],
265
     qa_cluster.TestClusterCopyfile),
266
    ("cluster-command", qa_cluster.TestClusterCommand),
267
    ("cluster-burnin", qa_cluster.TestClusterBurnin),
268
    ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
269
    ("cluster-master-failover",
270
     qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
271
    (["cluster-oob", qa_config.NoVirtualCluster],
272
     qa_cluster.TestClusterOob),
273
    (qa_rapi.Enabled, qa_rapi.TestVersion),
274
    (qa_rapi.Enabled, qa_rapi.TestEmptyCluster),
275
    (qa_rapi.Enabled, qa_rapi.TestRapiQuery),
276
    ]:
277
    RunTestIf(test, fn)
278

    
279

    
280
def RunRepairDiskSizes():
281
  """Run the repair disk-sizes test.
282

283
  """
284
  RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
285

    
286

    
287
def RunOsTests():
288
  """Runs all tests related to gnt-os.
289

290
  """
291
  os_enabled = ["os", qa_config.NoVirtualCluster]
292

    
293
  if qa_config.TestEnabled(qa_rapi.Enabled):
294
    rapi_getos = qa_rapi.GetOperatingSystems
295
  else:
296
    rapi_getos = None
297

    
298
  for fn in [
299
    qa_os.TestOsList,
300
    qa_os.TestOsDiagnose,
301
    ]:
302
    RunTestIf(os_enabled, fn)
303

    
304
  for fn in [
305
    qa_os.TestOsValid,
306
    qa_os.TestOsInvalid,
307
    qa_os.TestOsPartiallyValid,
308
    ]:
309
    RunTestIf(os_enabled, fn, rapi_getos)
310

    
311
  for fn in [
312
    qa_os.TestOsModifyValid,
313
    qa_os.TestOsModifyInvalid,
314
    qa_os.TestOsStatesNonExisting,
315
    ]:
316
    RunTestIf(os_enabled, fn)
317

    
318

    
319
def RunCommonInstanceTests(instance, inst_nodes):
320
  """Runs a few tests that are common to all disk types.
321

322
  """
323
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
324
  RunTestIf(["instance-shutdown", "instance-console", qa_rapi.Enabled],
325
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
326
  RunTestIf(["instance-shutdown", "instance-modify"],
327
            qa_instance.TestInstanceStoppedModify, instance)
328
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
329

    
330
  # Test shutdown/start via RAPI
331
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
332
            qa_rapi.TestRapiInstanceShutdown, instance)
333
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
334
            qa_rapi.TestRapiInstanceStartup, instance)
335

    
336
  RunTestIf("instance-list", qa_instance.TestInstanceList)
337

    
338
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
339

    
340
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
341
  RunTestIf(["instance-modify", qa_rapi.Enabled],
342
            qa_rapi.TestRapiInstanceModify, instance)
343

    
344
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
345
  RunTestIf(["instance-console", qa_rapi.Enabled],
346
            qa_rapi.TestRapiInstanceConsole, instance)
347

    
348
  RunTestIf("instance-device-names", qa_instance.TestInstanceDeviceNames,
349
            instance)
350
  DOWN_TESTS = qa_config.Either([
351
    "instance-reinstall",
352
    "instance-rename",
353
    "instance-grow-disk",
354
    ])
355

    
356
  # shutdown instance for any 'down' tests
357
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
358

    
359
  # now run the 'down' state tests
360
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
361
  RunTestIf(["instance-reinstall", qa_rapi.Enabled],
362
            qa_rapi.TestRapiInstanceReinstall, instance)
363

    
364
  if qa_config.TestEnabled("instance-rename"):
365
    tgt_instance = qa_config.AcquireInstance()
366
    try:
367
      rename_source = instance.name
368
      rename_target = tgt_instance.name
369
      # perform instance rename to the same name
370
      RunTest(qa_instance.TestInstanceRenameAndBack,
371
              rename_source, rename_source)
372
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
373
                rename_source, rename_source)
374
      if rename_target is not None:
375
        # perform instance rename to a different name, if we have one configured
376
        RunTest(qa_instance.TestInstanceRenameAndBack,
377
                rename_source, rename_target)
378
        RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
379
                  rename_source, rename_target)
380
    finally:
381
      tgt_instance.Release()
382

    
383
  RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
384

    
385
  # and now start the instance again
386
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
387

    
388
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
389

    
390
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
391

    
392
  if instance.disk_template == constants.DT_DRBD8:
393
    RunTestIf("cluster-verify",
394
              qa_cluster.TestClusterVerifyDisksBrokenDRBD, instance, inst_nodes)
395
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
396

    
397
  RunTestIf(qa_rapi.Enabled, qa_rapi.TestInstance, instance)
398

    
399
  # Lists instances, too
400
  RunTestIf("node-list", qa_node.TestNodeList)
401

    
402
  # Some jobs have been run, let's test listing them
403
  RunTestIf("job-list", qa_job.TestJobList)
404

    
405

    
406
def RunCommonNodeTests():
407
  """Run a few common node tests.
408

409
  """
410
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
411
  RunTestIf("node-storage", qa_node.TestNodeStorage)
412
  RunTestIf(["node-oob", qa_config.NoVirtualCluster], qa_node.TestOutOfBand)
413

    
414

    
415
def RunGroupListTests():
416
  """Run tests for listing node groups.
417

418
  """
419
  RunTestIf("group-list", qa_group.TestGroupList)
420
  RunTestIf("group-list", qa_group.TestGroupListFields)
421

    
422

    
423
def RunNetworkTests():
424
  """Run tests for network management.
425

426
  """
427
  RunTestIf("network", qa_network.TestNetworkAddRemove)
428
  RunTestIf("network", qa_network.TestNetworkConnect)
429

    
430

    
431
def RunGroupRwTests():
432
  """Run tests for adding/removing/renaming groups.
433

434
  """
435
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
436
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
437
  RunTestIf("group-rwops", qa_group.TestGroupModify)
438
  RunTestIf(["group-rwops", qa_rapi.Enabled], qa_rapi.TestRapiNodeGroups)
439
  RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
440
            qa_group.GetDefaultGroup())
441

    
442

    
443
def RunExportImportTests(instance, inodes):
444
  """Tries to export and import the instance.
445

446
  @type inodes: list of nodes
447
  @param inodes: current nodes of the instance
448

449
  """
450
  # FIXME: export explicitly bails out on file based storage. other non-lvm
451
  # based storage types are untested, though. Also note that import could still
452
  # work, but is deeply embedded into the "export" case.
453
  if (qa_config.TestEnabled("instance-export") and
454
      instance.disk_template not in constants.DTS_FILEBASED):
455
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
456

    
457
    pnode = inodes[0]
458
    expnode = qa_config.AcquireNode(exclude=pnode)
459
    try:
460
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
461

    
462
      RunTest(qa_instance.TestBackupList, expnode)
463

    
464
      if qa_config.TestEnabled("instance-import"):
465
        newinst = qa_config.AcquireInstance()
466
        try:
467
          RunTest(qa_instance.TestInstanceImport, newinst, pnode,
468
                  expnode, name)
469
          # Check if starting the instance works
470
          RunTest(qa_instance.TestInstanceStartup, newinst)
471
          RunTest(qa_instance.TestInstanceRemove, newinst)
472
        finally:
473
          newinst.Release()
474
    finally:
475
      expnode.Release()
476

    
477
  # FIXME: inter-cluster-instance-move crashes on file based instances :/
478
  # See Issue 414.
479
  if (qa_config.TestEnabled([qa_rapi.Enabled, "inter-cluster-instance-move"])
480
      and (instance.disk_template not in constants.DTS_FILEBASED)):
481
    newinst = qa_config.AcquireInstance()
482
    try:
483
      tnode = qa_config.AcquireNode(exclude=inodes)
484
      try:
485
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
486
                inodes, tnode)
487
      finally:
488
        tnode.Release()
489
    finally:
490
      newinst.Release()
491

    
492

    
493
def RunDaemonTests(instance):
494
  """Test the ganeti-watcher script.
495

496
  """
497
  RunTest(qa_daemon.TestPauseWatcher)
498

    
499
  RunTestIf("instance-automatic-restart",
500
            qa_daemon.TestInstanceAutomaticRestart, instance)
501
  RunTestIf("instance-consecutive-failures",
502
            qa_daemon.TestInstanceConsecutiveFailures, instance)
503

    
504
  RunTest(qa_daemon.TestResumeWatcher)
505

    
506

    
507
def RunHardwareFailureTests(instance, inodes):
508
  """Test cluster internal hardware failure recovery.
509

510
  """
511
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
512
  RunTestIf(["instance-failover", qa_rapi.Enabled],
513
            qa_rapi.TestRapiInstanceFailover, instance)
514

    
515
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
516
  RunTestIf(["instance-migrate", qa_rapi.Enabled],
517
            qa_rapi.TestRapiInstanceMigrate, instance)
518

    
519
  if qa_config.TestEnabled("instance-replace-disks"):
520
    # We just need alternative secondary nodes, hence "- 1"
521
    othernodes = qa_config.AcquireManyNodes(len(inodes) - 1, exclude=inodes)
522
    try:
523
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceReplaceDisks, instance)
524
      RunTest(qa_instance.TestReplaceDisks,
525
              instance, inodes, othernodes)
526
    finally:
527
      qa_config.ReleaseManyNodes(othernodes)
528
    del othernodes
529

    
530
  if qa_config.TestEnabled("instance-recreate-disks"):
531
    try:
532
      acquirednodes = qa_config.AcquireManyNodes(len(inodes), exclude=inodes)
533
      othernodes = acquirednodes
534
    except qa_error.OutOfNodesError:
535
      if len(inodes) > 1:
536
        # If the cluster is not big enough, let's reuse some of the nodes, but
537
        # with different roles. In this way, we can test a DRBD instance even on
538
        # a 3-node cluster.
539
        acquirednodes = [qa_config.AcquireNode(exclude=inodes)]
540
        othernodes = acquirednodes + inodes[:-1]
541
      else:
542
        raise
543
    try:
544
      RunTest(qa_instance.TestRecreateDisks,
545
              instance, inodes, othernodes)
546
    finally:
547
      qa_config.ReleaseManyNodes(acquirednodes)
548

    
549
  if len(inodes) >= 2:
550
    RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, inodes[0], inodes[1])
551
    RunTestIf("node-failover", qa_node.TestNodeFailover, inodes[0], inodes[1])
552
    RunTestIf("node-migrate", qa_node.TestNodeMigrate, inodes[0], inodes[1])
553

    
554

    
555
def RunExclusiveStorageTests():
556
  """Test exclusive storage."""
557
  if not qa_config.TestEnabled("cluster-exclusive-storage"):
558
    return
559

    
560
  node = qa_config.AcquireNode()
561
  try:
562
    old_es = qa_cluster.TestSetExclStorCluster(False)
563
    qa_node.TestExclStorSingleNode(node)
564

    
565
    qa_cluster.TestSetExclStorCluster(True)
566
    qa_cluster.TestExclStorSharedPv(node)
567

    
568
    if qa_config.TestEnabled("instance-add-plain-disk"):
569
      # Make sure that the cluster doesn't have any pre-existing problem
570
      qa_cluster.AssertClusterVerify()
571

    
572
      # Create and allocate instances
573
      instance1 = qa_instance.TestInstanceAddWithPlainDisk([node])
574
      try:
575
        instance2 = qa_instance.TestInstanceAddWithPlainDisk([node])
576
        try:
577
          # cluster-verify checks that disks are allocated correctly
578
          qa_cluster.AssertClusterVerify()
579

    
580
          # Remove instances
581
          qa_instance.TestInstanceRemove(instance2)
582
          qa_instance.TestInstanceRemove(instance1)
583
        finally:
584
          instance2.Release()
585
      finally:
586
        instance1.Release()
587

    
588
    if qa_config.TestEnabled("instance-add-drbd-disk"):
589
      snode = qa_config.AcquireNode()
590
      try:
591
        qa_cluster.TestSetExclStorCluster(False)
592
        instance = qa_instance.TestInstanceAddWithDrbdDisk([node, snode])
593
        try:
594
          qa_cluster.TestSetExclStorCluster(True)
595
          exp_err = [constants.CV_EINSTANCEUNSUITABLENODE]
596
          qa_cluster.AssertClusterVerify(fail=True, errors=exp_err)
597
          qa_instance.TestInstanceRemove(instance)
598
        finally:
599
          instance.Release()
600
      finally:
601
        snode.Release()
602
    qa_cluster.TestSetExclStorCluster(old_es)
603
  finally:
604
    node.Release()
605

    
606

    
607
def RunCustomSshPortTests():
608
  """Test accessing nodes with custom SSH ports.
609

610
  This requires removing nodes, adding them to a new group, and then undoing
611
  the change.
612
  """
613
  if not qa_config.TestEnabled("group-custom-ssh-port"):
614
    return
615

    
616
  std_port = netutils.GetDaemonPort(constants.SSH)
617
  port = 211
618
  master = qa_config.GetMasterNode()
619
  with qa_config.AcquireManyNodesCtx(1, exclude=master) as nodes:
620
    # Checks if the node(s) could be contacted through IPv6.
621
    # If yes, better skip the whole test.
622

    
623
    for node in nodes:
624
      if qa_utils.UsesIPv6Connection(node.primary, std_port):
625
        print ("Node %s is likely to be reached using IPv6,"
626
               "skipping the test" % (node.primary, ))
627
        return
628

    
629
    for node in nodes:
630
      qa_node.NodeRemove(node)
631
    with qa_iptables.RulesContext(nodes) as r:
632
      with qa_group.NewGroupCtx() as group:
633
        qa_group.ModifyGroupSshPort(r, group, nodes, port)
634

    
635
        for node in nodes:
636
          qa_node.NodeAdd(node, group=group)
637

    
638
        # Make sure that the cluster doesn't have any pre-existing problem
639
        qa_cluster.AssertClusterVerify()
640

    
641
        # Create and allocate instances
642
        instance1 = qa_instance.TestInstanceAddWithPlainDisk(nodes)
643
        try:
644
          instance2 = qa_instance.TestInstanceAddWithPlainDisk(nodes)
645
          try:
646
            # cluster-verify checks that disks are allocated correctly
647
            qa_cluster.AssertClusterVerify()
648

    
649
            # Remove instances
650
            qa_instance.TestInstanceRemove(instance2)
651
            qa_instance.TestInstanceRemove(instance1)
652
          finally:
653
            instance2.Release()
654
        finally:
655
          instance1.Release()
656

    
657
        for node in nodes:
658
          qa_node.NodeRemove(node)
659

    
660
    for node in nodes:
661
      qa_node.NodeAdd(node)
662

    
663
    qa_cluster.AssertClusterVerify()
664

    
665

    
666
def _BuildSpecDict(par, mn, st, mx):
667
  return {
668
    constants.ISPECS_MINMAX: [{
669
      constants.ISPECS_MIN: {par: mn},
670
      constants.ISPECS_MAX: {par: mx},
671
      }],
672
    constants.ISPECS_STD: {par: st},
673
    }
674

    
675

    
676
def _BuildDoubleSpecDict(index, par, mn, st, mx):
677
  new_spec = {
678
    constants.ISPECS_MINMAX: [{}, {}],
679
    }
680
  if st is not None:
681
    new_spec[constants.ISPECS_STD] = {par: st}
682
  new_spec[constants.ISPECS_MINMAX][index] = {
683
    constants.ISPECS_MIN: {par: mn},
684
    constants.ISPECS_MAX: {par: mx},
685
    }
686
  return new_spec
687

    
688

    
689
def TestIPolicyPlainInstance():
690
  """Test instance policy interaction with instances"""
691
  params = ["memory-size", "cpu-count", "disk-count", "disk-size", "nic-count"]
692
  if not qa_config.IsTemplateSupported(constants.DT_PLAIN):
693
    print "Template %s not supported" % constants.DT_PLAIN
694
    return
695

    
696
  # This test assumes that the group policy is empty
697
  (_, old_specs) = qa_cluster.TestClusterSetISpecs()
698
  # We also assume to have only one min/max bound
699
  assert len(old_specs[constants.ISPECS_MINMAX]) == 1
700
  node = qa_config.AcquireNode()
701
  try:
702
    # Log of policy changes, list of tuples:
703
    # (full_change, incremental_change, policy_violated)
704
    history = []
705
    instance = qa_instance.TestInstanceAddWithPlainDisk([node])
706
    try:
707
      policyerror = [constants.CV_EINSTANCEPOLICY]
708
      for par in params:
709
        (iminval, imaxval) = qa_instance.GetInstanceSpec(instance.name, par)
710
        # Some specs must be multiple of 4
711
        new_spec = _BuildSpecDict(par, imaxval + 4, imaxval + 4, imaxval + 4)
712
        history.append((None, new_spec, True))
713
        if iminval > 0:
714
          # Some specs must be multiple of 4
715
          if iminval >= 4:
716
            upper = iminval - 4
717
          else:
718
            upper = iminval - 1
719
          new_spec = _BuildSpecDict(par, 0, upper, upper)
720
          history.append((None, new_spec, True))
721
        history.append((old_specs, None, False))
722

    
723
      # Test with two instance specs
724
      double_specs = copy.deepcopy(old_specs)
725
      double_specs[constants.ISPECS_MINMAX] = \
726
          double_specs[constants.ISPECS_MINMAX] * 2
727
      (par1, par2) = params[0:2]
728
      (_, imaxval1) = qa_instance.GetInstanceSpec(instance.name, par1)
729
      (_, imaxval2) = qa_instance.GetInstanceSpec(instance.name, par2)
730
      old_minmax = old_specs[constants.ISPECS_MINMAX][0]
731
      history.extend([
732
        (double_specs, None, False),
733
        # The first min/max limit is being violated
734
        (None,
735
         _BuildDoubleSpecDict(0, par1, imaxval1 + 4, imaxval1 + 4,
736
                              imaxval1 + 4),
737
         False),
738
        # Both min/max limits are being violated
739
        (None,
740
         _BuildDoubleSpecDict(1, par2, imaxval2 + 4, None, imaxval2 + 4),
741
         True),
742
        # The second min/max limit is being violated
743
        (None,
744
         _BuildDoubleSpecDict(0, par1,
745
                              old_minmax[constants.ISPECS_MIN][par1],
746
                              old_specs[constants.ISPECS_STD][par1],
747
                              old_minmax[constants.ISPECS_MAX][par1]),
748
         False),
749
        (old_specs, None, False),
750
        ])
751

    
752
      # Apply the changes, and check policy violations after each change
753
      qa_cluster.AssertClusterVerify()
754
      for (new_specs, diff_specs, failed) in history:
755
        qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
756
                                        diff_specs=diff_specs)
757
        if failed:
758
          qa_cluster.AssertClusterVerify(warnings=policyerror)
759
        else:
760
          qa_cluster.AssertClusterVerify()
761

    
762
      qa_instance.TestInstanceRemove(instance)
763
    finally:
764
      instance.Release()
765

    
766
    # Now we replay the same policy changes, and we expect that the instance
767
    # cannot be created for the cases where we had a policy violation above
768
    for (new_specs, diff_specs, failed) in history:
769
      qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
770
                                      diff_specs=diff_specs)
771
      if failed:
772
        qa_instance.TestInstanceAddWithPlainDisk([node], fail=True)
773
      # Instance creation with no policy violation has been tested already
774
  finally:
775
    node.Release()
776

    
777

    
778
def IsExclusiveStorageInstanceTestEnabled():
779
  test_name = "exclusive-storage-instance-tests"
780
  if qa_config.TestEnabled(test_name):
781
    vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
782
    vgscmd = utils.ShellQuoteArgs([
783
      "vgs", "--noheadings", "-o", "pv_count", vgname,
784
      ])
785
    nodes = qa_config.GetConfig()["nodes"]
786
    for node in nodes:
787
      try:
788
        pvnum = int(qa_utils.GetCommandOutput(node.primary, vgscmd))
789
      except Exception, e:
790
        msg = ("Cannot get the number of PVs on %s, needed by '%s': %s" %
791
               (node.primary, test_name, e))
792
        raise qa_error.Error(msg)
793
      if pvnum < 2:
794
        raise qa_error.Error("Node %s has not enough PVs (%s) to run '%s'" %
795
                             (node.primary, pvnum, test_name))
796
    res = True
797
  else:
798
    res = False
799
  return res
800

    
801

    
802
def RunInstanceTests():
803
  """Create and exercise instances."""
804

    
805
  for (test_name, templ, create_fun, num_nodes) in \
806
      qa_instance.available_instance_tests:
807
    if (qa_config.TestEnabled(test_name) and
808
        qa_config.IsTemplateSupported(templ)):
809
      inodes = qa_config.AcquireManyNodes(num_nodes)
810
      try:
811
        instance = RunTest(create_fun, inodes)
812
        try:
813
          RunTestIf("instance-user-down", qa_instance.TestInstanceUserDown,
814
                    instance, qa_config.GetMasterNode())
815
          RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
816
          RunDaemonTests(instance)
817
          for node in inodes:
818
            RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, node)
819
          if len(inodes) > 1:
820
            RunTestIf("group-rwops", qa_group.TestAssignNodesIncludingSplit,
821
                      constants.INITIAL_NODE_GROUP_NAME,
822
                      inodes[0].primary, inodes[1].primary)
823
          if qa_config.TestEnabled("instance-convert-disk"):
824
            RunTest(qa_instance.TestInstanceShutdown, instance)
825
            RunTest(qa_instance.TestInstanceConvertDiskToPlain,
826
                    instance, inodes)
827
            RunTest(qa_instance.TestInstanceStartup, instance)
828
          RunTestIf("instance-modify-disks",
829
                    qa_instance.TestInstanceModifyDisks, instance)
830
          RunCommonInstanceTests(instance, inodes)
831
          if qa_config.TestEnabled("instance-modify-primary"):
832
            othernode = qa_config.AcquireNode()
833
            RunTest(qa_instance.TestInstanceModifyPrimaryAndBack,
834
                    instance, inodes[0], othernode)
835
            othernode.Release()
836
          RunGroupListTests()
837
          RunExportImportTests(instance, inodes)
838
          RunHardwareFailureTests(instance, inodes)
839
          RunRepairDiskSizes()
840
          RunTest(qa_instance.TestInstanceRemove, instance)
841
        finally:
842
          instance.Release()
843
        del instance
844
      finally:
845
        qa_config.ReleaseManyNodes(inodes)
846
      qa_cluster.AssertClusterVerify()
847

    
848

    
849
def RunMonitoringTests():
850
  if qa_config.TestEnabled("mon-collector"):
851
    RunTest(qa_monitoring.TestInstStatusCollector)
852

    
853

    
854
def RunQa():
855
  """Main QA body.
856

857
  """
858
  rapi_user = "ganeti-qa"
859

    
860
  RunTestBlock(RunEnvTests)
861
  rapi_secret = SetupCluster(rapi_user)
862

    
863
  if qa_rapi.Enabled():
864
    # Load RAPI certificate
865
    qa_rapi.Setup(rapi_user, rapi_secret)
866

    
867
  RunTestBlock(RunClusterTests)
868
  RunTestBlock(RunOsTests)
869

    
870
  RunTestIf("tags", qa_tags.TestClusterTags)
871

    
872
  RunTestBlock(RunCommonNodeTests)
873
  RunTestBlock(RunGroupListTests)
874
  RunTestBlock(RunGroupRwTests)
875
  RunTestBlock(RunNetworkTests)
876

    
877
  # The master shouldn't be readded or put offline; "delay" needs a non-master
878
  # node to test
879
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
880
  try:
881
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
882
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
883
    RunTestIf("delay", qa_cluster.TestDelay, pnode)
884
  finally:
885
    pnode.Release()
886

    
887
  # Make sure the cluster is clean before running instance tests
888
  qa_cluster.AssertClusterVerify()
889

    
890
  pnode = qa_config.AcquireNode()
891
  try:
892
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
893

    
894
    if qa_rapi.Enabled():
895
      RunTest(qa_rapi.TestNode, pnode)
896

    
897
      if (qa_config.TestEnabled("instance-add-plain-disk")
898
          and qa_config.IsTemplateSupported(constants.DT_PLAIN)):
899
        for use_client in [True, False]:
900
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
901
                                  use_client)
902
          try:
903
            if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
904
              RunCommonInstanceTests(rapi_instance, [pnode])
905
            RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
906
          finally:
907
            rapi_instance.Release()
908
          del rapi_instance
909

    
910
  finally:
911
    pnode.Release()
912

    
913
  config_list = [
914
    ("default-instance-tests", lambda: None, lambda _: None),
915
    (IsExclusiveStorageInstanceTestEnabled,
916
     lambda: qa_cluster.TestSetExclStorCluster(True),
917
     qa_cluster.TestSetExclStorCluster),
918
  ]
919
  for (conf_name, setup_conf_f, restore_conf_f) in config_list:
920
    if qa_config.TestEnabled(conf_name):
921
      oldconf = setup_conf_f()
922
      RunTestBlock(RunInstanceTests)
923
      restore_conf_f(oldconf)
924

    
925
  pnode = qa_config.AcquireNode()
926
  try:
927
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
928
      for shutdown in [False, True]:
929
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, [pnode])
930
        try:
931
          expnode = qa_config.AcquireNode(exclude=pnode)
932
          try:
933
            if shutdown:
934
              # Stop instance before exporting and removing it
935
              RunTest(qa_instance.TestInstanceShutdown, instance)
936
            RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
937
            RunTest(qa_instance.TestBackupList, expnode)
938
          finally:
939
            expnode.Release()
940
        finally:
941
          instance.Release()
942
        del expnode
943
        del instance
944
      qa_cluster.AssertClusterVerify()
945

    
946
  finally:
947
    pnode.Release()
948

    
949
  RunTestIf("cluster-upgrade", qa_cluster.TestUpgrade)
950

    
951
  RunTestBlock(RunExclusiveStorageTests)
952
  RunTestIf(["cluster-instance-policy", "instance-add-plain-disk"],
953
            TestIPolicyPlainInstance)
954

    
955
  RunTestBlock(RunCustomSshPortTests)
956

    
957
  RunTestIf(
958
    "instance-add-restricted-by-disktemplates",
959
    qa_instance.TestInstanceCreationRestrictedByDiskTemplates)
960

    
961
  # Test removing instance with offline drbd secondary
962
  if qa_config.TestEnabled(["instance-remove-drbd-offline",
963
                            "instance-add-drbd-disk"]):
964
    # Make sure the master is not put offline
965
    snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
966
    try:
967
      pnode = qa_config.AcquireNode(exclude=snode)
968
      try:
969
        instance = qa_instance.TestInstanceAddWithDrbdDisk([pnode, snode])
970
        set_offline = lambda node: qa_node.MakeNodeOffline(node, "yes")
971
        set_online = lambda node: qa_node.MakeNodeOffline(node, "no")
972
        RunTest(qa_instance.TestRemoveInstanceOfflineNode, instance, snode,
973
                set_offline, set_online)
974
      finally:
975
        pnode.Release()
976
    finally:
977
      snode.Release()
978
    qa_cluster.AssertClusterVerify()
979

    
980
  RunTestBlock(RunMonitoringTests)
981

    
982
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
983

    
984
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
985

    
986

    
987
@UsesRapiClient
988
def main():
989
  """Main program.
990

991
  """
992
  colors.check_for_colors()
993

    
994
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
995
  parser.add_option("--yes-do-it", dest="yes_do_it",
996
                    action="store_true",
997
                    help="Really execute the tests")
998
  (opts, args) = parser.parse_args()
999

    
1000
  if len(args) == 1:
1001
    (config_file, ) = args
1002
  else:
1003
    parser.error("Wrong number of arguments.")
1004

    
1005
  if not opts.yes_do_it:
1006
    print ("Executing this script irreversibly destroys any Ganeti\n"
1007
           "configuration on all nodes involved. If you really want\n"
1008
           "to start testing, supply the --yes-do-it option.")
1009
    sys.exit(1)
1010

    
1011
  qa_config.Load(config_file)
1012

    
1013
  primary = qa_config.GetMasterNode().primary
1014
  qa_utils.StartMultiplexer(primary)
1015
  print ("SSH command for primary node: %s" %
1016
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
1017
  print ("SSH command for other nodes: %s" %
1018
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
1019
  try:
1020
    RunQa()
1021
  finally:
1022
    qa_utils.CloseMultiplexers()
1023

    
1024
if __name__ == "__main__":
1025
  main()