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': 44*bcdce5a7SAlberto 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 607676e2c5SJeff Cody def run_commit_test(self, top, base, need_ready=False): 617676e2c5SJeff Cody self.assert_no_active_block_jobs() 627676e2c5SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top=top, base=base) 637676e2c5SJeff Cody self.assert_qmp(result, 'return', {}) 647676e2c5SJeff Cody self.wait_for_complete(need_ready) 657676e2c5SJeff Cody 667676e2c5SJeff Cody def run_default_commit_test(self): 677676e2c5SJeff Cody self.assert_no_active_block_jobs() 687676e2c5SJeff Cody result = self.vm.qmp('block-commit', device='drive0') 697676e2c5SJeff Cody self.assert_qmp(result, 'return', {}) 707676e2c5SJeff Cody self.wait_for_complete() 717676e2c5SJeff Cody 72747051cdSJeff Codyclass TestSingleDrive(ImageCommitTestCase): 73747051cdSJeff Cody image_len = 1 * 1024 * 1024 74747051cdSJeff Cody test_len = 1 * 1024 * 256 75747051cdSJeff Cody 76747051cdSJeff Cody def setUp(self): 778b9a30caSFam Zheng iotests.create_image(backing_img, self.image_len) 78747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 79747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 8090c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img) 8190c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img) 82747051cdSJeff Cody self.vm = iotests.VM().add_drive(test_img) 83747051cdSJeff Cody self.vm.launch() 84747051cdSJeff Cody 85747051cdSJeff Cody def tearDown(self): 86747051cdSJeff Cody self.vm.shutdown() 87747051cdSJeff Cody os.remove(test_img) 88747051cdSJeff Cody os.remove(mid_img) 89747051cdSJeff Cody os.remove(backing_img) 90747051cdSJeff Cody 91747051cdSJeff Cody def test_commit(self): 924de43470SFam Zheng self.run_commit_test(mid_img, backing_img) 9390c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 9490c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 95747051cdSJeff Cody 96747051cdSJeff Cody def test_device_not_found(self): 97747051cdSJeff Cody result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img) 98747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'DeviceNotFound') 99747051cdSJeff Cody 100747051cdSJeff Cody def test_top_same_base(self): 101fb0a078fSFam Zheng self.assert_no_active_block_jobs() 102747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img) 103747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 104d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img) 105747051cdSJeff Cody 106747051cdSJeff Cody def test_top_invalid(self): 107fb0a078fSFam Zheng self.assert_no_active_block_jobs() 108747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img) 109747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 110747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 111747051cdSJeff Cody 112747051cdSJeff Cody def test_base_invalid(self): 113fb0a078fSFam Zheng self.assert_no_active_block_jobs() 114747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile') 115747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 116747051cdSJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 117747051cdSJeff Cody 118747051cdSJeff Cody def test_top_is_active(self): 1198b9a30caSFam Zheng self.run_commit_test(test_img, backing_img, need_ready=True) 12090c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 12190c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 122747051cdSJeff Cody 1237676e2c5SJeff Cody def test_top_is_default_active(self): 1247676e2c5SJeff Cody self.run_default_commit_test() 12590c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed")) 12690c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed")) 1277676e2c5SJeff Cody 128747051cdSJeff Cody def test_top_and_base_reversed(self): 129fb0a078fSFam Zheng self.assert_no_active_block_jobs() 130747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img) 131747051cdSJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 132d5208c45SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img) 133747051cdSJeff Cody 134747051cdSJeff Cody 1356bf0d1f4SJeff Codyclass TestRelativePaths(ImageCommitTestCase): 1366bf0d1f4SJeff Cody image_len = 1 * 1024 * 1024 1376bf0d1f4SJeff Cody test_len = 1 * 1024 * 256 1386bf0d1f4SJeff Cody 1396bf0d1f4SJeff Cody dir1 = "dir1" 1406bf0d1f4SJeff Cody dir2 = "dir2/" 1416bf0d1f4SJeff Cody dir3 = "dir2/dir3/" 1426bf0d1f4SJeff Cody 1436bf0d1f4SJeff Cody test_img = os.path.join(iotests.test_dir, dir3, 'test.img') 1446bf0d1f4SJeff Cody mid_img = "../mid.img" 1456bf0d1f4SJeff Cody backing_img = "../dir1/backing.img" 1466bf0d1f4SJeff Cody 1476bf0d1f4SJeff Cody backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img') 1486bf0d1f4SJeff Cody mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img') 1496bf0d1f4SJeff Cody 1506bf0d1f4SJeff Cody def setUp(self): 1516bf0d1f4SJeff Cody try: 1526bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir1)) 1536bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir2)) 1546bf0d1f4SJeff Cody os.mkdir(os.path.join(iotests.test_dir, self.dir3)) 1556bf0d1f4SJeff Cody except OSError as exception: 1566bf0d1f4SJeff Cody if exception.errno != errno.EEXIST: 1576bf0d1f4SJeff Cody raise 158915365a9SFam Zheng iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len) 1596bf0d1f4SJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs) 1606bf0d1f4SJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img) 1616bf0d1f4SJeff Cody qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs) 1626bf0d1f4SJeff Cody qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img) 16390c9b167SKevin Wolf qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs) 16490c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs) 1656bf0d1f4SJeff Cody self.vm = iotests.VM().add_drive(self.test_img) 1666bf0d1f4SJeff Cody self.vm.launch() 1676bf0d1f4SJeff Cody 1686bf0d1f4SJeff Cody def tearDown(self): 1696bf0d1f4SJeff Cody self.vm.shutdown() 1706bf0d1f4SJeff Cody os.remove(self.test_img) 1716bf0d1f4SJeff Cody os.remove(self.mid_img_abs) 1726bf0d1f4SJeff Cody os.remove(self.backing_img_abs) 1736bf0d1f4SJeff Cody try: 1746bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir1)) 1756bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir3)) 1766bf0d1f4SJeff Cody os.rmdir(os.path.join(iotests.test_dir, self.dir2)) 1776bf0d1f4SJeff Cody except OSError as exception: 1786bf0d1f4SJeff Cody if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY: 1796bf0d1f4SJeff Cody raise 1806bf0d1f4SJeff Cody 1816bf0d1f4SJeff Cody def test_commit(self): 1824de43470SFam Zheng self.run_commit_test(self.mid_img, self.backing_img) 18390c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 18490c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 1856bf0d1f4SJeff Cody 1866bf0d1f4SJeff Cody def test_device_not_found(self): 1876bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img) 1886bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'DeviceNotFound') 1896bf0d1f4SJeff Cody 1906bf0d1f4SJeff Cody def test_top_same_base(self): 191fb0a078fSFam Zheng self.assert_no_active_block_jobs() 1926bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img) 1936bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 1946bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 1956bf0d1f4SJeff Cody 1966bf0d1f4SJeff Cody def test_top_invalid(self): 197fb0a078fSFam Zheng self.assert_no_active_block_jobs() 1986bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img) 1996bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 2006bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Top image file badfile not found') 2016bf0d1f4SJeff Cody 2026bf0d1f4SJeff Cody def test_base_invalid(self): 203fb0a078fSFam Zheng self.assert_no_active_block_jobs() 2046bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile') 2056bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 2066bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found') 2076bf0d1f4SJeff Cody 2086bf0d1f4SJeff Cody def test_top_is_active(self): 2094de43470SFam Zheng self.run_commit_test(self.test_img, self.backing_img) 21090c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed")) 21190c9b167SKevin Wolf self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed")) 2126bf0d1f4SJeff Cody 2136bf0d1f4SJeff Cody def test_top_and_base_reversed(self): 214fb0a078fSFam Zheng self.assert_no_active_block_jobs() 2156bf0d1f4SJeff Cody result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img) 2166bf0d1f4SJeff Cody self.assert_qmp(result, 'error/class', 'GenericError') 2176bf0d1f4SJeff Cody self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img) 2186bf0d1f4SJeff Cody 219747051cdSJeff Cody 220747051cdSJeff Codyclass TestSetSpeed(ImageCommitTestCase): 221747051cdSJeff Cody image_len = 80 * 1024 * 1024 # MB 222747051cdSJeff Cody 223747051cdSJeff Cody def setUp(self): 224747051cdSJeff Cody qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 225747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img) 226747051cdSJeff Cody qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img) 22790c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img) 22890c9b167SKevin Wolf qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img) 229747051cdSJeff Cody self.vm = iotests.VM().add_drive(test_img) 230747051cdSJeff Cody self.vm.launch() 231747051cdSJeff Cody 232747051cdSJeff Cody def tearDown(self): 233747051cdSJeff Cody self.vm.shutdown() 234747051cdSJeff Cody os.remove(test_img) 235747051cdSJeff Cody os.remove(mid_img) 236747051cdSJeff Cody os.remove(backing_img) 237747051cdSJeff Cody 238747051cdSJeff Cody def test_set_speed(self): 239fb0a078fSFam Zheng self.assert_no_active_block_jobs() 240747051cdSJeff Cody 241b59b3d57SFam Zheng self.vm.pause_drive('drive0') 242747051cdSJeff Cody result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024) 243747051cdSJeff Cody self.assert_qmp(result, 'return', {}) 244747051cdSJeff Cody 245747051cdSJeff Cody # Ensure the speed we set was accepted 246747051cdSJeff Cody result = self.vm.qmp('query-block-jobs') 247747051cdSJeff Cody self.assert_qmp(result, 'return[0]/device', 'drive0') 248747051cdSJeff Cody self.assert_qmp(result, 'return[0]/speed', 1024 * 1024) 249747051cdSJeff Cody 250b59b3d57SFam Zheng self.cancel_and_wait(resume=True) 251747051cdSJeff Cody 2528b9a30caSFam Zhengclass TestActiveZeroLengthImage(TestSingleDrive): 2538b9a30caSFam Zheng image_len = 0 254747051cdSJeff Cody 255*bcdce5a7SAlberto Garciaclass TestReopenOverlay(ImageCommitTestCase): 256*bcdce5a7SAlberto Garcia image_len = 1024 * 1024 257*bcdce5a7SAlberto Garcia img0 = os.path.join(iotests.test_dir, '0.img') 258*bcdce5a7SAlberto Garcia img1 = os.path.join(iotests.test_dir, '1.img') 259*bcdce5a7SAlberto Garcia img2 = os.path.join(iotests.test_dir, '2.img') 260*bcdce5a7SAlberto Garcia img3 = os.path.join(iotests.test_dir, '3.img') 261*bcdce5a7SAlberto Garcia 262*bcdce5a7SAlberto Garcia def setUp(self): 263*bcdce5a7SAlberto Garcia iotests.create_image(self.img0, self.image_len) 264*bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1) 265*bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2) 266*bcdce5a7SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3) 267*bcdce5a7SAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1) 268*bcdce5a7SAlberto Garcia self.vm = iotests.VM().add_drive(self.img3) 269*bcdce5a7SAlberto Garcia self.vm.launch() 270*bcdce5a7SAlberto Garcia 271*bcdce5a7SAlberto Garcia def tearDown(self): 272*bcdce5a7SAlberto Garcia self.vm.shutdown() 273*bcdce5a7SAlberto Garcia os.remove(self.img0) 274*bcdce5a7SAlberto Garcia os.remove(self.img1) 275*bcdce5a7SAlberto Garcia os.remove(self.img2) 276*bcdce5a7SAlberto Garcia os.remove(self.img3) 277*bcdce5a7SAlberto Garcia 278*bcdce5a7SAlberto Garcia # This tests what happens when the overlay image of the 'top' node 279*bcdce5a7SAlberto Garcia # needs to be reopened in read-write mode in order to update the 280*bcdce5a7SAlberto Garcia # backing image string. 281*bcdce5a7SAlberto Garcia def test_reopen_overlay(self): 282*bcdce5a7SAlberto Garcia self.run_commit_test(self.img1, self.img0) 283*bcdce5a7SAlberto Garcia 284747051cdSJeff Codyif __name__ == '__main__': 285747051cdSJeff Cody iotests.main(supported_fmts=['qcow2', 'qed']) 286