Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / tests / qemu-iotests / 065
blobb76701c71e8d1cb5b8708ba7065242251f7824e5
1 #!/usr/bin/env python3
2 # group: rw quick
4 # Test for additional information emitted by qemu-img info on qcow2
5 # images
7 # Copyright (C) 2013 Red Hat, Inc.
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.
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.
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/>.
23 import os
24 import re
25 import json
26 import iotests
27 from iotests import qemu_img, qemu_img_info, supports_qcow2_zstd_compression
28 import unittest
30 test_img = os.path.join(iotests.test_dir, 'test.img')
32 class TestImageInfoSpecific(iotests.QMPTestCase):
33     '''Abstract base class for ImageInfoSpecific tests'''
35     def setUp(self):
36         if self.img_options is None:
37             self.skipTest('Skipping abstract test class')
38         qemu_img('create', '-f', iotests.imgfmt, '-o', self.img_options,
39                  test_img, '128K')
41     def tearDown(self):
42         os.remove(test_img)
44 class TestQemuImgInfo(TestImageInfoSpecific):
45     '''Abstract base class for qemu-img info tests'''
47     img_options = None
48     json_compare = None
49     human_compare = None
51     def test_json(self):
52         data = qemu_img_info(test_img)['format-specific']
53         self.assertEqual(data['type'], iotests.imgfmt)
54         self.assertEqual(data['data'], self.json_compare)
56     def test_human(self):
57         data = qemu_img('info', '--output=human', test_img).stdout.split('\n')
58         data = data[(data.index('Format specific information:') + 1)
59                     :data.index("Child node '/file':")]
60         for field in data:
61             self.assertTrue(re.match('^ {4}[^ ]', field) is not None)
62         data = [line.strip() for line in data]
63         self.assertEqual(data, self.human_compare)
65 class TestQMP(TestImageInfoSpecific):
66     '''Abstract base class for qemu QMP tests'''
68     img_options = None
69     qemu_options = ''
70     TestImageInfoSpecific = TestImageInfoSpecific
72     def setUp(self):
73         self.TestImageInfoSpecific.setUp(self)
74         self.vm = iotests.VM().add_drive(test_img, self.qemu_options)
75         self.vm.launch()
77     def tearDown(self):
78         self.vm.shutdown()
79         self.TestImageInfoSpecific.tearDown(self)
81     def test_qmp(self):
82         result = self.vm.qmp('query-block')['return']
83         drive = next(drive for drive in result if drive['device'] == 'drive0')
84         data = drive['inserted']['image']['format-specific']
85         self.assertEqual(data['type'], iotests.imgfmt)
86         self.assertEqual(data['data'], self.compare)
88 class TestQCow2(TestQemuImgInfo):
89     '''Testing a qcow2 version 2 image'''
90     img_options = 'compat=0.10,compression_type=zlib'
91     json_compare = { 'compat': '0.10', 'refcount-bits': 16,
92                      'compression-type': 'zlib' }
93     human_compare = [ 'compat: 0.10', 'compression type: zlib',
94                       'refcount bits: 16' ]
96 class TestQCow3NotLazy(TestQemuImgInfo):
97     '''Testing a qcow2 version 3 image with lazy refcounts disabled'''
98     if supports_qcow2_zstd_compression():
99         compression_type = 'zstd'
100     else:
101         compression_type = 'zlib'
103     img_options = 'compat=1.1,lazy_refcounts=off'
104     img_options += f',compression_type={compression_type}'
105     json_compare = { 'compat': '1.1', 'lazy-refcounts': False,
106                      'refcount-bits': 16, 'corrupt': False,
107                      'compression-type': compression_type, 'extended-l2': False }
108     human_compare = [ 'compat: 1.1', f'compression type: {compression_type}',
109                       'lazy refcounts: false', 'refcount bits: 16',
110                       'corrupt: false', 'extended l2: false' ]
112 class TestQCow3Lazy(TestQemuImgInfo):
113     '''Testing a qcow2 version 3 image with lazy refcounts enabled'''
114     img_options = 'compat=1.1,lazy_refcounts=on,compression_type=zlib'
115     json_compare = { 'compat': '1.1', 'lazy-refcounts': True,
116                      'refcount-bits': 16, 'corrupt': False,
117                      'compression-type': 'zlib', 'extended-l2': False }
118     human_compare = [ 'compat: 1.1', 'compression type: zlib',
119                       'lazy refcounts: true', 'refcount bits: 16',
120                       'corrupt: false', 'extended l2: false' ]
122 class TestQCow3NotLazyQMP(TestQMP):
123     '''Testing a qcow2 version 3 image with lazy refcounts disabled, opening
124        with lazy refcounts enabled'''
125     img_options = 'compat=1.1,lazy_refcounts=off,compression_type=zlib'
126     qemu_options = 'lazy-refcounts=on'
127     compare = { 'compat': '1.1', 'lazy-refcounts': False,
128                 'refcount-bits': 16, 'corrupt': False,
129                 'compression-type': 'zlib', 'extended-l2': False }
132 class TestQCow3LazyQMP(TestQMP):
133     '''Testing a qcow2 version 3 image with lazy refcounts enabled, opening
134        with lazy refcounts disabled'''
135     if supports_qcow2_zstd_compression():
136         compression_type = 'zstd'
137     else:
138         compression_type = 'zlib'
140     img_options = 'compat=1.1,lazy_refcounts=on'
141     img_options += f',compression_type={compression_type}'
142     qemu_options = 'lazy-refcounts=off'
143     compare = { 'compat': '1.1', 'lazy-refcounts': True,
144                 'refcount-bits': 16, 'corrupt': False,
145                 'compression-type': compression_type, 'extended-l2': False }
147 TestImageInfoSpecific = None
148 TestQemuImgInfo = None
149 TestQMP = None
151 if __name__ == '__main__':
152     iotests.main(supported_fmts=['qcow2'],
153                  supported_protocols=['file'],
154                  unsupported_imgopts=['refcount_bits'])