Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ c5c8d092

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

    
55
from ganeti.http.auth import ParsePasswordFile
56
import ganeti.rapi.client # pylint: disable=W0611
57
from ganeti.rapi.client import UsesRapiClient
58

    
59

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

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

    
69

    
70
def _DescriptionOf(fn):
71
  """Computes the description of an item.
72

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

    
82
  return desc
83

    
84

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

88
  """
89

    
90
  tstart = datetime.datetime.now()
91

    
92
  desc = _DescriptionOf(fn)
93

    
94
  print
95
  print _FormatHeader("%s start %s" % (tstart, desc))
96

    
97
  try:
98
    retval = fn(*args, **kwargs)
99
    return retval
100
  finally:
101
    tstop = datetime.datetime.now()
102
    tdelta = tstop - tstart
103
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
104

    
105

    
106
def RunTestIf(testnames, fn, *args, **kwargs):
107
  """Runs a test conditionally.
108

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

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

    
122

    
123
def RunEnvTests():
124
  """Run several environment tests.
125

126
  """
127
  RunTestIf("env", qa_env.TestSshConnection)
128
  RunTestIf("env", qa_env.TestIcmpPing)
129
  RunTestIf("env", qa_env.TestGanetiCommands)
130

    
131

    
132
def _LookupRapiSecret(rapi_user):
133
  """Find the RAPI secret for the given user.
134

135
  @param rapi_user: Login user
136
  @return: Login secret for the user
137

138
  """
139
  CTEXT = "{CLEARTEXT}"
140
  master = qa_config.GetMasterNode()
141
  cmd = ["cat", qa_utils.MakeNodePath(master, pathutils.RAPI_USERS_FILE)]
142
  file_content = qa_utils.GetCommandOutput(master.primary,
143
                                           utils.ShellQuoteArgs(cmd))
144
  users = ParsePasswordFile(file_content)
145
  entry = users.get(rapi_user)
146
  if not entry:
147
    raise qa_error.Error("User %s not found in RAPI users file" % rapi_user)
148
  secret = entry.password
149
  if secret.upper().startswith(CTEXT):
150
    secret = secret[len(CTEXT):]
151
  elif secret.startswith("{"):
152
    raise qa_error.Error("Unsupported password schema for RAPI user %s:"
153
                         " not a clear text password" % rapi_user)
154
  return secret
155

    
156

    
157
def SetupCluster(rapi_user):
158
  """Initializes the cluster.
159

160
  @param rapi_user: Login user for RAPI
161
  @return: Login secret for RAPI
162

163
  """
164
  rapi_secret = utils.GenerateSecret()
165
  RunTestIf("create-cluster", qa_cluster.TestClusterInit,
166
            rapi_user, rapi_secret)
167
  if not qa_config.TestEnabled("create-cluster"):
168
    # If the cluster is already in place, we assume that exclusive-storage is
169
    # already set according to the configuration
170
    qa_config.SetExclusiveStorage(qa_config.get("exclusive-storage", False))
171
    if qa_rapi.Enabled():
172
      # To support RAPI on an existing cluster we have to find out the secret
173
      rapi_secret = _LookupRapiSecret(rapi_user)
174

    
175
  # Test on empty cluster
176
  RunTestIf("node-list", qa_node.TestNodeList)
177
  RunTestIf("instance-list", qa_instance.TestInstanceList)
178
  RunTestIf("job-list", qa_job.TestJobList)
179

    
180
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
181
  if not qa_config.TestEnabled("create-cluster"):
182
    # consider the nodes are already there
183
    qa_node.MarkNodeAddedAll()
184

    
185
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
186

    
187
  # enable the watcher (unconditionally)
188
  RunTest(qa_daemon.TestResumeWatcher)
189

    
190
  RunTestIf("node-list", qa_node.TestNodeList)
191

    
192
  # Test listing fields
193
  RunTestIf("node-list", qa_node.TestNodeListFields)
194
  RunTestIf("instance-list", qa_instance.TestInstanceListFields)
195
  RunTestIf("job-list", qa_job.TestJobListFields)
196
  RunTestIf("instance-export", qa_instance.TestBackupListFields)
197

    
198
  RunTestIf("node-info", qa_node.TestNodeInfo)
199

    
200
  return rapi_secret
201

    
202

    
203
def RunClusterTests():
204
  """Runs tests related to gnt-cluster.
