Statistics
| Branch: | Tag: | Revision:

root / qa / ganeti-qa.py @ 66cb789f

History | View | Annotate | Download (29.5 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_network
42
import qa_node
43
import qa_os
44
import qa_job
45
import qa_rapi
46
import qa_tags
47
import qa_utils
48

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

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

    
58

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

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

    
68

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

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

    
78
  return desc.rstrip(".")
79

    
80

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

84
  """
85

    
86
  tstart = datetime.datetime.now()
87

    
88
  desc = _DescriptionOf(fn)
89

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

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

    
101

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

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

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

    
118

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

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

    
127

    
128
def _LookupRapiSecret(rapi_user):
129
  """Find the RAPI secret for the given user.
130

131
  @param rapi_user: Login user
132
  @return: Login secret for the user
133

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

    
152

    
153
def SetupCluster(rapi_user):
154
  """Initializes the cluster.
155

156
  @param rapi_user: Login user for RAPI
157
  @return: Login secret for RAPI
158

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

    
171
  # Test on empty cluster
172
  RunTestIf("node-list", qa_node.TestNodeList)
173
  RunTestIf("instance-list", qa_instance.TestInstanceList)
174
  RunTestIf("job-list", qa_job.TestJobList)
175

    
176
  RunTestIf("create-cluster", qa_node.TestNodeAddAll)
177
  if not qa_config.TestEnabled("create-cluster"):
178
    # consider the nodes are already there
179
    qa_node.MarkNodeAddedAll()
180

    
181
  RunTestIf("test-jobqueue", qa_cluster.TestJobqueue)
182
  RunTestIf("test-jobqueue", qa_job.TestJobCancellation)
183

    
184
  # enable the watcher (unconditionally)
185
  RunTest(qa_daemon.TestResumeWatcher)
186

    
187
  RunTestIf("node-list", qa_node.TestNodeList)
188

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

    
195
  RunTestIf("node-info", qa_node.TestNodeInfo)
196

    
197
  return rapi_secret
198

    
199

    
200
def RunClusterTests():
201
  """Runs tests related to gnt-cluster.
202

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

    
236

    
237
def RunRepairDiskSizes():
238
  """Run the repair disk-sizes test.
239

240
  """
241
  RunTestIf("cluster-repair-disk-sizes", qa_cluster.TestClusterRepairDiskSizes)
242

    
243

    
244
def RunOsTests():
245
  """Runs all tests related to gnt-os.
246

247
  """
248
  os_enabled = ["os", qa_config.NoVirtualCluster]
249

    
250
  if qa_config.TestEnabled(qa_rapi.Enabled):
251
    rapi_getos = qa_rapi.GetOperatingSystems
252
  else:
253
    rapi_getos = None
254

    
255
  for fn in [
256
    qa_os.TestOsList,
257
    qa_os.TestOsDiagnose,
258
    ]:
259
    RunTestIf(os_enabled, fn)
260

    
261
  for fn in [
262
    qa_os.TestOsValid,
263
    qa_os.TestOsInvalid,
264
    qa_os.TestOsPartiallyValid,
265
    ]:
266
    RunTestIf(os_enabled, fn, rapi_getos)
267

    
268
  for fn in [
269
    qa_os.TestOsModifyValid,
270
    qa_os.TestOsModifyInvalid,
271
    qa_os.TestOsStatesNonExisting,
272
    ]:
273
    RunTestIf(os_enabled, fn)
274

    
275

    
276
def RunCommonInstanceTests(instance):
277
  """Runs a few tests that are common to all disk types.
278

279
  """
280
  RunTestIf("instance-shutdown", qa_instance.TestInstanceShutdown, instance)
281
  RunTestIf(["instance-shutdown", "instance-console", qa_rapi.Enabled],
282
            qa_rapi.TestRapiStoppedInstanceConsole, instance)
283
  RunTestIf(["instance-shutdown", "instance-modify"],
284
            qa_instance.TestInstanceStoppedModify, instance)
285
  RunTestIf("instance-shutdown", qa_instance.TestInstanceStartup, instance)
286

    
287
  # Test shutdown/start via RAPI
288
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
289
            qa_rapi.TestRapiInstanceShutdown, instance)
