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
.event_wait(name
='BLOCK_JOB_READY')
308 self
.assert_qmp(event
, 'data/device', 'commit-drive0')
309 self
.assert_qmp(event
, 'data/type', 'commit')
310 self
.assert_qmp_absent(event
, 'data/error')
312 result
= self
.vm
.qmp('block-job-complete', device
='commit-drive0')
313 self
.assert_qmp(result
, 'return', {})
315 self
.wait_until_completed(drive
='commit-drive0')
317 # In this case the base node of the stream job is the same as the
318 # top node of commit job. Since block-commit removes the top node
319 # when it finishes, this is not allowed.
320 def test_overlapping_4(self
):
321 self
.assert_no_active_block_jobs()
323 # Commit from node2 into node0
324 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=self
.imgs
[2], base
=self
.imgs
[0])
325 self
.assert_qmp(result
, 'return', {})
327 # Stream from node2 into node4
328 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node2', job_id
='node4')
329 self
.assert_qmp(result
, 'error/class', 'GenericError')
331 self
.wait_until_completed()
332 self
.assert_no_active_block_jobs()
334 # Test a block-stream and a block-commit job in parallel
335 # Here the stream job is supposed to finish quickly in order to reproduce
336 # the scenario that triggers the bug fixed in 3d5d319e1221 and 1a63a907507
337 def test_stream_commit_1(self
):
338 self
.assertLessEqual(8, self
.num_imgs
)
339 self
.assert_no_active_block_jobs()
341 # Stream from node0 into node2
342 result
= self
.vm
.qmp('block-stream', device
='node2', base_node
='node0', job_id
='node2')
343 self
.assert_qmp(result
, 'return', {})
345 # Commit from the active layer into node3
346 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[3])
347 self
.assert_qmp(result
, 'return', {})
349 # Wait for all jobs to be finished.
350 pending_jobs
= ['node2', 'drive0']
351 while len(pending_jobs
) > 0:
352 for event
in self
.vm
.get_qmp_events(wait
=True):
353 if event
['event'] == 'BLOCK_JOB_COMPLETED':
354 node_name
= self
.dictpath(event
, 'data/device')
355 self
.assertTrue(node_name
in pending_jobs
)
356 self
.assert_qmp_absent(event
, 'data/error')
357 pending_jobs
.remove(node_name
)
358 if event
['event'] == 'BLOCK_JOB_READY':
359 self
.assert_qmp(event
, 'data/device', 'drive0')
360 self
.assert_qmp(event
, 'data/type', 'commit')
361 self
.assert_qmp_absent(event
, 'data/error')
362 self
.assertTrue('drive0' in pending_jobs
)
363 self
.vm
.qmp('block-job-complete', device
='drive0')
365 self
.assert_no_active_block_jobs()
367 # This is similar to test_stream_commit_1 but both jobs are slowed
368 # down so they can run in parallel for a little while.
369 def test_stream_commit_2(self
):
370 self
.assertLessEqual(8, self
.num_imgs
)
371 self
.assert_no_active_block_jobs()
373 # Stream from node0 into node4
374 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node0', job_id
='node4', speed
=1024*1024)
375 self
.assert_qmp(result
, 'return', {})
377 # Commit from the active layer into node5
378 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[5], speed
=1024*1024)
379 self
.assert_qmp(result
, 'return', {})
381 # Wait for all jobs to be finished.
382 pending_jobs
= ['node4', 'drive0']
383 while len(pending_jobs
) > 0:
384 for event
in self
.vm
.get_qmp_events(wait
=True):
385 if event
['event'] == 'BLOCK_JOB_COMPLETED':
386 node_name
= self
.dictpath(event
, 'data/device')
387 self
.assertTrue(node_name
in pending_jobs
)
388 self
.assert_qmp_absent(event
, 'data/error')
389 pending_jobs
.remove(node_name
)
390 if event
['event'] == 'BLOCK_JOB_READY':
391 self
.assert_qmp(event
, 'data/device', 'drive0')
392 self
.assert_qmp(event
, 'data/type', 'commit')
393 self
.assert_qmp_absent(event
, 'data/error')
394 self
.assertTrue('drive0' in pending_jobs
)
395 self
.vm
.qmp('block-job-complete', device
='drive0')
397 self
.assert_no_active_block_jobs()
399 # Test the base_node parameter
400 def test_stream_base_node_name(self
):
401 self
.assert_no_active_block_jobs()
403 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[4]),
404 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[3]),
405 'image file map matches backing file before streaming')
407 # Error: the base node does not exist
408 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='none', job_id
='stream')
409 self
.assert_qmp(result
, 'error/class', 'GenericError')
411 # Error: the base node is not a backing file of the top node
412 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node6', job_id
='stream')
413 self
.assert_qmp(result
, 'error/class', 'GenericError')
415 # Error: the base node is the same as the top node
416 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node4', job_id
='stream')
417 self
.assert_qmp(result
, 'error/class', 'GenericError')
419 # Error: cannot specify 'base' and 'base-node' at the same time
420 result
= self
.vm
.qmp('block-stream', device
='node4', base
=self
.imgs
[2], base_node
='node2', job_id
='stream')
421 self
.assert_qmp(result
, 'error/class', 'GenericError')
423 # Success: the base node is a backing file of the top node
424 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node2', job_id
='stream')
425 self
.assert_qmp(result
, 'return', {})
427 self
.wait_until_completed(drive
='stream')
429 self
.assert_no_active_block_jobs()
432 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[4]),
433 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[3]),
434 'image file map matches backing file after streaming')
436 class TestQuorum(iotests
.QMPTestCase
):
442 opts
= ['driver=quorum', 'vote-threshold=2']
444 # Initialize file names and command-line options
445 for i
in range(self
.num_children
):
446 child_img
= os
.path
.join(iotests
.test_dir
, 'img-%d.img' % i
)
447 backing_img
= os
.path
.join(iotests
.test_dir
, 'backing-%d.img' % i
)
448 self
.children
.append(child_img
)
449 self
.backing
.append(backing_img
)
450 qemu_img('create', '-f', iotests
.imgfmt
, backing_img
, '1M')
451 qemu_io('-f', iotests
.imgfmt
,
452 '-c', 'write -P 0x55 0 1024', backing_img
)
453 qemu_img('create', '-f', iotests
.imgfmt
,
454 '-o', 'backing_file=%s' % backing_img
, child_img
)
455 opts
.append("children.%d.file.filename=%s" % (i
, child_img
))
456 opts
.append("children.%d.node-name=node%d" % (i
, i
))
458 # Attach the drive to the VM
459 self
.vm
= iotests
.VM()
460 self
.vm
.add_drive(path
= None, opts
= ','.join(opts
))
465 for img
in self
.children
:
467 for img
in self
.backing
:
470 def test_stream_quorum(self
):
471 if not iotests
.supports_quorum():
474 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.children
[0]),
475 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.backing
[0]),
476 'image file map matches backing file before streaming')
478 self
.assert_no_active_block_jobs()
480 result
= self
.vm
.qmp('block-stream', device
='node0', job_id
='stream-node0')
481 self
.assert_qmp(result
, 'return', {})
483 self
.wait_until_completed(drive
='stream-node0')
485 self
.assert_no_active_block_jobs()
488 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.children
[0]),
489 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.backing
[0]),
490 'image file map does not match backing file after streaming')
492 class TestSmallerBackingFile(iotests
.QMPTestCase
):
493 backing_len
= 1 * 1024 * 1024 # MB
494 image_len
= 2 * backing_len
497 iotests
.create_image(backing_img
, self
.backing_len
)
498 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
, str(self
.image_len
))
499 self
.vm
= iotests
.VM().add_drive(test_img
)
502 # If this hangs, then you are missing a fix to complete streaming when the
503 # end of the backing file is reached.
504 def test_stream(self
):
505 self
.assert_no_active_block_jobs()
507 result
= self
.vm
.qmp('block-stream', device
='drive0')
508 self
.assert_qmp(result
, 'return', {})
510 self
.wait_until_completed()
512 self
.assert_no_active_block_jobs()
515 class TestErrors(iotests
.QMPTestCase
):
516 image_len
= 2 * 1024 * 1024 # MB
518 # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
519 STREAM_BUFFER_SIZE
= 512 * 1024
521 def create_blkdebug_file(self
, name
, event
, errno
):
522 file = open(name
, 'w')
541 ''' % (event
, errno
, self
.STREAM_BUFFER_SIZE
// 512, event
, event
))
544 class TestEIO(TestErrors
):
546 self
.blkdebug_file
= backing_img
+ ".blkdebug"
547 iotests
.create_image(backing_img
, TestErrors
.image_len
)
548 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 5)
549 qemu_img('create', '-f', iotests
.imgfmt
,
550 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
551 % (self
.blkdebug_file
, backing_img
),
553 self
.vm
= iotests
.VM().add_drive(test_img
)
559 os
.remove(backing_img
)
560 os
.remove(self
.blkdebug_file
)
562 def test_report(self
):
563 self
.assert_no_active_block_jobs()
565 result
= self
.vm
.qmp('block-stream', device
='drive0')
566 self
.assert_qmp(result
, 'return', {})
571 for event
in self
.vm
.get_qmp_events(wait
=True):
572 if event
['event'] == 'BLOCK_JOB_ERROR':
573 self
.assert_qmp(event
, 'data/device', 'drive0')
574 self
.assert_qmp(event
, 'data/operation', 'read')
576 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
577 self
.assertTrue(error
, 'job completed unexpectedly')
578 self
.assert_qmp(event
, 'data/type', 'stream')
579 self
.assert_qmp(event
, 'data/device', 'drive0')
580 self
.assert_qmp(event
, 'data/error', 'Input/output error')
581 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
582 self
.assert_qmp(event
, 'data/len', self
.image_len
)
584 elif event
['event'] == 'JOB_STATUS_CHANGE':
585 self
.assert_qmp(event
, 'data/id', 'drive0')
587 self
.assert_no_active_block_jobs()
590 def test_ignore(self
):
591 self
.assert_no_active_block_jobs()
593 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='ignore')
594 self
.assert_qmp(result
, 'return', {})
599 for event
in self
.vm
.get_qmp_events(wait
=True):
600 if event
['event'] == 'BLOCK_JOB_ERROR':
602 self
.assert_qmp(event
, 'data/device', 'drive0')
603 self
.assert_qmp(event
, 'data/operation', 'read')
604 result
= self
.vm
.qmp('query-block-jobs')
605 if result
== {'return': []}:
606 # Job finished too quickly
608 self
.assert_qmp(result
, 'return[0]/paused', False)
609 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
610 self
.assertTrue(error
, 'job completed unexpectedly')
611 self
.assert_qmp(event
, 'data/type', 'stream')
612 self
.assert_qmp(event
, 'data/device', 'drive0')
613 self
.assert_qmp(event
, 'data/error', 'Input/output error')
614 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
615 self
.assert_qmp(event
, 'data/len', self
.image_len
)
617 elif event
['event'] == 'JOB_STATUS_CHANGE':
618 self
.assert_qmp(event
, 'data/id', 'drive0')
620 self
.assert_no_active_block_jobs()
624 self
.assert_no_active_block_jobs()
626 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='stop')
627 self
.assert_qmp(result
, 'return', {})
632 for event
in self
.vm
.get_qmp_events(wait
=True):
633 if event
['event'] == 'BLOCK_JOB_ERROR':
635 self
.assert_qmp(event
, 'data/device', 'drive0')
636 self
.assert_qmp(event
, 'data/operation', 'read')
638 result
= self
.vm
.qmp('query-block-jobs')
639 self
.assert_qmp(result
, 'return[0]/paused', True)
640 self
.assert_qmp(result
, 'return[0]/offset', self
.STREAM_BUFFER_SIZE
)
641 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
643 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
644 self
.assert_qmp(result
, 'return', {})
646 result
= self
.vm
.qmp('query-block-jobs')
647 if result
== {'return': []}:
648 # Race; likely already finished. Check.
650 self
.assert_qmp(result
, 'return[0]/paused', False)
651 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
652 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
653 self
.assertTrue(error
, 'job completed unexpectedly')
654 self
.assert_qmp(event
, 'data/type', 'stream')
655 self
.assert_qmp(event
, 'data/device', 'drive0')
656 self
.assert_qmp_absent(event
, 'data/error')
657 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
658 self
.assert_qmp(event
, 'data/len', self
.image_len
)
660 elif event
['event'] == 'JOB_STATUS_CHANGE':
661 self
.assert_qmp(event
, 'data/id', 'drive0')
663 self
.assert_no_active_block_jobs()
666 def test_enospc(self
):
667 self
.assert_no_active_block_jobs()
669 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
670 self
.assert_qmp(result
, 'return', {})
675 for event
in self
.vm
.get_qmp_events(wait
=True):
676 if event
['event'] == 'BLOCK_JOB_ERROR':
677 self
.assert_qmp(event
, 'data/device', 'drive0')
678 self
.assert_qmp(event
, 'data/operation', 'read')
680 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
681 self
.assertTrue(error
, 'job completed unexpectedly')
682 self
.assert_qmp(event
, 'data/type', 'stream')
683 self
.assert_qmp(event
, 'data/device', 'drive0')
684 self
.assert_qmp(event
, 'data/error', 'Input/output error')
685 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
686 self
.assert_qmp(event
, 'data/len', self
.image_len
)
688 elif event
['event'] == 'JOB_STATUS_CHANGE':
689 self
.assert_qmp(event
, 'data/id', 'drive0')
691 self
.assert_no_active_block_jobs()
694 class TestENOSPC(TestErrors
):
696 self
.blkdebug_file
= backing_img
+ ".blkdebug"
697 iotests
.create_image(backing_img
, TestErrors
.image_len
)
698 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 28)
699 qemu_img('create', '-f', iotests
.imgfmt
,
700 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
701 % (self
.blkdebug_file
, backing_img
),
703 self
.vm
= iotests
.VM().add_drive(test_img
)
709 os
.remove(backing_img
)
710 os
.remove(self
.blkdebug_file
)
712 def test_enospc(self
):
713 self
.assert_no_active_block_jobs()
715 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
716 self
.assert_qmp(result
, 'return', {})
721 for event
in self
.vm
.get_qmp_events(wait
=True):
722 if event
['event'] == 'BLOCK_JOB_ERROR':
723 self
.assert_qmp(event
, 'data/device', 'drive0')
724 self
.assert_qmp(event
, 'data/operation', 'read')
727 result
= self
.vm
.qmp('query-block-jobs')
728 self
.assert_qmp(result
, 'return[0]/paused', True)
729 self
.assert_qmp(result
, 'return[0]/offset', self
.STREAM_BUFFER_SIZE
)
730 self
.assert_qmp(result
, 'return[0]/io-status', 'nospace')
732 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
733 self
.assert_qmp(result
, 'return', {})
735 result
= self
.vm
.qmp('query-block-jobs')
736 if result
== {'return': []}:
737 # Race; likely already finished. Check.
739 self
.assert_qmp(result
, 'return[0]/paused', False)
740 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
741 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
742 self
.assertTrue(error
, 'job completed unexpectedly')
743 self
.assert_qmp(event
, 'data/type', 'stream')
744 self
.assert_qmp(event
, 'data/device', 'drive0')
745 self
.assert_qmp_absent(event
, 'data/error')
746 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
747 self
.assert_qmp(event
, 'data/len', self
.image_len
)
749 elif event
['event'] == 'JOB_STATUS_CHANGE':
750 self
.assert_qmp(event
, 'data/id', 'drive0')
752 self
.assert_no_active_block_jobs()
755 class TestStreamStop(iotests
.QMPTestCase
):
756 image_len
= 8 * 1024 * 1024 * 1024 # GB
759 qemu_img('create', backing_img
, str(TestStreamStop
.image_len
))
760 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
761 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
762 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
763 self
.vm
= iotests
.VM().add_drive("blkdebug::" + test_img
)
769 os
.remove(backing_img
)
771 def test_stream_stop(self
):
772 self
.assert_no_active_block_jobs()
774 self
.vm
.pause_drive('drive0')
775 result
= self
.vm
.qmp('block-stream', device
='drive0')
776 self
.assert_qmp(result
, 'return', {})
779 events
= self
.vm
.get_qmp_events(wait
=False)
781 self
.assert_qmp(e
, 'event', 'JOB_STATUS_CHANGE')
782 self
.assert_qmp(e
, 'data/id', 'drive0')
784 self
.cancel_and_wait(resume
=True)
786 class TestSetSpeed(iotests
.QMPTestCase
):
787 image_len
= 80 * 1024 * 1024 # MB
790 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
791 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
792 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
793 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
794 self
.vm
= iotests
.VM().add_drive('blkdebug::' + test_img
)
800 os
.remove(backing_img
)
802 # This is a short performance test which is not run by default.
803 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
804 def perf_test_throughput(self
):
805 self
.assert_no_active_block_jobs()
807 result
= self
.vm
.qmp('block-stream', device
='drive0')
808 self
.assert_qmp(result
, 'return', {})
810 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
811 self
.assert_qmp(result
, 'return', {})
813 self
.wait_until_completed()
815 self
.assert_no_active_block_jobs()
817 def test_set_speed(self
):
818 self
.assert_no_active_block_jobs()
820 self
.vm
.pause_drive('drive0')
821 result
= self
.vm
.qmp('block-stream', device
='drive0')
822 self
.assert_qmp(result
, 'return', {})
825 result
= self
.vm
.qmp('query-block-jobs')
826 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
827 self
.assert_qmp(result
, 'return[0]/speed', 0)
829 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
830 self
.assert_qmp(result
, 'return', {})
832 # Ensure the speed we set was accepted
833 result
= self
.vm
.qmp('query-block-jobs')
834 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
835 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
837 self
.cancel_and_wait(resume
=True)
838 self
.vm
.pause_drive('drive0')
840 # Check setting speed in block-stream works
841 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=4 * 1024 * 1024)
842 self
.assert_qmp(result
, 'return', {})
844 result
= self
.vm
.qmp('query-block-jobs')
845 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
846 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
848 self
.cancel_and_wait(resume
=True)
850 def test_set_speed_invalid(self
):
851 self
.assert_no_active_block_jobs()
853 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=-1)
854 self
.assert_qmp(result
, 'error/class', 'GenericError')
856 self
.assert_no_active_block_jobs()
858 self
.vm
.pause_drive('drive0')
859 result
= self
.vm
.qmp('block-stream', device
='drive0')
860 self
.assert_qmp(result
, 'return', {})
862 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
863 self
.assert_qmp(result
, 'error/class', 'GenericError')
865 self
.cancel_and_wait(resume
=True)
867 if __name__
== '__main__':
868 iotests
.main(supported_fmts
=['qcow2', 'qed'])