Statistics
| Branch: | Tag: | Revision:

root / qa / qa_instance.py @ 113b8d89

History | View | Annotate | Download (9.7 kB)

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

    
18

    
19
"""Instance related QA tests.
20

21
"""
22

    
23
import re
24
import time
25

    
26
from ganeti import utils
27
from ganeti import constants
28

    
29
import qa_config
30
import qa_utils
31
import qa_error
32

    
33
from qa_utils import AssertEqual, AssertNotEqual, StartSSH
34

    
35

    
36
def _GetDiskStatePath(disk):
37
  return "/sys/block/%s/device/state" % disk
38

    
39

    
40
def _GetGenericAddParameters():
41
  return ['--os-size=%s' % qa_config.get('os-size'),
42
          '--swap-size=%s' % qa_config.get('swap-size'),
43
          '--memory=%s' % qa_config.get('mem')]
44

    
45

    
46
def _DiskTest(node, disk_template):
47
  master = qa_config.GetMasterNode()
48

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

    
58
    AssertEqual(StartSSH(master['primary'],
59
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
60
    return instance
61
  except:
62
    qa_config.ReleaseInstance(instance)
63
    raise
64

    
65

    
66
def TestInstanceAddWithPlainDisk(node):
67
  """gnt-instance add -t plain"""
68
  return _DiskTest(node['primary'], 'plain')
69

    
70

    
71
def TestInstanceAddWithLocalMirrorDisk(node):
72
  """gnt-instance add -t local_raid1"""
73
  return _DiskTest(node['primary'], 'local_raid1')
74

    
75

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

    
81

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

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

    
90
  qa_config.ReleaseInstance(instance)
91

    
92

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

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

    
101

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

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

    
110

    
111
def TestInstanceReinstall(instance):
112
  """gnt-instance reinstall"""
113
  master = qa_config.GetMasterNode()
114

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

    
119

    
120
def TestInstanceFailover(instance):
121
  """gnt-instance failover"""
122
  master = qa_config.GetMasterNode()
123

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

    
128

    
129
def TestInstanceInfo(instance):
130
  """gnt-instance info"""
131
  master = qa_config.GetMasterNode()
132

    
133
  cmd = ['gnt-instance', 'info', instance['name']]
134
  AssertEqual(StartSSH(master['primary'],
135
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
136

    
137

    
138
def TestInstanceList():
139
  """gnt-instance list"""
140
  master = qa_config.GetMasterNode()
141

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

    
146

    
147
def TestInstanceExport(instance, node):
148
  """gnt-backup export"""
149
  master = qa_config.GetMasterNode()
150

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

    
155
  return qa_utils.ResolveInstanceName(instance)
156

    
157

    
158
def TestInstanceImport(node, newinst, expnode, name):
159
  """gnt-backup import"""
160
  master = qa_config.GetMasterNode()
161

    
162
  cmd = (['gnt-backup', 'import',
163
          '--disk-template=plain',
164
          '--no-ip-check',
165
          '--src-node=%s' % expnode['primary'],
166
          '--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
167
          '--node=%s' % node['primary']] +
168
         _GetGenericAddParameters())
169
  cmd.append(newinst['name'])
170
  AssertEqual(StartSSH(master['primary'],
171
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
172

    
173

    
174
def TestBackupList(expnode):
175
  """gnt-backup list"""
176
  master = qa_config.GetMasterNode()
177

    
178
  cmd = ['gnt-backup', 'list', '--nodes=%s' % expnode['primary']]
179
  AssertEqual(StartSSH(master['primary'],
180
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
181

    
182

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

    
188
  instance_full = qa_utils.ResolveInstanceName(instance)
189
  node_full = qa_utils.ResolveNodeName(node)
190
  node2_full = qa_utils.ResolveNodeName(node2)
191

    
192
  cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers',
193
         '--output=node,phys,instance',
194
         node['primary'], node2['primary']]
195
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
196

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

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

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

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

    
218
  # Check whether nodes have ability to stop disks
219
  for node_name, disks in node2disk.iteritems():
220
    cmds = []
221
    for disk in disks:
222
      cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
223
    AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0)
224

    
225
  # Get device paths
226
  cmd = ['gnt-instance', 'activate-disks', instance['name']]
227
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
228
  devpath = []
229
  for line in output.splitlines():
230
    (_, _, tmpdevpath) = line.split(':')
231
    devpath.append(tmpdevpath)
232

    
233
  # Get drbd device paths
234
  cmd = ['gnt-instance', 'info', instance['name']]
235
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
236
  pattern = (r'\s+-\s+type:\s+drbd,\s+.*$'
237
             r'\s+primary:\s+(/dev/drbd\d+)\s+')
238
  drbddevs = re.findall(pattern, output, re.M)
239

    
240
  halted_disks = []
241
  try:
242
    # Deactivate disks
243
    cmds = []
244
    for name in node2disk[[node2_full, node_full][int(onmaster)]]:
245
      halted_disks.append(name)
246
      cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
247
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
248
                         ' && '.join(cmds)).wait(), 0)
249

    
250
    # Write something to the disks and give some time to notice the problem
251
    cmds = []
252
    for disk in devpath:
253
      cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
254
                      "if=%s" % disk, "of=%s" % disk]))
255
    for _ in (0, 1, 2):
256
      AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0)
257
      time.sleep(3)
258

    
259
    for name in drbddevs:
260
      cmd = ['drbdsetup', name, 'show']
261
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
262

    
263
    # For manual checks
264
    cmd = ['gnt-instance', 'info', instance['name']]
265
    AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
266

    
267
  finally:
268
    # Activate disks again
269
    cmds = []
270
    for name in halted_disks:
271
      cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
272
    AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
273
                         '; '.join(cmds)).wait(), 0)
274

    
275
  if onmaster:
276
    for name in drbddevs:
277
      cmd = ['drbdsetup', name, 'detach']
278
      AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
279
  else:
280
    for name in drbddevs:
281
      cmd = ['drbdsetup', name, 'disconnect']
282
      AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0)
283

    
284
  # Make sure disks are up again
285
  #cmd = ['gnt-instance', 'activate-disks', instance['name']]
286
  #AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
287

    
288
  # Restart instance
289
  cmd = ['gnt-instance', 'shutdown', instance['name']]
290
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
291

    
292
  #cmd = ['gnt-instance', 'startup', '--force', instance['name']]
293
  cmd = ['gnt-instance', 'startup', instance['name']]
294
  AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
295

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

    
299

    
300
def TestInstanceMasterDiskFailure(instance, node, node2):
301
  """Testing disk failure on master node."""
302
  print qa_utils.FormatError("Disk failure on primary node cannot be "
303
                             "tested due to potential crashes.")
304
  # The following can cause crashes, thus it's disabled until fixed
305
  #return _TestInstanceDiskFailure(instance, node, node2, True)
306

    
307

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