xref: /openbmc/qemu/tests/qemu-iotests/040 (revision d4398489)
1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
2747051cdSJeff Cody#
3747051cdSJeff Cody# Tests for image block commit.
4747051cdSJeff Cody#
5747051cdSJeff Cody# Copyright (C) 2012 IBM, Corp.
6747051cdSJeff Cody# Copyright (C) 2012 Red Hat, Inc.
7747051cdSJeff Cody#
8747051cdSJeff Cody# This program is free software; you can redistribute it and/or modify
9747051cdSJeff Cody# it under the terms of the GNU General Public License as published by
10747051cdSJeff Cody# the Free Software Foundation; either version 2 of the License, or
11747051cdSJeff Cody# (at your option) any later version.
12747051cdSJeff Cody#
13747051cdSJeff Cody# This program is distributed in the hope that it will be useful,
14747051cdSJeff Cody# but WITHOUT ANY WARRANTY; without even the implied warranty of
15747051cdSJeff Cody# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16747051cdSJeff Cody# GNU General Public License for more details.
17747051cdSJeff Cody#
18747051cdSJeff Cody# You should have received a copy of the GNU General Public License
19747051cdSJeff Cody# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20747051cdSJeff Cody#
21747051cdSJeff Cody# Test for live block commit
22747051cdSJeff Cody# Derived from Image Streaming Test 030
23747051cdSJeff Cody
24747051cdSJeff Codyimport time
25747051cdSJeff Codyimport os
26747051cdSJeff Codyimport iotests
27747051cdSJeff Codyfrom iotests import qemu_img, qemu_io
28747051cdSJeff Codyimport struct
296bf0d1f4SJeff Codyimport errno
30747051cdSJeff Cody
31747051cdSJeff Codybacking_img = os.path.join(iotests.test_dir, 'backing.img')
32747051cdSJeff Codymid_img = os.path.join(iotests.test_dir, 'mid.img')
33747051cdSJeff Codytest_img = os.path.join(iotests.test_dir, 'test.img')
34747051cdSJeff Cody
35747051cdSJeff Codyclass ImageCommitTestCase(iotests.QMPTestCase):
36747051cdSJeff Cody    '''Abstract base class for image commit test cases'''
37747051cdSJeff Cody
387676e2c5SJeff Cody    def wait_for_complete(self, need_ready=False):
394de43470SFam Zheng        completed = False
408b9a30caSFam Zheng        ready = False
414de43470SFam Zheng        while not completed:
424de43470SFam Zheng            for event in self.vm.get_qmp_events(wait=True):
434de43470SFam Zheng                if event['event'] == 'BLOCK_JOB_COMPLETED':
44bcdce5a7SAlberto Garcia                    self.assert_qmp_absent(event, 'data/error')
454de43470SFam Zheng                    self.assert_qmp(event, 'data/type', 'commit')
464de43470SFam Zheng                    self.assert_qmp(event, 'data/device', 'drive0')
471d3ba15aSMax Reitz                    self.assert_qmp(event, 'data/offset', event['data']['len'])
488b9a30caSFam Zheng                    if need_ready:
498b9a30caSFam Zheng                        self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event")
504de43470SFam Zheng                    completed = True
514de43470SFam Zheng                elif event['event'] == 'BLOCK_JOB_READY':
528b9a30caSFam Zheng                    ready = True
534de43470SFam Zheng                    self.assert_qmp(event, 'data/type', 'commit')
544de43470SFam Zheng                    self.assert_qmp(event, 'data/device', 'drive0')
554de43470SFam Zheng                    self.vm.qmp('block-job-complete', device='drive0')
564de43470SFam Zheng
57fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
584de43470SFam Zheng        self.vm.shutdown()
594de43470SFam Zheng
60d57177a4SKevin Wolf    def run_commit_test(self, top, base, need_ready=False, node_names=False):
617676e2c5SJeff Cody        self.assert_no_active_block_jobs()
62d57177a4SKevin Wolf        if node_names:
63d57177a4SKevin Wolf            result = self.vm.qmp('block-commit', device='drive0', top_node=top, base_node=base)
64d57177a4SKevin Wolf        else:
657676e2c5SJeff Cody            result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
667676e2c5SJeff Cody        self.assert_qmp(result, 'return', {})
677676e2c5SJeff Cody        self.wait_for_complete(need_ready)
687676e2c5SJeff Cody
697676e2c5SJeff Cody    def run_default_commit_test(self):
707676e2c5SJeff Cody        self.assert_no_active_block_jobs()
717676e2c5SJeff Cody        result = self.vm.qmp('block-commit', device='drive0')
727676e2c5SJeff Cody        self.assert_qmp(result, 'return', {})
737676e2c5SJeff Cody        self.wait_for_complete()
747676e2c5SJeff Cody
75747051cdSJeff Codyclass TestSingleDrive(ImageCommitTestCase):
76c3971b88SKevin Wolf    # Need some space after the copied data so that throttling is effective in
77c3971b88SKevin Wolf    # tests that use it rather than just completing the job immediately
78c3971b88SKevin Wolf    image_len = 2 * 1024 * 1024
79747051cdSJeff Cody    test_len = 1 * 1024 * 256
80747051cdSJeff Cody
81747051cdSJeff Cody    def setUp(self):
828b9a30caSFam Zheng        iotests.create_image(backing_img, self.image_len)
83747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
84747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
8590c9b167SKevin Wolf        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img)
8690c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
87d3c8c674SKevin Wolf        self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none")
88f357576fSJohn Snow        self.vm.add_device(iotests.get_virtio_scsi_device())
89c3971b88SKevin Wolf        self.vm.add_device("scsi-hd,id=scsi0,drive=drive0")
90747051cdSJeff Cody        self.vm.launch()
9186472071SMax Reitz        self.has_quit = False
92747051cdSJeff Cody
93747051cdSJeff Cody    def tearDown(self):
9486472071SMax Reitz        self.vm.shutdown(has_quit=self.has_quit)
95747051cdSJeff Cody        os.remove(test_img)
96747051cdSJeff Cody        os.remove(mid_img)
97747051cdSJeff Cody        os.remove(backing_img)
98747051cdSJeff Cody
99747051cdSJeff Cody    def test_commit(self):
1004de43470SFam Zheng        self.run_commit_test(mid_img, backing_img)
10190c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
10290c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
103747051cdSJeff Cody
104d57177a4SKevin Wolf    def test_commit_node(self):
105d57177a4SKevin Wolf        self.run_commit_test("mid", "base", node_names=True)
106d57177a4SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
107d57177a4SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
108d57177a4SKevin Wolf
1099442bebeSThomas Huth    @iotests.skip_if_unsupported(['throttle'])
11086472071SMax Reitz    def test_commit_with_filter_and_quit(self):
11186472071SMax Reitz        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
11286472071SMax Reitz        self.assert_qmp(result, 'return', {})
11386472071SMax Reitz
11486472071SMax Reitz        # Add a filter outside of the backing chain
11586472071SMax Reitz        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
11686472071SMax Reitz        self.assert_qmp(result, 'return', {})
11786472071SMax Reitz
11886472071SMax Reitz        result = self.vm.qmp('block-commit', device='drive0')
11986472071SMax Reitz        self.assert_qmp(result, 'return', {})
12086472071SMax Reitz
12186472071SMax Reitz        # Quit immediately, thus forcing a simultaneous cancel of the
12286472071SMax Reitz        # block job and a bdrv_drain_all()
12386472071SMax Reitz        result = self.vm.qmp('quit')
12486472071SMax Reitz        self.assert_qmp(result, 'return', {})
12586472071SMax Reitz
12686472071SMax Reitz        self.has_quit = True
12786472071SMax Reitz
12886472071SMax Reitz    # Same as above, but this time we add the filter after starting the job
1299442bebeSThomas Huth    @iotests.skip_if_unsupported(['throttle'])
13086472071SMax Reitz    def test_commit_plus_filter_and_quit(self):
13186472071SMax Reitz        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
13286472071SMax Reitz        self.assert_qmp(result, 'return', {})
13386472071SMax Reitz
13486472071SMax Reitz        result = self.vm.qmp('block-commit', device='drive0')
13586472071SMax Reitz        self.assert_qmp(result, 'return', {})
13686472071SMax Reitz
13786472071SMax Reitz        # Add a filter outside of the backing chain
13886472071SMax Reitz        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
13986472071SMax Reitz        self.assert_qmp(result, 'return', {})
14086472071SMax Reitz
14186472071SMax Reitz        # Quit immediately, thus forcing a simultaneous cancel of the
14286472071SMax Reitz        # block job and a bdrv_drain_all()
14386472071SMax Reitz        result = self.vm.qmp('quit')
14486472071SMax Reitz        self.assert_qmp(result, 'return', {})
14586472071SMax Reitz
14686472071SMax Reitz        self.has_quit = True
14786472071SMax Reitz
148747051cdSJeff Cody    def test_device_not_found(self):
149747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
150747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
151747051cdSJeff Cody
152747051cdSJeff Cody    def test_top_same_base(self):
153fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
154747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
155747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
156d5208c45SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
157747051cdSJeff Cody
158747051cdSJeff Cody    def test_top_invalid(self):
159fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
160747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
161747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
162747051cdSJeff Cody        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
163747051cdSJeff Cody
164747051cdSJeff Cody    def test_base_invalid(self):
165fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
166747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
167747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
168747051cdSJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
169747051cdSJeff Cody
170d57177a4SKevin Wolf    def test_top_node_invalid(self):
171d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
172d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base')
173d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
174d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
175d57177a4SKevin Wolf
176d57177a4SKevin Wolf    def test_base_node_invalid(self):
177d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
178d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile')
179d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
180d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
181d57177a4SKevin Wolf
182d57177a4SKevin Wolf    def test_top_path_and_node(self):
183d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
184d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img)
185d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
186d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive")
187d57177a4SKevin Wolf
188d57177a4SKevin Wolf    def test_base_path_and_node(self):
189d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
190d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img)
191d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
192d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive")
193d57177a4SKevin Wolf
194747051cdSJeff Cody    def test_top_is_active(self):
1958b9a30caSFam Zheng        self.run_commit_test(test_img, backing_img, need_ready=True)
19690c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
19790c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
198747051cdSJeff Cody
1997676e2c5SJeff Cody    def test_top_is_default_active(self):
2007676e2c5SJeff Cody        self.run_default_commit_test()
20190c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
20290c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
2037676e2c5SJeff Cody
204747051cdSJeff Cody    def test_top_and_base_reversed(self):
205fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
206747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
207747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
208d5208c45SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
209747051cdSJeff Cody
210d57177a4SKevin Wolf    def test_top_and_base_node_reversed(self):
211d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
212d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top')
213d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
214d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain")
215d57177a4SKevin Wolf
216d57177a4SKevin Wolf    def test_top_node_in_wrong_chain(self):
217d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
218d57177a4SKevin Wolf
219d57177a4SKevin Wolf        result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null')
220d57177a4SKevin Wolf        self.assert_qmp(result, 'return', {})
221d57177a4SKevin Wolf
222d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base')
223d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
224d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain")
225d57177a4SKevin Wolf
226c3971b88SKevin Wolf    # When the job is running on a BB that is automatically deleted on hot
227c3971b88SKevin Wolf    # unplug, the job is cancelled when the device disappears
228c3971b88SKevin Wolf    def test_hot_unplug(self):
229c3971b88SKevin Wolf        if self.image_len == 0:
230c3971b88SKevin Wolf            return
231c3971b88SKevin Wolf
232c3971b88SKevin Wolf        self.assert_no_active_block_jobs()
233c3971b88SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
2349a3a9a63SMax Reitz                             base=backing_img, speed=(self.image_len // 4))
235c3971b88SKevin Wolf        self.assert_qmp(result, 'return', {})
236c3971b88SKevin Wolf        result = self.vm.qmp('device_del', id='scsi0')
237c3971b88SKevin Wolf        self.assert_qmp(result, 'return', {})
238c3971b88SKevin Wolf
239c3971b88SKevin Wolf        cancelled = False
240c3971b88SKevin Wolf        deleted = False
241c3971b88SKevin Wolf        while not cancelled or not deleted:
242c3971b88SKevin Wolf            for event in self.vm.get_qmp_events(wait=True):
243c3971b88SKevin Wolf                if event['event'] == 'DEVICE_DELETED':
244c3971b88SKevin Wolf                    self.assert_qmp(event, 'data/device', 'scsi0')
245c3971b88SKevin Wolf                    deleted = True
246c3971b88SKevin Wolf                elif event['event'] == 'BLOCK_JOB_CANCELLED':
247c3971b88SKevin Wolf                    self.assert_qmp(event, 'data/device', 'drive0')
248c3971b88SKevin Wolf                    cancelled = True
2491dac83f1SKevin Wolf                elif event['event'] == 'JOB_STATUS_CHANGE':
2501dac83f1SKevin Wolf                    self.assert_qmp(event, 'data/id', 'drive0')
251c3971b88SKevin Wolf                else:
252c3971b88SKevin Wolf                    self.fail("Unexpected event %s" % (event['event']))
253c3971b88SKevin Wolf
254c3971b88SKevin Wolf        self.assert_no_active_block_jobs()
255747051cdSJeff Cody
256d3c8c674SKevin Wolf    # Tests that the insertion of the commit_top filter node doesn't make a
257d3c8c674SKevin Wolf    # difference to query-blockstat
258d3c8c674SKevin Wolf    def test_implicit_node(self):
259d3c8c674SKevin Wolf        if self.image_len == 0:
260d3c8c674SKevin Wolf            return
261d3c8c674SKevin Wolf
262d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
263d3c8c674SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
2649a3a9a63SMax Reitz                             base=backing_img, speed=(self.image_len // 4))
265d3c8c674SKevin Wolf        self.assert_qmp(result, 'return', {})
266d3c8c674SKevin Wolf
267d3c8c674SKevin Wolf        result = self.vm.qmp('query-block')
268d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
269d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
270d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img)
271d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2)
272d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
273d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img)
274d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img)
275d3c8c674SKevin Wolf
276d3c8c674SKevin Wolf        result = self.vm.qmp('query-blockstats')
277d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/node-name', 'top')
278d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/node-name', 'mid')
279d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base')
280d3c8c674SKevin Wolf
281d3c8c674SKevin Wolf        self.cancel_and_wait()
282d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
283d3c8c674SKevin Wolf
2846bf0d1f4SJeff Codyclass TestRelativePaths(ImageCommitTestCase):
2856bf0d1f4SJeff Cody    image_len = 1 * 1024 * 1024
2866bf0d1f4SJeff Cody    test_len = 1 * 1024 * 256
2876bf0d1f4SJeff Cody
2886bf0d1f4SJeff Cody    dir1 = "dir1"
2896bf0d1f4SJeff Cody    dir2 = "dir2/"
2906bf0d1f4SJeff Cody    dir3 = "dir2/dir3/"
2916bf0d1f4SJeff Cody
2926bf0d1f4SJeff Cody    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
2936bf0d1f4SJeff Cody    mid_img = "../mid.img"
2946bf0d1f4SJeff Cody    backing_img = "../dir1/backing.img"
2956bf0d1f4SJeff Cody
2966bf0d1f4SJeff Cody    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
2976bf0d1f4SJeff Cody    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
2986bf0d1f4SJeff Cody
2996bf0d1f4SJeff Cody    def setUp(self):
3006bf0d1f4SJeff Cody        try:
3016bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
3026bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
3036bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
3046bf0d1f4SJeff Cody        except OSError as exception:
3056bf0d1f4SJeff Cody            if exception.errno != errno.EEXIST:
3066bf0d1f4SJeff Cody                raise
307915365a9SFam Zheng        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
3086bf0d1f4SJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
3096bf0d1f4SJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
3106bf0d1f4SJeff Cody        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
3116bf0d1f4SJeff Cody        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
31290c9b167SKevin Wolf        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
31390c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
3146bf0d1f4SJeff Cody        self.vm = iotests.VM().add_drive(self.test_img)
3156bf0d1f4SJeff Cody        self.vm.launch()
3166bf0d1f4SJeff Cody
3176bf0d1f4SJeff Cody    def tearDown(self):
3186bf0d1f4SJeff Cody        self.vm.shutdown()
3196bf0d1f4SJeff Cody        os.remove(self.test_img)
3206bf0d1f4SJeff Cody        os.remove(self.mid_img_abs)
3216bf0d1f4SJeff Cody        os.remove(self.backing_img_abs)
3226bf0d1f4SJeff Cody        try:
3236bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
3246bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
3256bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
3266bf0d1f4SJeff Cody        except OSError as exception:
3276bf0d1f4SJeff Cody            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
3286bf0d1f4SJeff Cody                raise
3296bf0d1f4SJeff Cody
3306bf0d1f4SJeff Cody    def test_commit(self):
3314de43470SFam Zheng        self.run_commit_test(self.mid_img, self.backing_img)
33290c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
33390c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
3346bf0d1f4SJeff Cody
3356bf0d1f4SJeff Cody    def test_device_not_found(self):
3366bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
3376bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
3386bf0d1f4SJeff Cody
3396bf0d1f4SJeff Cody    def test_top_same_base(self):
340fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3416bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
3426bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3436bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
3446bf0d1f4SJeff Cody
3456bf0d1f4SJeff Cody    def test_top_invalid(self):
346fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3476bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
3486bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3496bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
3506bf0d1f4SJeff Cody
3516bf0d1f4SJeff Cody    def test_base_invalid(self):
352fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3536bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
3546bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3556bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
3566bf0d1f4SJeff Cody
3576bf0d1f4SJeff Cody    def test_top_is_active(self):
3584de43470SFam Zheng        self.run_commit_test(self.test_img, self.backing_img)
35990c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
36090c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
3616bf0d1f4SJeff Cody
3626bf0d1f4SJeff Cody    def test_top_and_base_reversed(self):
363fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3646bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
3656bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3666bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
3676bf0d1f4SJeff Cody
368747051cdSJeff Cody
369747051cdSJeff Codyclass TestSetSpeed(ImageCommitTestCase):
370747051cdSJeff Cody    image_len = 80 * 1024 * 1024 # MB
371747051cdSJeff Cody
372747051cdSJeff Cody    def setUp(self):
373747051cdSJeff Cody        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
374747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
375747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
37690c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
37790c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
37851c493c5SMax Reitz        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
379747051cdSJeff Cody        self.vm.launch()
380747051cdSJeff Cody
381747051cdSJeff Cody    def tearDown(self):
382747051cdSJeff Cody        self.vm.shutdown()
383747051cdSJeff Cody        os.remove(test_img)
384747051cdSJeff Cody        os.remove(mid_img)
385747051cdSJeff Cody        os.remove(backing_img)
386747051cdSJeff Cody
387747051cdSJeff Cody    def test_set_speed(self):
388fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
389747051cdSJeff Cody
390b59b3d57SFam Zheng        self.vm.pause_drive('drive0')
391747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
392747051cdSJeff Cody        self.assert_qmp(result, 'return', {})
393747051cdSJeff Cody
394747051cdSJeff Cody        # Ensure the speed we set was accepted
395747051cdSJeff Cody        result = self.vm.qmp('query-block-jobs')
396747051cdSJeff Cody        self.assert_qmp(result, 'return[0]/device', 'drive0')
397747051cdSJeff Cody        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
398747051cdSJeff Cody
399b59b3d57SFam Zheng        self.cancel_and_wait(resume=True)
400747051cdSJeff Cody
4018b9a30caSFam Zhengclass TestActiveZeroLengthImage(TestSingleDrive):
4028b9a30caSFam Zheng    image_len = 0
403747051cdSJeff Cody
404bcdce5a7SAlberto Garciaclass TestReopenOverlay(ImageCommitTestCase):
405bcdce5a7SAlberto Garcia    image_len = 1024 * 1024
406bcdce5a7SAlberto Garcia    img0 = os.path.join(iotests.test_dir, '0.img')
407bcdce5a7SAlberto Garcia    img1 = os.path.join(iotests.test_dir, '1.img')
408bcdce5a7SAlberto Garcia    img2 = os.path.join(iotests.test_dir, '2.img')
409bcdce5a7SAlberto Garcia    img3 = os.path.join(iotests.test_dir, '3.img')
410bcdce5a7SAlberto Garcia
411bcdce5a7SAlberto Garcia    def setUp(self):
412bcdce5a7SAlberto Garcia        iotests.create_image(self.img0, self.image_len)
413bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
414bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
415bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
416bcdce5a7SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
417bcdce5a7SAlberto Garcia        self.vm = iotests.VM().add_drive(self.img3)
418bcdce5a7SAlberto Garcia        self.vm.launch()
419bcdce5a7SAlberto Garcia
420bcdce5a7SAlberto Garcia    def tearDown(self):
421bcdce5a7SAlberto Garcia        self.vm.shutdown()
422bcdce5a7SAlberto Garcia        os.remove(self.img0)
423bcdce5a7SAlberto Garcia        os.remove(self.img1)
424bcdce5a7SAlberto Garcia        os.remove(self.img2)
425bcdce5a7SAlberto Garcia        os.remove(self.img3)
426bcdce5a7SAlberto Garcia
427bcdce5a7SAlberto Garcia    # This tests what happens when the overlay image of the 'top' node
428bcdce5a7SAlberto Garcia    # needs to be reopened in read-write mode in order to update the
429bcdce5a7SAlberto Garcia    # backing image string.
430bcdce5a7SAlberto Garcia    def test_reopen_overlay(self):
431bcdce5a7SAlberto Garcia        self.run_commit_test(self.img1, self.img0)
432bcdce5a7SAlberto Garcia
433*d4398489SKevin Wolfclass TestErrorHandling(iotests.QMPTestCase):
434*d4398489SKevin Wolf    image_len = 2 * 1024 * 1024
435*d4398489SKevin Wolf
436*d4398489SKevin Wolf    def setUp(self):
437*d4398489SKevin Wolf        iotests.create_image(backing_img, self.image_len)
438*d4398489SKevin Wolf        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
439*d4398489SKevin Wolf        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
440*d4398489SKevin Wolf
441*d4398489SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x11 0 512k', mid_img)
442*d4398489SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x22 0 512k', test_img)
443*d4398489SKevin Wolf
444*d4398489SKevin Wolf        self.vm = iotests.VM()
445*d4398489SKevin Wolf        self.vm.launch()
446*d4398489SKevin Wolf
447*d4398489SKevin Wolf        self.blkdebug_file = iotests.file_path("blkdebug.conf")
448*d4398489SKevin Wolf
449*d4398489SKevin Wolf    def tearDown(self):
450*d4398489SKevin Wolf        self.vm.shutdown()
451*d4398489SKevin Wolf        os.remove(test_img)
452*d4398489SKevin Wolf        os.remove(mid_img)
453*d4398489SKevin Wolf        os.remove(backing_img)
454*d4398489SKevin Wolf
455*d4398489SKevin Wolf    def blockdev_add(self, **kwargs):
456*d4398489SKevin Wolf        result = self.vm.qmp('blockdev-add', **kwargs)
457*d4398489SKevin Wolf        self.assert_qmp(result, 'return', {})
458*d4398489SKevin Wolf
459*d4398489SKevin Wolf    def add_block_nodes(self, base_debug=None, mid_debug=None, top_debug=None):
460*d4398489SKevin Wolf        self.blockdev_add(node_name='base-file', driver='file',
461*d4398489SKevin Wolf                          filename=backing_img)
462*d4398489SKevin Wolf        self.blockdev_add(node_name='mid-file', driver='file',
463*d4398489SKevin Wolf                          filename=mid_img)
464*d4398489SKevin Wolf        self.blockdev_add(node_name='top-file', driver='file',
465*d4398489SKevin Wolf                          filename=test_img)
466*d4398489SKevin Wolf
467*d4398489SKevin Wolf        if base_debug:
468*d4398489SKevin Wolf            self.blockdev_add(node_name='base-dbg', driver='blkdebug',
469*d4398489SKevin Wolf                              image='base-file', inject_error=base_debug)
470*d4398489SKevin Wolf        if mid_debug:
471*d4398489SKevin Wolf            self.blockdev_add(node_name='mid-dbg', driver='blkdebug',
472*d4398489SKevin Wolf                              image='mid-file', inject_error=mid_debug)
473*d4398489SKevin Wolf        if top_debug:
474*d4398489SKevin Wolf            self.blockdev_add(node_name='top-dbg', driver='blkdebug',
475*d4398489SKevin Wolf                              image='top-file', inject_error=top_debug)
476*d4398489SKevin Wolf
477*d4398489SKevin Wolf        self.blockdev_add(node_name='base-fmt', driver='raw',
478*d4398489SKevin Wolf                          file=('base-dbg' if base_debug else 'base-file'))
479*d4398489SKevin Wolf        self.blockdev_add(node_name='mid-fmt', driver=iotests.imgfmt,
480*d4398489SKevin Wolf                          file=('mid-dbg' if mid_debug else 'mid-file'),
481*d4398489SKevin Wolf                          backing='base-fmt')
482*d4398489SKevin Wolf        self.blockdev_add(node_name='top-fmt', driver=iotests.imgfmt,
483*d4398489SKevin Wolf                          file=('top-dbg' if top_debug else 'top-file'),
484*d4398489SKevin Wolf                          backing='mid-fmt')
485*d4398489SKevin Wolf
486*d4398489SKevin Wolf    def run_job(self, expected_events, error_pauses_job=False):
487*d4398489SKevin Wolf        match_device = {'data': {'device': 'job0'}}
488*d4398489SKevin Wolf        events = [
489*d4398489SKevin Wolf            ('BLOCK_JOB_COMPLETED', match_device),
490*d4398489SKevin Wolf            ('BLOCK_JOB_CANCELLED', match_device),
491*d4398489SKevin Wolf            ('BLOCK_JOB_ERROR', match_device),
492*d4398489SKevin Wolf            ('BLOCK_JOB_READY', match_device),
493*d4398489SKevin Wolf        ]
494*d4398489SKevin Wolf
495*d4398489SKevin Wolf        completed = False
496*d4398489SKevin Wolf        log = []
497*d4398489SKevin Wolf        while not completed:
498*d4398489SKevin Wolf            ev = self.vm.events_wait(events, timeout=5.0)
499*d4398489SKevin Wolf            if ev['event'] == 'BLOCK_JOB_COMPLETED':
500*d4398489SKevin Wolf                completed = True
501*d4398489SKevin Wolf            elif ev['event'] == 'BLOCK_JOB_ERROR':
502*d4398489SKevin Wolf                if error_pauses_job:
503*d4398489SKevin Wolf                    result = self.vm.qmp('block-job-resume', device='job0')
504*d4398489SKevin Wolf                    self.assert_qmp(result, 'return', {})
505*d4398489SKevin Wolf            elif ev['event'] == 'BLOCK_JOB_READY':
506*d4398489SKevin Wolf                result = self.vm.qmp('block-job-complete', device='job0')
507*d4398489SKevin Wolf                self.assert_qmp(result, 'return', {})
508*d4398489SKevin Wolf            else:
509*d4398489SKevin Wolf                self.fail("Unexpected event: %s" % ev)
510*d4398489SKevin Wolf            log.append(iotests.filter_qmp_event(ev))
511*d4398489SKevin Wolf
512*d4398489SKevin Wolf        self.maxDiff = None
513*d4398489SKevin Wolf        self.assertEqual(expected_events, log)
514*d4398489SKevin Wolf
515*d4398489SKevin Wolf    def event_error(self, op, action):
516*d4398489SKevin Wolf        return {
517*d4398489SKevin Wolf            'event': 'BLOCK_JOB_ERROR',
518*d4398489SKevin Wolf            'data': {'action': action, 'device': 'job0', 'operation': op},
519*d4398489SKevin Wolf            'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'}
520*d4398489SKevin Wolf        }
521*d4398489SKevin Wolf
522*d4398489SKevin Wolf    def event_ready(self):
523*d4398489SKevin Wolf        return {
524*d4398489SKevin Wolf            'event': 'BLOCK_JOB_READY',
525*d4398489SKevin Wolf            'data': {'device': 'job0',
526*d4398489SKevin Wolf                     'len': 524288,
527*d4398489SKevin Wolf                     'offset': 524288,
528*d4398489SKevin Wolf                     'speed': 0,
529*d4398489SKevin Wolf                     'type': 'commit'},
530*d4398489SKevin Wolf            'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'},
531*d4398489SKevin Wolf        }
532*d4398489SKevin Wolf
533*d4398489SKevin Wolf    def event_completed(self, errmsg=None, active=True):
534*d4398489SKevin Wolf        max_len = 524288 if active else self.image_len
535*d4398489SKevin Wolf        data = {
536*d4398489SKevin Wolf            'device': 'job0',
537*d4398489SKevin Wolf            'len': max_len,
538*d4398489SKevin Wolf            'offset': 0 if errmsg else max_len,
539*d4398489SKevin Wolf            'speed': 0,
540*d4398489SKevin Wolf            'type': 'commit'
541*d4398489SKevin Wolf        }
542*d4398489SKevin Wolf        if errmsg:
543*d4398489SKevin Wolf            data['error'] = errmsg
544*d4398489SKevin Wolf
545*d4398489SKevin Wolf        return {
546*d4398489SKevin Wolf            'event': 'BLOCK_JOB_COMPLETED',
547*d4398489SKevin Wolf            'data': data,
548*d4398489SKevin Wolf            'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'},
549*d4398489SKevin Wolf        }
550*d4398489SKevin Wolf
551*d4398489SKevin Wolf    def blkdebug_event(self, event, is_raw=False):
552*d4398489SKevin Wolf        if event:
553*d4398489SKevin Wolf            return [{
554*d4398489SKevin Wolf                'event': event,
555*d4398489SKevin Wolf                'sector': 512 if is_raw else 1024,
556*d4398489SKevin Wolf                'once': True,
557*d4398489SKevin Wolf            }]
558*d4398489SKevin Wolf        return None
559*d4398489SKevin Wolf
560*d4398489SKevin Wolf    def prepare_and_start_job(self, on_error, active=True,
561*d4398489SKevin Wolf                              top_event=None, mid_event=None, base_event=None):
562*d4398489SKevin Wolf
563*d4398489SKevin Wolf        top_debug = self.blkdebug_event(top_event)
564*d4398489SKevin Wolf        mid_debug = self.blkdebug_event(mid_event)
565*d4398489SKevin Wolf        base_debug = self.blkdebug_event(base_event, True)
566*d4398489SKevin Wolf
567*d4398489SKevin Wolf        self.add_block_nodes(top_debug=top_debug, mid_debug=mid_debug,
568*d4398489SKevin Wolf                             base_debug=base_debug)
569*d4398489SKevin Wolf
570*d4398489SKevin Wolf        result = self.vm.qmp('block-commit', job_id='job0', device='top-fmt',
571*d4398489SKevin Wolf                             top_node='top-fmt' if active else 'mid-fmt',
572*d4398489SKevin Wolf                             base_node='mid-fmt' if active else 'base-fmt',
573*d4398489SKevin Wolf                             on_error=on_error)
574*d4398489SKevin Wolf        self.assert_qmp(result, 'return', {})
575*d4398489SKevin Wolf
576*d4398489SKevin Wolf    def testActiveReadErrorReport(self):
577*d4398489SKevin Wolf        self.prepare_and_start_job('report', top_event='read_aio')
578*d4398489SKevin Wolf        self.run_job([
579*d4398489SKevin Wolf            self.event_error('read', 'report'),
580*d4398489SKevin Wolf            self.event_completed('Input/output error')
581*d4398489SKevin Wolf        ])
582*d4398489SKevin Wolf
583*d4398489SKevin Wolf        self.vm.shutdown()
584*d4398489SKevin Wolf        self.assertFalse(iotests.compare_images(test_img, mid_img),
585*d4398489SKevin Wolf                         'target image matches source after error')
586*d4398489SKevin Wolf
587*d4398489SKevin Wolf    def testActiveReadErrorStop(self):
588*d4398489SKevin Wolf        self.prepare_and_start_job('stop', top_event='read_aio')
589*d4398489SKevin Wolf        self.run_job([
590*d4398489SKevin Wolf            self.event_error('read', 'stop'),
591*d4398489SKevin Wolf            self.event_ready(),
592*d4398489SKevin Wolf            self.event_completed()
593*d4398489SKevin Wolf        ], error_pauses_job=True)
594*d4398489SKevin Wolf
595*d4398489SKevin Wolf        self.vm.shutdown()
596*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(test_img, mid_img),
597*d4398489SKevin Wolf                        'target image does not match source after commit')
598*d4398489SKevin Wolf
599*d4398489SKevin Wolf    def testActiveReadErrorIgnore(self):
600*d4398489SKevin Wolf        self.prepare_and_start_job('ignore', top_event='read_aio')
601*d4398489SKevin Wolf        self.run_job([
602*d4398489SKevin Wolf            self.event_error('read', 'ignore'),
603*d4398489SKevin Wolf            self.event_ready(),
604*d4398489SKevin Wolf            self.event_completed()
605*d4398489SKevin Wolf        ])
606*d4398489SKevin Wolf
607*d4398489SKevin Wolf        # For commit, 'ignore' actually means retry, so this will succeed
608*d4398489SKevin Wolf        self.vm.shutdown()
609*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(test_img, mid_img),
610*d4398489SKevin Wolf                        'target image does not match source after commit')
611*d4398489SKevin Wolf
612*d4398489SKevin Wolf    def testActiveWriteErrorReport(self):
613*d4398489SKevin Wolf        self.prepare_and_start_job('report', mid_event='write_aio')
614*d4398489SKevin Wolf        self.run_job([
615*d4398489SKevin Wolf            self.event_error('write', 'report'),
616*d4398489SKevin Wolf            self.event_completed('Input/output error')
617*d4398489SKevin Wolf        ])
618*d4398489SKevin Wolf
619*d4398489SKevin Wolf        self.vm.shutdown()
620*d4398489SKevin Wolf        self.assertFalse(iotests.compare_images(test_img, mid_img),
621*d4398489SKevin Wolf                         'target image matches source after error')
622*d4398489SKevin Wolf
623*d4398489SKevin Wolf    def testActiveWriteErrorStop(self):
624*d4398489SKevin Wolf        self.prepare_and_start_job('stop', mid_event='write_aio')
625*d4398489SKevin Wolf        self.run_job([
626*d4398489SKevin Wolf            self.event_error('write', 'stop'),
627*d4398489SKevin Wolf            self.event_ready(),
628*d4398489SKevin Wolf            self.event_completed()
629*d4398489SKevin Wolf        ], error_pauses_job=True)
630*d4398489SKevin Wolf
631*d4398489SKevin Wolf        self.vm.shutdown()
632*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(test_img, mid_img),
633*d4398489SKevin Wolf                        'target image does not match source after commit')
634*d4398489SKevin Wolf
635*d4398489SKevin Wolf    def testActiveWriteErrorIgnore(self):
636*d4398489SKevin Wolf        self.prepare_and_start_job('ignore', mid_event='write_aio')
637*d4398489SKevin Wolf        self.run_job([
638*d4398489SKevin Wolf            self.event_error('write', 'ignore'),
639*d4398489SKevin Wolf            self.event_ready(),
640*d4398489SKevin Wolf            self.event_completed()
641*d4398489SKevin Wolf        ])
642*d4398489SKevin Wolf
643*d4398489SKevin Wolf        # For commit, 'ignore' actually means retry, so this will succeed
644*d4398489SKevin Wolf        self.vm.shutdown()
645*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(test_img, mid_img),
646*d4398489SKevin Wolf                        'target image does not match source after commit')
647*d4398489SKevin Wolf
648*d4398489SKevin Wolf    def testIntermediateReadErrorReport(self):
649*d4398489SKevin Wolf        self.prepare_and_start_job('report', active=False, mid_event='read_aio')
650*d4398489SKevin Wolf        self.run_job([
651*d4398489SKevin Wolf            self.event_error('read', 'report'),
652*d4398489SKevin Wolf            self.event_completed('Input/output error', active=False)
653*d4398489SKevin Wolf        ])
654*d4398489SKevin Wolf
655*d4398489SKevin Wolf        self.vm.shutdown()
656*d4398489SKevin Wolf        self.assertFalse(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
657*d4398489SKevin Wolf                         'target image matches source after error')
658*d4398489SKevin Wolf
659*d4398489SKevin Wolf    def testIntermediateReadErrorStop(self):
660*d4398489SKevin Wolf        self.prepare_and_start_job('stop', active=False, mid_event='read_aio')
661*d4398489SKevin Wolf        self.run_job([
662*d4398489SKevin Wolf            self.event_error('read', 'stop'),
663*d4398489SKevin Wolf            self.event_completed(active=False)
664*d4398489SKevin Wolf        ], error_pauses_job=True)
665*d4398489SKevin Wolf
666*d4398489SKevin Wolf        self.vm.shutdown()
667*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
668*d4398489SKevin Wolf                        'target image does not match source after commit')
669*d4398489SKevin Wolf
670*d4398489SKevin Wolf    def testIntermediateReadErrorIgnore(self):
671*d4398489SKevin Wolf        self.prepare_and_start_job('ignore', active=False, mid_event='read_aio')
672*d4398489SKevin Wolf        self.run_job([
673*d4398489SKevin Wolf            self.event_error('read', 'ignore'),
674*d4398489SKevin Wolf            self.event_completed(active=False)
675*d4398489SKevin Wolf        ])
676*d4398489SKevin Wolf
677*d4398489SKevin Wolf        # For commit, 'ignore' actually means retry, so this will succeed
678*d4398489SKevin Wolf        self.vm.shutdown()
679*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
680*d4398489SKevin Wolf                        'target image does not match source after commit')
681*d4398489SKevin Wolf
682*d4398489SKevin Wolf    def testIntermediateWriteErrorReport(self):
683*d4398489SKevin Wolf        self.prepare_and_start_job('report', active=False, base_event='write_aio')
684*d4398489SKevin Wolf        self.run_job([
685*d4398489SKevin Wolf            self.event_error('write', 'report'),
686*d4398489SKevin Wolf            self.event_completed('Input/output error', active=False)
687*d4398489SKevin Wolf        ])
688*d4398489SKevin Wolf
689*d4398489SKevin Wolf        self.vm.shutdown()
690*d4398489SKevin Wolf        self.assertFalse(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
691*d4398489SKevin Wolf                         'target image matches source after error')
692*d4398489SKevin Wolf
693*d4398489SKevin Wolf    def testIntermediateWriteErrorStop(self):
694*d4398489SKevin Wolf        self.prepare_and_start_job('stop', active=False, base_event='write_aio')
695*d4398489SKevin Wolf        self.run_job([
696*d4398489SKevin Wolf            self.event_error('write', 'stop'),
697*d4398489SKevin Wolf            self.event_completed(active=False)
698*d4398489SKevin Wolf        ], error_pauses_job=True)
699*d4398489SKevin Wolf
700*d4398489SKevin Wolf        self.vm.shutdown()
701*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
702*d4398489SKevin Wolf                        'target image does not match source after commit')
703*d4398489SKevin Wolf
704*d4398489SKevin Wolf    def testIntermediateWriteErrorIgnore(self):
705*d4398489SKevin Wolf        self.prepare_and_start_job('ignore', active=False, base_event='write_aio')
706*d4398489SKevin Wolf        self.run_job([
707*d4398489SKevin Wolf            self.event_error('write', 'ignore'),
708*d4398489SKevin Wolf            self.event_completed(active=False)
709*d4398489SKevin Wolf        ])
710*d4398489SKevin Wolf
711*d4398489SKevin Wolf        # For commit, 'ignore' actually means retry, so this will succeed
712*d4398489SKevin Wolf        self.vm.shutdown()
713*d4398489SKevin Wolf        self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'),
714*d4398489SKevin Wolf                        'target image does not match source after commit')
715*d4398489SKevin Wolf
716747051cdSJeff Codyif __name__ == '__main__':
717103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2', 'qed'],
718103cbc77SMax Reitz                 supported_protocols=['file'])
719