1#!/usr/bin/env python 2# 3# Tests for image streaming. 4# 5# Copyright (C) 2012 IBM Corp. 6# 7# This program is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 2 of the License, or 10# (at your option) any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program. If not, see <http://www.gnu.org/licenses/>. 19# 20 21import time 22import os 23import iotests 24from iotests import qemu_img, qemu_io 25 26backing_img = os.path.join(iotests.test_dir, 'backing.img') 27mid_img = os.path.join(iotests.test_dir, 'mid.img') 28test_img = os.path.join(iotests.test_dir, 'test.img') 29 30class TestSingleDrive(iotests.QMPTestCase): 31 image_len = 1 * 1024 * 1024 # MB 32 33 def setUp(self): 34 iotests.create_image(backing_img, TestSingleDrive.image_len) 35 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 36 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 37 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 512', backing_img) 38 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 524288 512', mid_img) 39 self.vm = iotests.VM().add_drive("blkdebug::" + test_img, "backing.node-name=mid") 40 self.vm.launch() 41 42 def tearDown(self): 43 self.vm.shutdown() 44 os.remove(test_img) 45 os.remove(mid_img) 46 os.remove(backing_img) 47 48 def test_stream(self): 49 self.assert_no_active_block_jobs() 50 51 result = self.vm.qmp('block-stream', device='drive0') 52 self.assert_qmp(result, 'return', {}) 53 54 self.wait_until_completed() 55 56 self.assert_no_active_block_jobs() 57 self.vm.shutdown() 58 59 self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img), 60 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 61 'image file map does not match backing file after streaming') 62 63 def test_stream_intermediate(self): 64 self.assert_no_active_block_jobs() 65 66 self.assertNotEqual(qemu_io('-f', 'raw', '-rU', '-c', 'map', backing_img), 67 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', mid_img), 68 'image file map matches backing file before streaming') 69 70 result = self.vm.qmp('block-stream', device='mid', job_id='stream-mid') 71 self.assert_qmp(result, 'return', {}) 72 73 self.wait_until_completed(drive='stream-mid') 74 75 self.assert_no_active_block_jobs() 76 self.vm.shutdown() 77 78 self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img), 79 qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img), 80 'image file map does not match backing file after streaming') 81 82 def test_stream_pause(self): 83 self.assert_no_active_block_jobs() 84 85 self.vm.pause_drive('drive0') 86 result = self.vm.qmp('block-stream', device='drive0') 87 self.assert_qmp(result, 'return', {}) 88 89 result = self.vm.qmp('block-job-pause', device='drive0') 90 self.assert_qmp(result, 'return', {}) 91 92 time.sleep(1) 93 result = self.vm.qmp('query-block-jobs') 94 offset = self.dictpath(result, 'return[0]/offset') 95 96 time.sleep(1) 97 result = self.vm.qmp('query-block-jobs') 98 self.assert_qmp(result, 'return[0]/offset', offset) 99 100 result = self.vm.qmp('block-job-resume', device='drive0') 101 self.assert_qmp(result, 'return', {}) 102 103 self.vm.resume_drive('drive0') 104 self.wait_until_completed() 105 106 self.assert_no_active_block_jobs() 107 self.vm.shutdown() 108 109 self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img), 110 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 111 'image file map does not match backing file after streaming') 112 113 def test_stream_no_op(self): 114 self.assert_no_active_block_jobs() 115 116 # The image map is empty before the operation 117 empty_map = qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', test_img) 118 119 # This is a no-op: no data should ever be copied from the base image 120 result = self.vm.qmp('block-stream', device='drive0', base=mid_img) 121 self.assert_qmp(result, 'return', {}) 122 123 self.wait_until_completed() 124 125 self.assert_no_active_block_jobs() 126 self.vm.shutdown() 127 128 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 129 empty_map, 'image file map changed after a no-op') 130 131 def test_stream_partial(self): 132 self.assert_no_active_block_jobs() 133 134 result = self.vm.qmp('block-stream', device='drive0', base=backing_img) 135 self.assert_qmp(result, 'return', {}) 136 137 self.wait_until_completed() 138 139 self.assert_no_active_block_jobs() 140 self.vm.shutdown() 141 142 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img), 143 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 144 'image file map does not match backing file after streaming') 145 146 def test_device_not_found(self): 147 result = self.vm.qmp('block-stream', device='nonexistent') 148 self.assert_qmp(result, 'error/class', 'GenericError') 149 150 def test_job_id_missing(self): 151 result = self.vm.qmp('block-stream', device='mid') 152 self.assert_qmp(result, 'error/class', 'GenericError') 153 154 155class TestParallelOps(iotests.QMPTestCase): 156 num_ops = 4 # Number of parallel block-stream operations 157 num_imgs = num_ops * 2 + 1 158 image_len = num_ops * 1024 * 1024 159 imgs = [] 160 161 def setUp(self): 162 opts = [] 163 self.imgs = [] 164 165 # Initialize file names and command-line options 166 for i in range(self.num_imgs): 167 img_depth = self.num_imgs - i - 1 168 opts.append("backing." * img_depth + "node-name=node%d" % i) 169 self.imgs.append(os.path.join(iotests.test_dir, 'img-%d.img' % i)) 170 171 # Create all images 172 iotests.create_image(self.imgs[0], self.image_len) 173 for i in range(1, self.num_imgs): 174 qemu_img('create', '-f', iotests.imgfmt, 175 '-o', 'backing_file=%s' % self.imgs[i-1], self.imgs[i]) 176 177 # Put data into the images we are copying data from 178 for i in range(self.num_imgs / 2): 179 img_index = i * 2 + 1 180 # Alternate between 512k and 1M. 181 # This way jobs will not finish in the same order they were created 182 num_kb = 512 + 512 * (i % 2) 183 qemu_io('-f', iotests.imgfmt, 184 '-c', 'write -P %d %d %d' % (i, i*1024*1024, num_kb * 1024), 185 self.imgs[img_index]) 186 187 # Attach the drive to the VM 188 self.vm = iotests.VM() 189 self.vm.add_drive(self.imgs[-1], ','.join(opts)) 190 self.vm.launch() 191 192 def tearDown(self): 193 self.vm.shutdown() 194 for img in self.imgs: 195 os.remove(img) 196 197 # Test that it's possible to run several block-stream operations 198 # in parallel in the same snapshot chain 199 def test_stream_parallel(self): 200 self.assert_no_active_block_jobs() 201 202 # Check that the maps don't match before the streaming operations 203 for i in range(2, self.num_imgs, 2): 204 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i]), 205 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i-1]), 206 'image file map matches backing file before streaming') 207 208 # Create all streaming jobs 209 pending_jobs = [] 210 for i in range(2, self.num_imgs, 2): 211 node_name = 'node%d' % i 212 job_id = 'stream-%s' % node_name 213 pending_jobs.append(job_id) 214 result = self.vm.qmp('block-stream', device=node_name, job_id=job_id, base=self.imgs[i-2], speed=512*1024) 215 self.assert_qmp(result, 'return', {}) 216 217 # Wait for all jobs to be finished. 218 while len(pending_jobs) > 0: 219 for event in self.vm.get_qmp_events(wait=True): 220 if event['event'] == 'BLOCK_JOB_COMPLETED': 221 job_id = self.dictpath(event, 'data/device') 222 self.assertTrue(job_id in pending_jobs) 223 self.assert_qmp_absent(event, 'data/error') 224 pending_jobs.remove(job_id) 225 226 self.assert_no_active_block_jobs() 227 self.vm.shutdown() 228 229 # Check that all maps match now 230 for i in range(2, self.num_imgs, 2): 231 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i]), 232 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i-1]), 233 'image file map does not match backing file after streaming') 234 235 # Test that it's not possible to perform two block-stream 236 # operations if there are nodes involved in both. 237 def test_overlapping_1(self): 238 self.assert_no_active_block_jobs() 239 240 # Set a speed limit to make sure that this job blocks the rest 241 result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4', base=self.imgs[1], speed=1024*1024) 242 self.assert_qmp(result, 'return', {}) 243 244 result = self.vm.qmp('block-stream', device='node5', job_id='stream-node5', base=self.imgs[2]) 245 self.assert_qmp(result, 'error/class', 'GenericError') 246 247 result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3', base=self.imgs[2]) 248 self.assert_qmp(result, 'error/class', 'GenericError') 249 250 result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4-v2') 251 self.assert_qmp(result, 'error/class', 'GenericError') 252 253 # block-commit should also fail if it touches nodes used by the stream job 254 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[4], job_id='commit-node4') 255 self.assert_qmp(result, 'error/class', 'GenericError') 256 257 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[1], top=self.imgs[3], job_id='commit-node1') 258 self.assert_qmp(result, 'error/class', 'GenericError') 259 260 # This fails because it needs to modify the backing string in node2, which is blocked 261 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[0], top=self.imgs[1], job_id='commit-node0') 262 self.assert_qmp(result, 'error/class', 'GenericError') 263 264 self.wait_until_completed(drive='stream-node4') 265 self.assert_no_active_block_jobs() 266 267 # Similar to test_overlapping_1, but with block-commit 268 # blocking the other jobs 269 def test_overlapping_2(self): 270 self.assertLessEqual(9, self.num_imgs) 271 self.assert_no_active_block_jobs() 272 273 # Set a speed limit to make sure that this job blocks the rest 274 result = self.vm.qmp('block-commit', device='drive0', top=self.imgs[5], base=self.imgs[3], job_id='commit-node3', speed=1024*1024) 275 self.assert_qmp(result, 'return', {}) 276 277 result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3') 278 self.assert_qmp(result, 'error/class', 'GenericError') 279 280 result = self.vm.qmp('block-stream', device='node6', base=self.imgs[2], job_id='stream-node6') 281 self.assert_qmp(result, 'error/class', 'GenericError') 282 283 result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], job_id='stream-node4') 284 self.assert_qmp(result, 'error/class', 'GenericError') 285 286 result = self.vm.qmp('block-stream', device='node6', base=self.imgs[4], job_id='stream-node6-v2') 287 self.assert_qmp(result, 'error/class', 'GenericError') 288 289 # This fails because block-commit needs to block node6, the overlay of the 'top' image 290 result = self.vm.qmp('block-stream', device='node7', base=self.imgs[5], job_id='stream-node6-v3') 291 self.assert_qmp(result, 'error/class', 'GenericError') 292 293 # This fails because block-commit currently blocks the active layer even if it's not used 294 result = self.vm.qmp('block-stream', device='drive0', base=self.imgs[5], job_id='stream-drive0') 295 self.assert_qmp(result, 'error/class', 'GenericError') 296 297 self.wait_until_completed(drive='commit-node3') 298 299 # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter. 300 # Internally this uses a mirror block job, hence the separate test case. 301 def test_overlapping_3(self): 302 self.assertLessEqual(8, self.num_imgs) 303 self.assert_no_active_block_jobs() 304 305 # Set a speed limit to make sure that this job blocks the rest 306 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3], job_id='commit-drive0', speed=1024*1024) 307 self.assert_qmp(result, 'return', {}) 308 309 result = self.vm.qmp('block-stream', device='node5', base=self.imgs[3], job_id='stream-node6') 310 self.assert_qmp(result, 'error/class', 'GenericError') 311 312 event = self.vm.get_qmp_event(wait=True) 313 self.assertEqual(event['event'], 'BLOCK_JOB_READY') 314 self.assert_qmp(event, 'data/device', 'commit-drive0') 315 self.assert_qmp(event, 'data/type', 'commit') 316 self.assert_qmp_absent(event, 'data/error') 317 318 result = self.vm.qmp('block-job-complete', device='commit-drive0') 319 self.assert_qmp(result, 'return', {}) 320 321 self.wait_until_completed(drive='commit-drive0') 322 323 # Test a block-stream and a block-commit job in parallel 324 def test_stream_commit(self): 325 self.assertLessEqual(8, self.num_imgs) 326 self.assert_no_active_block_jobs() 327 328 # Stream from node0 into node2 329 result = self.vm.qmp('block-stream', device='node2', job_id='node2') 330 self.assert_qmp(result, 'return', {}) 331 332 # Commit from the active layer into node3 333 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3]) 334 self.assert_qmp(result, 'return', {}) 335 336 # Wait for all jobs to be finished. 337 pending_jobs = ['node2', 'drive0'] 338 while len(pending_jobs) > 0: 339 for event in self.vm.get_qmp_events(wait=True): 340 if event['event'] == 'BLOCK_JOB_COMPLETED': 341 node_name = self.dictpath(event, 'data/device') 342 self.assertTrue(node_name in pending_jobs) 343 self.assert_qmp_absent(event, 'data/error') 344 pending_jobs.remove(node_name) 345 if event['event'] == 'BLOCK_JOB_READY': 346 self.assert_qmp(event, 'data/device', 'drive0') 347 self.assert_qmp(event, 'data/type', 'commit') 348 self.assert_qmp_absent(event, 'data/error') 349 self.assertTrue('drive0' in pending_jobs) 350 self.vm.qmp('block-job-complete', device='drive0') 351 352 self.assert_no_active_block_jobs() 353 354 # Test the base_node parameter 355 def test_stream_base_node_name(self): 356 self.assert_no_active_block_jobs() 357 358 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]), 359 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]), 360 'image file map matches backing file before streaming') 361 362 # Error: the base node does not exist 363 result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream') 364 self.assert_qmp(result, 'error/class', 'GenericError') 365 366 # Error: the base node is not a backing file of the top node 367 result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream') 368 self.assert_qmp(result, 'error/class', 'GenericError') 369 370 # Error: the base node is the same as the top node 371 result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream') 372 self.assert_qmp(result, 'error/class', 'GenericError') 373 374 # Error: cannot specify 'base' and 'base-node' at the same time 375 result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream') 376 self.assert_qmp(result, 'error/class', 'GenericError') 377 378 # Success: the base node is a backing file of the top node 379 result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream') 380 self.assert_qmp(result, 'return', {}) 381 382 self.wait_until_completed(drive='stream') 383 384 self.assert_no_active_block_jobs() 385 self.vm.shutdown() 386 387 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]), 388 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]), 389 'image file map matches backing file after streaming') 390 391class TestQuorum(iotests.QMPTestCase): 392 num_children = 3 393 children = [] 394 backing = [] 395 396 def setUp(self): 397 opts = ['driver=quorum', 'vote-threshold=2'] 398 399 # Initialize file names and command-line options 400 for i in range(self.num_children): 401 child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i) 402 backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i) 403 self.children.append(child_img) 404 self.backing.append(backing_img) 405 qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M') 406 qemu_io('-f', iotests.imgfmt, 407 '-c', 'write -P 0x55 0 1024', backing_img) 408 qemu_img('create', '-f', iotests.imgfmt, 409 '-o', 'backing_file=%s' % backing_img, child_img) 410 opts.append("children.%d.file.filename=%s" % (i, child_img)) 411 opts.append("children.%d.node-name=node%d" % (i, i)) 412 413 # Attach the drive to the VM 414 self.vm = iotests.VM() 415 self.vm.add_drive(path = None, opts = ','.join(opts)) 416 self.vm.launch() 417 418 def tearDown(self): 419 self.vm.shutdown() 420 for img in self.children: 421 os.remove(img) 422 for img in self.backing: 423 os.remove(img) 424 425 def test_stream_quorum(self): 426 if not iotests.supports_quorum(): 427 return 428 429 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]), 430 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]), 431 'image file map matches backing file before streaming') 432 433 self.assert_no_active_block_jobs() 434 435 result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0') 436 self.assert_qmp(result, 'return', {}) 437 438 self.wait_until_completed(drive='stream-node0') 439 440 self.assert_no_active_block_jobs() 441 self.vm.shutdown() 442 443 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]), 444 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]), 445 'image file map does not match backing file after streaming') 446 447class TestSmallerBackingFile(iotests.QMPTestCase): 448 backing_len = 1 * 1024 * 1024 # MB 449 image_len = 2 * backing_len 450 451 def setUp(self): 452 iotests.create_image(backing_img, self.backing_len) 453 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len)) 454 self.vm = iotests.VM().add_drive(test_img) 455 self.vm.launch() 456 457 # If this hangs, then you are missing a fix to complete streaming when the 458 # end of the backing file is reached. 459 def test_stream(self): 460 self.assert_no_active_block_jobs() 461 462 result = self.vm.qmp('block-stream', device='drive0') 463 self.assert_qmp(result, 'return', {}) 464 465 self.wait_until_completed() 466 467 self.assert_no_active_block_jobs() 468 self.vm.shutdown() 469 470class TestErrors(iotests.QMPTestCase): 471 image_len = 2 * 1024 * 1024 # MB 472 473 # this should match STREAM_BUFFER_SIZE/512 in block/stream.c 474 STREAM_BUFFER_SIZE = 512 * 1024 475 476 def create_blkdebug_file(self, name, event, errno): 477 file = open(name, 'w') 478 file.write(''' 479[inject-error] 480state = "1" 481event = "%s" 482errno = "%d" 483immediately = "off" 484once = "on" 485sector = "%d" 486 487[set-state] 488state = "1" 489event = "%s" 490new_state = "2" 491 492[set-state] 493state = "2" 494event = "%s" 495new_state = "1" 496''' % (event, errno, self.STREAM_BUFFER_SIZE / 512, event, event)) 497 file.close() 498 499class TestEIO(TestErrors): 500 def setUp(self): 501 self.blkdebug_file = backing_img + ".blkdebug" 502 iotests.create_image(backing_img, TestErrors.image_len) 503 self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5) 504 qemu_img('create', '-f', iotests.imgfmt, 505 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' 506 % (self.blkdebug_file, backing_img), 507 test_img) 508 self.vm = iotests.VM().add_drive(test_img) 509 self.vm.launch() 510 511 def tearDown(self): 512 self.vm.shutdown() 513 os.remove(test_img) 514 os.remove(backing_img) 515 os.remove(self.blkdebug_file) 516 517 def test_report(self): 518 self.assert_no_active_block_jobs() 519 520 result = self.vm.qmp('block-stream', device='drive0') 521 self.assert_qmp(result, 'return', {}) 522 523 completed = False 524 error = False 525 while not completed: 526 for event in self.vm.get_qmp_events(wait=True): 527 if event['event'] == 'BLOCK_JOB_ERROR': 528 self.assert_qmp(event, 'data/device', 'drive0') 529 self.assert_qmp(event, 'data/operation', 'read') 530 error = True 531 elif event['event'] == 'BLOCK_JOB_COMPLETED': 532 self.assertTrue(error, 'job completed unexpectedly') 533 self.assert_qmp(event, 'data/type', 'stream') 534 self.assert_qmp(event, 'data/device', 'drive0') 535 self.assert_qmp(event, 'data/error', 'Input/output error') 536 self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE) 537 self.assert_qmp(event, 'data/len', self.image_len) 538 completed = True 539 540 self.assert_no_active_block_jobs() 541 self.vm.shutdown() 542 543 def test_ignore(self): 544 self.assert_no_active_block_jobs() 545 546 result = self.vm.qmp('block-stream', device='drive0', on_error='ignore') 547 self.assert_qmp(result, 'return', {}) 548 549 error = False 550 completed = False 551 while not completed: 552 for event in self.vm.get_qmp_events(wait=True): 553 if event['event'] == 'BLOCK_JOB_ERROR': 554 error = True 555 self.assert_qmp(event, 'data/device', 'drive0') 556 self.assert_qmp(event, 'data/operation', 'read') 557 result = self.vm.qmp('query-block-jobs') 558 if result == {'return': []}: 559 # Job finished too quickly 560 continue 561 self.assert_qmp(result, 'return[0]/paused', False) 562 elif event['event'] == 'BLOCK_JOB_COMPLETED': 563 self.assertTrue(error, 'job completed unexpectedly') 564 self.assert_qmp(event, 'data/type', 'stream') 565 self.assert_qmp(event, 'data/device', 'drive0') 566 self.assert_qmp(event, 'data/error', 'Input/output error') 567 self.assert_qmp(event, 'data/offset', self.image_len) 568 self.assert_qmp(event, 'data/len', self.image_len) 569 completed = True 570 571 self.assert_no_active_block_jobs() 572 self.vm.shutdown() 573 574 def test_stop(self): 575 self.assert_no_active_block_jobs() 576 577 result = self.vm.qmp('block-stream', device='drive0', on_error='stop') 578 self.assert_qmp(result, 'return', {}) 579 580 error = False 581 completed = False 582 while not completed: 583 for event in self.vm.get_qmp_events(wait=True): 584 if event['event'] == 'BLOCK_JOB_ERROR': 585 error = True 586 self.assert_qmp(event, 'data/device', 'drive0') 587 self.assert_qmp(event, 'data/operation', 'read') 588 589 result = self.vm.qmp('query-block-jobs') 590 self.assert_qmp(result, 'return[0]/paused', True) 591 self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE) 592 self.assert_qmp(result, 'return[0]/io-status', 'failed') 593 594 result = self.vm.qmp('block-job-resume', device='drive0') 595 self.assert_qmp(result, 'return', {}) 596 597 result = self.vm.qmp('query-block-jobs') 598 if result == {'return': []}: 599 # Race; likely already finished. Check. 600 continue 601 self.assert_qmp(result, 'return[0]/paused', False) 602 self.assert_qmp(result, 'return[0]/io-status', 'ok') 603 elif event['event'] == 'BLOCK_JOB_COMPLETED': 604 self.assertTrue(error, 'job completed unexpectedly') 605 self.assert_qmp(event, 'data/type', 'stream') 606 self.assert_qmp(event, 'data/device', 'drive0') 607 self.assert_qmp_absent(event, 'data/error') 608 self.assert_qmp(event, 'data/offset', self.image_len) 609 self.assert_qmp(event, 'data/len', self.image_len) 610 completed = True 611 612 self.assert_no_active_block_jobs() 613 self.vm.shutdown() 614 615 def test_enospc(self): 616 self.assert_no_active_block_jobs() 617 618 result = self.vm.qmp('block-stream', device='drive0', on_error='enospc') 619 self.assert_qmp(result, 'return', {}) 620 621 completed = False 622 error = False 623 while not completed: 624 for event in self.vm.get_qmp_events(wait=True): 625 if event['event'] == 'BLOCK_JOB_ERROR': 626 self.assert_qmp(event, 'data/device', 'drive0') 627 self.assert_qmp(event, 'data/operation', 'read') 628 error = True 629 elif event['event'] == 'BLOCK_JOB_COMPLETED': 630 self.assertTrue(error, 'job completed unexpectedly') 631 self.assert_qmp(event, 'data/type', 'stream') 632 self.assert_qmp(event, 'data/device', 'drive0') 633 self.assert_qmp(event, 'data/error', 'Input/output error') 634 self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE) 635 self.assert_qmp(event, 'data/len', self.image_len) 636 completed = True 637 638 self.assert_no_active_block_jobs() 639 self.vm.shutdown() 640 641class TestENOSPC(TestErrors): 642 def setUp(self): 643 self.blkdebug_file = backing_img + ".blkdebug" 644 iotests.create_image(backing_img, TestErrors.image_len) 645 self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28) 646 qemu_img('create', '-f', iotests.imgfmt, 647 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' 648 % (self.blkdebug_file, backing_img), 649 test_img) 650 self.vm = iotests.VM().add_drive(test_img) 651 self.vm.launch() 652 653 def tearDown(self): 654 self.vm.shutdown() 655 os.remove(test_img) 656 os.remove(backing_img) 657 os.remove(self.blkdebug_file) 658 659 def test_enospc(self): 660 self.assert_no_active_block_jobs() 661 662 result = self.vm.qmp('block-stream', device='drive0', on_error='enospc') 663 self.assert_qmp(result, 'return', {}) 664 665 error = False 666 completed = False 667 while not completed: 668 for event in self.vm.get_qmp_events(wait=True): 669 if event['event'] == 'BLOCK_JOB_ERROR': 670 self.assert_qmp(event, 'data/device', 'drive0') 671 self.assert_qmp(event, 'data/operation', 'read') 672 673 result = self.vm.qmp('query-block-jobs') 674 self.assert_qmp(result, 'return[0]/paused', True) 675 self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE) 676 self.assert_qmp(result, 'return[0]/io-status', 'nospace') 677 678 result = self.vm.qmp('block-job-resume', device='drive0') 679 self.assert_qmp(result, 'return', {}) 680 681 result = self.vm.qmp('query-block-jobs') 682 self.assert_qmp(result, 'return[0]/paused', False) 683 self.assert_qmp(result, 'return[0]/io-status', 'ok') 684 error = True 685 elif event['event'] == 'BLOCK_JOB_COMPLETED': 686 self.assertTrue(error, 'job completed unexpectedly') 687 self.assert_qmp(event, 'data/type', 'stream') 688 self.assert_qmp(event, 'data/device', 'drive0') 689 self.assert_qmp_absent(event, 'data/error') 690 self.assert_qmp(event, 'data/offset', self.image_len) 691 self.assert_qmp(event, 'data/len', self.image_len) 692 completed = True 693 694 self.assert_no_active_block_jobs() 695 self.vm.shutdown() 696 697class TestStreamStop(iotests.QMPTestCase): 698 image_len = 8 * 1024 * 1024 * 1024 # GB 699 700 def setUp(self): 701 qemu_img('create', backing_img, str(TestStreamStop.image_len)) 702 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img) 703 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 704 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img) 705 self.vm = iotests.VM().add_drive("blkdebug::" + test_img) 706 self.vm.launch() 707 708 def tearDown(self): 709 self.vm.shutdown() 710 os.remove(test_img) 711 os.remove(backing_img) 712 713 def test_stream_stop(self): 714 self.assert_no_active_block_jobs() 715 716 self.vm.pause_drive('drive0') 717 result = self.vm.qmp('block-stream', device='drive0') 718 self.assert_qmp(result, 'return', {}) 719 720 time.sleep(0.1) 721 events = self.vm.get_qmp_events(wait=False) 722 self.assertEqual(events, [], 'unexpected QMP event: %s' % events) 723 724 self.cancel_and_wait(resume=True) 725 726class TestSetSpeed(iotests.QMPTestCase): 727 image_len = 80 * 1024 * 1024 # MB 728 729 def setUp(self): 730 qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 731 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img) 732 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 733 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img) 734 self.vm = iotests.VM().add_drive('blkdebug::' + test_img) 735 self.vm.launch() 736 737 def tearDown(self): 738 self.vm.shutdown() 739 os.remove(test_img) 740 os.remove(backing_img) 741 742 # This is a short performance test which is not run by default. 743 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput" 744 def perf_test_throughput(self): 745 self.assert_no_active_block_jobs() 746 747 result = self.vm.qmp('block-stream', device='drive0') 748 self.assert_qmp(result, 'return', {}) 749 750 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) 751 self.assert_qmp(result, 'return', {}) 752 753 self.wait_until_completed() 754 755 self.assert_no_active_block_jobs() 756 757 def test_set_speed(self): 758 self.assert_no_active_block_jobs() 759 760 self.vm.pause_drive('drive0') 761 result = self.vm.qmp('block-stream', device='drive0') 762 self.assert_qmp(result, 'return', {}) 763 764 # Default speed is 0 765 result = self.vm.qmp('query-block-jobs') 766 self.assert_qmp(result, 'return[0]/device', 'drive0') 767 self.assert_qmp(result, 'return[0]/speed', 0) 768 769 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) 770 self.assert_qmp(result, 'return', {}) 771 772 # Ensure the speed we set was accepted 773 result = self.vm.qmp('query-block-jobs') 774 self.assert_qmp(result, 'return[0]/device', 'drive0') 775 self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024) 776 777 self.cancel_and_wait(resume=True) 778 self.vm.pause_drive('drive0') 779 780 # Check setting speed in block-stream works 781 result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024) 782 self.assert_qmp(result, 'return', {}) 783 784 result = self.vm.qmp('query-block-jobs') 785 self.assert_qmp(result, 'return[0]/device', 'drive0') 786 self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024) 787 788 self.cancel_and_wait(resume=True) 789 790 def test_set_speed_invalid(self): 791 self.assert_no_active_block_jobs() 792 793 result = self.vm.qmp('block-stream', device='drive0', speed=-1) 794 self.assert_qmp(result, 'error/class', 'GenericError') 795 796 self.assert_no_active_block_jobs() 797 798 result = self.vm.qmp('block-stream', device='drive0') 799 self.assert_qmp(result, 'return', {}) 800 801 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1) 802 self.assert_qmp(result, 'error/class', 'GenericError') 803 804 self.cancel_and_wait() 805 806if __name__ == '__main__': 807 iotests.main(supported_fmts=['qcow2', 'qed']) 808