290
  RunTestIf(["instance-shutdown", qa_rapi.Enabled],
291
            qa_rapi.TestRapiInstanceStartup, instance)
292

    
293
  RunTestIf("instance-list", qa_instance.TestInstanceList)
294

    
295
  RunTestIf("instance-info", qa_instance.TestInstanceInfo, instance)
296

    
297
  RunTestIf("instance-modify", qa_instance.TestInstanceModify, instance)
298
  RunTestIf(["instance-modify", qa_rapi.Enabled],
299
            qa_rapi.TestRapiInstanceModify, instance)
300

    
301
  RunTestIf("instance-console", qa_instance.TestInstanceConsole, instance)
302
  RunTestIf(["instance-console", qa_rapi.Enabled],
303
            qa_rapi.TestRapiInstanceConsole, instance)
304

    
305
  RunTestIf("instance-device-names", qa_instance.TestInstanceDeviceNames,
306
            instance)
307
  DOWN_TESTS = qa_config.Either([
308
    "instance-reinstall",
309
    "instance-rename",
310
    "instance-grow-disk",
311
    ])
312

    
313
  # shutdown instance for any 'down' tests
314
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceShutdown, instance)
315

    
316
  # now run the 'down' state tests
317
  RunTestIf("instance-reinstall", qa_instance.TestInstanceReinstall, instance)
318
  RunTestIf(["instance-reinstall", qa_rapi.Enabled],
319
            qa_rapi.TestRapiInstanceReinstall, instance)
320

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

    
340
  RunTestIf(["instance-grow-disk"], qa_instance.TestInstanceGrowDisk, instance)
341

    
342
  # and now start the instance again
343
  RunTestIf(DOWN_TESTS, qa_instance.TestInstanceStartup, instance)
344

    
345
  RunTestIf("instance-reboot", qa_instance.TestInstanceReboot, instance)
346

    
347
  RunTestIf("tags", qa_tags.TestInstanceTags, instance)
348

    
349
  RunTestIf("cluster-verify", qa_cluster.TestClusterVerify)
350

    
351
  RunTestIf(qa_rapi.Enabled, qa_rapi.TestInstance, instance)
352

    
353
  # Lists instances, too
354
  RunTestIf("node-list", qa_node.TestNodeList)
355

    
356
  # Some jobs have been run, let's test listing them
357
  RunTestIf("job-list", qa_job.TestJobList)
358

    
359

    
360
def RunCommonNodeTests():
361
  """Run a few common node tests.
362

363
  """
364
  RunTestIf("node-volumes", qa_node.TestNodeVolumes)
365
  RunTestIf("node-storage", qa_node.TestNodeStorage)
366
  RunTestIf(["node-oob", qa_config.NoVirtualCluster], qa_node.TestOutOfBand)
367

    
368

    
369
def RunGroupListTests():
370
  """Run tests for listing node groups.
371

372
  """
373
  RunTestIf("group-list", qa_group.TestGroupList)
374
  RunTestIf("group-list", qa_group.TestGroupListFields)
375

    
376

    
377
def RunNetworkTests():
378
  """Run tests for network management.
379

380
  """
381
  RunTestIf("network", qa_network.TestNetworkAddRemove)
382
  RunTestIf("network", qa_network.TestNetworkConnect)
383
  RunTestIf(["network", "tags"], qa_network.TestNetworkTags)
384

    
385

    
386
def RunGroupRwTests():
387
  """Run tests for adding/removing/renaming groups.
388

389
  """
390
  RunTestIf("group-rwops", qa_group.TestGroupAddRemoveRename)
391
  RunTestIf("group-rwops", qa_group.TestGroupAddWithOptions)
392
  RunTestIf("group-rwops", qa_group.TestGroupModify)
393
  RunTestIf(["group-rwops", qa_rapi.Enabled], qa_rapi.TestRapiNodeGroups)
