1 from __future__
import print_function
2 # Common utilities and Python wrappers for qemu-iotests
4 # Copyright (C) 2012 IBM Corp.
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,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU 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, see <http://www.gnu.org/licenses/>.
33 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), '..', '..', 'scripts'))
37 # This will not work if arguments contain spaces but is necessary if we
38 # want to support the override options that ./check supports.
39 qemu_img_args
= [os
.environ
.get('QEMU_IMG_PROG', 'qemu-img')]
40 if os
.environ
.get('QEMU_IMG_OPTIONS'):
41 qemu_img_args
+= os
.environ
['QEMU_IMG_OPTIONS'].strip().split(' ')
43 qemu_io_args
= [os
.environ
.get('QEMU_IO_PROG', 'qemu-io')]
44 if os
.environ
.get('QEMU_IO_OPTIONS'):
45 qemu_io_args
+= os
.environ
['QEMU_IO_OPTIONS'].strip().split(' ')
47 qemu_nbd_args
= [os
.environ
.get('QEMU_NBD_PROG', 'qemu-nbd')]
48 if os
.environ
.get('QEMU_NBD_OPTIONS'):
49 qemu_nbd_args
+= os
.environ
['QEMU_NBD_OPTIONS'].strip().split(' ')
51 qemu_prog
= os
.environ
.get('QEMU_PROG', 'qemu')
52 qemu_opts
= os
.environ
.get('QEMU_OPTIONS', '').strip().split(' ')
54 imgfmt
= os
.environ
.get('IMGFMT', 'raw')
55 imgproto
= os
.environ
.get('IMGPROTO', 'file')
56 test_dir
= os
.environ
.get('TEST_DIR')
57 output_dir
= os
.environ
.get('OUTPUT_DIR', '.')
58 cachemode
= os
.environ
.get('CACHEMODE')
59 qemu_default_machine
= os
.environ
.get('QEMU_DEFAULT_MACHINE')
61 socket_scm_helper
= os
.environ
.get('SOCKET_SCM_HELPER', 'socket_scm_helper')
64 luks_default_secret_object
= 'secret,id=keysec0,data=' + \
65 os
.environ
['IMGKEYSECRET']
66 luks_default_key_secret_opt
= 'key-secret=keysec0'
70 '''Run qemu-img and return the exit code'''
71 devnull
= open('/dev/null', 'r+')
72 exitcode
= subprocess
.call(qemu_img_args
+ list(args
), stdin
=devnull
, stdout
=devnull
)
74 sys
.stderr
.write('qemu-img received signal %i: %s\n' % (-exitcode
, ' '.join(qemu_img_args
+ list(args
))))
77 def qemu_img_create(*args
):
80 # default luks support
81 if '-f' in args
and args
[args
.index('-f') + 1] == 'luks':
84 if 'key-secret' not in args
[i
+ 1]:
85 args
[i
+ 1].append(luks_default_key_secret_opt
)
86 args
.insert(i
+ 2, '--object')
87 args
.insert(i
+ 3, luks_default_secret_object
)
89 args
= ['-o', luks_default_key_secret_opt
,
90 '--object', luks_default_secret_object
] + args
92 args
.insert(0, 'create')
94 return qemu_img(*args
)
96 def qemu_img_verbose(*args
):
97 '''Run qemu-img without suppressing its output and return the exit code'''
98 exitcode
= subprocess
.call(qemu_img_args
+ list(args
))
100 sys
.stderr
.write('qemu-img received signal %i: %s\n' % (-exitcode
, ' '.join(qemu_img_args
+ list(args
))))
103 def qemu_img_pipe(*args
):
104 '''Run qemu-img and return its output'''
105 subp
= subprocess
.Popen(qemu_img_args
+ list(args
),
106 stdout
=subprocess
.PIPE
,
107 stderr
=subprocess
.STDOUT
,
108 universal_newlines
=True)
109 exitcode
= subp
.wait()
111 sys
.stderr
.write('qemu-img received signal %i: %s\n' % (-exitcode
, ' '.join(qemu_img_args
+ list(args
))))
112 return subp
.communicate()[0]
114 def img_info_log(filename
, filter_path
=None, imgopts
=False, extra_args
=[]):
117 args
.append('--image-opts')
119 args
+= [ '-f', imgfmt
]
121 args
.append(filename
)
123 output
= qemu_img_pipe(*args
)
125 filter_path
= filename
126 log(filter_img_info(output
, filter_path
))
129 '''Run qemu-io and return the stdout data'''
130 args
= qemu_io_args
+ list(args
)
131 subp
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
,
132 stderr
=subprocess
.STDOUT
,
133 universal_newlines
=True)
134 exitcode
= subp
.wait()
136 sys
.stderr
.write('qemu-io received signal %i: %s\n' % (-exitcode
, ' '.join(args
)))
137 return subp
.communicate()[0]
139 def qemu_io_silent(*args
):
140 '''Run qemu-io and return the exit code, suppressing stdout'''
141 args
= qemu_io_args
+ list(args
)
142 exitcode
= subprocess
.call(args
, stdout
=open('/dev/null', 'w'))
144 sys
.stderr
.write('qemu-io received signal %i: %s\n' %
145 (-exitcode
, ' '.join(args
)))
149 class QemuIoInteractive
:
150 def __init__(self
, *args
):
151 self
.args
= qemu_io_args
+ list(args
)
152 self
._p
= subprocess
.Popen(self
.args
, stdin
=subprocess
.PIPE
,
153 stdout
=subprocess
.PIPE
,
154 stderr
=subprocess
.STDOUT
,
155 universal_newlines
=True)
156 assert self
._p
.stdout
.read(9) == 'qemu-io> '
159 self
._p
.communicate('q\n')
161 def _read_output(self
):
162 pattern
= 'qemu-io> '
167 c
= self
._p
.stdout
.read(1)
168 # check unexpected EOF
171 if c
== pattern
[pos
]:
176 return ''.join(s
[:-n
])
179 # quit command is in close(), '\n' is added automatically
180 assert '\n' not in cmd
182 assert cmd
!= 'q' and cmd
!= 'quit'
183 self
._p
.stdin
.write(cmd
+ '\n')
184 self
._p
.stdin
.flush()
185 return self
._read
_output
()
189 '''Run qemu-nbd in daemon mode and return the parent's exit code'''
190 return subprocess
.call(qemu_nbd_args
+ ['--fork'] + list(args
))
192 def compare_images(img1
, img2
, fmt1
=imgfmt
, fmt2
=imgfmt
):
193 '''Return True if two image files are identical'''
194 return qemu_img('compare', '-f', fmt1
,
195 '-F', fmt2
, img1
, img2
) == 0
197 def create_image(name
, size
):
198 '''Create a fully-allocated raw image with sector markers'''
199 file = open(name
, 'wb')
202 sector
= struct
.pack('>l504xl', i
// 512, i
// 512)
208 '''Return image's virtual size'''
209 r
= qemu_img_pipe('info', '--output=json', '-f', imgfmt
, img
)
210 return json
.loads(r
)['virtual-size']
212 test_dir_re
= re
.compile(r
"%s" % test_dir
)
213 def filter_test_dir(msg
):
214 return test_dir_re
.sub("TEST_DIR", msg
)
216 win32_re
= re
.compile(r
"\r")
217 def filter_win32(msg
):
218 return win32_re
.sub("", msg
)
220 qemu_io_re
= re
.compile(r
"[0-9]* ops; [0-9\/:. sec]* \([0-9\/.inf]* [EPTGMKiBbytes]*\/sec and [0-9\/.inf]* ops\/sec\)")
221 def filter_qemu_io(msg
):
222 msg
= filter_win32(msg
)
223 return qemu_io_re
.sub("X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)", msg
)
225 chown_re
= re
.compile(r
"chown [0-9]+:[0-9]+")
226 def filter_chown(msg
):
227 return chown_re
.sub("chown UID:GID", msg
)
229 def filter_qmp_event(event
):
230 '''Filter a QMP event dict'''
232 if 'timestamp' in event
:
233 event
['timestamp']['seconds'] = 'SECS'
234 event
['timestamp']['microseconds'] = 'USECS'
237 def filter_testfiles(msg
):
238 prefix
= os
.path
.join(test_dir
, "%s-" % (os
.getpid()))
239 return msg
.replace(prefix
, 'TEST_DIR/PID-')
241 def filter_img_info(output
, filename
):
243 for line
in output
.split('\n'):
244 if 'disk size' in line
or 'actual-size' in line
:
246 line
= line
.replace(filename
, 'TEST_IMG') \
247 .replace(imgfmt
, 'IMGFMT')
248 line
= re
.sub('iters: [0-9]+', 'iters: XXX', line
)
249 line
= re
.sub('uuid: [-a-f0-9]+', 'uuid: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', line
)
251 return '\n'.join(lines
)
253 def log(msg
, filters
=[]):
259 def __init__(self
, seconds
, errmsg
= "Timeout"):
260 self
.seconds
= seconds
263 signal
.signal(signal
.SIGALRM
, self
.timeout
)
264 signal
.setitimer(signal
.ITIMER_REAL
, self
.seconds
)
266 def __exit__(self
, type, value
, traceback
):
267 signal
.setitimer(signal
.ITIMER_REAL
, 0)
269 def timeout(self
, signum
, frame
):
270 raise Exception(self
.errmsg
)
273 class FilePath(object):
274 '''An auto-generated filename that cleans itself up.
276 Use this context manager to generate filenames and ensure that the file
279 with TestFilePath('test.img') as img_path:
280 qemu_img('create', img_path, '1G')
281 # migration_sock_path is automatically deleted
283 def __init__(self
, name
):
284 filename
= '{0}-{1}'.format(os
.getpid(), name
)
285 self
.path
= os
.path
.join(test_dir
, filename
)
290 def __exit__(self
, exc_type
, exc_val
, exc_tb
):
298 def file_path_remover():
299 for path
in reversed(file_path_remover
.paths
):
306 def file_path(*names
):
307 ''' Another way to get auto-generated filename that cleans itself up.
311 img_a, img_b = file_path('a.img', 'b.img')
312 sock = file_path('socket')
315 if not hasattr(file_path_remover
, 'paths'):
316 file_path_remover
.paths
= []
317 atexit
.register(file_path_remover
)
321 filename
= '{0}-{1}'.format(os
.getpid(), name
)
322 path
= os
.path
.join(test_dir
, filename
)
323 file_path_remover
.paths
.append(path
)
326 return paths
[0] if len(paths
) == 1 else paths
328 def remote_filename(path
):
329 if imgproto
== 'file':
331 elif imgproto
== 'ssh':
332 return "ssh://127.0.0.1%s" % (path
)
334 raise Exception("Protocol %s not supported" % (imgproto
))
336 class VM(qtest
.QEMUQtestMachine
):
339 def __init__(self
, path_suffix
=''):
340 name
= "qemu%s-%d" % (path_suffix
, os
.getpid())
341 super(VM
, self
).__init
__(qemu_prog
, qemu_opts
, name
=name
,
343 socket_scm_helper
=socket_scm_helper
)
346 def add_object(self
, opts
):
347 self
._args
.append('-object')
348 self
._args
.append(opts
)
351 def add_device(self
, opts
):
352 self
._args
.append('-device')
353 self
._args
.append(opts
)
356 def add_drive_raw(self
, opts
):
357 self
._args
.append('-drive')
358 self
._args
.append(opts
)
361 def add_drive(self
, path
, opts
='', interface
='virtio', format
=imgfmt
):
362 '''Add a virtio-blk drive to the VM'''
363 options
= ['if=%s' % interface
,
364 'id=drive%d' % self
._num
_drives
]
367 options
.append('file=%s' % path
)
368 options
.append('format=%s' % format
)
369 options
.append('cache=%s' % cachemode
)
374 if format
== 'luks' and 'key-secret' not in opts
:
375 # default luks support
376 if luks_default_secret_object
not in self
._args
:
377 self
.add_object(luks_default_secret_object
)
379 options
.append(luks_default_key_secret_opt
)
381 self
._args
.append('-drive')
382 self
._args
.append(','.join(options
))
383 self
._num
_drives
+= 1
386 def add_blockdev(self
, opts
):
387 self
._args
.append('-blockdev')
388 if isinstance(opts
, str):
389 self
._args
.append(opts
)
391 self
._args
.append(','.join(opts
))
394 def add_incoming(self
, addr
):
395 self
._args
.append('-incoming')
396 self
._args
.append(addr
)
399 def pause_drive(self
, drive
, event
=None):
400 '''Pause drive r/w operations'''
402 self
.pause_drive(drive
, "read_aio")
403 self
.pause_drive(drive
, "write_aio")
405 self
.qmp('human-monitor-command',
406 command_line
='qemu-io %s "break %s bp_%s"' % (drive
, event
, drive
))
408 def resume_drive(self
, drive
):
409 self
.qmp('human-monitor-command',
410 command_line
='qemu-io %s "remove_break bp_%s"' % (drive
, drive
))
412 def hmp_qemu_io(self
, drive
, cmd
):
413 '''Write to a given drive using an HMP command'''
414 return self
.qmp('human-monitor-command',
415 command_line
='qemu-io %s "%s"' % (drive
, cmd
))
417 def flatten_qmp_object(self
, obj
, output
=None, basestr
=''):
420 if isinstance(obj
, list):
421 for i
in range(len(obj
)):
422 self
.flatten_qmp_object(obj
[i
], output
, basestr
+ str(i
) + '.')
423 elif isinstance(obj
, dict):
425 self
.flatten_qmp_object(obj
[key
], output
, basestr
+ key
+ '.')
427 output
[basestr
[:-1]] = obj
# Strip trailing '.'
430 def qmp_to_opts(self
, obj
):
431 obj
= self
.flatten_qmp_object(obj
)
434 output_list
+= [key
+ '=' + obj
[key
]]
435 return ','.join(output_list
)
437 def get_qmp_events_filtered(self
, wait
=True):
439 for ev
in self
.get_qmp_events(wait
=wait
):
440 result
.append(filter_qmp_event(ev
))
443 def qmp_log(self
, cmd
, filters
=[filter_testfiles
], **kwargs
):
444 logmsg
= "{'execute': '%s', 'arguments': %s}" % (cmd
, kwargs
)
446 result
= self
.qmp(cmd
, **kwargs
)
447 log(str(result
), filters
)
450 def run_job(self
, job
, auto_finalize
=True, auto_dismiss
=False):
452 for ev
in self
.get_qmp_events_filtered(wait
=True):
453 if ev
['event'] == 'JOB_STATUS_CHANGE':
454 status
= ev
['data']['status']
455 if status
== 'aborting':
456 result
= self
.qmp('query-jobs')
457 for j
in result
['return']:
459 log('Job failed: %s' % (j
['error']))
460 elif status
== 'pending' and not auto_finalize
:
461 self
.qmp_log('job-finalize', id=job
)
462 elif status
== 'concluded' and not auto_dismiss
:
463 self
.qmp_log('job-dismiss', id=job
)
464 elif status
== 'null':
470 index_re
= re
.compile(r
'([^\[]+)\[([^\]]+)\]')
472 class QMPTestCase(unittest
.TestCase
):
473 '''Abstract base class for QMP test cases'''
475 def dictpath(self
, d
, path
):
476 '''Traverse a path in a nested dict'''
477 for component
in path
.split('/'):
478 m
= index_re
.match(component
)
480 component
, idx
= m
.groups()
483 if not isinstance(d
, dict) or component
not in d
:
484 self
.fail('failed path traversal for "%s" in "%s"' % (path
, str(d
)))
488 if not isinstance(d
, list):
489 self
.fail('path component "%s" in "%s" is not a list in "%s"' % (component
, path
, str(d
)))
493 self
.fail('invalid index "%s" in path "%s" in "%s"' % (idx
, path
, str(d
)))
496 def assert_qmp_absent(self
, d
, path
):
498 result
= self
.dictpath(d
, path
)
499 except AssertionError:
501 self
.fail('path "%s" has value "%s"' % (path
, str(result
)))
503 def assert_qmp(self
, d
, path
, value
):
504 '''Assert that the value for a specific path in a QMP dict matches'''
505 result
= self
.dictpath(d
, path
)
506 self
.assertEqual(result
, value
, 'values not equal "%s" and "%s"' % (str(result
), str(value
)))
508 def assert_no_active_block_jobs(self
):
509 result
= self
.vm
.qmp('query-block-jobs')
510 self
.assert_qmp(result
, 'return', [])
512 def assert_has_block_node(self
, node_name
=None, file_name
=None):
513 """Issue a query-named-block-nodes and assert node_name and/or
514 file_name is present in the result"""
515 def check_equal_or_none(a
, b
):
516 return a
== None or b
== None or a
== b
517 assert node_name
or file_name
518 result
= self
.vm
.qmp('query-named-block-nodes')
519 for x
in result
["return"]:
520 if check_equal_or_none(x
.get("node-name"), node_name
) and \
521 check_equal_or_none(x
.get("file"), file_name
):
523 self
.assertTrue(False, "Cannot find %s %s in result:\n%s" % \
524 (node_name
, file_name
, result
))
526 def assert_json_filename_equal(self
, json_filename
, reference
):
527 '''Asserts that the given filename is a json: filename and that its
528 content is equal to the given reference object'''
529 self
.assertEqual(json_filename
[:5], 'json:')
530 self
.assertEqual(self
.vm
.flatten_qmp_object(json
.loads(json_filename
[5:])),
531 self
.vm
.flatten_qmp_object(reference
))
533 def cancel_and_wait(self
, drive
='drive0', force
=False, resume
=False):
534 '''Cancel a block job and wait for it to finish, returning the event'''
535 result
= self
.vm
.qmp('block-job-cancel', device
=drive
, force
=force
)
536 self
.assert_qmp(result
, 'return', {})
539 self
.vm
.resume_drive(drive
)
544 for event
in self
.vm
.get_qmp_events(wait
=True):
545 if event
['event'] == 'BLOCK_JOB_COMPLETED' or \
546 event
['event'] == 'BLOCK_JOB_CANCELLED':
547 self
.assert_qmp(event
, 'data/device', drive
)
550 elif event
['event'] == 'JOB_STATUS_CHANGE':
551 self
.assert_qmp(event
, 'data/id', drive
)
554 self
.assert_no_active_block_jobs()
557 def wait_until_completed(self
, drive
='drive0', check_offset
=True):
558 '''Wait for a block job to finish, returning the event'''
560 for event
in self
.vm
.get_qmp_events(wait
=True):
561 if event
['event'] == 'BLOCK_JOB_COMPLETED':
562 self
.assert_qmp(event
, 'data/device', drive
)
563 self
.assert_qmp_absent(event
, 'data/error')
565 self
.assert_qmp(event
, 'data/offset', event
['data']['len'])
566 self
.assert_no_active_block_jobs()
568 elif event
['event'] == 'JOB_STATUS_CHANGE':
569 self
.assert_qmp(event
, 'data/id', drive
)
571 def wait_ready(self
, drive
='drive0'):
572 '''Wait until a block job BLOCK_JOB_READY event'''
573 f
= {'data': {'type': 'mirror', 'device': drive
} }
574 event
= self
.vm
.event_wait(name
='BLOCK_JOB_READY', match
=f
)
576 def wait_ready_and_cancel(self
, drive
='drive0'):
577 self
.wait_ready(drive
=drive
)
578 event
= self
.cancel_and_wait(drive
=drive
)
579 self
.assertEquals(event
['event'], 'BLOCK_JOB_COMPLETED')
580 self
.assert_qmp(event
, 'data/type', 'mirror')
581 self
.assert_qmp(event
, 'data/offset', event
['data']['len'])
583 def complete_and_wait(self
, drive
='drive0', wait_ready
=True):
584 '''Complete a block job and wait for it to finish'''
586 self
.wait_ready(drive
=drive
)
588 result
= self
.vm
.qmp('block-job-complete', device
=drive
)
589 self
.assert_qmp(result
, 'return', {})
591 event
= self
.wait_until_completed(drive
=drive
)
592 self
.assert_qmp(event
, 'data/type', 'mirror')
594 def pause_wait(self
, job_id
='job0'):
595 with
Timeout(1, "Timeout waiting for job to pause"):
597 result
= self
.vm
.qmp('query-block-jobs')
599 for job
in result
['return']:
600 if job
['device'] == job_id
:
602 if job
['paused'] == True and job
['busy'] == False:
607 def pause_job(self
, job_id
='job0', wait
=True):
608 result
= self
.vm
.qmp('block-job-pause', device
=job_id
)
609 self
.assert_qmp(result
, 'return', {})
611 return self
.pause_wait(job_id
)
616 '''Skip this test suite'''
617 # Each test in qemu-iotests has a number ("seq")
618 seq
= os
.path
.basename(sys
.argv
[0])
620 open('%s/%s.notrun' % (output_dir
, seq
), 'wb').write(reason
+ '\n')
621 print('%s not run: %s' % (seq
, reason
))
624 def verify_image_format(supported_fmts
=[], unsupported_fmts
=[]):
625 assert not (supported_fmts
and unsupported_fmts
)
627 if 'generic' in supported_fmts
and \
628 os
.environ
.get('IMGFMT_GENERIC', 'true') == 'true':
630 # _supported_fmt generic
634 not_sup
= supported_fmts
and (imgfmt
not in supported_fmts
)
635 if not_sup
or (imgfmt
in unsupported_fmts
):
636 notrun('not suitable for this image format: %s' % imgfmt
)
638 def verify_protocol(supported
=[], unsupported
=[]):
639 assert not (supported
and unsupported
)
641 if 'generic' in supported
:
644 not_sup
= supported
and (imgproto
not in supported
)
645 if not_sup
or (imgproto
in unsupported
):
646 notrun('not suitable for this protocol: %s' % imgproto
)
648 def verify_platform(supported_oses
=['linux']):
649 if True not in [sys
.platform
.startswith(x
) for x
in supported_oses
]:
650 notrun('not suitable for this OS: %s' % sys
.platform
)
652 def verify_cache_mode(supported_cache_modes
=[]):
653 if supported_cache_modes
and (cachemode
not in supported_cache_modes
):
654 notrun('not suitable for this cache mode: %s' % cachemode
)
656 def supports_quorum():
657 return 'quorum' in qemu_img_pipe('--help')
660 '''Skip test suite if quorum support is not available'''
661 if not supports_quorum():
662 notrun('quorum support missing')
664 def main(supported_fmts
=[], supported_oses
=['linux'], supported_cache_modes
=[],
665 unsupported_fmts
=[]):
670 # We are using TEST_DIR and QEMU_DEFAULT_MACHINE as proxies to
671 # indicate that we're not being run via "check". There may be
672 # other things set up by "check" that individual test cases rely
674 if test_dir
is None or qemu_default_machine
is None:
675 sys
.stderr
.write('Please run this test via the "check" script\n')
676 sys
.exit(os
.EX_USAGE
)
678 debug
= '-d' in sys
.argv
680 verify_image_format(supported_fmts
, unsupported_fmts
)
681 verify_platform(supported_oses
)
682 verify_cache_mode(supported_cache_modes
)
684 # We need to filter out the time taken from the output so that qemu-iotest
685 # can reliably diff the results against master output.
690 sys
.argv
.remove('-d')
692 output
= StringIO
.StringIO()
694 logging
.basicConfig(level
=(logging
.DEBUG
if debug
else logging
.WARN
))
696 class MyTestRunner(unittest
.TextTestRunner
):
697 def __init__(self
, stream
=output
, descriptions
=True, verbosity
=verbosity
):
698 unittest
.TextTestRunner
.__init
__(self
, stream
, descriptions
, verbosity
)
700 # unittest.main() will use sys.exit() so expect a SystemExit exception
702 unittest
.main(testRunner
=MyTestRunner
)
705 sys
.stderr
.write(re
.sub(r
'Ran (\d+) tests? in [\d.]+s', r
'Ran \1 tests', output
.getvalue()))