1 # Common utilities and Python wrappers for qemu-iotests
3 # Copyright (C) 2012 IBM Corp.
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.
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.
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/>.
26 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), '..', '..', 'scripts'))
32 # This will not work if arguments contain spaces but is necessary if we
33 # want to support the override options that ./check supports.
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(' ')
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(' ')
42 qemu_prog
= os
.environ
.get('QEMU_PROG', 'qemu')
43 qemu_opts
= os
.environ
.get('QEMU_OPTIONS', '').strip().split(' ')
45 imgfmt
= os
.environ
.get('IMGFMT', 'raw')
46 imgproto
= os
.environ
.get('IMGPROTO', 'file')
47 test_dir
= os
.environ
.get('TEST_DIR')
48 output_dir
= os
.environ
.get('OUTPUT_DIR', '.')
49 cachemode
= os
.environ
.get('CACHEMODE')
50 qemu_default_machine
= os
.environ
.get('QEMU_DEFAULT_MACHINE')
52 socket_scm_helper
= os
.environ
.get('SOCKET_SCM_HELPER', 'socket_scm_helper')
56 '''Run qemu-img and return the exit code'''
57 devnull
= open('/dev/null', 'r+')
58 exitcode
= subprocess
.call(qemu_img_args
+ list(args
), stdin
=devnull
, stdout
=devnull
)
60 sys
.stderr
.write('qemu-img received signal %i: %s\n' % (-exitcode
, ' '.join(qemu_img_args
+ list(args
))))
63 def qemu_img_verbose(*args
):
64 '''Run qemu-img without suppressing its output and return the exit code'''
65 exitcode
= subprocess
.call(qemu_img_args
+ list(args
))
67 sys
.stderr
.write('qemu-img received signal %i: %s\n' % (-exitcode
, ' '.join(qemu_img_args
+ list(args
))))
70 def qemu_img_pipe(*args
):
71 '''Run qemu-img and return its output'''
72 subp
= subprocess
.Popen(qemu_img_args
+ list(args
),
73 stdout
=subprocess
.PIPE
,
74 stderr
=subprocess
.STDOUT
)
75 exitcode
= subp
.wait()
77 sys
.stderr
.write('qemu-img received signal %i: %s\n' % (-exitcode
, ' '.join(qemu_img_args
+ list(args
))))
78 return subp
.communicate()[0]
81 '''Run qemu-io and return the stdout data'''
82 args
= qemu_io_args
+ list(args
)
83 subp
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
,
84 stderr
=subprocess
.STDOUT
)
85 exitcode
= subp
.wait()
87 sys
.stderr
.write('qemu-io received signal %i: %s\n' % (-exitcode
, ' '.join(args
)))
88 return subp
.communicate()[0]
90 def compare_images(img1
, img2
, fmt1
=imgfmt
, fmt2
=imgfmt
):
91 '''Return True if two image files are identical'''
92 return qemu_img('compare', '-f', fmt1
,
93 '-F', fmt2
, img1
, img2
) == 0
95 def create_image(name
, size
):
96 '''Create a fully-allocated raw image with sector markers'''
97 file = open(name
, 'w')
100 sector
= struct
.pack('>l504xl', i
/ 512, i
/ 512)
106 '''Return image's virtual size'''
107 r
= qemu_img_pipe('info', '--output=json', '-f', imgfmt
, img
)
108 return json
.loads(r
)['virtual-size']
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
)
114 win32_re
= re
.compile(r
"\r")
115 def filter_win32(msg
):
116 return win32_re
.sub("", msg
)
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
)
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
)
127 def log(msg
, filters
=[]):
132 class VM(qtest
.QEMUQtestMachine
):
136 super(VM
, self
).__init
__(qemu_prog
, qemu_opts
, test_dir
=test_dir
,
137 socket_scm_helper
=socket_scm_helper
)
142 def add_drive_raw(self
, opts
):
143 self
._args
.append('-drive')
144 self
._args
.append(opts
)
147 def add_drive(self
, path
, opts
='', interface
='virtio', format
=imgfmt
):
148 '''Add a virtio-blk drive to the VM'''
149 options
= ['if=%s' % interface
,
150 'id=drive%d' % self
._num
_drives
]
153 options
.append('file=%s' % path
)
154 options
.append('format=%s' % format
)
155 options
.append('cache=%s' % cachemode
)
160 self
._args
.append('-drive')
161 self
._args
.append(','.join(options
))
162 self
._num
_drives
+= 1
165 def pause_drive(self
, drive
, event
=None):
166 '''Pause drive r/w operations'''
168 self
.pause_drive(drive
, "read_aio")
169 self
.pause_drive(drive
, "write_aio")
171 self
.qmp('human-monitor-command',
172 command_line
='qemu-io %s "break %s bp_%s"' % (drive
, event
, drive
))
174 def resume_drive(self
, drive
):
175 self
.qmp('human-monitor-command',
176 command_line
='qemu-io %s "remove_break bp_%s"' % (drive
, drive
))
178 def hmp_qemu_io(self
, drive
, cmd
):
179 '''Write to a given drive using an HMP command'''
180 return self
.qmp('human-monitor-command',
181 command_line
='qemu-io %s "%s"' % (drive
, cmd
))
184 index_re
= re
.compile(r
'([^\[]+)\[([^\]]+)\]')
186 class QMPTestCase(unittest
.TestCase
):
187 '''Abstract base class for QMP test cases'''
189 def dictpath(self
, d
, path
):
190 '''Traverse a path in a nested dict'''
191 for component
in path
.split('/'):
192 m
= index_re
.match(component
)
194 component
, idx
= m
.groups()
197 if not isinstance(d
, dict) or component
not in d
:
198 self
.fail('failed path traversal for "%s" in "%s"' % (path
, str(d
)))
202 if not isinstance(d
, list):
203 self
.fail('path component "%s" in "%s" is not a list in "%s"' % (component
, path
, str(d
)))
207 self
.fail('invalid index "%s" in path "%s" in "%s"' % (idx
, path
, str(d
)))
210 def assert_qmp_absent(self
, d
, path
):
212 result
= self
.dictpath(d
, path
)
213 except AssertionError:
215 self
.fail('path "%s" has value "%s"' % (path
, str(result
)))
217 def assert_qmp(self
, d
, path
, value
):
218 '''Assert that the value for a specific path in a QMP dict matches'''
219 result
= self
.dictpath(d
, path
)
220 self
.assertEqual(result
, value
, 'values not equal "%s" and "%s"' % (str(result
), str(value
)))
222 def assert_no_active_block_jobs(self
):
223 result
= self
.vm
.qmp('query-block-jobs')
224 self
.assert_qmp(result
, 'return', [])
226 def assert_has_block_node(self
, node_name
=None, file_name
=None):
227 """Issue a query-named-block-nodes and assert node_name and/or
228 file_name is present in the result"""
229 def check_equal_or_none(a
, b
):
230 return a
== None or b
== None or a
== b
231 assert node_name
or file_name
232 result
= self
.vm
.qmp('query-named-block-nodes')
233 for x
in result
["return"]:
234 if check_equal_or_none(x
.get("node-name"), node_name
) and \
235 check_equal_or_none(x
.get("file"), file_name
):
237 self
.assertTrue(False, "Cannot find %s %s in result:\n%s" % \
238 (node_name
, file_name
, result
))
240 def cancel_and_wait(self
, drive
='drive0', force
=False, resume
=False):
241 '''Cancel a block job and wait for it to finish, returning the event'''
242 result
= self
.vm
.qmp('block-job-cancel', device
=drive
, force
=force
)
243 self
.assert_qmp(result
, 'return', {})
246 self
.vm
.resume_drive(drive
)
251 for event
in self
.vm
.get_qmp_events(wait
=True):
252 if event
['event'] == 'BLOCK_JOB_COMPLETED' or \
253 event
['event'] == 'BLOCK_JOB_CANCELLED':
254 self
.assert_qmp(event
, 'data/device', drive
)
258 self
.assert_no_active_block_jobs()
261 def wait_until_completed(self
, drive
='drive0', check_offset
=True):
262 '''Wait for a block job to finish, returning the event'''
265 for event
in self
.vm
.get_qmp_events(wait
=True):
266 if event
['event'] == 'BLOCK_JOB_COMPLETED':
267 self
.assert_qmp(event
, 'data/device', drive
)
268 self
.assert_qmp_absent(event
, 'data/error')
270 self
.assert_qmp(event
, 'data/offset', event
['data']['len'])
273 self
.assert_no_active_block_jobs()
276 def wait_ready(self
, drive
='drive0'):
277 '''Wait until a block job BLOCK_JOB_READY event'''
278 f
= {'data': {'type': 'mirror', 'device': drive
} }
279 event
= self
.vm
.event_wait(name
='BLOCK_JOB_READY', match
=f
)
281 def wait_ready_and_cancel(self
, drive
='drive0'):
282 self
.wait_ready(drive
=drive
)
283 event
= self
.cancel_and_wait(drive
=drive
)
284 self
.assertEquals(event
['event'], 'BLOCK_JOB_COMPLETED')
285 self
.assert_qmp(event
, 'data/type', 'mirror')
286 self
.assert_qmp(event
, 'data/offset', event
['data']['len'])
288 def complete_and_wait(self
, drive
='drive0', wait_ready
=True):
289 '''Complete a block job and wait for it to finish'''
291 self
.wait_ready(drive
=drive
)
293 result
= self
.vm
.qmp('block-job-complete', device
=drive
)
294 self
.assert_qmp(result
, 'return', {})
296 event
= self
.wait_until_completed(drive
=drive
)
297 self
.assert_qmp(event
, 'data/type', 'mirror')
300 '''Skip this test suite'''
301 # Each test in qemu-iotests has a number ("seq")
302 seq
= os
.path
.basename(sys
.argv
[0])
304 open('%s/%s.notrun' % (output_dir
, seq
), 'wb').write(reason
+ '\n')
305 print '%s not run: %s' % (seq
, reason
)
308 def verify_image_format(supported_fmts
=[]):
309 if supported_fmts
and (imgfmt
not in supported_fmts
):
310 notrun('not suitable for this image format: %s' % imgfmt
)
312 def verify_platform(supported_oses
=['linux']):
313 if True not in [sys
.platform
.startswith(x
) for x
in supported_oses
]:
314 notrun('not suitable for this OS: %s' % sys
.platform
)
317 '''Skip test suite if quorum support is not available'''
318 if 'quorum' not in qemu_img_pipe('--help'):
319 notrun('quorum support missing')
321 def main(supported_fmts
=[], supported_oses
=['linux']):
326 # We are using TEST_DIR and QEMU_DEFAULT_MACHINE as proxies to
327 # indicate that we're not being run via "check". There may be
328 # other things set up by "check" that individual test cases rely
330 if test_dir
is None or qemu_default_machine
is None:
331 sys
.stderr
.write('Please run this test via the "check" script\n')
332 sys
.exit(os
.EX_USAGE
)
334 debug
= '-d' in sys
.argv
336 verify_image_format(supported_fmts
)
337 verify_platform(supported_oses
)
339 # We need to filter out the time taken from the output so that qemu-iotest
340 # can reliably diff the results against master output.
345 sys
.argv
.remove('-d')
347 output
= StringIO
.StringIO()
349 class MyTestRunner(unittest
.TextTestRunner
):
350 def __init__(self
, stream
=output
, descriptions
=True, verbosity
=verbosity
):
351 unittest
.TextTestRunner
.__init
__(self
, stream
, descriptions
, verbosity
)
353 # unittest.main() will use sys.exit() so expect a SystemExit exception
355 unittest
.main(testRunner
=MyTestRunner
)
358 sys
.stderr
.write(re
.sub(r
'Ran (\d+) tests? in [\d.]+s', r
'Ran \1 tests', output
.getvalue()))