root / qa / qa_instance.py @ 973d7867
History | View | Annotate | Download (10.5 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 |
@qa_utils.DefineHook('instance-add-plain-disk') |
67 |
def TestInstanceAddWithPlainDisk(node): |
68 |
"""gnt-instance add -t plain"""
|
69 |
return _DiskTest(node['primary'], 'plain') |
70 |
|
71 |
|
72 |
@qa_utils.DefineHook('instance-add-local-mirror-disk') |
73 |
def TestInstanceAddWithLocalMirrorDisk(node): |
74 |
"""gnt-instance add -t local_raid1"""
|
75 |
return _DiskTest(node['primary'], 'local_raid1') |
76 |
|
77 |
|
78 |
@qa_utils.DefineHook('instance-add-remote-raid-disk') |
79 |
def TestInstanceAddWithRemoteRaidDisk(node, node2): |
80 |
"""gnt-instance add -t remote_raid1"""
|
81 |
return _DiskTest("%s:%s" % (node['primary'], node2['primary']), |
82 |
'remote_raid1')
|
83 |
|
84 |
|
85 |
@qa_utils.DefineHook('instance-add-drbd-disk') |
86 |
def TestInstanceAddWithDrbdDisk(node, node2): |
87 |
"""gnt-instance add -t drbd"""
|
88 |
return _DiskTest("%s:%s" % (node['primary'], node2['primary']), |
89 |
'drbd')
|
90 |
|
91 |
|
92 |
@qa_utils.DefineHook('instance-remove') |
93 |
def TestInstanceRemove(instance): |
94 |
"""gnt-instance remove"""
|
95 |
master = qa_config.GetMasterNode() |
96 |
|
97 |
cmd = ['gnt-instance', 'remove', '-f', instance['name']] |
98 |
AssertEqual(StartSSH(master['primary'],
|
99 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
100 |
|
101 |
qa_config.ReleaseInstance(instance) |
102 |
|
103 |
|
104 |
@qa_utils.DefineHook('instance-startup') |
105 |
def TestInstanceStartup(instance): |
106 |
"""gnt-instance startup"""
|
107 |
master = qa_config.GetMasterNode() |
108 |
|
109 |
cmd = ['gnt-instance', 'startup', instance['name']] |
110 |
AssertEqual(StartSSH(master['primary'],
|
111 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
112 |
|
113 |
|
114 |
@qa_utils.DefineHook('instance-shutdown') |
115 |
def TestInstanceShutdown(instance): |
116 |
"""gnt-instance shutdown"""
|
117 |
master = qa_config.GetMasterNode() |
118 |
|
119 |
cmd = ['gnt-instance', 'shutdown', instance['name']] |
120 |
AssertEqual(StartSSH(master['primary'],
|
121 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
122 |
|
123 |
|
124 |
@qa_utils.DefineHook('instance-reinstall') |
125 |
def TestInstanceReinstall(instance): |
126 |
"""gnt-instance reinstall"""
|
127 |
master = qa_config.GetMasterNode() |
128 |
|
129 |
cmd = ['gnt-instance', 'reinstall', '-f', instance['name']] |
130 |
AssertEqual(StartSSH(master['primary'],
|
131 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
132 |
|
133 |
|
134 |
@qa_utils.DefineHook('instance-failover') |
135 |
def TestInstanceFailover(instance): |
136 |
"""gnt-instance failover"""
|
137 |
master = qa_config.GetMasterNode() |
138 |
|
139 |
cmd = ['gnt-instance', 'failover', '--force', instance['name']] |
140 |
AssertEqual(StartSSH(master['primary'],
|
141 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
142 |
|
143 |
|
144 |
@qa_utils.DefineHook('instance-info') |
145 |
def TestInstanceInfo(instance): |
146 |
"""gnt-instance info"""
|
147 |
master = qa_config.GetMasterNode() |
148 |
|
149 |
cmd = ['gnt-instance', 'info', instance['name']] |
150 |
AssertEqual(StartSSH(master['primary'],
|
151 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
152 |
|
153 |
|
154 |
@qa_utils.DefineHook('instance-list') |
155 |
def TestInstanceList(): |
156 |
"""gnt-instance list"""
|
157 |
master = qa_config.GetMasterNode() |
158 |
|
159 |
cmd = ['gnt-instance', 'list'] |
160 |
AssertEqual(StartSSH(master['primary'],
|
161 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
162 |
|
163 |
|
164 |
@qa_utils.DefineHook('backup-export') |
165 |
def TestInstanceExport(instance, node): |
166 |
"""gnt-backup export"""
|
167 |
master = qa_config.GetMasterNode() |
168 |
|
169 |
cmd = ['gnt-backup', 'export', '-n', node['primary'], instance['name']] |
170 |
AssertEqual(StartSSH(master['primary'],
|
171 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
172 |
|
173 |
return qa_utils.ResolveInstanceName(instance)
|
174 |
|
175 |
|
176 |
@qa_utils.DefineHook('backup-import') |
177 |
def TestInstanceImport(node, newinst, expnode, name): |
178 |
"""gnt-backup import"""
|
179 |
master = qa_config.GetMasterNode() |
180 |
|
181 |
cmd = (['gnt-backup', 'import', |
182 |
'--disk-template=plain',
|
183 |
'--no-ip-check',
|
184 |
'--src-node=%s' % expnode['primary'], |
185 |
'--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
|
186 |
'--node=%s' % node['primary']] + |
187 |
_GetGenericAddParameters()) |
188 |
cmd.append(newinst['name'])
|
189 |
AssertEqual(StartSSH(master['primary'],
|
190 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
191 |
|
192 |
|
193 |
@qa_utils.DefineHook('backup-list') |
194 |
def TestBackupList(expnode): |
195 |
"""gnt-backup list"""
|
196 |
master = qa_config.GetMasterNode() |
197 |
|
198 |
cmd = ['gnt-backup', 'list', '--node=%s' % expnode['primary']] |
199 |
AssertEqual(StartSSH(master['primary'],
|
200 |
utils.ShellQuoteArgs(cmd)).wait(), 0)
|
201 |
|
202 |
|
203 |
def _TestInstanceDiskFailure(instance, node, node2, onmaster): |
204 |
"""Testing disk failure."""
|
205 |
master = qa_config.GetMasterNode() |
206 |
sq = utils.ShellQuoteArgs |
207 |
|
208 |
instance_full = qa_utils.ResolveInstanceName(instance) |
209 |
node_full = qa_utils.ResolveNodeName(node) |
210 |
node2_full = qa_utils.ResolveNodeName(node2) |
211 |
|
212 |
cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers', |
213 |
'--output=node,phys,instance',
|
214 |
node['primary'], node2['primary']] |
215 |
output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
|
216 |
|
217 |
# Get physical disk names
|
218 |
re_disk = re.compile(r'^/dev/([a-z]+)\d+$')
|
219 |
node2disk = {} |
220 |
for line in output.splitlines(): |
221 |
(node_name, phys, inst) = line.split('|')
|
222 |
if inst == instance_full:
|
223 |
if node_name not in node2disk: |
224 |
node2disk[node_name] = [] |
225 |
|
226 |
m = re_disk.match(phys) |
227 |
if not m: |
228 |
raise qa_error.Error("Unknown disk name format: %s" % disk) |
229 |
|
230 |
name = m.group(1)
|
231 |
if name not in node2disk[node_name]: |
232 |
node2disk[node_name].append(name) |
233 |
|
234 |
if [node2_full, node_full][int(onmaster)] not in node2disk: |
235 |
raise qa_error.Error("Couldn't find physical disks used on" |
236 |
" %s node" % ["secondary", "master"][int(onmaster)]) |
237 |
|
238 |
# Check whether nodes have ability to stop disks
|
239 |
for node_name, disks in node2disk.iteritems(): |
240 |
cmds = [] |
241 |
for disk in disks: |
242 |
cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)])) |
243 |
AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0) |
244 |
|
245 |
# Get device paths
|
246 |
cmd = ['gnt-instance', 'activate-disks', instance['name']] |
247 |
output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
|
248 |
devpath = [] |
249 |
for line in output.splitlines(): |
250 |
(_, _, tmpdevpath) = line.split(':')
|
251 |
devpath.append(tmpdevpath) |
252 |
|
253 |
# Get drbd device paths
|
254 |
cmd = ['gnt-instance', 'info', instance['name']] |
255 |
output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
|
256 |
pattern = (r'\s+-\s+type:\s+drbd,\s+.*$'
|
257 |
r'\s+primary:\s+(/dev/drbd\d+)\s+')
|
258 |
drbddevs = re.findall(pattern, output, re.M) |
259 |
|
260 |
halted_disks = [] |
261 |
try:
|
262 |
# Deactivate disks
|
263 |
cmds = [] |
264 |
for name in node2disk[[node2_full, node_full][int(onmaster)]]: |
265 |
halted_disks.append(name) |
266 |
cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name)) |
267 |
AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'], |
268 |
' && '.join(cmds)).wait(), 0) |
269 |
|
270 |
# Write something to the disks and give some time to notice the problem
|
271 |
cmds = [] |
272 |
for disk in devpath: |
273 |
cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc", |
274 |
"if=%s" % disk, "of=%s" % disk])) |
275 |
for _ in (0, 1, 2): |
276 |
AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0) |
277 |
time.sleep(3)
|
278 |
|
279 |
for name in drbddevs: |
280 |
cmd = ['drbdsetup', name, 'show'] |
281 |
AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0) |
282 |
|
283 |
# For manual checks
|
284 |
cmd = ['gnt-instance', 'info', instance['name']] |
285 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
286 |
|
287 |
finally:
|
288 |
# Activate disks again
|
289 |
cmds = [] |
290 |
for name in halted_disks: |
291 |
cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name)) |
292 |
AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'], |
293 |
'; '.join(cmds)).wait(), 0) |
294 |
|
295 |
if onmaster:
|
296 |
for name in drbddevs: |
297 |
cmd = ['drbdsetup', name, 'detach'] |
298 |
AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0) |
299 |
else:
|
300 |
for name in drbddevs: |
301 |
cmd = ['drbdsetup', name, 'disconnect'] |
302 |
AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0) |
303 |
|
304 |
# Make sure disks are up again
|
305 |
#cmd = ['gnt-instance', 'activate-disks', instance['name']]
|
306 |
#AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
|
307 |
|
308 |
# Restart instance
|
309 |
cmd = ['gnt-instance', 'shutdown', instance['name']] |
310 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
311 |
|
312 |
#cmd = ['gnt-instance', 'startup', '--force', instance['name']]
|
313 |
cmd = ['gnt-instance', 'startup', instance['name']] |
314 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
315 |
|
316 |
cmd = ['gnt-cluster', 'verify'] |
317 |
AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0) |
318 |
|
319 |
|
320 |
def TestInstanceMasterDiskFailure(instance, node, node2): |
321 |
"""Testing disk failure on master node."""
|
322 |
print qa_utils.FormatError("Disk failure on primary node cannot be" |
323 |
" tested due to potential crashes.")
|
324 |
# The following can cause crashes, thus it's disabled until fixed
|
325 |
#return _TestInstanceDiskFailure(instance, node, node2, True)
|
326 |
|
327 |
|
328 |
def TestInstanceSecondaryDiskFailure(instance, node, node2): |
329 |
"""Testing disk failure on secondary node."""
|
330 |
return _TestInstanceDiskFailure(instance, node, node2, False) |