xref: /openbmc/qemu/tests/qemu-iotests/040 (revision 9a3a9a63)
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()
95747051cdSJeff Cody
96747051cdSJeff Cody    def tearDown(self):
97747051cdSJeff Cody        self.vm.shutdown()
98747051cdSJeff Cody        os.remove(test_img)
99747051cdSJeff Cody        os.remove(mid_img)
100747051cdSJeff Cody        os.remove(backing_img)
101747051cdSJeff Cody
102747051cdSJeff Cody    def test_commit(self):
1034de43470SFam Zheng        self.run_commit_test(mid_img, backing_img)
10490c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
10590c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
106747051cdSJeff Cody
107d57177a4SKevin Wolf    def test_commit_node(self):
108d57177a4SKevin Wolf        self.run_commit_test("mid", "base", node_names=True)
109d57177a4SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
110d57177a4SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
111d57177a4SKevin Wolf
112747051cdSJeff Cody    def test_device_not_found(self):
113747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
114747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
115747051cdSJeff Cody
116747051cdSJeff Cody    def test_top_same_base(self):
117fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
118747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
119747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
120d5208c45SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
121747051cdSJeff Cody
122747051cdSJeff Cody    def test_top_invalid(self):
123fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
124747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
125747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
126747051cdSJeff Cody        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
127747051cdSJeff Cody
128747051cdSJeff Cody    def test_base_invalid(self):
129fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
130747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
131747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
132747051cdSJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
133747051cdSJeff Cody
134d57177a4SKevin Wolf    def test_top_node_invalid(self):
135d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
136d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base')
137d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
138d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
139d57177a4SKevin Wolf
140d57177a4SKevin Wolf    def test_base_node_invalid(self):
141d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
142d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile')
143d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
144d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
145d57177a4SKevin Wolf
146d57177a4SKevin Wolf    def test_top_path_and_node(self):
147d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
148d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img)
149d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
150d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive")
151d57177a4SKevin Wolf
152d57177a4SKevin Wolf    def test_base_path_and_node(self):
153d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
154d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img)
155d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
156d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive")
157d57177a4SKevin Wolf
158747051cdSJeff Cody    def test_top_is_active(self):
1598b9a30caSFam Zheng        self.run_commit_test(test_img, backing_img, need_ready=True)
16090c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
16190c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
162747051cdSJeff Cody
1637676e2c5SJeff Cody    def test_top_is_default_active(self):
1647676e2c5SJeff Cody        self.run_default_commit_test()
16590c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
16690c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
1677676e2c5SJeff Cody
168747051cdSJeff Cody    def test_top_and_base_reversed(self):
169fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
170747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
171747051cdSJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
172d5208c45SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
173747051cdSJeff Cody
174d57177a4SKevin Wolf    def test_top_and_base_node_reversed(self):
175d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
176d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top')
177d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
178d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain")
179d57177a4SKevin Wolf
180d57177a4SKevin Wolf    def test_top_node_in_wrong_chain(self):
181d57177a4SKevin Wolf        self.assert_no_active_block_jobs()
182d57177a4SKevin Wolf
183d57177a4SKevin Wolf        result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null')
184d57177a4SKevin Wolf        self.assert_qmp(result, 'return', {})
185d57177a4SKevin Wolf
186d57177a4SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base')
187d57177a4SKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
188d57177a4SKevin Wolf        self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain")
189d57177a4SKevin Wolf
190c3971b88SKevin Wolf    # When the job is running on a BB that is automatically deleted on hot
191c3971b88SKevin Wolf    # unplug, the job is cancelled when the device disappears
192c3971b88SKevin Wolf    def test_hot_unplug(self):
193c3971b88SKevin Wolf        if self.image_len == 0:
194c3971b88SKevin Wolf            return
195c3971b88SKevin Wolf
196c3971b88SKevin Wolf        self.assert_no_active_block_jobs()
197c3971b88SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
198*9a3a9a63SMax Reitz                             base=backing_img, speed=(self.image_len // 4))
199c3971b88SKevin Wolf        self.assert_qmp(result, 'return', {})
200c3971b88SKevin Wolf        result = self.vm.qmp('device_del', id='scsi0')
201c3971b88SKevin Wolf        self.assert_qmp(result, 'return', {})
202c3971b88SKevin Wolf
203c3971b88SKevin Wolf        cancelled = False
204c3971b88SKevin Wolf        deleted = False
205c3971b88SKevin Wolf        while not cancelled or not deleted:
206c3971b88SKevin Wolf            for event in self.vm.get_qmp_events(wait=True):
207c3971b88SKevin Wolf                if event['event'] == 'DEVICE_DELETED':
208c3971b88SKevin Wolf                    self.assert_qmp(event, 'data/device', 'scsi0')
209c3971b88SKevin Wolf                    deleted = True
210c3971b88SKevin Wolf                elif event['event'] == 'BLOCK_JOB_CANCELLED':
211c3971b88SKevin Wolf                    self.assert_qmp(event, 'data/device', 'drive0')
212c3971b88SKevin Wolf                    cancelled = True
2131dac83f1SKevin Wolf                elif event['event'] == 'JOB_STATUS_CHANGE':
2141dac83f1SKevin Wolf                    self.assert_qmp(event, 'data/id', 'drive0')
215c3971b88SKevin Wolf                else:
216c3971b88SKevin Wolf                    self.fail("Unexpected event %s" % (event['event']))
217c3971b88SKevin Wolf
218c3971b88SKevin Wolf        self.assert_no_active_block_jobs()
219747051cdSJeff Cody
220d3c8c674SKevin Wolf    # Tests that the insertion of the commit_top filter node doesn't make a
221d3c8c674SKevin Wolf    # difference to query-blockstat
222d3c8c674SKevin Wolf    def test_implicit_node(self):
223d3c8c674SKevin Wolf        if self.image_len == 0:
224d3c8c674SKevin Wolf            return
225d3c8c674SKevin Wolf
226d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
227d3c8c674SKevin Wolf        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
228*9a3a9a63SMax Reitz                             base=backing_img, speed=(self.image_len // 4))
229d3c8c674SKevin Wolf        self.assert_qmp(result, 'return', {})
230d3c8c674SKevin Wolf
231d3c8c674SKevin Wolf        result = self.vm.qmp('query-block')
232d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
233d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
234d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img)
235d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2)
236d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
237d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img)
238d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img)
239d3c8c674SKevin Wolf
240d3c8c674SKevin Wolf        result = self.vm.qmp('query-blockstats')
241d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/node-name', 'top')
242d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/node-name', 'mid')
243d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base')
244d3c8c674SKevin Wolf
245d3c8c674SKevin Wolf        self.cancel_and_wait()
246d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
247d3c8c674SKevin Wolf
2486bf0d1f4SJeff Codyclass TestRelativePaths(ImageCommitTestCase):
2496bf0d1f4SJeff Cody    image_len = 1 * 1024 * 1024
2506bf0d1f4SJeff Cody    test_len = 1 * 1024 * 256
2516bf0d1f4SJeff Cody
2526bf0d1f4SJeff Cody    dir1 = "dir1"
2536bf0d1f4SJeff Cody    dir2 = "dir2/"
2546bf0d1f4SJeff Cody    dir3 = "dir2/dir3/"
2556bf0d1f4SJeff Cody
2566bf0d1f4SJeff Cody    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
2576bf0d1f4SJeff Cody    mid_img = "../mid.img"
2586bf0d1f4SJeff Cody    backing_img = "../dir1/backing.img"
2596bf0d1f4SJeff Cody
2606bf0d1f4SJeff Cody    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
2616bf0d1f4SJeff Cody    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
2626bf0d1f4SJeff Cody
2636bf0d1f4SJeff Cody    def setUp(self):
2646bf0d1f4SJeff Cody        try:
2656bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
2666bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
2676bf0d1f4SJeff Cody            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
2686bf0d1f4SJeff Cody        except OSError as exception:
2696bf0d1f4SJeff Cody            if exception.errno != errno.EEXIST:
2706bf0d1f4SJeff Cody                raise
271915365a9SFam Zheng        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
2726bf0d1f4SJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
2736bf0d1f4SJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
2746bf0d1f4SJeff Cody        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
2756bf0d1f4SJeff Cody        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
27690c9b167SKevin Wolf        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
27790c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
2786bf0d1f4SJeff Cody        self.vm = iotests.VM().add_drive(self.test_img)
2796bf0d1f4SJeff Cody        self.vm.launch()
2806bf0d1f4SJeff Cody
2816bf0d1f4SJeff Cody    def tearDown(self):
2826bf0d1f4SJeff Cody        self.vm.shutdown()
2836bf0d1f4SJeff Cody        os.remove(self.test_img)
2846bf0d1f4SJeff Cody        os.remove(self.mid_img_abs)
2856bf0d1f4SJeff Cody        os.remove(self.backing_img_abs)
2866bf0d1f4SJeff Cody        try:
2876bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
2886bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
2896bf0d1f4SJeff Cody            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
2906bf0d1f4SJeff Cody        except OSError as exception:
2916bf0d1f4SJeff Cody            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
2926bf0d1f4SJeff Cody                raise
2936bf0d1f4SJeff Cody
2946bf0d1f4SJeff Cody    def test_commit(self):
2954de43470SFam Zheng        self.run_commit_test(self.mid_img, self.backing_img)
29690c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
29790c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
2986bf0d1f4SJeff Cody
2996bf0d1f4SJeff Cody    def test_device_not_found(self):
3006bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
3016bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
3026bf0d1f4SJeff Cody
3036bf0d1f4SJeff Cody    def test_top_same_base(self):
304fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3056bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
3066bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3076bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
3086bf0d1f4SJeff Cody
3096bf0d1f4SJeff Cody    def test_top_invalid(self):
310fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3116bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
3126bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3136bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
3146bf0d1f4SJeff Cody
3156bf0d1f4SJeff Cody    def test_base_invalid(self):
316fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3176bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
3186bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3196bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
3206bf0d1f4SJeff Cody
3216bf0d1f4SJeff Cody    def test_top_is_active(self):
3224de43470SFam Zheng        self.run_commit_test(self.test_img, self.backing_img)
32390c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
32490c9b167SKevin Wolf        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
3256bf0d1f4SJeff Cody
3266bf0d1f4SJeff Cody    def test_top_and_base_reversed(self):
327fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
3286bf0d1f4SJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
3296bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/class', 'GenericError')
3306bf0d1f4SJeff Cody        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
3316bf0d1f4SJeff Cody
332747051cdSJeff Cody
333747051cdSJeff Codyclass TestSetSpeed(ImageCommitTestCase):
334747051cdSJeff Cody    image_len = 80 * 1024 * 1024 # MB
335747051cdSJeff Cody
336747051cdSJeff Cody    def setUp(self):
337747051cdSJeff Cody        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
338747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
339747051cdSJeff Cody        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
34090c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
34190c9b167SKevin Wolf        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
34251c493c5SMax Reitz        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
343747051cdSJeff Cody        self.vm.launch()
344747051cdSJeff Cody
345747051cdSJeff Cody    def tearDown(self):
346747051cdSJeff Cody        self.vm.shutdown()
347747051cdSJeff Cody        os.remove(test_img)
348747051cdSJeff Cody        os.remove(mid_img)
349747051cdSJeff Cody        os.remove(backing_img)
350747051cdSJeff Cody
351747051cdSJeff Cody    def test_set_speed(self):
352fb0a078fSFam Zheng        self.assert_no_active_block_jobs()
353747051cdSJeff Cody
354b59b3d57SFam Zheng        self.vm.pause_drive('drive0')
355747051cdSJeff Cody        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
356747051cdSJeff Cody        self.assert_qmp(result, 'return', {})
357747051cdSJeff Cody
358747051cdSJeff Cody        # Ensure the speed we set was accepted
359747051cdSJeff Cody        result = self.vm.qmp('query-block-jobs')
360747051cdSJeff Cody        self.assert_qmp(result, 'return[0]/device', 'drive0')
361747051cdSJeff Cody        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
362747051cdSJeff Cody
363b59b3d57SFam Zheng        self.cancel_and_wait(resume=True)
364747051cdSJeff Cody
3658b9a30caSFam Zhengclass TestActiveZeroLengthImage(TestSingleDrive):
3668b9a30caSFam Zheng    image_len = 0
367747051cdSJeff Cody
368bcdce5a7SAlberto Garciaclass TestReopenOverlay(ImageCommitTestCase):
369bcdce5a7SAlberto Garcia    image_len = 1024 * 1024
370bcdce5a7SAlberto Garcia    img0 = os.path.join(iotests.test_dir, '0.img')
371bcdce5a7SAlberto Garcia    img1 = os.path.join(iotests.test_dir, '1.img')
372bcdce5a7SAlberto Garcia    img2 = os.path.join(iotests.test_dir, '2.img')
373bcdce5a7SAlberto Garcia    img3 = os.path.join(iotests.test_dir, '3.img')
374bcdce5a7SAlberto Garcia
375bcdce5a7SAlberto Garcia    def setUp(self):
376bcdce5a7SAlberto Garcia        iotests.create_image(self.img0, self.image_len)
377bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
378bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
379bcdce5a7SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
380bcdce5a7SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
381bcdce5a7SAlberto Garcia        self.vm = iotests.VM().add_drive(self.img3)
382bcdce5a7SAlberto Garcia        self.vm.launch()
383bcdce5a7SAlberto Garcia
384bcdce5a7SAlberto Garcia    def tearDown(self):
385bcdce5a7SAlberto Garcia        self.vm.shutdown()
386bcdce5a7SAlberto Garcia        os.remove(self.img0)
387bcdce5a7SAlberto Garcia        os.remove(self.img1)
388bcdce5a7SAlberto Garcia        os.remove(self.img2)
389bcdce5a7SAlberto Garcia        os.remove(self.img3)
390bcdce5a7SAlberto Garcia
391bcdce5a7SAlberto Garcia    # This tests what happens when the overlay image of the 'top' node
392bcdce5a7SAlberto Garcia    # needs to be reopened in read-write mode in order to update the
393bcdce5a7SAlberto Garcia    # backing image string.
394bcdce5a7SAlberto Garcia    def test_reopen_overlay(self):
395bcdce5a7SAlberto Garcia        self.run_commit_test(self.img1, self.img0)
396bcdce5a7SAlberto Garcia
397747051cdSJeff Codyif __name__ == '__main__':
398747051cdSJeff Cody    iotests.main(supported_fmts=['qcow2', 'qed'])
399