3 # Test for additional information emitted by qemu-img info on qcow2
6 # Copyright (C) 2013 Red Hat, Inc.
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.
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.
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/>.
26 from iotests import qemu_img, qemu_img_pipe
29 test_img = os.path.join(iotests.test_dir, 'test.img')
31 class TestImageInfoSpecific(iotests.QMPTestCase):
32 '''Abstract base class for ImageInfoSpecific tests'''
35 if self.img_options is None:
36 self.skipTest('Skipping abstract test class')
37 qemu_img('create', '-f', iotests.imgfmt, '-o', self.img_options,
43 class TestQemuImgInfo(TestImageInfoSpecific):
44 '''Abstract base class for qemu-img info tests'''
51 data = json.loads(qemu_img_pipe('info', '--output=json', test_img))
52 data = data['format-specific']
53 self.assertEqual(data['type'], iotests.imgfmt)
54 self.assertEqual(data['data'], self.json_compare)
57 data = qemu_img_pipe('info', '--output=human', test_img).split('\n')
58 data = data[(data.index('Format specific information:') + 1)
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'''
70 TestImageInfoSpecific = TestImageInfoSpecific
73 self.TestImageInfoSpecific.setUp(self)
74 self.vm = iotests.VM().add_drive(test_img, self.qemu_options)
79 self.TestImageInfoSpecific.tearDown(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'
91 json_compare = { 'compat': '0.10', 'refcount-bits': 16,
92 'compression-type': 'zlib' }
93 human_compare = [ 'compat: 0.10', 'compression type: zlib',
96 class TestQCow3NotLazy(TestQemuImgInfo):
97 '''Testing a qcow2 version 3 image with lazy refcounts disabled'''
98 img_options = 'compat=1.1,lazy_refcounts=off'
99 json_compare = { 'compat': '1.1', 'lazy-refcounts': False,
100 'refcount-bits': 16, 'corrupt': False,
101 'compression-type': 'zlib' }
102 human_compare = [ 'compat: 1.1', 'compression type: zlib',
103 'lazy refcounts: false', 'refcount bits: 16',
106 class TestQCow3Lazy(TestQemuImgInfo):
107 '''Testing a qcow2 version 3 image with lazy refcounts enabled'''
108 img_options = 'compat=1.1,lazy_refcounts=on'
109 json_compare = { 'compat': '1.1', 'lazy-refcounts': True,
110 'refcount-bits': 16, 'corrupt': False,
111 'compression-type': 'zlib' }
112 human_compare = [ 'compat: 1.1', 'compression type: zlib',
113 'lazy refcounts: true', 'refcount bits: 16',
116 class TestQCow3NotLazyQMP(TestQMP):
117 '''Testing a qcow2 version 3 image with lazy refcounts disabled, opening
118 with lazy refcounts enabled'''
119 img_options = 'compat=1.1,lazy_refcounts=off'
120 qemu_options = 'lazy-refcounts=on'
121 compare = { 'compat': '1.1', 'lazy-refcounts': False,
122 'refcount-bits': 16, 'corrupt': False,
123 'compression-type': 'zlib' }
126 class TestQCow3LazyQMP(TestQMP):
127 '''Testing a qcow2 version 3 image with lazy refcounts enabled, opening
128 with lazy refcounts disabled'''
129 img_options = 'compat=1.1,lazy_refcounts=on'
130 qemu_options = 'lazy-refcounts=off'
131 compare = { 'compat': '1.1', 'lazy-refcounts': True,
132 'refcount-bits': 16, 'corrupt': False,
133 'compression-type': 'zlib' }
135 TestImageInfoSpecific = None
136 TestQemuImgInfo = None
139 if __name__ == '__main__':
140 iotests.main(supported_fmts=['qcow2'],
141 supported_protocols=['file'])