205

206
  """
207
  for test, fn in [
208
    ("create-cluster", qa_cluster.TestClusterInitDisk),
209
    ("cluster-renew-crypto", qa_cluster.TestClusterRenewCrypto),
210
    ("cluster-verify", qa_cluster.TestClusterVerify),
211
    ("cluster-reserved-lvs", qa_cluster.TestClusterReservedLvs),
212
    # TODO: add more cluster modify tests
213
    ("cluster-modify", qa_cluster.TestClusterModifyEmpty),
214
    ("cluster-modify", qa_cluster.TestClusterModifyIPolicy),
215
    ("cluster-modify", qa_cluster.TestClusterModifyISpecs),
216
    ("cluster-modify", qa_cluster.TestClusterModifyBe),
217
    ("cluster-modify", qa_cluster.TestClusterModifyDisk),
218
    ("cluster-modify", qa_cluster.TestClusterModifyDiskTemplates),
219
    ("cluster-modify", qa_cluster.TestClusterModifyFileStorageDir),
220
    ("cluster-modify", qa_cluster.TestClusterModifySharedFileStorageDir),
221
    ("cluster-rename", qa_cluster.TestClusterRename),
222
    ("cluster-info", qa_cluster.TestClusterVersion),
223
    ("cluster-info", qa_cluster.TestClusterInfo),
224
    ("cluster-info", qa_cluster.TestClusterGetmaster),
225
    ("cluster-redist-conf", qa_cluster.TestClusterRedistConf),
226
    (["cluster-copyfile", qa_config.NoVirtualCluster],
227
     qa_cluster.TestClusterCopyfile),
228
    ("cluster-command", qa_cluster.TestClusterCommand),
229
    ("cluster-burnin", qa_cluster.TestClusterBurnin),
230
    ("cluster-master-failover", qa_cluster.TestClusterMasterFailover),
231
    ("cluster-master-failover",
232
     qa_cluster.TestClusterMasterFailoverWithDrainedQueue),
233
    (["cluster-oob", qa_config.NoVirtualCluster],
234
     qa_cluster.TestClusterOob),
235
    (qa_rapi.Enabled, qa_rapi.TestVersion),
236
    (qa_rapi.Enabled, qa_rapi.TestEmptyCluster),
237
    (qa_rapi.Enabled, qa_rapi.TestRapiQuery),
238
    ]:
239
    RunTestIf(test, fn)
240

    
241

    
242
def RunRepairDiskSizes():
243
  """Run the repair disk-sizes test.
244

245
  """
246
  RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
247

    
248

    
249
def RunOsTests():
250
  """Runs all tests related to gnt-os.
251

252
  """
253
  os_enabled = ["os", qa_config.NoVirtualCluster]
254

    
255
  if qa_config.TestEnabled(qa_rapi.Enabled):
256
    rapi_getos = qa_rapi.GetOperatingSystems
257
  else:
258
    rapi_getos = None
259

    
260
  for fn in [
261
    qa_os.TestOsList,
262
    qa_os.TestOsDiagnose,
263
    ]:
264
    RunTestIf(os_enabled, fn)
265

    
266
  for fn in [
267
    qa_os.TestOsValid,
268
    qa_os.TestOsInvalid,
269
    qa_os.TestOsPartiallyValid,
270
    ]:
271
    RunTestIf(os_enabled, fn, rapi_getos)
272

    
273
  for fn in [
274
    qa_os.TestOsModifyValid,
275
    qa_os.TestOsModifyInvalid,
276
    qa_os.TestOsStatesNonExisting,
277
    ]:
278
    RunTestIf(os_enabled, fn)
279

    
280

    
281
def RunCommonInstanceTests(instance, inst_nodes):
282
  """Runs a few tests that are common to all disk types.
283

