xref: /openbmc/qemu/tests/qemu-iotests/040 (revision 86472071)
1747051cdSJeff Cody#!/usr/bin/env python
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")
88f1d5516aSCornelia Huck        if iotests.qemu_default_machine == 's390-ccw-virtio':
89f1d5516aSCornelia Huck            self.vm.add_device("virtio-scsi-ccw")
90f1d5516aSCornelia Huck        else:
91c3971b88SKevin Wolf            self.vm.add_device("virtio-scsi-pci")
92f1d5516aSCornelia Huck
93c3971b88SKevin Wolf        self.vm.add_device("scsi-hd,id=scsi0,drive=drive0")
94747051cdSJeff Cody        self.vm.launch()
95*86472071SMax Reitz        self.has_quit = False
96747051cdSJeff Cody
97747051cdSJeff Cody    def tearDown(self):
98*86472071SMax Reitz        self.vm.shutdown(has_quit=self.has_quit)
99747051cdSJeff Cody        os.remove(test_img)
100747051cdSJeff Cody        os.remove(mid_img)
101747051cdSJeff Cody        os.remove(backing_img)
102747051cdSJeff Cody
103747051cdSJeff Cody    def test_commit(self):
1044de43470SFam Zheng        self.run_commit_test(mid_img, backing_img)
10590c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
10690c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
107747051cdSJeff Cody
108d57177a4SKevin Wolf    def test_commit_node(self):
109d57177a4SKevin Wolf        self.run_commit_test("mid", "base", node_names=True)
110d57177a4SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
111d57177a4SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
112d57177a4SKevin Wolf
113*86472071SMax Reitz    def test_commit_with_filter_and_quit(self):
114*86472071SMax Reitz        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
115*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
116*86472071SMax Reitz
117*86472071SMax Reitz        # Add a filter outside of the backing chain
118*86472071SMax Reitz        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
119*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
120*86472071SMax Reitz
121*86472071SMax Reitz        result = self.vm.qmp('block-commit', device='drive0')
122*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
123*86472071SMax Reitz
124*86472071SMax Reitz        # Quit immediately, thus forcing a simultaneous cancel of the
125*86472071SMax Reitz        # block job and a bdrv_drain_all()
126*86472071SMax Reitz        result = self.vm.qmp('quit')
127*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
128*86472071SMax Reitz
129*86472071SMax Reitz        self.has_quit = True
130*86472071SMax Reitz
131*86472071SMax Reitz    # Same as above, but this time we add the filter after starting the job
132*86472071SMax Reitz    def test_commit_plus_filter_and_quit(self):
133*86472071SMax Reitz        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
134*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
135*86472071SMax Reitz
136*86472071SMax Reitz        result = self.vm.qmp('block-commit', device='drive0')
137*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
138*86472071SMax Reitz
139*86472071SMax Reitz        # Add a filter outside of the backing chain
140*86472071SMax Reitz        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
141*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
142*86472071SMax Reitz
143*86472071SMax Reitz        # Quit immediately, thus forcing a simultaneous cancel of the
144*86472071SMax Reitz        # block job and a bdrv_drain_all()
145*86472071SMax Reitz        result = self.vm.qmp('quit')
146*86472071SMax Reitz        self.assert_qmp(result, 'return', {})
147*86472071SMax Reitz
148*86472071SMax Reitz        self.has_quit = True
149*86472071SMax Reitz
150747051cdSJeff Cody    def test_device_not_found(self):
151747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
152747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
153747051cdSJeff Cody
154747051cdSJeff Cody    def test_top_same_base(self):
155fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
156747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
157747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
158d5208c45SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
159747051cdSJeff Cody
160747051cdSJeff Cody    def test_top_invalid(self):
161fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
162747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
163747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
164747051cdSJeff Cody        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
165747051cdSJeff Cody
166747051cdSJeff Cody    def test_base_invalid(self):
167fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
168747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
169747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
170747051cdSJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
171747051cdSJeff Cody
172d57177a4SKevin Wolf    def test_top_node_invalid(self):
173d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
174d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base')
175d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
176d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
177d57177a4SKevin Wolf
178d57177a4SKevin Wolf    def test_base_node_invalid(self):
179d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
180d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile')
181d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
182d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
183d57177a4SKevin Wolf
184d57177a4SKevin Wolf    def test_top_path_and_node(self):
185d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
186d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img)
187d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
188d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive")
189d57177a4SKevin Wolf
190d57177a4SKevin Wolf    def test_base_path_and_node(self):
191d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
192d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img)
193d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
194d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive")
195d57177a4SKevin Wolf
196747051cdSJeff Cody    def test_top_is_active(self):
1978b9a30caSFam Zheng        self.run_commit_test(test_img, backing_img, need_ready=True)
19890c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
19990c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
200747051cdSJeff Cody
2017676e2c5SJeff Cody    def test_top_is_default_active(self):
2027676e2c5SJeff Cody        self.run_default_commit_test()
20390c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
20490c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
2057676e2c5SJeff Cody
206747051cdSJeff Cody    def test_top_and_base_reversed(self):
207fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
208747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
209747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
210d5208c45SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
211747051cdSJeff Cody
212d57177a4SKevin Wolf    def test_top_and_base_node_reversed(self):
213d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
214d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top')
215d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
216d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain")
217d57177a4SKevin Wolf
218d57177a4SKevin Wolf    def test_top_node_in_wrong_chain(self):
219d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
220d57177a4SKevin Wolf
221d57177a4SKevin Wolf        result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null')
222d57177a4SKevin Wolf        self.assert_qmp(result, 'return', {})
223d57177a4SKevin Wolf
224d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base')
225d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
226d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain")
227d57177a4SKevin Wolf
228c3971b88SKevin Wolf    # When the job is running on a BB that is automatically deleted on hot
229c3971b88SKevin Wolf    # unplug, the job is cancelled when the device disappears
230c3971b88SKevin Wolf    def test_hot_unplug(self):
231c3971b88SKevin Wolf        if self.image_len == 0:
232c3971b88SKevin Wolf            return
233c3971b88SKevin Wolf
234c3971b88SKevin Wolf        self.assert_no_active_block_jobs()
235c3971b88SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
2369a3a9a63SMax Reitz                             base=backing_img, speed=(self.image_len // 4))
237c3971b88SKevin Wolf        self.assert_qmp(result, 'return', {})
238c3971b88SKevin Wolf        result = self.vm.qmp('device_del', id='scsi0')
239c3971b88SKevin Wolf        self.assert_qmp(result, 'return', {})
240c3971b88SKevin Wolf
241c3971b88SKevin Wolf        cancelled = False
242c3971b88SKevin Wolf        deleted = False
243c3971b88SKevin Wolf        while not cancelled or not deleted:
244c3971b88SKevin Wolf            for event in self.vm.get_qmp_events(wait=True):
245c3971b88SKevin Wolf                if event['event'] == 'DEVICE_DELETED':
246c3971b88SKevin Wolf                    self.assert_qmp(event, 'data/device', 'scsi0')
247c3971b88SKevin Wolf                    deleted = True
248c3971b88SKevin Wolf                elif event['event'] == 'BLOCK_JOB_CANCELLED':
249c3971b88SKevin Wolf                    self.assert_qmp(event, 'data/device', 'drive0')
250c3971b88SKevin Wolf                    cancelled = True
2511dac83f1SKevin Wolf                elif event['event'] == 'JOB_STATUS_CHANGE':
2521dac83f1SKevin Wolf                    self.assert_qmp(event, 'data/id', 'drive0')
253c3971b88SKevin Wolf                else:
254c3971b88SKevin Wolf                    self.fail("Unexpected event %s" % (event['event']))
255c3971b88SKevin Wolf
256c3971b88SKevin Wolf        self.assert_no_active_block_jobs()
257747051cdSJeff Cody
258d3c8c674SKevin Wolf    # Tests that the insertion of the commit_top filter node doesn't make a
259d3c8c674SKevin Wolf    # difference to query-blockstat
260d3c8c674SKevin Wolf    def test_implicit_node(self):
261d3c8c674SKevin Wolf        if self.image_len == 0:
262d3c8c674SKevin Wolf            return
263d3c8c674SKevin Wolf
264d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
265d3c8c674SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
2669a3a9a63SMax Reitz                             base=backing_img, speed=(self.image_len // 4))
267d3c8c674SKevin Wolf        self.assert_qmp(result, 'return', {})
268d3c8c674SKevin Wolf
269d3c8c674SKevin Wolf        result = self.vm.qmp('query-block')
270d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
271d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
272d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img)
273d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2)
274d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
275d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img)
276d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img)
277d3c8c674SKevin Wolf
278d3c8c674SKevin Wolf        result = self.vm.qmp('query-blockstats')
279d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/node-name', 'top')
280d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/node-name', 'mid')
281d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base')
282d3c8c674SKevin Wolf
283d3c8c674SKevin Wolf        self.cancel_and_wait()
284d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
285d3c8c674SKevin Wolf
2866bf0d1f4SJeff Codyclass TestRelativePaths(ImageCommitTestCase):
2876bf0d1f4SJeff Cody    image_len = 1 * 1024 * 1024
2886bf0d1f4SJeff Cody    test_len = 1 * 1024 * 256
2896bf0d1f4SJeff Cody
2906bf0d1f4SJeff Cody    dir1 = "dir1"
2916bf0d1f4SJeff Cody    dir2 = "dir2/"
2926bf0d1f4SJeff Cody    dir3 = "dir2/dir3/"
2936bf0d1f4SJeff Cody
2946bf0d1f4SJeff Cody    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
2956bf0d1f4SJeff Cody    mid_img = "../mid.img"
2966bf0d1f4SJeff Cody    backing_img = "../dir1/backing.img"
2976bf0d1f4SJeff Cody
2986bf0d1f4SJeff Cody    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
2996bf0d1f4SJeff Cody    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
3006bf0d1f4SJeff Cody
3016bf0d1f4SJeff Cody    def setUp(self):
3026bf0d1f4SJeff Cody        try:
3036bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
3046bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
3056bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
3066bf0d1f4SJeff Cody        except OSError as exception:
3076bf0d1f4SJeff Cody            if exception.errno != errno.EEXIST:
3086bf0d1f4SJeff Cody                raise
309915365a9SFam Zheng        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
3106bf0d1f4SJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
3116bf0d1f4SJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
3126bf0d1f4SJeff Cody        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
3136bf0d1f4SJeff Cody        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
31490c9b167SKevin Wolf        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
31590c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
3166bf0d1f4SJeff Cody        self.vm = iotests.VM().add_drive(self.test_img)
3176bf0d1f4SJeff Cody        self.vm.launch()
3186bf0d1f4SJeff Cody
3196bf0d1f4SJeff Cody    def tearDown(self):
3206bf0d1f4SJeff Cody        self.vm.shutdown()
3216bf0d1f4SJeff Cody        os.remove(self.test_img)
3226bf0d1f4SJeff Cody        os.remove(self.mid_img_abs)
3236bf0d1f4SJeff Cody        os.remove(self.backing_img_abs)
3246bf0d1f4SJeff Cody        try:
3256bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
3266bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
3276bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
3286bf0d1f4SJeff Cody        except OSError as exception:
3296bf0d1f4SJeff Cody            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
3306bf0d1f4SJeff Cody                raise
3316bf0d1f4SJeff Cody
3326bf0d1f4SJeff Cody    def test_commit(self):
3334de43470SFam Zheng        self.run_commit_test(self.mid_img, self.backing_img)
33490c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
33590c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
3366bf0d1f4SJeff Cody
3376bf0d1f4SJeff Cody    def test_device_not_found(self):
3386bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
3396bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
3406bf0d1f4SJeff Cody
3416bf0d1f4SJeff Cody    def test_top_same_base(self):
342fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3436bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
3446bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3456bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
3466bf0d1f4SJeff Cody
3476bf0d1f4SJeff Cody    def test_top_invalid(self):
348fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3496bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
3506bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3516bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
3526bf0d1f4SJeff Cody
3536bf0d1f4SJeff Cody    def test_base_invalid(self):
354fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3556bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
3566bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3576bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
3586bf0d1f4SJeff Cody
3596bf0d1f4SJeff Cody    def test_top_is_active(self):
3604de43470SFam Zheng        self.run_commit_test(self.test_img, self.backing_img)
36190c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
36290c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
3636bf0d1f4SJeff Cody
3646bf0d1f4SJeff Cody    def test_top_and_base_reversed(self):
365fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3666bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
3676bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3686bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
3696bf0d1f4SJeff Cody
370747051cdSJeff Cody
371747051cdSJeff Codyclass TestSetSpeed(ImageCommitTestCase):
372747051cdSJeff Cody    image_len = 80 * 1024 * 1024 # MB
373747051cdSJeff Cody
374747051cdSJeff Cody    def setUp(self):
375747051cdSJeff Cody        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
376747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
377747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
37890c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
37990c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
38051c493c5SMax Reitz        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
381747051cdSJeff Cody        self.vm.launch()
382747051cdSJeff Cody
383747051cdSJeff Cody    def tearDown(self):
384747051cdSJeff Cody        self.vm.shutdown()
385747051cdSJeff Cody        os.remove(test_img)
386747051cdSJeff Cody        os.remove(mid_img)
387747051cdSJeff Cody        os.remove(backing_img)
388747051cdSJeff Cody
389747051cdSJeff Cody    def test_set_speed(self):
390fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
391747051cdSJeff Cody
392b59b3d57SFam Zheng        self.vm.pause_drive('drive0')
393747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
394747051cdSJeff Cody        self.assert_qmp(result, 'return', {})
395747051cdSJeff Cody
396747051cdSJeff Cody        # Ensure the speed we set was accepted
397747051cdSJeff Cody        result = self.vm.qmp('query-block-jobs')
398747051cdSJeff Cody        self.assert_qmp(result, 'return[0]/device', 'drive0')
399747051cdSJeff Cody        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
400747051cdSJeff Cody
401b59b3d57SFam Zheng        self.cancel_and_wait(resume=True)
402747051cdSJeff Cody
4038b9a30caSFam Zhengclass TestActiveZeroLengthImage(TestSingleDrive):
4048b9a30caSFam Zheng    image_len = 0
405747051cdSJeff Cody
406bcdce5a7SAlberto Garciaclass TestReopenOverlay(ImageCommitTestCase):
407bcdce5a7SAlberto Garcia    image_len = 1024 * 1024
408bcdce5a7SAlberto Garcia    img0 = os.path.join(iotests.test_dir, '0.img')
409bcdce5a7SAlberto Garcia    img1 = os.path.join(iotests.test_dir, '1.img')
410bcdce5a7SAlberto Garcia    img2 = os.path.join(iotests.test_dir, '2.img')
411bcdce5a7SAlberto Garcia    img3 = os.path.join(iotests.test_dir, '3.img')
412bcdce5a7SAlberto Garcia
413bcdce5a7SAlberto Garcia    def setUp(self):
414bcdce5a7SAlberto Garcia        iotests.create_image(self.img0, self.image_len)
415bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
416bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
417bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
418bcdce5a7SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
419bcdce5a7SAlberto Garcia        self.vm = iotests.VM().add_drive(self.img3)
420bcdce5a7SAlberto Garcia        self.vm.launch()
421bcdce5a7SAlberto Garcia
422bcdce5a7SAlberto Garcia    def tearDown(self):
423bcdce5a7SAlberto Garcia        self.vm.shutdown()
424bcdce5a7SAlberto Garcia        os.remove(self.img0)
425bcdce5a7SAlberto Garcia        os.remove(self.img1)
426bcdce5a7SAlberto Garcia        os.remove(self.img2)
427bcdce5a7SAlberto Garcia        os.remove(self.img3)
428bcdce5a7SAlberto Garcia
429bcdce5a7SAlberto Garcia    # This tests what happens when the overlay image of the 'top' node
430bcdce5a7SAlberto Garcia    # needs to be reopened in read-write mode in order to update the
431bcdce5a7SAlberto Garcia    # backing image string.
432bcdce5a7SAlberto Garcia    def test_reopen_overlay(self):
433bcdce5a7SAlberto Garcia        self.run_commit_test(self.img1, self.img0)
434bcdce5a7SAlberto Garcia
435747051cdSJeff Codyif __name__ == '__main__':
436747051cdSJeff Cody    iotests.main(supported_fmts=['qcow2', 'qed'])
437