1#!/usr/bin/env python 2# 3# Tests for image block commit. 4# 5# Copyright (C) 2012 IBM, Corp. 6# Copyright (C) 2012 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# Test for live block commit 22# Derived from Image Streaming Test 030 23 24import time 25import os 26import iotests 27from iotests import qemu_img, qemu_io 28import struct 29import errno 30 31backing_img = os.path.join(iotests.test_dir, 'backing.img') 32mid_img = os.path.join(iotests.test_dir, 'mid.img') 33test_img = os.path.join(iotests.test_dir, 'test.img') 34 35class ImageCommitTestCase(iotests.QMPTestCase): 36 '''Abstract base class for image commit test cases''' 37 38 def wait_for_complete(self, need_ready=False): 39 completed = False 40 ready = False 41 while not completed: 42 for event in self.vm.get_qmp_events(wait=True): 43 if event['event'] == 'BLOCK_JOB_COMPLETED': 44 self.assert_qmp(event, 'data/type', 'commit') 45 self.assert_qmp(event, 'data/device', 'drive0') 46 self.assert_qmp(event, 'data/offset', event['data']['len']) 47 if need_ready: 48 self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event") 49 completed = True 50 elif event['event'] == 'BLOCK_JOB_READY': 51 ready = True 52 self.assert_qmp(event, 'data/type', 'commit') 53 self.assert_qmp(event, 'data/device', 'drive0') 54 self.vm.qmp('block-job-complete', device='drive0') 55 56 self.assert_no_active_block_jobs() 57 self.vm.shutdown() 58 59 def run_commit_test(self, top, base, need_ready=False): 60 self.assert_no_active_block_jobs() 61 result = self.vm.qmp('block-commit', device='drive0', top=top, base=base) 62 self.assert_qmp(result, 'return', {}) 63 self.wait_for_complete(need_ready) 64 65 def run_default_commit_test(self): 66 self.assert_no_active_block_jobs() 67 result = self.vm.qmp('block-commit', device='drive0') 68 self.assert_qmp(result, 'return', {}) 69 self.wait_for_complete() 70 71class TestSingleDrive(ImageCommitTestCase): 72 image_len = 1 * 1024 * 1024 73 test_len = 1 * 1024 * 256 74 75 def setUp(self): 76 iotests.create_image(backing_img, self.image_len) 77 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 78 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 79 qemu_io('-c', 'write -P 0xab 0 524288', backing_img) 80 qemu_io('-c', 'write -P 0xef 524288 524288', mid_img) 81 self.vm = iotests.VM().add_drive(test_img) 82 self.vm.launch() 83 84 def tearDown(self): 85 self.vm.shutdown() 86 os.remove(test_img) 87 os.remove(mid_img) 88 os.remove(backing_img) 89 90 def test_commit(self): 91 self.run_commit_test(mid_img, backing_img) 92 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 93 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 94 95 def test_device_not_found(self): 96 result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img) 97 self.assert_qmp(result, 'error/class', 'DeviceNotFound') 98 99 def test_top_same_base(self): 100 self.assert_no_active_block_jobs() 101 result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img) 102 self.assert_qmp(result, 'error/class', 'GenericError') 103 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img) 104 105 def test_top_invalid(self): 106 self.assert_no_active_block_jobs() 107 result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img) 108 self.assert_qmp(result, 'error/class', 'GenericError') 109 self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 110 111 def test_base_invalid(self): 112 self.assert_no_active_block_jobs() 113 result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile') 114 self.assert_qmp(result, 'error/class', 'GenericError') 115 self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 116 117 def test_top_is_active(self): 118 self.run_commit_test(test_img, backing_img, need_ready=True) 119 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 120 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 121 122 def test_top_is_default_active(self): 123 self.run_default_commit_test() 124 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 125 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 126 127 def test_top_and_base_reversed(self): 128 self.assert_no_active_block_jobs() 129 result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img) 130 self.assert_qmp(result, 'error/class', 'GenericError') 131 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img) 132 133 134class TestRelativePaths(ImageCommitTestCase): 135 image_len = 1 * 1024 * 1024 136 test_len = 1 * 1024 * 256 137 138 dir1 = "dir1" 139 dir2 = "dir2/" 140 dir3 = "dir2/dir3/" 141 142 test_img = os.path.join(iotests.test_dir, dir3, 'test.img') 143 mid_img = "../mid.img" 144 backing_img = "../dir1/backing.img" 145 146 backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img') 147 mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img') 148 149 def setUp(self): 150 try: 151 os.mkdir(os.path.join(iotests.test_dir, self.dir1)) 152 os.mkdir(os.path.join(iotests.test_dir, self.dir2)) 153 os.mkdir(os.path.join(iotests.test_dir, self.dir3)) 154 except OSError as exception: 155 if exception.errno != errno.EEXIST: 156 raise 157 iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len) 158 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs) 159 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img) 160 qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs) 161 qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img) 162 qemu_io('-c', 'write -P 0xab 0 524288', self.backing_img_abs) 163 qemu_io('-c', 'write -P 0xef 524288 524288', self.mid_img_abs) 164 self.vm = iotests.VM().add_drive(self.test_img) 165 self.vm.launch() 166 167 def tearDown(self): 168 self.vm.shutdown() 169 os.remove(self.test_img) 170 os.remove(self.mid_img_abs) 171 os.remove(self.backing_img_abs) 172 try: 173 os.rmdir(os.path.join(iotests.test_dir, self.dir1)) 174 os.rmdir(os.path.join(iotests.test_dir, self.dir3)) 175 os.rmdir(os.path.join(iotests.test_dir, self.dir2)) 176 except OSError as exception: 177 if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY: 178 raise 179 180 def test_commit(self): 181 self.run_commit_test(self.mid_img, self.backing_img) 182 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 183 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 184 185 def test_device_not_found(self): 186 result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img) 187 self.assert_qmp(result, 'error/class', 'DeviceNotFound') 188 189 def test_top_same_base(self): 190 self.assert_no_active_block_jobs() 191 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img) 192 self.assert_qmp(result, 'error/class', 'GenericError') 193 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 194 195 def test_top_invalid(self): 196 self.assert_no_active_block_jobs() 197 result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img) 198 self.assert_qmp(result, 'error/class', 'GenericError') 199 self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 200 201 def test_base_invalid(self): 202 self.assert_no_active_block_jobs() 203 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile') 204 self.assert_qmp(result, 'error/class', 'GenericError') 205 self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 206 207 def test_top_is_active(self): 208 self.run_commit_test(self.test_img, self.backing_img) 209 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 210 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 211 212 def test_top_and_base_reversed(self): 213 self.assert_no_active_block_jobs() 214 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img) 215 self.assert_qmp(result, 'error/class', 'GenericError') 216 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 217 218 219class TestSetSpeed(ImageCommitTestCase): 220 image_len = 80 * 1024 * 1024 # MB 221 222 def setUp(self): 223 qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 224 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 225 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 226 qemu_io('-c', 'write -P 0x1 0 512', test_img) 227 qemu_io('-c', 'write -P 0xef 524288 524288', mid_img) 228 self.vm = iotests.VM().add_drive(test_img) 229 self.vm.launch() 230 231 def tearDown(self): 232 self.vm.shutdown() 233 os.remove(test_img) 234 os.remove(mid_img) 235 os.remove(backing_img) 236 237 def test_set_speed(self): 238 self.assert_no_active_block_jobs() 239 240 self.vm.pause_drive('drive0') 241 result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024) 242 self.assert_qmp(result, 'return', {}) 243 244 # Ensure the speed we set was accepted 245 result = self.vm.qmp('query-block-jobs') 246 self.assert_qmp(result, 'return[0]/device', 'drive0') 247 self.assert_qmp(result, 'return[0]/speed', 1024 * 1024) 248 249 self.cancel_and_wait(resume=True) 250 251class TestActiveZeroLengthImage(TestSingleDrive): 252 image_len = 0 253 254if __name__ == '__main__': 255 iotests.main(supported_fmts=['qcow2', 'qed']) 256