284
  """
285
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
286
  RunTestIf(["instance-shutdown", "instance-console", qa_rapi.Enabled],
287
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
288
  RunTestIf(["instance-shutdown", "instance-modify"],
289
            qa_instance.TestInstanceStoppedModify, instance)
290
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
291

    
292
  # Test shutdown/start via RAPI
293
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
294
            qa_rapi.TestRapiInstanceShutdown, instance)
295
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
296
            qa_rapi.TestRapiInstanceStartup, instance)
297

    
298
  RunTestIf("instance-list", qa_instance.TestInstanceList)
299

    
300
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
301

    
302
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
303
  RunTestIf(["instance-modify", qa_rapi.Enabled],
304
            qa_rapi.TestRapiInstanceModify, instance)
305

    
306
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
307
  RunTestIf(["instance-console", qa_rapi.Enabled],
308
            qa_rapi.TestRapiInstanceConsole, instance)
309

    
310
  RunTestIf("instance-device-names", qa_instance.TestInstanceDeviceNames,
311
            instance)
312
  DOWN_TESTS = qa_config.Either([
313
    "instance-reinstall",
314
    "instance-rename",
315
    "instance-grow-disk",
316
    ])
317

    
318
  # shutdown instance for any 'down' tests
319
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
320

    
321
  # now run the 'down' state tests
322
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
323
  RunTestIf(["instance-reinstall", qa_rapi.Enabled],
324
            qa_rapi.TestRapiInstanceReinstall, instance)
325

    
326
  if qa_config.TestEnabled("instance-rename"):
327
    tgt_instance = qa_config.AcquireInstance()
328
    try:
329
      rename_source = instance.name
330
      rename_target = tgt_instance.name
331
      # perform instance rename to the same name
332
      RunTest(qa_instance.TestInstanceRenameAndBack,
333
              rename_source, rename_source)
334
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
335
                rename_source, rename_source)
336
      if rename_target is not None:
337
        # perform instance rename to a different name, if we have one configured
338
        RunTest(qa_instance.TestInstanceRenameAndBack,
339
                rename_source, rename_target)
340
        RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceRenameAndBack,
341
                  rename_source, rename_target)
342
    finally:
343
      tgt_instance.Release()
344

    
345
  RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
346

    
347
  # and now start the instance again
348
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
349

    
350
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
351

    
352
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
353

    
354
  if instance.disk_template == constants.DT_DRBD8:
355
    RunTestIf("cluster-verify",
356
              qa_cluster.TestClusterVerifyDisksBrokenDRBD, instance, inst_nodes)
357
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
358

    
359
  RunTestIf(qa_rapi.Enabled, qa_rapi.TestInstance, instance)
360

    
361
  # Lists instances, too
362
  RunTestIf("node-list", qa_node.TestNodeList)
363

    
364
  # Some jobs have been run, let's test listing them
365
  RunTestIf("job-list", qa_job.TestJobList)
366

    
367

    
368
def RunCommonNodeTests():
369
  """Run a few common node tests.
370

371
  """
372
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
373
  RunTestIf("node-storage", qa_node.TestNodeStorage)
374
  RunTestIf(["node-oob", qa_config.NoVirtualCluster], qa_node.TestOutOfBand)
375

    
376

    
377
def RunGroupListTests():
378
  """Run tests for listing node groups.
379

380
  """
381
  RunTestIf("group-list", qa_group.TestGroupList)
382
  RunTestIf("group-list", qa_group.TestGroupListFields)
383

    
384

    
385
def RunNetworkTests():
386
  """Run tests for network management.
387

388
  """
389
  RunTestIf("network", qa_network.TestNetworkAddRemove)
390
  RunTestIf("network", qa_network.TestNetworkConnect)
391

    
392

    
393
def RunGroupRwTests():
394
  """Run tests for adding/removing/renaming groups.
395

396
  """
397
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
398
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
399
  RunTestIf("group-rwops", qa_group.TestGroupModify)
400
  RunTestIf(["group-rwops", qa_rapi.Enabled], qa_rapi.TestRapiNodeGroups)
401
  RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
402
            qa_group.GetDefaultGroup())
403

    
404

    
405
def RunExportImportTests(instance, inodes):
406
  """Tries to export and import the instance.
407

