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