1747051cdSJeff Cody#!/usr/bin/env python 2747051cdSJeff Cody# 3747051cdSJeff Cody# Tests for image block commit. 4747051cdSJeff Cody# 5747051cdSJeff Cody# Copyright (C) 2012 IBM, Corp. 6747051cdSJeff Cody# Copyright (C) 2012 Red Hat, Inc. 7747051cdSJeff Cody# 8747051cdSJeff Cody# This program is free software; you can redistribute it and/or modify 9747051cdSJeff Cody# it under the terms of the GNU General Public License as published by 10747051cdSJeff Cody# the Free Software Foundation; either version 2 of the License, or 11747051cdSJeff Cody# (at your option) any later version. 12747051cdSJeff Cody# 13747051cdSJeff Cody# This program is distributed in the hope that it will be useful, 14747051cdSJeff Cody# but WITHOUT ANY WARRANTY; without even the implied warranty of 15747051cdSJeff Cody# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16747051cdSJeff Cody# GNU General Public License for more details. 17747051cdSJeff Cody# 18747051cdSJeff Cody# You should have received a copy of the GNU General Public License 19747051cdSJeff Cody# along with this program. If not, see <http://www.gnu.org/licenses/>. 20747051cdSJeff Cody# 21747051cdSJeff Cody# Test for live block commit 22747051cdSJeff Cody# Derived from Image Streaming Test 030 23747051cdSJeff Cody 24747051cdSJeff Codyimport time 25747051cdSJeff Codyimport os 26747051cdSJeff Codyimport iotests 27747051cdSJeff Codyfrom iotests import qemu_img, qemu_io 28747051cdSJeff Codyimport struct 296bf0d1f4SJeff Codyimport errno 30747051cdSJeff Cody 31747051cdSJeff Codybacking_img = os.path.join(iotests.test_dir, 'backing.img') 32747051cdSJeff Codymid_img = os.path.join(iotests.test_dir, 'mid.img') 33747051cdSJeff Codytest_img = os.path.join(iotests.test_dir, 'test.img') 34747051cdSJeff Cody 35747051cdSJeff Codyclass ImageCommitTestCase(iotests.QMPTestCase): 36747051cdSJeff Cody '''Abstract base class for image commit test cases''' 37747051cdSJeff Cody 387676e2c5SJeff Cody def wait_for_complete(self, need_ready=False): 394de43470SFam Zheng completed = False 408b9a30caSFam Zheng ready = False 414de43470SFam Zheng while not completed: 424de43470SFam Zheng for event in self.vm.get_qmp_events(wait=True): 434de43470SFam Zheng if event['event'] == 'BLOCK_JOB_COMPLETED': 44bcdce5a7SAlberto Garcia self.assert_qmp_absent(event, 'data/error') 454de43470SFam Zheng self.assert_qmp(event, 'data/type', 'commit') 464de43470SFam Zheng self.assert_qmp(event, 'data/device', 'drive0') 471d3ba15aSMax Reitz self.assert_qmp(event, 'data/offset', event['data']['len']) 488b9a30caSFam Zheng if need_ready: 498b9a30caSFam Zheng self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event") 504de43470SFam Zheng completed = True 514de43470SFam Zheng elif event['event'] == 'BLOCK_JOB_READY': 528b9a30caSFam Zheng ready = True 534de43470SFam Zheng self.assert_qmp(event, 'data/type', 'commit') 544de43470SFam Zheng self.assert_qmp(event, 'data/device', 'drive0') 554de43470SFam Zheng self.vm.qmp('block-job-complete', device='drive0') 564de43470SFam Zheng 57fb0a078fSFam Zheng self.assert_no_active_block_jobs() 584de43470SFam Zheng self.vm.shutdown() 594de43470SFam Zheng 60d57177a4SKevin Wolf def run_commit_test(self, top, base, need_ready=False, node_names=False): 617676e2c5SJeff Cody self.assert_no_active_block_jobs() 62d57177a4SKevin Wolf if node_names: 63d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node=top, base_node=base) 64d57177a4SKevin Wolf else: 657676e2c5SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top=top, base=base) 667676e2c5SJeff Cody self.assert_qmp(result, 'return', {}) 677676e2c5SJeff Cody self.wait_for_complete(need_ready) 687676e2c5SJeff Cody 697676e2c5SJeff Cody def run_default_commit_test(self): 707676e2c5SJeff Cody self.assert_no_active_block_jobs() 717676e2c5SJeff Cody result = self.vm.qmp('block-commit', device='drive0') 727676e2c5SJeff Cody self.assert_qmp(result, 'return', {}) 737676e2c5SJeff Cody self.wait_for_complete() 747676e2c5SJeff Cody 75747051cdSJeff Codyclass TestSingleDrive(ImageCommitTestCase): 76c3971b88SKevin Wolf # Need some space after the copied data so that throttling is effective in 77c3971b88SKevin Wolf # tests that use it rather than just completing the job immediately 78c3971b88SKevin Wolf image_len = 2 * 1024 * 1024 79747051cdSJeff Cody test_len = 1 * 1024 * 256 80747051cdSJeff Cody 81747051cdSJeff Cody def setUp(self): 828b9a30caSFam Zheng iotests.create_image(backing_img, self.image_len) 83747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 84747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 8590c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img) 8690c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img) 87d3c8c674SKevin Wolf self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none") 88f357576fSJohn Snow self.vm.add_device(iotests.get_virtio_scsi_device()) 89c3971b88SKevin Wolf self.vm.add_device("scsi-hd,id=scsi0,drive=drive0") 90747051cdSJeff Cody self.vm.launch() 9186472071SMax Reitz self.has_quit = False 92747051cdSJeff Cody 93747051cdSJeff Cody def tearDown(self): 9486472071SMax Reitz self.vm.shutdown(has_quit=self.has_quit) 95747051cdSJeff Cody os.remove(test_img) 96747051cdSJeff Cody os.remove(mid_img) 97747051cdSJeff Cody os.remove(backing_img) 98747051cdSJeff Cody 99747051cdSJeff Cody def test_commit(self): 1004de43470SFam Zheng self.run_commit_test(mid_img, backing_img) 10190c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 10290c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 103747051cdSJeff Cody 104d57177a4SKevin Wolf def test_commit_node(self): 105d57177a4SKevin Wolf self.run_commit_test("mid", "base", node_names=True) 106d57177a4SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 107d57177a4SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 108d57177a4SKevin Wolf 109*9442bebeSThomas Huth @iotests.skip_if_unsupported(['throttle']) 11086472071SMax Reitz def test_commit_with_filter_and_quit(self): 11186472071SMax Reitz result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg') 11286472071SMax Reitz self.assert_qmp(result, 'return', {}) 11386472071SMax Reitz 11486472071SMax Reitz # Add a filter outside of the backing chain 11586472071SMax Reitz result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid') 11686472071SMax Reitz self.assert_qmp(result, 'return', {}) 11786472071SMax Reitz 11886472071SMax Reitz result = self.vm.qmp('block-commit', device='drive0') 11986472071SMax Reitz self.assert_qmp(result, 'return', {}) 12086472071SMax Reitz 12186472071SMax Reitz # Quit immediately, thus forcing a simultaneous cancel of the 12286472071SMax Reitz # block job and a bdrv_drain_all() 12386472071SMax Reitz result = self.vm.qmp('quit') 12486472071SMax Reitz self.assert_qmp(result, 'return', {}) 12586472071SMax Reitz 12686472071SMax Reitz self.has_quit = True 12786472071SMax Reitz 12886472071SMax Reitz # Same as above, but this time we add the filter after starting the job 129*9442bebeSThomas Huth @iotests.skip_if_unsupported(['throttle']) 13086472071SMax Reitz def test_commit_plus_filter_and_quit(self): 13186472071SMax Reitz result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg') 13286472071SMax Reitz self.assert_qmp(result, 'return', {}) 13386472071SMax Reitz 13486472071SMax Reitz result = self.vm.qmp('block-commit', device='drive0') 13586472071SMax Reitz self.assert_qmp(result, 'return', {}) 13686472071SMax Reitz 13786472071SMax Reitz # Add a filter outside of the backing chain 13886472071SMax Reitz result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid') 13986472071SMax Reitz self.assert_qmp(result, 'return', {}) 14086472071SMax Reitz 14186472071SMax Reitz # Quit immediately, thus forcing a simultaneous cancel of the 14286472071SMax Reitz # block job and a bdrv_drain_all() 14386472071SMax Reitz result = self.vm.qmp('quit') 14486472071SMax Reitz self.assert_qmp(result, 'return', {}) 14586472071SMax Reitz 14686472071SMax Reitz self.has_quit = True 14786472071SMax Reitz 148747051cdSJeff Cody def test_device_not_found(self): 149747051cdSJeff Cody result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img) 150747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'DeviceNotFound') 151747051cdSJeff Cody 152747051cdSJeff Cody def test_top_same_base(self): 153fb0a078fSFam Zheng self.assert_no_active_block_jobs() 154747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img) 155747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 156d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img) 157747051cdSJeff Cody 158747051cdSJeff Cody def test_top_invalid(self): 159fb0a078fSFam Zheng self.assert_no_active_block_jobs() 160747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img) 161747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 162747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 163747051cdSJeff Cody 164747051cdSJeff Cody def test_base_invalid(self): 165fb0a078fSFam Zheng self.assert_no_active_block_jobs() 166747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile') 167747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 168747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 169747051cdSJeff Cody 170d57177a4SKevin Wolf def test_top_node_invalid(self): 171d57177a4SKevin Wolf self.assert_no_active_block_jobs() 172d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base') 173d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 174d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile") 175d57177a4SKevin Wolf 176d57177a4SKevin Wolf def test_base_node_invalid(self): 177d57177a4SKevin Wolf self.assert_no_active_block_jobs() 178d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile') 179d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 180d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile") 181d57177a4SKevin Wolf 182d57177a4SKevin Wolf def test_top_path_and_node(self): 183d57177a4SKevin Wolf self.assert_no_active_block_jobs() 184d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img) 185d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 186d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive") 187d57177a4SKevin Wolf 188d57177a4SKevin Wolf def test_base_path_and_node(self): 189d57177a4SKevin Wolf self.assert_no_active_block_jobs() 190d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img) 191d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 192d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive") 193d57177a4SKevin Wolf 194747051cdSJeff Cody def test_top_is_active(self): 1958b9a30caSFam Zheng self.run_commit_test(test_img, backing_img, need_ready=True) 19690c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 19790c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 198747051cdSJeff Cody 1997676e2c5SJeff Cody def test_top_is_default_active(self): 2007676e2c5SJeff Cody self.run_default_commit_test() 20190c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 20290c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 2037676e2c5SJeff Cody 204747051cdSJeff Cody def test_top_and_base_reversed(self): 205fb0a078fSFam Zheng self.assert_no_active_block_jobs() 206747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img) 207747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 208d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img) 209747051cdSJeff Cody 210d57177a4SKevin Wolf def test_top_and_base_node_reversed(self): 211d57177a4SKevin Wolf self.assert_no_active_block_jobs() 212d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top') 213d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 214d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain") 215d57177a4SKevin Wolf 216d57177a4SKevin Wolf def test_top_node_in_wrong_chain(self): 217d57177a4SKevin Wolf self.assert_no_active_block_jobs() 218d57177a4SKevin Wolf 219d57177a4SKevin Wolf result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null') 220d57177a4SKevin Wolf self.assert_qmp(result, 'return', {}) 221d57177a4SKevin Wolf 222d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base') 223d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 224d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain") 225d57177a4SKevin Wolf 226c3971b88SKevin Wolf # When the job is running on a BB that is automatically deleted on hot 227c3971b88SKevin Wolf # unplug, the job is cancelled when the device disappears 228c3971b88SKevin Wolf def test_hot_unplug(self): 229c3971b88SKevin Wolf if self.image_len == 0: 230c3971b88SKevin Wolf return 231c3971b88SKevin Wolf 232c3971b88SKevin Wolf self.assert_no_active_block_jobs() 233c3971b88SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top=mid_img, 2349a3a9a63SMax Reitz base=backing_img, speed=(self.image_len // 4)) 235c3971b88SKevin Wolf self.assert_qmp(result, 'return', {}) 236c3971b88SKevin Wolf result = self.vm.qmp('device_del', id='scsi0') 237c3971b88SKevin Wolf self.assert_qmp(result, 'return', {}) 238c3971b88SKevin Wolf 239c3971b88SKevin Wolf cancelled = False 240c3971b88SKevin Wolf deleted = False 241c3971b88SKevin Wolf while not cancelled or not deleted: 242c3971b88SKevin Wolf for event in self.vm.get_qmp_events(wait=True): 243c3971b88SKevin Wolf if event['event'] == 'DEVICE_DELETED': 244c3971b88SKevin Wolf self.assert_qmp(event, 'data/device', 'scsi0') 245c3971b88SKevin Wolf deleted = True 246c3971b88SKevin Wolf elif event['event'] == 'BLOCK_JOB_CANCELLED': 247c3971b88SKevin Wolf self.assert_qmp(event, 'data/device', 'drive0') 248c3971b88SKevin Wolf cancelled = True 2491dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 2501dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 251c3971b88SKevin Wolf else: 252c3971b88SKevin Wolf self.fail("Unexpected event %s" % (event['event'])) 253c3971b88SKevin Wolf 254c3971b88SKevin Wolf self.assert_no_active_block_jobs() 255747051cdSJeff Cody 256d3c8c674SKevin Wolf # Tests that the insertion of the commit_top filter node doesn't make a 257d3c8c674SKevin Wolf # difference to query-blockstat 258d3c8c674SKevin Wolf def test_implicit_node(self): 259d3c8c674SKevin Wolf if self.image_len == 0: 260d3c8c674SKevin Wolf return 261d3c8c674SKevin Wolf 262d3c8c674SKevin Wolf self.assert_no_active_block_jobs() 263d3c8c674SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top=mid_img, 2649a3a9a63SMax Reitz base=backing_img, speed=(self.image_len // 4)) 265d3c8c674SKevin Wolf self.assert_qmp(result, 'return', {}) 266d3c8c674SKevin Wolf 267d3c8c674SKevin Wolf result = self.vm.qmp('query-block') 268d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/file', test_img) 269d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt) 270d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img) 271d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2) 272d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img) 273d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img) 274d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img) 275d3c8c674SKevin Wolf 276d3c8c674SKevin Wolf result = self.vm.qmp('query-blockstats') 277d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/node-name', 'top') 278d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/backing/node-name', 'mid') 279d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base') 280d3c8c674SKevin Wolf 281d3c8c674SKevin Wolf self.cancel_and_wait() 282d3c8c674SKevin Wolf self.assert_no_active_block_jobs() 283d3c8c674SKevin Wolf 2846bf0d1f4SJeff Codyclass TestRelativePaths(ImageCommitTestCase): 2856bf0d1f4SJeff Cody image_len = 1 * 1024 * 1024 2866bf0d1f4SJeff Cody test_len = 1 * 1024 * 256 2876bf0d1f4SJeff Cody 2886bf0d1f4SJeff Cody dir1 = "dir1" 2896bf0d1f4SJeff Cody dir2 = "dir2/" 2906bf0d1f4SJeff Cody dir3 = "dir2/dir3/" 2916bf0d1f4SJeff Cody 2926bf0d1f4SJeff Cody test_img = os.path.join(iotests.test_dir, dir3, 'test.img') 2936bf0d1f4SJeff Cody mid_img = "../mid.img" 2946bf0d1f4SJeff Cody backing_img = "../dir1/backing.img" 2956bf0d1f4SJeff Cody 2966bf0d1f4SJeff Cody backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img') 2976bf0d1f4SJeff Cody mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img') 2986bf0d1f4SJeff Cody 2996bf0d1f4SJeff Cody def setUp(self): 3006bf0d1f4SJeff Cody try: 3016bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir1)) 3026bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir2)) 3036bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir3)) 3046bf0d1f4SJeff Cody except OSError as exception: 3056bf0d1f4SJeff Cody if exception.errno != errno.EEXIST: 3066bf0d1f4SJeff Cody raise 307915365a9SFam Zheng iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len) 3086bf0d1f4SJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs) 3096bf0d1f4SJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img) 3106bf0d1f4SJeff Cody qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs) 3116bf0d1f4SJeff Cody qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img) 31290c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs) 31390c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs) 3146bf0d1f4SJeff Cody self.vm = iotests.VM().add_drive(self.test_img) 3156bf0d1f4SJeff Cody self.vm.launch() 3166bf0d1f4SJeff Cody 3176bf0d1f4SJeff Cody def tearDown(self): 3186bf0d1f4SJeff Cody self.vm.shutdown() 3196bf0d1f4SJeff Cody os.remove(self.test_img) 3206bf0d1f4SJeff Cody os.remove(self.mid_img_abs) 3216bf0d1f4SJeff Cody os.remove(self.backing_img_abs) 3226bf0d1f4SJeff Cody try: 3236bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir1)) 3246bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir3)) 3256bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir2)) 3266bf0d1f4SJeff Cody except OSError as exception: 3276bf0d1f4SJeff Cody if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY: 3286bf0d1f4SJeff Cody raise 3296bf0d1f4SJeff Cody 3306bf0d1f4SJeff Cody def test_commit(self): 3314de43470SFam Zheng self.run_commit_test(self.mid_img, self.backing_img) 33290c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 33390c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 3346bf0d1f4SJeff Cody 3356bf0d1f4SJeff Cody def test_device_not_found(self): 3366bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img) 3376bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'DeviceNotFound') 3386bf0d1f4SJeff Cody 3396bf0d1f4SJeff Cody def test_top_same_base(self): 340fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3416bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img) 3426bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3436bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 3446bf0d1f4SJeff Cody 3456bf0d1f4SJeff Cody def test_top_invalid(self): 346fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3476bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img) 3486bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3496bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 3506bf0d1f4SJeff Cody 3516bf0d1f4SJeff Cody def test_base_invalid(self): 352fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3536bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile') 3546bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3556bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 3566bf0d1f4SJeff Cody 3576bf0d1f4SJeff Cody def test_top_is_active(self): 3584de43470SFam Zheng self.run_commit_test(self.test_img, self.backing_img) 35990c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 36090c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 3616bf0d1f4SJeff Cody 3626bf0d1f4SJeff Cody def test_top_and_base_reversed(self): 363fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3646bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img) 3656bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3666bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 3676bf0d1f4SJeff Cody 368747051cdSJeff Cody 369747051cdSJeff Codyclass TestSetSpeed(ImageCommitTestCase): 370747051cdSJeff Cody image_len = 80 * 1024 * 1024 # MB 371747051cdSJeff Cody 372747051cdSJeff Cody def setUp(self): 373747051cdSJeff Cody qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 374747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 375747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 37690c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img) 37790c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img) 37851c493c5SMax Reitz self.vm = iotests.VM().add_drive('blkdebug::' + test_img) 379747051cdSJeff Cody self.vm.launch() 380747051cdSJeff Cody 381747051cdSJeff Cody def tearDown(self): 382747051cdSJeff Cody self.vm.shutdown() 383747051cdSJeff Cody os.remove(test_img) 384747051cdSJeff Cody os.remove(mid_img) 385747051cdSJeff Cody os.remove(backing_img) 386747051cdSJeff Cody 387747051cdSJeff Cody def test_set_speed(self): 388fb0a078fSFam Zheng self.assert_no_active_block_jobs() 389747051cdSJeff Cody 390b59b3d57SFam Zheng self.vm.pause_drive('drive0') 391747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024) 392747051cdSJeff Cody self.assert_qmp(result, 'return', {}) 393747051cdSJeff Cody 394747051cdSJeff Cody # Ensure the speed we set was accepted 395747051cdSJeff Cody result = self.vm.qmp('query-block-jobs') 396747051cdSJeff Cody self.assert_qmp(result, 'return[0]/device', 'drive0') 397747051cdSJeff Cody self.assert_qmp(result, 'return[0]/speed', 1024 * 1024) 398747051cdSJeff Cody 399b59b3d57SFam Zheng self.cancel_and_wait(resume=True) 400747051cdSJeff Cody 4018b9a30caSFam Zhengclass TestActiveZeroLengthImage(TestSingleDrive): 4028b9a30caSFam Zheng image_len = 0 403747051cdSJeff Cody 404bcdce5a7SAlberto Garciaclass TestReopenOverlay(ImageCommitTestCase): 405bcdce5a7SAlberto Garcia image_len = 1024 * 1024 406bcdce5a7SAlberto Garcia img0 = os.path.join(iotests.test_dir, '0.img') 407bcdce5a7SAlberto Garcia img1 = os.path.join(iotests.test_dir, '1.img') 408bcdce5a7SAlberto Garcia img2 = os.path.join(iotests.test_dir, '2.img') 409bcdce5a7SAlberto Garcia img3 = os.path.join(iotests.test_dir, '3.img') 410bcdce5a7SAlberto Garcia 411bcdce5a7SAlberto Garcia def setUp(self): 412bcdce5a7SAlberto Garcia iotests.create_image(self.img0, self.image_len) 413bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1) 414bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2) 415bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3) 416bcdce5a7SAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1) 417bcdce5a7SAlberto Garcia self.vm = iotests.VM().add_drive(self.img3) 418bcdce5a7SAlberto Garcia self.vm.launch() 419bcdce5a7SAlberto Garcia 420bcdce5a7SAlberto Garcia def tearDown(self): 421bcdce5a7SAlberto Garcia self.vm.shutdown() 422bcdce5a7SAlberto Garcia os.remove(self.img0) 423bcdce5a7SAlberto Garcia os.remove(self.img1) 424bcdce5a7SAlberto Garcia os.remove(self.img2) 425bcdce5a7SAlberto Garcia os.remove(self.img3) 426bcdce5a7SAlberto Garcia 427bcdce5a7SAlberto Garcia # This tests what happens when the overlay image of the 'top' node 428bcdce5a7SAlberto Garcia # needs to be reopened in read-write mode in order to update the 429bcdce5a7SAlberto Garcia # backing image string. 430bcdce5a7SAlberto Garcia def test_reopen_overlay(self): 431bcdce5a7SAlberto Garcia self.run_commit_test(self.img1, self.img0) 432bcdce5a7SAlberto Garcia 433747051cdSJeff Codyif __name__ == '__main__': 434103cbc77SMax Reitz iotests.main(supported_fmts=['qcow2', 'qed'], 435103cbc77SMax Reitz supported_protocols=['file']) 436