1#!/usr/bin/env python3 2# group: rw quick 3# 4# Test for additional information emitted by qemu-img info on qcow2 5# images 6# 7# Copyright (C) 2013 Red Hat, Inc. 8# 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. 13# 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. 18# 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/>. 21# 22 23import os 24import re 25import json 26import iotests 27from iotests import qemu_img, qemu_img_pipe 28import unittest 29 30test_img = os.path.join(iotests.test_dir, 'test.img') 31 32class TestImageInfoSpecific(iotests.QMPTestCase): 33 '''Abstract base class for ImageInfoSpecific tests''' 34 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') 40 41 def tearDown(self): 42 os.remove(test_img) 43 44class TestQemuImgInfo(TestImageInfoSpecific): 45 '''Abstract base class for qemu-img info tests''' 46 47 img_options = None 48 json_compare = None 49 human_compare = None 50 51 def test_json(self): 52 data = json.loads(qemu_img_pipe('info', '--output=json', test_img)) 53 data = data['format-specific'] 54 self.assertEqual(data['type'], iotests.imgfmt) 55 self.assertEqual(data['data'], self.json_compare) 56 57 def test_human(self): 58 data = qemu_img_pipe('info', '--output=human', test_img).split('\n') 59 data = data[(data.index('Format specific information:') + 1) 60 :data.index('')] 61 for field in data: 62 self.assertTrue(re.match('^ {4}[^ ]', field) is not None) 63 data = [line.strip() for line in data] 64 self.assertEqual(data, self.human_compare) 65 66class TestQMP(TestImageInfoSpecific): 67 '''Abstract base class for qemu QMP tests''' 68 69 img_options = None 70 qemu_options = '' 71 TestImageInfoSpecific = TestImageInfoSpecific 72 73 def setUp(self): 74 self.TestImageInfoSpecific.setUp(self) 75 self.vm = iotests.VM().add_drive(test_img, self.qemu_options) 76 self.vm.launch() 77 78 def tearDown(self): 79 self.vm.shutdown() 80 self.TestImageInfoSpecific.tearDown(self) 81 82 def test_qmp(self): 83 result = self.vm.qmp('query-block')['return'] 84 drive = next(drive for drive in result if drive['device'] == 'drive0') 85 data = drive['inserted']['image']['format-specific'] 86 self.assertEqual(data['type'], iotests.imgfmt) 87 self.assertEqual(data['data'], self.compare) 88 89class TestQCow2(TestQemuImgInfo): 90 '''Testing a qcow2 version 2 image''' 91 img_options = 'compat=0.10' 92 json_compare = { 'compat': '0.10', 'refcount-bits': 16, 93 'compression-type': 'zlib' } 94 human_compare = [ 'compat: 0.10', 'compression type: zlib', 95 'refcount bits: 16' ] 96 97class TestQCow3NotLazy(TestQemuImgInfo): 98 '''Testing a qcow2 version 3 image with lazy refcounts disabled''' 99 img_options = 'compat=1.1,lazy_refcounts=off' 100 json_compare = { 'compat': '1.1', 'lazy-refcounts': False, 101 'refcount-bits': 16, 'corrupt': False, 102 'compression-type': 'zlib', 'extended-l2': False } 103 human_compare = [ 'compat: 1.1', 'compression type: zlib', 104 'lazy refcounts: false', 'refcount bits: 16', 105 'corrupt: false', 'extended l2: false' ] 106 107class TestQCow3Lazy(TestQemuImgInfo): 108 '''Testing a qcow2 version 3 image with lazy refcounts enabled''' 109 img_options = 'compat=1.1,lazy_refcounts=on' 110 json_compare = { 'compat': '1.1', 'lazy-refcounts': True, 111 'refcount-bits': 16, 'corrupt': False, 112 'compression-type': 'zlib', 'extended-l2': False } 113 human_compare = [ 'compat: 1.1', 'compression type: zlib', 114 'lazy refcounts: true', 'refcount bits: 16', 115 'corrupt: false', 'extended l2: false' ] 116 117class TestQCow3NotLazyQMP(TestQMP): 118 '''Testing a qcow2 version 3 image with lazy refcounts disabled, opening 119 with lazy refcounts enabled''' 120 img_options = 'compat=1.1,lazy_refcounts=off' 121 qemu_options = 'lazy-refcounts=on' 122 compare = { 'compat': '1.1', 'lazy-refcounts': False, 123 'refcount-bits': 16, 'corrupt': False, 124 'compression-type': 'zlib', 'extended-l2': False } 125 126 127class TestQCow3LazyQMP(TestQMP): 128 '''Testing a qcow2 version 3 image with lazy refcounts enabled, opening 129 with lazy refcounts disabled''' 130 img_options = 'compat=1.1,lazy_refcounts=on' 131 qemu_options = 'lazy-refcounts=off' 132 compare = { 'compat': '1.1', 'lazy-refcounts': True, 133 'refcount-bits': 16, 'corrupt': False, 134 'compression-type': 'zlib', 'extended-l2': False } 135 136TestImageInfoSpecific = None 137TestQemuImgInfo = None 138TestQMP = None 139 140if __name__ == '__main__': 141 iotests.main(supported_fmts=['qcow2'], 142 supported_protocols=['file']) 143