qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

reopen-file (3138B)


      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 
     22 import os
     23 import iotests
     24 from iotests import imgfmt, qemu_img_create, QMPTestCase
     25 
     26 
     27 image_size = 1 * 1024 * 1024
     28 test_img = os.path.join(iotests.test_dir, 'test.img')
     29 
     30 
     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
     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         result = self.vm.qmp('blockdev-reopen', options=[{
     68             'driver': imgfmt,
     69             'node-name': 'format',
     70             'file': 'raw'
     71         }])
     72         self.assert_qmp(result, 'return', {})
     73 
     74         # Do some I/O to the image to see whether it still works
     75         # (Pattern verification will be checked by tearDown())
     76         result = self.vm.qmp('human-monitor-command',
     77                              command_line='qemu-io format "write -P 42 0 64k"')
     78         self.assert_qmp(result, 'return', '')
     79 
     80         result = self.vm.qmp('human-monitor-command',
     81                              command_line='qemu-io format "read -P 42 0 64k"')
     82         self.assert_qmp(result, 'return', '')
     83 
     84 
     85 if __name__ == '__main__':
     86     # Must support creating images and reopen
     87     iotests.main(supported_fmts=['qcow', 'qcow2', 'qed', 'raw', 'vdi', 'vhdx',
     88                                  'vmdk', 'vpc'],
     89                  supported_protocols=['file'])