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