Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Tests for image streaming. |
| 4 | # |
| 5 | # Copyright (C) 2012 IBM Corp. |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | |
Paolo Bonzini | 0c81734 | 2012-09-28 17:22:52 +0200 | [diff] [blame] | 21 | import time |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 22 | import os |
| 23 | import iotests |
| 24 | from iotests import qemu_img, qemu_io |
| 25 | |
| 26 | backing_img = os.path.join(iotests.test_dir, 'backing.img') |
Paolo Bonzini | 6e34360 | 2012-05-09 15:05:03 +0200 | [diff] [blame] | 27 | mid_img = os.path.join(iotests.test_dir, 'mid.img') |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 28 | test_img = os.path.join(iotests.test_dir, 'test.img') |
| 29 | |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 30 | class TestSingleDrive(iotests.QMPTestCase): |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 31 | image_len = 1 * 1024 * 1024 # MB |
| 32 | |
| 33 | def setUp(self): |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 34 | iotests.create_image(backing_img, TestSingleDrive.image_len) |
Paolo Bonzini | 6e34360 | 2012-05-09 15:05:03 +0200 | [diff] [blame] | 35 | qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) |
| 36 | qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 37 | qemu_io('-c', 'write -P 0x1 0 512', backing_img) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 38 | self.vm = iotests.VM().add_drive(test_img) |
| 39 | self.vm.launch() |
| 40 | |
| 41 | def tearDown(self): |
| 42 | self.vm.shutdown() |
| 43 | os.remove(test_img) |
Paolo Bonzini | 6e34360 | 2012-05-09 15:05:03 +0200 | [diff] [blame] | 44 | os.remove(mid_img) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 45 | os.remove(backing_img) |
| 46 | |
| 47 | def test_stream(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 48 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 49 | |
Stefan Hajnoczi | db58f9c | 2012-04-11 16:27:10 +0100 | [diff] [blame] | 50 | result = self.vm.qmp('block-stream', device='drive0') |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 51 | self.assert_qmp(result, 'return', {}) |
| 52 | |
Fam Zheng | 9974ad4 | 2014-04-02 13:54:07 +0800 | [diff] [blame] | 53 | self.wait_until_completed() |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 54 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 55 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 863a5d0 | 2012-05-08 16:51:53 +0200 | [diff] [blame] | 56 | self.vm.shutdown() |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 57 | |
Paolo Bonzini | efcc7a2 | 2012-05-08 16:51:58 +0200 | [diff] [blame] | 58 | self.assertEqual(qemu_io('-c', 'map', backing_img), |
| 59 | qemu_io('-c', 'map', test_img), |
| 60 | 'image file map does not match backing file after streaming') |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 61 | |
Paolo Bonzini | 0c81734 | 2012-09-28 17:22:52 +0200 | [diff] [blame] | 62 | def test_stream_pause(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 63 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 0c81734 | 2012-09-28 17:22:52 +0200 | [diff] [blame] | 64 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 65 | self.vm.pause_drive('drive0') |
Paolo Bonzini | 0c81734 | 2012-09-28 17:22:52 +0200 | [diff] [blame] | 66 | result = self.vm.qmp('block-stream', device='drive0') |
| 67 | self.assert_qmp(result, 'return', {}) |
| 68 | |
| 69 | result = self.vm.qmp('block-job-pause', device='drive0') |
| 70 | self.assert_qmp(result, 'return', {}) |
| 71 | |
| 72 | time.sleep(1) |
| 73 | result = self.vm.qmp('query-block-jobs') |
| 74 | offset = self.dictpath(result, 'return[0]/offset') |
| 75 | |
| 76 | time.sleep(1) |
| 77 | result = self.vm.qmp('query-block-jobs') |
| 78 | self.assert_qmp(result, 'return[0]/offset', offset) |
| 79 | |
| 80 | result = self.vm.qmp('block-job-resume', device='drive0') |
| 81 | self.assert_qmp(result, 'return', {}) |
| 82 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 83 | self.vm.resume_drive('drive0') |
Fam Zheng | 9974ad4 | 2014-04-02 13:54:07 +0800 | [diff] [blame] | 84 | self.wait_until_completed() |
Paolo Bonzini | 0c81734 | 2012-09-28 17:22:52 +0200 | [diff] [blame] | 85 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 86 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 0c81734 | 2012-09-28 17:22:52 +0200 | [diff] [blame] | 87 | self.vm.shutdown() |
| 88 | |
| 89 | self.assertEqual(qemu_io('-c', 'map', backing_img), |
| 90 | qemu_io('-c', 'map', test_img), |
| 91 | 'image file map does not match backing file after streaming') |
| 92 | |
Paolo Bonzini | 6e34360 | 2012-05-09 15:05:03 +0200 | [diff] [blame] | 93 | def test_stream_partial(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 94 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 6e34360 | 2012-05-09 15:05:03 +0200 | [diff] [blame] | 95 | |
| 96 | result = self.vm.qmp('block-stream', device='drive0', base=mid_img) |
| 97 | self.assert_qmp(result, 'return', {}) |
| 98 | |
Fam Zheng | 9974ad4 | 2014-04-02 13:54:07 +0800 | [diff] [blame] | 99 | self.wait_until_completed() |
Paolo Bonzini | 6e34360 | 2012-05-09 15:05:03 +0200 | [diff] [blame] | 100 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 101 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 6e34360 | 2012-05-09 15:05:03 +0200 | [diff] [blame] | 102 | self.vm.shutdown() |
| 103 | |
| 104 | self.assertEqual(qemu_io('-c', 'map', mid_img), |
| 105 | qemu_io('-c', 'map', test_img), |
| 106 | 'image file map does not match backing file after streaming') |
| 107 | |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 108 | def test_device_not_found(self): |
Stefan Hajnoczi | db58f9c | 2012-04-11 16:27:10 +0100 | [diff] [blame] | 109 | result = self.vm.qmp('block-stream', device='nonexistent') |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 110 | self.assert_qmp(result, 'error/class', 'DeviceNotFound') |
| 111 | |
Stefan Hajnoczi | 774a885 | 2012-08-28 15:26:49 +0100 | [diff] [blame] | 112 | |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 113 | class TestSmallerBackingFile(iotests.QMPTestCase): |
Stefan Hajnoczi | 774a885 | 2012-08-28 15:26:49 +0100 | [diff] [blame] | 114 | backing_len = 1 * 1024 * 1024 # MB |
| 115 | image_len = 2 * backing_len |
| 116 | |
| 117 | def setUp(self): |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 118 | iotests.create_image(backing_img, self.backing_len) |
Stefan Hajnoczi | 774a885 | 2012-08-28 15:26:49 +0100 | [diff] [blame] | 119 | qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len)) |
| 120 | self.vm = iotests.VM().add_drive(test_img) |
| 121 | self.vm.launch() |
| 122 | |
| 123 | # If this hangs, then you are missing a fix to complete streaming when the |
| 124 | # end of the backing file is reached. |
| 125 | def test_stream(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 126 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | 774a885 | 2012-08-28 15:26:49 +0100 | [diff] [blame] | 127 | |
| 128 | result = self.vm.qmp('block-stream', device='drive0') |
| 129 | self.assert_qmp(result, 'return', {}) |
| 130 | |
Fam Zheng | 9974ad4 | 2014-04-02 13:54:07 +0800 | [diff] [blame] | 131 | self.wait_until_completed() |
Stefan Hajnoczi | 774a885 | 2012-08-28 15:26:49 +0100 | [diff] [blame] | 132 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 133 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | 774a885 | 2012-08-28 15:26:49 +0100 | [diff] [blame] | 134 | self.vm.shutdown() |
| 135 | |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 136 | class TestErrors(iotests.QMPTestCase): |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 137 | image_len = 2 * 1024 * 1024 # MB |
| 138 | |
| 139 | # this should match STREAM_BUFFER_SIZE/512 in block/stream.c |
| 140 | STREAM_BUFFER_SIZE = 512 * 1024 |
| 141 | |
| 142 | def create_blkdebug_file(self, name, event, errno): |
| 143 | file = open(name, 'w') |
| 144 | file.write(''' |
| 145 | [inject-error] |
| 146 | state = "1" |
| 147 | event = "%s" |
| 148 | errno = "%d" |
| 149 | immediately = "off" |
| 150 | once = "on" |
| 151 | sector = "%d" |
| 152 | |
| 153 | [set-state] |
| 154 | state = "1" |
| 155 | event = "%s" |
| 156 | new_state = "2" |
| 157 | |
| 158 | [set-state] |
| 159 | state = "2" |
| 160 | event = "%s" |
| 161 | new_state = "1" |
| 162 | ''' % (event, errno, self.STREAM_BUFFER_SIZE / 512, event, event)) |
| 163 | file.close() |
| 164 | |
| 165 | class TestEIO(TestErrors): |
| 166 | def setUp(self): |
| 167 | self.blkdebug_file = backing_img + ".blkdebug" |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 168 | iotests.create_image(backing_img, TestErrors.image_len) |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 169 | self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5) |
| 170 | qemu_img('create', '-f', iotests.imgfmt, |
| 171 | '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' |
| 172 | % (self.blkdebug_file, backing_img), |
| 173 | test_img) |
| 174 | self.vm = iotests.VM().add_drive(test_img) |
| 175 | self.vm.launch() |
| 176 | |
| 177 | def tearDown(self): |
| 178 | self.vm.shutdown() |
| 179 | os.remove(test_img) |
| 180 | os.remove(backing_img) |
| 181 | os.remove(self.blkdebug_file) |
| 182 | |
| 183 | def test_report(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 184 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 185 | |
| 186 | result = self.vm.qmp('block-stream', device='drive0') |
| 187 | self.assert_qmp(result, 'return', {}) |
| 188 | |
| 189 | completed = False |
| 190 | error = False |
| 191 | while not completed: |
| 192 | for event in self.vm.get_qmp_events(wait=True): |
| 193 | if event['event'] == 'BLOCK_JOB_ERROR': |
| 194 | self.assert_qmp(event, 'data/device', 'drive0') |
| 195 | self.assert_qmp(event, 'data/operation', 'read') |
| 196 | error = True |
| 197 | elif event['event'] == 'BLOCK_JOB_COMPLETED': |
| 198 | self.assertTrue(error, 'job completed unexpectedly') |
| 199 | self.assert_qmp(event, 'data/type', 'stream') |
| 200 | self.assert_qmp(event, 'data/device', 'drive0') |
| 201 | self.assert_qmp(event, 'data/error', 'Input/output error') |
| 202 | self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE) |
| 203 | self.assert_qmp(event, 'data/len', self.image_len) |
| 204 | completed = True |
| 205 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 206 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 207 | self.vm.shutdown() |
| 208 | |
| 209 | def test_ignore(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 210 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 211 | |
| 212 | result = self.vm.qmp('block-stream', device='drive0', on_error='ignore') |
| 213 | self.assert_qmp(result, 'return', {}) |
| 214 | |
| 215 | error = False |
| 216 | completed = False |
| 217 | while not completed: |
| 218 | for event in self.vm.get_qmp_events(wait=True): |
| 219 | if event['event'] == 'BLOCK_JOB_ERROR': |
| 220 | self.assert_qmp(event, 'data/device', 'drive0') |
| 221 | self.assert_qmp(event, 'data/operation', 'read') |
| 222 | result = self.vm.qmp('query-block-jobs') |
| 223 | self.assert_qmp(result, 'return[0]/paused', False) |
| 224 | error = True |
| 225 | elif event['event'] == 'BLOCK_JOB_COMPLETED': |
| 226 | self.assertTrue(error, 'job completed unexpectedly') |
| 227 | self.assert_qmp(event, 'data/type', 'stream') |
| 228 | self.assert_qmp(event, 'data/device', 'drive0') |
| 229 | self.assert_qmp(event, 'data/error', 'Input/output error') |
| 230 | self.assert_qmp(event, 'data/offset', self.image_len) |
| 231 | self.assert_qmp(event, 'data/len', self.image_len) |
| 232 | completed = True |
| 233 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 234 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 235 | self.vm.shutdown() |
| 236 | |
| 237 | def test_stop(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 238 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 239 | |
| 240 | result = self.vm.qmp('block-stream', device='drive0', on_error='stop') |
| 241 | self.assert_qmp(result, 'return', {}) |
| 242 | |
| 243 | error = False |
| 244 | completed = False |
| 245 | while not completed: |
| 246 | for event in self.vm.get_qmp_events(wait=True): |
| 247 | if event['event'] == 'BLOCK_JOB_ERROR': |
| 248 | self.assert_qmp(event, 'data/device', 'drive0') |
| 249 | self.assert_qmp(event, 'data/operation', 'read') |
| 250 | |
| 251 | result = self.vm.qmp('query-block-jobs') |
| 252 | self.assert_qmp(result, 'return[0]/paused', True) |
| 253 | self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE) |
| 254 | self.assert_qmp(result, 'return[0]/io-status', 'failed') |
| 255 | |
| 256 | result = self.vm.qmp('block-job-resume', device='drive0') |
| 257 | self.assert_qmp(result, 'return', {}) |
| 258 | |
| 259 | result = self.vm.qmp('query-block-jobs') |
| 260 | self.assert_qmp(result, 'return[0]/paused', False) |
| 261 | self.assert_qmp(result, 'return[0]/io-status', 'ok') |
| 262 | error = True |
| 263 | elif event['event'] == 'BLOCK_JOB_COMPLETED': |
| 264 | self.assertTrue(error, 'job completed unexpectedly') |
| 265 | self.assert_qmp(event, 'data/type', 'stream') |
| 266 | self.assert_qmp(event, 'data/device', 'drive0') |
| 267 | self.assert_qmp_absent(event, 'data/error') |
| 268 | self.assert_qmp(event, 'data/offset', self.image_len) |
| 269 | self.assert_qmp(event, 'data/len', self.image_len) |
| 270 | completed = True |
| 271 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 272 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 273 | self.vm.shutdown() |
| 274 | |
| 275 | def test_enospc(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 276 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 277 | |
| 278 | result = self.vm.qmp('block-stream', device='drive0', on_error='enospc') |
| 279 | self.assert_qmp(result, 'return', {}) |
| 280 | |
| 281 | completed = False |
| 282 | error = False |
| 283 | while not completed: |
| 284 | for event in self.vm.get_qmp_events(wait=True): |
| 285 | if event['event'] == 'BLOCK_JOB_ERROR': |
| 286 | self.assert_qmp(event, 'data/device', 'drive0') |
| 287 | self.assert_qmp(event, 'data/operation', 'read') |
| 288 | error = True |
| 289 | elif event['event'] == 'BLOCK_JOB_COMPLETED': |
| 290 | self.assertTrue(error, 'job completed unexpectedly') |
| 291 | self.assert_qmp(event, 'data/type', 'stream') |
| 292 | self.assert_qmp(event, 'data/device', 'drive0') |
| 293 | self.assert_qmp(event, 'data/error', 'Input/output error') |
| 294 | self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE) |
| 295 | self.assert_qmp(event, 'data/len', self.image_len) |
| 296 | completed = True |
| 297 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 298 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 299 | self.vm.shutdown() |
| 300 | |
| 301 | class TestENOSPC(TestErrors): |
| 302 | def setUp(self): |
| 303 | self.blkdebug_file = backing_img + ".blkdebug" |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 304 | iotests.create_image(backing_img, TestErrors.image_len) |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 305 | self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28) |
| 306 | qemu_img('create', '-f', iotests.imgfmt, |
| 307 | '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' |
| 308 | % (self.blkdebug_file, backing_img), |
| 309 | test_img) |
| 310 | self.vm = iotests.VM().add_drive(test_img) |
| 311 | self.vm.launch() |
| 312 | |
| 313 | def tearDown(self): |
| 314 | self.vm.shutdown() |
| 315 | os.remove(test_img) |
| 316 | os.remove(backing_img) |
| 317 | os.remove(self.blkdebug_file) |
| 318 | |
| 319 | def test_enospc(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 320 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 321 | |
| 322 | result = self.vm.qmp('block-stream', device='drive0', on_error='enospc') |
| 323 | self.assert_qmp(result, 'return', {}) |
| 324 | |
| 325 | error = False |
| 326 | completed = False |
| 327 | while not completed: |
| 328 | for event in self.vm.get_qmp_events(wait=True): |
| 329 | if event['event'] == 'BLOCK_JOB_ERROR': |
| 330 | self.assert_qmp(event, 'data/device', 'drive0') |
| 331 | self.assert_qmp(event, 'data/operation', 'read') |
| 332 | |
| 333 | result = self.vm.qmp('query-block-jobs') |
| 334 | self.assert_qmp(result, 'return[0]/paused', True) |
| 335 | self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE) |
| 336 | self.assert_qmp(result, 'return[0]/io-status', 'nospace') |
| 337 | |
| 338 | result = self.vm.qmp('block-job-resume', device='drive0') |
| 339 | self.assert_qmp(result, 'return', {}) |
| 340 | |
| 341 | result = self.vm.qmp('query-block-jobs') |
| 342 | self.assert_qmp(result, 'return[0]/paused', False) |
| 343 | self.assert_qmp(result, 'return[0]/io-status', 'ok') |
| 344 | error = True |
| 345 | elif event['event'] == 'BLOCK_JOB_COMPLETED': |
| 346 | self.assertTrue(error, 'job completed unexpectedly') |
| 347 | self.assert_qmp(event, 'data/type', 'stream') |
| 348 | self.assert_qmp(event, 'data/device', 'drive0') |
| 349 | self.assert_qmp_absent(event, 'data/error') |
| 350 | self.assert_qmp(event, 'data/offset', self.image_len) |
| 351 | self.assert_qmp(event, 'data/len', self.image_len) |
| 352 | completed = True |
| 353 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 354 | self.assert_no_active_block_jobs() |
Paolo Bonzini | 90f0b71 | 2012-09-28 17:23:02 +0200 | [diff] [blame] | 355 | self.vm.shutdown() |
Stefan Hajnoczi | 774a885 | 2012-08-28 15:26:49 +0100 | [diff] [blame] | 356 | |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 357 | class TestStreamStop(iotests.QMPTestCase): |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 358 | image_len = 8 * 1024 * 1024 * 1024 # GB |
| 359 | |
| 360 | def setUp(self): |
| 361 | qemu_img('create', backing_img, str(TestStreamStop.image_len)) |
Fam Zheng | 7890111 | 2013-10-30 17:42:28 +0800 | [diff] [blame] | 362 | qemu_io('-c', 'write -P 0x1 0 32M', backing_img) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 363 | qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) |
Fam Zheng | 7890111 | 2013-10-30 17:42:28 +0800 | [diff] [blame] | 364 | qemu_io('-c', 'write -P 0x1 32M 32M', test_img) |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 365 | self.vm = iotests.VM().add_drive("blkdebug::" + test_img) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 366 | self.vm.launch() |
| 367 | |
| 368 | def tearDown(self): |
| 369 | self.vm.shutdown() |
| 370 | os.remove(test_img) |
| 371 | os.remove(backing_img) |
| 372 | |
| 373 | def test_stream_stop(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 374 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 375 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 376 | self.vm.pause_drive('drive0') |
Stefan Hajnoczi | db58f9c | 2012-04-11 16:27:10 +0100 | [diff] [blame] | 377 | result = self.vm.qmp('block-stream', device='drive0') |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 378 | self.assert_qmp(result, 'return', {}) |
| 379 | |
Paolo Bonzini | 0fd05e8 | 2012-06-06 16:23:27 +0200 | [diff] [blame] | 380 | time.sleep(0.1) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 381 | events = self.vm.get_qmp_events(wait=False) |
| 382 | self.assertEqual(events, [], 'unexpected QMP event: %s' % events) |
| 383 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 384 | self.cancel_and_wait(resume=True) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 385 | |
Stefan Hajnoczi | 2499a09 | 2013-05-28 17:11:37 +0200 | [diff] [blame] | 386 | class TestSetSpeed(iotests.QMPTestCase): |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 387 | image_len = 80 * 1024 * 1024 # MB |
| 388 | |
| 389 | def setUp(self): |
| 390 | qemu_img('create', backing_img, str(TestSetSpeed.image_len)) |
Fam Zheng | 7890111 | 2013-10-30 17:42:28 +0800 | [diff] [blame] | 391 | qemu_io('-c', 'write -P 0x1 0 32M', backing_img) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 392 | qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) |
Fam Zheng | 7890111 | 2013-10-30 17:42:28 +0800 | [diff] [blame] | 393 | qemu_io('-c', 'write -P 0x1 32M 32M', test_img) |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 394 | self.vm = iotests.VM().add_drive('blkdebug::' + test_img) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 395 | self.vm.launch() |
| 396 | |
| 397 | def tearDown(self): |
| 398 | self.vm.shutdown() |
| 399 | os.remove(test_img) |
| 400 | os.remove(backing_img) |
| 401 | |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 402 | # This is a short performance test which is not run by default. |
| 403 | # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput" |
| 404 | def perf_test_throughput(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 405 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 406 | |
Stefan Hajnoczi | db58f9c | 2012-04-11 16:27:10 +0100 | [diff] [blame] | 407 | result = self.vm.qmp('block-stream', device='drive0') |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 408 | self.assert_qmp(result, 'return', {}) |
| 409 | |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 410 | result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 411 | self.assert_qmp(result, 'return', {}) |
| 412 | |
Fam Zheng | 9974ad4 | 2014-04-02 13:54:07 +0800 | [diff] [blame] | 413 | self.wait_until_completed() |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 414 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 415 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 416 | |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 417 | def test_set_speed(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 418 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 419 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 420 | self.vm.pause_drive('drive0') |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 421 | result = self.vm.qmp('block-stream', device='drive0') |
| 422 | self.assert_qmp(result, 'return', {}) |
| 423 | |
| 424 | # Default speed is 0 |
| 425 | result = self.vm.qmp('query-block-jobs') |
| 426 | self.assert_qmp(result, 'return[0]/device', 'drive0') |
| 427 | self.assert_qmp(result, 'return[0]/speed', 0) |
| 428 | |
| 429 | result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) |
| 430 | self.assert_qmp(result, 'return', {}) |
| 431 | |
| 432 | # Ensure the speed we set was accepted |
| 433 | result = self.vm.qmp('query-block-jobs') |
| 434 | self.assert_qmp(result, 'return[0]/device', 'drive0') |
| 435 | self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024) |
| 436 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 437 | self.cancel_and_wait(resume=True) |
| 438 | self.vm.pause_drive('drive0') |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 439 | |
| 440 | # Check setting speed in block-stream works |
| 441 | result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024) |
| 442 | self.assert_qmp(result, 'return', {}) |
| 443 | |
| 444 | result = self.vm.qmp('query-block-jobs') |
| 445 | self.assert_qmp(result, 'return[0]/device', 'drive0') |
| 446 | self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024) |
| 447 | |
Fam Zheng | b59b3d5 | 2013-11-20 10:01:56 +0800 | [diff] [blame] | 448 | self.cancel_and_wait(resume=True) |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 449 | |
| 450 | def test_set_speed_invalid(self): |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 451 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 452 | |
| 453 | result = self.vm.qmp('block-stream', device='drive0', speed=-1) |
Kevin Wolf | 58c8cce | 2012-08-15 14:08:56 +0200 | [diff] [blame] | 454 | self.assert_qmp(result, 'error/class', 'GenericError') |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 455 | |
Stefan Hajnoczi | ecc1c88 | 2013-05-28 17:11:34 +0200 | [diff] [blame] | 456 | self.assert_no_active_block_jobs() |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 457 | |
| 458 | result = self.vm.qmp('block-stream', device='drive0') |
| 459 | self.assert_qmp(result, 'return', {}) |
| 460 | |
| 461 | result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1) |
Kevin Wolf | 58c8cce | 2012-08-15 14:08:56 +0200 | [diff] [blame] | 462 | self.assert_qmp(result, 'error/class', 'GenericError') |
Stefan Hajnoczi | e425306 | 2012-04-25 16:51:04 +0100 | [diff] [blame] | 463 | |
| 464 | self.cancel_and_wait() |
| 465 | |
Stefan Hajnoczi | 37ce63e | 2012-02-29 13:25:22 +0000 | [diff] [blame] | 466 | if __name__ == '__main__': |
| 467 | iotests.main(supported_fmts=['qcow2', 'qed']) |