MAINTAINERS: Cover docs/igd-assign.txt in VFIO section
[qemu/ar7.git] / tests / qemu-iotests / 298
blobd535946b5fea3d601e887c67365c431346604f54
1 #!/usr/bin/env python3
3 # Test for preallocate filter
5 # Copyright (c) 2020 Virtuozzo International GmbH.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 import os
22 import iotests
24 MiB = 1024 * 1024
25 disk = os.path.join(iotests.test_dir, 'disk')
26 overlay = os.path.join(iotests.test_dir, 'overlay')
27 refdisk = os.path.join(iotests.test_dir, 'refdisk')
28 drive_opts = f'node-name=disk,driver={iotests.imgfmt},' \
29     f'file.node-name=filter,file.driver=preallocate,' \
30     f'file.file.node-name=file,file.file.filename={disk}'
33 class TestPreallocateBase(iotests.QMPTestCase):
34     def setUp(self):
35         iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
37     def tearDown(self):
38         try:
39             self.check_small()
40             check = iotests.qemu_img_check(disk)
41             self.assertFalse('leaks' in check)
42             self.assertFalse('corruptions' in check)
43             self.assertEqual(check['check-errors'], 0)
44         finally:
45             os.remove(disk)
47     def check_big(self):
48         self.assertTrue(os.path.getsize(disk) > 100 * MiB)
50     def check_small(self):
51         self.assertTrue(os.path.getsize(disk) < 10 * MiB)
54 class TestQemuImg(TestPreallocateBase):
55     def test_qemu_img(self):
56         p = iotests.QemuIoInteractive('--image-opts', drive_opts)
58         p.cmd('write 0 1M')
59         p.cmd('flush')
61         self.check_big()
63         p.close()
66 class TestPreallocateFilter(TestPreallocateBase):
67     def setUp(self):
68         super().setUp()
69         self.vm = iotests.VM().add_drive(path=None, opts=drive_opts)
70         self.vm.launch()
72     def tearDown(self):
73         self.vm.shutdown()
74         super().tearDown()
76     def test_prealloc(self):
77         self.vm.hmp_qemu_io('drive0', 'write 0 1M')
78         self.check_big()
80     def test_external_snapshot(self):
81         self.test_prealloc()
83         result = self.vm.qmp('blockdev-snapshot-sync', node_name='disk',
84                              snapshot_file=overlay,
85                              snapshot_node_name='overlay')
86         self.assert_qmp(result, 'return', {})
88         # on reopen to  r-o base preallocation should be dropped
89         self.check_small()
91         self.vm.hmp_qemu_io('drive0', 'write 1M 1M')
93         result = self.vm.qmp('block-commit', device='overlay')
94         self.assert_qmp(result, 'return', {})
95         self.complete_and_wait()
97         # commit of new megabyte should trigger preallocation
98         self.check_big()
100     def test_reopen_opts(self):
101         result = self.vm.qmp('x-blockdev-reopen', **{
102             'node-name': 'disk',
103             'driver': iotests.imgfmt,
104             'file': {
105                 'node-name': 'filter',
106                 'driver': 'preallocate',
107                 'prealloc-size': 20 * MiB,
108                 'prealloc-align': 5 * MiB,
109                 'file': {
110                     'node-name': 'file',
111                     'driver': 'file',
112                     'filename': disk
113                 }
114             }
115         })
116         self.assert_qmp(result, 'return', {})
118         self.vm.hmp_qemu_io('drive0', 'write 0 1M')
119         self.assertTrue(os.path.getsize(disk) == 25 * MiB)
122 class TestTruncate(iotests.QMPTestCase):
123     def setUp(self):
124         iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
125         iotests.qemu_img_create('-f', iotests.imgfmt, refdisk, str(10 * MiB))
127     def tearDown(self):
128         os.remove(disk)
129         os.remove(refdisk)
131     def do_test(self, prealloc_mode, new_size):
132         ret = iotests.qemu_io_silent('--image-opts', '-c', 'write 0 10M', '-c',
133                                      f'truncate -m {prealloc_mode} {new_size}',
134                                      drive_opts)
135         self.assertEqual(ret, 0)
137         ret = iotests.qemu_io_silent('-f', iotests.imgfmt, '-c', 'write 0 10M',
138                                      '-c',
139                                      f'truncate -m {prealloc_mode} {new_size}',
140                                      refdisk)
141         self.assertEqual(ret, 0)
143         stat = os.stat(disk)
144         refstat = os.stat(refdisk)
146         # Probably we'll want preallocate filter to keep align to cluster when
147         # shrink preallocation, so, ignore small differece
148         self.assertLess(abs(stat.st_size - refstat.st_size), 64 * 1024)
150         # Preallocate filter may leak some internal clusters (for example, if
151         # guest write far over EOF, skipping some clusters - they will remain
152         # fallocated, preallocate filter don't care about such leaks, it drops
153         # only trailing preallocation.
154         self.assertLess(abs(stat.st_blocks - refstat.st_blocks) * 512,
155                         1024 * 1024)
157     def test_real_shrink(self):
158         self.do_test('off', '5M')
160     def test_truncate_inside_preallocated_area__falloc(self):
161         self.do_test('falloc', '50M')
163     def test_truncate_inside_preallocated_area__metadata(self):
164         self.do_test('metadata', '50M')
166     def test_truncate_inside_preallocated_area__full(self):
167         self.do_test('full', '50M')
169     def test_truncate_inside_preallocated_area__off(self):
170         self.do_test('off', '50M')
172     def test_truncate_over_preallocated_area__falloc(self):
173         self.do_test('falloc', '150M')
175     def test_truncate_over_preallocated_area__metadata(self):
176         self.do_test('metadata', '150M')
178     def test_truncate_over_preallocated_area__full(self):
179         self.do_test('full', '150M')
181     def test_truncate_over_preallocated_area__off(self):
182         self.do_test('off', '150M')
185 if __name__ == '__main__':
186     iotests.main(supported_fmts=['qcow2'], required_fmts=['preallocate'])