xref: /openbmc/qemu/tests/qemu-iotests/040 (revision eabfeb0c)
1#!/usr/bin/env python3
2#
3# Tests for image block commit.
4#
5# Copyright (C) 2012 IBM, Corp.
6# Copyright (C) 2012 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21# Test for live block commit
22# Derived from Image Streaming Test 030
23
24import time
25import os
26import iotests
27from iotests import qemu_img, qemu_io
28import struct
29import errno
30
31backing_img = os.path.join(iotests.test_dir, 'backing.img')
32mid_img = os.path.join(iotests.test_dir, 'mid.img')
33test_img = os.path.join(iotests.test_dir, 'test.img')
34
35class ImageCommitTestCase(iotests.QMPTestCase):
36    '''Abstract base class for image commit test cases'''
37
38    def wait_for_complete(self, need_ready=False):
39        completed = False
40        ready = False
41        while not completed:
42            for event in self.vm.get_qmp_events(wait=True):
43                if event['event'] == 'BLOCK_JOB_COMPLETED':
44                    self.assert_qmp_absent(event, 'data/error')
45                    self.assert_qmp(event, 'data/type', 'commit')
46                    self.assert_qmp(event, 'data/device', 'drive0')
47                    self.assert_qmp(event, 'data/offset', event['data']['len'])
48                    if need_ready:
49                        self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event")
50                    completed = True
51                elif event['event'] == 'BLOCK_JOB_READY':
52                    ready = True
53                    self.assert_qmp(event, 'data/type', 'commit')
54                    self.assert_qmp(event, 'data/device', 'drive0')
55                    self.vm.qmp('block-job-complete', device='drive0')
56
57        self.assert_no_active_block_jobs()
58        self.vm.shutdown()
59
60    def run_commit_test(self, top, base, need_ready=False, node_names=False):
61        self.assert_no_active_block_jobs()
62        if node_names:
63            result = self.vm.qmp('block-commit', device='drive0', top_node=top, base_node=base)
64        else:
65            result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
66        self.assert_qmp(result, 'return', {})
67        self.wait_for_complete(need_ready)
68
69    def run_default_commit_test(self):
70        self.assert_no_active_block_jobs()
71        result = self.vm.qmp('block-commit', device='drive0')
72        self.assert_qmp(result, 'return', {})
73        self.wait_for_complete()
74
75class TestSingleDrive(ImageCommitTestCase):
76    # Need some space after the copied data so that throttling is effective in
77    # tests that use it rather than just completing the job immediately
78    image_len = 2 * 1024 * 1024
79    test_len = 1 * 1024 * 256
80
81    def setUp(self):
82        iotests.create_image(backing_img, self.image_len)
83        qemu_img('create', '-f', iotests.imgfmt,
84                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', mid_img)
85        qemu_img('create', '-f', iotests.imgfmt,
86                 '-o', 'backing_file=%s' % mid_img,
87                 '-F', iotests.imgfmt, test_img)
88        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img)
89        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
90        self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none")
91        self.vm.add_device(iotests.get_virtio_scsi_device())
92        self.vm.add_device("scsi-hd,id=scsi0,drive=drive0")
93        self.vm.launch()
94        self.has_quit = False
95
96    def tearDown(self):
97        self.vm.shutdown(has_quit=self.has_quit)
98        os.remove(test_img)
99        os.remove(mid_img)
100        os.remove(backing_img)
101
102    def test_commit(self):
103        self.run_commit_test(mid_img, backing_img)
104        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
105        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
106
107    def test_commit_node(self):
108        self.run_commit_test("mid", "base", node_names=True)
109        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
110        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
111
112    @iotests.skip_if_unsupported(['throttle'])
113    def test_commit_with_filter_and_quit(self):
114        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
115        self.assert_qmp(result, 'return', {})
116
117        # Add a filter outside of the backing chain
118        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
119        self.assert_qmp(result, 'return', {})
120
121        result = self.vm.qmp('block-commit', device='drive0')
122        self.assert_qmp(result, 'return', {})
123
124        # Quit immediately, thus forcing a simultaneous cancel of the
125        # block job and a bdrv_drain_all()
126        result = self.vm.qmp('quit')
127        self.assert_qmp(result, 'return', {})
128
129        self.has_quit = True
130
131    # Same as above, but this time we add the filter after starting the job
132    @iotests.skip_if_unsupported(['throttle'])
133    def test_commit_plus_filter_and_quit(self):
134        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
135        self.assert_qmp(result, 'return', {})
136
137        result = self.vm.qmp('block-commit', device='drive0')
138        self.assert_qmp(result, 'return', {})
139
140        # Add a filter outside of the backing chain
141        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
142        self.assert_qmp(result, 'return', {})
143
144        # Quit immediately, thus forcing a simultaneous cancel of the
145        # block job and a bdrv_drain_all()
146        result = self.vm.qmp('quit')
147        self.assert_qmp(result, 'return', {})
148
149        self.has_quit = True
150
151    def test_device_not_found(self):
152        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
153        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
154
155    def test_top_same_base(self):
156        self.assert_no_active_block_jobs()
157        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
158        self.assert_qmp(result, 'error/class', 'GenericError')
159        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
160
161    def test_top_invalid(self):
162        self.assert_no_active_block_jobs()
163        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
164        self.assert_qmp(result, 'error/class', 'GenericError')
165        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
166
167    def test_base_invalid(self):
168        self.assert_no_active_block_jobs()
169        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
170        self.assert_qmp(result, 'error/class', 'GenericError')
171        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
172
173    def test_top_node_invalid(self):
174        self.assert_no_active_block_jobs()
175        result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base')
176        self.assert_qmp(result, 'error/class', 'GenericError')
177        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
178
179    def test_base_node_invalid(self):
180        self.assert_no_active_block_jobs()
181        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile')
182        self.assert_qmp(result, 'error/class', 'GenericError')
183        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
184
185    def test_top_path_and_node(self):
186        self.assert_no_active_block_jobs()
187        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img)
188        self.assert_qmp(result, 'error/class', 'GenericError')
189        self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive")
190
191    def test_base_path_and_node(self):
192        self.assert_no_active_block_jobs()
193        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img)
194        self.assert_qmp(result, 'error/class', 'GenericError')
195        self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive")
196
197    def test_top_is_active(self):
198        self.run_commit_test(test_img, backing_img, need_ready=True)
199        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
200        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
201
202    def test_top_is_default_active(self):
203        self.run_default_commit_test()
204        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
205        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
206
207    def test_top_and_base_reversed(self):
208        self.assert_no_active_block_jobs()
209        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
210        self.assert_qmp(result, 'error/class', 'GenericError')
211        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
212
213    def test_top_and_base_node_reversed(self):
214        self.assert_no_active_block_jobs()
215        result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top')
216        self.assert_qmp(result, 'error/class', 'GenericError')
217        self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain")
218
219    def test_top_node_in_wrong_chain(self):
220        self.assert_no_active_block_jobs()
221
222        result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null')
223        self.assert_qmp(result, 'return', {})
224
225        result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base')
226        self.assert_qmp(result, 'error/class', 'GenericError')
227        self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain")
228
229    # When the job is running on a BB that is automatically deleted on hot
230    # unplug, the job is cancelled when the device disappears
231    def test_hot_unplug(self):
232        if self.image_len == 0:
233            return
234
235        self.assert_no_active_block_jobs()
236        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
237                             base=backing_img, speed=(self.image_len // 4))
238        self.assert_qmp(result, 'return', {})
239        result = self.vm.qmp('device_del', id='scsi0')
240        self.assert_qmp(result, 'return', {})
241
242        cancelled = False
243        deleted = False
244        while not cancelled or not deleted:
245            for event in self.vm.get_qmp_events(wait=True):
246                if event['event'] == 'DEVICE_DELETED':
247                    self.assert_qmp(event, 'data/device', 'scsi0')
248                    deleted = True
249                elif event['event'] == 'BLOCK_JOB_CANCELLED':
250                    self.assert_qmp(event, 'data/device', 'drive0')
251                    cancelled = True
252                elif event['event'] == 'JOB_STATUS_CHANGE':
253                    self.assert_qmp(event, 'data/id', 'drive0')
254                else:
255                    self.fail("Unexpected event %s" % (event['event']))
256
257        self.assert_no_active_block_jobs()
258
259    # Tests that the insertion of the commit_top filter node doesn't make a
260    # difference to query-blockstat
261    def test_implicit_node(self):
262        if self.image_len == 0:
263            return
264
265        self.assert_no_active_block_jobs()
266        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
267                             base=backing_img, speed=(self.image_len // 4))
268        self.assert_qmp(result, 'return', {})
269
270        result = self.vm.qmp('query-block')
271        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
272        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
273        self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img)
274        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2)
275        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
276        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img)
277        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img)
278
279        result = self.vm.qmp('query-blockstats')
280        self.assert_qmp(result, 'return[0]/node-name', 'top')
281        self.assert_qmp(result, 'return[0]/backing/node-name', 'mid')
282        self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base')
283
284        self.cancel_and_wait()
285        self.assert_no_active_block_jobs()
286
287class TestRelativePaths(ImageCommitTestCase):
288    image_len = 1 * 1024 * 1024
289    test_len = 1 * 1024 * 256
290
291    dir1 = "dir1"
292    dir2 = "dir2/"
293    dir3 = "dir2/dir3/"
294
295    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
296    mid_img = "../mid.img"
297    backing_img = "../dir1/backing.img"
298
299    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
300    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
301
302    def setUp(self):
303        try:
304            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
305            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
306            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
307        except OSError as exception:
308            if exception.errno != errno.EEXIST:
309                raise
310        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
311        qemu_img('create', '-f', iotests.imgfmt,
312                 '-o', 'backing_file=%s' % self.backing_img_abs,
313                 '-F', 'raw', self.mid_img_abs)
314        qemu_img('create', '-f', iotests.imgfmt,
315                 '-o', 'backing_file=%s' % self.mid_img_abs,
316                 '-F', iotests.imgfmt, self.test_img)
317        qemu_img('rebase', '-u', '-b', self.backing_img,
318                 '-F', 'raw', self.mid_img_abs)
319        qemu_img('rebase', '-u', '-b', self.mid_img,
320                 '-F', iotests.imgfmt, self.test_img)
321        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
322        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
323        self.vm = iotests.VM().add_drive(self.test_img)
324        self.vm.launch()
325
326    def tearDown(self):
327        self.vm.shutdown()
328        os.remove(self.test_img)
329        os.remove(self.mid_img_abs)
330        os.remove(self.backing_img_abs)
331        try:
332            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
333            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
334            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
335        except OSError as exception:
336            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
337                raise
338
339    def test_commit(self):
340        self.run_commit_test(self.mid_img, self.backing_img)
341        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
342        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
343
344    def test_device_not_found(self):
345        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
346        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
347
348    def test_top_same_base(self):
349        self.assert_no_active_block_jobs()
350        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
351        self.assert_qmp(result, 'error/class', 'GenericError')
352        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
353
354    def test_top_invalid(self):
355        self.assert_no_active_block_jobs()
356        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
357        self.assert_qmp(result, 'error/class', 'GenericError')
358        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
359
360    def test_base_invalid(self):
361        self.assert_no_active_block_jobs()
362        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
363        self.assert_qmp(result, 'error/class', 'GenericError')
364        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
365
366    def test_top_is_active(self):
367        self.run_commit_test(self.test_img, self.backing_img)
368        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
369        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
370
371    def test_top_and_base_reversed(self):
372        self.assert_no_active_block_jobs()
373        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
374        self.assert_qmp(result, 'error/class', 'GenericError')
375        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
376
377
378class TestSetSpeed(ImageCommitTestCase):
379    image_len = 80 * 1024 * 1024 # MB
380
381    def setUp(self):
382        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
383        qemu_img('create', '-f', iotests.imgfmt,
384                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', mid_img)
385        qemu_img('create', '-f', iotests.imgfmt,
386                 '-o', 'backing_file=%s' % mid_img,
387                 '-F', iotests.imgfmt, test_img)
388        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
389        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
390        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
391        self.vm.launch()
392
393    def tearDown(self):
394        self.vm.shutdown()
395        os.remove(test_img)
396        os.remove(mid_img)
397        os.remove(backing_img)
398
399    def test_set_speed(self):
400        self.assert_no_active_block_jobs()
401
402        self.vm.pause_drive('drive0')
403        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
404        self.assert_qmp(result, 'return', {})
405
406        # Ensure the speed we set was accepted
407        result = self.vm.qmp('query-block-jobs')
408        self.assert_qmp(result, 'return[0]/device', 'drive0')
409        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
410
411        self.cancel_and_wait(resume=True)
412
413class TestActiveZeroLengthImage(TestSingleDrive):
414    image_len = 0
415
416class TestReopenOverlay(ImageCommitTestCase):
417    image_len = 1024 * 1024
418    img0 = os.path.join(iotests.test_dir, '0.img')
419    img1 = os.path.join(iotests.test_dir, '1.img')
420    img2 = os.path.join(iotests.test_dir, '2.img')
421    img3 = os.path.join(iotests.test_dir, '3.img')
422
423    def setUp(self):
424        iotests.create_image(self.img0, self.image_len)
425        qemu_img('create', '-f', iotests.imgfmt,
426                 '-o', 'backing_file=%s' % self.img0, '-F', 'raw', self.img1)
427        qemu_img('create', '-f', iotests.imgfmt,
428                 '-o', 'backing_file=%s' % self.img1,
429                 '-F', iotests.imgfmt, self.img2)
430        qemu_img('create', '-f', iotests.imgfmt,
431                 '-o', 'backing_file=%s' % self.img2,
432                 '-F', iotests.imgfmt, self.img3)
433        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
434        self.vm = iotests.VM().add_drive(self.img3)
435        self.vm.launch()
436
437    def tearDown(self):
438        self.vm.shutdown()
439        os.remove(self.img0)
440        os.remove(self.img1)
441        os.remove(self.img2)
442        os.remove(self.img3)
443
444    # This tests what happens when the overlay image of the 'top' node
445    # needs to be reopened in read-write mode in order to update the
446    # backing image string.
447    def test_reopen_overlay(self):
448        self.run_commit_test(self.img1, self.img0)
449
450class TestErrorHandling(iotests.QMPTestCase):
451    image_len = 2 * 1024 * 1024
452
453    def setUp(self):
454        iotests.create_image(backing_img, self.image_len)
455        qemu_img('create', '-f', iotests.imgfmt,
456                 '-o', 'backing_file=%s' % backing_img,
457                 '-F', 'raw', mid_img)
458        qemu_img('create', '-f', iotests.imgfmt,
459                 '-o', 'backing_file=%s' % mid_img,
460                 '-F', iotests.imgfmt, test_img)
461
462        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x11 0 512k', mid_img)
463        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x22 0 512k', test_img)
464
465        self.vm = iotests.VM()
466        self.vm.launch()
467
468        self.blkdebug_file = iotests.file_path("blkdebug.conf")
469
470    def tearDown(self):
471        self.vm.shutdown()
472        os.remove(test_img)
473        os.remove(mid_img)
474        os.remove(backing_img)
475
476    def blockdev_add(self, **kwargs):
477        result = self.vm.qmp('blockdev-add', **kwargs)
478        self.assert_qmp(result, 'return', {})
479
480    def add_block_nodes(self, base_debug=None, mid_debug=None, top_debug=None):
481        self.blockdev_add(node_name='base-file', driver='file',
482                          filename=backing_img)
483        self.blockdev_add(node_name='mid-file', driver='file',
484                          filename=mid_img)
485        self.blockdev_add(node_name='top-file', driver='file',
486                          filename=test_img)
487
488        if base_debug:
489            self.blockdev_add(node_name='base-dbg', driver='blkdebug',
490                              image='base-file', inject_error=base_debug)
491        if mid_debug:
492            self.blockdev_add(node_name='mid-dbg', driver='blkdebug',
493                              image='mid-file', inject_error=mid_debug)
494        if top_debug:
495            self.blockdev_add(node_name='top-dbg', driver='blkdebug',
496                              image='top-file', inject_error=top_debug)
497
498        self.blockdev_add(node_name='base-fmt', driver='raw',
499                          file=('base-dbg' if base_debug else 'base-file'))
500        self.blockdev_add(node_name='mid-fmt', driver=iotests.imgfmt,
501                          file=('mid-dbg' if mid_debug else 'mid-file'),
502                          backing='base-fmt')
503        self.blockdev_add(node_name='top-fmt', driver=iotests.imgfmt,
504                          file=('top-dbg' if top_debug else 'top-file'),
505                          backing='mid-fmt')
506
507    def run_job(self, expected_events, error_pauses_job=False):
508        match_device = {'data': {'device': 'job0'}}
509        events = [
510            ('BLOCK_JOB_COMPLETED', match_device),
511            ('BLOCK_JOB_CANCELLED', match_device),
512            ('BLOCK_JOB_ERROR', match_device),
513            ('BLOCK_JOB_READY', match_device),
514        ]
515
516        completed = False
517        log = []
518        while not completed:
519            ev = self.vm.events_wait(events, timeout=5.0)
520            if ev['event'] == 'BLOCK_JOB_COMPLETED':
521                completed = True
522            elif ev['event'] == 'BLOCK_JOB_ERROR':
523                if error_pauses_job:
524                    result = self.vm.qmp('block-job-resume', device='job0')
525                    self.assert_qmp(result, 'return', {})
526            elif ev['event'] == 'BLOCK_JOB_READY':
527                result = self.vm.qmp('block-job-complete', device='job0')
528                self.assert_qmp(result, 'return', {})
529            else:
530                self.fail("Unexpected event: %s" % ev)
531            log.append(iotests.filter_qmp_event(ev))
532
533        self.maxDiff = None
534        self.assertEqual(expected_events, log)
535
536    def event_error(self, op, action):
537        return {
538            'event': 'BLOCK_JOB_ERROR',
539            'data': {'action': action, 'device': 'job0', 'operation': op},
540            'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'}
541        }
542
543    def event_ready(self):
544        return {
545            'event': 'BLOCK_JOB_READY',
546            'data': {'device': 'job0',
547                     'len': 524288,
548                     'offset': 524288,
549                     'speed': 0,
550                     'type': 'commit'},
551            'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'},
552        }
553
554    def event_completed(self, errmsg=None, active=True):
555        max_len = 524288 if active else self.image_len
556        data = {
557            'device': 'job0',
558            'len': max_len,
559            'offset': 0 if errmsg else max_len,
560            'speed': 0,
561            'type': 'commit'
562        }
563        if errmsg:
564            data['error'] = errmsg
565
566        return {
567            'event': 'BLOCK_JOB_COMPLETED',
568            'data': data,
569            'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'},
570        }
571
572    def blkdebug_event(self, event, is_raw=False):
573        if event:
574            return [{
575                'event': event,
576                'sector': 512 if is_raw else 1024,
577                'once': True,
578            }]
579        return None
580
581    def prepare_and_start_job(self, on_error, active=True,
582                              top_event=None, mid_event=None, base_event=None):
583
584        top_debug = self.blkdebug_event(top_event)
585        mid_debug = self.blkdebug_event(mid_event)
586        base_debug = self.blkdebug_event(base_event, True)
587
588        self.add_block_nodes(top_debug=top_debug, mid_debug=mid_debug,
589                             base_debug=base_debug)
590
591        result = self.vm.qmp('block-commit', job_id='job0', device='top-fmt',
592                             top_node='top-fmt' if active else 'mid-fmt',
593                             base_node='mid-fmt' if active else 'base-fmt',
594                             on_error=on_error)
595        self.assert_qmp(result, 'return', {})
596
597    def testActiveReadErrorReport(self):
598        self.prepare_and_start_job('report', top_event='read_aio')
599        self.run_job([
600            self.event_error('read', 'report'),
601            self.event_completed('Input/output error')
602        ])
603
604        self.vm.shutdown()
605        self.assertFalse(iotests.compare_images(test_img, mid_img),
606                         'target image matches source after error')
607
608    def testActiveReadErrorStop(self):
609        self.prepare_and_start_job('stop', top_event='read_aio')
610        self.run_job([
611            self.event_error('read', 'stop'),
612            self.event_ready(),
613            self.event_completed()
614        ], error_pauses_job=True)
615
616        self.vm.shutdown()
617        self.assertTrue(iotests.compare_images(test_img, mid_img),
618                        'target image does not match source after commit')
619
620    def testActiveReadErrorIgnore(self):
621        self.prepare_and_start_job('ignore', top_event='read_aio')
622        self.run_job([
623            self.event_error('read', 'ignore'),
624            self.event_ready(),
625            self.event_completed()
626        ])
627
628        # For commit, 'ignore' actually means retry, so this will succeed
629        self.vm.shutdown()
630        self.assertTrue(iotests.compare_images(test_img, mid_img),
631                        'target image does not match source after commit')
632
633    def testActiveWriteErrorReport(self):
634        self.prepare_and_start_job('report', mid_event='write_aio')
635        self.run_job([
636            self.event_error('write', 'report'),
637            self.event_completed('Input/output error')
638        ])
639
640        self.vm.shutdown()
641        self.assertFalse(iotests.compare_images(test_img, mid_img),
642                         'target image matches source after error')
643
644    def testActiveWriteErrorStop(self):
645        self.prepare_and_start_job('stop', mid_event='write_aio')
646        self.run_job([
647            self.event_error('write', 'stop'),
648            self.event_ready(),
649            self.event_completed()
650        ], error_pauses_job=True)
651
652        self.vm.shutdown()
653        self.assertTrue(iotests.compare_images(test_img, mid_img),
654                        'target image does not match source after commit')
655
656    def testActiveWriteErrorIgnore(self):
657        self.prepare_and_start_job('ignore', mid_event='write_aio')
658        self.run_job([
659            self.event_error('write', 'ignore'),
660            self.event_ready(),
661            self.event_completed()
662        ])
663
664        # For commit, 'ignore' actually means retry, so this will succeed
665        self.vm.shutdown()
666        self.assertTrue(iotests.compare_images(test_img, mid_img),
667                        'target image does not match source after commit')
668
669    def testIntermediateReadErrorReport(self):
670        self.prepare_and_start_job('report', active=False, mid_event='read_aio')
671        self.run_job([
672            self.event_error('read', 'report'),
673            self.event_completed('Input/output error', active=False)
674        ])
675
676        self.vm.shutdown()
677        self.assertFalse(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
678                         'target image matches source after error')
679
680    def testIntermediateReadErrorStop(self):
681        self.prepare_and_start_job('stop', active=False, mid_event='read_aio')
682        self.run_job([
683            self.event_error('read', 'stop'),
684            self.event_completed(active=False)
685        ], error_pauses_job=True)
686
687        self.vm.shutdown()
688        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
689                        'target image does not match source after commit')
690
691    def testIntermediateReadErrorIgnore(self):
692        self.prepare_and_start_job('ignore', active=False, mid_event='read_aio')
693        self.run_job([
694            self.event_error('read', 'ignore'),
695            self.event_completed(active=False)
696        ])
697
698        # For commit, 'ignore' actually means retry, so this will succeed
699        self.vm.shutdown()
700        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
701                        'target image does not match source after commit')
702
703    def testIntermediateWriteErrorReport(self):
704        self.prepare_and_start_job('report', active=False, base_event='write_aio')
705        self.run_job([
706            self.event_error('write', 'report'),
707            self.event_completed('Input/output error', active=False)
708        ])
709
710        self.vm.shutdown()
711        self.assertFalse(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
712                         'target image matches source after error')
713
714    def testIntermediateWriteErrorStop(self):
715        self.prepare_and_start_job('stop', active=False, base_event='write_aio')
716        self.run_job([
717            self.event_error('write', 'stop'),
718            self.event_completed(active=False)
719        ], error_pauses_job=True)
720
721        self.vm.shutdown()
722        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
723                        'target image does not match source after commit')
724
725    def testIntermediateWriteErrorIgnore(self):
726        self.prepare_and_start_job('ignore', active=False, base_event='write_aio')
727        self.run_job([
728            self.event_error('write', 'ignore'),
729            self.event_completed(active=False)
730        ])
731
732        # For commit, 'ignore' actually means retry, so this will succeed
733        self.vm.shutdown()
734        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
735                        'target image does not match source after commit')
736
737if __name__ == '__main__':
738    iotests.main(supported_fmts=['qcow2', 'qed'],
739                 supported_protocols=['file'])
740