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