Statistics
| Branch: | Tag: | Revision:

root / qa / qa_instance.py @ dfe11bad

History | View | Annotate | Download (9.8 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, args):
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
            '--node=%s' % node['primary']] +
54
           _GetGenericAddParameters())
55
    if args:
56
      cmd += args
57
    cmd.append(instance['name'])
58

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

    
66

    
67
def TestInstanceAddWithPlainDisk(node):
68
  """gnt-instance add -t plain"""
69
  return _DiskTest(node, ['--disk-template=plain'])
70

    
71

    
72
def TestInstanceAddWithLocalMirrorDisk(node):
73
  """gnt-instance add -t local_raid1"""
74
  return _DiskTest(node, ['--disk-template=local_raid1'])
75

    
76

    
77
def TestInstanceAddWithRemoteRaidDisk(node, node2):
78
  """gnt-instance add -t remote_raid1"""
79
  return _DiskTest(node,
80
                   ['--disk-template=remote_raid1',
81
                    '--secondary-node=%s' % node2['primary']])
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 TestInstanceReinstall(instance):
114
  """gnt-instance reinstall"""
115
  master = qa_config.GetMasterNode()
116

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

    
121

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

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

    
130

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

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

    
139

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

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

    
148

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

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

    
157
  return qa_utils.ResolveInstanceName(instance)
158

    
159

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

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

    
175

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

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

    
184

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

    
190
  instance_full = qa_utils.ResolveInstanceName(instance)
191
  node_full = qa_utils.ResolveNodeName(node)
192
  node2_full = qa_utils.ResolveNodeName(node2)
193

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
301

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

    
309

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