xref: /openbmc/qemu/tests/qemu-iotests/040 (revision b957a1b0)
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 assert_no_active_commit(self):
39        result = self.vm.qmp('query-block-jobs')
40        self.assert_qmp(result, 'return', [])
41
42    def cancel_and_wait(self, drive='drive0'):
43        '''Cancel a block job and wait for it to finish'''
44        result = self.vm.qmp('block-job-cancel', device=drive)
45        self.assert_qmp(result, 'return', {})
46
47        cancelled = False
48        while not cancelled:
49            for event in self.vm.get_qmp_events(wait=True):
50                if event['event'] == 'BLOCK_JOB_CANCELLED':
51                    self.assert_qmp(event, 'data/type', 'commit')
52                    self.assert_qmp(event, 'data/device', drive)
53                    cancelled = True
54
55        self.assert_no_active_commit()
56
57    def create_image(self, name, size):
58        file = open(name, 'w')
59        i = 0
60        while i < size:
61            sector = struct.pack('>l504xl', i / 512, i / 512)
62            file.write(sector)
63            i = i + 512
64        file.close()
65
66
67class TestSingleDrive(ImageCommitTestCase):
68    image_len = 1 * 1024 * 1024
69    test_len = 1 * 1024 * 256
70
71    def setUp(self):
72        self.create_image(backing_img, TestSingleDrive.image_len)
73        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
74        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
75        qemu_io('-c', 'write -P 0xab 0 524288', backing_img)
76        qemu_io('-c', 'write -P 0xef 524288 524288', mid_img)
77        self.vm = iotests.VM().add_drive(test_img)
78        self.vm.launch()
79
80    def tearDown(self):
81        self.vm.shutdown()
82        os.remove(test_img)
83        os.remove(mid_img)
84        os.remove(backing_img)
85
86    def test_commit(self):
87        self.assert_no_active_commit()
88        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img)
89        self.assert_qmp(result, 'return', {})
90
91        completed = False
92        while not completed:
93            for event in self.vm.get_qmp_events(wait=True):
94                if event['event'] == 'BLOCK_JOB_COMPLETED':
95                    self.assert_qmp(event, 'data/type', 'commit')
96                    self.assert_qmp(event, 'data/device', 'drive0')
97                    self.assert_qmp(event, 'data/offset', self.image_len)
98                    self.assert_qmp(event, 'data/len', self.image_len)
99                    completed = True
100
101        self.assert_no_active_commit()
102        self.vm.shutdown()
103
104        self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
105        self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
106
107    def test_device_not_found(self):
108        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
109        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
110
111    def test_top_same_base(self):
112        self.assert_no_active_commit()
113        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
114        self.assert_qmp(result, 'error/class', 'GenericError')
115        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
116
117    def test_top_invalid(self):
118        self.assert_no_active_commit()
119        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
120        self.assert_qmp(result, 'error/class', 'GenericError')
121        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
122
123    def test_base_invalid(self):
124        self.assert_no_active_commit()
125        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
126        self.assert_qmp(result, 'error/class', 'GenericError')
127        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
128
129    def test_top_is_active(self):
130        self.assert_no_active_commit()
131        result = self.vm.qmp('block-commit', device='drive0', top='%s' % test_img, base='%s' % backing_img)
132        self.assert_qmp(result, 'error/class', 'GenericError')
133        self.assert_qmp(result, 'error/desc', 'Top image as the active layer is currently unsupported')
134
135    def test_top_and_base_reversed(self):
136        self.assert_no_active_commit()
137        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
138        self.assert_qmp(result, 'error/class', 'GenericError')
139        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
140
141    def test_top_omitted(self):
142        self.assert_no_active_commit()
143        result = self.vm.qmp('block-commit', device='drive0')
144        self.assert_qmp(result, 'error/class', 'GenericError')
145        self.assert_qmp(result, 'error/desc', "Parameter 'top' is missing")
146
147class TestRelativePaths(ImageCommitTestCase):
148    image_len = 1 * 1024 * 1024
149    test_len = 1 * 1024 * 256
150
151    dir1 = "dir1"
152    dir2 = "dir2/"
153    dir3 = "dir2/dir3/"
154
155    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
156    mid_img = "../mid.img"
157    backing_img = "../dir1/backing.img"
158
159    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
160    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
161
162    def setUp(self):
163        try:
164            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
165            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
166            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
167        except OSError as exception:
168            if exception.errno != errno.EEXIST:
169                raise
170        self.create_image(self.backing_img_abs, TestRelativePaths.image_len)
171        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
172        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
173        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
174        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
175        qemu_io('-c', 'write -P 0xab 0 524288', self.backing_img_abs)
176        qemu_io('-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
177        self.vm = iotests.VM().add_drive(self.test_img)
178        self.vm.launch()
179
180    def tearDown(self):
181        self.vm.shutdown()
182        os.remove(self.test_img)
183        os.remove(self.mid_img_abs)
184        os.remove(self.backing_img_abs)
185        try:
186            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
187            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
188            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
189        except OSError as exception:
190            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
191                raise
192
193    def test_commit(self):
194        self.assert_no_active_commit()
195        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img)
196        self.assert_qmp(result, 'return', {})
197
198        completed = False
199        while not completed:
200            for event in self.vm.get_qmp_events(wait=True):
201                if event['event'] == 'BLOCK_JOB_COMPLETED':
202                    self.assert_qmp(event, 'data/type', 'commit')
203                    self.assert_qmp(event, 'data/device', 'drive0')
204                    self.assert_qmp(event, 'data/offset', self.image_len)
205                    self.assert_qmp(event, 'data/len', self.image_len)
206                    completed = True
207
208        self.assert_no_active_commit()
209        self.vm.shutdown()
210
211        self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
212        self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
213
214    def test_device_not_found(self):
215        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
216        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
217
218    def test_top_same_base(self):
219        self.assert_no_active_commit()
220        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
221        self.assert_qmp(result, 'error/class', 'GenericError')
222        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
223
224    def test_top_invalid(self):
225        self.assert_no_active_commit()
226        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
227        self.assert_qmp(result, 'error/class', 'GenericError')
228        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
229
230    def test_base_invalid(self):
231        self.assert_no_active_commit()
232        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
233        self.assert_qmp(result, 'error/class', 'GenericError')
234        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
235
236    def test_top_is_active(self):
237        self.assert_no_active_commit()
238        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.test_img, base='%s' % self.backing_img)
239        self.assert_qmp(result, 'error/class', 'GenericError')
240        self.assert_qmp(result, 'error/desc', 'Top image as the active layer is currently unsupported')
241
242    def test_top_and_base_reversed(self):
243        self.assert_no_active_commit()
244        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
245        self.assert_qmp(result, 'error/class', 'GenericError')
246        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
247
248
249class TestSetSpeed(ImageCommitTestCase):
250    image_len = 80 * 1024 * 1024 # MB
251
252    def setUp(self):
253        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
254        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
255        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
256        self.vm = iotests.VM().add_drive(test_img)
257        self.vm.launch()
258
259    def tearDown(self):
260        self.vm.shutdown()
261        os.remove(test_img)
262        os.remove(mid_img)
263        os.remove(backing_img)
264
265    def test_set_speed(self):
266        self.assert_no_active_commit()
267
268        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
269        self.assert_qmp(result, 'return', {})
270
271        # Ensure the speed we set was accepted
272        result = self.vm.qmp('query-block-jobs')
273        self.assert_qmp(result, 'return[0]/device', 'drive0')
274        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
275
276        self.cancel_and_wait()
277
278
279if __name__ == '__main__':
280    iotests.main(supported_fmts=['qcow2', 'qed'])
281