4 # Test graph changes while I/O is happening
6 # Copyright (C) 2022 Red Hat, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from threading import Thread
25 from iotests import imgfmt, qemu_img, qemu_img_create, QMPTestCase, \
29 top = os.path.join(iotests.test_dir, 'top.img')
30 nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
33 def do_qemu_img_bench() -> None:
35 Do some I/O requests on `nbd_sock`.
37 assert qemu_img('bench', '-f', 'raw', '-c', '2000000',
38 f'nbd+unix:///node0?socket={nbd_sock}') == 0
41 class TestGraphChangesWhileIO(QMPTestCase):
42 def setUp(self) -> None:
43 # Create an overlay that can be added at runtime on top of the
44 # null-co block node that will receive I/O
45 assert qemu_img_create('-f', imgfmt, '-F', 'raw', '-b', 'null-co://',
48 # QSD instance with a null-co block node in an I/O thread,
49 # exported over NBD (on `nbd_sock`, export name "node0")
50 self.qsd = QemuStorageDaemon(
51 '--object', 'iothread,id=iothread0',
52 '--blockdev', 'null-co,node-name=node0,read-zeroes=true',
53 '--nbd-server', f'addr.type=unix,addr.path={nbd_sock}',
54 '--export', 'nbd,id=exp0,node-name=node0,iothread=iothread0,' +
55 'fixed-iothread=true,writable=true',
59 def tearDown(self) -> None:
62 def test_blockdev_add_while_io(self) -> None:
63 # Run qemu-img bench in the background
64 bench_thr = Thread(target=do_qemu_img_bench)
67 # While qemu-img bench is running, repeatedly add and remove an
68 # overlay to/from node0
69 while bench_thr.is_alive():
70 result = self.qsd.qmp('blockdev-add', {
72 'node-name': 'overlay',
79 self.assert_qmp(result, 'return', {})
81 result = self.qsd.qmp('blockdev-del', {
82 'node-name': 'overlay'
84 self.assert_qmp(result, 'return', {})
88 if __name__ == '__main__':
89 # Format must support raw backing files
90 iotests.main(supported_fmts=['qcow', 'qcow2', 'qed'],
91 supported_protocols=['file'])