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/>.
34 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts'))
38 # This will not work if arguments contain spaces but is necessary if we
39 # want to support the override options that ./check supports.
40 qemu_img_args = [os.environ.get('QEMU_IMG_PROG', 'qemu-img')]
41 if os.environ.get('QEMU_IMG_OPTIONS'):
42 qemu_img_args += os.environ['QEMU_IMG_OPTIONS'].strip().split(' ')
44 qemu_io_args = [os.environ.get('QEMU_IO_PROG', 'qemu-io')]
45 if os.environ.get('QEMU_IO_OPTIONS'):
46 qemu_io_args += os.environ['QEMU_IO_OPTIONS'].strip().split(' ')
48 qemu_nbd_args = [os.environ.get('QEMU_NBD_PROG', 'qemu-nbd')]
49 if os.environ.get('QEMU_NBD_OPTIONS'):
50 qemu_nbd_args += os.environ['QEMU_NBD_OPTIONS'].strip().split(' ')
52 qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
53 qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
55 imgfmt = os.environ.get('IMGFMT', 'raw')
56 imgproto = os.environ.get('IMGPROTO', 'file')
57 test_dir = os.environ.get('TEST_DIR')
58 output_dir = os.environ.get('OUTPUT_DIR', '.')
59 cachemode = os.environ.get('CACHEMODE')
60 qemu_default_machine = os.environ.get('QEMU_DEFAULT_MACHINE')
62 socket_scm_helper = os.environ.get('SOCKET_SCM_HELPER', 'socket_scm_helper')
65 luks_default_secret_object = 'secret,id=keysec0,data=' + \
66 os.environ['IMGKEYSECRET']
67 luks_default_key_secret_opt = 'key-secret=keysec0'
71 '''Run qemu-img and return the exit code'''
72 devnull = open('/dev/null', 'r+')
73 exitcode = subprocess.call(qemu_img_args + list(args), stdin=devnull, stdout=devnull)
75 sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args))))
78 def qemu_img_create(*args):
81 # default luks support
82 if '-f' in args and args[args.index('-f') + 1] == 'luks':
85 if 'key-secret' not in args[i + 1]:
86 args[i + 1].append(luks_default_key_secret_opt)
87 args.insert(i + 2, '--object')
88 args.insert(i + 3, luks_default_secret_object)
90 args = ['-o', luks_default_key_secret_opt,
91 '--object', luks_default_secret_object] + args
93 args.insert(0, 'create')
95 return qemu_img(*args)
97 def qemu_img_verbose(*args):
98 '''Run qemu-img without suppressing its output and return the exit code'''
99 exitcode = subprocess.call(qemu_img_args + list(args))
101 sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args))))
104 def qemu_img_pipe(*args):
105 '''Run qemu-img and return its output'''
106 subp = subprocess.Popen(qemu_img_args + list(args),
107 stdout=subprocess.PIPE,
108 stderr=subprocess.STDOUT,
109 universal_newlines=True)
110 exitcode = subp.wait()
112 sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args))))
113 return subp.communicate()[0]
115 def img_info_log(filename, filter_path=None, imgopts=False, extra_args=[]):
118 args.append('--image-opts')
120 args += [ '-f', imgfmt ]
122 args.append(filename)
124 output = qemu_img_pipe(*args)
126 filter_path = filename
127 log(filter_img_info(output, filter_path))
130 '''Run qemu-io and return the stdout data'''
131 args = qemu_io_args + list(args)
132 subp = subprocess.Popen(args, stdout=subprocess.PIPE,
133 stderr=subprocess.STDOUT,
134 universal_newlines=True)
135 exitcode = subp.wait()
137 sys.stderr.write('qemu-io received signal %i: %s\n' % (-exitcode, ' '.join(args)))
138 return subp.communicate()[0]
140 def qemu_io_silent(*args):
141 '''Run qemu-io and return the exit code, suppressing stdout'''
142 args = qemu_io_args + list(args)
143 exitcode = subprocess.call(args, stdout=open('/dev/null', 'w'))
145 sys.stderr.write('qemu-io received signal %i: %s\n' %
146 (-exitcode, ' '.join(args)))
150 class QemuIoInteractive:
151 def __init__(self, *args):
152 self.args = qemu_io_args + list(args)
153 self._p = subprocess.Popen(self.args, stdin=subprocess.PIPE,
154 stdout=subprocess.PIPE,
155 stderr=subprocess.STDOUT,
156 universal_newlines=True)
157 assert self._p.stdout.read(9) == 'qemu-io> '
160 self._p.communicate('q\n')
162 def _read_output(self):
163 pattern = 'qemu-io> '
168 c = self._p.stdout.read(1)
169 # check unexpected EOF
172 if c == pattern[pos]:
177 return ''.join(s[:-n])
180 # quit command is in close(), '\n' is added automatically
181 assert '\n' not in cmd
183 assert cmd != 'q' and cmd != 'quit'
184 self._p.stdin.write(cmd + '\n')
185 self._p.stdin.flush()
186 return self._read_output()
190 '''Run qemu-nbd in daemon mode and return the parent's exit code'''
191 return subprocess.call(qemu_nbd_args + ['--fork'] + list(args))
193 def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt):
194 '''Return True if two image files are identical'''
195 return qemu_img('compare', '-f', fmt1,
196 '-F', fmt2, img1, img2) == 0
198 def create_image(name, size):
199 '''Create a fully-allocated raw image with sector markers'''
200 file = open(name, 'wb')
203 sector = struct.pack('>l504xl', i // 512, i // 512)
209 '''Return image's virtual size'''
210 r = qemu_img_pipe('info', '--output=json', '-f', imgfmt, img)
211 return json.loads(r)['virtual-size']
213 test_dir_re = re.compile(r"%s" % test_dir)
214 def filter_test_dir(msg):
215 return test_dir_re.sub("TEST_DIR", msg)
217 win32_re = re.compile(r"\r")
218 def filter_win32(msg):
219 return win32_re.sub("", msg)
221 qemu_io_re = re.compile(r"[0-9]* ops; [0-9\/:. sec]* \([0-9\/.inf]* [EPTGMKiBbytes]*\/sec and [0-9\/.inf]* ops\/sec\)")
222 def filter_qemu_io(msg):
223 msg = filter_win32(msg)
224 return qemu_io_re.sub("X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)", msg)
226 chown_re = re.compile(r"chown [0-9]+:[0-9]+")
227 def filter_chown(msg):
228 return chown_re.sub("chown UID:GID", msg)
230 def filter_qmp_event(event):
231 '''Filter a QMP event dict'''
233 if 'timestamp' in event:
234 event['timestamp']['seconds'] = 'SECS'
235 event['timestamp']['microseconds'] = 'USECS'
238 def filter_testfiles(msg):
239 prefix = os.path.join(test_dir, "%s-" % (os.getpid()))
240 return msg.replace(prefix, 'TEST_DIR/PID-')
242 def filter_img_info(output, filename):
244 for line in output.split('\n'):
245 if 'disk size' in line or 'actual-size' in line:
247 line = line.replace(filename, 'TEST_IMG') \
248 .replace(imgfmt, 'IMGFMT')
249 line = re.sub('iters: [0-9]+', 'iters: XXX', line)
250 line = re.sub('uuid: [-a-f0-9]+', 'uuid: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', line)
252 return '\n'.join(lines)
254 def log(msg, filters=[]):
257 if type(msg) is dict or type(msg) is list:
258 print(json.dumps(msg, sort_keys=True))
263 def __init__(self, seconds, errmsg = "Timeout"):
264 self.seconds = seconds
267 signal.signal(signal.SIGALRM, self.timeout)
268 signal.setitimer(signal.ITIMER_REAL, self.seconds)
270 def __exit__(self, type, value, traceback):
271 signal.setitimer(signal.ITIMER_REAL, 0)
273 def timeout(self, signum, frame):
274 raise Exception(self.errmsg)
277 class FilePath(object):
278 '''An auto-generated filename that cleans itself up.
280 Use this context manager to generate filenames and ensure that the file
283 with TestFilePath('test.img') as img_path:
284 qemu_img('create', img_path, '1G')
285 # migration_sock_path is automatically deleted
287 def __init__(self, name):
288 filename = '{0}-{1}'.format(os.getpid(), name)
289 self.path = os.path.join(test_dir, filename)
294 def __exit__(self, exc_type, exc_val, exc_tb):
302 def file_path_remover():
303 for path in reversed(file_path_remover.paths):
310 def file_path(*names):
311 ''' Another way to get auto-generated filename that cleans itself up.
315 img_a, img_b = file_path('a.img', 'b.img')
316 sock = file_path('socket')
319 if not hasattr(file_path_remover, 'paths'):
320 file_path_remover.paths = []
321 atexit.register(file_path_remover)
325 filename = '{0}-{1}'.format(os.getpid(), name)
326 path = os.path.join(test_dir, filename)
327 file_path_remover.paths.append(path)
330 return paths[0] if len(paths) == 1 else paths
332 def remote_filename(path):
333 if imgproto == 'file':
335 elif imgproto == 'ssh':
336 return "ssh://127.0.0.1%s" % (path)
338 raise Exception("Protocol %s not supported" % (imgproto))
340 class VM(qtest.QEMUQtestMachine):
343 def __init__(self, path_suffix=''):
344 name = "qemu%s-%d" % (path_suffix, os.getpid())
345 super(VM, self).__init__(qemu_prog, qemu_opts, name=name,
347 socket_scm_helper=socket_scm_helper)
350 def add_object(self, opts):
351 self._args.append('-object')
352 self._args.append(opts)
355 def add_device(self, opts):
356 self._args.append('-device')
357 self._args.append(opts)
360 def add_drive_raw(self, opts):
361 self._args.append('-drive')
362 self._args.append(opts)
365 def add_drive(self, path, opts='', interface='virtio', format=imgfmt):
366 '''Add a virtio-blk drive to the VM'''
367 options = ['if=%s' % interface,
368 'id=drive%d' % self._num_drives]
371 options.append('file=%s' % path)
372 options.append('format=%s' % format)
373 options.append('cache=%s' % cachemode)
378 if format == 'luks' and 'key-secret' not in opts:
379 # default luks support
380 if luks_default_secret_object not in self._args:
381 self.add_object(luks_default_secret_object)
383 options.append(luks_default_key_secret_opt)
385 self._args.append('-drive')
386 self._args.append(','.join(options))
387 self._num_drives += 1
390 def add_blockdev(self, opts):
391 self._args.append('-blockdev')
392 if isinstance(opts, str):
393 self._args.append(opts)
395 self._args.append(','.join(opts))
398 def add_incoming(self, addr):
399 self._args.append('-incoming')
400 self._args.append(addr)
403 def pause_drive(self, drive, event=None):
404 '''Pause drive r/w operations'''
406 self.pause_drive(drive, "read_aio")
407 self.pause_drive(drive, "write_aio")
409 self.qmp('human-monitor-command',
410 command_line='qemu-io %s "break %s bp_%s"' % (drive, event, drive))
412 def resume_drive(self, drive):
413 self.qmp('human-monitor-command',
414 command_line='qemu-io %s "remove_break bp_%s"' % (drive, drive))
416 def hmp_qemu_io(self, drive, cmd):
417 '''Write to a given drive using an HMP command'''
418 return self.qmp('human-monitor-command',
419 command_line='qemu-io %s "%s"' % (drive, cmd))
421 def flatten_qmp_object(self, obj, output=None, basestr=''):
424 if isinstance(obj, list):
425 for i in range(len(obj)):
426 self.flatten_qmp_object(obj[i], output, basestr + str(i) + '.')
427 elif isinstance(obj, dict):
429 self.flatten_qmp_object(obj[key], output, basestr + key + '.')
431 output[basestr[:-1]] = obj # Strip trailing '.'
434 def qmp_to_opts(self, obj):
435 obj = self.flatten_qmp_object(obj)
438 output_list += [key + '=' + obj[key]]
439 return ','.join(output_list)
441 def get_qmp_events_filtered(self, wait=True):
443 for ev in self.get_qmp_events(wait=wait):
444 result.append(filter_qmp_event(ev))
447 def qmp_log(self, cmd, filters=[filter_testfiles], **kwargs):
448 logmsg = '{"execute": "%s", "arguments": %s}' % \
449 (cmd, json.dumps(kwargs, sort_keys=True))
451 result = self.qmp(cmd, **kwargs)
452 log(json.dumps(result, sort_keys=True), filters)
455 def run_job(self, job, auto_finalize=True, auto_dismiss=False):
457 for ev in self.get_qmp_events_filtered(wait=True):
458 if ev['event'] == 'JOB_STATUS_CHANGE':
459 status = ev['data']['status']
460 if status == 'aborting':
461 result = self.qmp('query-jobs')
462 for j in result['return']:
464 log('Job failed: %s' % (j['error']))
465 elif status == 'pending' and not auto_finalize:
466 self.qmp_log('job-finalize', id=job)
467 elif status == 'concluded' and not auto_dismiss:
468 self.qmp_log('job-dismiss', id=job)
469 elif status == 'null':
475 index_re = re.compile(r'([^\[]+)\[([^\]]+)\]')
477 class QMPTestCase(unittest.TestCase):
478 '''Abstract base class for QMP test cases'''
480 def dictpath(self, d, path):
481 '''Traverse a path in a nested dict'''
482 for component in path.split('/'):
483 m = index_re.match(component)
485 component, idx = m.groups()
488 if not isinstance(d, dict) or component not in d:
489 self.fail('failed path traversal for "%s" in "%s"' % (path, str(d)))
493 if not isinstance(d, list):
494 self.fail('path component "%s" in "%s" is not a list in "%s"' % (component, path, str(d)))
498 self.fail('invalid index "%s" in path "%s" in "%s"' % (idx, path, str(d)))
501 def assert_qmp_absent(self, d, path):
503 result = self.dictpath(d, path)
504 except AssertionError:
506 self.fail('path "%s" has value "%s"' % (path, str(result)))
508 def assert_qmp(self, d, path, value):
509 '''Assert that the value for a specific path in a QMP dict matches'''
510 result = self.dictpath(d, path)
511 self.assertEqual(result, value, 'values not equal "%s" and "%s"' % (str(result), str(value)))
513 def assert_no_active_block_jobs(self):
514 result = self.vm.qmp('query-block-jobs')
515 self.assert_qmp(result, 'return', [])
517 def assert_has_block_node(self, node_name=None, file_name=None):
518 """Issue a query-named-block-nodes and assert node_name and/or
519 file_name is present in the result"""
520 def check_equal_or_none(a, b):
521 return a == None or b == None or a == b
522 assert node_name or file_name
523 result = self.vm.qmp('query-named-block-nodes')
524 for x in result["return"]:
525 if check_equal_or_none(x.get("node-name"), node_name) and \
526 check_equal_or_none(x.get("file"), file_name):
528 self.assertTrue(False, "Cannot find %s %s in result:\n%s" % \
529 (node_name, file_name, result))
531 def assert_json_filename_equal(self, json_filename, reference):
532 '''Asserts that the given filename is a json: filename and that its
533 content is equal to the given reference object'''
534 self.assertEqual(json_filename[:5], 'json:')
535 self.assertEqual(self.vm.flatten_qmp_object(json.loads(json_filename[5:])),
536 self.vm.flatten_qmp_object(reference))
538 def cancel_and_wait(self, drive='drive0', force=False, resume=False):
539 '''Cancel a block job and wait for it to finish, returning the event'''
540 result = self.vm.qmp('block-job-cancel', device=drive, force=force)
541 self.assert_qmp(result, 'return', {})
544 self.vm.resume_drive(drive)
549 for event in self.vm.get_qmp_events(wait=True):
550 if event['event'] == 'BLOCK_JOB_COMPLETED' or \
551 event['event'] == 'BLOCK_JOB_CANCELLED':
552 self.assert_qmp(event, 'data/device', drive)
555 elif event['event'] == 'JOB_STATUS_CHANGE':
556 self.assert_qmp(event, 'data/id', drive)
559 self.assert_no_active_block_jobs()
562 def wait_until_completed(self, drive='drive0', check_offset=True):
563 '''Wait for a block job to finish, returning the event'''
565 for event in self.vm.get_qmp_events(wait=True):
566 if event['event'] == 'BLOCK_JOB_COMPLETED':
567 self.assert_qmp(event, 'data/device', drive)
568 self.assert_qmp_absent(event, 'data/error')
570 self.assert_qmp(event, 'data/offset', event['data']['len'])
571 self.assert_no_active_block_jobs()
573 elif event['event'] == 'JOB_STATUS_CHANGE':
574 self.assert_qmp(event, 'data/id', drive)
576 def wait_ready(self, drive='drive0'):
577 '''Wait until a block job BLOCK_JOB_READY event'''
578 f = {'data': {'type': 'mirror', 'device': drive } }
579 event = self.vm.event_wait(name='BLOCK_JOB_READY', match=f)
581 def wait_ready_and_cancel(self, drive='drive0'):
582 self.wait_ready(drive=drive)
583 event = self.cancel_and_wait(drive=drive)
584 self.assertEquals(event['event'], 'BLOCK_JOB_COMPLETED')
585 self.assert_qmp(event, 'data/type', 'mirror')
586 self.assert_qmp(event, 'data/offset', event['data']['len'])
588 def complete_and_wait(self, drive='drive0', wait_ready=True):
589 '''Complete a block job and wait for it to finish'''
591 self.wait_ready(drive=drive)
593 result = self.vm.qmp('block-job-complete', device=drive)
594 self.assert_qmp(result, 'return', {})
596 event = self.wait_until_completed(drive=drive)
597 self.assert_qmp(event, 'data/type', 'mirror')
599 def pause_wait(self, job_id='job0'):
600 with Timeout(1, "Timeout waiting for job to pause"):
602 result = self.vm.qmp('query-block-jobs')
604 for job in result['return']:
605 if job['device'] == job_id:
607 if job['paused'] == True and job['busy'] == False:
612 def pause_job(self, job_id='job0', wait=True):
613 result = self.vm.qmp('block-job-pause', device=job_id)
614 self.assert_qmp(result, 'return', {})
616 return self.pause_wait(job_id)
621 '''Skip this test suite'''
622 # Each test in qemu-iotests has a number ("seq")
623 seq = os.path.basename(sys.argv[0])
625 open('%s/%s.notrun' % (output_dir, seq), 'wb').write(reason + '\n')
626 print('%s not run: %s' % (seq, reason))
629 def verify_image_format(supported_fmts=[], unsupported_fmts=[]):
630 assert not (supported_fmts and unsupported_fmts)
632 if 'generic' in supported_fmts and \
633 os.environ.get('IMGFMT_GENERIC', 'true') == 'true':
635 # _supported_fmt generic
639 not_sup = supported_fmts and (imgfmt not in supported_fmts)
640 if not_sup or (imgfmt in unsupported_fmts):
641 notrun('not suitable for this image format: %s' % imgfmt)
643 def verify_protocol(supported=[], unsupported=[]):
644 assert not (supported and unsupported)
646 if 'generic' in supported:
649 not_sup = supported and (imgproto not in supported)
650 if not_sup or (imgproto in unsupported):
651 notrun('not suitable for this protocol: %s' % imgproto)
653 def verify_platform(supported_oses=['linux']):
654 if True not in [sys.platform.startswith(x) for x in supported_oses]:
655 notrun('not suitable for this OS: %s' % sys.platform)
657 def verify_cache_mode(supported_cache_modes=[]):
658 if supported_cache_modes and (cachemode not in supported_cache_modes):
659 notrun('not suitable for this cache mode: %s' % cachemode)
661 def supports_quorum():
662 return 'quorum' in qemu_img_pipe('--help')
665 '''Skip test suite if quorum support is not available'''
666 if not supports_quorum():
667 notrun('quorum support missing')
669 def main(supported_fmts=[], supported_oses=['linux'], supported_cache_modes=[],
670 unsupported_fmts=[]):
675 # We are using TEST_DIR and QEMU_DEFAULT_MACHINE as proxies to
676 # indicate that we're not being run via "check". There may be
677 # other things set up by "check" that individual test cases rely
679 if test_dir is None or qemu_default_machine is None:
680 sys.stderr.write('Please run this test via the "check" script\n')
681 sys.exit(os.EX_USAGE)
683 debug = '-d' in sys.argv
685 verify_image_format(supported_fmts, unsupported_fmts)
686 verify_platform(supported_oses)
687 verify_cache_mode(supported_cache_modes)
692 sys.argv.remove('-d')
694 # We need to filter out the time taken from the output so that
695 # qemu-iotest can reliably diff the results against master output.
696 if sys.version_info.major >= 3:
697 output = io.StringIO()
699 # io.StringIO is for unicode strings, which is not what
700 # 2.x's test runner emits.
701 output = io.BytesIO()
703 logging.basicConfig(level=(logging.DEBUG if debug else logging.WARN))
705 class MyTestRunner(unittest.TextTestRunner):
706 def __init__(self, stream=output, descriptions=True, verbosity=verbosity):
707 unittest.TextTestRunner.__init__(self, stream, descriptions, verbosity)
709 # unittest.main() will use sys.exit() so expect a SystemExit exception
711 unittest.main(testRunner=MyTestRunner)
714 sys.stderr.write(re.sub(r'Ran (\d+) tests? in [\d.]+s', r'Ran \1 tests', output.getvalue()))