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") 88*f357576fSJohn 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 10986472071SMax Reitz def test_commit_with_filter_and_quit(self): 11086472071SMax Reitz result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg') 11186472071SMax Reitz self.assert_qmp(result, 'return', {}) 11286472071SMax Reitz 11386472071SMax Reitz # Add a filter outside of the backing chain 11486472071SMax Reitz result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid') 11586472071SMax Reitz self.assert_qmp(result, 'return', {}) 11686472071SMax Reitz 11786472071SMax Reitz result = self.vm.qmp('block-commit', device='drive0') 11886472071SMax Reitz self.assert_qmp(result, 'return', {}) 11986472071SMax Reitz 12086472071SMax Reitz # Quit immediately, thus forcing a simultaneous cancel of the 12186472071SMax Reitz # block job and a bdrv_drain_all() 12286472071SMax Reitz result = self.vm.qmp('quit') 12386472071SMax Reitz self.assert_qmp(result, 'return', {}) 12486472071SMax Reitz 12586472071SMax Reitz self.has_quit = True 12686472071SMax Reitz 12786472071SMax Reitz # Same as above, but this time we add the filter after starting the job 12886472071SMax Reitz def test_commit_plus_filter_and_quit(self): 12986472071SMax Reitz result = self.vm.qmp('object-add', qom_type='throttle-group', id='tg') 13086472071SMax Reitz self.assert_qmp(result, 'return', {}) 13186472071SMax Reitz 13286472071SMax Reitz result = self.vm.qmp('block-commit', device='drive0') 13386472071SMax Reitz self.assert_qmp(result, 'return', {}) 13486472071SMax Reitz 13586472071SMax Reitz # Add a filter outside of the backing chain 13686472071SMax Reitz result = self.vm.qmp('blockdev-add', driver='throttle', node_name='filter', throttle_group='tg', file='mid') 13786472071SMax Reitz self.assert_qmp(result, 'return', {}) 13886472071SMax Reitz 13986472071SMax Reitz # Quit immediately, thus forcing a simultaneous cancel of the 14086472071SMax Reitz # block job and a bdrv_drain_all() 14186472071SMax Reitz result = self.vm.qmp('quit') 14286472071SMax Reitz self.assert_qmp(result, 'return', {}) 14386472071SMax Reitz 14486472071SMax Reitz self.has_quit = True 14586472071SMax Reitz 146747051cdSJeff Cody def test_device_not_found(self): 147747051cdSJeff Cody result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img) 148747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'DeviceNotFound') 149747051cdSJeff Cody 150747051cdSJeff Cody def test_top_same_base(self): 151fb0a078fSFam Zheng self.assert_no_active_block_jobs() 152747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img) 153747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 154d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img) 155747051cdSJeff Cody 156747051cdSJeff Cody def test_top_invalid(self): 157fb0a078fSFam Zheng self.assert_no_active_block_jobs() 158747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img) 159747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 160747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 161747051cdSJeff Cody 162747051cdSJeff Cody def test_base_invalid(self): 163fb0a078fSFam Zheng self.assert_no_active_block_jobs() 164747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile') 165747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 166747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 167747051cdSJeff Cody 168d57177a4SKevin Wolf def test_top_node_invalid(self): 169d57177a4SKevin Wolf self.assert_no_active_block_jobs() 170d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base') 171d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 172d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile") 173d57177a4SKevin Wolf 174d57177a4SKevin Wolf def test_base_node_invalid(self): 175d57177a4SKevin Wolf self.assert_no_active_block_jobs() 176d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile') 177d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 178d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile") 179d57177a4SKevin Wolf 180d57177a4SKevin Wolf def test_top_path_and_node(self): 181d57177a4SKevin Wolf self.assert_no_active_block_jobs() 182d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img) 183d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 184d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive") 185d57177a4SKevin Wolf 186d57177a4SKevin Wolf def test_base_path_and_node(self): 187d57177a4SKevin Wolf self.assert_no_active_block_jobs() 188d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img) 189d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 190d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive") 191d57177a4SKevin Wolf 192747051cdSJeff Cody def test_top_is_active(self): 1938b9a30caSFam Zheng self.run_commit_test(test_img, backing_img, need_ready=True) 19490c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 19590c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 196747051cdSJeff Cody 1977676e2c5SJeff Cody def test_top_is_default_active(self): 1987676e2c5SJeff Cody self.run_default_commit_test() 19990c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 20090c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 2017676e2c5SJeff Cody 202747051cdSJeff Cody def test_top_and_base_reversed(self): 203fb0a078fSFam Zheng self.assert_no_active_block_jobs() 204747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img) 205747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 206d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img) 207747051cdSJeff Cody 208d57177a4SKevin Wolf def test_top_and_base_node_reversed(self): 209d57177a4SKevin Wolf self.assert_no_active_block_jobs() 210d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top') 211d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 212d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain") 213d57177a4SKevin Wolf 214d57177a4SKevin Wolf def test_top_node_in_wrong_chain(self): 215d57177a4SKevin Wolf self.assert_no_active_block_jobs() 216d57177a4SKevin Wolf 217d57177a4SKevin Wolf result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null') 218d57177a4SKevin Wolf self.assert_qmp(result, 'return', {}) 219d57177a4SKevin Wolf 220d57177a4SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base') 221d57177a4SKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 222d57177a4SKevin Wolf self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain") 223d57177a4SKevin Wolf 224c3971b88SKevin Wolf # When the job is running on a BB that is automatically deleted on hot 225c3971b88SKevin Wolf # unplug, the job is cancelled when the device disappears 226c3971b88SKevin Wolf def test_hot_unplug(self): 227c3971b88SKevin Wolf if self.image_len == 0: 228c3971b88SKevin Wolf return 229c3971b88SKevin Wolf 230c3971b88SKevin Wolf self.assert_no_active_block_jobs() 231c3971b88SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top=mid_img, 2329a3a9a63SMax Reitz base=backing_img, speed=(self.image_len // 4)) 233c3971b88SKevin Wolf self.assert_qmp(result, 'return', {}) 234c3971b88SKevin Wolf result = self.vm.qmp('device_del', id='scsi0') 235c3971b88SKevin Wolf self.assert_qmp(result, 'return', {}) 236c3971b88SKevin Wolf 237c3971b88SKevin Wolf cancelled = False 238c3971b88SKevin Wolf deleted = False 239c3971b88SKevin Wolf while not cancelled or not deleted: 240c3971b88SKevin Wolf for event in self.vm.get_qmp_events(wait=True): 241c3971b88SKevin Wolf if event['event'] == 'DEVICE_DELETED': 242c3971b88SKevin Wolf self.assert_qmp(event, 'data/device', 'scsi0') 243c3971b88SKevin Wolf deleted = True 244c3971b88SKevin Wolf elif event['event'] == 'BLOCK_JOB_CANCELLED': 245c3971b88SKevin Wolf self.assert_qmp(event, 'data/device', 'drive0') 246c3971b88SKevin Wolf cancelled = True 2471dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 2481dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 249c3971b88SKevin Wolf else: 250c3971b88SKevin Wolf self.fail("Unexpected event %s" % (event['event'])) 251c3971b88SKevin Wolf 252c3971b88SKevin Wolf self.assert_no_active_block_jobs() 253747051cdSJeff Cody 254d3c8c674SKevin Wolf # Tests that the insertion of the commit_top filter node doesn't make a 255d3c8c674SKevin Wolf # difference to query-blockstat 256d3c8c674SKevin Wolf def test_implicit_node(self): 257d3c8c674SKevin Wolf if self.image_len == 0: 258d3c8c674SKevin Wolf return 259d3c8c674SKevin Wolf 260d3c8c674SKevin Wolf self.assert_no_active_block_jobs() 261d3c8c674SKevin Wolf result = self.vm.qmp('block-commit', device='drive0', top=mid_img, 2629a3a9a63SMax Reitz base=backing_img, speed=(self.image_len // 4)) 263d3c8c674SKevin Wolf self.assert_qmp(result, 'return', {}) 264d3c8c674SKevin Wolf 265d3c8c674SKevin Wolf result = self.vm.qmp('query-block') 266d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/file', test_img) 267d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt) 268d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img) 269d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2) 270d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img) 271d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img) 272d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img) 273d3c8c674SKevin Wolf 274d3c8c674SKevin Wolf result = self.vm.qmp('query-blockstats') 275d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/node-name', 'top') 276d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/backing/node-name', 'mid') 277d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base') 278d3c8c674SKevin Wolf 279d3c8c674SKevin Wolf self.cancel_and_wait() 280d3c8c674SKevin Wolf self.assert_no_active_block_jobs() 281d3c8c674SKevin Wolf 2826bf0d1f4SJeff Codyclass TestRelativePaths(ImageCommitTestCase): 2836bf0d1f4SJeff Cody image_len = 1 * 1024 * 1024 2846bf0d1f4SJeff Cody test_len = 1 * 1024 * 256 2856bf0d1f4SJeff Cody 2866bf0d1f4SJeff Cody dir1 = "dir1" 2876bf0d1f4SJeff Cody dir2 = "dir2/" 2886bf0d1f4SJeff Cody dir3 = "dir2/dir3/" 2896bf0d1f4SJeff Cody 2906bf0d1f4SJeff Cody test_img = os.path.join(iotests.test_dir, dir3, 'test.img') 2916bf0d1f4SJeff Cody mid_img = "../mid.img" 2926bf0d1f4SJeff Cody backing_img = "../dir1/backing.img" 2936bf0d1f4SJeff Cody 2946bf0d1f4SJeff Cody backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img') 2956bf0d1f4SJeff Cody mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img') 2966bf0d1f4SJeff Cody 2976bf0d1f4SJeff Cody def setUp(self): 2986bf0d1f4SJeff Cody try: 2996bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir1)) 3006bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir2)) 3016bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir3)) 3026bf0d1f4SJeff Cody except OSError as exception: 3036bf0d1f4SJeff Cody if exception.errno != errno.EEXIST: 3046bf0d1f4SJeff Cody raise 305915365a9SFam Zheng iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len) 3066bf0d1f4SJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs) 3076bf0d1f4SJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img) 3086bf0d1f4SJeff Cody qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs) 3096bf0d1f4SJeff Cody qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img) 31090c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs) 31190c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs) 3126bf0d1f4SJeff Cody self.vm = iotests.VM().add_drive(self.test_img) 3136bf0d1f4SJeff Cody self.vm.launch() 3146bf0d1f4SJeff Cody 3156bf0d1f4SJeff Cody def tearDown(self): 3166bf0d1f4SJeff Cody self.vm.shutdown() 3176bf0d1f4SJeff Cody os.remove(self.test_img) 3186bf0d1f4SJeff Cody os.remove(self.mid_img_abs) 3196bf0d1f4SJeff Cody os.remove(self.backing_img_abs) 3206bf0d1f4SJeff Cody try: 3216bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir1)) 3226bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir3)) 3236bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir2)) 3246bf0d1f4SJeff Cody except OSError as exception: 3256bf0d1f4SJeff Cody if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY: 3266bf0d1f4SJeff Cody raise 3276bf0d1f4SJeff Cody 3286bf0d1f4SJeff Cody def test_commit(self): 3294de43470SFam Zheng self.run_commit_test(self.mid_img, self.backing_img) 33090c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 33190c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 3326bf0d1f4SJeff Cody 3336bf0d1f4SJeff Cody def test_device_not_found(self): 3346bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img) 3356bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'DeviceNotFound') 3366bf0d1f4SJeff Cody 3376bf0d1f4SJeff Cody def test_top_same_base(self): 338fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3396bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img) 3406bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3416bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 3426bf0d1f4SJeff Cody 3436bf0d1f4SJeff Cody def test_top_invalid(self): 344fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3456bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img) 3466bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3476bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 3486bf0d1f4SJeff Cody 3496bf0d1f4SJeff Cody def test_base_invalid(self): 350fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3516bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile') 3526bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3536bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 3546bf0d1f4SJeff Cody 3556bf0d1f4SJeff Cody def test_top_is_active(self): 3564de43470SFam Zheng self.run_commit_test(self.test_img, self.backing_img) 35790c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 35890c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 3596bf0d1f4SJeff Cody 3606bf0d1f4SJeff Cody def test_top_and_base_reversed(self): 361fb0a078fSFam Zheng self.assert_no_active_block_jobs() 3626bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img) 3636bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 3646bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 3656bf0d1f4SJeff Cody 366747051cdSJeff Cody 367747051cdSJeff Codyclass TestSetSpeed(ImageCommitTestCase): 368747051cdSJeff Cody image_len = 80 * 1024 * 1024 # MB 369747051cdSJeff Cody 370747051cdSJeff Cody def setUp(self): 371747051cdSJeff Cody qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 372747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 373747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 37490c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img) 37590c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img) 37651c493c5SMax Reitz self.vm = iotests.VM().add_drive('blkdebug::' + test_img) 377747051cdSJeff Cody self.vm.launch() 378747051cdSJeff Cody 379747051cdSJeff Cody def tearDown(self): 380747051cdSJeff Cody self.vm.shutdown() 381747051cdSJeff Cody os.remove(test_img) 382747051cdSJeff Cody os.remove(mid_img) 383747051cdSJeff Cody os.remove(backing_img) 384747051cdSJeff Cody 385747051cdSJeff Cody def test_set_speed(self): 386fb0a078fSFam Zheng self.assert_no_active_block_jobs() 387747051cdSJeff Cody 388b59b3d57SFam Zheng self.vm.pause_drive('drive0') 389747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024) 390747051cdSJeff Cody self.assert_qmp(result, 'return', {}) 391747051cdSJeff Cody 392747051cdSJeff Cody # Ensure the speed we set was accepted 393747051cdSJeff Cody result = self.vm.qmp('query-block-jobs') 394747051cdSJeff Cody self.assert_qmp(result, 'return[0]/device', 'drive0') 395747051cdSJeff Cody self.assert_qmp(result, 'return[0]/speed', 1024 * 1024) 396747051cdSJeff Cody 397b59b3d57SFam Zheng self.cancel_and_wait(resume=True) 398747051cdSJeff Cody 3998b9a30caSFam Zhengclass TestActiveZeroLengthImage(TestSingleDrive): 4008b9a30caSFam Zheng image_len = 0 401747051cdSJeff Cody 402bcdce5a7SAlberto Garciaclass TestReopenOverlay(ImageCommitTestCase): 403bcdce5a7SAlberto Garcia image_len = 1024 * 1024 404bcdce5a7SAlberto Garcia img0 = os.path.join(iotests.test_dir, '0.img') 405bcdce5a7SAlberto Garcia img1 = os.path.join(iotests.test_dir, '1.img') 406bcdce5a7SAlberto Garcia img2 = os.path.join(iotests.test_dir, '2.img') 407bcdce5a7SAlberto Garcia img3 = os.path.join(iotests.test_dir, '3.img') 408bcdce5a7SAlberto Garcia 409bcdce5a7SAlberto Garcia def setUp(self): 410bcdce5a7SAlberto Garcia iotests.create_image(self.img0, self.image_len) 411bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1) 412bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2) 413bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3) 414bcdce5a7SAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1) 415bcdce5a7SAlberto Garcia self.vm = iotests.VM().add_drive(self.img3) 416bcdce5a7SAlberto Garcia self.vm.launch() 417bcdce5a7SAlberto Garcia 418bcdce5a7SAlberto Garcia def tearDown(self): 419bcdce5a7SAlberto Garcia self.vm.shutdown() 420bcdce5a7SAlberto Garcia os.remove(self.img0) 421bcdce5a7SAlberto Garcia os.remove(self.img1) 422bcdce5a7SAlberto Garcia os.remove(self.img2) 423bcdce5a7SAlberto Garcia os.remove(self.img3) 424bcdce5a7SAlberto Garcia 425bcdce5a7SAlberto Garcia # This tests what happens when the overlay image of the 'top' node 426bcdce5a7SAlberto Garcia # needs to be reopened in read-write mode in order to update the 427bcdce5a7SAlberto Garcia # backing image string. 428bcdce5a7SAlberto Garcia def test_reopen_overlay(self): 429bcdce5a7SAlberto Garcia self.run_commit_test(self.img1, self.img0) 430bcdce5a7SAlberto Garcia 431747051cdSJeff Codyif __name__ == '__main__': 432747051cdSJeff Cody iotests.main(supported_fmts=['qcow2', 'qed']) 433