3 # Tests for image streaming.
5 # Copyright (C) 2012 IBM Corp.
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.
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.
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/>.
25 from iotests import qemu_img, qemu_io
27 backing_img = os.path.join(iotests.test_dir, 'backing.img')
28 mid_img = os.path.join(iotests.test_dir, 'mid.img')
29 test_img = os.path.join(iotests.test_dir, 'test.img')
31 class TestSingleDrive(iotests.QMPTestCase):
32 image_len = 1 * 1024 * 1024 # MB
35 iotests.create_image(backing_img, TestSingleDrive.image_len)
36 qemu_img('create', '-f', iotests.imgfmt,
37 '-o', 'backing_file=%s' % backing_img,
39 qemu_img('create', '-f', iotests.imgfmt,
40 '-o', 'backing_file=%s' % mid_img,
41 '-F', iotests.imgfmt, test_img)
42 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 512', backing_img)
43 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 524288 512', mid_img)
44 self.vm = iotests.VM().add_drive("blkdebug::" + test_img,
45 "backing.node-name=mid," +
46 "backing.backing.node-name=base")
53 os.remove(backing_img)
55 def test_stream(self):
56 self.assert_no_active_block_jobs()
58 result = self.vm.qmp('block-stream', device='drive0')
59 self.assert_qmp(result, 'return', {})
61 self.wait_until_completed()
63 self.assert_no_active_block_jobs()
66 self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img),
67 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
68 'image file map does not match backing file after streaming')
70 def test_stream_intermediate(self):
71 self.assert_no_active_block_jobs()
73 self.assertNotEqual(qemu_io('-f', 'raw', '-rU', '-c', 'map', backing_img),
74 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', mid_img),
75 'image file map matches backing file before streaming')
77 result = self.vm.qmp('block-stream', device='mid', job_id='stream-mid')
78 self.assert_qmp(result, 'return', {})
80 self.wait_until_completed(drive='stream-mid')
82 self.assert_no_active_block_jobs()
85 self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img),
86 qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img),
87 'image file map does not match backing file after streaming')
89 def test_stream_pause(self):
90 self.assert_no_active_block_jobs()
92 self.vm.pause_drive('drive0')
93 result = self.vm.qmp('block-stream', device='drive0')
94 self.assert_qmp(result, 'return', {})
96 self.pause_job('drive0', wait=False)
97 self.vm.resume_drive('drive0')
98 self.pause_wait('drive0')
100 result = self.vm.qmp('query-block-jobs')
101 offset = self.dictpath(result, 'return[0]/offset')
104 result = self.vm.qmp('query-block-jobs')
105 self.assert_qmp(result, 'return[0]/offset', offset)
107 result = self.vm.qmp('block-job-resume', device='drive0')
108 self.assert_qmp(result, 'return', {})
110 self.wait_until_completed()
112 self.assert_no_active_block_jobs()
115 self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img),
116 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
117 'image file map does not match backing file after streaming')
119 def test_stream_no_op(self):
120 self.assert_no_active_block_jobs()
122 # The image map is empty before the operation
123 empty_map = qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', test_img)
125 # This is a no-op: no data should ever be copied from the base image
126 result = self.vm.qmp('block-stream', device='drive0', base=mid_img)
127 self.assert_qmp(result, 'return', {})
129 self.wait_until_completed()
131 self.assert_no_active_block_jobs()
134 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
135 empty_map, 'image file map changed after a no-op')
137 def test_stream_partial(self):
138 self.assert_no_active_block_jobs()
140 result = self.vm.qmp('block-stream', device='drive0', base=backing_img)
141 self.assert_qmp(result, 'return', {})
143 self.wait_until_completed()
145 self.assert_no_active_block_jobs()
148 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img),
149 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
150 'image file map does not match backing file after streaming')
152 def test_device_not_found(self):
153 result = self.vm.qmp('block-stream', device='nonexistent')
154 self.assert_qmp(result, 'error/desc',
155 'Cannot find device=nonexistent nor node_name=nonexistent')
157 def test_job_id_missing(self):
158 result = self.vm.qmp('block-stream', device='mid')
159 self.assert_qmp(result, 'error/desc', "Invalid job ID ''")
161 def test_read_only(self):
162 # Create a new file that we can attach (we need a read-only top)
163 with iotests.FilePath('ro-top.img') as ro_top_path:
164 qemu_img('create', '-f', iotests.imgfmt, ro_top_path,
167 result = self.vm.qmp('blockdev-add',
169 driver=iotests.imgfmt,
173 'filename': ro_top_path,
177 self.assert_qmp(result, 'return', {})
179 result = self.vm.qmp('block-stream', job_id='stream',
180 device='ro-top', base_node='base')
181 self.assert_qmp(result, 'error/desc', 'Block node is read-only')
183 result = self.vm.qmp('blockdev-del', node_name='ro-top')
184 self.assert_qmp(result, 'return', {})
187 class TestParallelOps(iotests.QMPTestCase):
188 num_ops = 4 # Number of parallel block-stream operations
189 num_imgs = num_ops * 2 + 1
190 image_len = num_ops * 4 * 1024 * 1024
197 # Initialize file names and command-line options
198 for i in range(self.num_imgs):
199 img_depth = self.num_imgs - i - 1
200 opts.append("backing." * img_depth + "node-name=node%d" % i)
201 self.imgs.append(os.path.join(iotests.test_dir, 'img-%d.img' % i))
204 iotests.create_image(self.imgs[0], self.image_len)
205 for i in range(1, self.num_imgs):
206 qemu_img('create', '-f', iotests.imgfmt,
207 '-o', 'backing_file=%s' % self.imgs[i-1],
208 '-F', 'raw' if i == 1 else iotests.imgfmt, self.imgs[i])
210 # Put data into the images we are copying data from
211 odd_img_indexes = [x for x in reversed(range(self.num_imgs)) if x % 2 == 1]
212 for i in range(len(odd_img_indexes)):
213 # Alternate between 2MB and 4MB.
214 # This way jobs will not finish in the same order they were created
215 num_mb = 2 + 2 * (i % 2)
216 qemu_io('-f', iotests.imgfmt,
217 '-c', 'write -P 0xFF %dM %dM' % (i * 4, num_mb),
218 self.imgs[odd_img_indexes[i]])
220 # Attach the drive to the VM
221 self.vm = iotests.VM()
222 self.vm.add_drive(self.imgs[-1], ','.join(opts))
227 for img in self.imgs:
230 # Test that it's possible to run several block-stream operations
231 # in parallel in the same snapshot chain
232 @unittest.skipIf(os.environ.get('QEMU_CHECK_BLOCK_AUTO'), 'disabled in CI')
233 def test_stream_parallel(self):
234 self.assert_no_active_block_jobs()
236 # Check that the maps don't match before the streaming operations
237 for i in range(2, self.num_imgs, 2):
238 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i]),
239 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i-1]),
240 'image file map matches backing file before streaming')
242 # Create all streaming jobs
244 for i in range(2, self.num_imgs, 2):
245 node_name = 'node%d' % i
246 job_id = 'stream-%s' % node_name
247 pending_jobs.append(job_id)
248 result = self.vm.qmp('block-stream', device=node_name, job_id=job_id, base=self.imgs[i-2], speed=1024)
249 self.assert_qmp(result, 'return', {})
251 for job in pending_jobs:
252 result = self.vm.qmp('block-job-set-speed', device=job, speed=0)
253 self.assert_qmp(result, 'return', {})
255 # Wait for all jobs to be finished.
256 while len(pending_jobs) > 0:
257 for event in self.vm.get_qmp_events(wait=True):
258 if event['event'] == 'BLOCK_JOB_COMPLETED':
259 job_id = self.dictpath(event, 'data/device')
260 self.assertTrue(job_id in pending_jobs)
261 self.assert_qmp_absent(event, 'data/error')
262 pending_jobs.remove(job_id)
264 self.assert_no_active_block_jobs()
267 # Check that all maps match now
268 for i in range(2, self.num_imgs, 2):
269 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i]),
270 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i-1]),
271 'image file map does not match backing file after streaming')
273 # Test that it's not possible to perform two block-stream
274 # operations if there are nodes involved in both.
275 def test_overlapping_1(self):
276 self.assert_no_active_block_jobs()
278 # Set a speed limit to make sure that this job blocks the rest
279 result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4', base=self.imgs[1], speed=1024*1024)
280 self.assert_qmp(result, 'return', {})
282 result = self.vm.qmp('block-stream', device='node5', job_id='stream-node5', base=self.imgs[2])
283 self.assert_qmp(result, 'error/desc',
284 "Node 'node4' is busy: block device is in use by block job: stream")
286 result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3', base=self.imgs[2])
287 self.assert_qmp(result, 'error/desc',
288 "Node 'node3' is busy: block device is in use by block job: stream")
290 result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4-v2')
291 self.assert_qmp(result, 'error/desc',
292 "Node 'node4' is busy: block device is in use by block job: stream")
294 # block-commit should also fail if it touches nodes used by the stream job
295 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[4], job_id='commit-node4')
296 self.assert_qmp(result, 'error/desc',
297 "Node 'node4' is busy: block device is in use by block job: stream")
299 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[1], top=self.imgs[3], job_id='commit-node1')
300 self.assert_qmp(result, 'error/desc',
301 "Node 'node3' is busy: block device is in use by block job: stream")
303 # This fails because it needs to modify the backing string in node2, which is blocked
304 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[0], top=self.imgs[1], job_id='commit-node0')
305 self.assert_qmp(result, 'error/desc',
306 "Node 'node2' is busy: block device is in use by block job: stream")
308 result = self.vm.qmp('block-job-set-speed', device='stream-node4', speed=0)
309 self.assert_qmp(result, 'return', {})
311 self.wait_until_completed(drive='stream-node4')
312 self.assert_no_active_block_jobs()
314 # Similar to test_overlapping_1, but with block-commit
315 # blocking the other jobs
316 def test_overlapping_2(self):
317 self.assertLessEqual(9, self.num_imgs)
318 self.assert_no_active_block_jobs()
320 # Set a speed limit to make sure that this job blocks the rest
321 result = self.vm.qmp('block-commit', device='drive0', top=self.imgs[5], base=self.imgs[3], job_id='commit-node3', speed=1024*1024)
322 self.assert_qmp(result, 'return', {})
324 result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3')
325 self.assert_qmp(result, 'error/desc',
326 "Node 'node3' is busy: block device is in use by block job: commit")
328 result = self.vm.qmp('block-stream', device='node6', base=self.imgs[2], job_id='stream-node6')
329 self.assert_qmp(result, 'error/desc',
330 "Node 'node5' is busy: block device is in use by block job: commit")
332 result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], job_id='stream-node4')
333 self.assert_qmp(result, 'error/desc',
334 "Node 'node4' is busy: block device is in use by block job: commit")
336 result = self.vm.qmp('block-stream', device='node6', base=self.imgs[4], job_id='stream-node6-v2')
337 self.assert_qmp(result, 'error/desc',
338 "Node 'node5' is busy: block device is in use by block job: commit")
340 # This fails because block-commit currently blocks the active layer even if it's not used
341 result = self.vm.qmp('block-stream', device='drive0', base=self.imgs[5], job_id='stream-drive0')
342 self.assert_qmp(result, 'error/desc',
343 "Node 'drive0' is busy: block device is in use by block job: commit")
345 result = self.vm.qmp('block-job-set-speed', device='commit-node3', speed=0)
346 self.assert_qmp(result, 'return', {})
348 self.wait_until_completed(drive='commit-node3')
350 # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter.
351 # Internally this uses a mirror block job, hence the separate test case.
352 def test_overlapping_3(self):
353 self.assertLessEqual(8, self.num_imgs)
354 self.assert_no_active_block_jobs()
356 # Set a speed limit to make sure that this job blocks the rest
357 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3], job_id='commit-drive0', speed=1024*1024)
358 self.assert_qmp(result, 'return', {})
360 result = self.vm.qmp('block-stream', device='node5', base=self.imgs[3], job_id='stream-node6')
361 self.assert_qmp(result, 'error/desc',
362 "Node 'node5' is busy: block device is in use by block job: commit")
364 result = self.vm.qmp('block-job-set-speed', device='commit-drive0', speed=0)
365 self.assert_qmp(result, 'return', {})
367 event = self.vm.event_wait(name='BLOCK_JOB_READY')
368 self.assert_qmp(event, 'data/device', 'commit-drive0')
369 self.assert_qmp(event, 'data/type', 'commit')
370 self.assert_qmp_absent(event, 'data/error')
372 result = self.vm.qmp('block-job-complete', device='commit-drive0')
373 self.assert_qmp(result, 'return', {})
375 self.wait_until_completed(drive='commit-drive0')
377 # In this case the base node of the stream job is the same as the
378 # top node of commit job. Since this results in the commit filter
379 # node being part of the stream chain, this is not allowed.
380 def test_overlapping_4(self):
381 self.assert_no_active_block_jobs()
383 # Commit from node2 into node0
384 result = self.vm.qmp('block-commit', device='drive0',
385 top=self.imgs[2], base=self.imgs[0],
386 filter_node_name='commit-filter', speed=1024*1024)
387 self.assert_qmp(result, 'return', {})
389 # Stream from node2 into node4
390 result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='node4')
391 self.assert_qmp(result, 'error/desc',
392 "Cannot freeze 'backing' link to 'commit-filter'")
394 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=0)
395 self.assert_qmp(result, 'return', {})
397 self.wait_until_completed()
398 self.assert_no_active_block_jobs()
400 # In this case the base node of the stream job is the commit job's
401 # filter node. stream does not have a real dependency on its base
402 # node, so even though commit removes it when it is done, there is
404 def test_overlapping_5(self):
405 self.assert_no_active_block_jobs()
407 # Commit from node2 into node0
408 result = self.vm.qmp('block-commit', device='drive0',
409 top_node='node2', base_node='node0',
410 filter_node_name='commit-filter', speed=1024*1024)
411 self.assert_qmp(result, 'return', {})
413 # Stream from node2 into node4
414 result = self.vm.qmp('block-stream', device='node4',
415 base_node='commit-filter', job_id='node4')
416 self.assert_qmp(result, 'return', {})
418 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=0)
419 self.assert_qmp(result, 'return', {})
421 self.vm.run_job(job='drive0', auto_dismiss=True)
422 self.vm.run_job(job='node4', auto_dismiss=True)
423 self.assert_no_active_block_jobs()
425 # Test a block-stream and a block-commit job in parallel
426 # Here the stream job is supposed to finish quickly in order to reproduce
427 # the scenario that triggers the bug fixed in 3d5d319e1221 and 1a63a907507
428 def test_stream_commit_1(self):
429 self.assertLessEqual(8, self.num_imgs)
430 self.assert_no_active_block_jobs()
432 # Stream from node0 into node2
433 result = self.vm.qmp('block-stream', device='node2', base_node='node0', job_id='node2')
434 self.assert_qmp(result, 'return', {})
436 # Commit from the active layer into node3
437 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3])
438 self.assert_qmp(result, 'return', {})
440 # Wait for all jobs to be finished.
441 pending_jobs = ['node2', 'drive0']
442 while len(pending_jobs) > 0:
443 for event in self.vm.get_qmp_events(wait=True):
444 if event['event'] == 'BLOCK_JOB_COMPLETED':
445 node_name = self.dictpath(event, 'data/device')
446 self.assertTrue(node_name in pending_jobs)
447 self.assert_qmp_absent(event, 'data/error')
448 pending_jobs.remove(node_name)
449 if event['event'] == 'BLOCK_JOB_READY':
450 self.assert_qmp(event, 'data/device', 'drive0')
451 self.assert_qmp(event, 'data/type', 'commit')
452 self.assert_qmp_absent(event, 'data/error')
453 self.assertTrue('drive0' in pending_jobs)
454 self.vm.qmp('block-job-complete', device='drive0')
456 self.assert_no_active_block_jobs()
458 # This is similar to test_stream_commit_1 but both jobs are slowed
459 # down so they can run in parallel for a little while.
460 def test_stream_commit_2(self):
461 self.assertLessEqual(8, self.num_imgs)
462 self.assert_no_active_block_jobs()
464 # Stream from node0 into node4
465 result = self.vm.qmp('block-stream', device='node4', base_node='node0', job_id='node4', speed=1024*1024)
466 self.assert_qmp(result, 'return', {})
468 # Commit from the active layer into node5
469 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[5], speed=1024*1024)
470 self.assert_qmp(result, 'return', {})
472 for job in ['drive0', 'node4']:
473 result = self.vm.qmp('block-job-set-speed', device=job, speed=0)
474 self.assert_qmp(result, 'return', {})
476 # Wait for all jobs to be finished.
477 pending_jobs = ['node4', 'drive0']
478 while len(pending_jobs) > 0:
479 for event in self.vm.get_qmp_events(wait=True):
480 if event['event'] == 'BLOCK_JOB_COMPLETED':
481 node_name = self.dictpath(event, 'data/device')
482 self.assertTrue(node_name in pending_jobs)
483 self.assert_qmp_absent(event, 'data/error')
484 pending_jobs.remove(node_name)
485 if event['event'] == 'BLOCK_JOB_READY':
486 self.assert_qmp(event, 'data/device', 'drive0')
487 self.assert_qmp(event, 'data/type', 'commit')
488 self.assert_qmp_absent(event, 'data/error')
489 self.assertTrue('drive0' in pending_jobs)
490 self.vm.qmp('block-job-complete', device='drive0')
492 self.assert_no_active_block_jobs()
494 # Test the base_node parameter
495 def test_stream_base_node_name(self):
496 self.assert_no_active_block_jobs()
498 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]),
499 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]),
500 'image file map matches backing file before streaming')
502 # Error: the base node does not exist
503 result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream')
504 self.assert_qmp(result, 'error/desc',
505 'Cannot find device= nor node_name=none')
507 # Error: the base node is not a backing file of the top node
508 result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream')
509 self.assert_qmp(result, 'error/desc',
510 "Node 'node6' is not a backing image of 'node4'")
512 # Error: the base node is the same as the top node
513 result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream')
514 self.assert_qmp(result, 'error/desc',
515 "Node 'node4' is not a backing image of 'node4'")
517 # Error: cannot specify 'base' and 'base-node' at the same time
518 result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream')
519 self.assert_qmp(result, 'error/desc',
520 "'base' and 'base-node' cannot be specified at the same time")
522 # Success: the base node is a backing file of the top node
523 result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream')
524 self.assert_qmp(result, 'return', {})
526 self.wait_until_completed(drive='stream')
528 self.assert_no_active_block_jobs()
531 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]),
532 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]),
533 'image file map matches backing file after streaming')
535 class TestQuorum(iotests.QMPTestCase):
540 @iotests.skip_if_unsupported(['quorum'])
542 opts = ['driver=quorum', 'vote-threshold=2']
544 # Initialize file names and command-line options
545 for i in range(self.num_children):
546 child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i)
547 backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i)
548 self.children.append(child_img)
549 self.backing.append(backing_img)
550 qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
551 qemu_io('-f', iotests.imgfmt,
552 '-c', 'write -P 0x55 0 1024', backing_img)
553 qemu_img('create', '-f', iotests.imgfmt,
554 '-o', 'backing_file=%s' % backing_img,
555 '-F', iotests.imgfmt, child_img)
556 opts.append("children.%d.file.filename=%s" % (i, child_img))
557 opts.append("children.%d.node-name=node%d" % (i, i))
559 # Attach the drive to the VM
560 self.vm = iotests.VM()
561 self.vm.add_drive(path = None, opts = ','.join(opts))
566 for img in self.children:
568 for img in self.backing:
571 def test_stream_quorum(self):
572 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]),
573 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]),
574 'image file map matches backing file before streaming')
576 self.assert_no_active_block_jobs()
578 result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0')
579 self.assert_qmp(result, 'return', {})
581 self.wait_until_completed(drive='stream-node0')
583 self.assert_no_active_block_jobs()
586 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]),
587 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]),
588 'image file map does not match backing file after streaming')
590 class TestSmallerBackingFile(iotests.QMPTestCase):
591 backing_len = 1 * 1024 * 1024 # MB
592 image_len = 2 * backing_len
595 iotests.create_image(backing_img, self.backing_len)
596 qemu_img('create', '-f', iotests.imgfmt,
597 '-o', 'backing_file=%s' % backing_img,
598 '-F', 'raw', test_img, str(self.image_len))
599 self.vm = iotests.VM().add_drive(test_img)
602 # If this hangs, then you are missing a fix to complete streaming when the
603 # end of the backing file is reached.
604 def test_stream(self):
605 self.assert_no_active_block_jobs()
607 result = self.vm.qmp('block-stream', device='drive0')
608 self.assert_qmp(result, 'return', {})
610 self.wait_until_completed()
612 self.assert_no_active_block_jobs()
615 class TestErrors(iotests.QMPTestCase):
616 image_len = 2 * 1024 * 1024 # MB
618 # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
619 STREAM_BUFFER_SIZE = 512 * 1024
621 def create_blkdebug_file(self, name, event, errno):
622 file = open(name, 'w')
641 ''' % (event, errno, self.STREAM_BUFFER_SIZE // 512, event, event))
644 class TestEIO(TestErrors):
646 self.blkdebug_file = backing_img + ".blkdebug"
647 iotests.create_image(backing_img, TestErrors.image_len)
648 self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
649 qemu_img('create', '-f', iotests.imgfmt,
650 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
651 % (self.blkdebug_file, backing_img),
653 self.vm = iotests.VM().add_drive(test_img)
659 os.remove(backing_img)
660 os.remove(self.blkdebug_file)
662 def test_report(self):
663 self.assert_no_active_block_jobs()
665 result = self.vm.qmp('block-stream', device='drive0')
666 self.assert_qmp(result, 'return', {})
671 for event in self.vm.get_qmp_events(wait=True):
672 if event['event'] == 'BLOCK_JOB_ERROR':
673 self.assert_qmp(event, 'data/device', 'drive0')
674 self.assert_qmp(event, 'data/operation', 'read')
676 elif event['event'] == 'BLOCK_JOB_COMPLETED':
677 self.assertTrue(error, 'job completed unexpectedly')
678 self.assert_qmp(event, 'data/type', 'stream')
679 self.assert_qmp(event, 'data/device', 'drive0')
680 self.assert_qmp(event, 'data/error', 'Input/output error')
681 self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
682 self.assert_qmp(event, 'data/len', self.image_len)
684 elif event['event'] == 'JOB_STATUS_CHANGE':
685 self.assert_qmp(event, 'data/id', 'drive0')
687 self.assert_no_active_block_jobs()
690 def test_ignore(self):
691 self.assert_no_active_block_jobs()
693 result = self.vm.qmp('block-stream', device='drive0', on_error='ignore')
694 self.assert_qmp(result, 'return', {})
699 for event in self.vm.get_qmp_events(wait=True):
700 if event['event'] == 'BLOCK_JOB_ERROR':
702 self.assert_qmp(event, 'data/device', 'drive0')
703 self.assert_qmp(event, 'data/operation', 'read')
704 result = self.vm.qmp('query-block-jobs')
705 if result == {'return': []}:
706 # Job finished too quickly
708 self.assert_qmp(result, 'return[0]/paused', False)
709 elif event['event'] == 'BLOCK_JOB_COMPLETED':
710 self.assertTrue(error, 'job completed unexpectedly')
711 self.assert_qmp(event, 'data/type', 'stream')
712 self.assert_qmp(event, 'data/device', 'drive0')
713 self.assert_qmp(event, 'data/error', 'Input/output error')
714 self.assert_qmp(event, 'data/offset', self.image_len)
715 self.assert_qmp(event, 'data/len', self.image_len)
717 elif event['event'] == 'JOB_STATUS_CHANGE':
718 self.assert_qmp(event, 'data/id', 'drive0')
720 self.assert_no_active_block_jobs()
724 self.assert_no_active_block_jobs()
726 result = self.vm.qmp('block-stream', device='drive0', on_error='stop')
727 self.assert_qmp(result, 'return', {})
732 for event in self.vm.get_qmp_events(wait=True):
733 if event['event'] == 'BLOCK_JOB_ERROR':
735 self.assert_qmp(event, 'data/device', 'drive0')
736 self.assert_qmp(event, 'data/operation', 'read')
738 result = self.vm.qmp('query-block-jobs')
739 self.assert_qmp(result, 'return[0]/paused', True)
740 self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
741 self.assert_qmp(result, 'return[0]/io-status', 'failed')
743 result = self.vm.qmp('block-job-resume', device='drive0')
744 self.assert_qmp(result, 'return', {})
746 result = self.vm.qmp('query-block-jobs')
747 if result == {'return': []}:
748 # Race; likely already finished. Check.
750 self.assert_qmp(result, 'return[0]/paused', False)
751 self.assert_qmp(result, 'return[0]/io-status', 'ok')
752 elif event['event'] == 'BLOCK_JOB_COMPLETED':
753 self.assertTrue(error, 'job completed unexpectedly')
754 self.assert_qmp(event, 'data/type', 'stream')
755 self.assert_qmp(event, 'data/device', 'drive0')
756 self.assert_qmp_absent(event, 'data/error')
757 self.assert_qmp(event, 'data/offset', self.image_len)
758 self.assert_qmp(event, 'data/len', self.image_len)
760 elif event['event'] == 'JOB_STATUS_CHANGE':
761 self.assert_qmp(event, 'data/id', 'drive0')
763 self.assert_no_active_block_jobs()
766 def test_enospc(self):
767 self.assert_no_active_block_jobs()
769 result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
770 self.assert_qmp(result, 'return', {})
775 for event in self.vm.get_qmp_events(wait=True):
776 if event['event'] == 'BLOCK_JOB_ERROR':
777 self.assert_qmp(event, 'data/device', 'drive0')
778 self.assert_qmp(event, 'data/operation', 'read')
780 elif event['event'] == 'BLOCK_JOB_COMPLETED':
781 self.assertTrue(error, 'job completed unexpectedly')
782 self.assert_qmp(event, 'data/type', 'stream')
783 self.assert_qmp(event, 'data/device', 'drive0')
784 self.assert_qmp(event, 'data/error', 'Input/output error')
785 self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
786 self.assert_qmp(event, 'data/len', self.image_len)
788 elif event['event'] == 'JOB_STATUS_CHANGE':
789 self.assert_qmp(event, 'data/id', 'drive0')
791 self.assert_no_active_block_jobs()
794 class TestENOSPC(TestErrors):
796 self.blkdebug_file = backing_img + ".blkdebug"
797 iotests.create_image(backing_img, TestErrors.image_len)
798 self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28)
799 qemu_img('create', '-f', iotests.imgfmt,
800 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
801 % (self.blkdebug_file, backing_img),
803 self.vm = iotests.VM().add_drive(test_img)
809 os.remove(backing_img)
810 os.remove(self.blkdebug_file)
812 def test_enospc(self):
813 self.assert_no_active_block_jobs()
815 result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
816 self.assert_qmp(result, 'return', {})
821 for event in self.vm.get_qmp_events(wait=True):
822 if event['event'] == 'BLOCK_JOB_ERROR':
823 self.assert_qmp(event, 'data/device', 'drive0')
824 self.assert_qmp(event, 'data/operation', 'read')
827 result = self.vm.qmp('query-block-jobs')
828 self.assert_qmp(result, 'return[0]/paused', True)
829 self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
830 self.assert_qmp(result, 'return[0]/io-status', 'nospace')
832 result = self.vm.qmp('block-job-resume', device='drive0')
833 self.assert_qmp(result, 'return', {})
835 result = self.vm.qmp('query-block-jobs')
836 if result == {'return': []}:
837 # Race; likely already finished. Check.
839 self.assert_qmp(result, 'return[0]/paused', False)
840 self.assert_qmp(result, 'return[0]/io-status', 'ok')
841 elif event['event'] == 'BLOCK_JOB_COMPLETED':
842 self.assertTrue(error, 'job completed unexpectedly')
843 self.assert_qmp(event, 'data/type', 'stream')
844 self.assert_qmp(event, 'data/device', 'drive0')
845 self.assert_qmp_absent(event, 'data/error')
846 self.assert_qmp(event, 'data/offset', self.image_len)
847 self.assert_qmp(event, 'data/len', self.image_len)
849 elif event['event'] == 'JOB_STATUS_CHANGE':
850 self.assert_qmp(event, 'data/id', 'drive0')
852 self.assert_no_active_block_jobs()
855 class TestStreamStop(iotests.QMPTestCase):
856 image_len = 8 * 1024 * 1024 * 1024 # GB
859 qemu_img('create', backing_img, str(TestStreamStop.image_len))
860 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
861 qemu_img('create', '-f', iotests.imgfmt,
862 '-o', 'backing_file=%s' % backing_img,
863 '-F', 'raw', test_img)
864 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
865 self.vm = iotests.VM().add_drive("blkdebug::" + test_img)
871 os.remove(backing_img)
873 def test_stream_stop(self):
874 self.assert_no_active_block_jobs()
876 self.vm.pause_drive('drive0')
877 result = self.vm.qmp('block-stream', device='drive0')
878 self.assert_qmp(result, 'return', {})
881 events = self.vm.get_qmp_events(wait=False)
883 self.assert_qmp(e, 'event', 'JOB_STATUS_CHANGE')
884 self.assert_qmp(e, 'data/id', 'drive0')
886 self.cancel_and_wait(resume=True)
888 class TestSetSpeed(iotests.QMPTestCase):
889 image_len = 80 * 1024 * 1024 # MB
892 qemu_img('create', backing_img, str(TestSetSpeed.image_len))
893 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
894 qemu_img('create', '-f', iotests.imgfmt,
895 '-o', 'backing_file=%s' % backing_img,
896 '-F', 'raw', test_img)
897 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
898 self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
904 os.remove(backing_img)
906 # This is a short performance test which is not run by default.
907 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
908 def perf_test_throughput(self):
909 self.assert_no_active_block_jobs()
911 result = self.vm.qmp('block-stream', device='drive0')
912 self.assert_qmp(result, 'return', {})
914 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
915 self.assert_qmp(result, 'return', {})
917 self.wait_until_completed()
919 self.assert_no_active_block_jobs()
921 def test_set_speed(self):
922 self.assert_no_active_block_jobs()
924 self.vm.pause_drive('drive0')
925 result = self.vm.qmp('block-stream', device='drive0')
926 self.assert_qmp(result, 'return', {})
929 result = self.vm.qmp('query-block-jobs')
930 self.assert_qmp(result, 'return[0]/device', 'drive0')
931 self.assert_qmp(result, 'return[0]/speed', 0)
933 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
934 self.assert_qmp(result, 'return', {})
936 # Ensure the speed we set was accepted
937 result = self.vm.qmp('query-block-jobs')
938 self.assert_qmp(result, 'return[0]/device', 'drive0')
939 self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
941 self.cancel_and_wait(resume=True)
942 self.vm.pause_drive('drive0')
944 # Check setting speed in block-stream works
945 result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024)
946 self.assert_qmp(result, 'return', {})
948 result = self.vm.qmp('query-block-jobs')
949 self.assert_qmp(result, 'return[0]/device', 'drive0')
950 self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
952 self.cancel_and_wait(resume=True)
954 def test_set_speed_invalid(self):
955 self.assert_no_active_block_jobs()
957 result = self.vm.qmp('block-stream', device='drive0', speed=-1)
958 self.assert_qmp(result, 'error/desc', "Parameter 'speed' expects a non-negative value")
960 self.assert_no_active_block_jobs()
962 self.vm.pause_drive('drive0')
963 result = self.vm.qmp('block-stream', device='drive0')
964 self.assert_qmp(result, 'return', {})
966 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
967 self.assert_qmp(result, 'error/desc', "Parameter 'speed' expects a non-negative value")
969 self.cancel_and_wait(resume=True)
971 if __name__ == '__main__':
972 iotests.main(supported_fmts=['qcow2', 'qed'],
973 supported_protocols=['file'])