394
  RunTestIf(["group-rwops", "tags"], qa_tags.TestGroupTags,
395
            qa_group.GetDefaultGroup())
396

    
397

    
398
def RunExportImportTests(instance, inodes):
399
  """Tries to export and import the instance.
400

401
  @type inodes: list of nodes
402
  @param inodes: current nodes of the instance
403

404
  """
405
  # FIXME: export explicitly bails out on file based storage. other non-lvm
406
  # based storage types are untested, though. Also note that import could still
407
  # work, but is deeply embedded into the "export" case.
408
  if (qa_config.TestEnabled("instance-export") and
409
      instance.disk_template != constants.DT_FILE):
410
    RunTest(qa_instance.TestInstanceExportNoTarget, instance)
411

    
412
    pnode = inodes[0]
413
    expnode = qa_config.AcquireNode(exclude=pnode)
414
    try:
415
      name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
416

    
417
      RunTest(qa_instance.TestBackupList, expnode)
418

    
419
      if qa_config.TestEnabled("instance-import"):
420
        newinst = qa_config.AcquireInstance()
421
        try:
422
          RunTest(qa_instance.TestInstanceImport, newinst, pnode,
423
                  expnode, name)
424
          # Check if starting the instance works
425
          RunTest(qa_instance.TestInstanceStartup, newinst)
426
          RunTest(qa_instance.TestInstanceRemove, newinst)
427
        finally:
428
          newinst.Release()
429
    finally:
430
      expnode.Release()
431

    
432
  # FIXME: inter-cluster-instance-move crashes on file based instances :/
433
  # See Issue 414.
434
  if (qa_config.TestEnabled([qa_rapi.Enabled, "inter-cluster-instance-move"])
435
      and instance.disk_template != constants.DT_FILE):
436
    newinst = qa_config.AcquireInstance()
437
    try:
438
      tnode = qa_config.AcquireNode(exclude=inodes)
439
      try:
440
        RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
441
                inodes, tnode)
442
      finally:
443
        tnode.Release()
444
    finally:
445
      newinst.Release()
446

    
447

    
448
def RunDaemonTests(instance):
449
  """Test the ganeti-watcher script.
450

451
  """
452
  RunTest(qa_daemon.TestPauseWatcher)
453

    
454
  RunTestIf("instance-automatic-restart",
455
            qa_daemon.TestInstanceAutomaticRestart, instance)
456
  RunTestIf("instance-consecutive-failures",
457
            qa_daemon.TestInstanceConsecutiveFailures, instance)
458

    
459
  RunTest(qa_daemon.TestResumeWatcher)
460

    
461

    
462
def RunHardwareFailureTests(instance, inodes):
463
  """Test cluster internal hardware failure recovery.
464

465
  """
466
  RunTestIf("instance-failover", qa_instance.TestInstanceFailover, instance)
467
  RunTestIf(["instance-failover", qa_rapi.Enabled],
468
            qa_rapi.TestRapiInstanceFailover, instance)
469

    
470
  RunTestIf("instance-migrate", qa_instance.TestInstanceMigrate, instance)
471
  RunTestIf(["instance-migrate", qa_rapi.Enabled],
472
            qa_rapi.TestRapiInstanceMigrate, instance)
473

    
474
  if qa_config.TestEnabled("instance-replace-disks"):
475
    # We just need alternative secondary nodes, hence "- 1"
476
    othernodes = qa_config.AcquireManyNodes(len(inodes) - 1, exclude=inodes)
477
    try:
478
      RunTestIf(qa_rapi.Enabled, qa_rapi.TestRapiInstanceReplaceDisks, instance)
479
      RunTest(qa_instance.TestReplaceDisks,
480
              instance, inodes, othernodes)
481
    finally:
482
      qa_config.ReleaseManyNodes(othernodes)
483
    del othernodes
484

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

    
504
  if len(inodes) >= 2:
505
    RunTestIf("node-evacuate", qa_node.TestNodeEvacuate, inodes[0], inodes[1])
