xref: /openbmc/qemu/tests/qemu-iotests/030 (revision 7d405b2f)
1#!/usr/bin/env python
2#
3# Tests for image streaming.
4#
5# Copyright (C) 2012 IBM Corp.
6#
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.
11#
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.
16#
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/>.
19#
20
21import time
22import os
23import iotests
24from iotests import qemu_img, qemu_io
25
26backing_img = os.path.join(iotests.test_dir, 'backing.img')
27mid_img = os.path.join(iotests.test_dir, 'mid.img')
28test_img = os.path.join(iotests.test_dir, 'test.img')
29
30class TestSingleDrive(iotests.QMPTestCase):
31    image_len = 1 * 1024 * 1024 # MB
32
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()
41
42    def tearDown(self):
43        self.vm.shutdown()
44        os.remove(test_img)
45        os.remove(mid_img)
46        os.remove(backing_img)
47
48    def test_stream(self):
49        self.assert_no_active_block_jobs()
50
51        result = self.vm.qmp('block-stream', device='drive0')
52        self.assert_qmp(result, 'return', {})
53
54        self.wait_until_completed()
55
56        self.assert_no_active_block_jobs()
57        self.vm.shutdown()
58
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')
62
63    def test_stream_intermediate(self):
64        self.assert_no_active_block_jobs()
65
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')
69
70        result = self.vm.qmp('block-stream', device='mid', job_id='stream-mid')
71        self.assert_qmp(result, 'return', {})
72
73        self.wait_until_completed(drive='stream-mid')
74
75        self.assert_no_active_block_jobs()
76        self.vm.shutdown()
77
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')
81
82    def test_stream_pause(self):
83        self.assert_no_active_block_jobs()
84
85        self.vm.pause_drive('drive0')
86        result = self.vm.qmp('block-stream', device='drive0')
87        self.assert_qmp(result, 'return', {})
88
89        self.pause_job('drive0', wait=False)
90        self.vm.resume_drive('drive0')
91        self.pause_wait('drive0')
92
93        result = self.vm.qmp('query-block-jobs')
94        offset = self.dictpath(result, 'return[0]/offset')
95
96        time.sleep(0.5)
97        result = self.vm.qmp('query-block-jobs')
98        self.assert_qmp(result, 'return[0]/offset', offset)
99
100        result = self.vm.qmp('block-job-resume', device='drive0')
101        self.assert_qmp(result, 'return', {})
102
103        self.wait_until_completed()
104
105        self.assert_no_active_block_jobs()
106        self.vm.shutdown()
107
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')
111
112    def test_stream_no_op(self):
113        self.assert_no_active_block_jobs()
114
115        # The image map is empty before the operation
116        empty_map = qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', test_img)
117
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', {})
121
122        self.wait_until_completed()
123
124        self.assert_no_active_block_jobs()
125        self.vm.shutdown()
126
127        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
128                         empty_map, 'image file map changed after a no-op')
129
130    def test_stream_partial(self):
131        self.assert_no_active_block_jobs()
132
133        result = self.vm.qmp('block-stream', device='drive0', base=backing_img)
134        self.assert_qmp(result, 'return', {})
135
136        self.wait_until_completed()
137
138        self.assert_no_active_block_jobs()
139        self.vm.shutdown()
140
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')
144
145    def test_device_not_found(self):
146        result = self.vm.qmp('block-stream', device='nonexistent')
147        self.assert_qmp(result, 'error/class', 'GenericError')
148
149    def test_job_id_missing(self):
150        result = self.vm.qmp('block-stream', device='mid')
151        self.assert_qmp(result, 'error/class', 'GenericError')
152
153
154class 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
158    imgs = []
159
160    def setUp(self):
161        opts = []
162        self.imgs = []
163
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))
169
170        # Create all images
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])
175
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]])
185
186        # Attach the drive to the VM
187        self.vm = iotests.VM()
188        self.vm.add_drive(self.imgs[-1], ','.join(opts))
189        self.vm.launch()
190
191    def tearDown(self):
192        self.vm.shutdown()
193        for img in self.imgs:
194            os.remove(img)
195
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()
200
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')
206
207        # Create all streaming jobs
208        pending_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', {})
215
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)
224
225        self.assert_no_active_block_jobs()
226        self.vm.shutdown()
227
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')
233
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()
238
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', {})
242
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')
245
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')
248
249        result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4-v2')
250        self.assert_qmp(result, 'error/class', 'GenericError')
251
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')
255
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')
258
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')
262
263        self.wait_until_completed(drive='stream-node4')
264        self.assert_no_active_block_jobs()
265
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()
271
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', {})
275
276        result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3')
277        self.assert_qmp(result, 'error/class', 'GenericError')
278
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')
281
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')
284
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')
287
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')
291
292        self.wait_until_completed(drive='commit-node3')
293
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()
299
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', {})
303
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')
306
307        event = self.vm.get_qmp_event(wait=True)
308        self.assertEqual(event['event'], 'BLOCK_JOB_READY')
309        self.assert_qmp(event, 'data/device', 'commit-drive0')
310        self.assert_qmp(event, 'data/type', 'commit')
311        self.assert_qmp_absent(event, 'data/error')
312
313        result = self.vm.qmp('block-job-complete', device='commit-drive0')
314        self.assert_qmp(result, 'return', {})
315
316        self.wait_until_completed(drive='commit-drive0')
317
318    # Test a block-stream and a block-commit job in parallel
319    # Here the stream job is supposed to finish quickly in order to reproduce
320    # the scenario that triggers the bug fixed in 3d5d319e1221 and 1a63a907507
321    def test_stream_commit_1(self):
322        self.assertLessEqual(8, self.num_imgs)
323        self.assert_no_active_block_jobs()
324
325        # Stream from node0 into node2
326        result = self.vm.qmp('block-stream', device='node2', base_node='node0', job_id='node2')
327        self.assert_qmp(result, 'return', {})
328
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', {})
332
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')
348
349        self.assert_no_active_block_jobs()
350
351    # This is similar to test_stream_commit_1 but both jobs are slowed
352    # down so they can run in parallel for a little while.
353    def test_stream_commit_2(self):
354        self.assertLessEqual(8, self.num_imgs)
355        self.assert_no_active_block_jobs()
356
357        # Stream from node0 into node4
358        result = self.vm.qmp('block-stream', device='node4', base_node='node0', job_id='node4', speed=1024*1024)
359        self.assert_qmp(result, 'return', {})
360
361        # Commit from the active layer into node5
362        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[5], speed=1024*1024)
363        self.assert_qmp(result, 'return', {})
364
365        # Wait for all jobs to be finished.
366        pending_jobs = ['node4', 'drive0']
367        while len(pending_jobs) > 0:
368            for event in self.vm.get_qmp_events(wait=True):
369                if event['event'] == 'BLOCK_JOB_COMPLETED':
370                    node_name = self.dictpath(event, 'data/device')
371                    self.assertTrue(node_name in pending_jobs)
372                    self.assert_qmp_absent(event, 'data/error')
373                    pending_jobs.remove(node_name)
374                if event['event'] == 'BLOCK_JOB_READY':
375                    self.assert_qmp(event, 'data/device', 'drive0')
376                    self.assert_qmp(event, 'data/type', 'commit')
377                    self.assert_qmp_absent(event, 'data/error')
378                    self.assertTrue('drive0' in pending_jobs)
379                    self.vm.qmp('block-job-complete', device='drive0')
380
381        self.assert_no_active_block_jobs()
382
383    # Test the base_node parameter
384    def test_stream_base_node_name(self):
385        self.assert_no_active_block_jobs()
386
387        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]),
388                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]),
389                            'image file map matches backing file before streaming')
390
391        # Error: the base node does not exist
392        result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream')
393        self.assert_qmp(result, 'error/class', 'GenericError')
394
395        # Error: the base node is not a backing file of the top node
396        result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream')
397        self.assert_qmp(result, 'error/class', 'GenericError')
398
399        # Error: the base node is the same as the top node
400        result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream')
401        self.assert_qmp(result, 'error/class', 'GenericError')
402
403        # Error: cannot specify 'base' and 'base-node' at the same time
404        result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream')
405        self.assert_qmp(result, 'error/class', 'GenericError')
406
407        # Success: the base node is a backing file of the top node
408        result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream')
409        self.assert_qmp(result, 'return', {})
410
411        self.wait_until_completed(drive='stream')
412
413        self.assert_no_active_block_jobs()
414        self.vm.shutdown()
415
416        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]),
417                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]),
418                         'image file map matches backing file after streaming')
419
420class TestQuorum(iotests.QMPTestCase):
421    num_children = 3
422    children = []
423    backing = []
424
425    def setUp(self):
426        opts = ['driver=quorum', 'vote-threshold=2']
427
428        # Initialize file names and command-line options
429        for i in range(self.num_children):
430            child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i)
431            backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i)
432            self.children.append(child_img)
433            self.backing.append(backing_img)
434            qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
435            qemu_io('-f', iotests.imgfmt,
436                    '-c', 'write -P 0x55 0 1024', backing_img)
437            qemu_img('create', '-f', iotests.imgfmt,
438                     '-o', 'backing_file=%s' % backing_img, child_img)
439            opts.append("children.%d.file.filename=%s" % (i, child_img))
440            opts.append("children.%d.node-name=node%d" % (i, i))
441
442        # Attach the drive to the VM
443        self.vm = iotests.VM()
444        self.vm.add_drive(path = None, opts = ','.join(opts))
445        self.vm.launch()
446
447    def tearDown(self):
448        self.vm.shutdown()
449        for img in self.children:
450            os.remove(img)
451        for img in self.backing:
452            os.remove(img)
453
454    def test_stream_quorum(self):
455        if not iotests.supports_quorum():
456            return
457
458        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]),
459                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]),
460                            'image file map matches backing file before streaming')
461
462        self.assert_no_active_block_jobs()
463
464        result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0')
465        self.assert_qmp(result, 'return', {})
466
467        self.wait_until_completed(drive='stream-node0')
468
469        self.assert_no_active_block_jobs()
470        self.vm.shutdown()
471
472        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]),
473                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]),
474                         'image file map does not match backing file after streaming')
475
476class TestSmallerBackingFile(iotests.QMPTestCase):
477    backing_len = 1 * 1024 * 1024 # MB
478    image_len = 2 * backing_len
479
480    def setUp(self):
481        iotests.create_image(backing_img, self.backing_len)
482        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len))
483        self.vm = iotests.VM().add_drive(test_img)
484        self.vm.launch()
485
486    # If this hangs, then you are missing a fix to complete streaming when the
487    # end of the backing file is reached.
488    def test_stream(self):
489        self.assert_no_active_block_jobs()
490
491        result = self.vm.qmp('block-stream', device='drive0')
492        self.assert_qmp(result, 'return', {})
493
494        self.wait_until_completed()
495
496        self.assert_no_active_block_jobs()
497        self.vm.shutdown()
498
499class TestErrors(iotests.QMPTestCase):
500    image_len = 2 * 1024 * 1024 # MB
501
502    # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
503    STREAM_BUFFER_SIZE = 512 * 1024
504
505    def create_blkdebug_file(self, name, event, errno):
506        file = open(name, 'w')
507        file.write('''
508[inject-error]
509state = "1"
510event = "%s"
511errno = "%d"
512immediately = "off"
513once = "on"
514sector = "%d"
515
516[set-state]
517state = "1"
518event = "%s"
519new_state = "2"
520
521[set-state]
522state = "2"
523event = "%s"
524new_state = "1"
525''' % (event, errno, self.STREAM_BUFFER_SIZE / 512, event, event))
526        file.close()
527
528class TestEIO(TestErrors):
529    def setUp(self):
530        self.blkdebug_file = backing_img + ".blkdebug"
531        iotests.create_image(backing_img, TestErrors.image_len)
532        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
533        qemu_img('create', '-f', iotests.imgfmt,
534                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
535                       % (self.blkdebug_file, backing_img),
536                 test_img)
537        self.vm = iotests.VM().add_drive(test_img)
538        self.vm.launch()
539
540    def tearDown(self):
541        self.vm.shutdown()
542        os.remove(test_img)
543        os.remove(backing_img)
544        os.remove(self.blkdebug_file)
545
546    def test_report(self):
547        self.assert_no_active_block_jobs()
548
549        result = self.vm.qmp('block-stream', device='drive0')
550        self.assert_qmp(result, 'return', {})
551
552        completed = False
553        error = False
554        while not completed:
555            for event in self.vm.get_qmp_events(wait=True):
556                if event['event'] == 'BLOCK_JOB_ERROR':
557                    self.assert_qmp(event, 'data/device', 'drive0')
558                    self.assert_qmp(event, 'data/operation', 'read')
559                    error = True
560                elif event['event'] == 'BLOCK_JOB_COMPLETED':
561                    self.assertTrue(error, 'job completed unexpectedly')
562                    self.assert_qmp(event, 'data/type', 'stream')
563                    self.assert_qmp(event, 'data/device', 'drive0')
564                    self.assert_qmp(event, 'data/error', 'Input/output error')
565                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
566                    self.assert_qmp(event, 'data/len', self.image_len)
567                    completed = True
568
569        self.assert_no_active_block_jobs()
570        self.vm.shutdown()
571
572    def test_ignore(self):
573        self.assert_no_active_block_jobs()
574
575        result = self.vm.qmp('block-stream', device='drive0', on_error='ignore')
576        self.assert_qmp(result, 'return', {})
577
578        error = False
579        completed = False
580        while not completed:
581            for event in self.vm.get_qmp_events(wait=True):
582                if event['event'] == 'BLOCK_JOB_ERROR':
583                    error = True
584                    self.assert_qmp(event, 'data/device', 'drive0')
585                    self.assert_qmp(event, 'data/operation', 'read')
586                    result = self.vm.qmp('query-block-jobs')
587                    if result == {'return': []}:
588                        # Job finished too quickly
589                        continue
590                    self.assert_qmp(result, 'return[0]/paused', False)
591                elif event['event'] == 'BLOCK_JOB_COMPLETED':
592                    self.assertTrue(error, 'job completed unexpectedly')
593                    self.assert_qmp(event, 'data/type', 'stream')
594                    self.assert_qmp(event, 'data/device', 'drive0')
595                    self.assert_qmp(event, 'data/error', 'Input/output error')
596                    self.assert_qmp(event, 'data/offset', self.image_len)
597                    self.assert_qmp(event, 'data/len', self.image_len)
598                    completed = True
599
600        self.assert_no_active_block_jobs()
601        self.vm.shutdown()
602
603    def test_stop(self):
604        self.assert_no_active_block_jobs()
605
606        result = self.vm.qmp('block-stream', device='drive0', on_error='stop')
607        self.assert_qmp(result, 'return', {})
608
609        error = False
610        completed = False
611        while not completed:
612            for event in self.vm.get_qmp_events(wait=True):
613                if event['event'] == 'BLOCK_JOB_ERROR':
614                    error = True
615                    self.assert_qmp(event, 'data/device', 'drive0')
616                    self.assert_qmp(event, 'data/operation', 'read')
617
618                    result = self.vm.qmp('query-block-jobs')
619                    self.assert_qmp(result, 'return[0]/paused', True)
620                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
621                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
622
623                    result = self.vm.qmp('block-job-resume', device='drive0')
624                    self.assert_qmp(result, 'return', {})
625
626                    result = self.vm.qmp('query-block-jobs')
627                    if result == {'return': []}:
628                        # Race; likely already finished. Check.
629                        continue
630                    self.assert_qmp(result, 'return[0]/paused', False)
631                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
632                elif event['event'] == 'BLOCK_JOB_COMPLETED':
633                    self.assertTrue(error, 'job completed unexpectedly')
634                    self.assert_qmp(event, 'data/type', 'stream')
635                    self.assert_qmp(event, 'data/device', 'drive0')
636                    self.assert_qmp_absent(event, 'data/error')
637                    self.assert_qmp(event, 'data/offset', self.image_len)
638                    self.assert_qmp(event, 'data/len', self.image_len)
639                    completed = True
640
641        self.assert_no_active_block_jobs()
642        self.vm.shutdown()
643
644    def test_enospc(self):
645        self.assert_no_active_block_jobs()
646
647        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
648        self.assert_qmp(result, 'return', {})
649
650        completed = False
651        error = False
652        while not completed:
653            for event in self.vm.get_qmp_events(wait=True):
654                if event['event'] == 'BLOCK_JOB_ERROR':
655                    self.assert_qmp(event, 'data/device', 'drive0')
656                    self.assert_qmp(event, 'data/operation', 'read')
657                    error = True
658                elif event['event'] == 'BLOCK_JOB_COMPLETED':
659                    self.assertTrue(error, 'job completed unexpectedly')
660                    self.assert_qmp(event, 'data/type', 'stream')
661                    self.assert_qmp(event, 'data/device', 'drive0')
662                    self.assert_qmp(event, 'data/error', 'Input/output error')
663                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
664                    self.assert_qmp(event, 'data/len', self.image_len)
665                    completed = True
666
667        self.assert_no_active_block_jobs()
668        self.vm.shutdown()
669
670class TestENOSPC(TestErrors):
671    def setUp(self):
672        self.blkdebug_file = backing_img + ".blkdebug"
673        iotests.create_image(backing_img, TestErrors.image_len)
674        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28)
675        qemu_img('create', '-f', iotests.imgfmt,
676                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
677                       % (self.blkdebug_file, backing_img),
678                 test_img)
679        self.vm = iotests.VM().add_drive(test_img)
680        self.vm.launch()
681
682    def tearDown(self):
683        self.vm.shutdown()
684        os.remove(test_img)
685        os.remove(backing_img)
686        os.remove(self.blkdebug_file)
687
688    def test_enospc(self):
689        self.assert_no_active_block_jobs()
690
691        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
692        self.assert_qmp(result, 'return', {})
693
694        error = False
695        completed = False
696        while not completed:
697            for event in self.vm.get_qmp_events(wait=True):
698                if event['event'] == 'BLOCK_JOB_ERROR':
699                    self.assert_qmp(event, 'data/device', 'drive0')
700                    self.assert_qmp(event, 'data/operation', 'read')
701                    error = True
702
703                    result = self.vm.qmp('query-block-jobs')
704                    self.assert_qmp(result, 'return[0]/paused', True)
705                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
706                    self.assert_qmp(result, 'return[0]/io-status', 'nospace')
707
708                    result = self.vm.qmp('block-job-resume', device='drive0')
709                    self.assert_qmp(result, 'return', {})
710
711                    result = self.vm.qmp('query-block-jobs')
712                    if result == {'return': []}:
713                        # Race; likely already finished. Check.
714                        continue
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)
724                    completed = True
725
726        self.assert_no_active_block_jobs()
727        self.vm.shutdown()
728
729class TestStreamStop(iotests.QMPTestCase):
730    image_len = 8 * 1024 * 1024 * 1024 # GB
731
732    def setUp(self):
733        qemu_img('create', backing_img, str(TestStreamStop.image_len))
734        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
735        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
736        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
737        self.vm = iotests.VM().add_drive("blkdebug::" + test_img)
738        self.vm.launch()
739
740    def tearDown(self):
741        self.vm.shutdown()
742        os.remove(test_img)
743        os.remove(backing_img)
744
745    def test_stream_stop(self):
746        self.assert_no_active_block_jobs()
747
748        self.vm.pause_drive('drive0')
749        result = self.vm.qmp('block-stream', device='drive0')
750        self.assert_qmp(result, 'return', {})
751
752        time.sleep(0.1)
753        events = self.vm.get_qmp_events(wait=False)
754        self.assertEqual(events, [], 'unexpected QMP event: %s' % events)
755
756        self.cancel_and_wait(resume=True)
757
758class TestSetSpeed(iotests.QMPTestCase):
759    image_len = 80 * 1024 * 1024 # MB
760
761    def setUp(self):
762        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
763        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
764        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
765        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
766        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
767        self.vm.launch()
768
769    def tearDown(self):
770        self.vm.shutdown()
771        os.remove(test_img)
772        os.remove(backing_img)
773
774    # This is a short performance test which is not run by default.
775    # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
776    def perf_test_throughput(self):
777        self.assert_no_active_block_jobs()
778
779        result = self.vm.qmp('block-stream', device='drive0')
780        self.assert_qmp(result, 'return', {})
781
782        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
783        self.assert_qmp(result, 'return', {})
784
785        self.wait_until_completed()
786
787        self.assert_no_active_block_jobs()
788
789    def test_set_speed(self):
790        self.assert_no_active_block_jobs()
791
792        self.vm.pause_drive('drive0')
793        result = self.vm.qmp('block-stream', device='drive0')
794        self.assert_qmp(result, 'return', {})
795
796        # Default speed is 0
797        result = self.vm.qmp('query-block-jobs')
798        self.assert_qmp(result, 'return[0]/device', 'drive0')
799        self.assert_qmp(result, 'return[0]/speed', 0)
800
801        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
802        self.assert_qmp(result, 'return', {})
803
804        # Ensure the speed we set was accepted
805        result = self.vm.qmp('query-block-jobs')
806        self.assert_qmp(result, 'return[0]/device', 'drive0')
807        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
808
809        self.cancel_and_wait(resume=True)
810        self.vm.pause_drive('drive0')
811
812        # Check setting speed in block-stream works
813        result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024)
814        self.assert_qmp(result, 'return', {})
815
816        result = self.vm.qmp('query-block-jobs')
817        self.assert_qmp(result, 'return[0]/device', 'drive0')
818        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
819
820        self.cancel_and_wait(resume=True)
821
822    def test_set_speed_invalid(self):
823        self.assert_no_active_block_jobs()
824
825        result = self.vm.qmp('block-stream', device='drive0', speed=-1)
826        self.assert_qmp(result, 'error/class', 'GenericError')
827
828        self.assert_no_active_block_jobs()
829
830        self.vm.pause_drive('drive0')
831        result = self.vm.qmp('block-stream', device='drive0')
832        self.assert_qmp(result, 'return', {})
833
834        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
835        self.assert_qmp(result, 'error/class', 'GenericError')
836
837        self.cancel_and_wait(resume=True)
838
839if __name__ == '__main__':
840    iotests.main(supported_fmts=['qcow2', 'qed'])
841