xref: /openbmc/qemu/tests/qemu-iotests/040 (revision 4a09d0bb)
1#!/usr/bin/env python
2#
3# Tests for image block commit.
4#
5# Copyright (C) 2012 IBM, Corp.
6# Copyright (C) 2012 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21# Test for live block commit
22# Derived from Image Streaming Test 030
23
24import time
25import os
26import iotests
27from iotests import qemu_img, qemu_io
28import struct
29import errno
30
31backing_img = os.path.join(iotests.test_dir, 'backing.img')
32mid_img = os.path.join(iotests.test_dir, 'mid.img')
33test_img = os.path.join(iotests.test_dir, 'test.img')
34
35class ImageCommitTestCase(iotests.QMPTestCase):
36    '''Abstract base class for image commit test cases'''
37
38    def wait_for_complete(self, need_ready=False):
39        completed = False
40        ready = False
41        while not completed:
42            for event in self.vm.get_qmp_events(wait=True):
43                if event['event'] == 'BLOCK_JOB_COMPLETED':
44                    self.assert_qmp_absent(event, 'data/error')
45                    self.assert_qmp(event, 'data/type', 'commit')
46                    self.assert_qmp(event, 'data/device', 'drive0')
47                    self.assert_qmp(event, 'data/offset', event['data']['len'])
48                    if need_ready:
49                        self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event")
50                    completed = True
51                elif event['event'] == 'BLOCK_JOB_READY':
52                    ready = True
53                    self.assert_qmp(event, 'data/type', 'commit')
54                    self.assert_qmp(event, 'data/device', 'drive0')
55                    self.vm.qmp('block-job-complete', device='drive0')
56
57        self.assert_no_active_block_jobs()
58        self.vm.shutdown()
59
60    def run_commit_test(self, top, base, need_ready=False):
61        self.assert_no_active_block_jobs()
62        result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
63        self.assert_qmp(result, 'return', {})
64        self.wait_for_complete(need_ready)
65
66    def run_default_commit_test(self):
67        self.assert_no_active_block_jobs()
68        result = self.vm.qmp('block-commit', device='drive0')
69        self.assert_qmp(result, 'return', {})
70        self.wait_for_complete()
71
72class TestSingleDrive(ImageCommitTestCase):
73    image_len = 1 * 1024 * 1024
74    test_len = 1 * 1024 * 256
75
76    def setUp(self):
77        iotests.create_image(backing_img, self.image_len)
78        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
79        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
80        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img)
81        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
82        self.vm = iotests.VM().add_drive(test_img)
83        self.vm.launch()
84
85    def tearDown(self):
86        self.vm.shutdown()
87        os.remove(test_img)
88        os.remove(mid_img)
89        os.remove(backing_img)
90
91    def test_commit(self):
92        self.run_commit_test(mid_img, backing_img)
93        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
94        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
95
96    def test_device_not_found(self):
97        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
98        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
99
100    def test_top_same_base(self):
101        self.assert_no_active_block_jobs()
102        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
103        self.assert_qmp(result, 'error/class', 'GenericError')
104        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
105
106    def test_top_invalid(self):
107        self.assert_no_active_block_jobs()
108        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
109        self.assert_qmp(result, 'error/class', 'GenericError')
110        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
111
112    def test_base_invalid(self):
113        self.assert_no_active_block_jobs()
114        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
115        self.assert_qmp(result, 'error/class', 'GenericError')
116        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
117
118    def test_top_is_active(self):
119        self.run_commit_test(test_img, backing_img, need_ready=True)
120        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
121        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
122
123    def test_top_is_default_active(self):
124        self.run_default_commit_test()
125        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
126        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
127
128    def test_top_and_base_reversed(self):
129        self.assert_no_active_block_jobs()
130        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
131        self.assert_qmp(result, 'error/class', 'GenericError')
132        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
133
134
135class TestRelativePaths(ImageCommitTestCase):
136    image_len = 1 * 1024 * 1024
137    test_len = 1 * 1024 * 256
138
139    dir1 = "dir1"
140    dir2 = "dir2/"
141    dir3 = "dir2/dir3/"
142
143    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
144    mid_img = "../mid.img"
145    backing_img = "../dir1/backing.img"
146
147    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
148    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
149
150    def setUp(self):
151        try:
152            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
153            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
154            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
155        except OSError as exception:
156            if exception.errno != errno.EEXIST:
157                raise
158        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
159        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
160        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
161        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
162        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
163        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
164        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
165        self.vm = iotests.VM().add_drive(self.test_img)
166        self.vm.launch()
167
168    def tearDown(self):
169        self.vm.shutdown()
170        os.remove(self.test_img)
171        os.remove(self.mid_img_abs)
172        os.remove(self.backing_img_abs)
173        try:
174            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
175            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
176            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
177        except OSError as exception:
178            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
179                raise
180
181    def test_commit(self):
182        self.run_commit_test(self.mid_img, self.backing_img)
183        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
184        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
185
186    def test_device_not_found(self):
187        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
188        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
189
190    def test_top_same_base(self):
191        self.assert_no_active_block_jobs()
192        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
193        self.assert_qmp(result, 'error/class', 'GenericError')
194        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
195
196    def test_top_invalid(self):
197        self.assert_no_active_block_jobs()
198        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
199        self.assert_qmp(result, 'error/class', 'GenericError')
200        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
201
202    def test_base_invalid(self):
203        self.assert_no_active_block_jobs()
204        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
205        self.assert_qmp(result, 'error/class', 'GenericError')
206        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
207
208    def test_top_is_active(self):
209        self.run_commit_test(self.test_img, self.backing_img)
210        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
211        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
212
213    def test_top_and_base_reversed(self):
214        self.assert_no_active_block_jobs()
215        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
216        self.assert_qmp(result, 'error/class', 'GenericError')
217        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
218
219
220class TestSetSpeed(ImageCommitTestCase):
221    image_len = 80 * 1024 * 1024 # MB
222
223    def setUp(self):
224        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
225        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
226        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
227        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
228        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
229        self.vm = iotests.VM().add_drive(test_img)
230        self.vm.launch()
231
232    def tearDown(self):
233        self.vm.shutdown()
234        os.remove(test_img)
235        os.remove(mid_img)
236        os.remove(backing_img)
237
238    def test_set_speed(self):
239        self.assert_no_active_block_jobs()
240
241        self.vm.pause_drive('drive0')
242        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
243        self.assert_qmp(result, 'return', {})
244
245        # Ensure the speed we set was accepted
246        result = self.vm.qmp('query-block-jobs')
247        self.assert_qmp(result, 'return[0]/device', 'drive0')
248        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
249
250        self.cancel_and_wait(resume=True)
251
252class TestActiveZeroLengthImage(TestSingleDrive):
253    image_len = 0
254
255class TestReopenOverlay(ImageCommitTestCase):
256    image_len = 1024 * 1024
257    img0 = os.path.join(iotests.test_dir, '0.img')
258    img1 = os.path.join(iotests.test_dir, '1.img')
259    img2 = os.path.join(iotests.test_dir, '2.img')
260    img3 = os.path.join(iotests.test_dir, '3.img')
261
262    def setUp(self):
263        iotests.create_image(self.img0, self.image_len)
264        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
265        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
266        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
267        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
268        self.vm = iotests.VM().add_drive(self.img3)
269        self.vm.launch()
270
271    def tearDown(self):
272        self.vm.shutdown()
273        os.remove(self.img0)
274        os.remove(self.img1)
275        os.remove(self.img2)
276        os.remove(self.img3)
277
278    # This tests what happens when the overlay image of the 'top' node
279    # needs to be reopened in read-write mode in order to update the
280    # backing image string.
281    def test_reopen_overlay(self):
282        self.run_commit_test(self.img1, self.img0)
283
284if __name__ == '__main__':
285    iotests.main(supported_fmts=['qcow2', 'qed'])
286