506
    RunTestIf("node-failover", qa_node.TestNodeFailover, inodes[0], inodes[1])
507

    
508

    
509
def RunExclusiveStorageTests():
510
  """Test exclusive storage."""
511
  if not qa_config.TestEnabled("cluster-exclusive-storage"):
512
    return
513

    
514
  node = qa_config.AcquireNode()
515
  try:
516
    old_es = qa_cluster.TestSetExclStorCluster(False)
517
    qa_node.TestExclStorSingleNode(node)
518

    
519
    qa_cluster.TestSetExclStorCluster(True)
520
    qa_cluster.TestExclStorSharedPv(node)
521

    
522
    if qa_config.TestEnabled("instance-add-plain-disk"):
523
      # Make sure that the cluster doesn't have any pre-existing problem
524
      qa_cluster.AssertClusterVerify()
525

    
526
      # Create and allocate instances
527
      instance1 = qa_instance.TestInstanceAddWithPlainDisk([node])
528
      try:
529
        instance2 = qa_instance.TestInstanceAddWithPlainDisk([node])
530
        try:
531
          # cluster-verify checks that disks are allocated correctly
532
          qa_cluster.AssertClusterVerify()
533

    
534
          # Remove instances
535
          qa_instance.TestInstanceRemove(instance2)
536
          qa_instance.TestInstanceRemove(instance1)
537
        finally:
538
          instance2.Release()
539
      finally:
540
        instance1.Release()
541

    
542
    if qa_config.TestEnabled("instance-add-drbd-disk"):
543
      snode = qa_config.AcquireNode()
544
      try:
545
        qa_cluster.TestSetExclStorCluster(False)
546
        instance = qa_instance.TestInstanceAddWithDrbdDisk([node, snode])
547
        try:
548
          qa_cluster.TestSetExclStorCluster(True)
549
          exp_err = [constants.CV_EINSTANCEUNSUITABLENODE]
550
          qa_cluster.AssertClusterVerify(fail=True, errors=exp_err)
551
          qa_instance.TestInstanceRemove(instance)
552
        finally:
553
          instance.Release()
554
      finally:
555
        snode.Release()
556
    qa_cluster.TestSetExclStorCluster(old_es)
557
  finally:
558
    node.Release()
559

    
560

    
561
def _BuildSpecDict(par, mn, st, mx):
562
  return {
563
    constants.ISPECS_MINMAX: [{
564
      constants.ISPECS_MIN: {par: mn},
565
      constants.ISPECS_MAX: {par: mx},
566
      }],
567
    constants.ISPECS_STD: {par: st},
568
    }
569

    
570

    
571
def _BuildDoubleSpecDict(index, par, mn, st, mx):
572
  new_spec = {
573
    constants.ISPECS_MINMAX: [{}, {}],
574
    }
575
  if st is not None:
576
    new_spec[constants.ISPECS_STD] = {par: st}
577
  new_spec[constants.ISPECS_MINMAX][index] = {
578
    constants.ISPECS_MIN: {par: mn},
579
    constants.ISPECS_MAX: {par: mx},
580
    }
581
  return new_spec
582

    
583

    
584
def TestIPolicyPlainInstance():
585
  """Test instance policy interaction with instances"""
586
  params = ["memory-size", "cpu-count", "disk-count", "disk-size", "nic-count"]
587
  if not qa_config.IsTemplateSupported(constants.DT_PLAIN):
588
    print "Template %s not supported" % constants.DT_PLAIN
589
    return
590

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

    
618
      # Test with two instance specs
619
      double_specs = copy.deepcopy(old_specs)
620
      double_specs[constants.ISPECS_MINMAX] = \
621
          double_specs[constants.ISPECS_MINMAX] * 2
622
      (par1, par2) = params[0:2]
623
      (_, imaxval1) = qa_instance.GetInstanceSpec(instance.name, par1)
624
      (_, imaxval2) = qa_instance.GetInstanceSpec(instance.name, par2)
