xref: /qemu/tests/qemu-iotests/040 (revision 7678b74a)
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, node_names=False):
61        self.assert_no_active_block_jobs()
62        if node_names:
63            result = self.vm.qmp('block-commit', device='drive0', top_node=top, base_node=base)
64        else:
65            result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
66        self.assert_qmp(result, 'return', {})
67        self.wait_for_complete(need_ready)
68
69    def run_default_commit_test(self):
70        self.assert_no_active_block_jobs()
71        result = self.vm.qmp('block-commit', device='drive0')
72        self.assert_qmp(result, 'return', {})
73        self.wait_for_complete()
74
75class TestSingleDrive(ImageCommitTestCase):
76    # Need some space after the copied data so that throttling is effective in
77    # tests that use it rather than just completing the job immediately
78    image_len = 2 * 1024 * 1024
79    test_len = 1 * 1024 * 256
80
81    def setUp(self):
82        iotests.create_image(backing_img, self.image_len)
83        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
84        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
85        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img)
86        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
87        self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none")
88        self.vm.add_device(iotests.get_virtio_scsi_device())
89        self.vm.add_device("scsi-hd,id=scsi0,drive=drive0")
90        self.vm.launch()
91        self.has_quit = False
92
93    def tearDown(self):
94        self.vm.shutdown(has_quit=self.has_quit)
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_commit_node(self):
105        self.run_commit_test("mid", "base", node_names=True)
106        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
107        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
108
109    def test_commit_with_filter_and_quit(self):
110        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
111        self.assert_qmp(result, 'return', {})
112
113        # Add a filter outside of the backing chain
114        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
115        self.assert_qmp(result, 'return', {})
116
117        result = self.vm.qmp('block-commit', device='drive0')
118        self.assert_qmp(result, 'return', {})
119
120        # Quit immediately, thus forcing a simultaneous cancel of the
121        # block job and a bdrv_drain_all()
122        result = self.vm.qmp('quit')
123        self.assert_qmp(result, 'return', {})
124
125        self.has_quit = True
126
127    # Same as above, but this time we add the filter after starting the job
128    def test_commit_plus_filter_and_quit(self):
129        result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg')
130        self.assert_qmp(result, 'return', {})
131
132        result = self.vm.qmp('block-commit', device='drive0')
133        self.assert_qmp(result, 'return', {})
134
135        # Add a filter outside of the backing chain
136        result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid')
137        self.assert_qmp(result, 'return', {})
138
139        # Quit immediately, thus forcing a simultaneous cancel of the
140        # block job and a bdrv_drain_all()
141        result = self.vm.qmp('quit')
142        self.assert_qmp(result, 'return', {})
143
144        self.has_quit = True
145
146    def test_device_not_found(self):
147        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
148        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
149
150    def test_top_same_base(self):
151        self.assert_no_active_block_jobs()
152        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
153        self.assert_qmp(result, 'error/class', 'GenericError')
154        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
155
156    def test_top_invalid(self):
157        self.assert_no_active_block_jobs()
158        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
159        self.assert_qmp(result, 'error/class', 'GenericError')
160        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
161
162    def test_base_invalid(self):
163        self.assert_no_active_block_jobs()
164        result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
165        self.assert_qmp(result, 'error/class', 'GenericError')
166        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
167
168    def test_top_node_invalid(self):
169        self.assert_no_active_block_jobs()
170        result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base')
171        self.assert_qmp(result, 'error/class', 'GenericError')
172        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
173
174    def test_base_node_invalid(self):
175        self.assert_no_active_block_jobs()
176        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile')
177        self.assert_qmp(result, 'error/class', 'GenericError')
178        self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
179
180    def test_top_path_and_node(self):
181        self.assert_no_active_block_jobs()
182        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img)
183        self.assert_qmp(result, 'error/class', 'GenericError')
184        self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive")
185
186    def test_base_path_and_node(self):
187        self.assert_no_active_block_jobs()
188        result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img)
189        self.assert_qmp(result, 'error/class', 'GenericError')
190        self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive")
191
192    def test_top_is_active(self):
193        self.run_commit_test(test_img, backing_img, need_ready=True)
194        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
195        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
196
197    def test_top_is_default_active(self):
198        self.run_default_commit_test()
199        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
200        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
201
202    def test_top_and_base_reversed(self):
203        self.assert_no_active_block_jobs()
204        result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
205        self.assert_qmp(result, 'error/class', 'GenericError')
206        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
207
208    def test_top_and_base_node_reversed(self):
209        self.assert_no_active_block_jobs()
210        result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top')
211        self.assert_qmp(result, 'error/class', 'GenericError')
212        self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain")
213
214    def test_top_node_in_wrong_chain(self):
215        self.assert_no_active_block_jobs()
216
217        result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null')
218        self.assert_qmp(result, 'return', {})
219
220        result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base')
221        self.assert_qmp(result, 'error/class', 'GenericError')
222        self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain")
223
224    # When the job is running on a BB that is automatically deleted on hot
225    # unplug, the job is cancelled when the device disappears
226    def test_hot_unplug(self):
227        if self.image_len == 0:
228            return
229
230        self.assert_no_active_block_jobs()
231        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
232                             base=backing_img, speed=(self.image_len // 4))
233        self.assert_qmp(result, 'return', {})
234        result = self.vm.qmp('device_del', id='scsi0')
235        self.assert_qmp(result, 'return', {})
236
237        cancelled = False
238        deleted = False
239        while not cancelled or not deleted:
240            for event in self.vm.get_qmp_events(wait=True):
241                if event['event'] == 'DEVICE_DELETED':
242                    self.assert_qmp(event, 'data/device', 'scsi0')
243                    deleted = True
244                elif event['event'] == 'BLOCK_JOB_CANCELLED':
245                    self.assert_qmp(event, 'data/device', 'drive0')
246                    cancelled = True
247                elif event['event'] == 'JOB_STATUS_CHANGE':
248                    self.assert_qmp(event, 'data/id', 'drive0')
249                else:
250                    self.fail("Unexpected event %s" % (event['event']))
251
252        self.assert_no_active_block_jobs()
253
254    # Tests that the insertion of the commit_top filter node doesn't make a
255    # difference to query-blockstat
256    def test_implicit_node(self):
257        if self.image_len == 0:
258            return
259
260        self.assert_no_active_block_jobs()
261        result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
262                             base=backing_img, speed=(self.image_len // 4))
263        self.assert_qmp(result, 'return', {})
264
265        result = self.vm.qmp('query-block')
266        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
267        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
268        self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img)
269        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2)
270        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
271        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img)
272        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img)
273
274        result = self.vm.qmp('query-blockstats')
275        self.assert_qmp(result, 'return[0]/node-name', 'top')
276        self.assert_qmp(result, 'return[0]/backing/node-name', 'mid')
277        self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base')
278
279        self.cancel_and_wait()
280        self.assert_no_active_block_jobs()
281
282class TestRelativePaths(ImageCommitTestCase):
283    image_len = 1 * 1024 * 1024
284    test_len = 1 * 1024 * 256
285
286    dir1 = "dir1"
287    dir2 = "dir2/"
288    dir3 = "dir2/dir3/"
289
290    test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
291    mid_img = "../mid.img"
292    backing_img = "../dir1/backing.img"
293
294    backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
295    mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
296
297    def setUp(self):
298        try:
299            os.mkdir(os.path.join(iotests.test_dir, self.dir1))
300            os.mkdir(os.path.join(iotests.test_dir, self.dir2))
301            os.mkdir(os.path.join(iotests.test_dir, self.dir3))
302        except OSError as exception:
303            if exception.errno != errno.EEXIST:
304                raise
305        iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
306        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
307        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
308        qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
309        qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
310        qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
311        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
312        self.vm = iotests.VM().add_drive(self.test_img)
313        self.vm.launch()
314
315    def tearDown(self):
316        self.vm.shutdown()
317        os.remove(self.test_img)
318        os.remove(self.mid_img_abs)
319        os.remove(self.backing_img_abs)
320        try:
321            os.rmdir(os.path.join(iotests.test_dir, self.dir1))
322            os.rmdir(os.path.join(iotests.test_dir, self.dir3))
323            os.rmdir(os.path.join(iotests.test_dir, self.dir2))
324        except OSError as exception:
325            if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
326                raise
327
328    def test_commit(self):
329        self.run_commit_test(self.mid_img, self.backing_img)
330        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
331        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
332
333    def test_device_not_found(self):
334        result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
335        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
336
337    def test_top_same_base(self):
338        self.assert_no_active_block_jobs()
339        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
340        self.assert_qmp(result, 'error/class', 'GenericError')
341        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
342
343    def test_top_invalid(self):
344        self.assert_no_active_block_jobs()
345        result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
346        self.assert_qmp(result, 'error/class', 'GenericError')
347        self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
348
349    def test_base_invalid(self):
350        self.assert_no_active_block_jobs()
351        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
352        self.assert_qmp(result, 'error/class', 'GenericError')
353        self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
354
355    def test_top_is_active(self):
356        self.run_commit_test(self.test_img, self.backing_img)
357        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
358        self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
359
360    def test_top_and_base_reversed(self):
361        self.assert_no_active_block_jobs()
362        result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
363        self.assert_qmp(result, 'error/class', 'GenericError')
364        self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
365
366
367class TestSetSpeed(ImageCommitTestCase):
368    image_len = 80 * 1024 * 1024 # MB
369
370    def setUp(self):
371        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
372        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
373        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
374        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
375        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
376        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
377        self.vm.launch()
378
379    def tearDown(self):
380        self.vm.shutdown()
381        os.remove(test_img)
382        os.remove(mid_img)
383        os.remove(backing_img)
384
385    def test_set_speed(self):
386        self.assert_no_active_block_jobs()
387
388        self.vm.pause_drive('drive0')
389        result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
390        self.assert_qmp(result, 'return', {})
391
392        # Ensure the speed we set was accepted
393        result = self.vm.qmp('query-block-jobs')
394        self.assert_qmp(result, 'return[0]/device', 'drive0')
395        self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
396
397        self.cancel_and_wait(resume=True)
398
399class TestActiveZeroLengthImage(TestSingleDrive):
400    image_len = 0
401
402class TestReopenOverlay(ImageCommitTestCase):
403    image_len = 1024 * 1024
404    img0 = os.path.join(iotests.test_dir, '0.img')
405    img1 = os.path.join(iotests.test_dir, '1.img')
406    img2 = os.path.join(iotests.test_dir, '2.img')
407    img3 = os.path.join(iotests.test_dir, '3.img')
408
409    def setUp(self):
410        iotests.create_image(self.img0, self.image_len)
411        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
412        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
413        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
414        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
415        self.vm = iotests.VM().add_drive(self.img3)
416        self.vm.launch()
417
418    def tearDown(self):
419        self.vm.shutdown()
420        os.remove(self.img0)
421        os.remove(self.img1)
422        os.remove(self.img2)
423        os.remove(self.img3)
424
425    # This tests what happens when the overlay image of the 'top' node
426    # needs to be reopened in read-write mode in order to update the
427    # backing image string.
428    def test_reopen_overlay(self):
429        self.run_commit_test(self.img1, self.img0)
430
431if __name__ == '__main__':
432    iotests.main(supported_fmts=['qcow2', 'qed'],
433                 supported_protocols=['file'])
434