408
  @type inodes: list of nodes
409
  @param inodes: current nodes of the instance
410

411
  """
412
  # FIXME: export explicitly bails out on file based storage. other non-lvm
413
  # based storage types are untested, though. Also note that import could still
414
  # work, but is deeply embedded into the "export" case.
415
  if (qa_config.TestEnabled("instance-export") and
416
      instance.disk_template not in [constants.DT_FILE,
417
                                     constants.DT_SHARED_FILE]):
418
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
419

    
420
    pnode = inodes[0]
421
    expnode = qa_config.AcquireNode(exclude=pnode)
422
    try:
423
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
424

    
425
      RunTest(qa_instance.TestBackupList, expnode)
426

    
427
      if qa_config.TestEnabled("instance-import"):
428
        newinst = qa_config.AcquireInstance()
429
        try:
430
          RunTest(qa_instance.TestInstanceImport, newinst, pnode,
431
                  expnode, name)
432
          # Check if starting the instance works
433
          RunTest(qa_instance.TestInstanceStartup, newinst)
434
          RunTest(qa_instance.TestInstanceRemove, newinst)
435
        finally:
436
          newinst.Release()
437
    finally:
438
      expnode.Release()
439

    
440
  # FIXME: inter-cluster-instance-move crashes on file based instances :/
441
  # See Issue 414.
442
  if (qa_config.TestEnabled([qa_rapi.Enabled, "inter-cluster-instance-move"])
443
      and (instance.disk_template not in
444
           [constants.DT_FILE, constants.DT_SHARED_FILE])):
445
    newinst = qa_config.AcquireInstance()
446
    try:
447
      tnode = qa_config.AcquireNode(exclude=inodes)
448
      try:
449
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
450
                inodes, tnode)
451
      finally:
452
        tnode.Release()
453
    finally:
454
      newinst.Release()
455

    
456

    
457
def RunDaemonTests(instance):
458
  """Test the ganeti-watcher script.
459

460
  """
461
  RunTest(qa_daemon.TestPauseWatcher)
462

    
463
  RunTestIf("instance-automatic-restart",
464
            qa_daemon.TestInstanceAutomaticRestart, instance)
465
  RunTestIf("instance-consecutive-failures",
466
            qa_daemon.TestInstanceConsecutiveFailures, instance)
467

    
468
  RunTest(qa_daemon.TestResumeWatcher)
469

    
470

    
471
def RunHardwareFailureTests(instance, inodes):
472
  """Test cluster internal hardware failure recovery.
473

