Statistics
| Branch: | Tag: | Revision:

root / qa / qa_instance.py @ 6a343475

History | View | Annotate | Download (16.1 kB)

1
#
2
#
3

    
4
# Copyright (C) 2007 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
"""Instance related QA tests.
23

24
"""
25

    
26
import re
27
import time
28

    
29
from ganeti import utils
30
from ganeti import constants
31

    
32
import qa_config
33
import qa_utils
34
import qa_error
35

    
36
from qa_utils import AssertEqual, AssertNotEqual, AssertIn, StartSSH
37

    
38

    
39
def _GetDiskStatePath(disk):
40
  return "/sys/block/%s/device/state" % disk
41

    
42

    
43
def _GetGenericAddParameters():
44
  params = ['-B', '%s=%s' % (constants.BE_MEMORY, qa_config.get('mem'))]
45
  for idx, size in enumerate(qa_config.get('disk')):
46
    params.extend(["--disk", "%s:size=%s" % (idx, size)])
47
  return params
48

    
49

    
50
def _DiskTest(node, disk_template):
51
  master = qa_config.GetMasterNode()
52

    
53
  instance = qa_config.AcquireInstance()
54
  try:
55
    cmd = (['gnt-instance', 'add',
56
            '--os-type=%s' % qa_config.get('os'),
57
            '--disk-template=%s' % disk_template,
58
            '--node=%s' % node] +
59
           _GetGenericAddParameters())
60
    cmd.append(instance['name'])
