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 29747051cdSJeff Cody 30747051cdSJeff Codybacking_img = os.path.join(iotests.test_dir, 'backing.img') 31747051cdSJeff Codymid_img = os.path.join(iotests.test_dir, 'mid.img') 32747051cdSJeff Codytest_img = os.path.join(iotests.test_dir, 'test.img') 33747051cdSJeff Cody 34747051cdSJeff Codyclass ImageCommitTestCase(iotests.QMPTestCase): 35747051cdSJeff Cody '''Abstract base class for image commit test cases''' 36747051cdSJeff Cody 37747051cdSJeff Cody def assert_no_active_commit(self): 38747051cdSJeff Cody result = self.vm.qmp('query-block-jobs') 39747051cdSJeff Cody self.assert_qmp(result, 'return', []) 40747051cdSJeff Cody 41747051cdSJeff Cody def cancel_and_wait(self, drive='drive0'): 42747051cdSJeff Cody '''Cancel a block job and wait for it to finish''' 43747051cdSJeff Cody result = self.vm.qmp('block-job-cancel', device=drive) 44747051cdSJeff Cody self.assert_qmp(result, 'return', {}) 45747051cdSJeff Cody 46747051cdSJeff Cody cancelled = False 47747051cdSJeff Cody while not cancelled: 48747051cdSJeff Cody for event in self.vm.get_qmp_events(wait=True): 49747051cdSJeff Cody if event['event'] == 'BLOCK_JOB_CANCELLED': 50747051cdSJeff Cody self.assert_qmp(event, 'data/type', 'commit') 51747051cdSJeff Cody self.assert_qmp(event, 'data/device', drive) 52747051cdSJeff Cody cancelled = True 53747051cdSJeff Cody 54747051cdSJeff Cody self.assert_no_active_commit() 55747051cdSJeff Cody 56747051cdSJeff Cody def create_image(self, name, size): 57747051cdSJeff Cody file = open(name, 'w') 58747051cdSJeff Cody i = 0 59747051cdSJeff Cody while i < size: 60747051cdSJeff Cody sector = struct.pack('>l504xl', i / 512, i / 512) 61747051cdSJeff Cody file.write(sector) 62747051cdSJeff Cody i = i + 512 63747051cdSJeff Cody file.close() 64747051cdSJeff Cody 65747051cdSJeff Cody 66747051cdSJeff Codyclass TestSingleDrive(ImageCommitTestCase): 67747051cdSJeff Cody image_len = 1 * 1024 * 1024 68747051cdSJeff Cody test_len = 1 * 1024 * 256 69747051cdSJeff Cody 70747051cdSJeff Cody def setUp(self): 71747051cdSJeff Cody self.create_image(backing_img, TestSingleDrive.image_len) 72747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 73747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 74747051cdSJeff Cody qemu_io('-c', 'write -P 0xab 0 524288', backing_img) 75747051cdSJeff Cody qemu_io('-c', 'write -P 0xef 524288 524288', mid_img) 76747051cdSJeff Cody self.vm = iotests.VM().add_drive(test_img) 77747051cdSJeff Cody self.vm.launch() 78747051cdSJeff Cody 79747051cdSJeff Cody def tearDown(self): 80747051cdSJeff Cody self.vm.shutdown() 81747051cdSJeff Cody os.remove(test_img) 82747051cdSJeff Cody os.remove(mid_img) 83747051cdSJeff Cody os.remove(backing_img) 84747051cdSJeff Cody 85747051cdSJeff Cody def test_commit(self): 86747051cdSJeff Cody self.assert_no_active_commit() 87747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img) 88747051cdSJeff Cody self.assert_qmp(result, 'return', {}) 89747051cdSJeff Cody 90747051cdSJeff Cody completed = False 91747051cdSJeff Cody while not completed: 92747051cdSJeff Cody for event in self.vm.get_qmp_events(wait=True): 93747051cdSJeff Cody if event['event'] == 'BLOCK_JOB_COMPLETED': 94747051cdSJeff Cody self.assert_qmp(event, 'data/type', 'commit') 95747051cdSJeff Cody self.assert_qmp(event, 'data/device', 'drive0') 96747051cdSJeff Cody self.assert_qmp(event, 'data/offset', self.image_len) 97747051cdSJeff Cody self.assert_qmp(event, 'data/len', self.image_len) 98747051cdSJeff Cody completed = True 99747051cdSJeff Cody 100747051cdSJeff Cody self.assert_no_active_commit() 101747051cdSJeff Cody self.vm.shutdown() 102747051cdSJeff Cody 103747051cdSJeff Cody self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 104747051cdSJeff Cody self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 105747051cdSJeff Cody 106747051cdSJeff Cody def test_device_not_found(self): 107747051cdSJeff Cody result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img) 108747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'DeviceNotFound') 109747051cdSJeff Cody 110747051cdSJeff Cody def test_top_same_base(self): 111747051cdSJeff Cody self.assert_no_active_commit() 112747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img) 113747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 114*d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img) 115747051cdSJeff Cody 116747051cdSJeff Cody def test_top_invalid(self): 117747051cdSJeff Cody self.assert_no_active_commit() 118747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img) 119747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 120747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 121747051cdSJeff Cody 122747051cdSJeff Cody def test_base_invalid(self): 123747051cdSJeff Cody self.assert_no_active_commit() 124747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile') 125747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 126747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 127747051cdSJeff Cody 128747051cdSJeff Cody def test_top_is_active(self): 129747051cdSJeff Cody self.assert_no_active_commit() 130747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % test_img, base='%s' % backing_img) 131747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 132747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Top image as the active layer is currently unsupported') 133747051cdSJeff Cody 134747051cdSJeff Cody def test_top_and_base_reversed(self): 135747051cdSJeff Cody self.assert_no_active_commit() 136747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img) 137747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 138*d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img) 139747051cdSJeff Cody 140747051cdSJeff Cody def test_top_omitted(self): 141747051cdSJeff Cody self.assert_no_active_commit() 142747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0') 143747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 144747051cdSJeff Cody self.assert_qmp(result, 'error/desc', "Parameter 'top' is missing") 145747051cdSJeff Cody 146747051cdSJeff Cody 147747051cdSJeff Codyclass TestSetSpeed(ImageCommitTestCase): 148747051cdSJeff Cody image_len = 80 * 1024 * 1024 # MB 149747051cdSJeff Cody 150747051cdSJeff Cody def setUp(self): 151747051cdSJeff Cody qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 152747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 153747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 154747051cdSJeff Cody self.vm = iotests.VM().add_drive(test_img) 155747051cdSJeff Cody self.vm.launch() 156747051cdSJeff Cody 157747051cdSJeff Cody def tearDown(self): 158747051cdSJeff Cody self.vm.shutdown() 159747051cdSJeff Cody os.remove(test_img) 160747051cdSJeff Cody os.remove(mid_img) 161747051cdSJeff Cody os.remove(backing_img) 162747051cdSJeff Cody 163747051cdSJeff Cody def test_set_speed(self): 164747051cdSJeff Cody self.assert_no_active_commit() 165747051cdSJeff Cody 166747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024) 167747051cdSJeff Cody self.assert_qmp(result, 'return', {}) 168747051cdSJeff Cody 169747051cdSJeff Cody # Ensure the speed we set was accepted 170747051cdSJeff Cody result = self.vm.qmp('query-block-jobs') 171747051cdSJeff Cody self.assert_qmp(result, 'return[0]/device', 'drive0') 172747051cdSJeff Cody self.assert_qmp(result, 'return[0]/speed', 1024 * 1024) 173747051cdSJeff Cody 174747051cdSJeff Cody self.cancel_and_wait() 175747051cdSJeff Cody 176747051cdSJeff Cody 177747051cdSJeff Codyif __name__ == '__main__': 178747051cdSJeff Cody iotests.main(supported_fmts=['qcow2', 'qed']) 179