474
  """
475
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
476
  RunTestIf(["instance-failover", qa_rapi.Enabled],
477
            qa_rapi.TestRapiInstanceFailover, instance)
478

    
479
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
480
  RunTestIf(["instance-migrate", qa_rapi.Enabled],
481
            qa_rapi.TestRapiInstanceMigrate, instance)
482

    
483
  if qa_config.TestEnabled("instance-replace-disks"):
484
    # We just need alternative secondary nodes, hence "- 1"
485
    othernodes = qa_config.AcquireManyNodes(len(inodes) - 1, exclude=inodes)
486
    try:
487
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceReplaceDisks, instance)
488
      RunTest(qa_instance.TestReplaceDisks,
489
              instance, inodes, othernodes)
490
    finally:
491
      qa_config.ReleaseManyNodes(othernodes)
492
    del othernodes
493

    
494
  if qa_config.TestEnabled("instance-recreate-disks"):
495
    try:
496
      acquirednodes = qa_config.AcquireManyNodes(len(inodes), exclude=inodes)
497
      othernodes = acquirednodes
498
    except qa_error.OutOfNodesError:
499
      if len(inodes) > 1:
500
        # If the cluster is not big enough, let's reuse some of the nodes, but
501
        # with different roles. In this way, we can test a DRBD instance even on
502
        # a 3-node cluster.
503
        acquirednodes = [qa_config.AcquireNode(exclude=inodes)]
504
        othernodes = acquirednodes + inodes[:-1]
505
      else:
506
        raise
507
    try:
508
      RunTest(qa_instance.TestRecreateDisks,
509
              instance, inodes, othernodes)
510
    finally:
511
      qa_config.ReleaseManyNodes(acquirednodes)
512

    
513
  if len(inodes) >= 2:
514
    RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, inodes[0], inodes[1])
515
    RunTestIf("node-failover", qa_node.TestNodeFailover, inodes[0], inodes[1])
516
    RunTestIf("node-migrate", qa_node.TestNodeMigrate, inodes[0], inodes[1])
517

    
518

    
519
def RunExclusiveStorageTests():
520
  """Test exclusive storage."""
521
  if not qa_config.TestEnabled("cluster-exclusive-storage"):
522
    return
523

    
524
  node = qa_config.AcquireNode()
525
  try:
526
    old_es = qa_cluster.TestSetExclStorCluster(False)
527
    qa_node.TestExclStorSingleNode(node)
528

    
529
    qa_cluster.TestSetExclStorCluster(True)
530
    qa_cluster.TestExclStorSharedPv(node)
531

    
532
    if qa_config.TestEnabled("instance-add-plain-disk"):
533
      # Make sure that the cluster doesn't have any pre-existing problem
534
      qa_cluster.AssertClusterVerify()
535

    
536
      # Create and allocate instances
537
      instance1 = qa_instance.TestInstanceAddWithPlainDisk([node])
538
      try:
539
        instance2 = qa_instance.TestInstanceAddWithPlainDisk([node])
540
        try:
541
          # cluster-verify checks that disks are allocated correctly
542
          qa_cluster.AssertClusterVerify()
543

    
544
          # Remove instances
545
          qa_instance.TestInstanceRemove(instance2)
546
          qa_instance.TestInstanceRemove(instance1)
547
        finally:
548
          instance2.Release()
549
      finally:
550
        instance1.Release()
551

    
552
    if qa_config.TestEnabled("instance-add-drbd-disk"):
553
      snode = qa_config.AcquireNode()
554
      try:
555
        qa_cluster.TestSetExclStorCluster(False)
556
        instance = qa_instance.TestInstanceAddWithDrbdDisk([node, snode])
557
        try:
558
          qa_cluster.TestSetExclStorCluster(True)
559
          exp_err = [constants.CV_EINSTANCEUNSUITABLENODE]
560
          qa_cluster.AssertClusterVerify(fail=True, errors=exp_err)
561
          qa_instance.TestInstanceRemove(instance)
562
        finally:
563
          instance.Release()
564
      finally:
565
        snode.Release()
566
    qa_cluster.TestSetExclStorCluster(old_es)
567
  finally:
568
    node.Release()
569

    
570

    
571
def _BuildSpecDict(par, mn, st, mx):
572
  return {
573
    constants.ISPECS_MINMAX: [{
574
      constants.ISPECS_MIN: {par: mn},
575
      constants.ISPECS_MAX: {par: mx},
576
      }],
577
    constants.ISPECS_STD: {par: st},
578
    }
579

    
580

    
581
def _BuildDoubleSpecDict(index, par, mn, st, mx):
582
  new_spec = {
583
    constants.ISPECS_MINMAX: [{}, {}],
584
    }
585
  if st is not None:
586
    new_spec[constants.ISPECS_STD] = {par: st}
587
  new_spec[constants.ISPECS_MINMAX][index] = {
588
    constants.ISPECS_MIN: {par: mn},
589
    constants.ISPECS_MAX: {par: mx},
590
    }
591
  return new_spec
592

    
593

    
594
def TestIPolicyPlainInstance():
595
  """Test instance policy interaction with instances"""
596
  params = ["memory-size", "cpu-count", "disk-count", "disk-size", "nic-count"]
597
  if not qa_config.IsTemplateSupported(constants.DT_PLAIN):
598
    print "Template %s not supported" % constants.DT_PLAIN
599
    return
600

    
601
  # This test assumes that the group policy is empty
602
  (_, old_specs) = qa_cluster.TestClusterSetISpecs()
603
  # We also assume to have only one min/max bound
604
  assert len(old_specs[constants.ISPECS_MINMAX]) == 1
605
  node = qa_config.AcquireNode()
606
  try:
607
    # Log of policy changes, list of tuples:
608
    # (full_change, incremental_change, policy_violated)
609
    history = []
610
    instance = qa_instance.TestInstanceAddWithPlainDisk([node])
611
    try:
612
      policyerror = [constants.CV_EINSTANCEPOLICY]
613
      for par in params:
614
        (iminval, imaxval) = qa_instance.GetInstanceSpec(instance.name, par)
615
        # Some specs must be multiple of 4
616
        new_spec = _BuildSpecDict(par, imaxval + 4, imaxval + 4, imaxval + 4)
617
        history.append((None, new_spec, True))
618
        if iminval > 0:
619
          # Some specs must be multiple of 4
620
          if iminval >= 4:
621
            upper = iminval - 4
622
          else:
623
            upper = iminval - 1
624
          new_spec = _BuildSpecDict(par, 0, upper, upper)
625
          history.append((None, new_spec, True))
626
        history.append((old_specs, None, False))
627

    
628
      # Test with two instance specs
629
      double_specs = copy.deepcopy(old_specs)
630
      double_specs[constants.ISPECS_MINMAX] = \
631
          double_specs[constants.ISPECS_MINMAX] * 2
632
      (par1, par2) = params[0:2]
633
      (_, imaxval1) = qa_instance.GetInstanceSpec(instance.name, par1)
634
      (_, imaxval2) = qa_instance.GetInstanceSpec(instance.name, par2)
635
      old_minmax = old_specs[constants.ISPECS_MINMAX][0]
636
      history.extend([
637
        (double_specs, None, False),
638
        # The first min/max limit is being violated
639
        (None,
640
         _BuildDoubleSpecDict(0, par1, imaxval1 + 4, imaxval1 + 4,
641
                              imaxval1 + 4),
642
         False),
643
        # Both min/max limits are being violated
644
        (None,
645
         _BuildDoubleSpecDict(1, par2, imaxval2 + 4, None, imaxval2 + 4),
646
         True),
647
        # The second min/max limit is being violated
648
        (None,
649
         _BuildDoubleSpecDict(0, par1,
650
                              old_minmax[constants.ISPECS_MIN][par1],
651
                              old_specs[constants.ISPECS_STD][par1],
652
                              old_minmax[constants.ISPECS_MAX][par1]),
653
         False),
654
        (old_specs, None, False),
655
        ])
656

    
657
      # Apply the changes, and check policy violations after each change
658
      qa_cluster.AssertClusterVerify()
659
      for (new_specs, diff_specs, failed) in history:
660
        qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
661
                                        diff_specs=diff_specs)
662
        if failed:
663
          qa_cluster.AssertClusterVerify(warnings=policyerror)
664
        else:
665
          qa_cluster.AssertClusterVerify()
666

    
667
      qa_instance.TestInstanceRemove(instance)
668
    finally:
669
      instance.Release()
670

    
671
    # Now we replay the same policy changes, and we expect that the instance
672
    # cannot be created for the cases where we had a policy violation above
673
    for (new_specs, diff_specs, failed) in history:
674
      qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
675
                                      diff_specs=diff_specs)
676
      if failed:
677
        qa_instance.TestInstanceAddWithPlainDisk([node], fail=True)
678
      # Instance creation with no policy violation has been tested already
679
  finally:
680
    node.Release()
681

    
682

    
683
def IsExclusiveStorageInstanceTestEnabled():
684
  test_name = "exclusive-storage-instance-tests"
685
  if qa_config.TestEnabled(test_name):
686
    vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
687
    vgscmd = utils.ShellQuoteArgs([
688
      "vgs", "--noheadings", "-o", "pv_count", vgname,
689
      ])
690
    nodes = qa_config.GetConfig()["nodes"]
691
    for node in nodes:
692
      try:
693
        pvnum = int(qa_utils.GetCommandOutput(node.primary, vgscmd))
694
      except Exception, e:
695
        msg = ("Cannot get the number of PVs on %s, needed by '%s': %s" %
696
               (node.primary, test_name, e))
697
        raise qa_error.Error(msg)
698
      if pvnum < 2:
699
        raise qa_error.Error("Node %s has not enough PVs (%s) to run '%s'" %
700
                             (node.primary, pvnum, test_name))
701
    res = True
702
  else:
703
    res = False
704
  return res
705

    
706

    
707
def RunInstanceTests():
708
  """Create and exercise instances."""
709

    
710
  for (test_name, templ, create_fun, num_nodes) in \
711
      qa_instance.available_instance_tests:
712
    if (qa_config.TestEnabled(test_name) and
713
        qa_config.IsTemplateSupported(templ)):
714
      inodes = qa_config.AcquireManyNodes(num_nodes)
715
      try:
716
        instance = RunTest(create_fun, inodes)
717
        try:
718
          RunTestIf("cluster-epo", qa_cluster.TestClusterEpo)
719
          RunDaemonTests(instance)
720
          for node in inodes:
721
            RunTestIf("haskell-confd", qa_node.TestNodeListDrbd, node)
722
          if len(inodes) > 1:
723
            RunTestIf("group-rwops", qa_group.TestAssignNodesIncludingSplit,
724
                      constants.INITIAL_NODE_GROUP_NAME,
725
                      inodes[0].primary, inodes[1].primary)
726
          if qa_config.TestEnabled("instance-convert-disk"):
727
            RunTest(qa_instance.TestInstanceShutdown, instance)
728
            RunTest(qa_instance.TestInstanceConvertDiskToPlain,
729
                    instance, inodes)
730
            RunTest(qa_instance.TestInstanceStartup, instance)
731
          RunTestIf("instance-modify-disks",
732
                    qa_instance.TestInstanceModifyDisks, instance)
733
          RunCommonInstanceTests(instance, inodes)
734
          if qa_config.TestEnabled("instance-modify-primary"):
735
            othernode = qa_config.AcquireNode()
736
            RunTest(qa_instance.TestInstanceModifyPrimaryAndBack,
737
                    instance, inodes[0], othernode)
738
            othernode.Release()
739
          RunGroupListTests()
740
          RunExportImportTests(instance, inodes)
741
          RunHardwareFailureTests(instance, inodes)
742
          RunRepairDiskSizes()
743
          RunTest(qa_instance.TestInstanceRemove, instance)
744
        finally:
745
          instance.Release()
746
        del instance
747
      finally:
748
        qa_config.ReleaseManyNodes(inodes)
749
      qa_cluster.AssertClusterVerify()
750

    
751

    
752
def RunMonitoringTests():
753
  if qa_config.TestEnabled("mon-collector"):
754
    RunTest(qa_monitoring.TestInstStatusCollector)
755

    
756

    
757
def RunQa():
758
  """Main QA body.