625
      old_minmax = old_specs[constants.ISPECS_MINMAX][0]
626
      history.extend([
627
        (double_specs, None, False),
628
        # The first min/max limit is being violated
629
        (None,
630
         _BuildDoubleSpecDict(0, par1, imaxval1 + 4, imaxval1 + 4,
631
                              imaxval1 + 4),
632
         False),
633
        # Both min/max limits are being violated
634
        (None,
635
         _BuildDoubleSpecDict(1, par2, imaxval2 + 4, None, imaxval2 + 4),
636
         True),
637
        # The second min/max limit is being violated
638
        (None,
639
         _BuildDoubleSpecDict(0, par1,
640
                              old_minmax[constants.ISPECS_MIN][par1],
641
                              old_specs[constants.ISPECS_STD][par1],
642
                              old_minmax[constants.ISPECS_MAX][par1]),
643
         False),
644
        (old_specs, None, False),
645
        ])
646

    
647
      # Apply the changes, and check policy violations after each change
648
      qa_cluster.AssertClusterVerify()
649
      for (new_specs, diff_specs, failed) in history:
650
        qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
651
                                        diff_specs=diff_specs)
652
        if failed:
653
          qa_cluster.AssertClusterVerify(warnings=policyerror)
654
        else:
655
          qa_cluster.AssertClusterVerify()
656

    
657
      qa_instance.TestInstanceRemove(instance)
658
    finally:
659
      instance.Release()
660

    
661
    # Now we replay the same policy changes, and we expect that the instance
662
    # cannot be created for the cases where we had a policy violation above
663
    for (new_specs, diff_specs, failed) in history:
664
      qa_cluster.TestClusterSetISpecs(new_specs=new_specs,
665
                                      diff_specs=diff_specs)
666
      if failed:
667
        qa_instance.TestInstanceAddWithPlainDisk([node], fail=True)
668
      # Instance creation with no policy violation has been tested already
669
  finally:
670
    node.Release()
671

    
672

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

    
696

    
697
def RunInstanceTests():
698
  """Create and exercise instances."""
699
  instance_tests = [
700
    ("instance-add-plain-disk", constants.DT_PLAIN,
701
     qa_instance.TestInstanceAddWithPlainDisk, 1),
702
    ("instance-add-drbd-disk", constants.DT_DRBD8,
703
     qa_instance.TestInstanceAddWithDrbdDisk, 2),
704
    ("instance-add-diskless", constants.DT_DISKLESS,
705
     qa_instance.TestInstanceAddDiskless, 1),
706
    ("instance-add-file", constants.DT_FILE,
707
     qa_instance.TestInstanceAddFile, 1)
708
    ]
709

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

    
750

    
751
def RunQa():
752
  """Main QA body.
753

754
  """
755
  rapi_user = "ganeti-qa"
756

    
757
  RunEnvTests()
758
  rapi_secret = SetupCluster(rapi_user)
759

    
760
  if qa_rapi.Enabled():
761
    # Load RAPI certificate
762
    qa_rapi.Setup(rapi_user, rapi_secret)
763

    
764
  RunClusterTests()
765
  RunOsTests()
766

    
767
  RunTestIf("tags", qa_tags.TestClusterTags)
768

    
769
  RunCommonNodeTests()
770
  RunGroupListTests()
771
  RunGroupRwTests()
772
  RunNetworkTests()
773

    
774
  # The master shouldn't be readded or put offline; "delay" needs a non-master
775
  # node to test
776
  pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
777
  try:
778
    RunTestIf("node-readd", qa_node.TestNodeReadd, pnode)
779
    RunTestIf("node-modify", qa_node.TestNodeModify, pnode)
780
    RunTestIf("delay", qa_cluster.TestDelay, pnode)
781
  finally:
782
    pnode.Release()
783

    
784
  # Make sure the cluster is clean before running instance tests
785
  qa_cluster.AssertClusterVerify()
786

    
787
  pnode = qa_config.AcquireNode()
788
  try:
789
    RunTestIf("tags", qa_tags.TestNodeTags, pnode)
