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