xref: /openbmc/qemu/tests/qemu-iotests/030 (revision 3d9569b8)
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.event_wait(name='BLOCK_JOB_READY')
308        self.assert_qmp(event, 'data/device', 'commit-drive0')
309        self.assert_qmp(event, 'data/type', 'commit')
310        self.assert_qmp_absent(event, 'data/error')
311
312        result = self.vm.qmp('block-job-complete', device='commit-drive0')
313        self.assert_qmp(result, 'return', {})
314
315        self.wait_until_completed(drive='commit-drive0')
316
317    # In this case the base node of the stream job is the same as the
318    # top node of commit job. Since block-commit removes the top node
319    # when it finishes, this is not allowed.
320    def test_overlapping_4(self):
321        self.assert_no_active_block_jobs()
322
323        # Commit from node2 into node0
324        result = self.vm.qmp('block-commit', device='drive0', top=self.imgs[2], base=self.imgs[0])
325        self.assert_qmp(result, 'return', {})
326
327        # Stream from node2 into node4
328        result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='node4')
329        self.assert_qmp(result, 'error/class', 'GenericError')
330
331        self.wait_until_completed()
332        self.assert_no_active_block_jobs()
333
334    # Test a block-stream and a block-commit job in parallel
335    # Here the stream job is supposed to finish quickly in order to reproduce
336    # the scenario that triggers the bug fixed in 3d5d319e1221 and 1a63a907507
337    def test_stream_commit_1(self):
338        self.assertLessEqual(8, self.num_imgs)
339        self.assert_no_active_block_jobs()
340
341        # Stream from node0 into node2
342        result = self.vm.qmp('block-stream', device='node2', base_node='node0', job_id='node2')
343        self.assert_qmp(result, 'return', {})
344
345        # Commit from the active layer into node3
346        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3])
347        self.assert_qmp(result, 'return', {})
348
349        # Wait for all jobs to be finished.
350        pending_jobs = ['node2', 'drive0']
351        while len(pending_jobs) > 0:
352            for event in self.vm.get_qmp_events(wait=True):
353                if event['event'] == 'BLOCK_JOB_COMPLETED':
354                    node_name = self.dictpath(event, 'data/device')
355                    self.assertTrue(node_name in pending_jobs)
356                    self.assert_qmp_absent(event, 'data/error')
357                    pending_jobs.remove(node_name)
358                if event['event'] == 'BLOCK_JOB_READY':
359                    self.assert_qmp(event, 'data/device', 'drive0')
360                    self.assert_qmp(event, 'data/type', 'commit')
361                    self.assert_qmp_absent(event, 'data/error')
362                    self.assertTrue('drive0' in pending_jobs)
363                    self.vm.qmp('block-job-complete', device='drive0')
364
365        self.assert_no_active_block_jobs()
366
367    # This is similar to test_stream_commit_1 but both jobs are slowed
368    # down so they can run in parallel for a little while.
369    def test_stream_commit_2(self):
370        self.assertLessEqual(8, self.num_imgs)
371        self.assert_no_active_block_jobs()
372
373        # Stream from node0 into node4
374        result = self.vm.qmp('block-stream', device='node4', base_node='node0', job_id='node4', speed=1024*1024)
375        self.assert_qmp(result, 'return', {})
376
377        # Commit from the active layer into node5
378        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[5], speed=1024*1024)
379        self.assert_qmp(result, 'return', {})
380
381        # Wait for all jobs to be finished.
382        pending_jobs = ['node4', 'drive0']
383        while len(pending_jobs) > 0:
384            for event in self.vm.get_qmp_events(wait=True):
385                if event['event'] == 'BLOCK_JOB_COMPLETED':
386                    node_name = self.dictpath(event, 'data/device')
387                    self.assertTrue(node_name in pending_jobs)
388                    self.assert_qmp_absent(event, 'data/error')
389                    pending_jobs.remove(node_name)
390                if event['event'] == 'BLOCK_JOB_READY':
391                    self.assert_qmp(event, 'data/device', 'drive0')
392                    self.assert_qmp(event, 'data/type', 'commit')
393                    self.assert_qmp_absent(event, 'data/error')
394                    self.assertTrue('drive0' in pending_jobs)
395                    self.vm.qmp('block-job-complete', device='drive0')
396
397        self.assert_no_active_block_jobs()
398
399    # Test the base_node parameter
400    def test_stream_base_node_name(self):
401        self.assert_no_active_block_jobs()
402
403        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]),
404                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]),
405                            'image file map matches backing file before streaming')
406
407        # Error: the base node does not exist
408        result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream')
409        self.assert_qmp(result, 'error/class', 'GenericError')
410
411        # Error: the base node is not a backing file of the top node
412        result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream')
413        self.assert_qmp(result, 'error/class', 'GenericError')
414
415        # Error: the base node is the same as the top node
416        result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream')
417        self.assert_qmp(result, 'error/class', 'GenericError')
418
419        # Error: cannot specify 'base' and 'base-node' at the same time
420        result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream')
421        self.assert_qmp(result, 'error/class', 'GenericError')
422
423        # Success: the base node is a backing file of the top node
424        result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream')
425        self.assert_qmp(result, 'return', {})
426
427        self.wait_until_completed(drive='stream')
428
429        self.assert_no_active_block_jobs()
430        self.vm.shutdown()
431
432        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]),
433                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]),
434                         'image file map matches backing file after streaming')
435
436class TestQuorum(iotests.QMPTestCase):
437    num_children = 3
438    children = []
439    backing = []
440
441    def setUp(self):
442        opts = ['driver=quorum', 'vote-threshold=2']
443
444        # Initialize file names and command-line options
445        for i in range(self.num_children):
446            child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i)
447            backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i)
448            self.children.append(child_img)
449            self.backing.append(backing_img)
450            qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
451            qemu_io('-f', iotests.imgfmt,
452                    '-c', 'write -P 0x55 0 1024', backing_img)
453            qemu_img('create', '-f', iotests.imgfmt,
454                     '-o', 'backing_file=%s' % backing_img, child_img)
455            opts.append("children.%d.file.filename=%s" % (i, child_img))
456            opts.append("children.%d.node-name=node%d" % (i, i))
457
458        # Attach the drive to the VM
459        self.vm = iotests.VM()
460        self.vm.add_drive(path = None, opts = ','.join(opts))
461        self.vm.launch()
462
463    def tearDown(self):
464        self.vm.shutdown()
465        for img in self.children:
466            os.remove(img)
467        for img in self.backing:
468            os.remove(img)
469
470    def test_stream_quorum(self):
471        if not iotests.supports_quorum():
472            return
473
474        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]),
475                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]),
476                            'image file map matches backing file before streaming')
477
478        self.assert_no_active_block_jobs()
479
480        result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0')
481        self.assert_qmp(result, 'return', {})
482
483        self.wait_until_completed(drive='stream-node0')
484
485        self.assert_no_active_block_jobs()
486        self.vm.shutdown()
487
488        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]),
489                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]),
490                         'image file map does not match backing file after streaming')
491
492class TestSmallerBackingFile(iotests.QMPTestCase):
493    backing_len = 1 * 1024 * 1024 # MB
494    image_len = 2 * backing_len
495
496    def setUp(self):
497        iotests.create_image(backing_img, self.backing_len)
498        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len))
499        self.vm = iotests.VM().add_drive(test_img)
500        self.vm.launch()
501
502    # If this hangs, then you are missing a fix to complete streaming when the
503    # end of the backing file is reached.
504    def test_stream(self):
505        self.assert_no_active_block_jobs()
506
507        result = self.vm.qmp('block-stream', device='drive0')
508        self.assert_qmp(result, 'return', {})
509
510        self.wait_until_completed()
511
512        self.assert_no_active_block_jobs()
513        self.vm.shutdown()
514
515class TestErrors(iotests.QMPTestCase):
516    image_len = 2 * 1024 * 1024 # MB
517
518    # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
519    STREAM_BUFFER_SIZE = 512 * 1024
520
521    def create_blkdebug_file(self, name, event, errno):
522        file = open(name, 'w')
523        file.write('''
524[inject-error]
525state = "1"
526event = "%s"
527errno = "%d"
528immediately = "off"
529once = "on"
530sector = "%d"
531
532[set-state]
533state = "1"
534event = "%s"
535new_state = "2"
536
537[set-state]
538state = "2"
539event = "%s"
540new_state = "1"
541''' % (event, errno, self.STREAM_BUFFER_SIZE // 512, event, event))
542        file.close()
543
544class TestEIO(TestErrors):
545    def setUp(self):
546        self.blkdebug_file = backing_img + ".blkdebug"
547        iotests.create_image(backing_img, TestErrors.image_len)
548        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
549        qemu_img('create', '-f', iotests.imgfmt,
550                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
551                       % (self.blkdebug_file, backing_img),
552                 test_img)
553        self.vm = iotests.VM().add_drive(test_img)
554        self.vm.launch()
555
556    def tearDown(self):
557        self.vm.shutdown()
558        os.remove(test_img)
559        os.remove(backing_img)
560        os.remove(self.blkdebug_file)
561
562    def test_report(self):
563        self.assert_no_active_block_jobs()
564
565        result = self.vm.qmp('block-stream', device='drive0')
566        self.assert_qmp(result, 'return', {})
567
568        completed = False
569        error = False
570        while not completed:
571            for event in self.vm.get_qmp_events(wait=True):
572                if event['event'] == 'BLOCK_JOB_ERROR':
573                    self.assert_qmp(event, 'data/device', 'drive0')
574                    self.assert_qmp(event, 'data/operation', 'read')
575                    error = True
576                elif event['event'] == 'BLOCK_JOB_COMPLETED':
577                    self.assertTrue(error, 'job completed unexpectedly')
578                    self.assert_qmp(event, 'data/type', 'stream')
579                    self.assert_qmp(event, 'data/device', 'drive0')
580                    self.assert_qmp(event, 'data/error', 'Input/output error')
581                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
582                    self.assert_qmp(event, 'data/len', self.image_len)
583                    completed = True
584                elif event['event'] == 'JOB_STATUS_CHANGE':
585                    self.assert_qmp(event, 'data/id', 'drive0')
586
587        self.assert_no_active_block_jobs()
588        self.vm.shutdown()
589
590    def test_ignore(self):
591        self.assert_no_active_block_jobs()
592
593        result = self.vm.qmp('block-stream', device='drive0', on_error='ignore')
594        self.assert_qmp(result, 'return', {})
595
596        error = False
597        completed = False
598        while not completed:
599            for event in self.vm.get_qmp_events(wait=True):
600                if event['event'] == 'BLOCK_JOB_ERROR':
601                    error = True
602                    self.assert_qmp(event, 'data/device', 'drive0')
603                    self.assert_qmp(event, 'data/operation', 'read')
604                    result = self.vm.qmp('query-block-jobs')
605                    if result == {'return': []}:
606                        # Job finished too quickly
607                        continue
608                    self.assert_qmp(result, 'return[0]/paused', False)
609                elif event['event'] == 'BLOCK_JOB_COMPLETED':
610                    self.assertTrue(error, 'job completed unexpectedly')
611                    self.assert_qmp(event, 'data/type', 'stream')
612                    self.assert_qmp(event, 'data/device', 'drive0')
613                    self.assert_qmp(event, 'data/error', 'Input/output error')
614                    self.assert_qmp(event, 'data/offset', self.image_len)
615                    self.assert_qmp(event, 'data/len', self.image_len)
616                    completed = True
617                elif event['event'] == 'JOB_STATUS_CHANGE':
618                    self.assert_qmp(event, 'data/id', 'drive0')
619
620        self.assert_no_active_block_jobs()
621        self.vm.shutdown()
622
623    def test_stop(self):
624        self.assert_no_active_block_jobs()
625
626        result = self.vm.qmp('block-stream', device='drive0', on_error='stop')
627        self.assert_qmp(result, 'return', {})
628
629        error = False
630        completed = False
631        while not completed:
632            for event in self.vm.get_qmp_events(wait=True):
633                if event['event'] == 'BLOCK_JOB_ERROR':
634                    error = True
635                    self.assert_qmp(event, 'data/device', 'drive0')
636                    self.assert_qmp(event, 'data/operation', 'read')
637
638                    result = self.vm.qmp('query-block-jobs')
639                    self.assert_qmp(result, 'return[0]/paused', True)
640                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
641                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
642
643                    result = self.vm.qmp('block-job-resume', device='drive0')
644                    self.assert_qmp(result, 'return', {})
645
646                    result = self.vm.qmp('query-block-jobs')
647                    if result == {'return': []}:
648                        # Race; likely already finished. Check.
649                        continue
650                    self.assert_qmp(result, 'return[0]/paused', False)
651                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
652                elif event['event'] == 'BLOCK_JOB_COMPLETED':
653                    self.assertTrue(error, 'job completed unexpectedly')
654                    self.assert_qmp(event, 'data/type', 'stream')
655                    self.assert_qmp(event, 'data/device', 'drive0')
656                    self.assert_qmp_absent(event, 'data/error')
657                    self.assert_qmp(event, 'data/offset', self.image_len)
658                    self.assert_qmp(event, 'data/len', self.image_len)
659                    completed = True
660                elif event['event'] == 'JOB_STATUS_CHANGE':
661                    self.assert_qmp(event, 'data/id', 'drive0')
662
663        self.assert_no_active_block_jobs()
664        self.vm.shutdown()
665
666    def test_enospc(self):
667        self.assert_no_active_block_jobs()
668
669        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
670        self.assert_qmp(result, 'return', {})
671
672        completed = False
673        error = False
674        while not completed:
675            for event in self.vm.get_qmp_events(wait=True):
676                if event['event'] == 'BLOCK_JOB_ERROR':
677                    self.assert_qmp(event, 'data/device', 'drive0')
678                    self.assert_qmp(event, 'data/operation', 'read')
679                    error = True
680                elif event['event'] == 'BLOCK_JOB_COMPLETED':
681                    self.assertTrue(error, 'job completed unexpectedly')
682                    self.assert_qmp(event, 'data/type', 'stream')
683                    self.assert_qmp(event, 'data/device', 'drive0')
684                    self.assert_qmp(event, 'data/error', 'Input/output error')
685                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
686                    self.assert_qmp(event, 'data/len', self.image_len)
687                    completed = True
688                elif event['event'] == 'JOB_STATUS_CHANGE':
689                    self.assert_qmp(event, 'data/id', 'drive0')
690
691        self.assert_no_active_block_jobs()
692        self.vm.shutdown()
693
694class TestENOSPC(TestErrors):
695    def setUp(self):
696        self.blkdebug_file = backing_img + ".blkdebug"
697        iotests.create_image(backing_img, TestErrors.image_len)
698        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28)
699        qemu_img('create', '-f', iotests.imgfmt,
700                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
701                       % (self.blkdebug_file, backing_img),
702                 test_img)
703        self.vm = iotests.VM().add_drive(test_img)
704        self.vm.launch()
705
706    def tearDown(self):
707        self.vm.shutdown()
708        os.remove(test_img)
709        os.remove(backing_img)
710        os.remove(self.blkdebug_file)
711
712    def test_enospc(self):
713        self.assert_no_active_block_jobs()
714
715        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
716        self.assert_qmp(result, 'return', {})
717
718        error = False
719        completed = False
720        while not completed:
721            for event in self.vm.get_qmp_events(wait=True):
722                if event['event'] == 'BLOCK_JOB_ERROR':
723                    self.assert_qmp(event, 'data/device', 'drive0')
724                    self.assert_qmp(event, 'data/operation', 'read')
725                    error = True
726
727                    result = self.vm.qmp('query-block-jobs')
728                    self.assert_qmp(result, 'return[0]/paused', True)
729                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
730                    self.assert_qmp(result, 'return[0]/io-status', 'nospace')
731
732                    result = self.vm.qmp('block-job-resume', device='drive0')
733                    self.assert_qmp(result, 'return', {})
734
735                    result = self.vm.qmp('query-block-jobs')
736                    if result == {'return': []}:
737                        # Race; likely already finished. Check.
738                        continue
739                    self.assert_qmp(result, 'return[0]/paused', False)
740                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
741                elif event['event'] == 'BLOCK_JOB_COMPLETED':
742                    self.assertTrue(error, 'job completed unexpectedly')
743                    self.assert_qmp(event, 'data/type', 'stream')
744                    self.assert_qmp(event, 'data/device', 'drive0')
745                    self.assert_qmp_absent(event, 'data/error')
746                    self.assert_qmp(event, 'data/offset', self.image_len)
747                    self.assert_qmp(event, 'data/len', self.image_len)
748                    completed = True
749                elif event['event'] == 'JOB_STATUS_CHANGE':
750                    self.assert_qmp(event, 'data/id', 'drive0')
751
752        self.assert_no_active_block_jobs()
753        self.vm.shutdown()
754
755class TestStreamStop(iotests.QMPTestCase):
756    image_len = 8 * 1024 * 1024 * 1024 # GB
757
758    def setUp(self):
759        qemu_img('create', backing_img, str(TestStreamStop.image_len))
760        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
761        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
762        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
763        self.vm = iotests.VM().add_drive("blkdebug::" + test_img)
764        self.vm.launch()
765
766    def tearDown(self):
767        self.vm.shutdown()
768        os.remove(test_img)
769        os.remove(backing_img)
770
771    def test_stream_stop(self):
772        self.assert_no_active_block_jobs()
773
774        self.vm.pause_drive('drive0')
775        result = self.vm.qmp('block-stream', device='drive0')
776        self.assert_qmp(result, 'return', {})
777
778        time.sleep(0.1)
779        events = self.vm.get_qmp_events(wait=False)
780        for e in events:
781            self.assert_qmp(e, 'event', 'JOB_STATUS_CHANGE')
782            self.assert_qmp(e, 'data/id', 'drive0')
783
784        self.cancel_and_wait(resume=True)
785
786class TestSetSpeed(iotests.QMPTestCase):
787    image_len = 80 * 1024 * 1024 # MB
788
789    def setUp(self):
790        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
791        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
792        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
793        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
794        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
795        self.vm.launch()
796
797    def tearDown(self):
798        self.vm.shutdown()
799        os.remove(test_img)
800        os.remove(backing_img)
801
802    # This is a short performance test which is not run by default.
803    # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
804    def perf_test_throughput(self):
805        self.assert_no_active_block_jobs()
806
807        result = self.vm.qmp('block-stream', device='drive0')
808        self.assert_qmp(result, 'return', {})
809
810        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
811        self.assert_qmp(result, 'return', {})
812
813        self.wait_until_completed()
814
815        self.assert_no_active_block_jobs()
816
817    def test_set_speed(self):
818        self.assert_no_active_block_jobs()
819
820        self.vm.pause_drive('drive0')
821        result = self.vm.qmp('block-stream', device='drive0')
822        self.assert_qmp(result, 'return', {})
823
824        # Default speed is 0
825        result = self.vm.qmp('query-block-jobs')
826        self.assert_qmp(result, 'return[0]/device', 'drive0')
827        self.assert_qmp(result, 'return[0]/speed', 0)
828
829        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
830        self.assert_qmp(result, 'return', {})
831
832        # Ensure the speed we set was accepted
833        result = self.vm.qmp('query-block-jobs')
834        self.assert_qmp(result, 'return[0]/device', 'drive0')
835        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
836
837        self.cancel_and_wait(resume=True)
838        self.vm.pause_drive('drive0')
839
840        # Check setting speed in block-stream works
841        result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024)
842        self.assert_qmp(result, 'return', {})
843
844        result = self.vm.qmp('query-block-jobs')
845        self.assert_qmp(result, 'return[0]/device', 'drive0')
846        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
847
848        self.cancel_and_wait(resume=True)
849
850    def test_set_speed_invalid(self):
851        self.assert_no_active_block_jobs()
852
853        result = self.vm.qmp('block-stream', device='drive0', speed=-1)
854        self.assert_qmp(result, 'error/class', 'GenericError')
855
856        self.assert_no_active_block_jobs()
857
858        self.vm.pause_drive('drive0')
859        result = self.vm.qmp('block-stream', device='drive0')
860        self.assert_qmp(result, 'return', {})
861
862        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
863        self.assert_qmp(result, 'error/class', 'GenericError')
864
865        self.cancel_and_wait(resume=True)
866
867if __name__ == '__main__':
868    iotests.main(supported_fmts=['qcow2', 'qed'])
869