1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test what happens when a stream job does an unaligned prefetch read
5# which requires padding while having a NULL qiov.
6#
7# Copyright (C) Proxmox Server Solutions GmbH
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23import os
24import iotests
25from iotests import imgfmt, qemu_img_create, qemu_io, QMPTestCase
26
27image_size = 1 * 1024 * 1024
28cluster_size = 64 * 1024
29base = os.path.join(iotests.test_dir, 'base.img')
30top = os.path.join(iotests.test_dir, 'top.img')
31
32class TestStreamUnalignedPrefetch(QMPTestCase):
33    def setUp(self) -> None:
34        """
35        Create two images:
36        - base image {base} with {cluster_size // 2} bytes allocated
37        - top image {top} without any data allocated and coarser
38          cluster size
39
40        Attach a compress filter for the top image, because that
41        requires that the request alignment is the top image's cluster
42        size.
43        """
44        qemu_img_create('-f', imgfmt,
45                        '-o', 'cluster_size={}'.format(cluster_size // 2),
46                        base, str(image_size))
47        qemu_io('-c', f'write 0 {cluster_size // 2}', base)
48        qemu_img_create('-f', imgfmt,
49                        '-o', 'cluster_size={}'.format(cluster_size),
50                        top, str(image_size))
51
52        self.vm = iotests.VM()
53        self.vm.add_blockdev(self.vm.qmp_to_opts({
54            'driver': imgfmt,
55            'node-name': 'base',
56            'file': {
57                'driver': 'file',
58                'filename': base
59            }
60        }))
61        self.vm.add_blockdev(self.vm.qmp_to_opts({
62            'driver': 'compress',
63            'node-name': 'compress-top',
64            'file': {
65                'driver': imgfmt,
66                'node-name': 'top',
67                'file': {
68                    'driver': 'file',
69                    'filename': top
70                },
71                'backing': 'base'
72            }
73        }))
74        self.vm.launch()
75
76    def tearDown(self) -> None:
77        self.vm.shutdown()
78        os.remove(top)
79        os.remove(base)
80
81    def test_stream_unaligned_prefetch(self) -> None:
82        self.vm.cmd('block-stream', job_id='stream', device='compress-top')
83
84
85if __name__ == '__main__':
86    iotests.main(supported_fmts=['qcow2'], supported_protocols=['file'])
87