790

    
791
    if qa_rapi.Enabled():
792
      RunTest(qa_rapi.TestNode, pnode)
793

    
794
      if qa_config.TestEnabled("instance-add-plain-disk"):
795
        for use_client in [True, False]:
796
          rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
797
                                  use_client)
798
          try:
799
            if qa_config.TestEnabled("instance-plain-rapi-common-tests"):
800
              RunCommonInstanceTests(rapi_instance)
801
            RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
802
          finally:
803
            rapi_instance.Release()
804
          del rapi_instance
805

    
806
  finally:
807
    pnode.Release()
808

    
809
  config_list = [
810
    ("default-instance-tests", lambda: None, lambda _: None),
811
    (IsExclusiveStorageInstanceTestEnabled,
812
     lambda: qa_cluster.TestSetExclStorCluster(True),
813
     qa_cluster.TestSetExclStorCluster),
814
  ]
815
  for (conf_name, setup_conf_f, restore_conf_f) in config_list:
816
    if qa_config.TestEnabled(conf_name):
817
      oldconf = setup_conf_f()
818
      RunInstanceTests()
819
      restore_conf_f(oldconf)
820

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

    
842
  finally:
843
    pnode.Release()
844

    
845
  RunExclusiveStorageTests()
846
  RunTestIf(["cluster-instance-policy", "instance-add-plain-disk"],
847
            TestIPolicyPlainInstance)
848

    
849
  RunTestIf(
850
    "instance-add-restricted-by-disktemplates",
851
    qa_instance.TestInstanceCreationRestrictedByDiskTemplates)
852

    
853
  # Test removing instance with offline drbd secondary
854
  if qa_config.TestEnabled(["instance-remove-drbd-offline",
855
                            "instance-add-drbd-disk"]):
856
    # Make sure the master is not put offline
857
    snode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
858
    try:
859
      pnode = qa_config.AcquireNode(exclude=snode)
860
      try:
861
        instance = qa_instance.TestInstanceAddWithDrbdDisk([pnode, snode])
862
        set_offline = lambda node: qa_node.MakeNodeOffline(node, "yes")
863
        set_online = lambda node: qa_node.MakeNodeOffline(node, "no")
864
        RunTest(qa_instance.TestRemoveInstanceOfflineNode, instance, snode,
865
                set_offline, set_online)
866
      finally:
867
        pnode.Release()
868
    finally:
869
      snode.Release()
870
    qa_cluster.AssertClusterVerify()
871

    
872
  RunTestIf("create-cluster", qa_node.TestNodeRemoveAll)
873

    
874
  RunTestIf("cluster-destroy", qa_cluster.TestClusterDestroy)
875

    
876

    
877
@UsesRapiClient
878
def main():
879
  """Main program.
880

881
  """
882
  parser = optparse.OptionParser(usage="%prog [options] <config-file>")
883
  parser.add_option("--yes-do-it", dest="yes_do_it",
884
                    action="store_true",
885
                    help="Really execute the tests")
886
  (opts, args) = parser.parse_args()
887

    
888
  if len(args) == 1:
889
    (config_file, ) = args
890
  else:
891
    parser.error("Wrong number of arguments.")
892

    
893
  if not opts.yes_do_it:
894
    print ("Executing this script irreversibly destroys any Ganeti\n"
895
           "configuration on all nodes involved. If you really want\n"
896
           "to start testing, supply the --yes-do-it option.")
897
    sys.exit(1)
898

    
899
  qa_config.Load(config_file)
900

    
901
  primary = qa_config.GetMasterNode().primary
902
  qa_utils.StartMultiplexer(primary)
903
  print ("SSH command for primary node: %s" %
904
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand(primary, "")))
905
  print ("SSH command for other nodes: %s" %
906
         utils.ShellQuoteArgs(qa_utils.GetSSHCommand("NODE", "")))
907
  try:
908
    RunQa()
909
  finally:
910
    qa_utils.CloseMultiplexers()
911

    
912
if __name__ == "__main__":
913
  main()