xref: /openbmc/qemu/tests/qemu-iotests/281 (revision 5964ed56)
1#!/usr/bin/env python3
2#
3# Test cases for blockdev + IOThread interactions
4#
5# Copyright (C) 2019 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21import os
22import iotests
23from iotests import qemu_img
24
25image_len = 64 * 1024 * 1024
26
27# Test for RHBZ#1782175
28class TestDirtyBitmapIOThread(iotests.QMPTestCase):
29    drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
30    images = { 'drive0': drive0_img }
31
32    def setUp(self):
33        for name in self.images:
34            qemu_img('create', '-f', iotests.imgfmt,
35                     self.images[name], str(image_len))
36
37        self.vm = iotests.VM()
38        self.vm.add_object('iothread,id=iothread0')
39
40        for name in self.images:
41            self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s'
42                                 % (self.images[name], name))
43            self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s'
44                                 % (name, name))
45
46        self.vm.launch()
47        self.vm.qmp('x-blockdev-set-iothread',
48                    node_name='drive0', iothread='iothread0',
49                    force=True)
50
51    def tearDown(self):
52        self.vm.shutdown()
53        for name in self.images:
54            os.remove(self.images[name])
55
56    def test_add_dirty_bitmap(self):
57        result = self.vm.qmp(
58            'block-dirty-bitmap-add',
59            node='drive0',
60            name='bitmap1',
61            persistent=True,
62        )
63
64        self.assert_qmp(result, 'return', {})
65
66
67# Test for RHBZ#1746217 & RHBZ#1773517
68class TestNBDMirrorIOThread(iotests.QMPTestCase):
69    nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
70    drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
71    mirror_img = os.path.join(iotests.test_dir, 'mirror.img')
72    images = { 'drive0': drive0_img, 'mirror': mirror_img }
73
74    def setUp(self):
75        for name in self.images:
76            qemu_img('create', '-f', iotests.imgfmt,
77                     self.images[name], str(image_len))
78
79        self.vm_src = iotests.VM(path_suffix='src')
80        self.vm_src.add_object('iothread,id=iothread0')
81        self.vm_src.add_blockdev('driver=file,filename=%s,node-name=file0'
82                          % (self.drive0_img))
83        self.vm_src.add_blockdev('driver=qcow2,file=file0,node-name=drive0')
84        self.vm_src.launch()
85        self.vm_src.qmp('x-blockdev-set-iothread',
86                        node_name='drive0', iothread='iothread0',
87                        force=True)
88
89        self.vm_tgt = iotests.VM(path_suffix='tgt')
90        self.vm_tgt.add_object('iothread,id=iothread0')
91        self.vm_tgt.add_blockdev('driver=file,filename=%s,node-name=file0'
92                          % (self.mirror_img))
93        self.vm_tgt.add_blockdev('driver=qcow2,file=file0,node-name=drive0')
94        self.vm_tgt.launch()
95        self.vm_tgt.qmp('x-blockdev-set-iothread',
96                        node_name='drive0', iothread='iothread0',
97                        force=True)
98
99    def tearDown(self):
100        self.vm_src.shutdown()
101        self.vm_tgt.shutdown()
102        for name in self.images:
103            os.remove(self.images[name])
104
105    def test_nbd_mirror(self):
106        result = self.vm_tgt.qmp(
107            'nbd-server-start',
108            addr={
109                'type': 'unix',
110                'data': { 'path': self.nbd_sock }
111            }
112        )
113        self.assert_qmp(result, 'return', {})
114
115        result = self.vm_tgt.qmp(
116            'nbd-server-add',
117            device='drive0',
118            writable=True
119        )
120        self.assert_qmp(result, 'return', {})
121
122        result = self.vm_src.qmp(
123            'drive-mirror',
124            device='drive0',
125            target='nbd+unix:///drive0?socket=' + self.nbd_sock,
126            sync='full',
127            mode='existing',
128            speed=64*1024*1024,
129            job_id='j1'
130        )
131        self.assert_qmp(result, 'return', {})
132
133        self.vm_src.event_wait(name="BLOCK_JOB_READY")
134
135
136# Test for RHBZ#1779036
137class TestExternalSnapshotAbort(iotests.QMPTestCase):
138    drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
139    snapshot_img = os.path.join(iotests.test_dir, 'snapshot.img')
140    images = { 'drive0': drive0_img, 'snapshot': snapshot_img }
141
142    def setUp(self):
143        for name in self.images:
144            qemu_img('create', '-f', iotests.imgfmt,
145                     self.images[name], str(image_len))
146
147        self.vm = iotests.VM()
148        self.vm.add_object('iothread,id=iothread0')
149        self.vm.add_blockdev('driver=file,filename=%s,node-name=file0'
150                          % (self.drive0_img))
151        self.vm.add_blockdev('driver=qcow2,file=file0,node-name=drive0')
152        self.vm.launch()
153        self.vm.qmp('x-blockdev-set-iothread',
154                    node_name='drive0', iothread='iothread0',
155                    force=True)
156
157    def tearDown(self):
158        self.vm.shutdown()
159        for name in self.images:
160            os.remove(self.images[name])
161
162    def test_external_snapshot_abort(self):
163        # Use a two actions transaction with a bogus values on the second
164        # one to trigger an abort of the transaction.
165        result = self.vm.qmp('transaction', actions=[
166            {
167                'type': 'blockdev-snapshot-sync',
168                'data': { 'node-name': 'drive0',
169                          'snapshot-file': self.snapshot_img,
170                          'snapshot-node-name': 'snap1',
171                          'mode': 'absolute-paths',
172                          'format': 'qcow2' }
173            },
174            {
175                'type': 'blockdev-snapshot-sync',
176                'data': { 'node-name': 'drive0',
177                          'snapshot-file': '/fakesnapshot',
178                          'snapshot-node-name': 'snap2',
179                          'mode': 'absolute-paths',
180                          'format': 'qcow2' }
181            },
182        ])
183
184        # Crashes on failure, we expect this error.
185        self.assert_qmp(result, 'error/class', 'GenericError')
186
187
188# Test for RHBZ#1782111
189class TestBlockdevBackupAbort(iotests.QMPTestCase):
190    drive0_img = os.path.join(iotests.test_dir, 'drive0.img')
191    drive1_img = os.path.join(iotests.test_dir, 'drive1.img')
192    snap0_img = os.path.join(iotests.test_dir, 'snap0.img')
193    snap1_img = os.path.join(iotests.test_dir, 'snap1.img')
194    images = { 'drive0': drive0_img,
195               'drive1': drive1_img,
196               'snap0': snap0_img,
197               'snap1': snap1_img }
198
199    def setUp(self):
200        for name in self.images:
201            qemu_img('create', '-f', iotests.imgfmt,
202                     self.images[name], str(image_len))
203
204        self.vm = iotests.VM()
205        self.vm.add_object('iothread,id=iothread0')
206        self.vm.add_device('virtio-scsi,iothread=iothread0')
207
208        for name in self.images:
209            self.vm.add_blockdev('driver=file,filename=%s,node-name=file_%s'
210                                 % (self.images[name], name))
211            self.vm.add_blockdev('driver=qcow2,file=file_%s,node-name=%s'
212                                 % (name, name))
213
214        self.vm.add_device('scsi-hd,drive=drive0')
215        self.vm.add_device('scsi-hd,drive=drive1')
216        self.vm.launch()
217
218    def tearDown(self):
219        self.vm.shutdown()
220        for name in self.images:
221            os.remove(self.images[name])
222
223    def test_blockdev_backup_abort(self):
224        # Use a two actions transaction with a bogus values on the second
225        # one to trigger an abort of the transaction.
226        result = self.vm.qmp('transaction', actions=[
227            {
228                'type': 'blockdev-backup',
229                'data': { 'device': 'drive0',
230                          'target': 'snap0',
231                          'sync': 'full',
232                          'job-id': 'j1' }
233            },
234            {
235                'type': 'blockdev-backup',
236                'data': { 'device': 'drive1',
237                          'target': 'snap1',
238                          'sync': 'full' }
239            },
240        ])
241
242        # Hangs on failure, we expect this error.
243        self.assert_qmp(result, 'error/class', 'GenericError')
244
245if __name__ == '__main__':
246    iotests.main(supported_fmts=['qcow2'],
247                 supported_protocols=['file'])
248