759

760
  """
761
  rapi_user = "ganeti-qa"
762

    
763
  RunEnvTests()
764
  rapi_secret = SetupCluster(rapi_user)
765

    
766
  if qa_rapi.Enabled():
767
    # Load RAPI certificate
768
    qa_rapi.Setup(rapi_user, rapi_secret)
769

    
770
  RunClusterTests()
771
  RunOsTests()
772

    
773
  RunTestIf("tags", qa_tags.TestClusterTags)
774

    
775
  RunCommonNodeTests()
776
  RunGroupListTests()
777
  RunGroupRwTests()
778
  RunNetworkTests()
779

    
780
  # The master shouldn't be readded or put offline; "delay" needs a non-master
781
  # node to test
782
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
783
  try:
784
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
785
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
786
    RunTestIf("delay", qa_cluster.TestDelay, pnode)
787
  finally:
788
    pnode.Release()
789

    
790
  # Make sure the cluster is clean before running instance tests
791
  qa_cluster.AssertClusterVerify()
792

    
793
  pnode = qa_config.AcquireNode()
794
  try:
795
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
796

    
797
    if qa_rapi.Enabled():
798
      RunTest(qa_rapi.TestNode, pnode)
799

    
800
      if (qa_config.TestEnabled("instance-add-plain-disk")
801
          and qa_config.IsTemplateSupported(constants.DT_PLAIN)):
802
        for use_client in [True, False]:
803
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
804
                                  use_client)
805
          try:
806
            if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
807
              RunCommonInstanceTests(rapi_instance, [pnode])
808
            RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
809
          finally:
810
            rapi_instance.Release()
811
          del rapi_instance
812

    
813
  finally:
814
    pnode.Release()
815

    
816
  config_list = [
817
    ("default-instance-tests", lambda: None, lambda _: None),
818
    (IsExclusiveStorageInstanceTestEnabled,
819
     lambda: qa_cluster.TestSetExclStorCluster(True),
820
     qa_cluster.TestSetExclStorCluster),
821
  ]
822
  for (conf_name, setup_conf_f, restore_conf_f) in config_list:
823
    if qa_config.TestEnabled(conf_name):
824
      oldconf = setup_conf_f()
825
      RunInstanceTests()
826
      restore_conf_f(oldconf)
827

    
828
  pnode = qa_config.AcquireNode()
829
  try:
830
    if qa_config.TestEnabled(["instance-add-plain-disk", "instance-export"]):
831
      for shutdown in [False, True]:
832
        instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, [pnode])
833
        try:
834
          expnode = qa_config.AcquireNode(exclude=pnode)
835
          try:
836
            if shutdown:
837
              # Stop instance before exporting and removing it
838
              RunTest(qa_instance.TestInstanceShutdown, instance)
839
            RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
840
            RunTest(qa_instance.TestBackupList, expnode)
841
          finally:
842
            expnode.Release()
843
        finally:
844
          instance.Release()
845
        del expnode
846
        del instance
847
      qa_cluster.AssertClusterVerify()
848

    
849
  finally:
850
    pnode.Release()
851

    
852
  RunTestIf("cluster-upgrade", qa_cluster.TestUpgrade)
853

    
854
  RunExclusiveStorageTests()
855
  RunTestIf(["cluster-instance-policy", "instance-add-plain-disk"],
856
            TestIPolicyPlainInstance)
857

    
858
  RunTestIf(
859
    "instance-add-restricted-by-disktemplates",
860
    qa_instance.TestInstanceCreationRestrictedByDiskTemplates)
861

    
862
  # Test removing instance with offline drbd secondary
863
  if qa_config.TestEnabled(["instance-remove-drbd-offline",
864
                            "instance-add-drbd-disk"]):
865
    # Make sure the master is not put offline
866
    snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
867
    try:
868
      pnode = qa_config.AcquireNode(exclude=snode)
869
      try:
870
        instance = qa_instance.TestInstanceAddWithDrbdDisk([pnode, snode])
871
        set_offline = lambda node: qa_node.MakeNodeOffline(node, "yes")
872
        set_online = lambda node: qa_node.MakeNodeOffline(node, "no")
873
        RunTest(qa_instance.TestRemoveInstanceOfflineNode, instance, snode,
874
                set_offline, set_online)
875
      finally:
876
        pnode.Release()
877
    finally:
878
      snode.Release()
879
    qa_cluster.AssertClusterVerify()
880

    
881
  RunMonitoringTests()
882

    
883
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
884

    
885
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
886

    
887

    
888
@UsesRapiClient
889
def main():
890
  """Main program.
891

892
  """
893
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
894
  parser.add_option("--yes-do-it", dest="yes_do_it",
895
                    action="store_true",
896
                    help="Really execute the tests")
897
  (opts, args) = parser.parse_args()
898

    
899
  if len(args) == 1:
900
    (config_file, ) = args
901
  else:
902
    parser.error("Wrong number of arguments.")
903

    
904
  if not opts.yes_do_it:
905
    print ("Executing this script irreversibly destroys any Ganeti\n"
906
           "configuration on all nodes involved. If you really want\n"
907
           "to start testing, supply the --yes-do-it option.")
908
    sys.exit(1)
909

    
910
  qa_config.Load(config_file)
911

    
912
  primary = qa_config.GetMasterNode().primary
913
  qa_utils.StartMultiplexer(primary)
914
  print ("SSH command for primary node: %s" %
915
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
916
  print ("SSH command for other nodes: %s" %
917
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
918
  try:
919
    RunQa()
920
  finally:
921
    qa_utils.CloseMultiplexers()
922

    
923
if __name__ == "__main__":
924
  main()