root / qa / qa_instance.py @ 7910e7a5
History | View | Annotate | Download (12.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 |
return ['--os-size=%s' % qa_config.get('os-size'), |
45 |
'--swap-size=%s' % qa_config.get('swap-size'), |
46 |
'--memory=%s' % qa_config.get('mem')] |
47 |
|
48 |
|
49 |
def _DiskTest(node, disk_template): |
50 |
master = qa_config.GetMasterNode() |
51 |
|
52 |
instance = qa_config.AcquireInstance() |
53 |
try:
|
54 |
cmd = (['gnt-instance', 'add', |
55 |
'--os-type=%s' % qa_config.get('os'), |
56 |
'--disk-template=%s' % disk_template,
|
57 |
'--node=%s' % node] +
|
58 |
_GetGenericAddParameters()) |
59 |
cmd.append(instance['name'])
|
60 |
|
61 |
AssertEqual(StartSSH(master['primary'],
|
62 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
63 |
return instance
|
64 |
except:
|
65 |
qa_config.ReleaseInstance(instance) |
66 |
raise
|
67 |
|
68 |
|
69 |
@qa_utils.DefineHook('instance-add-plain-disk') |
70 |
def TestInstanceAddWithPlainDisk(node): |
71 |
"""gnt-instance add -t plain"""
|
72 |
return _DiskTest(node['primary'], 'plain') |
73 |
|
74 |
|
75 |
@qa_utils.DefineHook('instance-add-drbd-disk') |
76 |
def TestInstanceAddWithDrbdDisk(node, node2): |
77 |
"""gnt-instance add -t drbd"""
|
78 |
return _DiskTest("%s:%s" % (node['primary'], node2['primary']), |
79 |
'drbd')
|
80 |
|
81 |
|
82 |
@qa_utils.DefineHook('instance-remove') |
83 |
def TestInstanceRemove(instance): |
84 |
"""gnt-instance remove"""
|
85 |
master = qa_config.GetMasterNode() |
86 |
|
87 |
cmd = ['gnt-instance', 'remove', '-f', instance['name']] |
88 |
AssertEqual(StartSSH(master['primary'],
|
89 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
90 |
|
91 |
qa_config.ReleaseInstance(instance) |
92 |
|
93 |
|
94 |
@qa_utils.DefineHook('instance-startup') |
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 |
@qa_utils.DefineHook('instance-shutdown') |
105 |
def TestInstanceShutdown(instance): |
106 |
"""gnt-instance shutdown"""
|
107 |
master = qa_config.GetMasterNode() |
108 |
|
109 |
cmd = ['gnt-instance', 'shutdown', instance['name']] |
110 |
AssertEqual(StartSSH(master['primary'],
|
111 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
112 |
|
113 |
|
114 |
@qa_utils.DefineHook('instance-reinstall') |
115 |
def TestInstanceReinstall(instance): |
116 |
"""gnt-instance reinstall"""
|
117 |
master = qa_config.GetMasterNode() |
118 |
|
119 |
cmd = ['gnt-instance', 'reinstall', '-f', instance['name']] |
120 |
AssertEqual(StartSSH(master['primary'],
|
121 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
122 |
|
123 |
|
124 |
@qa_utils.DefineHook('instance-failover') |
125 |
def TestInstanceFailover(instance): |
126 |
"""gnt-instance failover"""
|
127 |
master = qa_config.GetMasterNode() |
128 |
|
129 |
cmd = ['gnt-instance', 'failover', '--force', instance['name']] |
130 |
AssertEqual(StartSSH(master['primary'],
|
131 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
132 |
|
133 |
|
134 |
@qa_utils.DefineHook('instance-info') |
135 |
def TestInstanceInfo(instance): |
136 |
"""gnt-instance info"""
|
137 |
master = qa_config.GetMasterNode() |
138 |
|
139 |
cmd = ['gnt-instance', 'info', instance['name']] |
140 |
AssertEqual(StartSSH(master['primary'],
|
141 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
142 |
|
143 |
|
144 |
@qa_utils.DefineHook('instance-modify') |
145 |
def TestInstanceModify(instance): |
146 |
"""gnt-instance modify"""
|
147 |
master = qa_config.GetMasterNode() |
148 |
|
149 |
orig_memory = qa_config.get('mem')
|
150 |
orig_bridge = qa_config.get('bridge', 'xen-br0') |
151 |
args = [ |
152 |
["--memory", "128"], |
153 |
["--memory", str(orig_memory)], |
154 |
["--cpu", "2"], |
155 |
["--cpu", "1"], |
156 |
["--bridge", "xen-br1"], |
157 |
["--bridge", orig_bridge],
|
158 |
["--kernel", "/dev/null"], |
159 |
["--kernel", "default"], |
160 |
["--initrd", "/dev/null"], |
161 |
["--initrd", "none"], |
162 |
["--initrd", "default"], |
163 |
["--hvm-boot-order", "acn"], |
164 |
["--hvm-boot-order", "default"], |
165 |
] |
166 |
for alist in args: |
167 |
cmd = ['gnt-instance', 'modify'] + alist + [instance['name']] |
168 |
AssertEqual(StartSSH(master['primary'],
|
169 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
170 |
|
171 |
# check no-modify
|
172 |
cmd = ['gnt-instance', 'modify', instance['name']] |
173 |
AssertNotEqual(StartSSH(master['primary'],
|
174 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
175 |
|
176 |
|
177 |
@qa_utils.DefineHook('instance-list') |
178 |
def TestInstanceList(): |
179 |
"""gnt-instance list"""
|
180 |
master = qa_config.GetMasterNode() |
181 |
|
182 |
cmd = ['gnt-instance', 'list'] |
183 |
AssertEqual(StartSSH(master['primary'],
|
184 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
185 |
|
186 |
|
187 |
@qa_utils.DefineHook('instance-console') |
188 |
def TestInstanceConsole(instance): |
189 |
"""gnt-instance console"""
|
190 |
master = qa_config.GetMasterNode() |
191 |
|
192 |
cmd = ['gnt-instance', 'console', '--show-cmd', instance['name']] |
193 |
AssertEqual(StartSSH(master['primary'],
|
194 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
195 |
|
196 |
|
197 |
@qa_utils.DefineHook('instance-replace-disks') |
198 |
def TestReplaceDisks(instance, pnode, snode, othernode): |
199 |
"""gnt-instance replace-disks"""
|
200 |
master = qa_config.GetMasterNode() |
201 |
|
202 |
def buildcmd(args): |
203 |
cmd = ['gnt-instance', 'replace-disks'] |
204 |
cmd.extend(args) |
205 |
cmd.append(instance["name"])
|
206 |
return cmd
|
207 |
|
208 |
cmd = buildcmd(["-p"])
|
209 |
AssertEqual(StartSSH(master['primary'],
|
210 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
211 |
|
212 |
cmd = buildcmd(["-s"])
|
213 |
AssertEqual(StartSSH(master['primary'],
|
214 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
215 |
|
216 |
cmd = buildcmd(["--new-secondary=%s" % othernode["primary"]]) |
217 |
AssertEqual(StartSSH(master['primary'],
|
218 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
219 |
|
220 |
# Restore
|
221 |
cmd = buildcmd(["--new-secondary=%s" % snode["primary"]]) |
222 |
AssertEqual(StartSSH(master['primary'],
|
223 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
224 |
|
225 |
|
226 |
@qa_utils.DefineHook('backup-export') |
227 |
def TestInstanceExport(instance, node): |
228 |
"""gnt-backup export"""
|
229 |
master = qa_config.GetMasterNode() |
230 |
|
231 |
cmd = ['gnt-backup', 'export', '-n', node['primary'], instance['name']] |
232 |
AssertEqual(StartSSH(master['primary'],
|
233 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
234 |
|
235 |
return qa_utils.ResolveInstanceName(instance)
|
236 |
|
237 |
|
238 |
@qa_utils.DefineHook('backup-import') |
239 |
def TestInstanceImport(node, newinst, expnode, name): |
240 |
"""gnt-backup import"""
|
241 |
master = qa_config.GetMasterNode() |
242 |
|
243 |
cmd = (['gnt-backup', 'import', |
244 |
'--disk-template=plain',
|
245 |
'--no-ip-check',
|
246 |
'--src-node=%s' % expnode['primary'], |
247 |
'--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
|
248 |
'--node=%s' % node['primary']] + |
249 |
_GetGenericAddParameters()) |
250 |
cmd.append(newinst['name'])
|
251 |
AssertEqual(StartSSH(master['primary'],
|
252 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
253 |
|
254 |
|
255 |
@qa_utils.DefineHook('backup-list') |
256 |
def TestBackupList(expnode): |
257 |
"""gnt-backup list"""
|
258 |
master = qa_config.GetMasterNode() |
259 |
|
260 |
cmd = ['gnt-backup', 'list', '--node=%s' % expnode['primary']] |
261 |
AssertEqual(StartSSH(master['primary'],
|
262 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
263 |
|
264 |
|
265 |
def _TestInstanceDiskFailure(instance, node, node2, onmaster): |
266 |
"""Testing disk failure."""
|
267 |
master = qa_config.GetMasterNode() |
268 |
sq = utils.ShellQuoteArgs |
269 |
|
270 |
instance_full = qa_utils.ResolveInstanceName(instance) |
271 |
node_full = qa_utils.ResolveNodeName(node) |
272 |
node2_full = qa_utils.ResolveNodeName(node2) |
273 |
|
274 |
print qa_utils.FormatInfo("Getting physical disk names") |
275 |
cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers', |
276 |
'--output=node,phys,instance',
|
277 |
node['primary'], node2['primary']] |
278 |
output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
|
279 |
|
280 |
# Get physical disk names
|
281 |
re_disk = re.compile(r'^/dev/([a-z]+)\d+$')
|
282 |
node2disk = {} |
283 |
for line in output.splitlines(): |
284 |
(node_name, phys, inst) = line.split('|')
|
285 |
if inst == instance_full:
|
286 |
if node_name not in node2disk: |
287 |
node2disk[node_name] = [] |
288 |
|
289 |
m = re_disk.match(phys) |
290 |
if not m: |
291 |
raise qa_error.Error("Unknown disk name format: %s" % disk) |
292 |
|
293 |
name = m.group(1)
|
294 |
if name not in node2disk[node_name]: |
295 |
node2disk[node_name].append(name) |
296 |
|
297 |
if [node2_full, node_full][int(onmaster)] not in node2disk: |
298 |
raise qa_error.Error("Couldn't find physical disks used on" |
299 |
" %s node" % ["secondary", "master"][int(onmaster)]) |
300 |
|
301 |
print qa_utils.FormatInfo("Checking whether nodes have ability to stop" |
302 |
" disks")
|
303 |
for node_name, disks in node2disk.iteritems(): |
304 |
cmds = [] |
305 |
for disk in disks: |
306 |
cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)])) |
307 |
AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0) |
308 |
|
309 |
print qa_utils.FormatInfo("Getting device paths") |
310 |
cmd = ['gnt-instance', 'activate-disks', instance['name']] |
311 |
output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
|
312 |
devpath = [] |
313 |
for line in output.splitlines(): |
314 |
(_, _, tmpdevpath) = line.split(':')
|
315 |
devpath.append(tmpdevpath) |
316 |
print devpath
|
317 |
|
318 |
print qa_utils.FormatInfo("Getting drbd device paths") |
319 |
cmd = ['gnt-instance', 'info', instance['name']] |
320 |
output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
|
321 |
pattern = (r'\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$'
|
322 |
r'\s+primary:\s+(/dev/drbd\d+)\s+')
|
323 |
drbddevs = re.findall(pattern, output, re.M) |
324 |
print drbddevs
|
325 |
|
326 |
halted_disks = [] |
327 |
try:
|
328 |
print qa_utils.FormatInfo("Deactivating disks") |
329 |
cmds = [] |
330 |
for name in node2disk[[node2_full, node_full][int(onmaster)]]: |
331 |
halted_disks.append(name) |
332 |
cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name)) |
333 |
AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'], |
334 |
' && '.join(cmds)).wait(), 0) |
335 |
|
336 |
print qa_utils.FormatInfo("Write to disks and give some time to notice" |
337 |
" to notice the problem")
|
338 |
cmds = [] |
339 |
for disk in devpath: |
340 |
cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc", |
341 |
"if=%s" % disk, "of=%s" % disk])) |
342 |
for _ in (0, 1, 2): |
343 |
AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0) |
344 |
time.sleep(3)
|
345 |
|
346 |
print qa_utils.FormatInfo("Debugging info") |
347 |
for name in drbddevs: |
348 |
cmd = ['drbdsetup', name, 'show'] |
349 |
AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0) |
350 |
|
351 |
cmd = ['gnt-instance', 'info', instance['name']] |
352 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
353 |
|
354 |
finally:
|
355 |
print qa_utils.FormatInfo("Activating disks again") |
356 |
cmds = [] |
357 |
for name in halted_disks: |
358 |
cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name)) |
359 |
AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'], |
360 |
'; '.join(cmds)).wait(), 0) |
361 |
|
362 |
if onmaster:
|
363 |
for name in drbddevs: |
364 |
cmd = ['drbdsetup', name, 'detach'] |
365 |
AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0) |
366 |
else:
|
367 |
for name in drbddevs: |
368 |
cmd = ['drbdsetup', name, 'disconnect'] |
369 |
AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0) |
370 |
|
371 |
# TODO
|
372 |
#cmd = ['vgs']
|
373 |
#AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
|
374 |
# sq(cmd)).wait(), 0)
|
375 |
|
376 |
print qa_utils.FormatInfo("Making sure disks are up again") |
377 |
cmd = ['gnt-instance', 'replace-disks', instance['name']] |
378 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
379 |
|
380 |
print qa_utils.FormatInfo("Restarting instance") |
381 |
cmd = ['gnt-instance', 'shutdown', instance['name']] |
382 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
383 |
|
384 |
cmd = ['gnt-instance', 'startup', instance['name']] |
385 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
386 |
|
387 |
cmd = ['gnt-cluster', 'verify'] |
388 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
389 |
|
390 |
|
391 |
def TestInstanceMasterDiskFailure(instance, node, node2): |
392 |
"""Testing disk failure on master node."""
|
393 |
print qa_utils.FormatError("Disk failure on primary node cannot be" |
394 |
" tested due to potential crashes.")
|
395 |
# The following can cause crashes, thus it's disabled until fixed
|
396 |
#return _TestInstanceDiskFailure(instance, node, node2, True)
|
397 |
|
398 |
|
399 |
def TestInstanceSecondaryDiskFailure(instance, node, node2): |
400 |
"""Testing disk failure on secondary node."""
|
401 |
return _TestInstanceDiskFailure(instance, node, node2, False) |