qa_instance.py 11.8 KB
Newer Older
1
2
3
#
#

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Copyright (C) 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Instance related QA tests.

"""

26
27
28
import re
import time

29
from ganeti import utils
Michael Hanselmann's avatar
Michael Hanselmann committed
30
from ganeti import constants
31
32

import qa_config
Michael Hanselmann's avatar
Michael Hanselmann committed
33
import qa_utils
34
35
36
import qa_error

from qa_utils import AssertEqual, AssertNotEqual, StartSSH
37

38
39
40

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


Michael Hanselmann's avatar
Michael Hanselmann committed
43
44
45
46
47
48
def _GetGenericAddParameters():
  return ['--os-size=%s' % qa_config.get('os-size'),
          '--swap-size=%s' % qa_config.get('swap-size'),
          '--memory=%s' % qa_config.get('mem')]


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

  instance = qa_config.AcquireInstance()
  try:
Michael Hanselmann's avatar
Michael Hanselmann committed
54
55
    cmd = (['gnt-instance', 'add',
            '--os-type=%s' % qa_config.get('os'),
56
57
            '--disk-template=%s' % disk_template,
            '--node=%s' % node] +
Michael Hanselmann's avatar
Michael Hanselmann committed
58
           _GetGenericAddParameters())
59
60
61
62
63
64
65
66
67
68
    cmd.append(instance['name'])

    AssertEqual(StartSSH(master['primary'],
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
    return instance
  except:
    qa_config.ReleaseInstance(instance)
    raise


Michael Hanselmann's avatar
Michael Hanselmann committed
69
@qa_utils.DefineHook('instance-add-plain-disk')
70
71
def TestInstanceAddWithPlainDisk(node):
  """gnt-instance add -t plain"""
72
  return _DiskTest(node['primary'], 'plain')
73
74


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


Michael Hanselmann's avatar
Michael Hanselmann committed
82
@qa_utils.DefineHook('instance-remove')
83
84
85
86
87
88
89
90
91
92
93
def TestInstanceRemove(instance):
  """gnt-instance remove"""
  master = qa_config.GetMasterNode()

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

  qa_config.ReleaseInstance(instance)


Michael Hanselmann's avatar
Michael Hanselmann committed
94
@qa_utils.DefineHook('instance-startup')
95
96
97
98
99
100
101
102
103
def TestInstanceStartup(instance):
  """gnt-instance startup"""
  master = qa_config.GetMasterNode()

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


Michael Hanselmann's avatar
Michael Hanselmann committed
104
@qa_utils.DefineHook('instance-shutdown')
105
106
107
108
109
110
111
112
113
def TestInstanceShutdown(instance):
  """gnt-instance shutdown"""
  master = qa_config.GetMasterNode()

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


Michael Hanselmann's avatar
Michael Hanselmann committed
114
@qa_utils.DefineHook('instance-reinstall')
Michael Hanselmann's avatar
Michael Hanselmann committed
115
116
117
118
119
120
121
122
123
def TestInstanceReinstall(instance):
  """gnt-instance reinstall"""
  master = qa_config.GetMasterNode()

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


Michael Hanselmann's avatar
Michael Hanselmann committed
124
@qa_utils.DefineHook('instance-failover')
125
126
127
128
129
130
131
132
133
def TestInstanceFailover(instance):
  """gnt-instance failover"""
  master = qa_config.GetMasterNode()

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


Michael Hanselmann's avatar
Michael Hanselmann committed
134
@qa_utils.DefineHook('instance-info')
135
136
137
138
139
140
141
def TestInstanceInfo(instance):
  """gnt-instance info"""
  master = qa_config.GetMasterNode()

  cmd = ['gnt-instance', 'info', instance['name']]
  AssertEqual(StartSSH(master['primary'],
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
Michael Hanselmann's avatar
Michael Hanselmann committed
142
143


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
@qa_utils.DefineHook('instance-modify')
def TestInstanceModify(instance):
  """gnt-instance modify"""
  master = qa_config.GetMasterNode()

  orig_memory = qa_config.get('mem')
  orig_bridge = qa_config.get('bridge', 'xen-br0')
  args = [
    ["--memory", "128"],
    ["--memory", str(orig_memory)],
    ["--cpu", "2"],
    ["--cpu", "1"],
    ["--bridge", "xen-br1"],
    ["--bridge", orig_bridge],
    ["--kernel", "/dev/null"],
    ["--kernel", "default"],
    ["--initrd", "/dev/null"],
    ["--initrd", "none"],
    ["--initrd", "default"],
    ["--hvm-boot-order", "acn"],
    ["--hvm-boot-order", "default"],
    ]
  for alist in args:
    cmd = ['gnt-instance', 'modify'] + alist + [instance['name']]
    AssertEqual(StartSSH(master['primary'],
                         utils.ShellQuoteArgs(cmd)).wait(), 0)

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


Michael Hanselmann's avatar
Michael Hanselmann committed
177
@qa_utils.DefineHook('instance-list')
Michael Hanselmann's avatar
Michael Hanselmann committed
178
179
180
181
182
183
184
185
186
def TestInstanceList():
  """gnt-instance list"""
  master = qa_config.GetMasterNode()

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


187
188
189
190
191
192
193
194
195
196
@qa_utils.DefineHook('instance-console')
def TestInstanceConsole(instance):
  """gnt-instance console"""
  master = qa_config.GetMasterNode()

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


Michael Hanselmann's avatar
Michael Hanselmann committed
197
@qa_utils.DefineHook('backup-export')
Michael Hanselmann's avatar
Michael Hanselmann committed
198
199
200
201
202
203
204
205
206
207
208
def TestInstanceExport(instance, node):
  """gnt-backup export"""
  master = qa_config.GetMasterNode()

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

  return qa_utils.ResolveInstanceName(instance)


Michael Hanselmann's avatar
Michael Hanselmann committed
209
@qa_utils.DefineHook('backup-import')
Michael Hanselmann's avatar
Michael Hanselmann committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def TestInstanceImport(node, newinst, expnode, name):
  """gnt-backup import"""
  master = qa_config.GetMasterNode()

  cmd = (['gnt-backup', 'import',
          '--disk-template=plain',
          '--no-ip-check',
          '--src-node=%s' % expnode['primary'],
          '--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
          '--node=%s' % node['primary']] +
         _GetGenericAddParameters())
  cmd.append(newinst['name'])
  AssertEqual(StartSSH(master['primary'],
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
Michael Hanselmann's avatar
Michael Hanselmann committed
224
225


Michael Hanselmann's avatar
Michael Hanselmann committed
226
@qa_utils.DefineHook('backup-list')
Michael Hanselmann's avatar
Michael Hanselmann committed
227
228
229
230
def TestBackupList(expnode):
  """gnt-backup list"""
  master = qa_config.GetMasterNode()

231
  cmd = ['gnt-backup', 'list', '--node=%s' % expnode['primary']]
Michael Hanselmann's avatar
Michael Hanselmann committed
232
233
  AssertEqual(StartSSH(master['primary'],
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
234
235
236
237
238
239
240
241
242
243
244


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

  instance_full = qa_utils.ResolveInstanceName(instance)
  node_full = qa_utils.ResolveNodeName(node)
  node2_full = qa_utils.ResolveNodeName(node2)

245
  print qa_utils.FormatInfo("Getting physical disk names")
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers',
         '--output=node,phys,instance',
         node['primary'], node2['primary']]
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))

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

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

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

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

272
273
  print qa_utils.FormatInfo("Checking whether nodes have ability to stop"
                            " disks")
274
275
276
277
278
279
  for node_name, disks in node2disk.iteritems():
    cmds = []
    for disk in disks:
      cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
    AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0)

280
  print qa_utils.FormatInfo("Getting device paths")
281
282
283
284
285
286
  cmd = ['gnt-instance', 'activate-disks', instance['name']]
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
  devpath = []
  for line in output.splitlines():
    (_, _, tmpdevpath) = line.split(':')
    devpath.append(tmpdevpath)
287
  print devpath
288

289
  print qa_utils.FormatInfo("Getting drbd device paths")
290
291
  cmd = ['gnt-instance', 'info', instance['name']]
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
292
  pattern = (r'\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$'
293
294
             r'\s+primary:\s+(/dev/drbd\d+)\s+')
  drbddevs = re.findall(pattern, output, re.M)
295
  print drbddevs
296
297
298

  halted_disks = []
  try:
299
    print qa_utils.FormatInfo("Deactivating disks")
Michael Hanselmann's avatar
Michael Hanselmann committed
300
301
302
303
304
305
306
    cmds = []
    for name in node2disk[[node2_full, node_full][int(onmaster)]]:
      halted_disks.append(name)
      cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
                         ' && '.join(cmds)).wait(), 0)

307
308
    print qa_utils.FormatInfo("Write to disks and give some time to notice"
                              " to notice the problem")
309
310
311
312
313
314
315
316
    cmds = []
    for disk in devpath:
      cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
                      "if=%s" % disk, "of=%s" % disk]))
    for _ in (0, 1, 2):
      AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0)
      time.sleep(3)

317
    print qa_utils.FormatInfo("Debugging info")
Michael Hanselmann's avatar
Michael Hanselmann committed
318
319
320
321
    for name in drbddevs:
      cmd = ['drbdsetup', name, 'show']
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)

322
323
324
325
    cmd = ['gnt-instance', 'info', instance['name']]
    AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)

  finally:
326
    print qa_utils.FormatInfo("Activating disks again")
327
328
329
330
331
332
    cmds = []
    for name in halted_disks:
      cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
                         '; '.join(cmds)).wait(), 0)

Michael Hanselmann's avatar
Michael Hanselmann committed
333
334
335
336
337
338
339
340
341
  if onmaster:
    for name in drbddevs:
      cmd = ['drbdsetup', name, 'detach']
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
  else:
    for name in drbddevs:
      cmd = ['drbdsetup', name, 'disconnect']
      AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0)

342
343
344
345
  # TODO
  #cmd = ['vgs']
  #AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
  #                     sq(cmd)).wait(), 0)
Michael Hanselmann's avatar
Michael Hanselmann committed
346

347
348
349
350
351
  print qa_utils.FormatInfo("Making sure disks are up again")
  cmd = ['gnt-instance', 'replace-disks', instance['name']]
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)

  print qa_utils.FormatInfo("Restarting instance")
352
353
354
  cmd = ['gnt-instance', 'shutdown', instance['name']]
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)

Michael Hanselmann's avatar
Michael Hanselmann committed
355
  cmd = ['gnt-instance', 'startup', instance['name']]
356
357
358
359
360
361
362
363
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)

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


def TestInstanceMasterDiskFailure(instance, node, node2):
  """Testing disk failure on master node."""
364
365
  print qa_utils.FormatError("Disk failure on primary node cannot be"
                             " tested due to potential crashes.")
366
  # The following can cause crashes, thus it's disabled until fixed
367
  #return _TestInstanceDiskFailure(instance, node, node2, True)
368
369
370
371
372


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