xref: /qemu/tests/qemu-iotests/065 (revision b30b8077243ea5dc93a540eedfecee3c74b19fa2)
1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick
33677e6f6SMax Reitz#
43677e6f6SMax Reitz# Test for additional information emitted by qemu-img info on qcow2
53677e6f6SMax Reitz# images
63677e6f6SMax Reitz#
73677e6f6SMax Reitz# Copyright (C) 2013 Red Hat, Inc.
83677e6f6SMax Reitz#
93677e6f6SMax Reitz# This program is free software; you can redistribute it and/or modify
103677e6f6SMax Reitz# it under the terms of the GNU General Public License as published by
113677e6f6SMax Reitz# the Free Software Foundation; either version 2 of the License, or
123677e6f6SMax Reitz# (at your option) any later version.
133677e6f6SMax Reitz#
143677e6f6SMax Reitz# This program is distributed in the hope that it will be useful,
153677e6f6SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
163677e6f6SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
173677e6f6SMax Reitz# GNU General Public License for more details.
183677e6f6SMax Reitz#
193677e6f6SMax Reitz# You should have received a copy of the GNU General Public License
203677e6f6SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
213677e6f6SMax Reitz#
223677e6f6SMax Reitz
233677e6f6SMax Reitzimport os
243677e6f6SMax Reitzimport re
253677e6f6SMax Reitzimport json
263677e6f6SMax Reitzimport iotests
273677e6f6SMax Reitzfrom iotests import qemu_img, qemu_img_pipe
283677e6f6SMax Reitzimport unittest
293677e6f6SMax Reitz
303677e6f6SMax Reitztest_img = os.path.join(iotests.test_dir, 'test.img')
313677e6f6SMax Reitz
323677e6f6SMax Reitzclass TestImageInfoSpecific(iotests.QMPTestCase):
333677e6f6SMax Reitz    '''Abstract base class for ImageInfoSpecific tests'''
343677e6f6SMax Reitz
353677e6f6SMax Reitz    def setUp(self):
363677e6f6SMax Reitz        if self.img_options is None:
373677e6f6SMax Reitz            self.skipTest('Skipping abstract test class')
383677e6f6SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-o', self.img_options,
393677e6f6SMax Reitz                 test_img, '128K')
403677e6f6SMax Reitz
413677e6f6SMax Reitz    def tearDown(self):
423677e6f6SMax Reitz        os.remove(test_img)
433677e6f6SMax Reitz
443677e6f6SMax Reitzclass TestQemuImgInfo(TestImageInfoSpecific):
453677e6f6SMax Reitz    '''Abstract base class for qemu-img info tests'''
463677e6f6SMax Reitz
473677e6f6SMax Reitz    img_options = None
483677e6f6SMax Reitz    json_compare = None
493677e6f6SMax Reitz    human_compare = None
503677e6f6SMax Reitz
513677e6f6SMax Reitz    def test_json(self):
523677e6f6SMax Reitz        data = json.loads(qemu_img_pipe('info', '--output=json', test_img))
533677e6f6SMax Reitz        data = data['format-specific']
543677e6f6SMax Reitz        self.assertEqual(data['type'], iotests.imgfmt)
553677e6f6SMax Reitz        self.assertEqual(data['data'], self.json_compare)
563677e6f6SMax Reitz
573677e6f6SMax Reitz    def test_human(self):
583677e6f6SMax Reitz        data = qemu_img_pipe('info', '--output=human', test_img).split('\n')
593677e6f6SMax Reitz        data = data[(data.index('Format specific information:') + 1)
603677e6f6SMax Reitz                    :data.index('')]
613677e6f6SMax Reitz        for field in data:
623677e6f6SMax Reitz            self.assertTrue(re.match('^ {4}[^ ]', field) is not None)
6368474776SMax Reitz        data = [line.strip() for line in data]
643677e6f6SMax Reitz        self.assertEqual(data, self.human_compare)
653677e6f6SMax Reitz
663677e6f6SMax Reitzclass TestQMP(TestImageInfoSpecific):
673677e6f6SMax Reitz    '''Abstract base class for qemu QMP tests'''
683677e6f6SMax Reitz
693677e6f6SMax Reitz    img_options = None
703677e6f6SMax Reitz    qemu_options = ''
713677e6f6SMax Reitz    TestImageInfoSpecific = TestImageInfoSpecific
723677e6f6SMax Reitz
733677e6f6SMax Reitz    def setUp(self):
743677e6f6SMax Reitz        self.TestImageInfoSpecific.setUp(self)
753677e6f6SMax Reitz        self.vm = iotests.VM().add_drive(test_img, self.qemu_options)
763677e6f6SMax Reitz        self.vm.launch()
773677e6f6SMax Reitz
783677e6f6SMax Reitz    def tearDown(self):
793677e6f6SMax Reitz        self.vm.shutdown()
803677e6f6SMax Reitz        self.TestImageInfoSpecific.tearDown(self)
813677e6f6SMax Reitz
823677e6f6SMax Reitz    def test_qmp(self):
833677e6f6SMax Reitz        result = self.vm.qmp('query-block')['return']
8468474776SMax Reitz        drive = next(drive for drive in result if drive['device'] == 'drive0')
853677e6f6SMax Reitz        data = drive['inserted']['image']['format-specific']
863677e6f6SMax Reitz        self.assertEqual(data['type'], iotests.imgfmt)
873677e6f6SMax Reitz        self.assertEqual(data['data'], self.compare)
883677e6f6SMax Reitz
893677e6f6SMax Reitzclass TestQCow2(TestQemuImgInfo):
903677e6f6SMax Reitz    '''Testing a qcow2 version 2 image'''
913677e6f6SMax Reitz    img_options = 'compat=0.10'
92572ad978SDenis Plotnikov    json_compare = { 'compat': '0.10', 'refcount-bits': 16,
93572ad978SDenis Plotnikov                     'compression-type': 'zlib' }
94572ad978SDenis Plotnikov    human_compare = [ 'compat: 0.10', 'compression type: zlib',
95572ad978SDenis Plotnikov                      'refcount bits: 16' ]
963677e6f6SMax Reitz
973677e6f6SMax Reitzclass TestQCow3NotLazy(TestQemuImgInfo):
983677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts disabled'''
993677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=off'
1000709c5a1SMax Reitz    json_compare = { 'compat': '1.1', 'lazy-refcounts': False,
101572ad978SDenis Plotnikov                     'refcount-bits': 16, 'corrupt': False,
1027be20252SAlberto Garcia                     'compression-type': 'zlib', 'extended-l2': False }
103572ad978SDenis Plotnikov    human_compare = [ 'compat: 1.1', 'compression type: zlib',
104572ad978SDenis Plotnikov                      'lazy refcounts: false', 'refcount bits: 16',
1057be20252SAlberto Garcia                      'corrupt: false', 'extended l2: false' ]
1063677e6f6SMax Reitz
1073677e6f6SMax Reitzclass TestQCow3Lazy(TestQemuImgInfo):
1083677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts enabled'''
1093677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=on'
1100709c5a1SMax Reitz    json_compare = { 'compat': '1.1', 'lazy-refcounts': True,
111572ad978SDenis Plotnikov                     'refcount-bits': 16, 'corrupt': False,
1127be20252SAlberto Garcia                     'compression-type': 'zlib', 'extended-l2': False }
113572ad978SDenis Plotnikov    human_compare = [ 'compat: 1.1', 'compression type: zlib',
114572ad978SDenis Plotnikov                      'lazy refcounts: true', 'refcount bits: 16',
1157be20252SAlberto Garcia                      'corrupt: false', 'extended l2: false' ]
1163677e6f6SMax Reitz
1173677e6f6SMax Reitzclass TestQCow3NotLazyQMP(TestQMP):
1183677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts disabled, opening
1193677e6f6SMax Reitz       with lazy refcounts enabled'''
1203677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=off'
1213677e6f6SMax Reitz    qemu_options = 'lazy-refcounts=on'
1220709c5a1SMax Reitz    compare = { 'compat': '1.1', 'lazy-refcounts': False,
123572ad978SDenis Plotnikov                'refcount-bits': 16, 'corrupt': False,
1247be20252SAlberto Garcia                'compression-type': 'zlib', 'extended-l2': False }
1250709c5a1SMax Reitz
1263677e6f6SMax Reitz
1273677e6f6SMax Reitzclass TestQCow3LazyQMP(TestQMP):
1283677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts enabled, opening
1293677e6f6SMax Reitz       with lazy refcounts disabled'''
1303677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=on'
1313677e6f6SMax Reitz    qemu_options = 'lazy-refcounts=off'
1320709c5a1SMax Reitz    compare = { 'compat': '1.1', 'lazy-refcounts': True,
133572ad978SDenis Plotnikov                'refcount-bits': 16, 'corrupt': False,
1347be20252SAlberto Garcia                'compression-type': 'zlib', 'extended-l2': False }
1353677e6f6SMax Reitz
1363677e6f6SMax ReitzTestImageInfoSpecific = None
1373677e6f6SMax ReitzTestQemuImgInfo = None
1383677e6f6SMax ReitzTestQMP = None
1393677e6f6SMax Reitz
1403677e6f6SMax Reitzif __name__ == '__main__':
141103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2'],
142*b30b8077SVladimir Sementsov-Ogievskiy                 supported_protocols=['file'],
143*b30b8077SVladimir Sementsov-Ogievskiy                 unsupported_imgopts=['refcount_bits'])
144