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