4 # Test reopening a format driver's file child
6 # Copyright (C) 2022 Red Hat, Inc.
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.
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.
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/>.
24 from iotests import imgfmt, qemu_img_create, QMPTestCase
27 image_size = 1 * 1024 * 1024
28 test_img = os.path.join(iotests.test_dir, 'test.img')
31 class TestReopenFile(QMPTestCase):
32 def setUp(self) -> None:
33 res = qemu_img_create('-f', imgfmt, test_img, str(image_size))
34 assert res.returncode == 0
36 # Add format driver node ('format') on top of the file ('file'), then
37 # add another raw node ('raw') on top of 'file' so for the reopen we
38 # can just switch from 'file' to 'raw'
39 self.vm = iotests.VM()
40 self.vm.add_blockdev(self.vm.qmp_to_opts({
42 'node-name': 'format',
49 self.vm.add_blockdev(self.vm.qmp_to_opts({
56 def tearDown(self) -> None:
60 # Check if there was any qemu-io run that failed
61 if 'Pattern verification failed' in self.vm.get_log():
62 print('ERROR: Pattern verification failed:')
63 print(self.vm.get_log())
64 self.fail('qemu-io pattern verification failed')
66 def test_reopen_file(self) -> None:
67 self.vm.cmd('blockdev-reopen', options=[{
69 'node-name': 'format',
73 # Do some I/O to the image to see whether it still works
74 # (Pattern verification will be checked by tearDown())
75 result = self.vm.qmp('human-monitor-command',
76 command_line='qemu-io format "write -P 42 0 64k"')
77 self.assert_qmp(result, 'return', '')
79 result = self.vm.qmp('human-monitor-command',
80 command_line='qemu-io format "read -P 42 0 64k"')
81 self.assert_qmp(result, 'return', '')
84 if __name__ == '__main__':
85 # Must support creating images and reopen
86 iotests.main(supported_fmts=['qcow', 'qcow2', 'qed', 'raw', 'vdi', 'vhdx',
88 supported_protocols=['file'])