4 # Copyright (C) 2007 Google Inc.
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.
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.
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
22 """Instance related QA tests.
29 from ganeti import utils
30 from ganeti import constants
36 from qa_utils import AssertEqual, AssertNotEqual, StartSSH
39 def _GetDiskStatePath(disk):
40 return "/sys/block/%s/device/state" % disk
43 def _GetGenericAddParameters():
44 params = ['-B', '%s=%s' % (constants.BE_MEMORY, qa_config.get('mem'))]
45 for idx, size in enumerate(qa_config.get('disk')):
46 params.extend(["--disk", "%s:size=%s" % (idx, size)])
50 def _DiskTest(node, disk_template):
51 master = qa_config.GetMasterNode()
53 instance = qa_config.AcquireInstance()
55 cmd = (['gnt-instance', 'add',
56 '--os-type=%s' % qa_config.get('os'),
57 '--disk-template=%s' % disk_template,
59 _GetGenericAddParameters())
60 cmd.append(instance['name'])
62 AssertEqual(StartSSH(master['primary'],
63 utils.ShellQuoteArgs(cmd)).wait(), 0)
66 qa_config.ReleaseInstance(instance)
70 def TestInstanceAddWithPlainDisk(node):
71 """gnt-instance add -t plain"""
72 return _DiskTest(node['primary'], 'plain')
75 def TestInstanceAddWithDrbdDisk(node, node2):
76 """gnt-instance add -t drbd"""
77 return _DiskTest("%s:%s" % (node['primary'], node2['primary']),
81 def TestInstanceRemove(instance):
82 """gnt-instance remove"""
83 master = qa_config.GetMasterNode()
85 cmd = ['gnt-instance', 'remove', '-f', instance['name']]
86 AssertEqual(StartSSH(master['primary'],
87 utils.ShellQuoteArgs(cmd)).wait(), 0)
89 qa_config.ReleaseInstance(instance)
92 def TestInstanceStartup(instance):
93 """gnt-instance startup"""
94 master = qa_config.GetMasterNode()
96 cmd = ['gnt-instance', 'startup', instance['name']]
97 AssertEqual(StartSSH(master['primary'],
98 utils.ShellQuoteArgs(cmd)).wait(), 0)
101 def TestInstanceShutdown(instance):
102 """gnt-instance shutdown"""
103 master = qa_config.GetMasterNode()
105 cmd = ['gnt-instance', 'shutdown', instance['name']]
106 AssertEqual(StartSSH(master['primary'],
107 utils.ShellQuoteArgs(cmd)).wait(), 0)
110 def TestInstanceReboot(instance):
111 """gnt-instance reboot"""
112 master = qa_config.GetMasterNode()
114 for reboottype in ["soft", "hard", "full"]:
115 cmd = ['gnt-instance', 'reboot', '--type=%s' % reboottype,
117 AssertEqual(StartSSH(master['primary'],
118 utils.ShellQuoteArgs(cmd)).wait(), 0)
121 def TestInstanceReinstall(instance):
122 """gnt-instance reinstall"""
123 master = qa_config.GetMasterNode()
125 cmd = ['gnt-instance', 'reinstall', '-f', instance['name']]
126 AssertEqual(StartSSH(master['primary'],
127 utils.ShellQuoteArgs(cmd)).wait(), 0)
130 def TestInstanceFailover(instance):
131 """gnt-instance failover"""
132 master = qa_config.GetMasterNode()
134 cmd = ['gnt-instance', 'failover', '--force', instance['name']]
135 AssertEqual(StartSSH(master['primary'],
136 utils.ShellQuoteArgs(cmd)).wait(), 0)
139 cmd = ['gnt-instance', 'failover', '--force', instance['name']]
140 AssertEqual(StartSSH(master['primary'],
141 utils.ShellQuoteArgs(cmd)).wait(), 0)
144 def TestInstanceInfo(instance):
145 """gnt-instance info"""
146 master = qa_config.GetMasterNode()
148 cmd = ['gnt-instance', 'info', instance['name']]
149 AssertEqual(StartSSH(master['primary'],
150 utils.ShellQuoteArgs(cmd)).wait(), 0)
153 def TestInstanceModify(instance):
154 """gnt-instance modify"""
155 master = qa_config.GetMasterNode()
157 # Assume /sbin/init exists on all systems
158 test_kernel = "/sbin/init"
159 test_initrd = test_kernel
161 orig_memory = qa_config.get('mem')
162 orig_bridge = qa_config.get('bridge', 'xen-br0')
164 ["-B", "%s=128" % constants.BE_MEMORY],
165 ["-B", "%s=%s" % (constants.BE_MEMORY, orig_memory)],
166 ["-B", "%s=2" % constants.BE_VCPUS],
167 ["-B", "%s=1" % constants.BE_VCPUS],
168 ["-B", "%s=%s" % (constants.BE_VCPUS, constants.VALUE_DEFAULT)],
170 ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, test_kernel)],
171 ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, constants.VALUE_DEFAULT)],
172 ["-H", "%s=%s" % (constants.HV_INITRD_PATH, test_initrd)],
173 ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_NONE)],
174 ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_DEFAULT)],
177 #["--bridge", "xen-br1"],
178 #["--bridge", orig_bridge],
180 # TODO: Do these tests only with xen-hvm
181 #["-H", "%s=acn" % constants.HV_BOOT_ORDER],
182 #["-H", "%s=%s" % (constants.HV_BOOT_ORDER, constants.VALUE_DEFAULT)],
185 cmd = ['gnt-instance', 'modify'] + alist + [instance['name']]
186 AssertEqual(StartSSH(master['primary'],
187 utils.ShellQuoteArgs(cmd)).wait(), 0)
190 cmd = ['gnt-instance', 'modify', instance['name']]
191 AssertNotEqual(StartSSH(master['primary'],
192 utils.ShellQuoteArgs(cmd)).wait(), 0)
195 def TestInstanceList():
196 """gnt-instance list"""
197 master = qa_config.GetMasterNode()
199 cmd = ['gnt-instance', 'list']
200 AssertEqual(StartSSH(master['primary'],
201 utils.ShellQuoteArgs(cmd)).wait(), 0)
204 def TestInstanceConsole(instance):
205 """gnt-instance console"""
206 master = qa_config.GetMasterNode()
208 cmd = ['gnt-instance', 'console', '--show-cmd', instance['name']]
209 AssertEqual(StartSSH(master['primary'],
210 utils.ShellQuoteArgs(cmd)).wait(), 0)
213 def TestReplaceDisks(instance, pnode, snode, othernode):
214 """gnt-instance replace-disks"""
215 master = qa_config.GetMasterNode()
218 cmd = ['gnt-instance', 'replace-disks']
220 cmd.append(instance["name"])
223 cmd = buildcmd(["-p"])
224 AssertEqual(StartSSH(master['primary'],
225 utils.ShellQuoteArgs(cmd)).wait(), 0)
227 cmd = buildcmd(["-s"])
228 AssertEqual(StartSSH(master['primary'],
229 utils.ShellQuoteArgs(cmd)).wait(), 0)
231 cmd = buildcmd(["--new-secondary=%s" % othernode["primary"]])
232 AssertEqual(StartSSH(master['primary'],
233 utils.ShellQuoteArgs(cmd)).wait(), 0)
236 cmd = buildcmd(["--new-secondary=%s" % snode["primary"]])
237 AssertEqual(StartSSH(master['primary'],
238 utils.ShellQuoteArgs(cmd)).wait(), 0)
241 def TestInstanceExport(instance, node):
242 """gnt-backup export"""
243 master = qa_config.GetMasterNode()
245 cmd = ['gnt-backup', 'export', '-n', node['primary'], instance['name']]
246 AssertEqual(StartSSH(master['primary'],
247 utils.ShellQuoteArgs(cmd)).wait(), 0)
249 return qa_utils.ResolveInstanceName(instance)
252 def TestInstanceImport(node, newinst, expnode, name):
253 """gnt-backup import"""
254 master = qa_config.GetMasterNode()
256 cmd = (['gnt-backup', 'import',
257 '--disk-template=plain',
259 '--src-node=%s' % expnode['primary'],
260 '--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
261 '--node=%s' % node['primary']] +
262 _GetGenericAddParameters())
263 cmd.append(newinst['name'])
264 AssertEqual(StartSSH(master['primary'],
265 utils.ShellQuoteArgs(cmd)).wait(), 0)
268 def TestBackupList(expnode):
269 """gnt-backup list"""
270 master = qa_config.GetMasterNode()
272 cmd = ['gnt-backup', 'list', '--node=%s' % expnode['primary']]
273 AssertEqual(StartSSH(master['primary'],
274 utils.ShellQuoteArgs(cmd)).wait(), 0)
277 def _TestInstanceDiskFailure(instance, node, node2, onmaster):
278 """Testing disk failure."""
279 master = qa_config.GetMasterNode()
280 sq = utils.ShellQuoteArgs
282 instance_full = qa_utils.ResolveInstanceName(instance)
283 node_full = qa_utils.ResolveNodeName(node)
284 node2_full = qa_utils.ResolveNodeName(node2)
286 print qa_utils.FormatInfo("Getting physical disk names")
287 cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers',
288 '--output=node,phys,instance',
289 node['primary'], node2['primary']]
290 output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
292 # Get physical disk names
293 re_disk = re.compile(r'^/dev/([a-z]+)\d+$')
295 for line in output.splitlines():
296 (node_name, phys, inst) = line.split('|')
297 if inst == instance_full:
298 if node_name not in node2disk:
299 node2disk[node_name] = []
301 m = re_disk.match(phys)
303 raise qa_error.Error("Unknown disk name format: %s" % disk)
306 if name not in node2disk[node_name]:
307 node2disk[node_name].append(name)
309 if [node2_full, node_full][int(onmaster)] not in node2disk:
310 raise qa_error.Error("Couldn't find physical disks used on"
311 " %s node" % ["secondary", "master"][int(onmaster)])
313 print qa_utils.FormatInfo("Checking whether nodes have ability to stop"
315 for node_name, disks in node2disk.iteritems():
318 cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
319 AssertEqual(StartSSH(node_name, ' && '.join(cmds)).wait(), 0)
321 print qa_utils.FormatInfo("Getting device paths")
322 cmd = ['gnt-instance', 'activate-disks', instance['name']]
323 output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
325 for line in output.splitlines():
326 (_, _, tmpdevpath) = line.split(':')
327 devpath.append(tmpdevpath)
330 print qa_utils.FormatInfo("Getting drbd device paths")
331 cmd = ['gnt-instance', 'info', instance['name']]
332 output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
333 pattern = (r'\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$'
334 r'\s+primary:\s+(/dev/drbd\d+)\s+')
335 drbddevs = re.findall(pattern, output, re.M)
340 print qa_utils.FormatInfo("Deactivating disks")
342 for name in node2disk[[node2_full, node_full][int(onmaster)]]:
343 halted_disks.append(name)
344 cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
345 AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
346 ' && '.join(cmds)).wait(), 0)
348 print qa_utils.FormatInfo("Write to disks and give some time to notice"
349 " to notice the problem")
352 cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
353 "if=%s" % disk, "of=%s" % disk]))
355 AssertEqual(StartSSH(node['primary'], ' && '.join(cmds)).wait(), 0)
358 print qa_utils.FormatInfo("Debugging info")
359 for name in drbddevs:
360 cmd = ['drbdsetup', name, 'show']
361 AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
363 cmd = ['gnt-instance', 'info', instance['name']]
364 AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
367 print qa_utils.FormatInfo("Activating disks again")
369 for name in halted_disks:
370 cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
371 AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
372 '; '.join(cmds)).wait(), 0)
375 for name in drbddevs:
376 cmd = ['drbdsetup', name, 'detach']
377 AssertEqual(StartSSH(node['primary'], sq(cmd)).wait(), 0)
379 for name in drbddevs:
380 cmd = ['drbdsetup', name, 'disconnect']
381 AssertEqual(StartSSH(node2['primary'], sq(cmd)).wait(), 0)
385 #AssertEqual(StartSSH([node2, node][int(onmaster)]['primary'],
386 # sq(cmd)).wait(), 0)
388 print qa_utils.FormatInfo("Making sure disks are up again")
389 cmd = ['gnt-instance', 'replace-disks', instance['name']]
390 AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
392 print qa_utils.FormatInfo("Restarting instance")
393 cmd = ['gnt-instance', 'shutdown', instance['name']]
394 AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
396 cmd = ['gnt-instance', 'startup', instance['name']]
397 AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
399 cmd = ['gnt-cluster', 'verify']
400 AssertEqual(StartSSH(master['primary'], sq(cmd)).wait(), 0)
403 def TestInstanceMasterDiskFailure(instance, node, node2):
404 """Testing disk failure on master node."""
405 print qa_utils.FormatError("Disk failure on primary node cannot be"
406 " tested due to potential crashes.")
407 # The following can cause crashes, thus it's disabled until fixed
408 #return _TestInstanceDiskFailure(instance, node, node2, True)
411 def TestInstanceSecondaryDiskFailure(instance, node, node2):
412 """Testing disk failure on secondary node."""
413 return _TestInstanceDiskFailure(instance, node, node2, False)