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
)
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_pause(self
):
64 self
.assert_no_active_block_jobs()
66 self
.vm
.pause_drive('drive0')
67 result
= self
.vm
.qmp('block-stream', device
='drive0')
68 self
.assert_qmp(result
, 'return', {})
70 result
= self
.vm
.qmp('block-job-pause', device
='drive0')
71 self
.assert_qmp(result
, 'return', {})
74 result
= self
.vm
.qmp('query-block-jobs')
75 offset
= self
.dictpath(result
, 'return[0]/offset')
78 result
= self
.vm
.qmp('query-block-jobs')
79 self
.assert_qmp(result
, 'return[0]/offset', offset
)
81 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
82 self
.assert_qmp(result
, 'return', {})
84 self
.vm
.resume_drive('drive0')
85 self
.wait_until_completed()
87 self
.assert_no_active_block_jobs()
90 self
.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img
),
91 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', test_img
),
92 'image file map does not match backing file after streaming')
94 def test_stream_no_op(self
):
95 self
.assert_no_active_block_jobs()
97 # The image map is empty before the operation
98 empty_map
= qemu_io('-f', iotests
.imgfmt
, '-c', 'map', test_img
)
100 # This is a no-op: no data should ever be copied from the base image
101 result
= self
.vm
.qmp('block-stream', device
='drive0', base
=mid_img
)
102 self
.assert_qmp(result
, 'return', {})
104 self
.wait_until_completed()
106 self
.assert_no_active_block_jobs()
109 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', test_img
),
110 empty_map
, 'image file map changed after a no-op')
112 def test_stream_partial(self
):
113 self
.assert_no_active_block_jobs()
115 result
= self
.vm
.qmp('block-stream', device
='drive0', base
=backing_img
)
116 self
.assert_qmp(result
, 'return', {})
118 self
.wait_until_completed()
120 self
.assert_no_active_block_jobs()
123 self
.assertEqual(qemu_io('-f', iotests
.imgfmt
, '-c', 'map', mid_img
),
124 qemu_io('-f', iotests
.imgfmt
, '-c', 'map', test_img
),
125 'image file map does not match backing file after streaming')
127 def test_device_not_found(self
):
128 result
= self
.vm
.qmp('block-stream', device
='nonexistent')
129 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
132 class TestSmallerBackingFile(iotests
.QMPTestCase
):
133 backing_len
= 1 * 1024 * 1024 # MB
134 image_len
= 2 * backing_len
137 iotests
.create_image(backing_img
, self
.backing_len
)
138 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
, str(self
.image_len
))
139 self
.vm
= iotests
.VM().add_drive(test_img
)
142 # If this hangs, then you are missing a fix to complete streaming when the
143 # end of the backing file is reached.
144 def test_stream(self
):
145 self
.assert_no_active_block_jobs()
147 result
= self
.vm
.qmp('block-stream', device
='drive0')
148 self
.assert_qmp(result
, 'return', {})
150 self
.wait_until_completed()
152 self
.assert_no_active_block_jobs()
155 class TestErrors(iotests
.QMPTestCase
):
156 image_len
= 2 * 1024 * 1024 # MB
158 # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
159 STREAM_BUFFER_SIZE
= 512 * 1024
161 def create_blkdebug_file(self
, name
, event
, errno
):
162 file = open(name
, 'w')
181 ''' % (event
, errno
, self
.STREAM_BUFFER_SIZE
/ 512, event
, event
))
184 class TestEIO(TestErrors
):
186 self
.blkdebug_file
= backing_img
+ ".blkdebug"
187 iotests
.create_image(backing_img
, TestErrors
.image_len
)
188 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 5)
189 qemu_img('create', '-f', iotests
.imgfmt
,
190 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
191 % (self
.blkdebug_file
, backing_img
),
193 self
.vm
= iotests
.VM().add_drive(test_img
)
199 os
.remove(backing_img
)
200 os
.remove(self
.blkdebug_file
)
202 def test_report(self
):
203 self
.assert_no_active_block_jobs()
205 result
= self
.vm
.qmp('block-stream', device
='drive0')
206 self
.assert_qmp(result
, 'return', {})
211 for event
in self
.vm
.get_qmp_events(wait
=True):
212 if event
['event'] == 'BLOCK_JOB_ERROR':
213 self
.assert_qmp(event
, 'data/device', 'drive0')
214 self
.assert_qmp(event
, 'data/operation', 'read')
216 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
217 self
.assertTrue(error
, 'job completed unexpectedly')
218 self
.assert_qmp(event
, 'data/type', 'stream')
219 self
.assert_qmp(event
, 'data/device', 'drive0')
220 self
.assert_qmp(event
, 'data/error', 'Input/output error')
221 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
222 self
.assert_qmp(event
, 'data/len', self
.image_len
)
225 self
.assert_no_active_block_jobs()
228 def test_ignore(self
):
229 self
.assert_no_active_block_jobs()
231 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='ignore')
232 self
.assert_qmp(result
, 'return', {})
237 for event
in self
.vm
.get_qmp_events(wait
=True):
238 if event
['event'] == 'BLOCK_JOB_ERROR':
239 self
.assert_qmp(event
, 'data/device', 'drive0')
240 self
.assert_qmp(event
, 'data/operation', 'read')
241 result
= self
.vm
.qmp('query-block-jobs')
242 self
.assert_qmp(result
, 'return[0]/paused', False)
244 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
245 self
.assertTrue(error
, 'job completed unexpectedly')
246 self
.assert_qmp(event
, 'data/type', 'stream')
247 self
.assert_qmp(event
, 'data/device', 'drive0')
248 self
.assert_qmp(event
, 'data/error', 'Input/output error')
249 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
250 self
.assert_qmp(event
, 'data/len', self
.image_len
)
253 self
.assert_no_active_block_jobs()
257 self
.assert_no_active_block_jobs()
259 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='stop')
260 self
.assert_qmp(result
, 'return', {})
265 for event
in self
.vm
.get_qmp_events(wait
=True):
266 if event
['event'] == 'BLOCK_JOB_ERROR':
268 self
.assert_qmp(event
, 'data/device', 'drive0')
269 self
.assert_qmp(event
, 'data/operation', 'read')
271 result
= self
.vm
.qmp('query-block-jobs')
272 self
.assert_qmp(result
, 'return[0]/paused', True)
273 self
.assert_qmp(result
, 'return[0]/offset', self
.STREAM_BUFFER_SIZE
)
274 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
276 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
277 self
.assert_qmp(result
, 'return', {})
279 result
= self
.vm
.qmp('query-block-jobs')
280 if result
== {'return': []}:
281 # Race; likely already finished. Check.
283 self
.assert_qmp(result
, 'return[0]/paused', False)
284 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
285 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
286 self
.assertTrue(error
, 'job completed unexpectedly')
287 self
.assert_qmp(event
, 'data/type', 'stream')
288 self
.assert_qmp(event
, 'data/device', 'drive0')
289 self
.assert_qmp_absent(event
, 'data/error')
290 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
291 self
.assert_qmp(event
, 'data/len', self
.image_len
)
294 self
.assert_no_active_block_jobs()
297 def test_enospc(self
):
298 self
.assert_no_active_block_jobs()
300 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
301 self
.assert_qmp(result
, 'return', {})
306 for event
in self
.vm
.get_qmp_events(wait
=True):
307 if event
['event'] == 'BLOCK_JOB_ERROR':
308 self
.assert_qmp(event
, 'data/device', 'drive0')
309 self
.assert_qmp(event
, 'data/operation', 'read')
311 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
312 self
.assertTrue(error
, 'job completed unexpectedly')
313 self
.assert_qmp(event
, 'data/type', 'stream')
314 self
.assert_qmp(event
, 'data/device', 'drive0')
315 self
.assert_qmp(event
, 'data/error', 'Input/output error')
316 self
.assert_qmp(event
, 'data/offset', self
.STREAM_BUFFER_SIZE
)
317 self
.assert_qmp(event
, 'data/len', self
.image_len
)
320 self
.assert_no_active_block_jobs()
323 class TestENOSPC(TestErrors
):
325 self
.blkdebug_file
= backing_img
+ ".blkdebug"
326 iotests
.create_image(backing_img
, TestErrors
.image_len
)
327 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 28)
328 qemu_img('create', '-f', iotests
.imgfmt
,
329 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
330 % (self
.blkdebug_file
, backing_img
),
332 self
.vm
= iotests
.VM().add_drive(test_img
)
338 os
.remove(backing_img
)
339 os
.remove(self
.blkdebug_file
)
341 def test_enospc(self
):
342 self
.assert_no_active_block_jobs()
344 result
= self
.vm
.qmp('block-stream', device
='drive0', on_error
='enospc')
345 self
.assert_qmp(result
, 'return', {})
350 for event
in self
.vm
.get_qmp_events(wait
=True):
351 if event
['event'] == 'BLOCK_JOB_ERROR':
352 self
.assert_qmp(event
, 'data/device', 'drive0')
353 self
.assert_qmp(event
, 'data/operation', 'read')
355 result
= self
.vm
.qmp('query-block-jobs')
356 self
.assert_qmp(result
, 'return[0]/paused', True)
357 self
.assert_qmp(result
, 'return[0]/offset', self
.STREAM_BUFFER_SIZE
)
358 self
.assert_qmp(result
, 'return[0]/io-status', 'nospace')
360 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
361 self
.assert_qmp(result
, 'return', {})
363 result
= self
.vm
.qmp('query-block-jobs')
364 self
.assert_qmp(result
, 'return[0]/paused', False)
365 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
367 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
368 self
.assertTrue(error
, 'job completed unexpectedly')
369 self
.assert_qmp(event
, 'data/type', 'stream')
370 self
.assert_qmp(event
, 'data/device', 'drive0')
371 self
.assert_qmp_absent(event
, 'data/error')
372 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
373 self
.assert_qmp(event
, 'data/len', self
.image_len
)
376 self
.assert_no_active_block_jobs()
379 class TestStreamStop(iotests
.QMPTestCase
):
380 image_len
= 8 * 1024 * 1024 * 1024 # GB
383 qemu_img('create', backing_img
, str(TestStreamStop
.image_len
))
384 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
385 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
386 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
387 self
.vm
= iotests
.VM().add_drive("blkdebug::" + test_img
)
393 os
.remove(backing_img
)
395 def test_stream_stop(self
):
396 self
.assert_no_active_block_jobs()
398 self
.vm
.pause_drive('drive0')
399 result
= self
.vm
.qmp('block-stream', device
='drive0')
400 self
.assert_qmp(result
, 'return', {})
403 events
= self
.vm
.get_qmp_events(wait
=False)
404 self
.assertEqual(events
, [], 'unexpected QMP event: %s' % events
)
406 self
.cancel_and_wait(resume
=True)
408 class TestSetSpeed(iotests
.QMPTestCase
):
409 image_len
= 80 * 1024 * 1024 # MB
412 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
413 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img
)
414 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
415 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 32M 32M', test_img
)
416 self
.vm
= iotests
.VM().add_drive('blkdebug::' + test_img
)
422 os
.remove(backing_img
)
424 # This is a short performance test which is not run by default.
425 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
426 def perf_test_throughput(self
):
427 self
.assert_no_active_block_jobs()
429 result
= self
.vm
.qmp('block-stream', device
='drive0')
430 self
.assert_qmp(result
, 'return', {})
432 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
433 self
.assert_qmp(result
, 'return', {})
435 self
.wait_until_completed()
437 self
.assert_no_active_block_jobs()
439 def test_set_speed(self
):
440 self
.assert_no_active_block_jobs()
442 self
.vm
.pause_drive('drive0')
443 result
= self
.vm
.qmp('block-stream', device
='drive0')
444 self
.assert_qmp(result
, 'return', {})
447 result
= self
.vm
.qmp('query-block-jobs')
448 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
449 self
.assert_qmp(result
, 'return[0]/speed', 0)
451 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
452 self
.assert_qmp(result
, 'return', {})
454 # Ensure the speed we set was accepted
455 result
= self
.vm
.qmp('query-block-jobs')
456 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
457 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
459 self
.cancel_and_wait(resume
=True)
460 self
.vm
.pause_drive('drive0')
462 # Check setting speed in block-stream works
463 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=4 * 1024 * 1024)
464 self
.assert_qmp(result
, 'return', {})
466 result
= self
.vm
.qmp('query-block-jobs')
467 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
468 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
470 self
.cancel_and_wait(resume
=True)
472 def test_set_speed_invalid(self
):
473 self
.assert_no_active_block_jobs()
475 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=-1)
476 self
.assert_qmp(result
, 'error/class', 'GenericError')
478 self
.assert_no_active_block_jobs()
480 result
= self
.vm
.qmp('block-stream', device
='drive0')
481 self
.assert_qmp(result
, 'return', {})
483 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
484 self
.assert_qmp(result
, 'error/class', 'GenericError')
486 self
.cancel_and_wait()
488 if __name__
== '__main__':
489 iotests
.main(supported_fmts
=['qcow2', 'qed'])