61

    
62
    AssertEqual(StartSSH(master['primary'],
63
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
64

    
65
    _CheckSsconfInstanceList(instance["name"])
66

    
67
    return instance
68
  except:
69
    qa_config.ReleaseInstance(instance)
70
    raise
71

    
72

    
73
def TestInstanceAddWithPlainDisk(node):
74
  """gnt-instance add -t plain"""
75
  return _DiskTest(node['primary'], 'plain')
76

    
77

    
78
def TestInstanceAddWithDrbdDisk(node, node2):
79
  """gnt-instance add -t drbd"""
80
  return _DiskTest("%s:%s" % (node['primary'], node2['primary']),
81
                   'drbd')
82

    
83

    
84
def TestInstanceRemove(instance):
85
  """gnt-instance remove"""
86
  master = qa_config.GetMasterNode()
87

    
88
  cmd = ['gnt-instance', 'remove', '-f', instance['name']]
89
  AssertEqual(StartSSH(master['primary'],
90
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
91

    
92
  qa_config.ReleaseInstance(instance)
93

    
94

    
95
def TestInstanceStartup(instance):
96
  """gnt-instance startup"""
97
  master = qa_config.GetMasterNode()
98

    
99
  cmd = ['gnt-instance', 'startup', instance['name']]
100
  AssertEqual(StartSSH(master['primary'],
101
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
102

    
103

    
104
def TestInstanceShutdown(instance):
105
  """gnt-instance shutdown"""
106
  master = qa_config.GetMasterNode()
107

    
108
  cmd = ['gnt-instance', 'shutdown', instance['name']]
109
  AssertEqual(StartSSH(master['primary'],
110
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
111

    
112

    
113
def TestInstanceReboot(instance):
114
  """gnt-instance reboot"""
115
  master = qa_config.GetMasterNode()
116

    
117
  options = qa_config.get('options', {})
118
  reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
119

    
120
  for rtype in reboot_types:
121
    cmd = ['gnt-instance', 'reboot', '--type=%s' % rtype, instance['name']]
122
    AssertEqual(StartSSH(master['primary'],
123
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
124

    
125

    
126
def TestInstanceReinstall(instance):
127
  """gnt-instance reinstall"""
128
  master = qa_config.GetMasterNode()
129

    
130
  cmd = ['gnt-instance', 'reinstall', '-f', instance['name']]
131
  AssertEqual(StartSSH(master['primary'],
132
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
133

    
134

    
135
def _ReadSsconfInstanceList():
136
  """Reads ssconf_instance_list from the master node.
137

138
  """
139
  master = qa_config.GetMasterNode()
140

    
141
  cmd = ["cat", utils.PathJoin(constants.DATA_DIR,
142
                               "ssconf_%s" % constants.SS_INSTANCE_LIST)]
143

    
144
  return qa_utils.GetCommandOutput(master["primary"],
145
                                   utils.ShellQuoteArgs(cmd)).splitlines()
146

    
147

    
148
def _CheckSsconfInstanceList(instance):
149
  """Checks if a certain instance is in the ssconf instance list.
150

151
  @type instance: string
152
  @param instance: Instance name
153

154
  """
155
  AssertIn(qa_utils.ResolveInstanceName(instance),
156
           _ReadSsconfInstanceList())
157

    
158

    
159
def TestInstanceRename(instance, rename_target):
160
  """gnt-instance rename"""
161
  master = qa_config.GetMasterNode()
162

    
163
  rename_source = instance['name']
164

    
165
  for name1, name2 in [(rename_source, rename_target),
166
                       (rename_target, rename_source)]:
167
    _CheckSsconfInstanceList(name1)
168
    cmd = ['gnt-instance', 'rename', name1, name2]
169
    AssertEqual(StartSSH(master['primary'],
170
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
171
    _CheckSsconfInstanceList(name2)
172

    
173

    
174
def TestInstanceFailover(instance):
175
  """gnt-instance failover"""
176
  master = qa_config.GetMasterNode()
177

    
178
  cmd = ['gnt-instance', 'failover', '--force', instance['name']]
179
  AssertEqual(StartSSH(master['primary'],
180
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
181

    
182
  # ... and back
183
  cmd = ['gnt-instance', 'failover', '--force', instance['name']]
184
  AssertEqual(StartSSH(master['primary'],
185
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
186

    
187

    
188
def TestInstanceMigrate(instance):
189
  """gnt-instance migrate"""
190
  master = qa_config.GetMasterNode()
191

    
192
  cmd = ["gnt-instance", "migrate", "--force", instance["name"]]
193
  AssertEqual(StartSSH(master["primary"],
194
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
195

    
196
  # ... and back
197
  cmd = ["gnt-instance", "migrate", "--force", instance["name"]]
198
  AssertEqual(StartSSH(master["primary"],
199
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
200

    
201

    
202
def TestInstanceInfo(instance):
203
  """gnt-instance info"""
204
  master = qa_config.GetMasterNode()
205

    
206
  cmd = ['gnt-instance', 'info', instance['name']]
207
  AssertEqual(StartSSH(master['primary'],
208
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
209

    
210

    
211
def TestInstanceModify(instance):
212
  """gnt-instance modify"""
213
  master = qa_config.GetMasterNode()
214

    
215
  # Assume /sbin/init exists on all systems
216
  test_kernel = "/sbin/init"
217
  test_initrd = test_kernel
218

    
219
  orig_memory = qa_config.get('mem')
220
  orig_bridge = qa_config.get('bridge', 'xen-br0')
221
  args = [
222
    ["-B", "%s=128" % constants.BE_MEMORY],
223
    ["-B", "%s=%s" % (constants.BE_MEMORY, orig_memory)],
224
    ["-B", "%s=2" % constants.BE_VCPUS],
225
    ["-B", "%s=1" % constants.BE_VCPUS],
226
    ["-B", "%s=%s" % (constants.BE_VCPUS, constants.VALUE_DEFAULT)],
227

    
228
    ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, test_kernel)],
229
    ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, constants.VALUE_DEFAULT)],
230
    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, test_initrd)],
231
    ["-H", "no_%s" % (constants.HV_INITRD_PATH, )],
232
    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_DEFAULT)],
233

    
234
    # TODO: bridge tests
235
    #["--bridge", "xen-br1"],
236
    #["--bridge", orig_bridge],
237

    
238
    # TODO: Do these tests only with xen-hvm
239
    #["-H", "%s=acn" % constants.HV_BOOT_ORDER],
240
    #["-H", "%s=%s" % (constants.HV_BOOT_ORDER, constants.VALUE_DEFAULT)],
241
    ]
242
  for alist in args:
243
    cmd = ['gnt-instance', 'modify'] + alist + [instance['name']]
244
    AssertEqual(StartSSH(master['primary'],
245
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
246

    
247
  # check no-modify
248
  cmd = ['gnt-instance', 'modify', instance['name']]
249
  AssertNotEqual(StartSSH(master['primary'],
250
                          utils.ShellQuoteArgs(cmd)).wait(), 0)
251

    
252

    
253
def TestInstanceConvertDisk(instance, snode):
254
  """gnt-instance modify -t"""
255
  master = qa_config.GetMasterNode()
256
  cmd = ['gnt-instance', 'modify', '-t', 'plain', instance['name']]
257
  AssertEqual(StartSSH(master['primary'],
258
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
259
  cmd = ['gnt-instance', 'modify', '-t', 'drbd', '-n', snode['primary'],
260
         instance['name']]
261
  AssertEqual(StartSSH(master['primary'],
262
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
263

    
264

    
265
def TestInstanceList():
266
  """gnt-instance list"""
267
  master = qa_config.GetMasterNode()
268

    
269
  cmd = ['gnt-instance', 'list']
270
  AssertEqual(StartSSH(master['primary'],
271
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
272

    
273

    
274
def TestInstanceConsole(instance):
275
  """gnt-instance console"""
276
  master = qa_config.GetMasterNode()
277

    
278
  cmd = ['gnt-instance', 'console', '--show-cmd', instance['name']]
279
  AssertEqual(StartSSH(master['primary'],
280
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
281

    
282

    
283
def TestReplaceDisks(instance, pnode, snode, othernode):
284
  """gnt-instance replace-disks"""
285
  master = qa_config.GetMasterNode()
286

    
287
  def buildcmd(args):
288
    cmd = ['gnt-instance', 'replace-disks']
289
    cmd.extend(args)
290
    cmd.append(instance["name"])
291
    return cmd
292

    
293
  cmd = buildcmd(["-p"])
294
  AssertEqual(StartSSH(master['primary'],
295
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
296

    
297
  cmd = buildcmd(["-s"])
298
  AssertEqual(StartSSH(master['primary'],
299
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
300

    
301
  cmd = buildcmd(["--new-secondary=%s" % othernode["primary"]])
302
  AssertEqual(StartSSH(master['primary'],
303
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
304

    
305
  # Restore
306
  cmd = buildcmd(["--new-secondary=%s" % snode["primary"]])
307
  AssertEqual(StartSSH(master['primary'],
308
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
309

    
310

    
311
def TestInstanceExport(instance, node):
312
  """gnt-backup export -n ..."""
313
  master = qa_config.GetMasterNode()
314

    
315
  cmd = ['gnt-backup', 'export', '-n', node['primary'], instance['name']]
316
  AssertEqual(StartSSH(master['primary'],
317
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
318

    
319
  return qa_utils.ResolveInstanceName(instance["name"])
320

    
321

    
322
def TestInstanceExportWithRemove(instance, node):
323
  """gnt-backup export --remove-instance"""
324
  master = qa_config.GetMasterNode()
325

    
326
  cmd = ['gnt-backup', 'export', '-n', node['primary'], "--remove-instance",
327
         instance['name']]
328
  AssertEqual(StartSSH(master['primary'],
329
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
330

    
331

    
332
def TestInstanceExportNoTarget(instance):
333
  """gnt-backup export (without target node, should fail)"""
334
  master = qa_config.GetMasterNode()
335

    
336
  cmd = ["gnt-backup", "export", instance["name"]]
337
  AssertNotEqual(StartSSH(master['primary'],
338
                          utils.ShellQuoteArgs(cmd)).wait(), 0)
339

    
340

    
341
def TestInstanceImport(node, newinst, expnode, name):
342
  """gnt-backup import"""
343
  master = qa_config.GetMasterNode()
344

    
345
  cmd = (['gnt-backup', 'import',
346
          '--disk-template=plain',
347
          '--no-ip-check',
348
          '--net', '0:mac=generate',
349
          '--src-node=%s' % expnode['primary'],
350
          '--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
351
          '--node=%s' % node['primary']] +
352
         _GetGenericAddParameters())
353
  cmd.append(newinst['name'])
354
  AssertEqual(StartSSH(master['primary'],
355
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
356

    
357

    
358
def TestBackupList(expnode):
359
  """gnt-backup list"""
360
  master = qa_config.GetMasterNode()
361

    
362
  cmd = ['gnt-backup', 'list', '--node=%s' % expnode['primary']]
363
  AssertEqual(StartSSH(master['primary'],
364
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
365

    
366

    
367
def _TestInstanceDiskFailure(instance, node, node2, onmaster):
368
  """Testing disk failure."""
369
  master = qa_config.GetMasterNode()
370
  sq = utils.ShellQuoteArgs
371

    
372
  instance_full = qa_utils.ResolveInstanceName(instance["name"])
373
  node_full = qa_utils.ResolveNodeName(node)
374
  node2_full = qa_utils.ResolveNodeName(node2)
375

    
376
  print qa_utils.FormatInfo("Getting physical disk names")
377
  cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers',
378
         '--output=node,phys,instance',
379
         node['primary'], node2['primary']]
380
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
381

    
382
  # Get physical disk names
383
  re_disk = re.compile(r'^/dev/([a-z]+)\d+$')
384
  node2disk = {}
385
  for line in output.splitlines():
386
    (node_name, phys, inst) = line.split('|')
387
    if inst == instance_full:
388
      if node_name not in node2disk:
389
        node2disk[node_name] = []
390

    
391
      m = re_disk.match(phys)
392
      if not m:
393
        raise qa_error.Error("Unknown disk name format: %s" % disk)
394

    
395
      name = m.group(1)
396
      if name not in node2disk[node_name]:
397
        node2disk[node_name].append(name)
398

    
399
  if [node2_full, node_full][int(onmaster)] not in node2disk:
400
    raise qa_error.Error("Couldn't find physical disks used on"
401
                         " %s node" % ["secondary", "master"][int(onmaster)])
402

    
403
  print qa_utils.FormatInfo("Checking whether nodes have ability to stop"
404
                            " disks")
405
  for node_name, disks in node2disk.iteritems():
406
    cmds = []
407
    for disk in disks:
408
      cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
409
    AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0)
410

    
411
  print qa_utils.FormatInfo("Getting device paths")
412
  cmd = ['gnt-instance', 'activate-disks', instance['name']]
413
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
414
  devpath = []
415
  for line in output.splitlines():
416
    (_, _, tmpdevpath) = line.split(':')
417
    devpath.append(tmpdevpath)
418
  print devpath
419

    
420
  print qa_utils.FormatInfo("Getting drbd device paths")
421
  cmd = ['gnt-instance', 'info', instance['name']]
422
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
423
  pattern = (r'\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$'
424
             r'\s+primary:\s+(/dev/drbd\d+)\s+')
425
  drbddevs = re.findall(pattern, output, re.M)
426
  print drbddevs
427

    
428
  halted_disks = []
429
  try:
430
    print qa_utils.FormatInfo("Deactivating disks")
431
    cmds = []
432
    for name in node2disk[[node2_full, node_full][int(onmaster)]]:
433
      halted_disks.append(name)
434
      cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
435
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
436
                         ' && '.join(cmds)).wait(), 0)
437

    
438
    print qa_utils.FormatInfo("Write to disks and give some time to notice"
439
                              " to notice the problem")
440
    cmds = []
441
    for disk in devpath:
442
      cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
443
                      "if=%s" % disk, "of=%s" % disk]))
444
    for _ in (0, 1, 2):
445
      AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0)
446
      time.sleep(3)
447

    
448
    print qa_utils.FormatInfo("Debugging info")
449
    for name in drbddevs:
450
      cmd = ['drbdsetup', name, 'show']
451
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
452

    
453
    cmd = ['gnt-instance', 'info', instance['name']]
454
    AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
455

    
456
  finally:
457
    print qa_utils.FormatInfo("Activating disks again")
458
    cmds = []
459
    for name in halted_disks:
460
      cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
461
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
462
                         '; '.join(cmds)).wait(), 0)
463

    
464
  if onmaster:
465
    for name in drbddevs:
466
      cmd = ['drbdsetup', name, 'detach']
467
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
468
  else:
469
    for name in drbddevs:
470
      cmd = ['drbdsetup', name, 'disconnect']
471
      AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0)
472

    
473
  # TODO
474
  #cmd = ['vgs']
475
  #AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
476
  #                     sq(cmd)).wait(), 0)
477

    
478
  print qa_utils.FormatInfo("Making sure disks are up again")
479
  cmd = ['gnt-instance', 'replace-disks', instance['name']]
480
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
481

    
482
  print qa_utils.FormatInfo("Restarting instance")
483
  cmd = ['gnt-instance', 'shutdown', instance['name']]
484
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
485

    
486
  cmd = ['gnt-instance', 'startup', instance['name']]
487
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
488

    
489
  cmd = ['gnt-cluster', 'verify']
490
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
491

    
492

    
493
def TestInstanceMasterDiskFailure(instance, node, node2):
494
  """Testing disk failure on master node."""
495
  print qa_utils.FormatError("Disk failure on primary node cannot be"
496
                             " tested due to potential crashes.")
497
  # The following can cause crashes, thus it's disabled until fixed
498
  #return _TestInstanceDiskFailure(instance, node, node2, True)
499

    
500

    
501
def TestInstanceSecondaryDiskFailure(instance, node, node2):
502
  """Testing disk failure on secondary node."""
503
  return _TestInstanceDiskFailure(instance, node, node2, False)