Philippe Mathieu-Daudé | 903cb1b | 2020-01-30 17:32:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Vladimir Sementsov-Ogievskiy | 9dd003a | 2021-01-16 16:44:19 +0300 | [diff] [blame] | 2 | # group: rw auto |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 3 | # |
| 4 | # Tests for image block commit. |
| 5 | # |
| 6 | # Copyright (C) 2012 IBM, Corp. |
| 7 | # Copyright (C) 2012 Red Hat, Inc. |
| 8 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License as published by |
| 11 | # the Free Software Foundation; either version 2 of the License, or |
| 12 | # (at your option) any later version. |
| 13 | # |
| 14 | # This program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | # GNU General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU General Public License |
| 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | # |
| 22 | # Test for live block commit |
| 23 | # Derived from Image Streaming Test 030 |
| 24 | |
| 25 | import time |
| 26 | import os |
| 27 | import iotests |
| 28 | from iotests import qemu_img, qemu_io |
| 29 | import struct |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 30 | import errno |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 31 | |
| 32 | backing_img = os.path.join(iotests.test_dir, 'backing.img') |
| 33 | mid_img = os.path.join(iotests.test_dir, 'mid.img') |
| 34 | test_img = os.path.join(iotests.test_dir, 'test.img') |
| 35 | |
| 36 | class ImageCommitTestCase(iotests.QMPTestCase): |
| 37 | '''Abstract base class for image commit test cases''' |
| 38 | |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 39 | def wait_for_complete(self, need_ready=False): |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 40 | completed = False |
Fam Zheng | 8b9a30c | 2014-06-24 20:26:37 +0800 | [diff] [blame] | 41 | ready = False |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 42 | while not completed: |
| 43 | for event in self.vm.get_qmp_events(wait=True): |
| 44 | if event['event'] == 'BLOCK_JOB_COMPLETED': |
Alberto Garcia | bcdce5a | 2015-10-28 15:43:50 +0200 | [diff] [blame] | 45 | self.assert_qmp_absent(event, 'data/error') |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 46 | self.assert_qmp(event, 'data/type', 'commit') |
| 47 | self.assert_qmp(event, 'data/device', 'drive0') |
Max Reitz | 1d3ba15 | 2014-10-24 15:57:35 +0200 | [diff] [blame] | 48 | self.assert_qmp(event, 'data/offset', event['data']['len']) |
Fam Zheng | 8b9a30c | 2014-06-24 20:26:37 +0800 | [diff] [blame] | 49 | if need_ready: |
| 50 | self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event") |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 51 | completed = True |
| 52 | elif event['event'] == 'BLOCK_JOB_READY': |
Fam Zheng | 8b9a30c | 2014-06-24 20:26:37 +0800 | [diff] [blame] | 53 | ready = True |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 54 | self.assert_qmp(event, 'data/type', 'commit') |
| 55 | self.assert_qmp(event, 'data/device', 'drive0') |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 56 | self.vm.qmp('block-job-complete', device='drive0') |
| 57 | |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 58 | self.assert_no_active_block_jobs() |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 59 | self.vm.shutdown() |
| 60 | |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 61 | def run_commit_test(self, top, base, need_ready=False, node_names=False): |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 62 | self.assert_no_active_block_jobs() |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 63 | if node_names: |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 64 | self.vm.cmd('block-commit', device='drive0', top_node=top, base_node=base) |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 65 | else: |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 66 | self.vm.cmd('block-commit', device='drive0', top=top, base=base) |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 67 | self.wait_for_complete(need_ready) |
| 68 | |
| 69 | def run_default_commit_test(self): |
| 70 | self.assert_no_active_block_jobs() |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 71 | self.vm.cmd('block-commit', device='drive0') |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 72 | self.wait_for_complete() |
| 73 | |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 74 | class TestSingleDrive(ImageCommitTestCase): |
Kevin Wolf | c3971b8 | 2017-06-02 23:10:10 +0200 | [diff] [blame] | 75 | # Need some space after the copied data so that throttling is effective in |
| 76 | # tests that use it rather than just completing the job immediately |
| 77 | image_len = 2 * 1024 * 1024 |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 78 | test_len = 1 * 1024 * 256 |
| 79 | |
| 80 | def setUp(self): |
Fam Zheng | 8b9a30c | 2014-06-24 20:26:37 +0800 | [diff] [blame] | 81 | iotests.create_image(backing_img, self.image_len) |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 82 | qemu_img('create', '-f', iotests.imgfmt, |
| 83 | '-o', 'backing_file=%s' % backing_img, '-F', 'raw', mid_img) |
| 84 | qemu_img('create', '-f', iotests.imgfmt, |
| 85 | '-o', 'backing_file=%s' % mid_img, |
| 86 | '-F', iotests.imgfmt, test_img) |
John Snow | aaa0c0e | 2022-04-18 17:14:56 -0400 | [diff] [blame] | 87 | if self.image_len: |
| 88 | qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img) |
| 89 | qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', |
| 90 | mid_img) |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 91 | self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none") |
Laurent Vivier | 22329f0 | 2021-03-23 16:53:05 +0000 | [diff] [blame] | 92 | self.vm.add_device('virtio-scsi') |
Kevin Wolf | c3971b8 | 2017-06-02 23:10:10 +0200 | [diff] [blame] | 93 | self.vm.add_device("scsi-hd,id=scsi0,drive=drive0") |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 94 | self.vm.launch() |
| 95 | |
| 96 | def tearDown(self): |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 97 | self.vm.shutdown() |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 98 | os.remove(test_img) |
| 99 | os.remove(mid_img) |
| 100 | os.remove(backing_img) |
| 101 | |
| 102 | def test_commit(self): |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 103 | self.run_commit_test(mid_img, backing_img) |
John Snow | aaa0c0e | 2022-04-18 17:14:56 -0400 | [diff] [blame] | 104 | if not self.image_len: |
| 105 | return |
John Snow | e9039c0 | 2022-04-18 17:14:55 -0400 | [diff] [blame] | 106 | qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img) |
| 107 | qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img) |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 108 | |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 109 | def test_commit_node(self): |
| 110 | self.run_commit_test("mid", "base", node_names=True) |
John Snow | aaa0c0e | 2022-04-18 17:14:56 -0400 | [diff] [blame] | 111 | if not self.image_len: |
| 112 | return |
John Snow | e9039c0 | 2022-04-18 17:14:55 -0400 | [diff] [blame] | 113 | qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img) |
| 114 | qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img) |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 115 | |
Thomas Huth | 9442beb | 2020-01-14 15:02:03 +0100 | [diff] [blame] | 116 | @iotests.skip_if_unsupported(['throttle']) |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 117 | def test_commit_with_filter_and_quit(self): |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 118 | self.vm.cmd('object-add', qom_type='throttle-group', id='tg') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 119 | |
| 120 | # Add a filter outside of the backing chain |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 121 | self.vm.cmd('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 122 | |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 123 | self.vm.cmd('block-commit', device='drive0') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 124 | |
| 125 | # Quit immediately, thus forcing a simultaneous cancel of the |
| 126 | # block job and a bdrv_drain_all() |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 127 | self.vm.cmd('quit') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 128 | |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 129 | # Same as above, but this time we add the filter after starting the job |
Thomas Huth | 9442beb | 2020-01-14 15:02:03 +0100 | [diff] [blame] | 130 | @iotests.skip_if_unsupported(['throttle']) |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 131 | def test_commit_plus_filter_and_quit(self): |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 132 | self.vm.cmd('object-add', qom_type='throttle-group', id='tg') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 133 | |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 134 | self.vm.cmd('block-commit', device='drive0') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 135 | |
| 136 | # Add a filter outside of the backing chain |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 137 | self.vm.cmd('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 138 | |
| 139 | # Quit immediately, thus forcing a simultaneous cancel of the |
| 140 | # block job and a bdrv_drain_all() |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 141 | self.vm.cmd('quit') |
Max Reitz | 8647207 | 2019-07-19 11:26:18 +0200 | [diff] [blame] | 142 | |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 143 | def test_device_not_found(self): |
| 144 | result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img) |
| 145 | self.assert_qmp(result, 'error/class', 'DeviceNotFound') |
| 146 | |
| 147 | def test_top_same_base(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 148 | self.assert_no_active_block_jobs() |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 149 | result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img) |
| 150 | self.assert_qmp(result, 'error/class', 'GenericError') |
Markus Armbruster | b0d7be2 | 2020-11-13 09:26:19 +0100 | [diff] [blame] | 151 | self.assert_qmp(result, 'error/desc', "Can't find '%s' in the backing chain" % backing_img) |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 152 | |
| 153 | def test_top_invalid(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 154 | self.assert_no_active_block_jobs() |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 155 | result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img) |
| 156 | self.assert_qmp(result, 'error/class', 'GenericError') |
| 157 | self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') |
| 158 | |
| 159 | def test_base_invalid(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 160 | self.assert_no_active_block_jobs() |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 161 | result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile') |
| 162 | self.assert_qmp(result, 'error/class', 'GenericError') |
Markus Armbruster | b0d7be2 | 2020-11-13 09:26:19 +0100 | [diff] [blame] | 163 | self.assert_qmp(result, 'error/desc', "Can't find 'badfile' in the backing chain") |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 164 | |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 165 | def test_top_node_invalid(self): |
| 166 | self.assert_no_active_block_jobs() |
| 167 | result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base') |
| 168 | self.assert_qmp(result, 'error/class', 'GenericError') |
Connor Kuehl | 785ec4b | 2021-03-05 09:19:28 -0600 | [diff] [blame] | 169 | self.assert_qmp(result, 'error/desc', "Cannot find device='' nor node-name='badfile'") |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 170 | |
| 171 | def test_base_node_invalid(self): |
| 172 | self.assert_no_active_block_jobs() |
| 173 | result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile') |
| 174 | self.assert_qmp(result, 'error/class', 'GenericError') |
Connor Kuehl | 785ec4b | 2021-03-05 09:19:28 -0600 | [diff] [blame] | 175 | self.assert_qmp(result, 'error/desc', "Cannot find device='' nor node-name='badfile'") |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 176 | |
| 177 | def test_top_path_and_node(self): |
| 178 | self.assert_no_active_block_jobs() |
| 179 | result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img) |
| 180 | self.assert_qmp(result, 'error/class', 'GenericError') |
| 181 | self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive") |
| 182 | |
| 183 | def test_base_path_and_node(self): |
| 184 | self.assert_no_active_block_jobs() |
| 185 | result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img) |
| 186 | self.assert_qmp(result, 'error/class', 'GenericError') |
| 187 | self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive") |
| 188 | |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 189 | def test_top_is_active(self): |
Fam Zheng | 8b9a30c | 2014-06-24 20:26:37 +0800 | [diff] [blame] | 190 | self.run_commit_test(test_img, backing_img, need_ready=True) |
John Snow | aaa0c0e | 2022-04-18 17:14:56 -0400 | [diff] [blame] | 191 | if not self.image_len: |
| 192 | return |
John Snow | e9039c0 | 2022-04-18 17:14:55 -0400 | [diff] [blame] | 193 | qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img) |
| 194 | qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img) |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 195 | |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 196 | def test_top_is_default_active(self): |
| 197 | self.run_default_commit_test() |
John Snow | aaa0c0e | 2022-04-18 17:14:56 -0400 | [diff] [blame] | 198 | if not self.image_len: |
| 199 | return |
John Snow | e9039c0 | 2022-04-18 17:14:55 -0400 | [diff] [blame] | 200 | qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img) |
| 201 | qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img) |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 202 | |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 203 | def test_top_and_base_reversed(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 204 | self.assert_no_active_block_jobs() |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 205 | result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img) |
| 206 | self.assert_qmp(result, 'error/class', 'GenericError') |
Markus Armbruster | b0d7be2 | 2020-11-13 09:26:19 +0100 | [diff] [blame] | 207 | self.assert_qmp(result, 'error/desc', "Can't find '%s' in the backing chain" % mid_img) |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 208 | |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 209 | def test_top_and_base_node_reversed(self): |
| 210 | self.assert_no_active_block_jobs() |
| 211 | result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top') |
| 212 | self.assert_qmp(result, 'error/class', 'GenericError') |
| 213 | self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain") |
| 214 | |
| 215 | def test_top_node_in_wrong_chain(self): |
| 216 | self.assert_no_active_block_jobs() |
| 217 | |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 218 | self.vm.cmd('blockdev-add', driver='null-co', node_name='null') |
Kevin Wolf | d57177a | 2018-08-10 18:20:45 +0200 | [diff] [blame] | 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 | |
Kevin Wolf | c3971b8 | 2017-06-02 23:10:10 +0200 | [diff] [blame] | 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() |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 231 | self.vm.cmd('block-commit', device='drive0', top=mid_img, |
| 232 | base=backing_img, speed=(self.image_len // 4)) |
| 233 | self.vm.cmd('device_del', id='scsi0') |
Kevin Wolf | c3971b8 | 2017-06-02 23:10:10 +0200 | [diff] [blame] | 234 | |
| 235 | cancelled = False |
| 236 | deleted = False |
| 237 | while not cancelled or not deleted: |
| 238 | for event in self.vm.get_qmp_events(wait=True): |
| 239 | if event['event'] == 'DEVICE_DELETED': |
| 240 | self.assert_qmp(event, 'data/device', 'scsi0') |
| 241 | deleted = True |
| 242 | elif event['event'] == 'BLOCK_JOB_CANCELLED': |
| 243 | self.assert_qmp(event, 'data/device', 'drive0') |
| 244 | cancelled = True |
Kevin Wolf | 1dac83f | 2018-04-30 19:09:46 +0200 | [diff] [blame] | 245 | elif event['event'] == 'JOB_STATUS_CHANGE': |
| 246 | self.assert_qmp(event, 'data/id', 'drive0') |
Kevin Wolf | c3971b8 | 2017-06-02 23:10:10 +0200 | [diff] [blame] | 247 | else: |
| 248 | self.fail("Unexpected event %s" % (event['event'])) |
| 249 | |
| 250 | self.assert_no_active_block_jobs() |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 251 | |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 252 | # Tests that the insertion of the commit_top filter node doesn't make a |
| 253 | # difference to query-blockstat |
| 254 | def test_implicit_node(self): |
| 255 | if self.image_len == 0: |
| 256 | return |
| 257 | |
| 258 | self.assert_no_active_block_jobs() |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 259 | self.vm.cmd('block-commit', device='drive0', top=mid_img, |
| 260 | base=backing_img, speed=(self.image_len // 4)) |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 261 | |
| 262 | result = self.vm.qmp('query-block') |
| 263 | self.assert_qmp(result, 'return[0]/inserted/file', test_img) |
| 264 | self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt) |
| 265 | self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img) |
| 266 | self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2) |
| 267 | self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img) |
| 268 | self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img) |
| 269 | self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img) |
| 270 | |
| 271 | result = self.vm.qmp('query-blockstats') |
| 272 | self.assert_qmp(result, 'return[0]/node-name', 'top') |
| 273 | self.assert_qmp(result, 'return[0]/backing/node-name', 'mid') |
| 274 | self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base') |
| 275 | |
| 276 | self.cancel_and_wait() |
| 277 | self.assert_no_active_block_jobs() |
| 278 | |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 279 | class TestRelativePaths(ImageCommitTestCase): |
| 280 | image_len = 1 * 1024 * 1024 |
| 281 | test_len = 1 * 1024 * 256 |
| 282 | |
| 283 | dir1 = "dir1" |
| 284 | dir2 = "dir2/" |
| 285 | dir3 = "dir2/dir3/" |
| 286 | |
| 287 | test_img = os.path.join(iotests.test_dir, dir3, 'test.img') |
| 288 | mid_img = "../mid.img" |
| 289 | backing_img = "../dir1/backing.img" |
| 290 | |
| 291 | backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img') |
| 292 | mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img') |
| 293 | |
| 294 | def setUp(self): |
| 295 | try: |
| 296 | os.mkdir(os.path.join(iotests.test_dir, self.dir1)) |
| 297 | os.mkdir(os.path.join(iotests.test_dir, self.dir2)) |
| 298 | os.mkdir(os.path.join(iotests.test_dir, self.dir3)) |
| 299 | except OSError as exception: |
| 300 | if exception.errno != errno.EEXIST: |
| 301 | raise |
Fam Zheng | 915365a | 2013-10-30 17:23:54 +0800 | [diff] [blame] | 302 | iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len) |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 303 | qemu_img('create', '-f', iotests.imgfmt, |
| 304 | '-o', 'backing_file=%s' % self.backing_img_abs, |
| 305 | '-F', 'raw', self.mid_img_abs) |
| 306 | qemu_img('create', '-f', iotests.imgfmt, |
| 307 | '-o', 'backing_file=%s' % self.mid_img_abs, |
| 308 | '-F', iotests.imgfmt, self.test_img) |
| 309 | qemu_img('rebase', '-u', '-b', self.backing_img, |
| 310 | '-F', 'raw', self.mid_img_abs) |
| 311 | qemu_img('rebase', '-u', '-b', self.mid_img, |
| 312 | '-F', iotests.imgfmt, self.test_img) |
Kevin Wolf | 90c9b16 | 2014-11-20 16:27:08 +0100 | [diff] [blame] | 313 | qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs) |
| 314 | qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs) |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 315 | self.vm = iotests.VM().add_drive(self.test_img) |
| 316 | self.vm.launch() |
| 317 | |
| 318 | def tearDown(self): |
| 319 | self.vm.shutdown() |
| 320 | os.remove(self.test_img) |
| 321 | os.remove(self.mid_img_abs) |
| 322 | os.remove(self.backing_img_abs) |
| 323 | try: |
| 324 | os.rmdir(os.path.join(iotests.test_dir, self.dir1)) |
| 325 | os.rmdir(os.path.join(iotests.test_dir, self.dir3)) |
| 326 | os.rmdir(os.path.join(iotests.test_dir, self.dir2)) |
| 327 | except OSError as exception: |
| 328 | if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY: |
| 329 | raise |
| 330 | |
| 331 | def test_commit(self): |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 332 | self.run_commit_test(self.mid_img, self.backing_img) |
John Snow | e9039c0 | 2022-04-18 17:14:55 -0400 | [diff] [blame] | 333 | qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs) |
| 334 | qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs) |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 335 | |
| 336 | def test_device_not_found(self): |
| 337 | result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img) |
| 338 | self.assert_qmp(result, 'error/class', 'DeviceNotFound') |
| 339 | |
| 340 | def test_top_same_base(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 341 | self.assert_no_active_block_jobs() |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 342 | result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img) |
| 343 | self.assert_qmp(result, 'error/class', 'GenericError') |
Markus Armbruster | b0d7be2 | 2020-11-13 09:26:19 +0100 | [diff] [blame] | 344 | self.assert_qmp(result, 'error/desc', "Can't find '%s' in the backing chain" % self.mid_img) |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 345 | |
| 346 | def test_top_invalid(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 347 | self.assert_no_active_block_jobs() |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 348 | result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img) |
| 349 | self.assert_qmp(result, 'error/class', 'GenericError') |
| 350 | self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') |
| 351 | |
| 352 | def test_base_invalid(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 353 | self.assert_no_active_block_jobs() |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 354 | result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile') |
| 355 | self.assert_qmp(result, 'error/class', 'GenericError') |
Markus Armbruster | b0d7be2 | 2020-11-13 09:26:19 +0100 | [diff] [blame] | 356 | self.assert_qmp(result, 'error/desc', "Can't find 'badfile' in the backing chain") |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 357 | |
| 358 | def test_top_is_active(self): |
Fam Zheng | 4de4347 | 2013-12-16 14:45:32 +0800 | [diff] [blame] | 359 | self.run_commit_test(self.test_img, self.backing_img) |
John Snow | e9039c0 | 2022-04-18 17:14:55 -0400 | [diff] [blame] | 360 | qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs) |
| 361 | qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs) |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 362 | |
| 363 | def test_top_and_base_reversed(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 364 | self.assert_no_active_block_jobs() |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 365 | result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img) |
| 366 | self.assert_qmp(result, 'error/class', 'GenericError') |
Markus Armbruster | b0d7be2 | 2020-11-13 09:26:19 +0100 | [diff] [blame] | 367 | self.assert_qmp(result, 'error/desc', "Can't find '%s' in the backing chain" % self.mid_img) |
Jeff Cody | 6bf0d1f | 2012-10-16 15:49:12 -0400 | [diff] [blame] | 368 | |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 369 | |
| 370 | class TestSetSpeed(ImageCommitTestCase): |
| 371 | image_len = 80 * 1024 * 1024 # MB |
| 372 | |
| 373 | def setUp(self): |
| 374 | qemu_img('create', backing_img, str(TestSetSpeed.image_len)) |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 375 | qemu_img('create', '-f', iotests.imgfmt, |
| 376 | '-o', 'backing_file=%s' % backing_img, '-F', 'raw', mid_img) |
| 377 | qemu_img('create', '-f', iotests.imgfmt, |
| 378 | '-o', 'backing_file=%s' % mid_img, |
| 379 | '-F', iotests.imgfmt, test_img) |
Kevin Wolf | 90c9b16 | 2014-11-20 16:27:08 +0100 | [diff] [blame] | 380 | qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img) |
| 381 | qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img) |
Max Reitz | 51c493c | 2017-11-09 21:30:22 +0100 | [diff] [blame] | 382 | self.vm = iotests.VM().add_drive('blkdebug::' + test_img) |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 383 | self.vm.launch() |
| 384 | |
| 385 | def tearDown(self): |
| 386 | self.vm.shutdown() |
| 387 | os.remove(test_img) |
| 388 | os.remove(mid_img) |
| 389 | os.remove(backing_img) |
| 390 | |
| 391 | def test_set_speed(self): |
Fam Zheng | fb0a078 | 2014-01-29 18:47:23 +0800 | [diff] [blame] | 392 | self.assert_no_active_block_jobs() |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 393 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 394 | self.vm.pause_drive('drive0') |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 395 | self.vm.cmd('block-commit', device='drive0', top=mid_img, speed=1024 * 1024) |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 396 | |
| 397 | # Ensure the speed we set was accepted |
| 398 | result = self.vm.qmp('query-block-jobs') |
| 399 | self.assert_qmp(result, 'return[0]/device', 'drive0') |
| 400 | self.assert_qmp(result, 'return[0]/speed', 1024 * 1024) |
| 401 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 402 | self.cancel_and_wait(resume=True) |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 403 | |
Fam Zheng | 8b9a30c | 2014-06-24 20:26:37 +0800 | [diff] [blame] | 404 | class TestActiveZeroLengthImage(TestSingleDrive): |
| 405 | image_len = 0 |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 406 | |
Alberto Garcia | bcdce5a | 2015-10-28 15:43:50 +0200 | [diff] [blame] | 407 | class TestReopenOverlay(ImageCommitTestCase): |
| 408 | image_len = 1024 * 1024 |
| 409 | img0 = os.path.join(iotests.test_dir, '0.img') |
| 410 | img1 = os.path.join(iotests.test_dir, '1.img') |
| 411 | img2 = os.path.join(iotests.test_dir, '2.img') |
| 412 | img3 = os.path.join(iotests.test_dir, '3.img') |
| 413 | |
| 414 | def setUp(self): |
| 415 | iotests.create_image(self.img0, self.image_len) |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 416 | qemu_img('create', '-f', iotests.imgfmt, |
| 417 | '-o', 'backing_file=%s' % self.img0, '-F', 'raw', self.img1) |
| 418 | qemu_img('create', '-f', iotests.imgfmt, |
| 419 | '-o', 'backing_file=%s' % self.img1, |
| 420 | '-F', iotests.imgfmt, self.img2) |
| 421 | qemu_img('create', '-f', iotests.imgfmt, |
| 422 | '-o', 'backing_file=%s' % self.img2, |
| 423 | '-F', iotests.imgfmt, self.img3) |
Alberto Garcia | bcdce5a | 2015-10-28 15:43:50 +0200 | [diff] [blame] | 424 | qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1) |
| 425 | self.vm = iotests.VM().add_drive(self.img3) |
| 426 | self.vm.launch() |
| 427 | |
| 428 | def tearDown(self): |
| 429 | self.vm.shutdown() |
| 430 | os.remove(self.img0) |
| 431 | os.remove(self.img1) |
| 432 | os.remove(self.img2) |
| 433 | os.remove(self.img3) |
| 434 | |
| 435 | # This tests what happens when the overlay image of the 'top' node |
| 436 | # needs to be reopened in read-write mode in order to update the |
| 437 | # backing image string. |
| 438 | def test_reopen_overlay(self): |
| 439 | self.run_commit_test(self.img1, self.img0) |
| 440 | |
Kevin Wolf | d439848 | 2020-02-14 21:08:12 +0100 | [diff] [blame] | 441 | class TestErrorHandling(iotests.QMPTestCase): |
| 442 | image_len = 2 * 1024 * 1024 |
| 443 | |
| 444 | def setUp(self): |
| 445 | iotests.create_image(backing_img, self.image_len) |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 446 | qemu_img('create', '-f', iotests.imgfmt, |
| 447 | '-o', 'backing_file=%s' % backing_img, |
| 448 | '-F', 'raw', mid_img) |
| 449 | qemu_img('create', '-f', iotests.imgfmt, |
| 450 | '-o', 'backing_file=%s' % mid_img, |
| 451 | '-F', iotests.imgfmt, test_img) |
Kevin Wolf | d439848 | 2020-02-14 21:08:12 +0100 | [diff] [blame] | 452 | |
| 453 | qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x11 0 512k', mid_img) |
| 454 | qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x22 0 512k', test_img) |
| 455 | |
| 456 | self.vm = iotests.VM() |
| 457 | self.vm.launch() |
| 458 | |
| 459 | self.blkdebug_file = iotests.file_path("blkdebug.conf") |
| 460 | |
| 461 | def tearDown(self): |
| 462 | self.vm.shutdown() |
| 463 | os.remove(test_img) |
| 464 | os.remove(mid_img) |
| 465 | os.remove(backing_img) |
| 466 | |
| 467 | def blockdev_add(self, **kwargs): |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 468 | self.vm.cmd('blockdev-add', **kwargs) |
Kevin Wolf | d439848 | 2020-02-14 21:08:12 +0100 | [diff] [blame] | 469 | |
| 470 | def add_block_nodes(self, base_debug=None, mid_debug=None, top_debug=None): |
| 471 | self.blockdev_add(node_name='base-file', driver='file', |
| 472 | filename=backing_img) |
| 473 | self.blockdev_add(node_name='mid-file', driver='file', |
| 474 | filename=mid_img) |
| 475 | self.blockdev_add(node_name='top-file', driver='file', |
| 476 | filename=test_img) |
| 477 | |
| 478 | if base_debug: |
| 479 | self.blockdev_add(node_name='base-dbg', driver='blkdebug', |
| 480 | image='base-file', inject_error=base_debug) |
| 481 | if mid_debug: |
| 482 | self.blockdev_add(node_name='mid-dbg', driver='blkdebug', |
| 483 | image='mid-file', inject_error=mid_debug) |
| 484 | if top_debug: |
| 485 | self.blockdev_add(node_name='top-dbg', driver='blkdebug', |
| 486 | image='top-file', inject_error=top_debug) |
| 487 | |
| 488 | self.blockdev_add(node_name='base-fmt', driver='raw', |
| 489 | file=('base-dbg' if base_debug else 'base-file')) |
| 490 | self.blockdev_add(node_name='mid-fmt', driver=iotests.imgfmt, |
| 491 | file=('mid-dbg' if mid_debug else 'mid-file'), |
| 492 | backing='base-fmt') |
| 493 | self.blockdev_add(node_name='top-fmt', driver=iotests.imgfmt, |
| 494 | file=('top-dbg' if top_debug else 'top-file'), |
| 495 | backing='mid-fmt') |
| 496 | |
| 497 | def run_job(self, expected_events, error_pauses_job=False): |
| 498 | match_device = {'data': {'device': 'job0'}} |
| 499 | events = [ |
| 500 | ('BLOCK_JOB_COMPLETED', match_device), |
| 501 | ('BLOCK_JOB_CANCELLED', match_device), |
| 502 | ('BLOCK_JOB_ERROR', match_device), |
| 503 | ('BLOCK_JOB_READY', match_device), |
| 504 | ] |
| 505 | |
| 506 | completed = False |
| 507 | log = [] |
| 508 | while not completed: |
| 509 | ev = self.vm.events_wait(events, timeout=5.0) |
| 510 | if ev['event'] == 'BLOCK_JOB_COMPLETED': |
| 511 | completed = True |
| 512 | elif ev['event'] == 'BLOCK_JOB_ERROR': |
| 513 | if error_pauses_job: |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 514 | self.vm.cmd('block-job-resume', device='job0') |
Kevin Wolf | d439848 | 2020-02-14 21:08:12 +0100 | [diff] [blame] | 515 | elif ev['event'] == 'BLOCK_JOB_READY': |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 516 | self.vm.cmd('block-job-complete', device='job0') |
Kevin Wolf | d439848 | 2020-02-14 21:08:12 +0100 | [diff] [blame] | 517 | else: |
| 518 | self.fail("Unexpected event: %s" % ev) |
| 519 | log.append(iotests.filter_qmp_event(ev)) |
| 520 | |
| 521 | self.maxDiff = None |
| 522 | self.assertEqual(expected_events, log) |
| 523 | |
| 524 | def event_error(self, op, action): |
| 525 | return { |
| 526 | 'event': 'BLOCK_JOB_ERROR', |
| 527 | 'data': {'action': action, 'device': 'job0', 'operation': op}, |
| 528 | 'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'} |
| 529 | } |
| 530 | |
| 531 | def event_ready(self): |
| 532 | return { |
| 533 | 'event': 'BLOCK_JOB_READY', |
| 534 | 'data': {'device': 'job0', |
| 535 | 'len': 524288, |
| 536 | 'offset': 524288, |
| 537 | 'speed': 0, |
| 538 | 'type': 'commit'}, |
| 539 | 'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'}, |
| 540 | } |
| 541 | |
| 542 | def event_completed(self, errmsg=None, active=True): |
| 543 | max_len = 524288 if active else self.image_len |
| 544 | data = { |
| 545 | 'device': 'job0', |
| 546 | 'len': max_len, |
| 547 | 'offset': 0 if errmsg else max_len, |
| 548 | 'speed': 0, |
| 549 | 'type': 'commit' |
| 550 | } |
| 551 | if errmsg: |
| 552 | data['error'] = errmsg |
| 553 | |
| 554 | return { |
| 555 | 'event': 'BLOCK_JOB_COMPLETED', |
| 556 | 'data': data, |
| 557 | 'timestamp': {'microseconds': 'USECS', 'seconds': 'SECS'}, |
| 558 | } |
| 559 | |
| 560 | def blkdebug_event(self, event, is_raw=False): |
| 561 | if event: |
| 562 | return [{ |
| 563 | 'event': event, |
| 564 | 'sector': 512 if is_raw else 1024, |
| 565 | 'once': True, |
| 566 | }] |
| 567 | return None |
| 568 | |
| 569 | def prepare_and_start_job(self, on_error, active=True, |
| 570 | top_event=None, mid_event=None, base_event=None): |
| 571 | |
| 572 | top_debug = self.blkdebug_event(top_event) |
| 573 | mid_debug = self.blkdebug_event(mid_event) |
| 574 | base_debug = self.blkdebug_event(base_event, True) |
| 575 | |
| 576 | self.add_block_nodes(top_debug=top_debug, mid_debug=mid_debug, |
| 577 | base_debug=base_debug) |
| 578 | |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 579 | self.vm.cmd('block-commit', job_id='job0', device='top-fmt', |
| 580 | top_node='top-fmt' if active else 'mid-fmt', |
| 581 | base_node='mid-fmt' if active else 'base-fmt', |
| 582 | on_error=on_error) |
Kevin Wolf | d439848 | 2020-02-14 21:08:12 +0100 | [diff] [blame] | 583 | |
| 584 | def testActiveReadErrorReport(self): |
| 585 | self.prepare_and_start_job('report', top_event='read_aio') |
| 586 | self.run_job([ |
| 587 | self.event_error('read', 'report'), |
| 588 | self.event_completed('Input/output error') |
| 589 | ]) |
| 590 | |
| 591 | self.vm.shutdown() |
| 592 | self.assertFalse(iotests.compare_images(test_img, mid_img), |
| 593 | 'target image matches source after error') |
| 594 | |
| 595 | def testActiveReadErrorStop(self): |
| 596 | self.prepare_and_start_job('stop', top_event='read_aio') |
| 597 | self.run_job([ |
| 598 | self.event_error('read', 'stop'), |
| 599 | self.event_ready(), |
| 600 | self.event_completed() |
| 601 | ], error_pauses_job=True) |
| 602 | |
| 603 | self.vm.shutdown() |
| 604 | self.assertTrue(iotests.compare_images(test_img, mid_img), |
| 605 | 'target image does not match source after commit') |
| 606 | |
| 607 | def testActiveReadErrorIgnore(self): |
| 608 | self.prepare_and_start_job('ignore', top_event='read_aio') |
| 609 | self.run_job([ |
| 610 | self.event_error('read', 'ignore'), |
| 611 | self.event_ready(), |
| 612 | self.event_completed() |
| 613 | ]) |
| 614 | |
| 615 | # For commit, 'ignore' actually means retry, so this will succeed |
| 616 | self.vm.shutdown() |
| 617 | self.assertTrue(iotests.compare_images(test_img, mid_img), |
| 618 | 'target image does not match source after commit') |
| 619 | |
| 620 | def testActiveWriteErrorReport(self): |
| 621 | self.prepare_and_start_job('report', mid_event='write_aio') |
| 622 | self.run_job([ |
| 623 | self.event_error('write', 'report'), |
| 624 | self.event_completed('Input/output error') |
| 625 | ]) |
| 626 | |
| 627 | self.vm.shutdown() |
| 628 | self.assertFalse(iotests.compare_images(test_img, mid_img), |
| 629 | 'target image matches source after error') |
| 630 | |
| 631 | def testActiveWriteErrorStop(self): |
| 632 | self.prepare_and_start_job('stop', mid_event='write_aio') |
| 633 | self.run_job([ |
| 634 | self.event_error('write', 'stop'), |
| 635 | self.event_ready(), |
| 636 | self.event_completed() |
| 637 | ], error_pauses_job=True) |
| 638 | |
| 639 | self.vm.shutdown() |
| 640 | self.assertTrue(iotests.compare_images(test_img, mid_img), |
| 641 | 'target image does not match source after commit') |
| 642 | |
| 643 | def testActiveWriteErrorIgnore(self): |
| 644 | self.prepare_and_start_job('ignore', mid_event='write_aio') |
| 645 | self.run_job([ |
| 646 | self.event_error('write', 'ignore'), |
| 647 | self.event_ready(), |
| 648 | self.event_completed() |
| 649 | ]) |
| 650 | |
| 651 | # For commit, 'ignore' actually means retry, so this will succeed |
| 652 | self.vm.shutdown() |
| 653 | self.assertTrue(iotests.compare_images(test_img, mid_img), |
| 654 | 'target image does not match source after commit') |
| 655 | |
| 656 | def testIntermediateReadErrorReport(self): |
| 657 | self.prepare_and_start_job('report', active=False, mid_event='read_aio') |
| 658 | self.run_job([ |
| 659 | self.event_error('read', 'report'), |
| 660 | self.event_completed('Input/output error', active=False) |
| 661 | ]) |
| 662 | |
| 663 | self.vm.shutdown() |
| 664 | self.assertFalse(iotests.compare_images(mid_img, backing_img, fmt2='raw'), |
| 665 | 'target image matches source after error') |
| 666 | |
| 667 | def testIntermediateReadErrorStop(self): |
| 668 | self.prepare_and_start_job('stop', active=False, mid_event='read_aio') |
| 669 | self.run_job([ |
| 670 | self.event_error('read', 'stop'), |
| 671 | self.event_completed(active=False) |
| 672 | ], error_pauses_job=True) |
| 673 | |
| 674 | self.vm.shutdown() |
| 675 | self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'), |
| 676 | 'target image does not match source after commit') |
| 677 | |
| 678 | def testIntermediateReadErrorIgnore(self): |
| 679 | self.prepare_and_start_job('ignore', active=False, mid_event='read_aio') |
| 680 | self.run_job([ |
| 681 | self.event_error('read', 'ignore'), |
| 682 | self.event_completed(active=False) |
| 683 | ]) |
| 684 | |
| 685 | # For commit, 'ignore' actually means retry, so this will succeed |
| 686 | self.vm.shutdown() |
| 687 | self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'), |
| 688 | 'target image does not match source after commit') |
| 689 | |
| 690 | def testIntermediateWriteErrorReport(self): |
| 691 | self.prepare_and_start_job('report', active=False, base_event='write_aio') |
| 692 | self.run_job([ |
| 693 | self.event_error('write', 'report'), |
| 694 | self.event_completed('Input/output error', active=False) |
| 695 | ]) |
| 696 | |
| 697 | self.vm.shutdown() |
| 698 | self.assertFalse(iotests.compare_images(mid_img, backing_img, fmt2='raw'), |
| 699 | 'target image matches source after error') |
| 700 | |
| 701 | def testIntermediateWriteErrorStop(self): |
| 702 | self.prepare_and_start_job('stop', active=False, base_event='write_aio') |
| 703 | self.run_job([ |
| 704 | self.event_error('write', 'stop'), |
| 705 | self.event_completed(active=False) |
| 706 | ], error_pauses_job=True) |
| 707 | |
| 708 | self.vm.shutdown() |
| 709 | self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'), |
| 710 | 'target image does not match source after commit') |
| 711 | |
| 712 | def testIntermediateWriteErrorIgnore(self): |
| 713 | self.prepare_and_start_job('ignore', active=False, base_event='write_aio') |
| 714 | self.run_job([ |
| 715 | self.event_error('write', 'ignore'), |
| 716 | self.event_completed(active=False) |
| 717 | ]) |
| 718 | |
| 719 | # For commit, 'ignore' actually means retry, so this will succeed |
| 720 | self.vm.shutdown() |
| 721 | self.assertTrue(iotests.compare_images(mid_img, backing_img, fmt2='raw'), |
| 722 | 'target image does not match source after commit') |
| 723 | |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 724 | class TestCommitWithFilters(iotests.QMPTestCase): |
| 725 | img0 = os.path.join(iotests.test_dir, '0.img') |
| 726 | img1 = os.path.join(iotests.test_dir, '1.img') |
| 727 | img2 = os.path.join(iotests.test_dir, '2.img') |
| 728 | img3 = os.path.join(iotests.test_dir, '3.img') |
| 729 | |
| 730 | def do_test_io(self, read_or_write): |
| 731 | for index, pattern_file in enumerate(self.pattern_files): |
John Snow | e9039c0 | 2022-04-18 17:14:55 -0400 | [diff] [blame] | 732 | qemu_io('-f', iotests.imgfmt, |
| 733 | '-c', |
| 734 | f'{read_or_write} -P {index + 1} {index}M 1M', |
| 735 | pattern_file) |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 736 | |
Thomas Huth | 024354e | 2022-02-23 13:31:27 +0100 | [diff] [blame] | 737 | @iotests.skip_if_unsupported(['throttle']) |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 738 | def setUp(self): |
| 739 | qemu_img('create', '-f', iotests.imgfmt, self.img0, '64M') |
| 740 | qemu_img('create', '-f', iotests.imgfmt, self.img1, '64M') |
| 741 | qemu_img('create', '-f', iotests.imgfmt, self.img2, '64M') |
| 742 | qemu_img('create', '-f', iotests.imgfmt, self.img3, '64M') |
| 743 | |
| 744 | # Distributions of the patterns in the files; this is checked |
| 745 | # by tearDown() and should be changed by the test cases as is |
| 746 | # necessary |
| 747 | self.pattern_files = [self.img0, self.img1, self.img2, self.img3] |
| 748 | |
| 749 | self.do_test_io('write') |
| 750 | |
| 751 | self.vm = iotests.VM().add_device('virtio-scsi,id=vio-scsi') |
| 752 | self.vm.launch() |
| 753 | |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 754 | self.vm.cmd('object-add', qom_type='throttle-group', id='tg') |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 755 | |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 756 | self.vm.cmd('blockdev-add', { |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 757 | 'node-name': 'top-filter', |
| 758 | 'driver': 'throttle', |
| 759 | 'throttle-group': 'tg', |
| 760 | 'file': { |
| 761 | 'node-name': 'cow-3', |
| 762 | 'driver': iotests.imgfmt, |
| 763 | 'file': { |
| 764 | 'driver': 'file', |
| 765 | 'filename': self.img3 |
| 766 | }, |
| 767 | 'backing': { |
| 768 | 'node-name': 'cow-2', |
| 769 | 'driver': iotests.imgfmt, |
| 770 | 'file': { |
| 771 | 'driver': 'file', |
| 772 | 'filename': self.img2 |
| 773 | }, |
| 774 | 'backing': { |
| 775 | 'node-name': 'cow-1', |
| 776 | 'driver': iotests.imgfmt, |
| 777 | 'file': { |
| 778 | 'driver': 'file', |
| 779 | 'filename': self.img1 |
| 780 | }, |
| 781 | 'backing': { |
| 782 | 'node-name': 'bottom-filter', |
| 783 | 'driver': 'throttle', |
| 784 | 'throttle-group': 'tg', |
| 785 | 'file': { |
| 786 | 'node-name': 'cow-0', |
| 787 | 'driver': iotests.imgfmt, |
| 788 | 'file': { |
| 789 | 'driver': 'file', |
| 790 | 'filename': self.img0 |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | }) |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 798 | |
| 799 | def tearDown(self): |
| 800 | self.vm.shutdown() |
| 801 | self.do_test_io('read') |
| 802 | |
| 803 | os.remove(self.img3) |
| 804 | os.remove(self.img2) |
| 805 | os.remove(self.img1) |
| 806 | os.remove(self.img0) |
| 807 | |
| 808 | # Filters make for funny filenames, so we cannot just use |
| 809 | # self.imgX to get them |
| 810 | def get_filename(self, node): |
| 811 | return self.vm.node_info(node)['image']['filename'] |
| 812 | |
| 813 | def test_filterless_commit(self): |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 814 | self.vm.cmd('block-commit', |
| 815 | job_id='commit', |
| 816 | device='top-filter', |
| 817 | top_node='cow-2', |
| 818 | base_node='cow-1', |
| 819 | backing_file=self.img1) |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 820 | self.wait_until_completed(drive='commit') |
| 821 | |
| 822 | self.assertIsNotNone(self.vm.node_info('cow-3')) |
| 823 | self.assertIsNone(self.vm.node_info('cow-2')) |
| 824 | self.assertIsNotNone(self.vm.node_info('cow-1')) |
| 825 | |
Michael Tokarev | 96420a3 | 2023-07-14 14:33:27 +0300 | [diff] [blame] | 826 | # 2 has been committed into 1 |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 827 | self.pattern_files[2] = self.img1 |
| 828 | |
| 829 | def test_commit_through_filter(self): |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 830 | self.vm.cmd('block-commit', |
| 831 | job_id='commit', |
| 832 | device='top-filter', |
| 833 | top_node='cow-1', |
| 834 | base_node='cow-0', |
| 835 | backing_file=self.img0) |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 836 | self.wait_until_completed(drive='commit') |
| 837 | |
| 838 | self.assertIsNotNone(self.vm.node_info('cow-2')) |
| 839 | self.assertIsNone(self.vm.node_info('cow-1')) |
| 840 | self.assertIsNone(self.vm.node_info('bottom-filter')) |
| 841 | self.assertIsNotNone(self.vm.node_info('cow-0')) |
| 842 | |
Michael Tokarev | 96420a3 | 2023-07-14 14:33:27 +0300 | [diff] [blame] | 843 | # 1 has been committed into 0 |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 844 | self.pattern_files[1] = self.img0 |
| 845 | |
| 846 | def test_filtered_active_commit_with_filter(self): |
| 847 | # Add a device, so the commit job finds a parent it can change |
| 848 | # to point to the base node (so we can test that top-filter is |
| 849 | # dropped from the graph) |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 850 | self.vm.cmd('device_add', id='drv0', driver='scsi-hd', |
| 851 | bus='vio-scsi.0', drive='top-filter') |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 852 | |
| 853 | # Try to release our reference to top-filter; that should not |
| 854 | # work because drv0 uses it |
| 855 | result = self.vm.qmp('blockdev-del', node_name='top-filter') |
| 856 | self.assert_qmp(result, 'error/class', 'GenericError') |
| 857 | self.assert_qmp(result, 'error/desc', 'Node top-filter is in use') |
| 858 | |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 859 | self.vm.cmd('block-commit', |
| 860 | job_id='commit', |
| 861 | device='top-filter', |
| 862 | base_node='cow-2') |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 863 | self.complete_and_wait(drive='commit') |
| 864 | |
| 865 | # Try to release our reference to top-filter again |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 866 | self.vm.cmd('blockdev-del', node_name='top-filter') |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 867 | |
| 868 | self.assertIsNone(self.vm.node_info('top-filter')) |
| 869 | self.assertIsNone(self.vm.node_info('cow-3')) |
| 870 | self.assertIsNotNone(self.vm.node_info('cow-2')) |
| 871 | |
| 872 | # Check that drv0 is now connected to cow-2 |
| 873 | blockdevs = self.vm.qmp('query-block')['return'] |
| 874 | drv0 = next(dev for dev in blockdevs if dev['qdev'] == 'drv0') |
| 875 | self.assertEqual(drv0['inserted']['node-name'], 'cow-2') |
| 876 | |
Michael Tokarev | 96420a3 | 2023-07-14 14:33:27 +0300 | [diff] [blame] | 877 | # 3 has been committed into 2 |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 878 | self.pattern_files[3] = self.img2 |
| 879 | |
| 880 | def test_filtered_active_commit_without_filter(self): |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 881 | self.vm.cmd('block-commit', |
| 882 | job_id='commit', |
| 883 | device='top-filter', |
| 884 | top_node='cow-3', |
| 885 | base_node='cow-2') |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 886 | self.complete_and_wait(drive='commit') |
| 887 | |
| 888 | self.assertIsNotNone(self.vm.node_info('top-filter')) |
| 889 | self.assertIsNone(self.vm.node_info('cow-3')) |
| 890 | self.assertIsNotNone(self.vm.node_info('cow-2')) |
| 891 | |
Michael Tokarev | 96420a3 | 2023-07-14 14:33:27 +0300 | [diff] [blame] | 892 | # 3 has been committed into 2 |
Max Reitz | ffbba7f | 2018-08-01 20:43:46 +0200 | [diff] [blame] | 893 | self.pattern_files[3] = self.img2 |
| 894 | |
Max Reitz | 374eedd | 2018-08-07 00:12:22 +0200 | [diff] [blame] | 895 | class TestCommitWithOverriddenBacking(iotests.QMPTestCase): |
| 896 | img_base_a = os.path.join(iotests.test_dir, 'base_a.img') |
| 897 | img_base_b = os.path.join(iotests.test_dir, 'base_b.img') |
| 898 | img_top = os.path.join(iotests.test_dir, 'top.img') |
| 899 | |
| 900 | def setUp(self): |
| 901 | qemu_img('create', '-f', iotests.imgfmt, self.img_base_a, '1M') |
| 902 | qemu_img('create', '-f', iotests.imgfmt, self.img_base_b, '1M') |
Eric Blake | 497a30d | 2021-05-03 14:36:00 -0700 | [diff] [blame] | 903 | qemu_img('create', '-f', iotests.imgfmt, '-b', self.img_base_a, |
| 904 | '-F', iotests.imgfmt, self.img_top) |
Max Reitz | 374eedd | 2018-08-07 00:12:22 +0200 | [diff] [blame] | 905 | |
| 906 | self.vm = iotests.VM() |
| 907 | self.vm.launch() |
| 908 | |
| 909 | # Use base_b instead of base_a as the backing of top |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 910 | self.vm.cmd('blockdev-add', { |
| 911 | 'node-name': 'top', |
| 912 | 'driver': iotests.imgfmt, |
| 913 | 'file': { |
| 914 | 'driver': 'file', |
| 915 | 'filename': self.img_top |
| 916 | }, |
| 917 | 'backing': { |
| 918 | 'node-name': 'base', |
| 919 | 'driver': iotests.imgfmt, |
| 920 | 'file': { |
| 921 | 'driver': 'file', |
| 922 | 'filename': self.img_base_b |
| 923 | } |
| 924 | } |
| 925 | }) |
Max Reitz | 374eedd | 2018-08-07 00:12:22 +0200 | [diff] [blame] | 926 | |
| 927 | def tearDown(self): |
| 928 | self.vm.shutdown() |
| 929 | os.remove(self.img_top) |
| 930 | os.remove(self.img_base_a) |
| 931 | os.remove(self.img_base_b) |
| 932 | |
| 933 | def test_commit_to_a(self): |
| 934 | # Try committing to base_a (which should fail, as top's |
| 935 | # backing image is base_b instead) |
| 936 | result = self.vm.qmp('block-commit', |
| 937 | job_id='commit', |
| 938 | device='top', |
| 939 | base=self.img_base_a) |
| 940 | self.assert_qmp(result, 'error/class', 'GenericError') |
| 941 | |
| 942 | def test_commit_to_b(self): |
| 943 | # Try committing to base_b (which should work, since that is |
| 944 | # actually top's backing image) |
Vladimir Sementsov-Ogievskiy | b6aed19 | 2023-10-06 18:41:25 +0300 | [diff] [blame] | 945 | self.vm.cmd('block-commit', |
| 946 | job_id='commit', |
| 947 | device='top', |
| 948 | base=self.img_base_b) |
Max Reitz | 374eedd | 2018-08-07 00:12:22 +0200 | [diff] [blame] | 949 | |
| 950 | self.vm.event_wait('BLOCK_JOB_READY') |
| 951 | self.vm.qmp('block-job-complete', device='commit') |
| 952 | self.vm.event_wait('BLOCK_JOB_COMPLETED') |
| 953 | |
Jeff Cody | 747051c | 2012-09-27 13:29:17 -0400 | [diff] [blame] | 954 | if __name__ == '__main__': |
Max Reitz | 103cbc7 | 2019-09-02 21:33:18 +0200 | [diff] [blame] | 955 | iotests.main(supported_fmts=['qcow2', 'qed'], |
| 956 | supported_protocols=['file']) |