Statistics
| Branch: | Tag: | Revision:

root / qa / qa_instance.py @ b8aa46ed

History | View | Annotate | Download (14.7 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, 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
    return instance
65
  except:
66
    qa_config.ReleaseInstance(instance)
67
    raise
68

    
69

    
70
def TestInstanceAddWithPlainDisk(node):
71
  """gnt-instance add -t plain"""
72
  return _DiskTest(node['primary'], 'plain')
73

    
74

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

    
80

    
81
def TestInstanceRemove(instance):
82
  """gnt-instance remove"""
83
  master = qa_config.GetMasterNode()
84

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

    
89
  qa_config.ReleaseInstance(instance)
90

    
91

    
92
def TestInstanceStartup(instance):
93
  """gnt-instance startup"""
94
  master = qa_config.GetMasterNode()
95

    
96
  cmd = ['gnt-instance', 'startup', instance['name']]
97
  AssertEqual(StartSSH(master['primary'],
98
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
99

    
100

    
101
def TestInstanceShutdown(instance):
102
  """gnt-instance shutdown"""
103
  master = qa_config.GetMasterNode()
104

    
105
  cmd = ['gnt-instance', 'shutdown', instance['name']]
106
  AssertEqual(StartSSH(master['primary'],
107
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
108

    
109

    
110
def TestInstanceReboot(instance):
111
  """gnt-instance reboot"""
112
  master = qa_config.GetMasterNode()
113

    
114
  options = qa_config.get('options', {})
115
  reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
116

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

    
122

    
123
def TestInstanceReinstall(instance):
124
  """gnt-instance reinstall"""
125
  master = qa_config.GetMasterNode()
126

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

    
131

    
132
def TestInstanceRename(instance):
133
  """gnt-instance rename"""
134
  master = qa_config.GetMasterNode()
135

    
136
  rename_source = instance['name']
137
  rename_target = qa_config.get('rename', None)
138
  if rename_target is None:
139
    print qa_utils.FormatError('"rename" entry is missing')
140
    return
141

    
142
  for name1, name2 in [(rename_source, rename_target),
143
                       (rename_target, rename_source)]:
144
    cmd = ['gnt-instance', 'rename', name1, name2]
145
    AssertEqual(StartSSH(master['primary'],
146
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
147

    
148

    
149
def TestInstanceFailover(instance):
150
  """gnt-instance failover"""
151
  master = qa_config.GetMasterNode()
152

    
153
  cmd = ['gnt-instance', 'failover', '--force', instance['name']]
154
  AssertEqual(StartSSH(master['primary'],
155
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
156

    
157
  # ... and back
158
  cmd = ['gnt-instance', 'failover', '--force', instance['name']]
159
  AssertEqual(StartSSH(master['primary'],
160
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
161

    
162

    
163
def TestInstanceInfo(instance):
164
  """gnt-instance info"""
165
  master = qa_config.GetMasterNode()
166

    
167
  cmd = ['gnt-instance', 'info', instance['name']]
168
  AssertEqual(StartSSH(master['primary'],
169
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
170

    
171

    
172
def TestInstanceModify(instance):
173
  """gnt-instance modify"""
174
  master = qa_config.GetMasterNode()
175

    
176
  # Assume /sbin/init exists on all systems
177
  test_kernel = "/sbin/init"
178
  test_initrd = test_kernel
179

    
180
  orig_memory = qa_config.get('mem')
181
  orig_bridge = qa_config.get('bridge', 'xen-br0')
182
  args = [
183
    ["-B", "%s=128" % constants.BE_MEMORY],
184
    ["-B", "%s=%s" % (constants.BE_MEMORY, orig_memory)],
185
    ["-B", "%s=2" % constants.BE_VCPUS],
186
    ["-B", "%s=1" % constants.BE_VCPUS],
187
    ["-B", "%s=%s" % (constants.BE_VCPUS, constants.VALUE_DEFAULT)],
188

    
189
    ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, test_kernel)],
190
    ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, constants.VALUE_DEFAULT)],
191
    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, test_initrd)],
192
    ["-H", "no_%s" % (constants.HV_INITRD_PATH, )],
193
    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_DEFAULT)],
194

    
195
    # TODO: bridge tests
196
    #["--bridge", "xen-br1"],
197
    #["--bridge", orig_bridge],
198

    
199
    # TODO: Do these tests only with xen-hvm
200
    #["-H", "%s=acn" % constants.HV_BOOT_ORDER],
201
    #["-H", "%s=%s" % (constants.HV_BOOT_ORDER, constants.VALUE_DEFAULT)],
202
    ]
203
  for alist in args:
204
    cmd = ['gnt-instance', 'modify'] + alist + [instance['name']]
205
    AssertEqual(StartSSH(master['primary'],
206
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
207

    
208
  # check no-modify
209
  cmd = ['gnt-instance', 'modify', instance['name']]
210
  AssertNotEqual(StartSSH(master['primary'],
211
                          utils.ShellQuoteArgs(cmd)).wait(), 0)
212

    
213

    
214
def TestInstanceConvertDisk(instance, snode):
215
  """gnt-instance modify -t"""
216
  master = qa_config.GetMasterNode()
217
  cmd = ['gnt-instance', 'modify', '-t', 'plain', instance['name']]
218
  AssertEqual(StartSSH(master['primary'],
219
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
220
  cmd = ['gnt-instance', 'modify', '-t', 'drbd', '-n', snode['primary'],
221
         instance['name']]
222
  AssertEqual(StartSSH(master['primary'],
223
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
224

    
225

    
226
def TestInstanceList():
227
  """gnt-instance list"""
228
  master = qa_config.GetMasterNode()
229

    
230
  cmd = ['gnt-instance', 'list']
231
  AssertEqual(StartSSH(master['primary'],
232
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
233

    
234

    
235
def TestInstanceConsole(instance):
236
  """gnt-instance console"""
237
  master = qa_config.GetMasterNode()
238

    
239
  cmd = ['gnt-instance', 'console', '--show-cmd', instance['name']]
240
  AssertEqual(StartSSH(master['primary'],
241
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
242

    
243

    
244
def TestReplaceDisks(instance, pnode, snode, othernode):
245
  """gnt-instance replace-disks"""
246
  master = qa_config.GetMasterNode()
247

    
248
  def buildcmd(args):
249
    cmd = ['gnt-instance', 'replace-disks']
250
    cmd.extend(args)
251
    cmd.append(instance["name"])
252
    return cmd
253

    
254
  cmd = buildcmd(["-p"])
255
  AssertEqual(StartSSH(master['primary'],
256
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
257

    
258
  cmd = buildcmd(["-s"])
259
  AssertEqual(StartSSH(master['primary'],
260
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
261

    
262
  cmd = buildcmd(["--new-secondary=%s" % othernode["primary"]])
263
  AssertEqual(StartSSH(master['primary'],
264
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
265

    
266
  # Restore
267
  cmd = buildcmd(["--new-secondary=%s" % snode["primary"]])
268
  AssertEqual(StartSSH(master['primary'],
269
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
270

    
271

    
272
def TestInstanceExport(instance, node):
273
  """gnt-backup export"""
274
  master = qa_config.GetMasterNode()
275

    
276
  cmd = ['gnt-backup', 'export', '-n', node['primary'], instance['name']]
277
  AssertEqual(StartSSH(master['primary'],
278
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
279

    
280
  return qa_utils.ResolveInstanceName(instance)
281

    
282

    
283
def TestInstanceExportWithRemove(instance, node):
284
  """gnt-backup export --remove-instance"""
285
  master = qa_config.GetMasterNode()
286

    
287
  cmd = ['gnt-backup', 'export', '-n', node['primary'], "--remove-instance",
288
         instance['name']]
289
  AssertEqual(StartSSH(master['primary'],
290
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
291

    
292

    
293
def TestInstanceImport(node, newinst, expnode, name):
294
  """gnt-backup import"""
295
  master = qa_config.GetMasterNode()
296

    
297
  cmd = (['gnt-backup', 'import',
298
          '--disk-template=plain',
299
          '--no-ip-check',
300
          '--net', '0:mac=generate',
301
          '--src-node=%s' % expnode['primary'],
302
          '--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
303
          '--node=%s' % node['primary']] +
304
         _GetGenericAddParameters())
305
  cmd.append(newinst['name'])
306
  AssertEqual(StartSSH(master['primary'],
307
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
308

    
309

    
310
def TestBackupList(expnode):
311
  """gnt-backup list"""
312
  master = qa_config.GetMasterNode()
313

    
314
  cmd = ['gnt-backup', 'list', '--node=%s' % expnode['primary']]
315
  AssertEqual(StartSSH(master['primary'],
316
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
317

    
318

    
319
def _TestInstanceDiskFailure(instance, node, node2, onmaster):
320
  """Testing disk failure."""
321
  master = qa_config.GetMasterNode()
322
  sq = utils.ShellQuoteArgs
323

    
324
  instance_full = qa_utils.ResolveInstanceName(instance)
325
  node_full = qa_utils.ResolveNodeName(node)
326
  node2_full = qa_utils.ResolveNodeName(node2)
327

    
328
  print qa_utils.FormatInfo("Getting physical disk names")
329
  cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers',
330
         '--output=node,phys,instance',
331
         node['primary'], node2['primary']]
332
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
333

    
334
  # Get physical disk names
335
  re_disk = re.compile(r'^/dev/([a-z]+)\d+$')
336
  node2disk = {}
337
  for line in output.splitlines():
338
    (node_name, phys, inst) = line.split('|')
339
    if inst == instance_full:
340
      if node_name not in node2disk:
341
        node2disk[node_name] = []
342

    
343
      m = re_disk.match(phys)
344
      if not m:
345
        raise qa_error.Error("Unknown disk name format: %s" % disk)
346

    
347
      name = m.group(1)
348
      if name not in node2disk[node_name]:
349
        node2disk[node_name].append(name)
350

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

    
355
  print qa_utils.FormatInfo("Checking whether nodes have ability to stop"
356
                            " disks")
357
  for node_name, disks in node2disk.iteritems():
358
    cmds = []
359
    for disk in disks:
360
      cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
361
    AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0)
362

    
363
  print qa_utils.FormatInfo("Getting device paths")
364
  cmd = ['gnt-instance', 'activate-disks', instance['name']]
365
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
366
  devpath = []
367
  for line in output.splitlines():
368
    (_, _, tmpdevpath) = line.split(':')
369
    devpath.append(tmpdevpath)
370
  print devpath
371

    
372
  print qa_utils.FormatInfo("Getting drbd device paths")
373
  cmd = ['gnt-instance', 'info', instance['name']]
374
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
375
  pattern = (r'\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$'
376
             r'\s+primary:\s+(/dev/drbd\d+)\s+')
377
  drbddevs = re.findall(pattern, output, re.M)
378
  print drbddevs
379

    
380
  halted_disks = []
381
  try:
382
    print qa_utils.FormatInfo("Deactivating disks")
383
    cmds = []
384
    for name in node2disk[[node2_full, node_full][int(onmaster)]]:
385
      halted_disks.append(name)
386
      cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
387
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
388
                         ' && '.join(cmds)).wait(), 0)
389

    
390
    print qa_utils.FormatInfo("Write to disks and give some time to notice"
391
                              " to notice the problem")
392
    cmds = []
393
    for disk in devpath:
394
      cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
395
                      "if=%s" % disk, "of=%s" % disk]))
396
    for _ in (0, 1, 2):
397
      AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0)
398
      time.sleep(3)
399

    
400
    print qa_utils.FormatInfo("Debugging info")
401
    for name in drbddevs:
402
      cmd = ['drbdsetup', name, 'show']
403
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
404

    
405
    cmd = ['gnt-instance', 'info', instance['name']]
406
    AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
407

    
408
  finally:
409
    print qa_utils.FormatInfo("Activating disks again")
410
    cmds = []
411
    for name in halted_disks:
412
      cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
413
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
414
                         '; '.join(cmds)).wait(), 0)
415

    
416
  if onmaster:
417
    for name in drbddevs:
418
      cmd = ['drbdsetup', name, 'detach']
419
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
420
  else:
421
    for name in drbddevs:
422
      cmd = ['drbdsetup', name, 'disconnect']
423
      AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0)
424

    
425
  # TODO
426
  #cmd = ['vgs']
427
  #AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
428
  #                     sq(cmd)).wait(), 0)
429

    
430
  print qa_utils.FormatInfo("Making sure disks are up again")
431
  cmd = ['gnt-instance', 'replace-disks', instance['name']]
432
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
433

    
434
  print qa_utils.FormatInfo("Restarting instance")
435
  cmd = ['gnt-instance', 'shutdown', instance['name']]
436
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
437

    
438
  cmd = ['gnt-instance', 'startup', instance['name']]
439
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
440

    
441
  cmd = ['gnt-cluster', 'verify']
442
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
443

    
444

    
445
def TestInstanceMasterDiskFailure(instance, node, node2):
446
  """Testing disk failure on master node."""
447
  print qa_utils.FormatError("Disk failure on primary node cannot be"
448
                             " tested due to potential crashes.")
449
  # The following can cause crashes, thus it's disabled until fixed
450
  #return _TestInstanceDiskFailure(instance, node, node2, True)
451

    
452

    
453
def TestInstanceSecondaryDiskFailure(instance, node, node2):
454
  """Testing disk failure on secondary node."""
455
  return _TestInstanceDiskFailure(instance, node, node2, False)