Statistics
| Branch: | Tag: | Revision:

root / qa / qa_instance.py @ b73302c4

History | View | Annotate | Download (14.3 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
  for reboottype in ["soft", "hard", "full"]:
115
    cmd = ['gnt-instance', 'reboot', '--type=%s' % reboottype,
116
           instance['name']]
117
    AssertEqual(StartSSH(master['primary'],
118
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
119

    
120

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

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

    
129

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

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

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

    
146

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

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

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

    
160

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

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

    
169

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

    
174
  # Assume /sbin/init exists on all systems
175
  test_kernel = "/sbin/init"
176
  test_initrd = test_kernel
177

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

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

    
193
    # TODO: bridge tests
194
    #["--bridge", "xen-br1"],
195
    #["--bridge", orig_bridge],
196

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

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

    
211

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

    
223

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

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

    
232

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

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

    
241

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

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

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

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

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

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

    
269

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

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

    
278
  return qa_utils.ResolveInstanceName(instance)
279

    
280

    
281
def TestInstanceImport(node, newinst, expnode, name):
282
  """gnt-backup import"""
283
  master = qa_config.GetMasterNode()
284

    
285
  cmd = (['gnt-backup', 'import',
286
          '--disk-template=plain',
287
          '--no-ip-check',
288
          '--net', '0:mac=generate',
289
          '--src-node=%s' % expnode['primary'],
290
          '--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
291
          '--node=%s' % node['primary']] +
292
         _GetGenericAddParameters())
293
  cmd.append(newinst['name'])
294
  AssertEqual(StartSSH(master['primary'],
295
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
296

    
297

    
298
def TestBackupList(expnode):
299
  """gnt-backup list"""
300
  master = qa_config.GetMasterNode()
301

    
302
  cmd = ['gnt-backup', 'list', '--node=%s' % expnode['primary']]
303
  AssertEqual(StartSSH(master['primary'],
304
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
305

    
306

    
307
def _TestInstanceDiskFailure(instance, node, node2, onmaster):
308
  """Testing disk failure."""
309
  master = qa_config.GetMasterNode()
310
  sq = utils.ShellQuoteArgs
311

    
312
  instance_full = qa_utils.ResolveInstanceName(instance)
313
  node_full = qa_utils.ResolveNodeName(node)
314
  node2_full = qa_utils.ResolveNodeName(node2)
315

    
316
  print qa_utils.FormatInfo("Getting physical disk names")
317
  cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers',
318
         '--output=node,phys,instance',
319
         node['primary'], node2['primary']]
320
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
321

    
322
  # Get physical disk names
323
  re_disk = re.compile(r'^/dev/([a-z]+)\d+$')
324
  node2disk = {}
325
  for line in output.splitlines():
326
    (node_name, phys, inst) = line.split('|')
327
    if inst == instance_full:
328
      if node_name not in node2disk:
329
        node2disk[node_name] = []
330

    
331
      m = re_disk.match(phys)
332
      if not m:
333
        raise qa_error.Error("Unknown disk name format: %s" % disk)
334

    
335
      name = m.group(1)
336
      if name not in node2disk[node_name]:
337
        node2disk[node_name].append(name)
338

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

    
343
  print qa_utils.FormatInfo("Checking whether nodes have ability to stop"
344
                            " disks")
345
  for node_name, disks in node2disk.iteritems():
346
    cmds = []
347
    for disk in disks:
348
      cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
349
    AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0)
350

    
351
  print qa_utils.FormatInfo("Getting device paths")
352
  cmd = ['gnt-instance', 'activate-disks', instance['name']]
353
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
354
  devpath = []
355
  for line in output.splitlines():
356
    (_, _, tmpdevpath) = line.split(':')
357
    devpath.append(tmpdevpath)
358
  print devpath
359

    
360
  print qa_utils.FormatInfo("Getting drbd device paths")
361
  cmd = ['gnt-instance', 'info', instance['name']]
362
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
363
  pattern = (r'\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$'
364
             r'\s+primary:\s+(/dev/drbd\d+)\s+')
365
  drbddevs = re.findall(pattern, output, re.M)
366
  print drbddevs
367

    
368
  halted_disks = []
369
  try:
370
    print qa_utils.FormatInfo("Deactivating disks")
371
    cmds = []
372
    for name in node2disk[[node2_full, node_full][int(onmaster)]]:
373
      halted_disks.append(name)
374
      cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
375
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
376
                         ' && '.join(cmds)).wait(), 0)
377

    
378
    print qa_utils.FormatInfo("Write to disks and give some time to notice"
379
                              " to notice the problem")
380
    cmds = []
381
    for disk in devpath:
382
      cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
383
                      "if=%s" % disk, "of=%s" % disk]))
384
    for _ in (0, 1, 2):
385
      AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0)
386
      time.sleep(3)
387

    
388
    print qa_utils.FormatInfo("Debugging info")
389
    for name in drbddevs:
390
      cmd = ['drbdsetup', name, 'show']
391
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
392

    
393
    cmd = ['gnt-instance', 'info', instance['name']]
394
    AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
395

    
396
  finally:
397
    print qa_utils.FormatInfo("Activating disks again")
398
    cmds = []
399
    for name in halted_disks:
400
      cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
401
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
402
                         '; '.join(cmds)).wait(), 0)
403

    
404
  if onmaster:
405
    for name in drbddevs:
406
      cmd = ['drbdsetup', name, 'detach']
407
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
408
  else:
409
    for name in drbddevs:
410
      cmd = ['drbdsetup', name, 'disconnect']
411
      AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0)
412

    
413
  # TODO
414
  #cmd = ['vgs']
415
  #AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
416
  #                     sq(cmd)).wait(), 0)
417

    
418
  print qa_utils.FormatInfo("Making sure disks are up again")
419
  cmd = ['gnt-instance', 'replace-disks', instance['name']]
420
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
421

    
422
  print qa_utils.FormatInfo("Restarting instance")
423
  cmd = ['gnt-instance', 'shutdown', instance['name']]
424
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
425

    
426
  cmd = ['gnt-instance', 'startup', instance['name']]
427
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
428

    
429
  cmd = ['gnt-cluster', 'verify']
430
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
431

    
432

    
433
def TestInstanceMasterDiskFailure(instance, node, node2):
434
  """Testing disk failure on master node."""
435
  print qa_utils.FormatError("Disk failure on primary node cannot be"
436
                             " tested due to potential crashes.")
437
  # The following can cause crashes, thus it's disabled until fixed
438
  #return _TestInstanceDiskFailure(instance, node, node2, True)
439

    
440

    
441
def TestInstanceSecondaryDiskFailure(instance, node, node2):
442
  """Testing disk failure on secondary node."""
443
  return _TestInstanceDiskFailure(instance, node, node2, False)