xref: /openbmc/qemu/tests/qemu-iotests/040 (revision 5692399f)
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(event, 'data/type', 'commit')
45                    self.assert_qmp(event, 'data/device', 'drive0')
46                    self.assert_qmp(event, 'data/offset', self.image_len)
47                    self.assert_qmp(event, 'data/len', self.image_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.assert_qmp(event, 'data/len', self.image_len)
56                    self.vm.qmp('block-job-complete', device='drive0')
57
58        self.assert_no_active_block_jobs()
59        self.vm.shutdown()
60
61    def run_commit_test(self, top, base, need_ready=False):
62        self.assert_no_active_block_jobs()
63        result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
64        self.assert_qmp(result, 'return', {})
65        self.wait_for_complete(need_ready)
66
67    def run_default_commit_test(self):
68        self.assert_no_active_block_jobs()
69        result = self.vm.qmp('block-commit', device='drive0')
70        self.assert_qmp(result, 'return', {})
71        self.wait_for_complete()
72
73class TestSingleDrive(ImageCommitTestCase):
74    image_len = 1 * 1024 * 1024
75    test_len = 1 * 1024 * 256
76
77    def setUp(self):
78        iotests.create_image(backing_img, self.image_len)
79        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
80        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
81        qemu_io('-c', 'write -P 0xab 0 524288', backing_img)
82        qemu_io('-c', 'write -P 0xef 524288 524288', mid_img)
83        self.vm = iotests.VM().add_drive(test_img)
84        self.vm.launch()
85
86    def tearDown(self):
87        self.vm.shutdown()
88        os.remove(test_img)
89        os.remove(mid_img)
90        os.remove(backing_img)
91
92    def test_commit(self):
93        self.run_commit_test(mid_img, backing_img)
94        self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
95        self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
96
97    def test_device_not_found(self):
98        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
99        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
100
101    def test_top_same_base(self):
102        self.assert_no_active_block_jobs()
103        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
104        self.assert_qmp(result, 'error/class', 'GenericError')
105        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
106
107    def test_top_invalid(self):
108        self.assert_no_active_block_jobs()
109        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
110        self.assert_qmp(result, 'error/class', 'GenericError')
111        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
112
113    def test_base_invalid(self):
114        self.assert_no_active_block_jobs()
115        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
116        self.assert_qmp(result, 'error/class', 'GenericError')
117        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
118
119    def test_top_is_active(self):
120        self.run_commit_test(test_img, backing_img, need_ready=True)
121        self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
122        self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
123
124    def test_top_is_default_active(self):
125        self.run_default_commit_test()
126        self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
127        self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
128
129    def test_top_and_base_reversed(self):
130        self.assert_no_active_block_jobs()
131        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
132        self.assert_qmp(result, 'error/class', 'GenericError')
133        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
134
135
136class TestRelativePaths(ImageCommitTestCase):
137    image_len = 1 * 1024 * 1024
138    test_len = 1 * 1024 * 256
139
140    dir1 = "dir1"
141    dir2 = "dir2/"
142    dir3 = "dir2/dir3/"
143
144    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
145    mid_img = "../mid.img"
146    backing_img = "../dir1/backing.img"
147
148    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
149    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
150
151    def setUp(self):
152        try:
153            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
154            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
155            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
156        except OSError as exception:
157            if exception.errno != errno.EEXIST:
158                raise
159        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
160        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
161        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
162        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
163        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
164        qemu_io('-c', 'write -P 0xab 0 524288', self.backing_img_abs)
165        qemu_io('-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
166        self.vm = iotests.VM().add_drive(self.test_img)
167        self.vm.launch()
168
169    def tearDown(self):
170        self.vm.shutdown()
171        os.remove(self.test_img)
172        os.remove(self.mid_img_abs)
173        os.remove(self.backing_img_abs)
174        try:
175            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
176            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
177            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
178        except OSError as exception:
179            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
180                raise
181
182    def test_commit(self):
183        self.run_commit_test(self.mid_img, self.backing_img)
184        self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
185        self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
186
187    def test_device_not_found(self):
188        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
189        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
190
191    def test_top_same_base(self):
192        self.assert_no_active_block_jobs()
193        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
194        self.assert_qmp(result, 'error/class', 'GenericError')
195        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
196
197    def test_top_invalid(self):
198        self.assert_no_active_block_jobs()
199        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
200        self.assert_qmp(result, 'error/class', 'GenericError')
201        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
202
203    def test_base_invalid(self):
204        self.assert_no_active_block_jobs()
205        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
206        self.assert_qmp(result, 'error/class', 'GenericError')
207        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
208
209    def test_top_is_active(self):
210        self.run_commit_test(self.test_img, self.backing_img)
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_top_and_base_reversed(self):
215        self.assert_no_active_block_jobs()
216        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
217        self.assert_qmp(result, 'error/class', 'GenericError')
218        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
219
220
221class TestSetSpeed(ImageCommitTestCase):
222    image_len = 80 * 1024 * 1024 # MB
223
224    def setUp(self):
225        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
226        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
227        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
228        qemu_io('-c', 'write -P 0x1 0 512', test_img)
229        qemu_io('-c', 'write -P 0xef 524288 524288', mid_img)
230        self.vm = iotests.VM().add_drive(test_img)
231        self.vm.launch()
232
233    def tearDown(self):
234        self.vm.shutdown()
235        os.remove(test_img)
236        os.remove(mid_img)
237        os.remove(backing_img)
238
239    def test_set_speed(self):
240        self.assert_no_active_block_jobs()
241
242        self.vm.pause_drive('drive0')
243        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
244        self.assert_qmp(result, 'return', {})
245
246        # Ensure the speed we set was accepted
247        result = self.vm.qmp('query-block-jobs')
248        self.assert_qmp(result, 'return[0]/device', 'drive0')
249        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
250
251        self.cancel_and_wait(resume=True)
252
253class TestActiveZeroLengthImage(TestSingleDrive):
254    image_len = 0
255
256if __name__ == '__main__':
257    iotests.main(supported_fmts=['qcow2', 'qed'])
258