1#!/usr/bin/env python3 2# group: rw quick 3# 4# Test reopening a format driver's file child 5# 6# Copyright (C) 2022 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 22import os 23import iotests 24from iotests import imgfmt, qemu_img_create, QMPTestCase 25 26 27image_size = 1 * 1024 * 1024 28test_img = os.path.join(iotests.test_dir, 'test.img') 29 30 31class TestReopenFile(QMPTestCase): 32 def setUp(self) -> None: 33 res = qemu_img_create('-f', imgfmt, test_img, str(image_size)) 34 assert res.returncode == 0 35 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({ 41 'driver': imgfmt, 42 'node-name': 'format', 43 'file': { 44 'driver': 'file', 45 'node-name': 'file', 46 'filename': test_img 47 } 48 })) 49 self.vm.add_blockdev(self.vm.qmp_to_opts({ 50 'driver': 'raw', 51 'node-name': 'raw', 52 'file': 'file' 53 })) 54 self.vm.launch() 55 56 def tearDown(self) -> None: 57 self.vm.shutdown() 58 os.remove(test_img) 59 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') 65 66 def test_reopen_file(self) -> None: 67 self.vm.cmd('blockdev-reopen', options=[{ 68 'driver': imgfmt, 69 'node-name': 'format', 70 'file': 'raw' 71 }]) 72 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', '') 78 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', '') 82 83 84if __name__ == '__main__': 85 # Must support creating images and reopen 86 iotests.main(supported_fmts=['qcow', 'qcow2', 'qed', 'raw', 'vdi', 'vhdx', 87 'vmdk', 'vpc'], 88 supported_protocols=['file']) 89