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