xref: /openbmc/qemu/tests/qemu-iotests/040 (revision 77d361b1)
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    # Need some space after the copied data so that throttling is effective in
74    # tests that use it rather than just completing the job immediately
75    image_len = 2 * 1024 * 1024
76    test_len = 1 * 1024 * 256
77
78    def setUp(self):
79        iotests.create_image(backing_img, self.image_len)
80        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
81        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
82        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img)
83        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
84        self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none")
85        if iotests.qemu_default_machine == 's390-ccw-virtio':
86            self.vm.add_device("virtio-scsi-ccw")
87        else:
88            self.vm.add_device("virtio-scsi-pci")
89
90        self.vm.add_device("scsi-hd,id=scsi0,drive=drive0")
91        self.vm.launch()
92
93    def tearDown(self):
94        self.vm.shutdown()
95        os.remove(test_img)
96        os.remove(mid_img)
97        os.remove(backing_img)
98
99    def test_commit(self):
100        self.run_commit_test(mid_img, backing_img)
101        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
102        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
103
104    def test_device_not_found(self):
105        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
106        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
107
108    def test_top_same_base(self):
109        self.assert_no_active_block_jobs()
110        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
111        self.assert_qmp(result, 'error/class', 'GenericError')
112        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
113
114    def test_top_invalid(self):
115        self.assert_no_active_block_jobs()
116        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
117        self.assert_qmp(result, 'error/class', 'GenericError')
118        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
119
120    def test_base_invalid(self):
121        self.assert_no_active_block_jobs()
122        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
123        self.assert_qmp(result, 'error/class', 'GenericError')
124        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
125
126    def test_top_is_active(self):
127        self.run_commit_test(test_img, backing_img, need_ready=True)
128        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
129        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
130
131    def test_top_is_default_active(self):
132        self.run_default_commit_test()
133        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
134        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
135
136    def test_top_and_base_reversed(self):
137        self.assert_no_active_block_jobs()
138        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
139        self.assert_qmp(result, 'error/class', 'GenericError')
140        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
141
142    # When the job is running on a BB that is automatically deleted on hot
143    # unplug, the job is cancelled when the device disappears
144    def test_hot_unplug(self):
145        if self.image_len == 0:
146            return
147
148        self.assert_no_active_block_jobs()
149        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
150                             base=backing_img, speed=(self.image_len / 4))
151        self.assert_qmp(result, 'return', {})
152        result = self.vm.qmp('device_del', id='scsi0')
153        self.assert_qmp(result, 'return', {})
154
155        cancelled = False
156        deleted = False
157        while not cancelled or not deleted:
158            for event in self.vm.get_qmp_events(wait=True):
159                if event['event'] == 'DEVICE_DELETED':
160                    self.assert_qmp(event, 'data/device', 'scsi0')
161                    deleted = True
162                elif event['event'] == 'BLOCK_JOB_CANCELLED':
163                    self.assert_qmp(event, 'data/device', 'drive0')
164                    cancelled = True
165                elif event['event'] == 'JOB_STATUS_CHANGE':
166                    self.assert_qmp(event, 'data/id', 'drive0')
167                else:
168                    self.fail("Unexpected event %s" % (event['event']))
169
170        self.assert_no_active_block_jobs()
171
172    # Tests that the insertion of the commit_top filter node doesn't make a
173    # difference to query-blockstat
174    def test_implicit_node(self):
175        if self.image_len == 0:
176            return
177
178        self.assert_no_active_block_jobs()
179        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
180                             base=backing_img, speed=(self.image_len / 4))
181        self.assert_qmp(result, 'return', {})
182
183        result = self.vm.qmp('query-block')
184        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
185        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
186        self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img)
187        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2)
188        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
189        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img)
190        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img)
191
192        result = self.vm.qmp('query-blockstats')
193        self.assert_qmp(result, 'return[0]/node-name', 'top')
194        self.assert_qmp(result, 'return[0]/backing/node-name', 'mid')
195        self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base')
196
197        self.cancel_and_wait()
198        self.assert_no_active_block_jobs()
199
200class TestRelativePaths(ImageCommitTestCase):
201    image_len = 1 * 1024 * 1024
202    test_len = 1 * 1024 * 256
203
204    dir1 = "dir1"
205    dir2 = "dir2/"
206    dir3 = "dir2/dir3/"
207
208    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
209    mid_img = "../mid.img"
210    backing_img = "../dir1/backing.img"
211
212    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
213    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
214
215    def setUp(self):
216        try:
217            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
218            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
219            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
220        except OSError as exception:
221            if exception.errno != errno.EEXIST:
222                raise
223        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
224        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
225        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
226        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
227        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
228        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
229        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
230        self.vm = iotests.VM().add_drive(self.test_img)
231        self.vm.launch()
232
233    def tearDown(self):
234        self.vm.shutdown()
235        os.remove(self.test_img)
236        os.remove(self.mid_img_abs)
237        os.remove(self.backing_img_abs)
238        try:
239            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
240            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
241            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
242        except OSError as exception:
243            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
244                raise
245
246    def test_commit(self):
247        self.run_commit_test(self.mid_img, self.backing_img)
248        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
249        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
250
251    def test_device_not_found(self):
252        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
253        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
254
255    def test_top_same_base(self):
256        self.assert_no_active_block_jobs()
257        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
258        self.assert_qmp(result, 'error/class', 'GenericError')
259        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
260
261    def test_top_invalid(self):
262        self.assert_no_active_block_jobs()
263        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
264        self.assert_qmp(result, 'error/class', 'GenericError')
265        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
266
267    def test_base_invalid(self):
268        self.assert_no_active_block_jobs()
269        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
270        self.assert_qmp(result, 'error/class', 'GenericError')
271        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
272
273    def test_top_is_active(self):
274        self.run_commit_test(self.test_img, self.backing_img)
275        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
276        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
277
278    def test_top_and_base_reversed(self):
279        self.assert_no_active_block_jobs()
280        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
281        self.assert_qmp(result, 'error/class', 'GenericError')
282        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
283
284
285class TestSetSpeed(ImageCommitTestCase):
286    image_len = 80 * 1024 * 1024 # MB
287
288    def setUp(self):
289        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
290        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
291        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
292        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
293        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
294        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
295        self.vm.launch()
296
297    def tearDown(self):
298        self.vm.shutdown()
299        os.remove(test_img)
300        os.remove(mid_img)
301        os.remove(backing_img)
302
303    def test_set_speed(self):
304        self.assert_no_active_block_jobs()
305
306        self.vm.pause_drive('drive0')
307        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
308        self.assert_qmp(result, 'return', {})
309
310        # Ensure the speed we set was accepted
311        result = self.vm.qmp('query-block-jobs')
312        self.assert_qmp(result, 'return[0]/device', 'drive0')
313        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
314
315        self.cancel_and_wait(resume=True)
316
317class TestActiveZeroLengthImage(TestSingleDrive):
318    image_len = 0
319
320class TestReopenOverlay(ImageCommitTestCase):
321    image_len = 1024 * 1024
322    img0 = os.path.join(iotests.test_dir, '0.img')
323    img1 = os.path.join(iotests.test_dir, '1.img')
324    img2 = os.path.join(iotests.test_dir, '2.img')
325    img3 = os.path.join(iotests.test_dir, '3.img')
326
327    def setUp(self):
328        iotests.create_image(self.img0, self.image_len)
329        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
330        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
331        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
332        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
333        self.vm = iotests.VM().add_drive(self.img3)
334        self.vm.launch()
335
336    def tearDown(self):
337        self.vm.shutdown()
338        os.remove(self.img0)
339        os.remove(self.img1)
340        os.remove(self.img2)
341        os.remove(self.img3)
342
343    # This tests what happens when the overlay image of the 'top' node
344    # needs to be reopened in read-write mode in order to update the
345    # backing image string.
346    def test_reopen_overlay(self):
347        self.run_commit_test(self.img1, self.img0)
348
349if __name__ == '__main__':
350    iotests.main(supported_fmts=['qcow2', 'qed'])
351