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/desc',
148 'Cannot find device=nonexistent nor node_name=nonexistent')
150 def test_job_id_missing(self
):
151 result
= self
.vm
.qmp('block-stream', device
='mid')
152 self
.assert_qmp(result
, 'error/desc', "Invalid job ID ''")
155 class TestParallelOps(iotests
.QMPTestCase
):
156 num_ops
= 4 # Number of parallel block-stream operations
157 num_imgs
= num_ops
* 2 + 1
158 image_len
= num_ops
* 4 * 1024 * 1024
165 # Initialize file names and command-line options
166 for i
in range(self
.num_imgs
):
167 img_depth
= self
.num_imgs
- i
- 1
168 opts
.append("backing." * img_depth
+ "node-name=node%d" % i
)
169 self
.imgs
.append(os
.path
.join(iotests
.test_dir
, 'img-%d.img' % i
))
172 iotests
.create_image(self
.imgs
[0], self
.image_len
)
173 for i
in range(1, self
.num_imgs
):
174 qemu_img('create', '-f', iotests
.imgfmt
,
175 '-o', 'backing_file=%s' % self
.imgs
[i
-1], self
.imgs
[i
])
177 # Put data into the images we are copying data from
178 odd_img_indexes
= [x
for x
in reversed(range(self
.num_imgs
)) if x
% 2 == 1]
179 for i
in range(len(odd_img_indexes
)):
180 # Alternate between 2MB and 4MB.
181 # This way jobs will not finish in the same order they were created
182 num_mb
= 2 + 2 * (i
% 2)
183 qemu_io('-f', iotests
.imgfmt
,
184 '-c', 'write -P 0xFF %dM %dM' % (i
* 4, num_mb
),
185 self
.imgs
[odd_img_indexes
[i
]])
187 # Attach the drive to the VM
188 self
.vm
= iotests
.VM()
189 self
.vm
.add_drive(self
.imgs
[-1], ','.join(opts
))
194 for img
in self
.imgs
:
197 # Test that it's possible to run several block-stream operations
198 # in parallel in the same snapshot chain
199 def test_stream_parallel(self
):
200 self
.assert_no_active_block_jobs()
202 # Check that the maps don't match before the streaming operations
203 for i
in range(2, self
.num_imgs
, 2):
204 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[i
]),
205 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[i
-1]),
206 'image file map matches backing file before streaming')
208 # Create all streaming jobs
210 for i
in range(2, self
.num_imgs
, 2):
211 node_name
= 'node%d' % i
212 job_id
= 'stream-%s' % node_name
213 pending_jobs
.append(job_id
)
214 result
= self
.vm
.qmp('block-stream', device
=node_name
, job_id
=job_id
, base
=self
.imgs
[i
-2], speed
=512*1024)
215 self
.assert_qmp(result
, 'return', {})
217 for job
in pending_jobs
:
218 result
= self
.vm
.qmp('block-job-set-speed', device
=job
, speed
=0)
219 self
.assert_qmp(result
, 'return', {})
221 # Wait for all jobs to be finished.
222 while len(pending_jobs
) > 0:
223 for event
in self
.vm
.get_qmp_events(wait
=True):
224 if event
['event'] == 'BLOCK_JOB_COMPLETED':
225 job_id
= self
.dictpath(event
, 'data/device')
226 self
.assertTrue(job_id
in pending_jobs
)
227 self
.assert_qmp_absent(event
, 'data/error')
228 pending_jobs
.remove(job_id
)
230 self
.assert_no_active_block_jobs()
233 # Check that all maps match now
234 for i
in range(2, self
.num_imgs
, 2):
235 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[i
]),
236 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[i
-1]),
237 'image file map does not match backing file after streaming')
239 # Test that it's not possible to perform two block-stream
240 # operations if there are nodes involved in both.
241 def test_overlapping_1(self
):
242 self
.assert_no_active_block_jobs()
244 # Set a speed limit to make sure that this job blocks the rest
245 result
= self
.vm
.qmp('block-stream', device
='node4', job_id
='stream-node4', base
=self
.imgs
[1], speed
=1024*1024)
246 self
.assert_qmp(result
, 'return', {})
248 result
= self
.vm
.qmp('block-stream', device
='node5', job_id
='stream-node5', base
=self
.imgs
[2])
249 self
.assert_qmp(result
, 'error/desc',
250 "Node 'node4' is busy: block device is in use by block job: stream")
252 result
= self
.vm
.qmp('block-stream', device
='node3', job_id
='stream-node3', base
=self
.imgs
[2])
253 self
.assert_qmp(result
, 'error/desc',
254 "Node 'node3' is busy: block device is in use by block job: stream")
256 result
= self
.vm
.qmp('block-stream', device
='node4', job_id
='stream-node4-v2')
257 self
.assert_qmp(result
, 'error/desc',
258 "Node 'node4' is busy: block device is in use by block job: stream")
260 # block-commit should also fail if it touches nodes used by the stream job
261 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[4], job_id
='commit-node4')
262 self
.assert_qmp(result
, 'error/desc',
263 "Node 'node4' is busy: block device is in use by block job: stream")
265 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[1], top
=self
.imgs
[3], job_id
='commit-node1')
266 self
.assert_qmp(result
, 'error/desc',
267 "Node 'node3' is busy: block device is in use by block job: stream")
269 # This fails because it needs to modify the backing string in node2, which is blocked
270 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[0], top
=self
.imgs
[1], job_id
='commit-node0')
271 self
.assert_qmp(result
, 'error/desc',
272 "Node 'node2' is busy: block device is in use by block job: stream")
274 result
= self
.vm
.qmp('block-job-set-speed', device
='stream-node4', speed
=0)
275 self
.assert_qmp(result
, 'return', {})
277 self
.wait_until_completed(drive
='stream-node4')
278 self
.assert_no_active_block_jobs()
280 # Similar to test_overlapping_1, but with block-commit
281 # blocking the other jobs
282 def test_overlapping_2(self
):
283 self
.assertLessEqual(9, self
.num_imgs
)
284 self
.assert_no_active_block_jobs()
286 # Set a speed limit to make sure that this job blocks the rest
287 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=self
.imgs
[5], base
=self
.imgs
[3], job_id
='commit-node3', speed
=1024*1024)
288 self
.assert_qmp(result
, 'return', {})
290 result
= self
.vm
.qmp('block-stream', device
='node3', job_id
='stream-node3')
291 self
.assert_qmp(result
, 'error/desc',
292 "Node 'node3' is busy: block device is in use by block job: commit")
294 result
= self
.vm
.qmp('block-stream', device
='node6', base
=self
.imgs
[2], job_id
='stream-node6')
295 self
.assert_qmp(result
, 'error/desc',
296 "Node 'node5' is busy: block device is in use by block job: commit")
298 result
= self
.vm
.qmp('block-stream', device
='node4', base
=self
.imgs
[2], job_id
='stream-node4')
299 self
.assert_qmp(result
, 'error/desc',
300 "Node 'node4' is busy: block device is in use by block job: commit")
302 result
= self
.vm
.qmp('block-stream', device
='node6', base
=self
.imgs
[4], job_id
='stream-node6-v2')
303 self
.assert_qmp(result
, 'error/desc',
304 "Node 'node5' is busy: block device is in use by block job: commit")
306 # This fails because block-commit currently blocks the active layer even if it's not used
307 result
= self
.vm
.qmp('block-stream', device
='drive0', base
=self
.imgs
[5], job_id
='stream-drive0')
308 self
.assert_qmp(result
, 'error/desc',
309 "Node 'drive0' is busy: block device is in use by block job: commit")
311 result
= self
.vm
.qmp('block-job-set-speed', device
='commit-node3', speed
=0)
312 self
.assert_qmp(result
, 'return', {})
314 self
.wait_until_completed(drive
='commit-node3')
316 # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter.
317 # Internally this uses a mirror block job, hence the separate test case.
318 def test_overlapping_3(self
):
319 self
.assertLessEqual(8, self
.num_imgs
)
320 self
.assert_no_active_block_jobs()
322 # Set a speed limit to make sure that this job blocks the rest
323 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[3], job_id
='commit-drive0', speed
=1024*1024)
324 self
.assert_qmp(result
, 'return', {})
326 result
= self
.vm
.qmp('block-stream', device
='node5', base
=self
.imgs
[3], job_id
='stream-node6')
327 self
.assert_qmp(result
, 'error/desc',
328 "Node 'node5' is busy: block device is in use by block job: commit")
330 event
= self
.vm
.event_wait(name
='BLOCK_JOB_READY')
331 self
.assert_qmp(event
, 'data/device', 'commit-drive0')
332 self
.assert_qmp(event
, 'data/type', 'commit')
333 self
.assert_qmp_absent(event
, 'data/error')
335 result
= self
.vm
.qmp('block-job-set-speed', device
='commit-drive0', speed
=0)
336 self
.assert_qmp(result
, 'return', {})
338 result
= self
.vm
.qmp('block-job-complete', device
='commit-drive0')
339 self
.assert_qmp(result
, 'return', {})
341 self
.wait_until_completed(drive
='commit-drive0')
343 # In this case the base node of the stream job is the same as the
344 # top node of commit job. Since this results in the commit filter
345 # node being part of the stream chain, this is not allowed.
346 def test_overlapping_4(self
):
347 self
.assert_no_active_block_jobs()
349 # Commit from node2 into node0
350 result
= self
.vm
.qmp('block-commit', device
='drive0',
351 top
=self
.imgs
[2], base
=self
.imgs
[0],
352 filter_node_name
='commit-filter', speed
=1024*1024)
353 self
.assert_qmp(result
, 'return', {})
355 # Stream from node2 into node4
356 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node2', job_id
='node4')
357 self
.assert_qmp(result
, 'error/desc',
358 "Cannot freeze 'backing' link to 'commit-filter'")
360 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=0)
361 self
.assert_qmp(result
, 'return', {})
363 self
.wait_until_completed()
364 self
.assert_no_active_block_jobs()
366 # In this case the base node of the stream job is the commit job's
367 # filter node. stream does not have a real dependency on its base
368 # node, so even though commit removes it when it is done, there is
370 def test_overlapping_5(self
):
371 self
.assert_no_active_block_jobs()
373 # Commit from node2 into node0
374 result
= self
.vm
.qmp('block-commit', device
='drive0',
375 top_node
='node2', base_node
='node0',
376 filter_node_name
='commit-filter', speed
=1024*1024)
377 self
.assert_qmp(result
, 'return', {})
379 # Stream from node2 into node4
380 result
= self
.vm
.qmp('block-stream', device
='node4',
381 base_node
='commit-filter', job_id
='node4')
382 self
.assert_qmp(result
, 'return', {})
384 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=0)
385 self
.assert_qmp(result
, 'return', {})
387 self
.vm
.run_job(job
='drive0', auto_dismiss
=True, use_log
=False)
388 self
.vm
.run_job(job
='node4', auto_dismiss
=True, use_log
=False)
389 self
.assert_no_active_block_jobs()
391 # Test a block-stream and a block-commit job in parallel
392 # Here the stream job is supposed to finish quickly in order to reproduce
393 # the scenario that triggers the bug fixed in 3d5d319e1221 and 1a63a907507
394 def test_stream_commit_1(self
):
395 self
.assertLessEqual(8, self
.num_imgs
)
396 self
.assert_no_active_block_jobs()
398 # Stream from node0 into node2
399 result
= self
.vm
.qmp('block-stream', device
='node2', base_node
='node0', job_id
='node2')
400 self
.assert_qmp(result
, 'return', {})
402 # Commit from the active layer into node3
403 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[3])
404 self
.assert_qmp(result
, 'return', {})
406 # Wait for all jobs to be finished.
407 pending_jobs
= ['node2', 'drive0']
408 while len(pending_jobs
) > 0:
409 for event
in self
.vm
.get_qmp_events(wait
=True):
410 if event
['event'] == 'BLOCK_JOB_COMPLETED':
411 node_name
= self
.dictpath(event
, 'data/device')
412 self
.assertTrue(node_name
in pending_jobs
)
413 self
.assert_qmp_absent(event
, 'data/error')
414 pending_jobs
.remove(node_name
)
415 if event
['event'] == 'BLOCK_JOB_READY':
416 self
.assert_qmp(event
, 'data/device', 'drive0')
417 self
.assert_qmp(event
, 'data/type', 'commit')
418 self
.assert_qmp_absent(event
, 'data/error')
419 self
.assertTrue('drive0' in pending_jobs
)
420 self
.vm
.qmp('block-job-complete', device
='drive0')
422 self
.assert_no_active_block_jobs()
424 # This is similar to test_stream_commit_1 but both jobs are slowed
425 # down so they can run in parallel for a little while.
426 def test_stream_commit_2(self
):
427 self
.assertLessEqual(8, self
.num_imgs
)
428 self
.assert_no_active_block_jobs()
430 # Stream from node0 into node4
431 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node0', job_id
='node4', speed
=1024*1024)
432 self
.assert_qmp(result
, 'return', {})
434 # Commit from the active layer into node5
435 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=self
.imgs
[5], speed
=1024*1024)
436 self
.assert_qmp(result
, 'return', {})
438 for job
in ['drive0', 'node4']:
439 result
= self
.vm
.qmp('block-job-set-speed', device
=job
, speed
=0)
440 self
.assert_qmp(result
, 'return', {})
442 # Wait for all jobs to be finished.
443 pending_jobs
= ['node4', 'drive0']
444 while len(pending_jobs
) > 0:
445 for event
in self
.vm
.get_qmp_events(wait
=True):
446 if event
['event'] == 'BLOCK_JOB_COMPLETED':
447 node_name
= self
.dictpath(event
, 'data/device')
448 self
.assertTrue(node_name
in pending_jobs
)
449 self
.assert_qmp_absent(event
, 'data/error')
450 pending_jobs
.remove(node_name
)
451 if event
['event'] == 'BLOCK_JOB_READY':
452 self
.assert_qmp(event
, 'data/device', 'drive0')
453 self
.assert_qmp(event
, 'data/type', 'commit')
454 self
.assert_qmp_absent(event
, 'data/error')
455 self
.assertTrue('drive0' in pending_jobs
)
456 self
.vm
.qmp('block-job-complete', device
='drive0')
458 self
.assert_no_active_block_jobs()
460 # Test the base_node parameter
461 def test_stream_base_node_name(self
):
462 self
.assert_no_active_block_jobs()
464 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[4]),
465 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.imgs
[3]),
466 'image file map matches backing file before streaming')
468 # Error: the base node does not exist
469 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='none', job_id
='stream')
470 self
.assert_qmp(result
, 'error/desc',
471 'Cannot find device= nor node_name=none')
473 # Error: the base node is not a backing file of the top node
474 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node6', job_id
='stream')
475 self
.assert_qmp(result
, 'error/desc',
476 "Node 'node6' is not a backing image of 'node4'")
478 # Error: the base node is the same as the top node
479 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node4', job_id
='stream')
480 self
.assert_qmp(result
, 'error/desc',
481 "Node 'node4' is not a backing image of 'node4'")
483 # Error: cannot specify 'base' and 'base-node' at the same time
484 result
= self
.vm
.qmp('block-stream', device
='node4', base
=self
.imgs
[2], base_node
='node2', job_id
='stream')
485 self
.assert_qmp(result
, 'error/desc',
486 "'base' and 'base-node' cannot be specified at the same time")
488 # Success: the base node is a backing file of the top node
489 result
= self
.vm
.qmp('block-stream', device
='node4', base_node
='node2', job_id
='stream')
490 self
.assert_qmp(result
, 'return', {})
492 self
.wait_until_completed(drive
='stream')
494 self
.assert_no_active_block_jobs()
497 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[4]),
498 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.imgs
[3]),
499 'image file map matches backing file after streaming')
501 class TestQuorum(iotests
.QMPTestCase
):
507 opts
= ['driver=quorum', 'vote-threshold=2']
509 # Initialize file names and command-line options
510 for i
in range(self
.num_children
):
511 child_img
= os
.path
.join(iotests
.test_dir
, 'img-%d.img' % i
)
512 backing_img
= os
.path
.join(iotests
.test_dir
, 'backing-%d.img' % i
)
513 self
.children
.append(child_img
)
514 self
.backing
.append(backing_img
)
515 qemu_img('create', '-f', iotests
.imgfmt
, backing_img
, '1M')
516 qemu_io('-f', iotests
.imgfmt
,
517 '-c', 'write -P 0x55 0 1024', backing_img
)
518 qemu_img('create', '-f', iotests
.imgfmt
,
519 '-o', 'backing_file=%s' % backing_img
, child_img
)
520 opts
.append("children.%d.file.filename=%s" % (i
, child_img
))
521 opts
.append("children.%d.node-name=node%d" % (i
, i
))
523 # Attach the drive to the VM
524 self
.vm
= iotests
.VM()
525 self
.vm
.add_drive(path
= None, opts
= ','.join(opts
))
530 for img
in self
.children
:
532 for img
in self
.backing
:
535 def test_stream_quorum(self
):
536 if not iotests
.supports_quorum():
539 self
.assertNotEqual(qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.children
[0]),
540 qemu_io('-f', iotests
.imgfmt
, '-rU', '-c', 'map', self
.backing
[0]),
541 'image file map matches backing file before streaming')
543 self
.assert_no_active_block_jobs()
545 result
= self
.vm
.qmp('block-stream', device
='node0', job_id
='stream-node0')
546 self
.assert_qmp(result
, 'return', {})
548 self
.wait_until_completed(drive
='stream-node0')
550 self
.assert_no_active_block_jobs()
553 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.children
[0]),
554 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', self
.backing
[0]),
555 'image file map does not match backing file after streaming')
557 class TestSmallerBackingFile(iotests
.QMPTestCase
):
558 backing_len
= 1 * 1024 * 1024 # MB
559 image_len
= 2 * backing_len
562 iotests
.create_image(backing_img
, self
.backing_len
)
563 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
, str(self
.image_len
))
564 self
.vm
= iotests
.VM().add_drive(test_img
)
567 # If this hangs, then you are missing a fix to complete streaming when the
568 # end of the backing file is reached.
569 def test_stream(self
):
570 self
.assert_no_active_block_jobs()
572 result
= self
.vm
.qmp('block-stream', device
='drive0')
573 self
.assert_qmp(result
, 'return', {})
575 self
.wait_until_completed()
577 self
.assert_no_active_block_jobs()
580 class TestErrors(iotests
.QMPTestCase
):
581 image_len
= 2 * 1024 * 1024 # MB
583 # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
584 STREAM_BUFFER_SIZE
= 512 * 1024
586 def create_blkdebug_file(self
, name
, event
, errno
):
587 file = open(name
, 'w')
606 ''' % (event
, errno
, self
.STREAM_BUFFER_SIZE
// 512, event
, event
))
609 class TestEIO(TestErrors
):
611 self
.blkdebug_file
= backing_img
+ ".blkdebug"
612 iotests
.create_image(backing_img
, TestErrors
.image_len
)
613 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 5)
614 qemu_img('create', '-f', iotests
.imgfmt
,
615 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
616 % (self
.blkdebug_file
, backing_img
),
618 self
.vm
= iotests
.VM().add_drive(test_img
)
624 os
.remove(backing_img
)
625 os
.remove(self
.blkdebug_file
)
627 def test_report(self
):
628 self
.assert_no_active_block_jobs()
630 result
= self
.vm
.qmp('block-stream', device
='drive0')
631 self
.assert_qmp(result
, 'return', {})
636 for event
in self
.vm
.get_qmp_events(wait
=True):
637 if event
['event'] == 'BLOCK_JOB_ERROR':
638 self
.assert_qmp(event
, 'data/device', 'drive0')
639 self
.assert_qmp(event
, 'data/operation', 'read')
641 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
642 self
.assertTrue(error
, 'job completed unexpectedly')
643 self
.assert_qmp(event
, 'data/type', 'stream')
644 self
.assert_qmp(event
, 'data/device', 'drive0')
645 self
.assert_qmp(event
, 'data/error', 'Input/output error')
646 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
647 self
.assert_qmp(event
, 'data/len', self
.image_len
)
649 elif event
['event'] == 'JOB_STATUS_CHANGE':
650 self
.assert_qmp(event
, 'data/id', 'drive0')
652 self
.assert_no_active_block_jobs()
655 def test_ignore(self
):
656 self
.assert_no_active_block_jobs()
658 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='ignore')
659 self
.assert_qmp(result
, 'return', {})
664 for event
in self
.vm
.get_qmp_events(wait
=True):
665 if event
['event'] == 'BLOCK_JOB_ERROR':
667 self
.assert_qmp(event
, 'data/device', 'drive0')
668 self
.assert_qmp(event
, 'data/operation', 'read')
669 result
= self
.vm
.qmp('query-block-jobs')
670 if result
== {'return': []}:
671 # Job finished too quickly
673 self
.assert_qmp(result
, 'return[0]/paused', False)
674 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
675 self
.assertTrue(error
, 'job completed unexpectedly')
676 self
.assert_qmp(event
, 'data/type', 'stream')
677 self
.assert_qmp(event
, 'data/device', 'drive0')
678 self
.assert_qmp(event
, 'data/error', 'Input/output error')
679 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
680 self
.assert_qmp(event
, 'data/len', self
.image_len
)
682 elif event
['event'] == 'JOB_STATUS_CHANGE':
683 self
.assert_qmp(event
, 'data/id', 'drive0')
685 self
.assert_no_active_block_jobs()
689 self
.assert_no_active_block_jobs()
691 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='stop')
692 self
.assert_qmp(result
, 'return', {})
697 for event
in self
.vm
.get_qmp_events(wait
=True):
698 if event
['event'] == 'BLOCK_JOB_ERROR':
700 self
.assert_qmp(event
, 'data/device', 'drive0')
701 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', 'failed')
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
)
725 elif event
['event'] == 'JOB_STATUS_CHANGE':
726 self
.assert_qmp(event
, 'data/id', 'drive0')
728 self
.assert_no_active_block_jobs()
731 def test_enospc(self
):
732 self
.assert_no_active_block_jobs()
734 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
735 self
.assert_qmp(result
, 'return', {})
740 for event
in self
.vm
.get_qmp_events(wait
=True):
741 if event
['event'] == 'BLOCK_JOB_ERROR':
742 self
.assert_qmp(event
, 'data/device', 'drive0')
743 self
.assert_qmp(event
, 'data/operation', 'read')
745 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
746 self
.assertTrue(error
, 'job completed unexpectedly')
747 self
.assert_qmp(event
, 'data/type', 'stream')
748 self
.assert_qmp(event
, 'data/device', 'drive0')
749 self
.assert_qmp(event
, 'data/error', 'Input/output error')
750 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
751 self
.assert_qmp(event
, 'data/len', self
.image_len
)
753 elif event
['event'] == 'JOB_STATUS_CHANGE':
754 self
.assert_qmp(event
, 'data/id', 'drive0')
756 self
.assert_no_active_block_jobs()
759 class TestENOSPC(TestErrors
):
761 self
.blkdebug_file
= backing_img
+ ".blkdebug"
762 iotests
.create_image(backing_img
, TestErrors
.image_len
)
763 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 28)
764 qemu_img('create', '-f', iotests
.imgfmt
,
765 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
766 % (self
.blkdebug_file
, backing_img
),
768 self
.vm
= iotests
.VM().add_drive(test_img
)
774 os
.remove(backing_img
)
775 os
.remove(self
.blkdebug_file
)
777 def test_enospc(self
):
778 self
.assert_no_active_block_jobs()
780 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
781 self
.assert_qmp(result
, 'return', {})
786 for event
in self
.vm
.get_qmp_events(wait
=True):
787 if event
['event'] == 'BLOCK_JOB_ERROR':
788 self
.assert_qmp(event
, 'data/device', 'drive0')
789 self
.assert_qmp(event
, 'data/operation', 'read')
792 result
= self
.vm
.qmp('query-block-jobs')
793 self
.assert_qmp(result
, 'return[0]/paused', True)
794 self
.assert_qmp(result
, 'return[0]/offset', self
.STREAM_BUFFER_SIZE
)
795 self
.assert_qmp(result
, 'return[0]/io-status', 'nospace')
797 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
798 self
.assert_qmp(result
, 'return', {})
800 result
= self
.vm
.qmp('query-block-jobs')
801 if result
== {'return': []}:
802 # Race; likely already finished. Check.
804 self
.assert_qmp(result
, 'return[0]/paused', False)
805 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
806 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
807 self
.assertTrue(error
, 'job completed unexpectedly')
808 self
.assert_qmp(event
, 'data/type', 'stream')
809 self
.assert_qmp(event
, 'data/device', 'drive0')
810 self
.assert_qmp_absent(event
, 'data/error')
811 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
812 self
.assert_qmp(event
, 'data/len', self
.image_len
)
814 elif event
['event'] == 'JOB_STATUS_CHANGE':
815 self
.assert_qmp(event
, 'data/id', 'drive0')
817 self
.assert_no_active_block_jobs()
820 class TestStreamStop(iotests
.QMPTestCase
):
821 image_len
= 8 * 1024 * 1024 * 1024 # GB
824 qemu_img('create', backing_img
, str(TestStreamStop
.image_len
))
825 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
826 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
827 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
828 self
.vm
= iotests
.VM().add_drive("blkdebug::" + test_img
)
834 os
.remove(backing_img
)
836 def test_stream_stop(self
):
837 self
.assert_no_active_block_jobs()
839 self
.vm
.pause_drive('drive0')
840 result
= self
.vm
.qmp('block-stream', device
='drive0')
841 self
.assert_qmp(result
, 'return', {})
844 events
= self
.vm
.get_qmp_events(wait
=False)
846 self
.assert_qmp(e
, 'event', 'JOB_STATUS_CHANGE')
847 self
.assert_qmp(e
, 'data/id', 'drive0')
849 self
.cancel_and_wait(resume
=True)
851 class TestSetSpeed(iotests
.QMPTestCase
):
852 image_len
= 80 * 1024 * 1024 # MB
855 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
856 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
857 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
858 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
859 self
.vm
= iotests
.VM().add_drive('blkdebug::' + test_img
)
865 os
.remove(backing_img
)
867 # This is a short performance test which is not run by default.
868 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
869 def perf_test_throughput(self
):
870 self
.assert_no_active_block_jobs()
872 result
= self
.vm
.qmp('block-stream', device
='drive0')
873 self
.assert_qmp(result
, 'return', {})
875 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
876 self
.assert_qmp(result
, 'return', {})
878 self
.wait_until_completed()
880 self
.assert_no_active_block_jobs()
882 def test_set_speed(self
):
883 self
.assert_no_active_block_jobs()
885 self
.vm
.pause_drive('drive0')
886 result
= self
.vm
.qmp('block-stream', device
='drive0')
887 self
.assert_qmp(result
, 'return', {})
890 result
= self
.vm
.qmp('query-block-jobs')
891 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
892 self
.assert_qmp(result
, 'return[0]/speed', 0)
894 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
895 self
.assert_qmp(result
, 'return', {})
897 # Ensure the speed we set was accepted
898 result
= self
.vm
.qmp('query-block-jobs')
899 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
900 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
902 self
.cancel_and_wait(resume
=True)
903 self
.vm
.pause_drive('drive0')
905 # Check setting speed in block-stream works
906 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=4 * 1024 * 1024)
907 self
.assert_qmp(result
, 'return', {})
909 result
= self
.vm
.qmp('query-block-jobs')
910 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
911 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
913 self
.cancel_and_wait(resume
=True)
915 def test_set_speed_invalid(self
):
916 self
.assert_no_active_block_jobs()
918 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=-1)
919 self
.assert_qmp(result
, 'error/desc', "Invalid parameter 'speed'")
921 self
.assert_no_active_block_jobs()
923 self
.vm
.pause_drive('drive0')
924 result
= self
.vm
.qmp('block-stream', device
='drive0')
925 self
.assert_qmp(result
, 'return', {})
927 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
928 self
.assert_qmp(result
, 'error/desc', "Invalid parameter 'speed'")
930 self
.cancel_and_wait(resume
=True)
932 if __name__
== '__main__':
933 iotests
.main(supported_fmts
=['qcow2', 'qed'])