137ce63ebSStefan Hajnoczi#!/usr/bin/env python 237ce63ebSStefan Hajnoczi# 337ce63ebSStefan Hajnoczi# Tests for image streaming. 437ce63ebSStefan Hajnoczi# 537ce63ebSStefan Hajnoczi# Copyright (C) 2012 IBM Corp. 637ce63ebSStefan Hajnoczi# 737ce63ebSStefan Hajnoczi# This program is free software; you can redistribute it and/or modify 837ce63ebSStefan Hajnoczi# it under the terms of the GNU General Public License as published by 937ce63ebSStefan Hajnoczi# the Free Software Foundation; either version 2 of the License, or 1037ce63ebSStefan Hajnoczi# (at your option) any later version. 1137ce63ebSStefan Hajnoczi# 1237ce63ebSStefan Hajnoczi# This program is distributed in the hope that it will be useful, 1337ce63ebSStefan Hajnoczi# but WITHOUT ANY WARRANTY; without even the implied warranty of 1437ce63ebSStefan Hajnoczi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1537ce63ebSStefan Hajnoczi# GNU General Public License for more details. 1637ce63ebSStefan Hajnoczi# 1737ce63ebSStefan Hajnoczi# You should have received a copy of the GNU General Public License 1837ce63ebSStefan Hajnoczi# along with this program. If not, see <http://www.gnu.org/licenses/>. 1937ce63ebSStefan Hajnoczi# 2037ce63ebSStefan Hajnoczi 210c817347SPaolo Bonziniimport time 2237ce63ebSStefan Hajnocziimport os 2337ce63ebSStefan Hajnocziimport iotests 2437ce63ebSStefan Hajnoczifrom iotests import qemu_img, qemu_io 2537ce63ebSStefan Hajnoczi 2637ce63ebSStefan Hajnoczibacking_img = os.path.join(iotests.test_dir, 'backing.img') 276e343609SPaolo Bonzinimid_img = os.path.join(iotests.test_dir, 'mid.img') 2837ce63ebSStefan Hajnoczitest_img = os.path.join(iotests.test_dir, 'test.img') 2937ce63ebSStefan Hajnoczi 302499a096SStefan Hajnocziclass TestSingleDrive(iotests.QMPTestCase): 3137ce63ebSStefan Hajnoczi image_len = 1 * 1024 * 1024 # MB 3237ce63ebSStefan Hajnoczi 3337ce63ebSStefan Hajnoczi def setUp(self): 342499a096SStefan Hajnoczi iotests.create_image(backing_img, TestSingleDrive.image_len) 356e343609SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 366e343609SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 3790c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 512', backing_img) 385e302a7dSAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 524288 512', mid_img) 397b8a9e5aSAlberto Garcia self.vm = iotests.VM().add_drive("blkdebug::" + test_img, "backing.node-name=mid") 4037ce63ebSStefan Hajnoczi self.vm.launch() 4137ce63ebSStefan Hajnoczi 4237ce63ebSStefan Hajnoczi def tearDown(self): 4337ce63ebSStefan Hajnoczi self.vm.shutdown() 4437ce63ebSStefan Hajnoczi os.remove(test_img) 456e343609SPaolo Bonzini os.remove(mid_img) 4637ce63ebSStefan Hajnoczi os.remove(backing_img) 4737ce63ebSStefan Hajnoczi 4837ce63ebSStefan Hajnoczi def test_stream(self): 49ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5037ce63ebSStefan Hajnoczi 51db58f9c0SStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0') 5237ce63ebSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 5337ce63ebSStefan Hajnoczi 549974ad40SFam Zheng self.wait_until_completed() 5537ce63ebSStefan Hajnoczi 56ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 57863a5d04SPaolo Bonzini self.vm.shutdown() 5837ce63ebSStefan Hajnoczi 5990c9b167SKevin Wolf self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img), 6090c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 61efcc7a23SPaolo Bonzini 'image file map does not match backing file after streaming') 6237ce63ebSStefan Hajnoczi 637b8a9e5aSAlberto Garcia def test_stream_intermediate(self): 647b8a9e5aSAlberto Garcia self.assert_no_active_block_jobs() 657b8a9e5aSAlberto Garcia 66aca7063aSFam Zheng self.assertNotEqual(qemu_io('-f', 'raw', '-rU', '-c', 'map', backing_img), 67aca7063aSFam Zheng qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', mid_img), 687b8a9e5aSAlberto Garcia 'image file map matches backing file before streaming') 697b8a9e5aSAlberto Garcia 707b8a9e5aSAlberto Garcia result = self.vm.qmp('block-stream', device='mid', job_id='stream-mid') 717b8a9e5aSAlberto Garcia self.assert_qmp(result, 'return', {}) 727b8a9e5aSAlberto Garcia 737b8a9e5aSAlberto Garcia self.wait_until_completed(drive='stream-mid') 747b8a9e5aSAlberto Garcia 757b8a9e5aSAlberto Garcia self.assert_no_active_block_jobs() 767b8a9e5aSAlberto Garcia self.vm.shutdown() 777b8a9e5aSAlberto Garcia 787b8a9e5aSAlberto Garcia self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img), 797b8a9e5aSAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img), 807b8a9e5aSAlberto Garcia 'image file map does not match backing file after streaming') 817b8a9e5aSAlberto Garcia 820c817347SPaolo Bonzini def test_stream_pause(self): 83ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 840c817347SPaolo Bonzini 85b59b3d57SFam Zheng self.vm.pause_drive('drive0') 860c817347SPaolo Bonzini result = self.vm.qmp('block-stream', device='drive0') 870c817347SPaolo Bonzini self.assert_qmp(result, 'return', {}) 880c817347SPaolo Bonzini 89f03d9d24SJohn Snow self.pause_job('drive0', wait=False) 902c93c5cbSKevin Wolf self.vm.resume_drive('drive0') 91f03d9d24SJohn Snow self.pause_wait('drive0') 922c93c5cbSKevin Wolf 930c817347SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 940c817347SPaolo Bonzini offset = self.dictpath(result, 'return[0]/offset') 950c817347SPaolo Bonzini 962c93c5cbSKevin Wolf time.sleep(0.5) 970c817347SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 980c817347SPaolo Bonzini self.assert_qmp(result, 'return[0]/offset', offset) 990c817347SPaolo Bonzini 1000c817347SPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 1010c817347SPaolo Bonzini self.assert_qmp(result, 'return', {}) 1020c817347SPaolo Bonzini 1039974ad40SFam Zheng self.wait_until_completed() 1040c817347SPaolo Bonzini 105ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 1060c817347SPaolo Bonzini self.vm.shutdown() 1070c817347SPaolo Bonzini 10890c9b167SKevin Wolf self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img), 10990c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 1100c817347SPaolo Bonzini 'image file map does not match backing file after streaming') 1110c817347SPaolo Bonzini 112409d5498SAlberto Garcia def test_stream_no_op(self): 113409d5498SAlberto Garcia self.assert_no_active_block_jobs() 114409d5498SAlberto Garcia 115409d5498SAlberto Garcia # The image map is empty before the operation 116aca7063aSFam Zheng empty_map = qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', test_img) 117409d5498SAlberto Garcia 118409d5498SAlberto Garcia # This is a no-op: no data should ever be copied from the base image 119409d5498SAlberto Garcia result = self.vm.qmp('block-stream', device='drive0', base=mid_img) 120409d5498SAlberto Garcia self.assert_qmp(result, 'return', {}) 121409d5498SAlberto Garcia 122409d5498SAlberto Garcia self.wait_until_completed() 123409d5498SAlberto Garcia 124409d5498SAlberto Garcia self.assert_no_active_block_jobs() 125409d5498SAlberto Garcia self.vm.shutdown() 126409d5498SAlberto Garcia 127409d5498SAlberto Garcia self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 128409d5498SAlberto Garcia empty_map, 'image file map changed after a no-op') 129409d5498SAlberto Garcia 1306e343609SPaolo Bonzini def test_stream_partial(self): 131ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 1326e343609SPaolo Bonzini 1335e302a7dSAlberto Garcia result = self.vm.qmp('block-stream', device='drive0', base=backing_img) 1346e343609SPaolo Bonzini self.assert_qmp(result, 'return', {}) 1356e343609SPaolo Bonzini 1369974ad40SFam Zheng self.wait_until_completed() 1376e343609SPaolo Bonzini 138ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 1396e343609SPaolo Bonzini self.vm.shutdown() 1406e343609SPaolo Bonzini 14190c9b167SKevin Wolf self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img), 14290c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), 1436e343609SPaolo Bonzini 'image file map does not match backing file after streaming') 1446e343609SPaolo Bonzini 14537ce63ebSStefan Hajnoczi def test_device_not_found(self): 146db58f9c0SStefan Hajnoczi result = self.vm.qmp('block-stream', device='nonexistent') 147b6c1bae5SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 14837ce63ebSStefan Hajnoczi 1490bb0aea4SKevin Wolf def test_job_id_missing(self): 1500bb0aea4SKevin Wolf result = self.vm.qmp('block-stream', device='mid') 1510bb0aea4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 1520bb0aea4SKevin Wolf 153774a8850SStefan Hajnoczi 154c1a34322SAlberto Garciaclass TestParallelOps(iotests.QMPTestCase): 155c1a34322SAlberto Garcia num_ops = 4 # Number of parallel block-stream operations 156c1a34322SAlberto Garcia num_imgs = num_ops * 2 + 1 157*7229e121SMax Reitz image_len = num_ops * 4 * 1024 * 1024 158c1a34322SAlberto Garcia imgs = [] 159c1a34322SAlberto Garcia 160c1a34322SAlberto Garcia def setUp(self): 161c1a34322SAlberto Garcia opts = [] 162c1a34322SAlberto Garcia self.imgs = [] 163c1a34322SAlberto Garcia 164c1a34322SAlberto Garcia # Initialize file names and command-line options 165c1a34322SAlberto Garcia for i in range(self.num_imgs): 166c1a34322SAlberto Garcia img_depth = self.num_imgs - i - 1 167c1a34322SAlberto Garcia opts.append("backing." * img_depth + "node-name=node%d" % i) 168c1a34322SAlberto Garcia self.imgs.append(os.path.join(iotests.test_dir, 'img-%d.img' % i)) 169c1a34322SAlberto Garcia 170c1a34322SAlberto Garcia # Create all images 171c1a34322SAlberto Garcia iotests.create_image(self.imgs[0], self.image_len) 172c1a34322SAlberto Garcia for i in range(1, self.num_imgs): 173c1a34322SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, 174c1a34322SAlberto Garcia '-o', 'backing_file=%s' % self.imgs[i-1], self.imgs[i]) 175c1a34322SAlberto Garcia 176c1a34322SAlberto Garcia # Put data into the images we are copying data from 17739eaefceSAlberto Garcia odd_img_indexes = [x for x in reversed(range(self.num_imgs)) if x % 2 == 1] 17839eaefceSAlberto Garcia for i in range(len(odd_img_indexes)): 179*7229e121SMax Reitz # Alternate between 2MB and 4MB. 180c1a34322SAlberto Garcia # This way jobs will not finish in the same order they were created 181*7229e121SMax Reitz num_mb = 2 + 2 * (i % 2) 182c1a34322SAlberto Garcia qemu_io('-f', iotests.imgfmt, 183*7229e121SMax Reitz '-c', 'write -P 0xFF %dM %dM' % (i * 4, num_mb), 18439eaefceSAlberto Garcia self.imgs[odd_img_indexes[i]]) 185c1a34322SAlberto Garcia 186c1a34322SAlberto Garcia # Attach the drive to the VM 187c1a34322SAlberto Garcia self.vm = iotests.VM() 188c1a34322SAlberto Garcia self.vm.add_drive(self.imgs[-1], ','.join(opts)) 189c1a34322SAlberto Garcia self.vm.launch() 190c1a34322SAlberto Garcia 191c1a34322SAlberto Garcia def tearDown(self): 192c1a34322SAlberto Garcia self.vm.shutdown() 193c1a34322SAlberto Garcia for img in self.imgs: 194c1a34322SAlberto Garcia os.remove(img) 195c1a34322SAlberto Garcia 196c1a34322SAlberto Garcia # Test that it's possible to run several block-stream operations 197c1a34322SAlberto Garcia # in parallel in the same snapshot chain 198c1a34322SAlberto Garcia def test_stream_parallel(self): 199c1a34322SAlberto Garcia self.assert_no_active_block_jobs() 200c1a34322SAlberto Garcia 201c1a34322SAlberto Garcia # Check that the maps don't match before the streaming operations 202c1a34322SAlberto Garcia for i in range(2, self.num_imgs, 2): 203aca7063aSFam Zheng self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i]), 204aca7063aSFam Zheng qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i-1]), 205c1a34322SAlberto Garcia 'image file map matches backing file before streaming') 206c1a34322SAlberto Garcia 207c1a34322SAlberto Garcia # Create all streaming jobs 208c1a34322SAlberto Garcia pending_jobs = [] 209c1a34322SAlberto Garcia for i in range(2, self.num_imgs, 2): 210c1a34322SAlberto Garcia node_name = 'node%d' % i 211c1a34322SAlberto Garcia job_id = 'stream-%s' % node_name 212c1a34322SAlberto Garcia pending_jobs.append(job_id) 213c1a34322SAlberto Garcia result = self.vm.qmp('block-stream', device=node_name, job_id=job_id, base=self.imgs[i-2], speed=512*1024) 214c1a34322SAlberto Garcia self.assert_qmp(result, 'return', {}) 215c1a34322SAlberto Garcia 216*7229e121SMax Reitz for job in pending_jobs: 217*7229e121SMax Reitz result = self.vm.qmp('block-job-set-speed', device=job, speed=0) 218*7229e121SMax Reitz self.assert_qmp(result, 'return', {}) 219*7229e121SMax Reitz 220c1a34322SAlberto Garcia # Wait for all jobs to be finished. 221c1a34322SAlberto Garcia while len(pending_jobs) > 0: 222c1a34322SAlberto Garcia for event in self.vm.get_qmp_events(wait=True): 223c1a34322SAlberto Garcia if event['event'] == 'BLOCK_JOB_COMPLETED': 224c1a34322SAlberto Garcia job_id = self.dictpath(event, 'data/device') 225c1a34322SAlberto Garcia self.assertTrue(job_id in pending_jobs) 226c1a34322SAlberto Garcia self.assert_qmp_absent(event, 'data/error') 227c1a34322SAlberto Garcia pending_jobs.remove(job_id) 228c1a34322SAlberto Garcia 229c1a34322SAlberto Garcia self.assert_no_active_block_jobs() 230c1a34322SAlberto Garcia self.vm.shutdown() 231c1a34322SAlberto Garcia 232c1a34322SAlberto Garcia # Check that all maps match now 233c1a34322SAlberto Garcia for i in range(2, self.num_imgs, 2): 234c1a34322SAlberto Garcia self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i]), 235c1a34322SAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i-1]), 236c1a34322SAlberto Garcia 'image file map does not match backing file after streaming') 237c1a34322SAlberto Garcia 238eb290b78SAlberto Garcia # Test that it's not possible to perform two block-stream 239eb290b78SAlberto Garcia # operations if there are nodes involved in both. 240eb290b78SAlberto Garcia def test_overlapping_1(self): 241eb290b78SAlberto Garcia self.assert_no_active_block_jobs() 242eb290b78SAlberto Garcia 243eb290b78SAlberto Garcia # Set a speed limit to make sure that this job blocks the rest 244eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4', base=self.imgs[1], speed=1024*1024) 245eb290b78SAlberto Garcia self.assert_qmp(result, 'return', {}) 246eb290b78SAlberto Garcia 247eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node5', job_id='stream-node5', base=self.imgs[2]) 248eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 249eb290b78SAlberto Garcia 250eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3', base=self.imgs[2]) 251eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 252eb290b78SAlberto Garcia 253eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4-v2') 254eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 255eb290b78SAlberto Garcia 256eb290b78SAlberto Garcia # block-commit should also fail if it touches nodes used by the stream job 257eb290b78SAlberto Garcia result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[4], job_id='commit-node4') 258eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 259eb290b78SAlberto Garcia 260eb290b78SAlberto Garcia result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[1], top=self.imgs[3], job_id='commit-node1') 261eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 262eb290b78SAlberto Garcia 263eb290b78SAlberto Garcia # This fails because it needs to modify the backing string in node2, which is blocked 264eb290b78SAlberto Garcia result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[0], top=self.imgs[1], job_id='commit-node0') 265eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 266eb290b78SAlberto Garcia 267*7229e121SMax Reitz result = self.vm.qmp('block-job-set-speed', device='stream-node4', speed=0) 268*7229e121SMax Reitz self.assert_qmp(result, 'return', {}) 269*7229e121SMax Reitz 270eb290b78SAlberto Garcia self.wait_until_completed(drive='stream-node4') 271eb290b78SAlberto Garcia self.assert_no_active_block_jobs() 272eb290b78SAlberto Garcia 273eb290b78SAlberto Garcia # Similar to test_overlapping_1, but with block-commit 274eb290b78SAlberto Garcia # blocking the other jobs 275eb290b78SAlberto Garcia def test_overlapping_2(self): 276eb290b78SAlberto Garcia self.assertLessEqual(9, self.num_imgs) 277eb290b78SAlberto Garcia self.assert_no_active_block_jobs() 278eb290b78SAlberto Garcia 279eb290b78SAlberto Garcia # Set a speed limit to make sure that this job blocks the rest 280eb290b78SAlberto Garcia result = self.vm.qmp('block-commit', device='drive0', top=self.imgs[5], base=self.imgs[3], job_id='commit-node3', speed=1024*1024) 281eb290b78SAlberto Garcia self.assert_qmp(result, 'return', {}) 282eb290b78SAlberto Garcia 283eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3') 284eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 285eb290b78SAlberto Garcia 286eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node6', base=self.imgs[2], job_id='stream-node6') 287eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 288eb290b78SAlberto Garcia 289eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], job_id='stream-node4') 290eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 291eb290b78SAlberto Garcia 292eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node6', base=self.imgs[4], job_id='stream-node6-v2') 293eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 294eb290b78SAlberto Garcia 295eb290b78SAlberto Garcia # This fails because block-commit currently blocks the active layer even if it's not used 296eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='drive0', base=self.imgs[5], job_id='stream-drive0') 297eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 298eb290b78SAlberto Garcia 299*7229e121SMax Reitz result = self.vm.qmp('block-job-set-speed', device='commit-node3', speed=0) 300*7229e121SMax Reitz self.assert_qmp(result, 'return', {}) 301*7229e121SMax Reitz 302eb290b78SAlberto Garcia self.wait_until_completed(drive='commit-node3') 303eb290b78SAlberto Garcia 304eb290b78SAlberto Garcia # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter. 305eb290b78SAlberto Garcia # Internally this uses a mirror block job, hence the separate test case. 306eb290b78SAlberto Garcia def test_overlapping_3(self): 307eb290b78SAlberto Garcia self.assertLessEqual(8, self.num_imgs) 308eb290b78SAlberto Garcia self.assert_no_active_block_jobs() 309eb290b78SAlberto Garcia 310eb290b78SAlberto Garcia # Set a speed limit to make sure that this job blocks the rest 311eb290b78SAlberto Garcia result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3], job_id='commit-drive0', speed=1024*1024) 312eb290b78SAlberto Garcia self.assert_qmp(result, 'return', {}) 313eb290b78SAlberto Garcia 314eb290b78SAlberto Garcia result = self.vm.qmp('block-stream', device='node5', base=self.imgs[3], job_id='stream-node6') 315eb290b78SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 316eb290b78SAlberto Garcia 3171dac83f1SKevin Wolf event = self.vm.event_wait(name='BLOCK_JOB_READY') 318eb290b78SAlberto Garcia self.assert_qmp(event, 'data/device', 'commit-drive0') 319eb290b78SAlberto Garcia self.assert_qmp(event, 'data/type', 'commit') 320eb290b78SAlberto Garcia self.assert_qmp_absent(event, 'data/error') 321eb290b78SAlberto Garcia 322*7229e121SMax Reitz result = self.vm.qmp('block-job-set-speed', device='commit-drive0', speed=0) 323*7229e121SMax Reitz self.assert_qmp(result, 'return', {}) 324*7229e121SMax Reitz 325eb290b78SAlberto Garcia result = self.vm.qmp('block-job-complete', device='commit-drive0') 326eb290b78SAlberto Garcia self.assert_qmp(result, 'return', {}) 327eb290b78SAlberto Garcia 328eb290b78SAlberto Garcia self.wait_until_completed(drive='commit-drive0') 329704d59f1SAlberto Garcia 330d20ba603SAlberto Garcia # In this case the base node of the stream job is the same as the 331d20ba603SAlberto Garcia # top node of commit job. Since block-commit removes the top node 332d20ba603SAlberto Garcia # when it finishes, this is not allowed. 333d20ba603SAlberto Garcia def test_overlapping_4(self): 334d20ba603SAlberto Garcia self.assert_no_active_block_jobs() 335d20ba603SAlberto Garcia 336d20ba603SAlberto Garcia # Commit from node2 into node0 337*7229e121SMax Reitz result = self.vm.qmp('block-commit', device='drive0', 338*7229e121SMax Reitz top=self.imgs[2], base=self.imgs[0], 339*7229e121SMax Reitz speed=1024*1024) 340d20ba603SAlberto Garcia self.assert_qmp(result, 'return', {}) 341d20ba603SAlberto Garcia 342d20ba603SAlberto Garcia # Stream from node2 into node4 343d20ba603SAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='node4') 344d20ba603SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 345d20ba603SAlberto Garcia 346*7229e121SMax Reitz result = self.vm.qmp('block-job-set-speed', device='drive0', speed=0) 347*7229e121SMax Reitz self.assert_qmp(result, 'return', {}) 348*7229e121SMax Reitz 349d20ba603SAlberto Garcia self.wait_until_completed() 350d20ba603SAlberto Garcia self.assert_no_active_block_jobs() 351d20ba603SAlberto Garcia 352704d59f1SAlberto Garcia # Test a block-stream and a block-commit job in parallel 35339eaefceSAlberto Garcia # Here the stream job is supposed to finish quickly in order to reproduce 35439eaefceSAlberto Garcia # the scenario that triggers the bug fixed in 3d5d319e1221 and 1a63a907507 35539eaefceSAlberto Garcia def test_stream_commit_1(self): 356704d59f1SAlberto Garcia self.assertLessEqual(8, self.num_imgs) 357704d59f1SAlberto Garcia self.assert_no_active_block_jobs() 358704d59f1SAlberto Garcia 359704d59f1SAlberto Garcia # Stream from node0 into node2 36039eaefceSAlberto Garcia result = self.vm.qmp('block-stream', device='node2', base_node='node0', job_id='node2') 361704d59f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 362704d59f1SAlberto Garcia 363704d59f1SAlberto Garcia # Commit from the active layer into node3 364704d59f1SAlberto Garcia result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3]) 365704d59f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 366704d59f1SAlberto Garcia 367704d59f1SAlberto Garcia # Wait for all jobs to be finished. 368704d59f1SAlberto Garcia pending_jobs = ['node2', 'drive0'] 369704d59f1SAlberto Garcia while len(pending_jobs) > 0: 370704d59f1SAlberto Garcia for event in self.vm.get_qmp_events(wait=True): 371704d59f1SAlberto Garcia if event['event'] == 'BLOCK_JOB_COMPLETED': 372704d59f1SAlberto Garcia node_name = self.dictpath(event, 'data/device') 373704d59f1SAlberto Garcia self.assertTrue(node_name in pending_jobs) 374704d59f1SAlberto Garcia self.assert_qmp_absent(event, 'data/error') 375704d59f1SAlberto Garcia pending_jobs.remove(node_name) 376704d59f1SAlberto Garcia if event['event'] == 'BLOCK_JOB_READY': 377704d59f1SAlberto Garcia self.assert_qmp(event, 'data/device', 'drive0') 378704d59f1SAlberto Garcia self.assert_qmp(event, 'data/type', 'commit') 379704d59f1SAlberto Garcia self.assert_qmp_absent(event, 'data/error') 380704d59f1SAlberto Garcia self.assertTrue('drive0' in pending_jobs) 381704d59f1SAlberto Garcia self.vm.qmp('block-job-complete', device='drive0') 382704d59f1SAlberto Garcia 383eb290b78SAlberto Garcia self.assert_no_active_block_jobs() 384eb290b78SAlberto Garcia 38539eaefceSAlberto Garcia # This is similar to test_stream_commit_1 but both jobs are slowed 38639eaefceSAlberto Garcia # down so they can run in parallel for a little while. 38739eaefceSAlberto Garcia def test_stream_commit_2(self): 38839eaefceSAlberto Garcia self.assertLessEqual(8, self.num_imgs) 38939eaefceSAlberto Garcia self.assert_no_active_block_jobs() 39039eaefceSAlberto Garcia 39139eaefceSAlberto Garcia # Stream from node0 into node4 39239eaefceSAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base_node='node0', job_id='node4', speed=1024*1024) 39339eaefceSAlberto Garcia self.assert_qmp(result, 'return', {}) 39439eaefceSAlberto Garcia 39539eaefceSAlberto Garcia # Commit from the active layer into node5 39639eaefceSAlberto Garcia result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[5], speed=1024*1024) 39739eaefceSAlberto Garcia self.assert_qmp(result, 'return', {}) 39839eaefceSAlberto Garcia 399*7229e121SMax Reitz for job in ['drive0', 'node4']: 400*7229e121SMax Reitz result = self.vm.qmp('block-job-set-speed', device=job, speed=0) 401*7229e121SMax Reitz self.assert_qmp(result, 'return', {}) 402*7229e121SMax Reitz 40339eaefceSAlberto Garcia # Wait for all jobs to be finished. 40439eaefceSAlberto Garcia pending_jobs = ['node4', 'drive0'] 40539eaefceSAlberto Garcia while len(pending_jobs) > 0: 40639eaefceSAlberto Garcia for event in self.vm.get_qmp_events(wait=True): 40739eaefceSAlberto Garcia if event['event'] == 'BLOCK_JOB_COMPLETED': 40839eaefceSAlberto Garcia node_name = self.dictpath(event, 'data/device') 40939eaefceSAlberto Garcia self.assertTrue(node_name in pending_jobs) 41039eaefceSAlberto Garcia self.assert_qmp_absent(event, 'data/error') 41139eaefceSAlberto Garcia pending_jobs.remove(node_name) 41239eaefceSAlberto Garcia if event['event'] == 'BLOCK_JOB_READY': 41339eaefceSAlberto Garcia self.assert_qmp(event, 'data/device', 'drive0') 41439eaefceSAlberto Garcia self.assert_qmp(event, 'data/type', 'commit') 41539eaefceSAlberto Garcia self.assert_qmp_absent(event, 'data/error') 41639eaefceSAlberto Garcia self.assertTrue('drive0' in pending_jobs) 41739eaefceSAlberto Garcia self.vm.qmp('block-job-complete', device='drive0') 41839eaefceSAlberto Garcia 41939eaefceSAlberto Garcia self.assert_no_active_block_jobs() 42039eaefceSAlberto Garcia 4217eb13c9dSAlberto Garcia # Test the base_node parameter 4227eb13c9dSAlberto Garcia def test_stream_base_node_name(self): 4237eb13c9dSAlberto Garcia self.assert_no_active_block_jobs() 4247eb13c9dSAlberto Garcia 425aca7063aSFam Zheng self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]), 426aca7063aSFam Zheng qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]), 4277eb13c9dSAlberto Garcia 'image file map matches backing file before streaming') 4287eb13c9dSAlberto Garcia 4297eb13c9dSAlberto Garcia # Error: the base node does not exist 4307eb13c9dSAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream') 4317eb13c9dSAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 4327eb13c9dSAlberto Garcia 4337eb13c9dSAlberto Garcia # Error: the base node is not a backing file of the top node 4347eb13c9dSAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream') 4357eb13c9dSAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 4367eb13c9dSAlberto Garcia 4377eb13c9dSAlberto Garcia # Error: the base node is the same as the top node 4387eb13c9dSAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream') 4397eb13c9dSAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 4407eb13c9dSAlberto Garcia 4417eb13c9dSAlberto Garcia # Error: cannot specify 'base' and 'base-node' at the same time 4427eb13c9dSAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream') 4437eb13c9dSAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 4447eb13c9dSAlberto Garcia 4457eb13c9dSAlberto Garcia # Success: the base node is a backing file of the top node 4467eb13c9dSAlberto Garcia result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream') 4477eb13c9dSAlberto Garcia self.assert_qmp(result, 'return', {}) 4487eb13c9dSAlberto Garcia 4497eb13c9dSAlberto Garcia self.wait_until_completed(drive='stream') 4507eb13c9dSAlberto Garcia 4517eb13c9dSAlberto Garcia self.assert_no_active_block_jobs() 4527eb13c9dSAlberto Garcia self.vm.shutdown() 4537eb13c9dSAlberto Garcia 4547eb13c9dSAlberto Garcia self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]), 4557eb13c9dSAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]), 4567eb13c9dSAlberto Garcia 'image file map matches backing file after streaming') 4577eb13c9dSAlberto Garcia 45848361afbSAlberto Garciaclass TestQuorum(iotests.QMPTestCase): 45948361afbSAlberto Garcia num_children = 3 46048361afbSAlberto Garcia children = [] 46148361afbSAlberto Garcia backing = [] 46248361afbSAlberto Garcia 46348361afbSAlberto Garcia def setUp(self): 46448361afbSAlberto Garcia opts = ['driver=quorum', 'vote-threshold=2'] 46548361afbSAlberto Garcia 46648361afbSAlberto Garcia # Initialize file names and command-line options 46748361afbSAlberto Garcia for i in range(self.num_children): 46848361afbSAlberto Garcia child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i) 46948361afbSAlberto Garcia backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i) 47048361afbSAlberto Garcia self.children.append(child_img) 47148361afbSAlberto Garcia self.backing.append(backing_img) 47248361afbSAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M') 47348361afbSAlberto Garcia qemu_io('-f', iotests.imgfmt, 47448361afbSAlberto Garcia '-c', 'write -P 0x55 0 1024', backing_img) 47548361afbSAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, 47648361afbSAlberto Garcia '-o', 'backing_file=%s' % backing_img, child_img) 47748361afbSAlberto Garcia opts.append("children.%d.file.filename=%s" % (i, child_img)) 47848361afbSAlberto Garcia opts.append("children.%d.node-name=node%d" % (i, i)) 47948361afbSAlberto Garcia 48048361afbSAlberto Garcia # Attach the drive to the VM 48148361afbSAlberto Garcia self.vm = iotests.VM() 48248361afbSAlberto Garcia self.vm.add_drive(path = None, opts = ','.join(opts)) 48348361afbSAlberto Garcia self.vm.launch() 48448361afbSAlberto Garcia 48548361afbSAlberto Garcia def tearDown(self): 48648361afbSAlberto Garcia self.vm.shutdown() 48748361afbSAlberto Garcia for img in self.children: 48848361afbSAlberto Garcia os.remove(img) 48948361afbSAlberto Garcia for img in self.backing: 49048361afbSAlberto Garcia os.remove(img) 49148361afbSAlberto Garcia 49248361afbSAlberto Garcia def test_stream_quorum(self): 49348361afbSAlberto Garcia if not iotests.supports_quorum(): 49448361afbSAlberto Garcia return 49548361afbSAlberto Garcia 496aca7063aSFam Zheng self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]), 497aca7063aSFam Zheng qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]), 49848361afbSAlberto Garcia 'image file map matches backing file before streaming') 49948361afbSAlberto Garcia 50048361afbSAlberto Garcia self.assert_no_active_block_jobs() 50148361afbSAlberto Garcia 50248361afbSAlberto Garcia result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0') 50348361afbSAlberto Garcia self.assert_qmp(result, 'return', {}) 50448361afbSAlberto Garcia 50548361afbSAlberto Garcia self.wait_until_completed(drive='stream-node0') 50648361afbSAlberto Garcia 50748361afbSAlberto Garcia self.assert_no_active_block_jobs() 50848361afbSAlberto Garcia self.vm.shutdown() 50948361afbSAlberto Garcia 51048361afbSAlberto Garcia self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]), 51148361afbSAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]), 51248361afbSAlberto Garcia 'image file map does not match backing file after streaming') 51348361afbSAlberto Garcia 5142499a096SStefan Hajnocziclass TestSmallerBackingFile(iotests.QMPTestCase): 515774a8850SStefan Hajnoczi backing_len = 1 * 1024 * 1024 # MB 516774a8850SStefan Hajnoczi image_len = 2 * backing_len 517774a8850SStefan Hajnoczi 518774a8850SStefan Hajnoczi def setUp(self): 5192499a096SStefan Hajnoczi iotests.create_image(backing_img, self.backing_len) 520774a8850SStefan Hajnoczi qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len)) 521774a8850SStefan Hajnoczi self.vm = iotests.VM().add_drive(test_img) 522774a8850SStefan Hajnoczi self.vm.launch() 523774a8850SStefan Hajnoczi 524774a8850SStefan Hajnoczi # If this hangs, then you are missing a fix to complete streaming when the 525774a8850SStefan Hajnoczi # end of the backing file is reached. 526774a8850SStefan Hajnoczi def test_stream(self): 527ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 528774a8850SStefan Hajnoczi 529774a8850SStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0') 530774a8850SStefan Hajnoczi self.assert_qmp(result, 'return', {}) 531774a8850SStefan Hajnoczi 5329974ad40SFam Zheng self.wait_until_completed() 533774a8850SStefan Hajnoczi 534ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 535774a8850SStefan Hajnoczi self.vm.shutdown() 536774a8850SStefan Hajnoczi 5372499a096SStefan Hajnocziclass TestErrors(iotests.QMPTestCase): 53890f0b711SPaolo Bonzini image_len = 2 * 1024 * 1024 # MB 53990f0b711SPaolo Bonzini 54090f0b711SPaolo Bonzini # this should match STREAM_BUFFER_SIZE/512 in block/stream.c 54190f0b711SPaolo Bonzini STREAM_BUFFER_SIZE = 512 * 1024 54290f0b711SPaolo Bonzini 54390f0b711SPaolo Bonzini def create_blkdebug_file(self, name, event, errno): 54490f0b711SPaolo Bonzini file = open(name, 'w') 54590f0b711SPaolo Bonzini file.write(''' 54690f0b711SPaolo Bonzini[inject-error] 54790f0b711SPaolo Bonzinistate = "1" 54890f0b711SPaolo Bonzinievent = "%s" 54990f0b711SPaolo Bonzinierrno = "%d" 55090f0b711SPaolo Bonziniimmediately = "off" 55190f0b711SPaolo Bonzinionce = "on" 55290f0b711SPaolo Bonzinisector = "%d" 55390f0b711SPaolo Bonzini 55490f0b711SPaolo Bonzini[set-state] 55590f0b711SPaolo Bonzinistate = "1" 55690f0b711SPaolo Bonzinievent = "%s" 55790f0b711SPaolo Bonzininew_state = "2" 55890f0b711SPaolo Bonzini 55990f0b711SPaolo Bonzini[set-state] 56090f0b711SPaolo Bonzinistate = "2" 56190f0b711SPaolo Bonzinievent = "%s" 56290f0b711SPaolo Bonzininew_state = "1" 5639a3a9a63SMax Reitz''' % (event, errno, self.STREAM_BUFFER_SIZE // 512, event, event)) 56490f0b711SPaolo Bonzini file.close() 56590f0b711SPaolo Bonzini 56690f0b711SPaolo Bonziniclass TestEIO(TestErrors): 56790f0b711SPaolo Bonzini def setUp(self): 56890f0b711SPaolo Bonzini self.blkdebug_file = backing_img + ".blkdebug" 5692499a096SStefan Hajnoczi iotests.create_image(backing_img, TestErrors.image_len) 57090f0b711SPaolo Bonzini self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5) 57190f0b711SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, 57290f0b711SPaolo Bonzini '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' 57390f0b711SPaolo Bonzini % (self.blkdebug_file, backing_img), 57490f0b711SPaolo Bonzini test_img) 57590f0b711SPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 57690f0b711SPaolo Bonzini self.vm.launch() 57790f0b711SPaolo Bonzini 57890f0b711SPaolo Bonzini def tearDown(self): 57990f0b711SPaolo Bonzini self.vm.shutdown() 58090f0b711SPaolo Bonzini os.remove(test_img) 58190f0b711SPaolo Bonzini os.remove(backing_img) 58290f0b711SPaolo Bonzini os.remove(self.blkdebug_file) 58390f0b711SPaolo Bonzini 58490f0b711SPaolo Bonzini def test_report(self): 585ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 58690f0b711SPaolo Bonzini 58790f0b711SPaolo Bonzini result = self.vm.qmp('block-stream', device='drive0') 58890f0b711SPaolo Bonzini self.assert_qmp(result, 'return', {}) 58990f0b711SPaolo Bonzini 59090f0b711SPaolo Bonzini completed = False 59190f0b711SPaolo Bonzini error = False 59290f0b711SPaolo Bonzini while not completed: 59390f0b711SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 59490f0b711SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 59590f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 59690f0b711SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 59790f0b711SPaolo Bonzini error = True 59890f0b711SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 59990f0b711SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 60090f0b711SPaolo Bonzini self.assert_qmp(event, 'data/type', 'stream') 60190f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 60290f0b711SPaolo Bonzini self.assert_qmp(event, 'data/error', 'Input/output error') 60390f0b711SPaolo Bonzini self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE) 60490f0b711SPaolo Bonzini self.assert_qmp(event, 'data/len', self.image_len) 60590f0b711SPaolo Bonzini completed = True 6061dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 6071dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 60890f0b711SPaolo Bonzini 609ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 61090f0b711SPaolo Bonzini self.vm.shutdown() 61190f0b711SPaolo Bonzini 61290f0b711SPaolo Bonzini def test_ignore(self): 613ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 61490f0b711SPaolo Bonzini 61590f0b711SPaolo Bonzini result = self.vm.qmp('block-stream', device='drive0', on_error='ignore') 61690f0b711SPaolo Bonzini self.assert_qmp(result, 'return', {}) 61790f0b711SPaolo Bonzini 61890f0b711SPaolo Bonzini error = False 61990f0b711SPaolo Bonzini completed = False 62090f0b711SPaolo Bonzini while not completed: 62190f0b711SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 62290f0b711SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 6232c3b44daSJohn Snow error = True 62490f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 62590f0b711SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 62690f0b711SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 6272c3b44daSJohn Snow if result == {'return': []}: 6282c3b44daSJohn Snow # Job finished too quickly 6292c3b44daSJohn Snow continue 63090f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 63190f0b711SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 63290f0b711SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 63390f0b711SPaolo Bonzini self.assert_qmp(event, 'data/type', 'stream') 63490f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 63590f0b711SPaolo Bonzini self.assert_qmp(event, 'data/error', 'Input/output error') 63690f0b711SPaolo Bonzini self.assert_qmp(event, 'data/offset', self.image_len) 63790f0b711SPaolo Bonzini self.assert_qmp(event, 'data/len', self.image_len) 63890f0b711SPaolo Bonzini completed = True 6391dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 6401dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 64190f0b711SPaolo Bonzini 642ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 64390f0b711SPaolo Bonzini self.vm.shutdown() 64490f0b711SPaolo Bonzini 64590f0b711SPaolo Bonzini def test_stop(self): 646ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 64790f0b711SPaolo Bonzini 64890f0b711SPaolo Bonzini result = self.vm.qmp('block-stream', device='drive0', on_error='stop') 64990f0b711SPaolo Bonzini self.assert_qmp(result, 'return', {}) 65090f0b711SPaolo Bonzini 65190f0b711SPaolo Bonzini error = False 65290f0b711SPaolo Bonzini completed = False 65390f0b711SPaolo Bonzini while not completed: 65490f0b711SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 65590f0b711SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 65601809194SJohn Snow error = True 65790f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 65890f0b711SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 65990f0b711SPaolo Bonzini 66090f0b711SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 66190f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', True) 66290f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE) 66390f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'failed') 66490f0b711SPaolo Bonzini 66590f0b711SPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 66690f0b711SPaolo Bonzini self.assert_qmp(result, 'return', {}) 66790f0b711SPaolo Bonzini 66890f0b711SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 66901809194SJohn Snow if result == {'return': []}: 67001809194SJohn Snow # Race; likely already finished. Check. 67101809194SJohn Snow continue 67290f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 67390f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'ok') 67490f0b711SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 67590f0b711SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 67690f0b711SPaolo Bonzini self.assert_qmp(event, 'data/type', 'stream') 67790f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 67890f0b711SPaolo Bonzini self.assert_qmp_absent(event, 'data/error') 67990f0b711SPaolo Bonzini self.assert_qmp(event, 'data/offset', self.image_len) 68090f0b711SPaolo Bonzini self.assert_qmp(event, 'data/len', self.image_len) 68190f0b711SPaolo Bonzini completed = True 6821dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 6831dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 68490f0b711SPaolo Bonzini 685ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 68690f0b711SPaolo Bonzini self.vm.shutdown() 68790f0b711SPaolo Bonzini 68890f0b711SPaolo Bonzini def test_enospc(self): 689ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 69090f0b711SPaolo Bonzini 69190f0b711SPaolo Bonzini result = self.vm.qmp('block-stream', device='drive0', on_error='enospc') 69290f0b711SPaolo Bonzini self.assert_qmp(result, 'return', {}) 69390f0b711SPaolo Bonzini 69490f0b711SPaolo Bonzini completed = False 69590f0b711SPaolo Bonzini error = False 69690f0b711SPaolo Bonzini while not completed: 69790f0b711SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 69890f0b711SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 69990f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 70090f0b711SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 70190f0b711SPaolo Bonzini error = True 70290f0b711SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 70390f0b711SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 70490f0b711SPaolo Bonzini self.assert_qmp(event, 'data/type', 'stream') 70590f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 70690f0b711SPaolo Bonzini self.assert_qmp(event, 'data/error', 'Input/output error') 70790f0b711SPaolo Bonzini self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE) 70890f0b711SPaolo Bonzini self.assert_qmp(event, 'data/len', self.image_len) 70990f0b711SPaolo Bonzini completed = True 7101dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 7111dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 71290f0b711SPaolo Bonzini 713ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 71490f0b711SPaolo Bonzini self.vm.shutdown() 71590f0b711SPaolo Bonzini 71690f0b711SPaolo Bonziniclass TestENOSPC(TestErrors): 71790f0b711SPaolo Bonzini def setUp(self): 71890f0b711SPaolo Bonzini self.blkdebug_file = backing_img + ".blkdebug" 7192499a096SStefan Hajnoczi iotests.create_image(backing_img, TestErrors.image_len) 72090f0b711SPaolo Bonzini self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28) 72190f0b711SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, 72290f0b711SPaolo Bonzini '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' 72390f0b711SPaolo Bonzini % (self.blkdebug_file, backing_img), 72490f0b711SPaolo Bonzini test_img) 72590f0b711SPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 72690f0b711SPaolo Bonzini self.vm.launch() 72790f0b711SPaolo Bonzini 72890f0b711SPaolo Bonzini def tearDown(self): 72990f0b711SPaolo Bonzini self.vm.shutdown() 73090f0b711SPaolo Bonzini os.remove(test_img) 73190f0b711SPaolo Bonzini os.remove(backing_img) 73290f0b711SPaolo Bonzini os.remove(self.blkdebug_file) 73390f0b711SPaolo Bonzini 73490f0b711SPaolo Bonzini def test_enospc(self): 735ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 73690f0b711SPaolo Bonzini 73790f0b711SPaolo Bonzini result = self.vm.qmp('block-stream', device='drive0', on_error='enospc') 73890f0b711SPaolo Bonzini self.assert_qmp(result, 'return', {}) 73990f0b711SPaolo Bonzini 74090f0b711SPaolo Bonzini error = False 74190f0b711SPaolo Bonzini completed = False 74290f0b711SPaolo Bonzini while not completed: 74390f0b711SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 74490f0b711SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 74590f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 74690f0b711SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 747dca9b6a2SMax Reitz error = True 74890f0b711SPaolo Bonzini 74990f0b711SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 75090f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', True) 75190f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE) 75290f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'nospace') 75390f0b711SPaolo Bonzini 75490f0b711SPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 75590f0b711SPaolo Bonzini self.assert_qmp(result, 'return', {}) 75690f0b711SPaolo Bonzini 75790f0b711SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 758dca9b6a2SMax Reitz if result == {'return': []}: 759dca9b6a2SMax Reitz # Race; likely already finished. Check. 760dca9b6a2SMax Reitz continue 76190f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 76290f0b711SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'ok') 76390f0b711SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 76490f0b711SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 76590f0b711SPaolo Bonzini self.assert_qmp(event, 'data/type', 'stream') 76690f0b711SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 76790f0b711SPaolo Bonzini self.assert_qmp_absent(event, 'data/error') 76890f0b711SPaolo Bonzini self.assert_qmp(event, 'data/offset', self.image_len) 76990f0b711SPaolo Bonzini self.assert_qmp(event, 'data/len', self.image_len) 77090f0b711SPaolo Bonzini completed = True 7711dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 7721dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 77390f0b711SPaolo Bonzini 774ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 77590f0b711SPaolo Bonzini self.vm.shutdown() 776774a8850SStefan Hajnoczi 7772499a096SStefan Hajnocziclass TestStreamStop(iotests.QMPTestCase): 77837ce63ebSStefan Hajnoczi image_len = 8 * 1024 * 1024 * 1024 # GB 77937ce63ebSStefan Hajnoczi 78037ce63ebSStefan Hajnoczi def setUp(self): 78137ce63ebSStefan Hajnoczi qemu_img('create', backing_img, str(TestStreamStop.image_len)) 78290c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img) 78337ce63ebSStefan Hajnoczi qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 78490c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img) 785b59b3d57SFam Zheng self.vm = iotests.VM().add_drive("blkdebug::" + test_img) 78637ce63ebSStefan Hajnoczi self.vm.launch() 78737ce63ebSStefan Hajnoczi 78837ce63ebSStefan Hajnoczi def tearDown(self): 78937ce63ebSStefan Hajnoczi self.vm.shutdown() 79037ce63ebSStefan Hajnoczi os.remove(test_img) 79137ce63ebSStefan Hajnoczi os.remove(backing_img) 79237ce63ebSStefan Hajnoczi 79337ce63ebSStefan Hajnoczi def test_stream_stop(self): 794ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 79537ce63ebSStefan Hajnoczi 796b59b3d57SFam Zheng self.vm.pause_drive('drive0') 797db58f9c0SStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0') 79837ce63ebSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 79937ce63ebSStefan Hajnoczi 8000fd05e8dSPaolo Bonzini time.sleep(0.1) 80137ce63ebSStefan Hajnoczi events = self.vm.get_qmp_events(wait=False) 8021dac83f1SKevin Wolf for e in events: 8031dac83f1SKevin Wolf self.assert_qmp(e, 'event', 'JOB_STATUS_CHANGE') 8041dac83f1SKevin Wolf self.assert_qmp(e, 'data/id', 'drive0') 80537ce63ebSStefan Hajnoczi 806b59b3d57SFam Zheng self.cancel_and_wait(resume=True) 80737ce63ebSStefan Hajnoczi 8082499a096SStefan Hajnocziclass TestSetSpeed(iotests.QMPTestCase): 80937ce63ebSStefan Hajnoczi image_len = 80 * 1024 * 1024 # MB 81037ce63ebSStefan Hajnoczi 81137ce63ebSStefan Hajnoczi def setUp(self): 81237ce63ebSStefan Hajnoczi qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 81390c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img) 81437ce63ebSStefan Hajnoczi qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 81590c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img) 816b59b3d57SFam Zheng self.vm = iotests.VM().add_drive('blkdebug::' + test_img) 81737ce63ebSStefan Hajnoczi self.vm.launch() 81837ce63ebSStefan Hajnoczi 81937ce63ebSStefan Hajnoczi def tearDown(self): 82037ce63ebSStefan Hajnoczi self.vm.shutdown() 82137ce63ebSStefan Hajnoczi os.remove(test_img) 82237ce63ebSStefan Hajnoczi os.remove(backing_img) 82337ce63ebSStefan Hajnoczi 824e425306aSStefan Hajnoczi # This is a short performance test which is not run by default. 825e425306aSStefan Hajnoczi # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput" 826e425306aSStefan Hajnoczi def perf_test_throughput(self): 827ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 82837ce63ebSStefan Hajnoczi 829db58f9c0SStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0') 83037ce63ebSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 83137ce63ebSStefan Hajnoczi 832e425306aSStefan Hajnoczi result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) 83337ce63ebSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 83437ce63ebSStefan Hajnoczi 8359974ad40SFam Zheng self.wait_until_completed() 83637ce63ebSStefan Hajnoczi 837ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 83837ce63ebSStefan Hajnoczi 839e425306aSStefan Hajnoczi def test_set_speed(self): 840ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 841e425306aSStefan Hajnoczi 842b59b3d57SFam Zheng self.vm.pause_drive('drive0') 843e425306aSStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0') 844e425306aSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 845e425306aSStefan Hajnoczi 846e425306aSStefan Hajnoczi # Default speed is 0 847e425306aSStefan Hajnoczi result = self.vm.qmp('query-block-jobs') 848e425306aSStefan Hajnoczi self.assert_qmp(result, 'return[0]/device', 'drive0') 849e425306aSStefan Hajnoczi self.assert_qmp(result, 'return[0]/speed', 0) 850e425306aSStefan Hajnoczi 851e425306aSStefan Hajnoczi result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) 852e425306aSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 853e425306aSStefan Hajnoczi 854e425306aSStefan Hajnoczi # Ensure the speed we set was accepted 855e425306aSStefan Hajnoczi result = self.vm.qmp('query-block-jobs') 856e425306aSStefan Hajnoczi self.assert_qmp(result, 'return[0]/device', 'drive0') 857e425306aSStefan Hajnoczi self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024) 858e425306aSStefan Hajnoczi 859b59b3d57SFam Zheng self.cancel_and_wait(resume=True) 860b59b3d57SFam Zheng self.vm.pause_drive('drive0') 861e425306aSStefan Hajnoczi 862e425306aSStefan Hajnoczi # Check setting speed in block-stream works 863e425306aSStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024) 864e425306aSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 865e425306aSStefan Hajnoczi 866e425306aSStefan Hajnoczi result = self.vm.qmp('query-block-jobs') 867e425306aSStefan Hajnoczi self.assert_qmp(result, 'return[0]/device', 'drive0') 868e425306aSStefan Hajnoczi self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024) 869e425306aSStefan Hajnoczi 870b59b3d57SFam Zheng self.cancel_and_wait(resume=True) 871e425306aSStefan Hajnoczi 872e425306aSStefan Hajnoczi def test_set_speed_invalid(self): 873ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 874e425306aSStefan Hajnoczi 875e425306aSStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0', speed=-1) 87658c8cce2SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 877e425306aSStefan Hajnoczi 878ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 879e425306aSStefan Hajnoczi 880dca9b6a2SMax Reitz self.vm.pause_drive('drive0') 881e425306aSStefan Hajnoczi result = self.vm.qmp('block-stream', device='drive0') 882e425306aSStefan Hajnoczi self.assert_qmp(result, 'return', {}) 883e425306aSStefan Hajnoczi 884e425306aSStefan Hajnoczi result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1) 88558c8cce2SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 886e425306aSStefan Hajnoczi 887dca9b6a2SMax Reitz self.cancel_and_wait(resume=True) 888e425306aSStefan Hajnoczi 88937ce63ebSStefan Hajnocziif __name__ == '__main__': 89037ce63ebSStefan Hajnoczi iotests.main(supported_fmts=['qcow2', 'qed']) 891