1*3677e6f6SMax Reitz#!/usr/bin/env python2 2*3677e6f6SMax Reitz# 3*3677e6f6SMax Reitz# Test for additional information emitted by qemu-img info on qcow2 4*3677e6f6SMax Reitz# images 5*3677e6f6SMax Reitz# 6*3677e6f6SMax Reitz# Copyright (C) 2013 Red Hat, Inc. 7*3677e6f6SMax Reitz# 8*3677e6f6SMax Reitz# This program is free software; you can redistribute it and/or modify 9*3677e6f6SMax Reitz# it under the terms of the GNU General Public License as published by 10*3677e6f6SMax Reitz# the Free Software Foundation; either version 2 of the License, or 11*3677e6f6SMax Reitz# (at your option) any later version. 12*3677e6f6SMax Reitz# 13*3677e6f6SMax Reitz# This program is distributed in the hope that it will be useful, 14*3677e6f6SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 15*3677e6f6SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*3677e6f6SMax Reitz# GNU General Public License for more details. 17*3677e6f6SMax Reitz# 18*3677e6f6SMax Reitz# You should have received a copy of the GNU General Public License 19*3677e6f6SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 20*3677e6f6SMax Reitz# 21*3677e6f6SMax Reitz 22*3677e6f6SMax Reitzimport os 23*3677e6f6SMax Reitzimport re 24*3677e6f6SMax Reitzimport json 25*3677e6f6SMax Reitzimport iotests 26*3677e6f6SMax Reitzfrom iotests import qemu_img, qemu_img_pipe 27*3677e6f6SMax Reitzimport unittest 28*3677e6f6SMax Reitz 29*3677e6f6SMax Reitztest_img = os.path.join(iotests.test_dir, 'test.img') 30*3677e6f6SMax Reitz 31*3677e6f6SMax Reitzclass TestImageInfoSpecific(iotests.QMPTestCase): 32*3677e6f6SMax Reitz '''Abstract base class for ImageInfoSpecific tests''' 33*3677e6f6SMax Reitz 34*3677e6f6SMax Reitz def setUp(self): 35*3677e6f6SMax Reitz if self.img_options is None: 36*3677e6f6SMax Reitz self.skipTest('Skipping abstract test class') 37*3677e6f6SMax Reitz qemu_img('create', '-f', iotests.imgfmt, '-o', self.img_options, 38*3677e6f6SMax Reitz test_img, '128K') 39*3677e6f6SMax Reitz 40*3677e6f6SMax Reitz def tearDown(self): 41*3677e6f6SMax Reitz os.remove(test_img) 42*3677e6f6SMax Reitz 43*3677e6f6SMax Reitzclass TestQemuImgInfo(TestImageInfoSpecific): 44*3677e6f6SMax Reitz '''Abstract base class for qemu-img info tests''' 45*3677e6f6SMax Reitz 46*3677e6f6SMax Reitz img_options = None 47*3677e6f6SMax Reitz json_compare = None 48*3677e6f6SMax Reitz human_compare = None 49*3677e6f6SMax Reitz 50*3677e6f6SMax Reitz def test_json(self): 51*3677e6f6SMax Reitz data = json.loads(qemu_img_pipe('info', '--output=json', test_img)) 52*3677e6f6SMax Reitz data = data['format-specific'] 53*3677e6f6SMax Reitz self.assertEqual(data['type'], iotests.imgfmt) 54*3677e6f6SMax Reitz self.assertEqual(data['data'], self.json_compare) 55*3677e6f6SMax Reitz 56*3677e6f6SMax Reitz def test_human(self): 57*3677e6f6SMax Reitz data = qemu_img_pipe('info', '--output=human', test_img).split('\n') 58*3677e6f6SMax Reitz data = data[(data.index('Format specific information:') + 1) 59*3677e6f6SMax Reitz :data.index('')] 60*3677e6f6SMax Reitz for field in data: 61*3677e6f6SMax Reitz self.assertTrue(re.match('^ {4}[^ ]', field) is not None) 62*3677e6f6SMax Reitz data = map(lambda line: line.strip(), data) 63*3677e6f6SMax Reitz self.assertEqual(data, self.human_compare) 64*3677e6f6SMax Reitz 65*3677e6f6SMax Reitzclass TestQMP(TestImageInfoSpecific): 66*3677e6f6SMax Reitz '''Abstract base class for qemu QMP tests''' 67*3677e6f6SMax Reitz 68*3677e6f6SMax Reitz img_options = None 69*3677e6f6SMax Reitz qemu_options = '' 70*3677e6f6SMax Reitz TestImageInfoSpecific = TestImageInfoSpecific 71*3677e6f6SMax Reitz 72*3677e6f6SMax Reitz def setUp(self): 73*3677e6f6SMax Reitz self.TestImageInfoSpecific.setUp(self) 74*3677e6f6SMax Reitz self.vm = iotests.VM().add_drive(test_img, self.qemu_options) 75*3677e6f6SMax Reitz self.vm.launch() 76*3677e6f6SMax Reitz 77*3677e6f6SMax Reitz def tearDown(self): 78*3677e6f6SMax Reitz self.vm.shutdown() 79*3677e6f6SMax Reitz self.TestImageInfoSpecific.tearDown(self) 80*3677e6f6SMax Reitz 81*3677e6f6SMax Reitz def test_qmp(self): 82*3677e6f6SMax Reitz result = self.vm.qmp('query-block')['return'] 83*3677e6f6SMax Reitz drive = filter(lambda drive: drive['device'] == 'drive0', result)[0] 84*3677e6f6SMax Reitz data = drive['inserted']['image']['format-specific'] 85*3677e6f6SMax Reitz self.assertEqual(data['type'], iotests.imgfmt) 86*3677e6f6SMax Reitz self.assertEqual(data['data'], self.compare) 87*3677e6f6SMax Reitz 88*3677e6f6SMax Reitzclass TestQCow2(TestQemuImgInfo): 89*3677e6f6SMax Reitz '''Testing a qcow2 version 2 image''' 90*3677e6f6SMax Reitz img_options = 'compat=0.10' 91*3677e6f6SMax Reitz json_compare = { 'compat': '0.10' } 92*3677e6f6SMax Reitz human_compare = [ 'compat: 0.10' ] 93*3677e6f6SMax Reitz 94*3677e6f6SMax Reitzclass TestQCow3NotLazy(TestQemuImgInfo): 95*3677e6f6SMax Reitz '''Testing a qcow2 version 3 image with lazy refcounts disabled''' 96*3677e6f6SMax Reitz img_options = 'compat=1.1,lazy_refcounts=off' 97*3677e6f6SMax Reitz json_compare = { 'compat': '1.1', 'lazy-refcounts': False } 98*3677e6f6SMax Reitz human_compare = [ 'compat: 1.1', 'lazy refcounts: false' ] 99*3677e6f6SMax Reitz 100*3677e6f6SMax Reitzclass TestQCow3Lazy(TestQemuImgInfo): 101*3677e6f6SMax Reitz '''Testing a qcow2 version 3 image with lazy refcounts enabled''' 102*3677e6f6SMax Reitz img_options = 'compat=1.1,lazy_refcounts=on' 103*3677e6f6SMax Reitz json_compare = { 'compat': '1.1', 'lazy-refcounts': True } 104*3677e6f6SMax Reitz human_compare = [ 'compat: 1.1', 'lazy refcounts: true' ] 105*3677e6f6SMax Reitz 106*3677e6f6SMax Reitzclass TestQCow3NotLazyQMP(TestQMP): 107*3677e6f6SMax Reitz '''Testing a qcow2 version 3 image with lazy refcounts disabled, opening 108*3677e6f6SMax Reitz with lazy refcounts enabled''' 109*3677e6f6SMax Reitz img_options = 'compat=1.1,lazy_refcounts=off' 110*3677e6f6SMax Reitz qemu_options = 'lazy-refcounts=on' 111*3677e6f6SMax Reitz compare = { 'compat': '1.1', 'lazy-refcounts': False } 112*3677e6f6SMax Reitz 113*3677e6f6SMax Reitzclass TestQCow3LazyQMP(TestQMP): 114*3677e6f6SMax Reitz '''Testing a qcow2 version 3 image with lazy refcounts enabled, opening 115*3677e6f6SMax Reitz with lazy refcounts disabled''' 116*3677e6f6SMax Reitz img_options = 'compat=1.1,lazy_refcounts=on' 117*3677e6f6SMax Reitz qemu_options = 'lazy-refcounts=off' 118*3677e6f6SMax Reitz compare = { 'compat': '1.1', 'lazy-refcounts': True } 119*3677e6f6SMax Reitz 120*3677e6f6SMax ReitzTestImageInfoSpecific = None 121*3677e6f6SMax ReitzTestQemuImgInfo = None 122*3677e6f6SMax ReitzTestQMP = None 123*3677e6f6SMax Reitz 124*3677e6f6SMax Reitzif __name__ == '__main__': 125*3677e6f6SMax Reitz iotests.main(supported_fmts=['qcow2']) 126