Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / tests / qemu-iotests / 298
blob09c9290711a840c0ba1cbbea74462ecbaf6c7a55
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         self.vm.cmd('blockdev-snapshot-sync', node_name='disk',
84                     snapshot_file=overlay,
85                     snapshot_node_name='overlay')
87         # on reopen to  r-o base preallocation should be dropped
88         self.check_small()
90         self.vm.hmp_qemu_io('drive0', 'write 1M 1M')
92         self.vm.cmd('block-commit', device='overlay')
93         self.complete_and_wait()
95         # commit of new megabyte should trigger preallocation
96         self.check_big()
98     def test_reopen_opts(self):
99         self.vm.cmd('blockdev-reopen', options=[{
100             'node-name': 'disk',
101             'driver': iotests.imgfmt,
102             'file': {
103                 'node-name': 'filter',
104                 'driver': 'preallocate',
105                 'prealloc-size': 20 * MiB,
106                 'prealloc-align': 5 * MiB,
107                 'file': {
108                     'node-name': 'file',
109                     'driver': 'file',
110                     'filename': disk
111                 }
112             }
113         }])
115         self.vm.hmp_qemu_io('drive0', 'write 0 1M')
116         self.assertTrue(os.path.getsize(disk) == 25 * MiB)
119 class TestTruncate(iotests.QMPTestCase):
120     def setUp(self):
121         iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
122         iotests.qemu_img_create('-f', iotests.imgfmt, refdisk, str(10 * MiB))
124     def tearDown(self):
125         os.remove(disk)
126         os.remove(refdisk)
128     def do_test(self, prealloc_mode, new_size):
129         iotests.qemu_io('--image-opts', '-c', 'write 0 10M', '-c',
130                         f'truncate -m {prealloc_mode} {new_size}',
131                         drive_opts)
133         iotests.qemu_io('-f', iotests.imgfmt, '-c', 'write 0 10M',
134                         '-c', f'truncate -m {prealloc_mode} {new_size}',
135                         refdisk)
137         stat = os.stat(disk)
138         refstat = os.stat(refdisk)
140         # The preallocate filter may keep cluster alignment when shrinking,
141         # so ignore small differences
142         self.assertLess(abs(stat.st_size - refstat.st_size), 64 * 1024)
144         # Preallocate filter may leak some internal clusters (for example, if
145         # guest write far over EOF, skipping some clusters - they will remain
146         # fallocated, preallocate filter don't care about such leaks, it drops
147         # only trailing preallocation.
148         self.assertLess(abs(stat.st_blocks - refstat.st_blocks) * 512,
149                         1024 * 1024)
151     def test_real_shrink(self):
152         self.do_test('off', '5M')
154     def test_truncate_inside_preallocated_area__falloc(self):
155         self.do_test('falloc', '50M')
157     def test_truncate_inside_preallocated_area__metadata(self):
158         self.do_test('metadata', '50M')
160     def test_truncate_inside_preallocated_area__full(self):
161         self.do_test('full', '50M')
163     def test_truncate_inside_preallocated_area__off(self):
164         self.do_test('off', '50M')
166     def test_truncate_over_preallocated_area__falloc(self):
167         self.do_test('falloc', '150M')
169     def test_truncate_over_preallocated_area__metadata(self):
170         self.do_test('metadata', '150M')
172     def test_truncate_over_preallocated_area__full(self):
173         self.do_test('full', '150M')
175     def test_truncate_over_preallocated_area__off(self):
176         self.do_test('off', '150M')
179 if __name__ == '__main__':
180     iotests.main(supported_fmts=['qcow2'], required_fmts=['preallocate'])