]>
Commit | Line | Data |
---|---|---|
f345cfd0 SH |
1 | # Common utilities and Python wrappers for qemu-iotests |
2 | # | |
3 | # Copyright (C) 2012 IBM Corp. | |
4 | # | |
5 | # This program is free software; you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation; either version 2 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | # | |
18 | ||
c1c71e49 | 19 | import errno |
f345cfd0 SH |
20 | import os |
21 | import re | |
22 | import subprocess | |
4f450568 | 23 | import string |
f345cfd0 | 24 | import unittest |
ed338bb0 FZ |
25 | import sys |
26 | sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) | |
ed338bb0 | 27 | import qtest |
2499a096 | 28 | import struct |
74f69050 | 29 | import json |
f345cfd0 | 30 | |
f345cfd0 | 31 | |
934659c4 | 32 | # This will not work if arguments contain spaces but is necessary if we |
f345cfd0 | 33 | # want to support the override options that ./check supports. |
934659c4 HR |
34 | qemu_img_args = [os.environ.get('QEMU_IMG_PROG', 'qemu-img')] |
35 | if os.environ.get('QEMU_IMG_OPTIONS'): | |
36 | qemu_img_args += os.environ['QEMU_IMG_OPTIONS'].strip().split(' ') | |
37 | ||
38 | qemu_io_args = [os.environ.get('QEMU_IO_PROG', 'qemu-io')] | |
39 | if os.environ.get('QEMU_IO_OPTIONS'): | |
40 | qemu_io_args += os.environ['QEMU_IO_OPTIONS'].strip().split(' ') | |
41 | ||
4c44b4a4 | 42 | qemu_prog = os.environ.get('QEMU_PROG', 'qemu') |
66613974 | 43 | qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ') |
f345cfd0 SH |
44 | |
45 | imgfmt = os.environ.get('IMGFMT', 'raw') | |
46 | imgproto = os.environ.get('IMGPROTO', 'file') | |
5a8fabf3 | 47 | test_dir = os.environ.get('TEST_DIR') |
e8f8624d | 48 | output_dir = os.environ.get('OUTPUT_DIR', '.') |
58cc2ae1 | 49 | cachemode = os.environ.get('CACHEMODE') |
e166b414 | 50 | qemu_default_machine = os.environ.get('QEMU_DEFAULT_MACHINE') |
f345cfd0 | 51 | |
30b005d9 | 52 | socket_scm_helper = os.environ.get('SOCKET_SCM_HELPER', 'socket_scm_helper') |
c0088d79 | 53 | debug = False |
30b005d9 | 54 | |
f345cfd0 SH |
55 | def qemu_img(*args): |
56 | '''Run qemu-img and return the exit code''' | |
57 | devnull = open('/dev/null', 'r+') | |
2ef6093c HR |
58 | exitcode = subprocess.call(qemu_img_args + list(args), stdin=devnull, stdout=devnull) |
59 | if exitcode < 0: | |
60 | sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args)))) | |
61 | return exitcode | |
f345cfd0 | 62 | |
d2ef210c | 63 | def qemu_img_verbose(*args): |
993d46ce | 64 | '''Run qemu-img without suppressing its output and return the exit code''' |
2ef6093c HR |
65 | exitcode = subprocess.call(qemu_img_args + list(args)) |
66 | if exitcode < 0: | |
67 | sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args)))) | |
68 | return exitcode | |
d2ef210c | 69 | |
3677e6f6 HR |
70 | def qemu_img_pipe(*args): |
71 | '''Run qemu-img and return its output''' | |
491e5e85 DB |
72 | subp = subprocess.Popen(qemu_img_args + list(args), |
73 | stdout=subprocess.PIPE, | |
74 | stderr=subprocess.STDOUT) | |
2ef6093c HR |
75 | exitcode = subp.wait() |
76 | if exitcode < 0: | |
77 | sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args)))) | |
78 | return subp.communicate()[0] | |
3677e6f6 | 79 | |
f345cfd0 SH |
80 | def qemu_io(*args): |
81 | '''Run qemu-io and return the stdout data''' | |
82 | args = qemu_io_args + list(args) | |
491e5e85 DB |
83 | subp = subprocess.Popen(args, stdout=subprocess.PIPE, |
84 | stderr=subprocess.STDOUT) | |
2ef6093c HR |
85 | exitcode = subp.wait() |
86 | if exitcode < 0: | |
87 | sys.stderr.write('qemu-io received signal %i: %s\n' % (-exitcode, ' '.join(args))) | |
88 | return subp.communicate()[0] | |
f345cfd0 | 89 | |
e1b5c51f | 90 | def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt): |
3a3918c3 | 91 | '''Return True if two image files are identical''' |
e1b5c51f PB |
92 | return qemu_img('compare', '-f', fmt1, |
93 | '-F', fmt2, img1, img2) == 0 | |
3a3918c3 | 94 | |
2499a096 SH |
95 | def create_image(name, size): |
96 | '''Create a fully-allocated raw image with sector markers''' | |
97 | file = open(name, 'w') | |
98 | i = 0 | |
99 | while i < size: | |
100 | sector = struct.pack('>l504xl', i / 512, i / 512) | |
101 | file.write(sector) | |
102 | i = i + 512 | |
103 | file.close() | |
104 | ||
74f69050 FZ |
105 | def image_size(img): |
106 | '''Return image's virtual size''' | |
107 | r = qemu_img_pipe('info', '--output=json', '-f', imgfmt, img) | |
108 | return json.loads(r)['virtual-size'] | |
109 | ||
a2d1c8fd DB |
110 | test_dir_re = re.compile(r"%s" % test_dir) |
111 | def filter_test_dir(msg): | |
112 | return test_dir_re.sub("TEST_DIR", msg) | |
113 | ||
114 | win32_re = re.compile(r"\r") | |
115 | def filter_win32(msg): | |
116 | return win32_re.sub("", msg) | |
117 | ||
118 | qemu_io_re = re.compile(r"[0-9]* ops; [0-9\/:. sec]* \([0-9\/.inf]* [EPTGMKiBbytes]*\/sec and [0-9\/.inf]* ops\/sec\)") | |
119 | def filter_qemu_io(msg): | |
120 | msg = filter_win32(msg) | |
121 | return qemu_io_re.sub("X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)", msg) | |
122 | ||
123 | chown_re = re.compile(r"chown [0-9]+:[0-9]+") | |
124 | def filter_chown(msg): | |
125 | return chown_re.sub("chown UID:GID", msg) | |
126 | ||
127 | def log(msg, filters=[]): | |
128 | for flt in filters: | |
129 | msg = flt(msg) | |
130 | print msg | |
131 | ||
4c44b4a4 | 132 | class VM(qtest.QEMUQtestMachine): |
f345cfd0 SH |
133 | '''A QEMU VM''' |
134 | ||
135 | def __init__(self): | |
4c44b4a4 DB |
136 | super(VM, self).__init__(qemu_prog, qemu_opts, test_dir=test_dir, |
137 | socket_scm_helper=socket_scm_helper) | |
c0088d79 KW |
138 | if debug: |
139 | self._debug = True | |
f345cfd0 | 140 | self._num_drives = 0 |
30b005d9 | 141 | |
486b88bd KW |
142 | def add_device(self, opts): |
143 | self._args.append('-device') | |
144 | self._args.append(opts) | |
145 | return self | |
146 | ||
78b666f4 FZ |
147 | def add_drive_raw(self, opts): |
148 | self._args.append('-drive') | |
149 | self._args.append(opts) | |
150 | return self | |
151 | ||
e1b5c51f | 152 | def add_drive(self, path, opts='', interface='virtio', format=imgfmt): |
f345cfd0 | 153 | '''Add a virtio-blk drive to the VM''' |
8e492253 | 154 | options = ['if=%s' % interface, |
f345cfd0 | 155 | 'id=drive%d' % self._num_drives] |
8e492253 HR |
156 | |
157 | if path is not None: | |
158 | options.append('file=%s' % path) | |
e1b5c51f | 159 | options.append('format=%s' % format) |
fc17c259 | 160 | options.append('cache=%s' % cachemode) |
8e492253 | 161 | |
f345cfd0 SH |
162 | if opts: |
163 | options.append(opts) | |
164 | ||
165 | self._args.append('-drive') | |
166 | self._args.append(','.join(options)) | |
167 | self._num_drives += 1 | |
168 | return self | |
169 | ||
3cf53c77 FZ |
170 | def pause_drive(self, drive, event=None): |
171 | '''Pause drive r/w operations''' | |
172 | if not event: | |
173 | self.pause_drive(drive, "read_aio") | |
174 | self.pause_drive(drive, "write_aio") | |
175 | return | |
176 | self.qmp('human-monitor-command', | |
177 | command_line='qemu-io %s "break %s bp_%s"' % (drive, event, drive)) | |
178 | ||
179 | def resume_drive(self, drive): | |
180 | self.qmp('human-monitor-command', | |
181 | command_line='qemu-io %s "remove_break bp_%s"' % (drive, drive)) | |
182 | ||
e3409362 IM |
183 | def hmp_qemu_io(self, drive, cmd): |
184 | '''Write to a given drive using an HMP command''' | |
185 | return self.qmp('human-monitor-command', | |
186 | command_line='qemu-io %s "%s"' % (drive, cmd)) | |
187 | ||
7898f74e | 188 | |
f345cfd0 SH |
189 | index_re = re.compile(r'([^\[]+)\[([^\]]+)\]') |
190 | ||
191 | class QMPTestCase(unittest.TestCase): | |
192 | '''Abstract base class for QMP test cases''' | |
193 | ||
194 | def dictpath(self, d, path): | |
195 | '''Traverse a path in a nested dict''' | |
196 | for component in path.split('/'): | |
197 | m = index_re.match(component) | |
198 | if m: | |
199 | component, idx = m.groups() | |
200 | idx = int(idx) | |
201 | ||
202 | if not isinstance(d, dict) or component not in d: | |
203 | self.fail('failed path traversal for "%s" in "%s"' % (path, str(d))) | |
204 | d = d[component] | |
205 | ||
206 | if m: | |
207 | if not isinstance(d, list): | |
208 | self.fail('path component "%s" in "%s" is not a list in "%s"' % (component, path, str(d))) | |
209 | try: | |
210 | d = d[idx] | |
211 | except IndexError: | |
212 | self.fail('invalid index "%s" in path "%s" in "%s"' % (idx, path, str(d))) | |
213 | return d | |
214 | ||
90f0b711 PB |
215 | def assert_qmp_absent(self, d, path): |
216 | try: | |
217 | result = self.dictpath(d, path) | |
218 | except AssertionError: | |
219 | return | |
220 | self.fail('path "%s" has value "%s"' % (path, str(result))) | |
221 | ||
f345cfd0 SH |
222 | def assert_qmp(self, d, path, value): |
223 | '''Assert that the value for a specific path in a QMP dict matches''' | |
224 | result = self.dictpath(d, path) | |
225 | self.assertEqual(result, value, 'values not equal "%s" and "%s"' % (str(result), str(value))) | |
226 | ||
ecc1c88e SH |
227 | def assert_no_active_block_jobs(self): |
228 | result = self.vm.qmp('query-block-jobs') | |
229 | self.assert_qmp(result, 'return', []) | |
230 | ||
e71fc0ba FZ |
231 | def assert_has_block_node(self, node_name=None, file_name=None): |
232 | """Issue a query-named-block-nodes and assert node_name and/or | |
233 | file_name is present in the result""" | |
234 | def check_equal_or_none(a, b): | |
235 | return a == None or b == None or a == b | |
236 | assert node_name or file_name | |
237 | result = self.vm.qmp('query-named-block-nodes') | |
238 | for x in result["return"]: | |
239 | if check_equal_or_none(x.get("node-name"), node_name) and \ | |
240 | check_equal_or_none(x.get("file"), file_name): | |
241 | return | |
242 | self.assertTrue(False, "Cannot find %s %s in result:\n%s" % \ | |
243 | (node_name, file_name, result)) | |
244 | ||
3cf53c77 | 245 | def cancel_and_wait(self, drive='drive0', force=False, resume=False): |
2575fe16 SH |
246 | '''Cancel a block job and wait for it to finish, returning the event''' |
247 | result = self.vm.qmp('block-job-cancel', device=drive, force=force) | |
248 | self.assert_qmp(result, 'return', {}) | |
249 | ||
3cf53c77 FZ |
250 | if resume: |
251 | self.vm.resume_drive(drive) | |
252 | ||
2575fe16 SH |
253 | cancelled = False |
254 | result = None | |
255 | while not cancelled: | |
256 | for event in self.vm.get_qmp_events(wait=True): | |
257 | if event['event'] == 'BLOCK_JOB_COMPLETED' or \ | |
258 | event['event'] == 'BLOCK_JOB_CANCELLED': | |
259 | self.assert_qmp(event, 'data/device', drive) | |
260 | result = event | |
261 | cancelled = True | |
262 | ||
263 | self.assert_no_active_block_jobs() | |
264 | return result | |
265 | ||
9974ad40 | 266 | def wait_until_completed(self, drive='drive0', check_offset=True): |
0dbe8a1b SH |
267 | '''Wait for a block job to finish, returning the event''' |
268 | completed = False | |
269 | while not completed: | |
270 | for event in self.vm.get_qmp_events(wait=True): | |
271 | if event['event'] == 'BLOCK_JOB_COMPLETED': | |
272 | self.assert_qmp(event, 'data/device', drive) | |
273 | self.assert_qmp_absent(event, 'data/error') | |
9974ad40 | 274 | if check_offset: |
1d3ba15a | 275 | self.assert_qmp(event, 'data/offset', event['data']['len']) |
0dbe8a1b SH |
276 | completed = True |
277 | ||
278 | self.assert_no_active_block_jobs() | |
279 | return event | |
280 | ||
866323f3 FZ |
281 | def wait_ready(self, drive='drive0'): |
282 | '''Wait until a block job BLOCK_JOB_READY event''' | |
d7b25297 FZ |
283 | f = {'data': {'type': 'mirror', 'device': drive } } |
284 | event = self.vm.event_wait(name='BLOCK_JOB_READY', match=f) | |
866323f3 FZ |
285 | |
286 | def wait_ready_and_cancel(self, drive='drive0'): | |
287 | self.wait_ready(drive=drive) | |
288 | event = self.cancel_and_wait(drive=drive) | |
289 | self.assertEquals(event['event'], 'BLOCK_JOB_COMPLETED') | |
290 | self.assert_qmp(event, 'data/type', 'mirror') | |
291 | self.assert_qmp(event, 'data/offset', event['data']['len']) | |
292 | ||
293 | def complete_and_wait(self, drive='drive0', wait_ready=True): | |
294 | '''Complete a block job and wait for it to finish''' | |
295 | if wait_ready: | |
296 | self.wait_ready(drive=drive) | |
297 | ||
298 | result = self.vm.qmp('block-job-complete', device=drive) | |
299 | self.assert_qmp(result, 'return', {}) | |
300 | ||
301 | event = self.wait_until_completed(drive=drive) | |
302 | self.assert_qmp(event, 'data/type', 'mirror') | |
303 | ||
f345cfd0 SH |
304 | def notrun(reason): |
305 | '''Skip this test suite''' | |
306 | # Each test in qemu-iotests has a number ("seq") | |
307 | seq = os.path.basename(sys.argv[0]) | |
308 | ||
e8f8624d | 309 | open('%s/%s.notrun' % (output_dir, seq), 'wb').write(reason + '\n') |
f345cfd0 SH |
310 | print '%s not run: %s' % (seq, reason) |
311 | sys.exit(0) | |
312 | ||
c6a92369 | 313 | def verify_image_format(supported_fmts=[]): |
f345cfd0 SH |
314 | if supported_fmts and (imgfmt not in supported_fmts): |
315 | notrun('not suitable for this image format: %s' % imgfmt) | |
316 | ||
c6a92369 | 317 | def verify_platform(supported_oses=['linux']): |
79e7a019 | 318 | if True not in [sys.platform.startswith(x) for x in supported_oses]: |
bc521696 FZ |
319 | notrun('not suitable for this OS: %s' % sys.platform) |
320 | ||
3f647b51 SS |
321 | def verify_quorum(): |
322 | '''Skip test suite if quorum support is not available''' | |
323 | if 'quorum' not in qemu_img_pipe('--help'): | |
324 | notrun('quorum support missing') | |
325 | ||
c6a92369 DB |
326 | def main(supported_fmts=[], supported_oses=['linux']): |
327 | '''Run tests''' | |
328 | ||
c0088d79 KW |
329 | global debug |
330 | ||
5a8fabf3 SS |
331 | # We are using TEST_DIR and QEMU_DEFAULT_MACHINE as proxies to |
332 | # indicate that we're not being run via "check". There may be | |
333 | # other things set up by "check" that individual test cases rely | |
334 | # on. | |
335 | if test_dir is None or qemu_default_machine is None: | |
336 | sys.stderr.write('Please run this test via the "check" script\n') | |
337 | sys.exit(os.EX_USAGE) | |
338 | ||
c6a92369 DB |
339 | debug = '-d' in sys.argv |
340 | verbosity = 1 | |
341 | verify_image_format(supported_fmts) | |
342 | verify_platform(supported_oses) | |
343 | ||
f345cfd0 SH |
344 | # We need to filter out the time taken from the output so that qemu-iotest |
345 | # can reliably diff the results against master output. | |
346 | import StringIO | |
aa4f592a FZ |
347 | if debug: |
348 | output = sys.stdout | |
349 | verbosity = 2 | |
350 | sys.argv.remove('-d') | |
351 | else: | |
352 | output = StringIO.StringIO() | |
f345cfd0 SH |
353 | |
354 | class MyTestRunner(unittest.TextTestRunner): | |
aa4f592a | 355 | def __init__(self, stream=output, descriptions=True, verbosity=verbosity): |
f345cfd0 SH |
356 | unittest.TextTestRunner.__init__(self, stream, descriptions, verbosity) |
357 | ||
358 | # unittest.main() will use sys.exit() so expect a SystemExit exception | |
359 | try: | |
360 | unittest.main(testRunner=MyTestRunner) | |
361 | finally: | |
aa4f592a FZ |
362 | if not debug: |
363 | sys.stderr.write(re.sub(r'Ran (\d+) tests? in [\d.]+s', r'Ran \1 tests', output.getvalue())) |