1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick 39b8c59e7SSergio Lopez# 49b8c59e7SSergio Lopez# Test cases for blockdev + IOThread interactions 59b8c59e7SSergio Lopez# 69b8c59e7SSergio Lopez# Copyright (C) 2019 Red Hat, Inc. 79b8c59e7SSergio Lopez# 89b8c59e7SSergio Lopez# This program is free software; you can redistribute it and/or modify 99b8c59e7SSergio Lopez# it under the terms of the GNU General Public License as published by 109b8c59e7SSergio Lopez# the Free Software Foundation; either version 2 of the License, or 119b8c59e7SSergio Lopez# (at your option) any later version. 129b8c59e7SSergio Lopez# 139b8c59e7SSergio Lopez# This program is distributed in the hope that it will be useful, 149b8c59e7SSergio Lopez# but WITHOUT ANY WARRANTY; without even the implied warranty of 159b8c59e7SSergio Lopez# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 169b8c59e7SSergio Lopez# GNU General Public License for more details. 179b8c59e7SSergio Lopez# 189b8c59e7SSergio Lopez# You should have received a copy of the GNU General Public License 199b8c59e7SSergio Lopez# along with this program. If not, see <http://www.gnu.org/licenses/>. 209b8c59e7SSergio Lopez# 219b8c59e7SSergio Lopez 229b8c59e7SSergio Lopezimport os 239b8c59e7SSergio Lopezimport iotests 249b8c59e7SSergio Lopezfrom iotests import qemu_img 259b8c59e7SSergio Lopez 269b8c59e7SSergio Lopezimage_len = 64 * 1024 * 1024 279b8c59e7SSergio Lopez 289b8c59e7SSergio Lopez# Test for RHBZ#1782175 299b8c59e7SSergio Lopezclass TestDirtyBitmapIOThread(iotests.QMPTestCase): 309b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 319b8c59e7SSergio Lopez images = { 'drive0': drive0_img } 329b8c59e7SSergio Lopez 339b8c59e7SSergio Lopez def setUp(self): 349b8c59e7SSergio Lopez for name in self.images: 359b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 369b8c59e7SSergio Lopez self.images[name], str(image_len)) 379b8c59e7SSergio Lopez 389b8c59e7SSergio Lopez self.vm = iotests.VM() 399b8c59e7SSergio Lopez self.vm.add_object('iothread,id=iothread0') 409b8c59e7SSergio Lopez 419b8c59e7SSergio Lopez for name in self.images: 429b8c59e7SSergio Lopez self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s' 439b8c59e7SSergio Lopez % (self.images[name], name)) 449b8c59e7SSergio Lopez self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s' 459b8c59e7SSergio Lopez % (name, name)) 469b8c59e7SSergio Lopez 479b8c59e7SSergio Lopez self.vm.launch() 489b8c59e7SSergio Lopez self.vm.qmp('x-blockdev-set-iothread', 499b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 509b8c59e7SSergio Lopez force=True) 519b8c59e7SSergio Lopez 529b8c59e7SSergio Lopez def tearDown(self): 539b8c59e7SSergio Lopez self.vm.shutdown() 549b8c59e7SSergio Lopez for name in self.images: 559b8c59e7SSergio Lopez os.remove(self.images[name]) 569b8c59e7SSergio Lopez 579b8c59e7SSergio Lopez def test_add_dirty_bitmap(self): 589b8c59e7SSergio Lopez result = self.vm.qmp( 599b8c59e7SSergio Lopez 'block-dirty-bitmap-add', 609b8c59e7SSergio Lopez node='drive0', 619b8c59e7SSergio Lopez name='bitmap1', 629b8c59e7SSergio Lopez persistent=True, 639b8c59e7SSergio Lopez ) 649b8c59e7SSergio Lopez 659b8c59e7SSergio Lopez self.assert_qmp(result, 'return', {}) 669b8c59e7SSergio Lopez 679b8c59e7SSergio Lopez 689b8c59e7SSergio Lopez# Test for RHBZ#1746217 & RHBZ#1773517 699b8c59e7SSergio Lopezclass TestNBDMirrorIOThread(iotests.QMPTestCase): 709b8c59e7SSergio Lopez nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock') 719b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 729b8c59e7SSergio Lopez mirror_img = os.path.join(iotests.test_dir, 'mirror.img') 739b8c59e7SSergio Lopez images = { 'drive0': drive0_img, 'mirror': mirror_img } 749b8c59e7SSergio Lopez 759b8c59e7SSergio Lopez def setUp(self): 769b8c59e7SSergio Lopez for name in self.images: 779b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 789b8c59e7SSergio Lopez self.images[name], str(image_len)) 799b8c59e7SSergio Lopez 809b8c59e7SSergio Lopez self.vm_src = iotests.VM(path_suffix='src') 819b8c59e7SSergio Lopez self.vm_src.add_object('iothread,id=iothread0') 829b8c59e7SSergio Lopez self.vm_src.add_blockdev('driver=file,filename=%s,node-name=file0' 839b8c59e7SSergio Lopez % (self.drive0_img)) 849b8c59e7SSergio Lopez self.vm_src.add_blockdev('driver=qcow2,file=file0,node-name=drive0') 859b8c59e7SSergio Lopez self.vm_src.launch() 869b8c59e7SSergio Lopez self.vm_src.qmp('x-blockdev-set-iothread', 879b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 889b8c59e7SSergio Lopez force=True) 899b8c59e7SSergio Lopez 909b8c59e7SSergio Lopez self.vm_tgt = iotests.VM(path_suffix='tgt') 919b8c59e7SSergio Lopez self.vm_tgt.add_object('iothread,id=iothread0') 929b8c59e7SSergio Lopez self.vm_tgt.add_blockdev('driver=file,filename=%s,node-name=file0' 939b8c59e7SSergio Lopez % (self.mirror_img)) 949b8c59e7SSergio Lopez self.vm_tgt.add_blockdev('driver=qcow2,file=file0,node-name=drive0') 959b8c59e7SSergio Lopez self.vm_tgt.launch() 969b8c59e7SSergio Lopez self.vm_tgt.qmp('x-blockdev-set-iothread', 979b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 989b8c59e7SSergio Lopez force=True) 999b8c59e7SSergio Lopez 1009b8c59e7SSergio Lopez def tearDown(self): 1019b8c59e7SSergio Lopez self.vm_src.shutdown() 1029b8c59e7SSergio Lopez self.vm_tgt.shutdown() 1039b8c59e7SSergio Lopez for name in self.images: 1049b8c59e7SSergio Lopez os.remove(self.images[name]) 1059b8c59e7SSergio Lopez 1069b8c59e7SSergio Lopez def test_nbd_mirror(self): 1079b8c59e7SSergio Lopez result = self.vm_tgt.qmp( 1089b8c59e7SSergio Lopez 'nbd-server-start', 1099b8c59e7SSergio Lopez addr={ 1109b8c59e7SSergio Lopez 'type': 'unix', 1119b8c59e7SSergio Lopez 'data': { 'path': self.nbd_sock } 1129b8c59e7SSergio Lopez } 1139b8c59e7SSergio Lopez ) 1149b8c59e7SSergio Lopez self.assert_qmp(result, 'return', {}) 1159b8c59e7SSergio Lopez 1169b8c59e7SSergio Lopez result = self.vm_tgt.qmp( 1179b8c59e7SSergio Lopez 'nbd-server-add', 1189b8c59e7SSergio Lopez device='drive0', 1199b8c59e7SSergio Lopez writable=True 1209b8c59e7SSergio Lopez ) 1219b8c59e7SSergio Lopez self.assert_qmp(result, 'return', {}) 1229b8c59e7SSergio Lopez 1239b8c59e7SSergio Lopez result = self.vm_src.qmp( 1249b8c59e7SSergio Lopez 'drive-mirror', 1259b8c59e7SSergio Lopez device='drive0', 1269b8c59e7SSergio Lopez target='nbd+unix:///drive0?socket=' + self.nbd_sock, 1279b8c59e7SSergio Lopez sync='full', 1289b8c59e7SSergio Lopez mode='existing', 1299b8c59e7SSergio Lopez speed=64*1024*1024, 1309b8c59e7SSergio Lopez job_id='j1' 1319b8c59e7SSergio Lopez ) 1329b8c59e7SSergio Lopez self.assert_qmp(result, 'return', {}) 1339b8c59e7SSergio Lopez 1349b8c59e7SSergio Lopez self.vm_src.event_wait(name="BLOCK_JOB_READY") 1359b8c59e7SSergio Lopez 1369b8c59e7SSergio Lopez 1379b8c59e7SSergio Lopez# Test for RHBZ#1779036 1389b8c59e7SSergio Lopezclass TestExternalSnapshotAbort(iotests.QMPTestCase): 1399b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 1409b8c59e7SSergio Lopez snapshot_img = os.path.join(iotests.test_dir, 'snapshot.img') 1419b8c59e7SSergio Lopez images = { 'drive0': drive0_img, 'snapshot': snapshot_img } 1429b8c59e7SSergio Lopez 1439b8c59e7SSergio Lopez def setUp(self): 1449b8c59e7SSergio Lopez for name in self.images: 1459b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 1469b8c59e7SSergio Lopez self.images[name], str(image_len)) 1479b8c59e7SSergio Lopez 1489b8c59e7SSergio Lopez self.vm = iotests.VM() 1499b8c59e7SSergio Lopez self.vm.add_object('iothread,id=iothread0') 1509b8c59e7SSergio Lopez self.vm.add_blockdev('driver=file,filename=%s,node-name=file0' 1519b8c59e7SSergio Lopez % (self.drive0_img)) 1529b8c59e7SSergio Lopez self.vm.add_blockdev('driver=qcow2,file=file0,node-name=drive0') 1539b8c59e7SSergio Lopez self.vm.launch() 1549b8c59e7SSergio Lopez self.vm.qmp('x-blockdev-set-iothread', 1559b8c59e7SSergio Lopez node_name='drive0', iothread='iothread0', 1569b8c59e7SSergio Lopez force=True) 1579b8c59e7SSergio Lopez 1589b8c59e7SSergio Lopez def tearDown(self): 1599b8c59e7SSergio Lopez self.vm.shutdown() 1609b8c59e7SSergio Lopez for name in self.images: 1619b8c59e7SSergio Lopez os.remove(self.images[name]) 1629b8c59e7SSergio Lopez 1639b8c59e7SSergio Lopez def test_external_snapshot_abort(self): 1649b8c59e7SSergio Lopez # Use a two actions transaction with a bogus values on the second 1659b8c59e7SSergio Lopez # one to trigger an abort of the transaction. 1669b8c59e7SSergio Lopez result = self.vm.qmp('transaction', actions=[ 1679b8c59e7SSergio Lopez { 1689b8c59e7SSergio Lopez 'type': 'blockdev-snapshot-sync', 1699b8c59e7SSergio Lopez 'data': { 'node-name': 'drive0', 1709b8c59e7SSergio Lopez 'snapshot-file': self.snapshot_img, 1719b8c59e7SSergio Lopez 'snapshot-node-name': 'snap1', 1729b8c59e7SSergio Lopez 'mode': 'absolute-paths', 1739b8c59e7SSergio Lopez 'format': 'qcow2' } 1749b8c59e7SSergio Lopez }, 1759b8c59e7SSergio Lopez { 1769b8c59e7SSergio Lopez 'type': 'blockdev-snapshot-sync', 1779b8c59e7SSergio Lopez 'data': { 'node-name': 'drive0', 1789b8c59e7SSergio Lopez 'snapshot-file': '/fakesnapshot', 1799b8c59e7SSergio Lopez 'snapshot-node-name': 'snap2', 1809b8c59e7SSergio Lopez 'mode': 'absolute-paths', 1819b8c59e7SSergio Lopez 'format': 'qcow2' } 1829b8c59e7SSergio Lopez }, 1839b8c59e7SSergio Lopez ]) 1849b8c59e7SSergio Lopez 1859b8c59e7SSergio Lopez # Crashes on failure, we expect this error. 1869b8c59e7SSergio Lopez self.assert_qmp(result, 'error/class', 'GenericError') 1879b8c59e7SSergio Lopez 1889b8c59e7SSergio Lopez 1899b8c59e7SSergio Lopez# Test for RHBZ#1782111 1909b8c59e7SSergio Lopezclass TestBlockdevBackupAbort(iotests.QMPTestCase): 1919b8c59e7SSergio Lopez drive0_img = os.path.join(iotests.test_dir, 'drive0.img') 1929b8c59e7SSergio Lopez drive1_img = os.path.join(iotests.test_dir, 'drive1.img') 1939b8c59e7SSergio Lopez snap0_img = os.path.join(iotests.test_dir, 'snap0.img') 1949b8c59e7SSergio Lopez snap1_img = os.path.join(iotests.test_dir, 'snap1.img') 1959b8c59e7SSergio Lopez images = { 'drive0': drive0_img, 1969b8c59e7SSergio Lopez 'drive1': drive1_img, 1979b8c59e7SSergio Lopez 'snap0': snap0_img, 1989b8c59e7SSergio Lopez 'snap1': snap1_img } 1999b8c59e7SSergio Lopez 2009b8c59e7SSergio Lopez def setUp(self): 2019b8c59e7SSergio Lopez for name in self.images: 2029b8c59e7SSergio Lopez qemu_img('create', '-f', iotests.imgfmt, 2039b8c59e7SSergio Lopez self.images[name], str(image_len)) 2049b8c59e7SSergio Lopez 2059b8c59e7SSergio Lopez self.vm = iotests.VM() 2069b8c59e7SSergio Lopez self.vm.add_object('iothread,id=iothread0') 2079b8c59e7SSergio Lopez self.vm.add_device('virtio-scsi,iothread=iothread0') 2089b8c59e7SSergio Lopez 2099b8c59e7SSergio Lopez for name in self.images: 2109b8c59e7SSergio Lopez self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s' 2119b8c59e7SSergio Lopez % (self.images[name], name)) 2129b8c59e7SSergio Lopez self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s' 2139b8c59e7SSergio Lopez % (name, name)) 2149b8c59e7SSergio Lopez 2159b8c59e7SSergio Lopez self.vm.add_device('scsi-hd,drive=drive0') 2169b8c59e7SSergio Lopez self.vm.add_device('scsi-hd,drive=drive1') 2179b8c59e7SSergio Lopez self.vm.launch() 2189b8c59e7SSergio Lopez 2199b8c59e7SSergio Lopez def tearDown(self): 2209b8c59e7SSergio Lopez self.vm.shutdown() 2219b8c59e7SSergio Lopez for name in self.images: 2229b8c59e7SSergio Lopez os.remove(self.images[name]) 2239b8c59e7SSergio Lopez 2249b8c59e7SSergio Lopez def test_blockdev_backup_abort(self): 2259b8c59e7SSergio Lopez # Use a two actions transaction with a bogus values on the second 2269b8c59e7SSergio Lopez # one to trigger an abort of the transaction. 2279b8c59e7SSergio Lopez result = self.vm.qmp('transaction', actions=[ 2289b8c59e7SSergio Lopez { 2299b8c59e7SSergio Lopez 'type': 'blockdev-backup', 2309b8c59e7SSergio Lopez 'data': { 'device': 'drive0', 2319b8c59e7SSergio Lopez 'target': 'snap0', 2329b8c59e7SSergio Lopez 'sync': 'full', 2339b8c59e7SSergio Lopez 'job-id': 'j1' } 2349b8c59e7SSergio Lopez }, 2359b8c59e7SSergio Lopez { 2369b8c59e7SSergio Lopez 'type': 'blockdev-backup', 2379b8c59e7SSergio Lopez 'data': { 'device': 'drive1', 2389b8c59e7SSergio Lopez 'target': 'snap1', 2399b8c59e7SSergio Lopez 'sync': 'full' } 2409b8c59e7SSergio Lopez }, 2419b8c59e7SSergio Lopez ]) 2429b8c59e7SSergio Lopez 2439b8c59e7SSergio Lopez # Hangs on failure, we expect this error. 2449b8c59e7SSergio Lopez self.assert_qmp(result, 'error/class', 'GenericError') 2459b8c59e7SSergio Lopez 2469b8c59e7SSergio Lopezif __name__ == '__main__': 2479b8c59e7SSergio Lopez iotests.main(supported_fmts=['qcow2'], 248*b30b8077SVladimir Sementsov-Ogievskiy supported_protocols=['file'], 249*b30b8077SVladimir Sementsov-Ogievskiy unsupported_imgopts=['compat']) 250