xref: /qemu/tests/qemu-iotests/065 (revision 0709c5a1530b046183b6e96d9631affcff76c1fc)
1f99b4b5dSMax Reitz#!/usr/bin/env python
23677e6f6SMax Reitz#
33677e6f6SMax Reitz# Test for additional information emitted by qemu-img info on qcow2
43677e6f6SMax Reitz# images
53677e6f6SMax Reitz#
63677e6f6SMax Reitz# Copyright (C) 2013 Red Hat, Inc.
73677e6f6SMax Reitz#
83677e6f6SMax Reitz# This program is free software; you can redistribute it and/or modify
93677e6f6SMax Reitz# it under the terms of the GNU General Public License as published by
103677e6f6SMax Reitz# the Free Software Foundation; either version 2 of the License, or
113677e6f6SMax Reitz# (at your option) any later version.
123677e6f6SMax Reitz#
133677e6f6SMax Reitz# This program is distributed in the hope that it will be useful,
143677e6f6SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
153677e6f6SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
163677e6f6SMax Reitz# GNU General Public License for more details.
173677e6f6SMax Reitz#
183677e6f6SMax Reitz# You should have received a copy of the GNU General Public License
193677e6f6SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
203677e6f6SMax Reitz#
213677e6f6SMax Reitz
223677e6f6SMax Reitzimport os
233677e6f6SMax Reitzimport re
243677e6f6SMax Reitzimport json
253677e6f6SMax Reitzimport iotests
263677e6f6SMax Reitzfrom iotests import qemu_img, qemu_img_pipe
273677e6f6SMax Reitzimport unittest
283677e6f6SMax Reitz
293677e6f6SMax Reitztest_img = os.path.join(iotests.test_dir, 'test.img')
303677e6f6SMax Reitz
313677e6f6SMax Reitzclass TestImageInfoSpecific(iotests.QMPTestCase):
323677e6f6SMax Reitz    '''Abstract base class for ImageInfoSpecific tests'''
333677e6f6SMax Reitz
343677e6f6SMax Reitz    def setUp(self):
353677e6f6SMax Reitz        if self.img_options is None:
363677e6f6SMax Reitz            self.skipTest('Skipping abstract test class')
373677e6f6SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-o', self.img_options,
383677e6f6SMax Reitz                 test_img, '128K')
393677e6f6SMax Reitz
403677e6f6SMax Reitz    def tearDown(self):
413677e6f6SMax Reitz        os.remove(test_img)
423677e6f6SMax Reitz
433677e6f6SMax Reitzclass TestQemuImgInfo(TestImageInfoSpecific):
443677e6f6SMax Reitz    '''Abstract base class for qemu-img info tests'''
453677e6f6SMax Reitz
463677e6f6SMax Reitz    img_options = None
473677e6f6SMax Reitz    json_compare = None
483677e6f6SMax Reitz    human_compare = None
493677e6f6SMax Reitz
503677e6f6SMax Reitz    def test_json(self):
513677e6f6SMax Reitz        data = json.loads(qemu_img_pipe('info', '--output=json', test_img))
523677e6f6SMax Reitz        data = data['format-specific']
533677e6f6SMax Reitz        self.assertEqual(data['type'], iotests.imgfmt)
543677e6f6SMax Reitz        self.assertEqual(data['data'], self.json_compare)
553677e6f6SMax Reitz
563677e6f6SMax Reitz    def test_human(self):
573677e6f6SMax Reitz        data = qemu_img_pipe('info', '--output=human', test_img).split('\n')
583677e6f6SMax Reitz        data = data[(data.index('Format specific information:') + 1)
593677e6f6SMax Reitz                    :data.index('')]
603677e6f6SMax Reitz        for field in data:
613677e6f6SMax Reitz            self.assertTrue(re.match('^ {4}[^ ]', field) is not None)
623677e6f6SMax Reitz        data = map(lambda line: line.strip(), data)
633677e6f6SMax Reitz        self.assertEqual(data, self.human_compare)
643677e6f6SMax Reitz
653677e6f6SMax Reitzclass TestQMP(TestImageInfoSpecific):
663677e6f6SMax Reitz    '''Abstract base class for qemu QMP tests'''
673677e6f6SMax Reitz
683677e6f6SMax Reitz    img_options = None
693677e6f6SMax Reitz    qemu_options = ''
703677e6f6SMax Reitz    TestImageInfoSpecific = TestImageInfoSpecific
713677e6f6SMax Reitz
723677e6f6SMax Reitz    def setUp(self):
733677e6f6SMax Reitz        self.TestImageInfoSpecific.setUp(self)
743677e6f6SMax Reitz        self.vm = iotests.VM().add_drive(test_img, self.qemu_options)
753677e6f6SMax Reitz        self.vm.launch()
763677e6f6SMax Reitz
773677e6f6SMax Reitz    def tearDown(self):
783677e6f6SMax Reitz        self.vm.shutdown()
793677e6f6SMax Reitz        self.TestImageInfoSpecific.tearDown(self)
803677e6f6SMax Reitz
813677e6f6SMax Reitz    def test_qmp(self):
823677e6f6SMax Reitz        result = self.vm.qmp('query-block')['return']
833677e6f6SMax Reitz        drive = filter(lambda drive: drive['device'] == 'drive0', result)[0]
843677e6f6SMax Reitz        data = drive['inserted']['image']['format-specific']
853677e6f6SMax Reitz        self.assertEqual(data['type'], iotests.imgfmt)
863677e6f6SMax Reitz        self.assertEqual(data['data'], self.compare)
873677e6f6SMax Reitz
883677e6f6SMax Reitzclass TestQCow2(TestQemuImgInfo):
893677e6f6SMax Reitz    '''Testing a qcow2 version 2 image'''
903677e6f6SMax Reitz    img_options = 'compat=0.10'
91*0709c5a1SMax Reitz    json_compare = { 'compat': '0.10', 'refcount-bits': 16 }
92*0709c5a1SMax Reitz    human_compare = [ 'compat: 0.10', 'refcount bits: 16' ]
933677e6f6SMax Reitz
943677e6f6SMax Reitzclass TestQCow3NotLazy(TestQemuImgInfo):
953677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts disabled'''
963677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=off'
97*0709c5a1SMax Reitz    json_compare = { 'compat': '1.1', 'lazy-refcounts': False,
98*0709c5a1SMax Reitz                     'refcount-bits': 16, 'corrupt': False }
99*0709c5a1SMax Reitz    human_compare = [ 'compat: 1.1', 'lazy refcounts: false',
100*0709c5a1SMax Reitz                      'refcount bits: 16', 'corrupt: false' ]
1013677e6f6SMax Reitz
1023677e6f6SMax Reitzclass TestQCow3Lazy(TestQemuImgInfo):
1033677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts enabled'''
1043677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=on'
105*0709c5a1SMax Reitz    json_compare = { 'compat': '1.1', 'lazy-refcounts': True,
106*0709c5a1SMax Reitz                     'refcount-bits': 16, 'corrupt': False }
107*0709c5a1SMax Reitz    human_compare = [ 'compat: 1.1', 'lazy refcounts: true',
108*0709c5a1SMax Reitz                      'refcount bits: 16', 'corrupt: false' ]
1093677e6f6SMax Reitz
1103677e6f6SMax Reitzclass TestQCow3NotLazyQMP(TestQMP):
1113677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts disabled, opening
1123677e6f6SMax Reitz       with lazy refcounts enabled'''
1133677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=off'
1143677e6f6SMax Reitz    qemu_options = 'lazy-refcounts=on'
115*0709c5a1SMax Reitz    compare = { 'compat': '1.1', 'lazy-refcounts': False,
116*0709c5a1SMax Reitz                'refcount-bits': 16, 'corrupt': False }
117*0709c5a1SMax Reitz
1183677e6f6SMax Reitz
1193677e6f6SMax Reitzclass TestQCow3LazyQMP(TestQMP):
1203677e6f6SMax Reitz    '''Testing a qcow2 version 3 image with lazy refcounts enabled, opening
1213677e6f6SMax Reitz       with lazy refcounts disabled'''
1223677e6f6SMax Reitz    img_options = 'compat=1.1,lazy_refcounts=on'
1233677e6f6SMax Reitz    qemu_options = 'lazy-refcounts=off'
124*0709c5a1SMax Reitz    compare = { 'compat': '1.1', 'lazy-refcounts': True,
125*0709c5a1SMax Reitz                'refcount-bits': 16, 'corrupt': False }
1263677e6f6SMax Reitz
1273677e6f6SMax ReitzTestImageInfoSpecific = None
1283677e6f6SMax ReitzTestQemuImgInfo = None
1293677e6f6SMax ReitzTestQMP = None
1303677e6f6SMax Reitz
1313677e6f6SMax Reitzif __name__ == '__main__':
1323677e6f6SMax Reitz    iotests.main(supported_fmts=['qcow2'])
133