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 19import errno 20import os 21import re 22import subprocess 23import string 24import unittest 25import sys 26sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) 27import qtest 28import struct 29import json 30import signal 31import logging 32 33 34# This will not work if arguments contain spaces but is necessary if we 35# want to support the override options that ./check supports. 36qemu_img_args = [os.environ.get('QEMU_IMG_PROG', 'qemu-img')] 37if os.environ.get('QEMU_IMG_OPTIONS'): 38 qemu_img_args += os.environ['QEMU_IMG_OPTIONS'].strip().split(' ') 39 40qemu_io_args = [os.environ.get('QEMU_IO_PROG', 'qemu-io')] 41if os.environ.get('QEMU_IO_OPTIONS'): 42 qemu_io_args += os.environ['QEMU_IO_OPTIONS'].strip().split(' ') 43 44qemu_nbd_args = [os.environ.get('QEMU_NBD_PROG', 'qemu-nbd')] 45if os.environ.get('QEMU_NBD_OPTIONS'): 46 qemu_nbd_args += os.environ['QEMU_NBD_OPTIONS'].strip().split(' ') 47 48qemu_prog = os.environ.get('QEMU_PROG', 'qemu') 49qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ') 50 51imgfmt = os.environ.get('IMGFMT', 'raw') 52imgproto = os.environ.get('IMGPROTO', 'file') 53test_dir = os.environ.get('TEST_DIR') 54output_dir = os.environ.get('OUTPUT_DIR', '.') 55cachemode = os.environ.get('CACHEMODE') 56qemu_default_machine = os.environ.get('QEMU_DEFAULT_MACHINE') 57 58socket_scm_helper = os.environ.get('SOCKET_SCM_HELPER', 'socket_scm_helper') 59debug = False 60 61luks_default_secret_object = 'secret,id=keysec0,data=' + \ 62 os.environ['IMGKEYSECRET'] 63luks_default_key_secret_opt = 'key-secret=keysec0' 64 65 66def qemu_img(*args): 67 '''Run qemu-img and return the exit code''' 68 devnull = open('/dev/null', 'r+') 69 exitcode = subprocess.call(qemu_img_args + list(args), stdin=devnull, stdout=devnull) 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 73 74def qemu_img_create(*args): 75 args = list(args) 76 77 # default luks support 78 if '-f' in args and args[args.index('-f') + 1] == 'luks': 79 if '-o' in args: 80 i = args.index('-o') 81 if 'key-secret' not in args[i + 1]: 82 args[i + 1].append(luks_default_key_secret_opt) 83 args.insert(i + 2, '--object') 84 args.insert(i + 3, luks_default_secret_object) 85 else: 86 args = ['-o', luks_default_key_secret_opt, 87 '--object', luks_default_secret_object] + args 88 89 args.insert(0, 'create') 90 91 return qemu_img(*args) 92 93def qemu_img_verbose(*args): 94 '''Run qemu-img without suppressing its output and return the exit code''' 95 exitcode = subprocess.call(qemu_img_args + list(args)) 96 if exitcode < 0: 97 sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args)))) 98 return exitcode 99 100def qemu_img_pipe(*args): 101 '''Run qemu-img and return its output''' 102 subp = subprocess.Popen(qemu_img_args + list(args), 103 stdout=subprocess.PIPE, 104 stderr=subprocess.STDOUT) 105 exitcode = subp.wait() 106 if exitcode < 0: 107 sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args)))) 108 return subp.communicate()[0] 109 110def qemu_io(*args): 111 '''Run qemu-io and return the stdout data''' 112 args = qemu_io_args + list(args) 113 subp = subprocess.Popen(args, stdout=subprocess.PIPE, 114 stderr=subprocess.STDOUT) 115 exitcode = subp.wait() 116 if exitcode < 0: 117 sys.stderr.write('qemu-io received signal %i: %s\n' % (-exitcode, ' '.join(args))) 118 return subp.communicate()[0] 119 120 121class QemuIoInteractive: 122 def __init__(self, *args): 123 self.args = qemu_io_args + list(args) 124 self._p = subprocess.Popen(self.args, stdin=subprocess.PIPE, 125 stdout=subprocess.PIPE, 126 stderr=subprocess.STDOUT) 127 assert self._p.stdout.read(9) == 'qemu-io> ' 128 129 def close(self): 130 self._p.communicate('q\n') 131 132 def _read_output(self): 133 pattern = 'qemu-io> ' 134 n = len(pattern) 135 pos = 0 136 s = [] 137 while pos != n: 138 c = self._p.stdout.read(1) 139 # check unexpected EOF 140 assert c != '' 141 s.append(c) 142 if c == pattern[pos]: 143 pos += 1 144 else: 145 pos = 0 146 147 return ''.join(s[:-n]) 148 149 def cmd(self, cmd): 150 # quit command is in close(), '\n' is added automatically 151 assert '\n' not in cmd 152 cmd = cmd.strip() 153 assert cmd != 'q' and cmd != 'quit' 154 self._p.stdin.write(cmd + '\n') 155 return self._read_output() 156 157 158def qemu_nbd(*args): 159 '''Run qemu-nbd in daemon mode and return the parent's exit code''' 160 return subprocess.call(qemu_nbd_args + ['--fork'] + list(args)) 161 162def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt): 163 '''Return True if two image files are identical''' 164 return qemu_img('compare', '-f', fmt1, 165 '-F', fmt2, img1, img2) == 0 166 167def create_image(name, size): 168 '''Create a fully-allocated raw image with sector markers''' 169 file = open(name, 'w') 170 i = 0 171 while i < size: 172 sector = struct.pack('>l504xl', i / 512, i / 512) 173 file.write(sector) 174 i = i + 512 175 file.close() 176 177def image_size(img): 178 '''Return image's virtual size''' 179 r = qemu_img_pipe('info', '--output=json', '-f', imgfmt, img) 180 return json.loads(r)['virtual-size'] 181 182test_dir_re = re.compile(r"%s" % test_dir) 183def filter_test_dir(msg): 184 return test_dir_re.sub("TEST_DIR", msg) 185 186win32_re = re.compile(r"\r") 187def filter_win32(msg): 188 return win32_re.sub("", msg) 189 190qemu_io_re = re.compile(r"[0-9]* ops; [0-9\/:. sec]* \([0-9\/.inf]* [EPTGMKiBbytes]*\/sec and [0-9\/.inf]* ops\/sec\)") 191def filter_qemu_io(msg): 192 msg = filter_win32(msg) 193 return qemu_io_re.sub("X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)", msg) 194 195chown_re = re.compile(r"chown [0-9]+:[0-9]+") 196def filter_chown(msg): 197 return chown_re.sub("chown UID:GID", msg) 198 199def filter_qmp_event(event): 200 '''Filter a QMP event dict''' 201 event = dict(event) 202 if 'timestamp' in event: 203 event['timestamp']['seconds'] = 'SECS' 204 event['timestamp']['microseconds'] = 'USECS' 205 return event 206 207def log(msg, filters=[]): 208 for flt in filters: 209 msg = flt(msg) 210 print msg 211 212class Timeout: 213 def __init__(self, seconds, errmsg = "Timeout"): 214 self.seconds = seconds 215 self.errmsg = errmsg 216 def __enter__(self): 217 signal.signal(signal.SIGALRM, self.timeout) 218 signal.setitimer(signal.ITIMER_REAL, self.seconds) 219 return self 220 def __exit__(self, type, value, traceback): 221 signal.setitimer(signal.ITIMER_REAL, 0) 222 return False 223 def timeout(self, signum, frame): 224 raise Exception(self.errmsg) 225 226 227class FilePath(object): 228 '''An auto-generated filename that cleans itself up. 229 230 Use this context manager to generate filenames and ensure that the file 231 gets deleted:: 232 233 with TestFilePath('test.img') as img_path: 234 qemu_img('create', img_path, '1G') 235 # migration_sock_path is automatically deleted 236 ''' 237 def __init__(self, name): 238 filename = '{0}-{1}'.format(os.getpid(), name) 239 self.path = os.path.join(test_dir, filename) 240 241 def __enter__(self): 242 return self.path 243 244 def __exit__(self, exc_type, exc_val, exc_tb): 245 try: 246 os.remove(self.path) 247 except OSError: 248 pass 249 return False 250 251 252class VM(qtest.QEMUQtestMachine): 253 '''A QEMU VM''' 254 255 def __init__(self, path_suffix=''): 256 name = "qemu%s-%d" % (path_suffix, os.getpid()) 257 super(VM, self).__init__(qemu_prog, qemu_opts, name=name, 258 test_dir=test_dir, 259 socket_scm_helper=socket_scm_helper) 260 self._num_drives = 0 261 262 def add_object(self, opts): 263 self._args.append('-object') 264 self._args.append(opts) 265 return self 266 267 def add_device(self, opts): 268 self._args.append('-device') 269 self._args.append(opts) 270 return self 271 272 def add_drive_raw(self, opts): 273 self._args.append('-drive') 274 self._args.append(opts) 275 return self 276 277 def add_drive(self, path, opts='', interface='virtio', format=imgfmt): 278 '''Add a virtio-blk drive to the VM''' 279 options = ['if=%s' % interface, 280 'id=drive%d' % self._num_drives] 281 282 if path is not None: 283 options.append('file=%s' % path) 284 options.append('format=%s' % format) 285 options.append('cache=%s' % cachemode) 286 287 if opts: 288 options.append(opts) 289 290 if format == 'luks' and 'key-secret' not in opts: 291 # default luks support 292 if luks_default_secret_object not in self._args: 293 self.add_object(luks_default_secret_object) 294 295 options.append(luks_default_key_secret_opt) 296 297 self._args.append('-drive') 298 self._args.append(','.join(options)) 299 self._num_drives += 1 300 return self 301 302 def add_blockdev(self, opts): 303 self._args.append('-blockdev') 304 if isinstance(opts, str): 305 self._args.append(opts) 306 else: 307 self._args.append(','.join(opts)) 308 return self 309 310 def add_incoming(self, addr): 311 self._args.append('-incoming') 312 self._args.append(addr) 313 return self 314 315 def pause_drive(self, drive, event=None): 316 '''Pause drive r/w operations''' 317 if not event: 318 self.pause_drive(drive, "read_aio") 319 self.pause_drive(drive, "write_aio") 320 return 321 self.qmp('human-monitor-command', 322 command_line='qemu-io %s "break %s bp_%s"' % (drive, event, drive)) 323 324 def resume_drive(self, drive): 325 self.qmp('human-monitor-command', 326 command_line='qemu-io %s "remove_break bp_%s"' % (drive, drive)) 327 328 def hmp_qemu_io(self, drive, cmd): 329 '''Write to a given drive using an HMP command''' 330 return self.qmp('human-monitor-command', 331 command_line='qemu-io %s "%s"' % (drive, cmd)) 332 333 334index_re = re.compile(r'([^\[]+)\[([^\]]+)\]') 335 336class QMPTestCase(unittest.TestCase): 337 '''Abstract base class for QMP test cases''' 338 339 def dictpath(self, d, path): 340 '''Traverse a path in a nested dict''' 341 for component in path.split('/'): 342 m = index_re.match(component) 343 if m: 344 component, idx = m.groups() 345 idx = int(idx) 346 347 if not isinstance(d, dict) or component not in d: 348 self.fail('failed path traversal for "%s" in "%s"' % (path, str(d))) 349 d = d[component] 350 351 if m: 352 if not isinstance(d, list): 353 self.fail('path component "%s" in "%s" is not a list in "%s"' % (component, path, str(d))) 354 try: 355 d = d[idx] 356 except IndexError: 357 self.fail('invalid index "%s" in path "%s" in "%s"' % (idx, path, str(d))) 358 return d 359 360 def flatten_qmp_object(self, obj, output=None, basestr=''): 361 if output is None: 362 output = dict() 363 if isinstance(obj, list): 364 for i in range(len(obj)): 365 self.flatten_qmp_object(obj[i], output, basestr + str(i) + '.') 366 elif isinstance(obj, dict): 367 for key in obj: 368 self.flatten_qmp_object(obj[key], output, basestr + key + '.') 369 else: 370 output[basestr[:-1]] = obj # Strip trailing '.' 371 return output 372 373 def qmp_to_opts(self, obj): 374 obj = self.flatten_qmp_object(obj) 375 output_list = list() 376 for key in obj: 377 output_list += [key + '=' + obj[key]] 378 return ','.join(output_list) 379 380 def assert_qmp_absent(self, d, path): 381 try: 382 result = self.dictpath(d, path) 383 except AssertionError: 384 return 385 self.fail('path "%s" has value "%s"' % (path, str(result))) 386 387 def assert_qmp(self, d, path, value): 388 '''Assert that the value for a specific path in a QMP dict matches''' 389 result = self.dictpath(d, path) 390 self.assertEqual(result, value, 'values not equal "%s" and "%s"' % (str(result), str(value))) 391 392 def assert_no_active_block_jobs(self): 393 result = self.vm.qmp('query-block-jobs') 394 self.assert_qmp(result, 'return', []) 395 396 def assert_has_block_node(self, node_name=None, file_name=None): 397 """Issue a query-named-block-nodes and assert node_name and/or 398 file_name is present in the result""" 399 def check_equal_or_none(a, b): 400 return a == None or b == None or a == b 401 assert node_name or file_name 402 result = self.vm.qmp('query-named-block-nodes') 403 for x in result["return"]: 404 if check_equal_or_none(x.get("node-name"), node_name) and \ 405 check_equal_or_none(x.get("file"), file_name): 406 return 407 self.assertTrue(False, "Cannot find %s %s in result:\n%s" % \ 408 (node_name, file_name, result)) 409 410 def assert_json_filename_equal(self, json_filename, reference): 411 '''Asserts that the given filename is a json: filename and that its 412 content is equal to the given reference object''' 413 self.assertEqual(json_filename[:5], 'json:') 414 self.assertEqual(self.flatten_qmp_object(json.loads(json_filename[5:])), 415 self.flatten_qmp_object(reference)) 416 417 def cancel_and_wait(self, drive='drive0', force=False, resume=False): 418 '''Cancel a block job and wait for it to finish, returning the event''' 419 result = self.vm.qmp('block-job-cancel', device=drive, force=force) 420 self.assert_qmp(result, 'return', {}) 421 422 if resume: 423 self.vm.resume_drive(drive) 424 425 cancelled = False 426 result = None 427 while not cancelled: 428 for event in self.vm.get_qmp_events(wait=True): 429 if event['event'] == 'BLOCK_JOB_COMPLETED' or \ 430 event['event'] == 'BLOCK_JOB_CANCELLED': 431 self.assert_qmp(event, 'data/device', drive) 432 result = event 433 cancelled = True 434 435 self.assert_no_active_block_jobs() 436 return result 437 438 def wait_until_completed(self, drive='drive0', check_offset=True): 439 '''Wait for a block job to finish, returning the event''' 440 completed = False 441 while not completed: 442 for event in self.vm.get_qmp_events(wait=True): 443 if event['event'] == 'BLOCK_JOB_COMPLETED': 444 self.assert_qmp(event, 'data/device', drive) 445 self.assert_qmp_absent(event, 'data/error') 446 if check_offset: 447 self.assert_qmp(event, 'data/offset', event['data']['len']) 448 completed = True 449 450 self.assert_no_active_block_jobs() 451 return event 452 453 def wait_ready(self, drive='drive0'): 454 '''Wait until a block job BLOCK_JOB_READY event''' 455 f = {'data': {'type': 'mirror', 'device': drive } } 456 event = self.vm.event_wait(name='BLOCK_JOB_READY', match=f) 457 458 def wait_ready_and_cancel(self, drive='drive0'): 459 self.wait_ready(drive=drive) 460 event = self.cancel_and_wait(drive=drive) 461 self.assertEquals(event['event'], 'BLOCK_JOB_COMPLETED') 462 self.assert_qmp(event, 'data/type', 'mirror') 463 self.assert_qmp(event, 'data/offset', event['data']['len']) 464 465 def complete_and_wait(self, drive='drive0', wait_ready=True): 466 '''Complete a block job and wait for it to finish''' 467 if wait_ready: 468 self.wait_ready(drive=drive) 469 470 result = self.vm.qmp('block-job-complete', device=drive) 471 self.assert_qmp(result, 'return', {}) 472 473 event = self.wait_until_completed(drive=drive) 474 self.assert_qmp(event, 'data/type', 'mirror') 475 476 def pause_job(self, job_id='job0'): 477 result = self.vm.qmp('block-job-pause', device=job_id) 478 self.assert_qmp(result, 'return', {}) 479 480 with Timeout(1, "Timeout waiting for job to pause"): 481 while True: 482 result = self.vm.qmp('query-block-jobs') 483 for job in result['return']: 484 if job['device'] == job_id and job['paused'] == True and job['busy'] == False: 485 return job 486 487 488def notrun(reason): 489 '''Skip this test suite''' 490 # Each test in qemu-iotests has a number ("seq") 491 seq = os.path.basename(sys.argv[0]) 492 493 open('%s/%s.notrun' % (output_dir, seq), 'wb').write(reason + '\n') 494 print '%s not run: %s' % (seq, reason) 495 sys.exit(0) 496 497def verify_image_format(supported_fmts=[], unsupported_fmts=[]): 498 if supported_fmts and (imgfmt not in supported_fmts): 499 notrun('not suitable for this image format: %s' % imgfmt) 500 if unsupported_fmts and (imgfmt in unsupported_fmts): 501 notrun('not suitable for this image format: %s' % imgfmt) 502 503def verify_platform(supported_oses=['linux']): 504 if True not in [sys.platform.startswith(x) for x in supported_oses]: 505 notrun('not suitable for this OS: %s' % sys.platform) 506 507def supports_quorum(): 508 return 'quorum' in qemu_img_pipe('--help') 509 510def verify_quorum(): 511 '''Skip test suite if quorum support is not available''' 512 if not supports_quorum(): 513 notrun('quorum support missing') 514 515def main(supported_fmts=[], supported_oses=['linux']): 516 '''Run tests''' 517 518 global debug 519 520 # We are using TEST_DIR and QEMU_DEFAULT_MACHINE as proxies to 521 # indicate that we're not being run via "check". There may be 522 # other things set up by "check" that individual test cases rely 523 # on. 524 if test_dir is None or qemu_default_machine is None: 525 sys.stderr.write('Please run this test via the "check" script\n') 526 sys.exit(os.EX_USAGE) 527 528 debug = '-d' in sys.argv 529 verbosity = 1 530 verify_image_format(supported_fmts) 531 verify_platform(supported_oses) 532 533 # We need to filter out the time taken from the output so that qemu-iotest 534 # can reliably diff the results against master output. 535 import StringIO 536 if debug: 537 output = sys.stdout 538 verbosity = 2 539 sys.argv.remove('-d') 540 else: 541 output = StringIO.StringIO() 542 543 logging.basicConfig(level=(logging.DEBUG if debug else logging.WARN)) 544 545 class MyTestRunner(unittest.TextTestRunner): 546 def __init__(self, stream=output, descriptions=True, verbosity=verbosity): 547 unittest.TextTestRunner.__init__(self, stream, descriptions, verbosity) 548 549 # unittest.main() will use sys.exit() so expect a SystemExit exception 550 try: 551 unittest.main(testRunner=MyTestRunner) 552 finally: 553 if not debug: 554 sys.stderr.write(re.sub(r'Ran (\d+) tests? in [\d.]+s', r'Ran \1 tests', output.getvalue())) 555