ui: avoid sign extension using client width/height
[qemu/ar7.git] / tests / qemu-iotests / 030
blob457984b8e9c7f30d7cc59b9f226b0f8e143063ab
1 #!/usr/bin/env python
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/>.
21 import time
22 import os
23 import iotests
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
33 def setUp(self):
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")
40 self.vm.launch()
42 def tearDown(self):
43 self.vm.shutdown()
44 os.remove(test_img)
45 os.remove(mid_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()
57 self.vm.shutdown()
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()
76 self.vm.shutdown()
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 result = self.vm.qmp('block-job-pause', device='drive0')
90 self.assert_qmp(result, 'return', {})
92 self.vm.resume_drive('drive0')
93 self.pause_job('drive0')
95 result = self.vm.qmp('query-block-jobs')
96 offset = self.dictpath(result, 'return[0]/offset')
98 time.sleep(0.5)
99 result = self.vm.qmp('query-block-jobs')
100 self.assert_qmp(result, 'return[0]/offset', offset)
102 result = self.vm.qmp('block-job-resume', device='drive0')
103 self.assert_qmp(result, 'return', {})
105 self.wait_until_completed()
107 self.assert_no_active_block_jobs()
108 self.vm.shutdown()
110 self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img),
111 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
112 'image file map does not match backing file after streaming')
114 def test_stream_no_op(self):
115 self.assert_no_active_block_jobs()
117 # The image map is empty before the operation
118 empty_map = qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', test_img)
120 # This is a no-op: no data should ever be copied from the base image
121 result = self.vm.qmp('block-stream', device='drive0', base=mid_img)
122 self.assert_qmp(result, 'return', {})
124 self.wait_until_completed()
126 self.assert_no_active_block_jobs()
127 self.vm.shutdown()
129 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
130 empty_map, 'image file map changed after a no-op')
132 def test_stream_partial(self):
133 self.assert_no_active_block_jobs()
135 result = self.vm.qmp('block-stream', device='drive0', base=backing_img)
136 self.assert_qmp(result, 'return', {})
138 self.wait_until_completed()
140 self.assert_no_active_block_jobs()
141 self.vm.shutdown()
143 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img),
144 qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
145 'image file map does not match backing file after streaming')
147 def test_device_not_found(self):
148 result = self.vm.qmp('block-stream', device='nonexistent')
149 self.assert_qmp(result, 'error/class', 'GenericError')
151 def test_job_id_missing(self):
152 result = self.vm.qmp('block-stream', device='mid')
153 self.assert_qmp(result, 'error/class', 'GenericError')
156 class TestParallelOps(iotests.QMPTestCase):
157 num_ops = 4 # Number of parallel block-stream operations
158 num_imgs = num_ops * 2 + 1
159 image_len = num_ops * 1024 * 1024
160 imgs = []
162 def setUp(self):
163 opts = []
164 self.imgs = []
166 # Initialize file names and command-line options
167 for i in range(self.num_imgs):
168 img_depth = self.num_imgs - i - 1
169 opts.append("backing." * img_depth + "node-name=node%d" % i)
170 self.imgs.append(os.path.join(iotests.test_dir, 'img-%d.img' % i))
172 # Create all images
173 iotests.create_image(self.imgs[0], self.image_len)
174 for i in range(1, self.num_imgs):
175 qemu_img('create', '-f', iotests.imgfmt,
176 '-o', 'backing_file=%s' % self.imgs[i-1], self.imgs[i])
178 # Put data into the images we are copying data from
179 for i in range(self.num_imgs / 2):
180 img_index = i * 2 + 1
181 # Alternate between 512k and 1M.
182 # This way jobs will not finish in the same order they were created
183 num_kb = 512 + 512 * (i % 2)
184 qemu_io('-f', iotests.imgfmt,
185 '-c', 'write -P %d %d %d' % (i, i*1024*1024, num_kb * 1024),
186 self.imgs[img_index])
188 # Attach the drive to the VM
189 self.vm = iotests.VM()
190 self.vm.add_drive(self.imgs[-1], ','.join(opts))
191 self.vm.launch()
193 def tearDown(self):
194 self.vm.shutdown()
195 for img in self.imgs:
196 os.remove(img)
198 # Test that it's possible to run several block-stream operations
199 # in parallel in the same snapshot chain
200 def test_stream_parallel(self):
201 self.assert_no_active_block_jobs()
203 # Check that the maps don't match before the streaming operations
204 for i in range(2, self.num_imgs, 2):
205 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i]),
206 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i-1]),
207 'image file map matches backing file before streaming')
209 # Create all streaming jobs
210 pending_jobs = []
211 for i in range(2, self.num_imgs, 2):
212 node_name = 'node%d' % i
213 job_id = 'stream-%s' % node_name
214 pending_jobs.append(job_id)
215 result = self.vm.qmp('block-stream', device=node_name, job_id=job_id, base=self.imgs[i-2], speed=512*1024)
216 self.assert_qmp(result, 'return', {})
218 # Wait for all jobs to be finished.
219 while len(pending_jobs) > 0:
220 for event in self.vm.get_qmp_events(wait=True):
221 if event['event'] == 'BLOCK_JOB_COMPLETED':
222 job_id = self.dictpath(event, 'data/device')
223 self.assertTrue(job_id in pending_jobs)
224 self.assert_qmp_absent(event, 'data/error')
225 pending_jobs.remove(job_id)
227 self.assert_no_active_block_jobs()
228 self.vm.shutdown()
230 # Check that all maps match now
231 for i in range(2, self.num_imgs, 2):
232 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i]),
233 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i-1]),
234 'image file map does not match backing file after streaming')
236 # Test that it's not possible to perform two block-stream
237 # operations if there are nodes involved in both.
238 def test_overlapping_1(self):
239 self.assert_no_active_block_jobs()
241 # Set a speed limit to make sure that this job blocks the rest
242 result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4', base=self.imgs[1], speed=1024*1024)
243 self.assert_qmp(result, 'return', {})
245 result = self.vm.qmp('block-stream', device='node5', job_id='stream-node5', base=self.imgs[2])
246 self.assert_qmp(result, 'error/class', 'GenericError')
248 result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3', base=self.imgs[2])
249 self.assert_qmp(result, 'error/class', 'GenericError')
251 result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4-v2')
252 self.assert_qmp(result, 'error/class', 'GenericError')
254 # block-commit should also fail if it touches nodes used by the stream job
255 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[4], job_id='commit-node4')
256 self.assert_qmp(result, 'error/class', 'GenericError')
258 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[1], top=self.imgs[3], job_id='commit-node1')
259 self.assert_qmp(result, 'error/class', 'GenericError')
261 # This fails because it needs to modify the backing string in node2, which is blocked
262 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[0], top=self.imgs[1], job_id='commit-node0')
263 self.assert_qmp(result, 'error/class', 'GenericError')
265 self.wait_until_completed(drive='stream-node4')
266 self.assert_no_active_block_jobs()
268 # Similar to test_overlapping_1, but with block-commit
269 # blocking the other jobs
270 def test_overlapping_2(self):
271 self.assertLessEqual(9, self.num_imgs)
272 self.assert_no_active_block_jobs()
274 # Set a speed limit to make sure that this job blocks the rest
275 result = self.vm.qmp('block-commit', device='drive0', top=self.imgs[5], base=self.imgs[3], job_id='commit-node3', speed=1024*1024)
276 self.assert_qmp(result, 'return', {})
278 result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3')
279 self.assert_qmp(result, 'error/class', 'GenericError')
281 result = self.vm.qmp('block-stream', device='node6', base=self.imgs[2], job_id='stream-node6')
282 self.assert_qmp(result, 'error/class', 'GenericError')
284 result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], job_id='stream-node4')
285 self.assert_qmp(result, 'error/class', 'GenericError')
287 result = self.vm.qmp('block-stream', device='node6', base=self.imgs[4], job_id='stream-node6-v2')
288 self.assert_qmp(result, 'error/class', 'GenericError')
290 # This fails because block-commit currently blocks the active layer even if it's not used
291 result = self.vm.qmp('block-stream', device='drive0', base=self.imgs[5], job_id='stream-drive0')
292 self.assert_qmp(result, 'error/class', 'GenericError')
294 self.wait_until_completed(drive='commit-node3')
296 # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter.
297 # Internally this uses a mirror block job, hence the separate test case.
298 def test_overlapping_3(self):
299 self.assertLessEqual(8, self.num_imgs)
300 self.assert_no_active_block_jobs()
302 # Set a speed limit to make sure that this job blocks the rest
303 result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3], job_id='commit-drive0', speed=1024*1024)
304 self.assert_qmp(result, 'return', {})
306 result = self.vm.qmp('block-stream', device='node5', base=self.imgs[3], job_id='stream-node6')
307 self.assert_qmp(result, 'error/class', 'GenericError')
309 event = self.vm.get_qmp_event(wait=True)
310 self.assertEqual(event['event'], 'BLOCK_JOB_READY')
311 self.assert_qmp(event, 'data/device', 'commit-drive0')
312 self.assert_qmp(event, 'data/type', 'commit')
313 self.assert_qmp_absent(event, 'data/error')
315 result = self.vm.qmp('block-job-complete', device='commit-drive0')
316 self.assert_qmp(result, 'return', {})
318 self.wait_until_completed(drive='commit-drive0')
320 # Test a block-stream and a block-commit job in parallel
321 def test_stream_commit(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', 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 # Test the base_node parameter
352 def test_stream_base_node_name(self):
353 self.assert_no_active_block_jobs()
355 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]),
356 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]),
357 'image file map matches backing file before streaming')
359 # Error: the base node does not exist
360 result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream')
361 self.assert_qmp(result, 'error/class', 'GenericError')
363 # Error: the base node is not a backing file of the top node
364 result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream')
365 self.assert_qmp(result, 'error/class', 'GenericError')
367 # Error: the base node is the same as the top node
368 result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream')
369 self.assert_qmp(result, 'error/class', 'GenericError')
371 # Error: cannot specify 'base' and 'base-node' at the same time
372 result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream')
373 self.assert_qmp(result, 'error/class', 'GenericError')
375 # Success: the base node is a backing file of the top node
376 result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream')
377 self.assert_qmp(result, 'return', {})
379 self.wait_until_completed(drive='stream')
381 self.assert_no_active_block_jobs()
382 self.vm.shutdown()
384 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]),
385 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]),
386 'image file map matches backing file after streaming')
388 class TestQuorum(iotests.QMPTestCase):
389 num_children = 3
390 children = []
391 backing = []
393 def setUp(self):
394 opts = ['driver=quorum', 'vote-threshold=2']
396 # Initialize file names and command-line options
397 for i in range(self.num_children):
398 child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i)
399 backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i)
400 self.children.append(child_img)
401 self.backing.append(backing_img)
402 qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
403 qemu_io('-f', iotests.imgfmt,
404 '-c', 'write -P 0x55 0 1024', backing_img)
405 qemu_img('create', '-f', iotests.imgfmt,
406 '-o', 'backing_file=%s' % backing_img, child_img)
407 opts.append("children.%d.file.filename=%s" % (i, child_img))
408 opts.append("children.%d.node-name=node%d" % (i, i))
410 # Attach the drive to the VM
411 self.vm = iotests.VM()
412 self.vm.add_drive(path = None, opts = ','.join(opts))
413 self.vm.launch()
415 def tearDown(self):
416 self.vm.shutdown()
417 for img in self.children:
418 os.remove(img)
419 for img in self.backing:
420 os.remove(img)
422 def test_stream_quorum(self):
423 if not iotests.supports_quorum():
424 return
426 self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]),
427 qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]),
428 'image file map matches backing file before streaming')
430 self.assert_no_active_block_jobs()
432 result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0')
433 self.assert_qmp(result, 'return', {})
435 self.wait_until_completed(drive='stream-node0')
437 self.assert_no_active_block_jobs()
438 self.vm.shutdown()
440 self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]),
441 qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]),
442 'image file map does not match backing file after streaming')
444 class TestSmallerBackingFile(iotests.QMPTestCase):
445 backing_len = 1 * 1024 * 1024 # MB
446 image_len = 2 * backing_len
448 def setUp(self):
449 iotests.create_image(backing_img, self.backing_len)
450 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len))
451 self.vm = iotests.VM().add_drive(test_img)
452 self.vm.launch()
454 # If this hangs, then you are missing a fix to complete streaming when the
455 # end of the backing file is reached.
456 def test_stream(self):
457 self.assert_no_active_block_jobs()
459 result = self.vm.qmp('block-stream', device='drive0')
460 self.assert_qmp(result, 'return', {})
462 self.wait_until_completed()
464 self.assert_no_active_block_jobs()
465 self.vm.shutdown()
467 class TestErrors(iotests.QMPTestCase):
468 image_len = 2 * 1024 * 1024 # MB
470 # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
471 STREAM_BUFFER_SIZE = 512 * 1024
473 def create_blkdebug_file(self, name, event, errno):
474 file = open(name, 'w')
475 file.write('''
476 [inject-error]
477 state = "1"
478 event = "%s"
479 errno = "%d"
480 immediately = "off"
481 once = "on"
482 sector = "%d"
484 [set-state]
485 state = "1"
486 event = "%s"
487 new_state = "2"
489 [set-state]
490 state = "2"
491 event = "%s"
492 new_state = "1"
493 ''' % (event, errno, self.STREAM_BUFFER_SIZE / 512, event, event))
494 file.close()
496 class TestEIO(TestErrors):
497 def setUp(self):
498 self.blkdebug_file = backing_img + ".blkdebug"
499 iotests.create_image(backing_img, TestErrors.image_len)
500 self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
501 qemu_img('create', '-f', iotests.imgfmt,
502 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
503 % (self.blkdebug_file, backing_img),
504 test_img)
505 self.vm = iotests.VM().add_drive(test_img)
506 self.vm.launch()
508 def tearDown(self):
509 self.vm.shutdown()
510 os.remove(test_img)
511 os.remove(backing_img)
512 os.remove(self.blkdebug_file)
514 def test_report(self):
515 self.assert_no_active_block_jobs()
517 result = self.vm.qmp('block-stream', device='drive0')
518 self.assert_qmp(result, 'return', {})
520 completed = False
521 error = False
522 while not completed:
523 for event in self.vm.get_qmp_events(wait=True):
524 if event['event'] == 'BLOCK_JOB_ERROR':
525 self.assert_qmp(event, 'data/device', 'drive0')
526 self.assert_qmp(event, 'data/operation', 'read')
527 error = True
528 elif event['event'] == 'BLOCK_JOB_COMPLETED':
529 self.assertTrue(error, 'job completed unexpectedly')
530 self.assert_qmp(event, 'data/type', 'stream')
531 self.assert_qmp(event, 'data/device', 'drive0')
532 self.assert_qmp(event, 'data/error', 'Input/output error')
533 self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
534 self.assert_qmp(event, 'data/len', self.image_len)
535 completed = True
537 self.assert_no_active_block_jobs()
538 self.vm.shutdown()
540 def test_ignore(self):
541 self.assert_no_active_block_jobs()
543 result = self.vm.qmp('block-stream', device='drive0', on_error='ignore')
544 self.assert_qmp(result, 'return', {})
546 error = False
547 completed = False
548 while not completed:
549 for event in self.vm.get_qmp_events(wait=True):
550 if event['event'] == 'BLOCK_JOB_ERROR':
551 error = True
552 self.assert_qmp(event, 'data/device', 'drive0')
553 self.assert_qmp(event, 'data/operation', 'read')
554 result = self.vm.qmp('query-block-jobs')
555 if result == {'return': []}:
556 # Job finished too quickly
557 continue
558 self.assert_qmp(result, 'return[0]/paused', False)
559 elif event['event'] == 'BLOCK_JOB_COMPLETED':
560 self.assertTrue(error, 'job completed unexpectedly')
561 self.assert_qmp(event, 'data/type', 'stream')
562 self.assert_qmp(event, 'data/device', 'drive0')
563 self.assert_qmp(event, 'data/error', 'Input/output error')
564 self.assert_qmp(event, 'data/offset', self.image_len)
565 self.assert_qmp(event, 'data/len', self.image_len)
566 completed = True
568 self.assert_no_active_block_jobs()
569 self.vm.shutdown()
571 def test_stop(self):
572 self.assert_no_active_block_jobs()
574 result = self.vm.qmp('block-stream', device='drive0', on_error='stop')
575 self.assert_qmp(result, 'return', {})
577 error = False
578 completed = False
579 while not completed:
580 for event in self.vm.get_qmp_events(wait=True):
581 if event['event'] == 'BLOCK_JOB_ERROR':
582 error = True
583 self.assert_qmp(event, 'data/device', 'drive0')
584 self.assert_qmp(event, 'data/operation', 'read')
586 result = self.vm.qmp('query-block-jobs')
587 self.assert_qmp(result, 'return[0]/paused', True)
588 self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
589 self.assert_qmp(result, 'return[0]/io-status', 'failed')
591 result = self.vm.qmp('block-job-resume', device='drive0')
592 self.assert_qmp(result, 'return', {})
594 result = self.vm.qmp('query-block-jobs')
595 if result == {'return': []}:
596 # Race; likely already finished. Check.
597 continue
598 self.assert_qmp(result, 'return[0]/paused', False)
599 self.assert_qmp(result, 'return[0]/io-status', 'ok')
600 elif event['event'] == 'BLOCK_JOB_COMPLETED':
601 self.assertTrue(error, 'job completed unexpectedly')
602 self.assert_qmp(event, 'data/type', 'stream')
603 self.assert_qmp(event, 'data/device', 'drive0')
604 self.assert_qmp_absent(event, 'data/error')
605 self.assert_qmp(event, 'data/offset', self.image_len)
606 self.assert_qmp(event, 'data/len', self.image_len)
607 completed = True
609 self.assert_no_active_block_jobs()
610 self.vm.shutdown()
612 def test_enospc(self):
613 self.assert_no_active_block_jobs()
615 result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
616 self.assert_qmp(result, 'return', {})
618 completed = False
619 error = False
620 while not completed:
621 for event in self.vm.get_qmp_events(wait=True):
622 if event['event'] == 'BLOCK_JOB_ERROR':
623 self.assert_qmp(event, 'data/device', 'drive0')
624 self.assert_qmp(event, 'data/operation', 'read')
625 error = True
626 elif event['event'] == 'BLOCK_JOB_COMPLETED':
627 self.assertTrue(error, 'job completed unexpectedly')
628 self.assert_qmp(event, 'data/type', 'stream')
629 self.assert_qmp(event, 'data/device', 'drive0')
630 self.assert_qmp(event, 'data/error', 'Input/output error')
631 self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
632 self.assert_qmp(event, 'data/len', self.image_len)
633 completed = True
635 self.assert_no_active_block_jobs()
636 self.vm.shutdown()
638 class TestENOSPC(TestErrors):
639 def setUp(self):
640 self.blkdebug_file = backing_img + ".blkdebug"
641 iotests.create_image(backing_img, TestErrors.image_len)
642 self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28)
643 qemu_img('create', '-f', iotests.imgfmt,
644 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
645 % (self.blkdebug_file, backing_img),
646 test_img)
647 self.vm = iotests.VM().add_drive(test_img)
648 self.vm.launch()
650 def tearDown(self):
651 self.vm.shutdown()
652 os.remove(test_img)
653 os.remove(backing_img)
654 os.remove(self.blkdebug_file)
656 def test_enospc(self):
657 self.assert_no_active_block_jobs()
659 result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
660 self.assert_qmp(result, 'return', {})
662 error = False
663 completed = False
664 while not completed:
665 for event in self.vm.get_qmp_events(wait=True):
666 if event['event'] == 'BLOCK_JOB_ERROR':
667 self.assert_qmp(event, 'data/device', 'drive0')
668 self.assert_qmp(event, 'data/operation', 'read')
669 error = True
671 result = self.vm.qmp('query-block-jobs')
672 self.assert_qmp(result, 'return[0]/paused', True)
673 self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
674 self.assert_qmp(result, 'return[0]/io-status', 'nospace')
676 result = self.vm.qmp('block-job-resume', device='drive0')
677 self.assert_qmp(result, 'return', {})
679 result = self.vm.qmp('query-block-jobs')
680 if result == {'return': []}:
681 # Race; likely already finished. Check.
682 continue
683 self.assert_qmp(result, 'return[0]/paused', False)
684 self.assert_qmp(result, 'return[0]/io-status', 'ok')
685 elif event['event'] == 'BLOCK_JOB_COMPLETED':
686 self.assertTrue(error, 'job completed unexpectedly')
687 self.assert_qmp(event, 'data/type', 'stream')
688 self.assert_qmp(event, 'data/device', 'drive0')
689 self.assert_qmp_absent(event, 'data/error')
690 self.assert_qmp(event, 'data/offset', self.image_len)
691 self.assert_qmp(event, 'data/len', self.image_len)
692 completed = True
694 self.assert_no_active_block_jobs()
695 self.vm.shutdown()
697 class TestStreamStop(iotests.QMPTestCase):
698 image_len = 8 * 1024 * 1024 * 1024 # GB
700 def setUp(self):
701 qemu_img('create', backing_img, str(TestStreamStop.image_len))
702 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
703 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
704 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
705 self.vm = iotests.VM().add_drive("blkdebug::" + test_img)
706 self.vm.launch()
708 def tearDown(self):
709 self.vm.shutdown()
710 os.remove(test_img)
711 os.remove(backing_img)
713 def test_stream_stop(self):
714 self.assert_no_active_block_jobs()
716 self.vm.pause_drive('drive0')
717 result = self.vm.qmp('block-stream', device='drive0')
718 self.assert_qmp(result, 'return', {})
720 time.sleep(0.1)
721 events = self.vm.get_qmp_events(wait=False)
722 self.assertEqual(events, [], 'unexpected QMP event: %s' % events)
724 self.cancel_and_wait(resume=True)
726 class TestSetSpeed(iotests.QMPTestCase):
727 image_len = 80 * 1024 * 1024 # MB
729 def setUp(self):
730 qemu_img('create', backing_img, str(TestSetSpeed.image_len))
731 qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
732 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
733 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
734 self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
735 self.vm.launch()
737 def tearDown(self):
738 self.vm.shutdown()
739 os.remove(test_img)
740 os.remove(backing_img)
742 # This is a short performance test which is not run by default.
743 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
744 def perf_test_throughput(self):
745 self.assert_no_active_block_jobs()
747 result = self.vm.qmp('block-stream', device='drive0')
748 self.assert_qmp(result, 'return', {})
750 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
751 self.assert_qmp(result, 'return', {})
753 self.wait_until_completed()
755 self.assert_no_active_block_jobs()
757 def test_set_speed(self):
758 self.assert_no_active_block_jobs()
760 self.vm.pause_drive('drive0')
761 result = self.vm.qmp('block-stream', device='drive0')
762 self.assert_qmp(result, 'return', {})
764 # Default speed is 0
765 result = self.vm.qmp('query-block-jobs')
766 self.assert_qmp(result, 'return[0]/device', 'drive0')
767 self.assert_qmp(result, 'return[0]/speed', 0)
769 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
770 self.assert_qmp(result, 'return', {})
772 # Ensure the speed we set was accepted
773 result = self.vm.qmp('query-block-jobs')
774 self.assert_qmp(result, 'return[0]/device', 'drive0')
775 self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
777 self.cancel_and_wait(resume=True)
778 self.vm.pause_drive('drive0')
780 # Check setting speed in block-stream works
781 result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024)
782 self.assert_qmp(result, 'return', {})
784 result = self.vm.qmp('query-block-jobs')
785 self.assert_qmp(result, 'return[0]/device', 'drive0')
786 self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
788 self.cancel_and_wait(resume=True)
790 def test_set_speed_invalid(self):
791 self.assert_no_active_block_jobs()
793 result = self.vm.qmp('block-stream', device='drive0', speed=-1)
794 self.assert_qmp(result, 'error/class', 'GenericError')
796 self.assert_no_active_block_jobs()
798 self.vm.pause_drive('drive0')
799 result = self.vm.qmp('block-stream', device='drive0')
800 self.assert_qmp(result, 'return', {})
802 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
803 self.assert_qmp(result, 'error/class', 'GenericError')
805 self.cancel_and_wait(resume=True)
807 if __name__ == '__main__':
808 iotests.main(supported_fmts=['qcow2', 'qed'])