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/>.
24 from iotests
import qemu_img
, qemu_io
26 backing_img
= os
.path
.join(iotests
.test_dir
, 'backing.img')
27 mid_img
= os
.path
.join(iotests
.test_dir
, 'mid.img')
28 test_img
= os
.path
.join(iotests
.test_dir
, 'test.img')
30 class TestSingleDrive(iotests
.QMPTestCase
):
31 image_len
= 1 * 1024 * 1024 # MB
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")
46 os
.remove(backing_img
)
48 def test_stream(self
):
49 self
.assert_no_active_block_jobs()
51 result
= self
.vm
.qmp('block-stream', device
='drive0')
52 self
.assert_qmp(result
, 'return', {})
54 self
.wait_until_completed()
56 self
.assert_no_active_block_jobs()
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')
63 def test_stream_intermediate(self
):
64 self
.assert_no_active_block_jobs()
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')
70 result
= self
.vm
.qmp('block-stream', device
='mid', job_id
='stream-mid')
71 self
.assert_qmp(result
, 'return', {})
73 self
.wait_until_completed(drive
='stream-mid')
75 self
.assert_no_active_block_jobs()
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')
82 def test_stream_pause(self
):
83 self
.assert_no_active_block_jobs()
85 self
.vm
.pause_drive('drive0')
86 result
= self
.vm
.qmp('block-stream', device
='drive0')
87 self
.assert_qmp(result
, 'return', {})
89 self
.pause_job('drive0', wait
=False)
90 self
.vm
.resume_drive('drive0')
91 self
.pause_wait('drive0')
93 result
= self
.vm
.qmp('query-block-jobs')
94 offset
= self
.dictpath(result
, 'return[0]/offset')
97 result
= self
.vm
.qmp('query-block-jobs')
98 self
.assert_qmp(result
, 'return[0]/offset', offset
)
100 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
101 self
.assert_qmp(result
, 'return', {})
103 self
.wait_until_completed()
105 self
.assert_no_active_block_jobs()
108 self
.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img
),
109 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', test_img
),
110 'image file map does not match backing file after streaming')
112 def test_stream_no_op(self
):
113 self
.assert_no_active_block_jobs()
115 # The image map is empty before the operation
116 empty_map
= qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', test_img
)
118 # This is a no-op: no data should ever be copied from the base image
119 result
= self
.vm
.qmp('block-stream', device
='drive0', base
=mid_img
)
120 self
.assert_qmp(result
, 'return', {})
122 self
.wait_until_completed()
124 self
.assert_no_active_block_jobs()
127 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', test_img
),
128 empty_map
, 'image file map changed after a no-op')
130 def test_stream_partial(self
):
131 self
.assert_no_active_block_jobs()
133 result
= self
.vm
.qmp('block-stream', device
='drive0', base
=backing_img
)
134 self
.assert_qmp(result
, 'return', {})
136 self
.wait_until_completed()
138 self
.assert_no_active_block_jobs()
141 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', mid_img
),
142 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', test_img
),
143 'image file map does not match backing file after streaming')
145 def test_device_not_found(self
):
146 result
= self
.vm
.qmp('block-stream', device
='nonexistent')
147 self
.assert_qmp(result
, 'error/class', 'GenericError')
149 def test_job_id_missing(self
):
150 result
= self
.vm
.qmp('block-stream', device
='mid')
151 self
.assert_qmp(result
, 'error/class', 'GenericError')
154 class TestParallelOps(iotests
.QMPTestCase
):
155 num_ops
= 4 # Number of parallel block-stream operations
156 num_imgs
= num_ops
* 2 + 1
157 image_len
= num_ops
* 512 * 1024
164 # Initialize file names and command-line options
165 for i
in range(self
.num_imgs
):
166 img_depth
= self
.num_imgs
- i
- 1
167 opts
.append("backing." * img_depth
+ "node-name=node%d" % i
)
168 self
.imgs
.append(os
.path
.join(iotests
.test_dir
, 'img-%d.img' % i
))
171 iotests
.create_image(self
.imgs
[0], self
.image_len
)
172 for i
in range(1, self
.num_imgs
):
173 qemu_img('create', '-f', iotests
.imgfmt
,
174 '-o', 'backing_file=%s' % self
.imgs
[i
-1], self
.imgs
[i
])
176 # Put data into the images we are copying data from
177 odd_img_indexes
= [x
for x
in reversed(range(self
.num_imgs
)) if x
% 2 == 1]
178 for i
in range(len(odd_img_indexes
)):
179 # Alternate between 256KB and 512KB.
180 # This way jobs will not finish in the same order they were created
181 num_kb
= 256 + 256 * (i
% 2)
182 qemu_io('-f', iotests
.imgfmt
,
183 '-c', 'write -P 0xFF %dk %dk' % (i
* 512, num_kb
),
184 self
.imgs
[odd_img_indexes
[i
]])
186 # Attach the drive to the VM
187 self
.vm
= iotests
.VM()
188 self
.vm
.add_drive(self
.imgs
[-1], ','.join(opts
))
193 for img
in self
.imgs
:
196 # Test that it's possible to run several block-stream operations
197 # in parallel in the same snapshot chain
198 def test_stream_parallel(self
):
199 self
.assert_no_active_block_jobs()
201 # Check that the maps don't match before the streaming operations
202 for i
in range(2, self
.num_imgs
, 2):
203 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[i
]),
204 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[i
-1]),
205 'image file map matches backing file before streaming')
207 # Create all streaming jobs
209 for i
in range(2, self
.num_imgs
, 2):
210 node_name
= 'node%d' % i
211 job_id
= 'stream-%s' % node_name
212 pending_jobs
.append(job_id
)
213 result
= self
.vm
.qmp('block-stream', device
=node_name
, job_id
=job_id
, base
=self
.imgs
[i
-2], speed
=512*1024)
214 self
.assert_qmp(result
, 'return', {})
216 # Wait for all jobs to be finished.
217 while len(pending_jobs
) > 0:
218 for event
in self
.vm
.get_qmp_events(wait
=True):
219 if event
['event'] == 'BLOCK_JOB_COMPLETED':
220 job_id
= self
.dictpath(event
, 'data/device')
221 self
.assertTrue(job_id
in pending_jobs
)
222 self
.assert_qmp_absent(event
, 'data/error')
223 pending_jobs
.remove(job_id
)
225 self
.assert_no_active_block_jobs()
228 # Check that all maps match now
229 for i
in range(2, self
.num_imgs
, 2):
230 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[i
]),
231 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[i
-1]),
232 'image file map does not match backing file after streaming')
234 # Test that it's not possible to perform two block-stream
235 # operations if there are nodes involved in both.
236 def test_overlapping_1(self
):
237 self
.assert_no_active_block_jobs()
239 # Set a speed limit to make sure that this job blocks the rest
240 result
= self
.vm
.qmp('block-stream', device
='node4', job_id
='stream-node4', base
=self
.imgs
[1], speed
=1024*1024)
241 self
.assert_qmp(result
, 'return', {})
243 result
= self
.vm
.qmp('block-stream', device
='node5', job_id
='stream-node5', base
=self
.imgs
[2])
244 self
.assert_qmp(result
, 'error/class', 'GenericError')
246 result
= self
.vm
.qmp('block-stream', device
='node3', job_id
='stream-node3', base
=self
.imgs
[2])
247 self
.assert_qmp(result
, 'error/class', 'GenericError')
249 result
= self
.vm
.qmp('block-stream', device
='node4', job_id
='stream-node4-v2')
250 self
.assert_qmp(result
, 'error/class', 'GenericError')
252 # block-commit should also fail if it touches nodes used by the stream job
253 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[4], job_id
='commit-node4')
254 self
.assert_qmp(result
, 'error/class', 'GenericError')
256 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[1], top
=self
.imgs
[3], job_id
='commit-node1')
257 self
.assert_qmp(result
, 'error/class', 'GenericError')
259 # This fails because it needs to modify the backing string in node2, which is blocked
260 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[0], top
=self
.imgs
[1], job_id
='commit-node0')
261 self
.assert_qmp(result
, 'error/class', 'GenericError')
263 self
.wait_until_completed(drive
='stream-node4')
264 self
.assert_no_active_block_jobs()
266 # Similar to test_overlapping_1, but with block-commit
267 # blocking the other jobs
268 def test_overlapping_2(self
):
269 self
.assertLessEqual(9, self
.num_imgs
)
270 self
.assert_no_active_block_jobs()
272 # Set a speed limit to make sure that this job blocks the rest
273 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=self
.imgs
[5], base
=self
.imgs
[3], job_id
='commit-node3', speed
=1024*1024)
274 self
.assert_qmp(result
, 'return', {})
276 result
= self
.vm
.qmp('block-stream', device
='node3', job_id
='stream-node3')
277 self
.assert_qmp(result
, 'error/class', 'GenericError')
279 result
= self
.vm
.qmp('block-stream', device
='node6', base
=self
.imgs
[2], job_id
='stream-node6')
280 self
.assert_qmp(result
, 'error/class', 'GenericError')
282 result
= self
.vm
.qmp('block-stream', device
='node4', base
=self
.imgs
[2], job_id
='stream-node4')
283 self
.assert_qmp(result
, 'error/class', 'GenericError')
285 result
= self
.vm
.qmp('block-stream', device
='node6', base
=self
.imgs
[4], job_id
='stream-node6-v2')
286 self
.assert_qmp(result
, 'error/class', 'GenericError')
288 # This fails because block-commit currently blocks the active layer even if it's not used
289 result
= self
.vm
.qmp('block-stream', device
='drive0', base
=self
.imgs
[5], job_id
='stream-drive0')
290 self
.assert_qmp(result
, 'error/class', 'GenericError')
292 self
.wait_until_completed(drive
='commit-node3')
294 # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter.
295 # Internally this uses a mirror block job, hence the separate test case.
296 def test_overlapping_3(self
):
297 self
.assertLessEqual(8, self
.num_imgs
)
298 self
.assert_no_active_block_jobs()
300 # Set a speed limit to make sure that this job blocks the rest
301 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[3], job_id
='commit-drive0', speed
=1024*1024)
302 self
.assert_qmp(result
, 'return', {})
304 result
= self
.vm
.qmp('block-stream', device
='node5', base
=self
.imgs
[3], job_id
='stream-node6')
305 self
.assert_qmp(result
, 'error/class', 'GenericError')
307 event
= self
.vm
.get_qmp_event(wait
=True)
308 self
.assertEqual(event
['event'], 'BLOCK_JOB_READY')
309 self
.assert_qmp(event
, 'data/device', 'commit-drive0')
310 self
.assert_qmp(event
, 'data/type', 'commit')
311 self
.assert_qmp_absent(event
, 'data/error')
313 result
= self
.vm
.qmp('block-job-complete', device
='commit-drive0')
314 self
.assert_qmp(result
, 'return', {})
316 self
.wait_until_completed(drive
='commit-drive0')
318 # Test a block-stream and a block-commit job in parallel
319 # Here the stream job is supposed to finish quickly in order to reproduce
320 # the scenario that triggers the bug fixed in 3d5d319e1221 and 1a63a907507
321 def test_stream_commit_1(self
):
322 self
.assertLessEqual(8, self
.num_imgs
)
323 self
.assert_no_active_block_jobs()
325 # Stream from node0 into node2
326 result
= self
.vm
.qmp('block-stream', device
='node2', base_node
='node0', job_id
='node2')
327 self
.assert_qmp(result
, 'return', {})
329 # Commit from the active layer into node3
330 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[3])
331 self
.assert_qmp(result
, 'return', {})
333 # Wait for all jobs to be finished.
334 pending_jobs
= ['node2', 'drive0']
335 while len(pending_jobs
) > 0:
336 for event
in self
.vm
.get_qmp_events(wait
=True):
337 if event
['event'] == 'BLOCK_JOB_COMPLETED':
338 node_name
= self
.dictpath(event
, 'data/device')
339 self
.assertTrue(node_name
in pending_jobs
)
340 self
.assert_qmp_absent(event
, 'data/error')
341 pending_jobs
.remove(node_name
)
342 if event
['event'] == 'BLOCK_JOB_READY':
343 self
.assert_qmp(event
, 'data/device', 'drive0')
344 self
.assert_qmp(event
, 'data/type', 'commit')
345 self
.assert_qmp_absent(event
, 'data/error')
346 self
.assertTrue('drive0' in pending_jobs
)
347 self
.vm
.qmp('block-job-complete', device
='drive0')
349 self
.assert_no_active_block_jobs()
351 # This is similar to test_stream_commit_1 but both jobs are slowed
352 # down so they can run in parallel for a little while.
353 def test_stream_commit_2(self
):
354 self
.assertLessEqual(8, self
.num_imgs
)
355 self
.assert_no_active_block_jobs()
357 # Stream from node0 into node4
358 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node0', job_id
='node4', speed
=1024*1024)
359 self
.assert_qmp(result
, 'return', {})
361 # Commit from the active layer into node5
362 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[5], speed
=1024*1024)
363 self
.assert_qmp(result
, 'return', {})
365 # Wait for all jobs to be finished.
366 pending_jobs
= ['node4', 'drive0']
367 while len(pending_jobs
) > 0:
368 for event
in self
.vm
.get_qmp_events(wait
=True):
369 if event
['event'] == 'BLOCK_JOB_COMPLETED':
370 node_name
= self
.dictpath(event
, 'data/device')
371 self
.assertTrue(node_name
in pending_jobs
)
372 self
.assert_qmp_absent(event
, 'data/error')
373 pending_jobs
.remove(node_name
)
374 if event
['event'] == 'BLOCK_JOB_READY':
375 self
.assert_qmp(event
, 'data/device', 'drive0')
376 self
.assert_qmp(event
, 'data/type', 'commit')
377 self
.assert_qmp_absent(event
, 'data/error')
378 self
.assertTrue('drive0' in pending_jobs
)
379 self
.vm
.qmp('block-job-complete', device
='drive0')
381 self
.assert_no_active_block_jobs()
383 # Test the base_node parameter
384 def test_stream_base_node_name(self
):
385 self
.assert_no_active_block_jobs()
387 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[4]),
388 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[3]),
389 'image file map matches backing file before streaming')
391 # Error: the base node does not exist
392 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='none', job_id
='stream')
393 self
.assert_qmp(result
, 'error/class', 'GenericError')
395 # Error: the base node is not a backing file of the top node
396 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node6', job_id
='stream')
397 self
.assert_qmp(result
, 'error/class', 'GenericError')
399 # Error: the base node is the same as the top node
400 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node4', job_id
='stream')
401 self
.assert_qmp(result
, 'error/class', 'GenericError')
403 # Error: cannot specify 'base' and 'base-node' at the same time
404 result
= self
.vm
.qmp('block-stream', device
='node4', base
=self
.imgs
[2], base_node
='node2', job_id
='stream')
405 self
.assert_qmp(result
, 'error/class', 'GenericError')
407 # Success: the base node is a backing file of the top node
408 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node2', job_id
='stream')
409 self
.assert_qmp(result
, 'return', {})
411 self
.wait_until_completed(drive
='stream')
413 self
.assert_no_active_block_jobs()
416 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[4]),
417 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[3]),
418 'image file map matches backing file after streaming')
420 class TestQuorum(iotests
.QMPTestCase
):
426 opts
= ['driver=quorum', 'vote-threshold=2']
428 # Initialize file names and command-line options
429 for i
in range(self
.num_children
):
430 child_img
= os
.path
.join(iotests
.test_dir
, 'img-%d.img' % i
)
431 backing_img
= os
.path
.join(iotests
.test_dir
, 'backing-%d.img' % i
)
432 self
.children
.append(child_img
)
433 self
.backing
.append(backing_img
)
434 qemu_img('create', '-f', iotests
.imgfmt
, backing_img
, '1M')
435 qemu_io('-f', iotests
.imgfmt
,
436 '-c', 'write -P 0x55 0 1024', backing_img
)
437 qemu_img('create', '-f', iotests
.imgfmt
,
438 '-o', 'backing_file=%s' % backing_img
, child_img
)
439 opts
.append("children.%d.file.filename=%s" % (i
, child_img
))
440 opts
.append("children.%d.node-name=node%d" % (i
, i
))
442 # Attach the drive to the VM
443 self
.vm
= iotests
.VM()
444 self
.vm
.add_drive(path
= None, opts
= ','.join(opts
))
449 for img
in self
.children
:
451 for img
in self
.backing
:
454 def test_stream_quorum(self
):
455 if not iotests
.supports_quorum():
458 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.children
[0]),
459 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.backing
[0]),
460 'image file map matches backing file before streaming')
462 self
.assert_no_active_block_jobs()
464 result
= self
.vm
.qmp('block-stream', device
='node0', job_id
='stream-node0')
465 self
.assert_qmp(result
, 'return', {})
467 self
.wait_until_completed(drive
='stream-node0')
469 self
.assert_no_active_block_jobs()
472 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.children
[0]),
473 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.backing
[0]),
474 'image file map does not match backing file after streaming')
476 class TestSmallerBackingFile(iotests
.QMPTestCase
):
477 backing_len
= 1 * 1024 * 1024 # MB
478 image_len
= 2 * backing_len
481 iotests
.create_image(backing_img
, self
.backing_len
)
482 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
, str(self
.image_len
))
483 self
.vm
= iotests
.VM().add_drive(test_img
)
486 # If this hangs, then you are missing a fix to complete streaming when the
487 # end of the backing file is reached.
488 def test_stream(self
):
489 self
.assert_no_active_block_jobs()
491 result
= self
.vm
.qmp('block-stream', device
='drive0')
492 self
.assert_qmp(result
, 'return', {})
494 self
.wait_until_completed()
496 self
.assert_no_active_block_jobs()
499 class TestErrors(iotests
.QMPTestCase
):
500 image_len
= 2 * 1024 * 1024 # MB
502 # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
503 STREAM_BUFFER_SIZE
= 512 * 1024
505 def create_blkdebug_file(self
, name
, event
, errno
):
506 file = open(name
, 'w')
525 ''' % (event
, errno
, self
.STREAM_BUFFER_SIZE
/ 512, event
, event
))
528 class TestEIO(TestErrors
):
530 self
.blkdebug_file
= backing_img
+ ".blkdebug"
531 iotests
.create_image(backing_img
, TestErrors
.image_len
)
532 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 5)
533 qemu_img('create', '-f', iotests
.imgfmt
,
534 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
535 % (self
.blkdebug_file
, backing_img
),
537 self
.vm
= iotests
.VM().add_drive(test_img
)
543 os
.remove(backing_img
)
544 os
.remove(self
.blkdebug_file
)
546 def test_report(self
):
547 self
.assert_no_active_block_jobs()
549 result
= self
.vm
.qmp('block-stream', device
='drive0')
550 self
.assert_qmp(result
, 'return', {})
555 for event
in self
.vm
.get_qmp_events(wait
=True):
556 if event
['event'] == 'BLOCK_JOB_ERROR':
557 self
.assert_qmp(event
, 'data/device', 'drive0')
558 self
.assert_qmp(event
, 'data/operation', 'read')
560 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
561 self
.assertTrue(error
, 'job completed unexpectedly')
562 self
.assert_qmp(event
, 'data/type', 'stream')
563 self
.assert_qmp(event
, 'data/device', 'drive0')
564 self
.assert_qmp(event
, 'data/error', 'Input/output error')
565 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
566 self
.assert_qmp(event
, 'data/len', self
.image_len
)
569 self
.assert_no_active_block_jobs()
572 def test_ignore(self
):
573 self
.assert_no_active_block_jobs()
575 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='ignore')
576 self
.assert_qmp(result
, 'return', {})
581 for event
in self
.vm
.get_qmp_events(wait
=True):
582 if event
['event'] == 'BLOCK_JOB_ERROR':
584 self
.assert_qmp(event
, 'data/device', 'drive0')
585 self
.assert_qmp(event
, 'data/operation', 'read')
586 result
= self
.vm
.qmp('query-block-jobs')
587 if result
== {'return': []}:
588 # Job finished too quickly
590 self
.assert_qmp(result
, 'return[0]/paused', False)
591 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
592 self
.assertTrue(error
, 'job completed unexpectedly')
593 self
.assert_qmp(event
, 'data/type', 'stream')
594 self
.assert_qmp(event
, 'data/device', 'drive0')
595 self
.assert_qmp(event
, 'data/error', 'Input/output error')
596 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
597 self
.assert_qmp(event
, 'data/len', self
.image_len
)
600 self
.assert_no_active_block_jobs()
604 self
.assert_no_active_block_jobs()
606 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='stop')
607 self
.assert_qmp(result
, 'return', {})
612 for event
in self
.vm
.get_qmp_events(wait
=True):
613 if event
['event'] == 'BLOCK_JOB_ERROR':
615 self
.assert_qmp(event
, 'data/device', 'drive0')
616 self
.assert_qmp(event
, 'data/operation', 'read')
618 result
= self
.vm
.qmp('query-block-jobs')
619 self
.assert_qmp(result
, 'return[0]/paused', True)
620 self
.assert_qmp(result
, 'return[0]/offset', self
.STREAM_BUFFER_SIZE
)
621 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
623 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
624 self
.assert_qmp(result
, 'return', {})
626 result
= self
.vm
.qmp('query-block-jobs')
627 if result
== {'return': []}:
628 # Race; likely already finished. Check.
630 self
.assert_qmp(result
, 'return[0]/paused', False)
631 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
632 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
633 self
.assertTrue(error
, 'job completed unexpectedly')
634 self
.assert_qmp(event
, 'data/type', 'stream')
635 self
.assert_qmp(event
, 'data/device', 'drive0')
636 self
.assert_qmp_absent(event
, 'data/error')
637 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
638 self
.assert_qmp(event
, 'data/len', self
.image_len
)
641 self
.assert_no_active_block_jobs()
644 def test_enospc(self
):
645 self
.assert_no_active_block_jobs()
647 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
648 self
.assert_qmp(result
, 'return', {})
653 for event
in self
.vm
.get_qmp_events(wait
=True):
654 if event
['event'] == 'BLOCK_JOB_ERROR':
655 self
.assert_qmp(event
, 'data/device', 'drive0')
656 self
.assert_qmp(event
, 'data/operation', 'read')
658 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
659 self
.assertTrue(error
, 'job completed unexpectedly')
660 self
.assert_qmp(event
, 'data/type', 'stream')
661 self
.assert_qmp(event
, 'data/device', 'drive0')
662 self
.assert_qmp(event
, 'data/error', 'Input/output error')
663 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
664 self
.assert_qmp(event
, 'data/len', self
.image_len
)
667 self
.assert_no_active_block_jobs()
670 class TestENOSPC(TestErrors
):
672 self
.blkdebug_file
= backing_img
+ ".blkdebug"
673 iotests
.create_image(backing_img
, TestErrors
.image_len
)
674 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 28)
675 qemu_img('create', '-f', iotests
.imgfmt
,
676 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
677 % (self
.blkdebug_file
, backing_img
),
679 self
.vm
= iotests
.VM().add_drive(test_img
)
685 os
.remove(backing_img
)
686 os
.remove(self
.blkdebug_file
)
688 def test_enospc(self
):
689 self
.assert_no_active_block_jobs()
691 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
692 self
.assert_qmp(result
, 'return', {})
697 for event
in self
.vm
.get_qmp_events(wait
=True):
698 if event
['event'] == 'BLOCK_JOB_ERROR':
699 self
.assert_qmp(event
, 'data/device', 'drive0')
700 self
.assert_qmp(event
, 'data/operation', 'read')
703 result
= self
.vm
.qmp('query-block-jobs')
704 self
.assert_qmp(result
, 'return[0]/paused', True)
705 self
.assert_qmp(result
, 'return[0]/offset', self
.STREAM_BUFFER_SIZE
)
706 self
.assert_qmp(result
, 'return[0]/io-status', 'nospace')
708 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
709 self
.assert_qmp(result
, 'return', {})
711 result
= self
.vm
.qmp('query-block-jobs')
712 if result
== {'return': []}:
713 # Race; likely already finished. Check.
715 self
.assert_qmp(result
, 'return[0]/paused', False)
716 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
717 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
718 self
.assertTrue(error
, 'job completed unexpectedly')
719 self
.assert_qmp(event
, 'data/type', 'stream')
720 self
.assert_qmp(event
, 'data/device', 'drive0')
721 self
.assert_qmp_absent(event
, 'data/error')
722 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
723 self
.assert_qmp(event
, 'data/len', self
.image_len
)
726 self
.assert_no_active_block_jobs()
729 class TestStreamStop(iotests
.QMPTestCase
):
730 image_len
= 8 * 1024 * 1024 * 1024 # GB
733 qemu_img('create', backing_img
, str(TestStreamStop
.image_len
))
734 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
735 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
736 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
737 self
.vm
= iotests
.VM().add_drive("blkdebug::" + test_img
)
743 os
.remove(backing_img
)
745 def test_stream_stop(self
):
746 self
.assert_no_active_block_jobs()
748 self
.vm
.pause_drive('drive0')
749 result
= self
.vm
.qmp('block-stream', device
='drive0')
750 self
.assert_qmp(result
, 'return', {})
753 events
= self
.vm
.get_qmp_events(wait
=False)
754 self
.assertEqual(events
, [], 'unexpected QMP event: %s' % events
)
756 self
.cancel_and_wait(resume
=True)
758 class TestSetSpeed(iotests
.QMPTestCase
):
759 image_len
= 80 * 1024 * 1024 # MB
762 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
763 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
764 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
765 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
766 self
.vm
= iotests
.VM().add_drive('blkdebug::' + test_img
)
772 os
.remove(backing_img
)
774 # This is a short performance test which is not run by default.
775 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
776 def perf_test_throughput(self
):
777 self
.assert_no_active_block_jobs()
779 result
= self
.vm
.qmp('block-stream', device
='drive0')
780 self
.assert_qmp(result
, 'return', {})
782 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
783 self
.assert_qmp(result
, 'return', {})
785 self
.wait_until_completed()
787 self
.assert_no_active_block_jobs()
789 def test_set_speed(self
):
790 self
.assert_no_active_block_jobs()
792 self
.vm
.pause_drive('drive0')
793 result
= self
.vm
.qmp('block-stream', device
='drive0')
794 self
.assert_qmp(result
, 'return', {})
797 result
= self
.vm
.qmp('query-block-jobs')
798 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
799 self
.assert_qmp(result
, 'return[0]/speed', 0)
801 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
802 self
.assert_qmp(result
, 'return', {})
804 # Ensure the speed we set was accepted
805 result
= self
.vm
.qmp('query-block-jobs')
806 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
807 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
809 self
.cancel_and_wait(resume
=True)
810 self
.vm
.pause_drive('drive0')
812 # Check setting speed in block-stream works
813 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=4 * 1024 * 1024)
814 self
.assert_qmp(result
, 'return', {})
816 result
= self
.vm
.qmp('query-block-jobs')
817 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
818 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
820 self
.cancel_and_wait(resume
=True)
822 def test_set_speed_invalid(self
):
823 self
.assert_no_active_block_jobs()
825 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=-1)
826 self
.assert_qmp(result
, 'error/class', 'GenericError')
828 self
.assert_no_active_block_jobs()
830 self
.vm
.pause_drive('drive0')
831 result
= self
.vm
.qmp('block-stream', device
='drive0')
832 self
.assert_qmp(result
, 'return', {})
834 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
835 self
.assert_qmp(result
, 'error/class', 'GenericError')
837 self
.cancel_and_wait(resume
=True)
839 if __name__
== '__main__':
840 iotests
.main(supported_fmts
=['qcow2', 'qed'])