xref: /openbmc/qemu/tests/qemu-iotests/240 (revision f0984d40)
1#!/usr/bin/env python3
2# group: quick
3
4# Test hot plugging and unplugging with iothreads
5#
6# Copyright (C) 2019 Igalia, S.L.
7# Author: Alberto Garcia <berto@igalia.com>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22import iotests
23import os
24
25nbd_sock = iotests.file_path('nbd.sock', base_dir=iotests.sock_dir)
26
27class TestCase(iotests.QMPTestCase):
28    test_driver = "null-co"
29
30    def required_drivers(self):
31        return [self.test_driver]
32
33    @iotests.skip_if_unsupported(required_drivers)
34    def setUp(self):
35        self.vm = iotests.VM()
36        self.vm.launch()
37
38    def tearDown(self):
39        self.vm.shutdown()
40
41    def test1(self):
42        iotests.log('==Unplug a SCSI disk and then plug it again==')
43        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0')
44        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
45        self.vm.qmp_log('device_add', id='scsi0', driver='virtio-scsi', iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
46        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
47        self.vm.qmp_log('device_del', id='scsi-hd0')
48        self.vm.event_wait('DEVICE_DELETED')
49        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
50        self.vm.qmp_log('device_del', id='scsi-hd0')
51        self.vm.event_wait('DEVICE_DELETED')
52        self.vm.qmp_log('blockdev-del', node_name='hd0')
53
54    def test2(self):
55        iotests.log('==Attach two SCSI disks using the same block device and the same iothread==')
56        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0', read_only=True)
57        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
58        self.vm.qmp_log('device_add', id='scsi0', driver='virtio-scsi', iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
59
60        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
61        self.vm.qmp_log('device_add', id='scsi-hd1', driver='scsi-hd', drive='hd0')
62        self.vm.qmp_log('device_del', id='scsi-hd0')
63        self.vm.event_wait('DEVICE_DELETED')
64        self.vm.qmp_log('device_del', id='scsi-hd1')
65        self.vm.event_wait('DEVICE_DELETED')
66        self.vm.qmp_log('blockdev-del', node_name='hd0')
67
68    def test3(self):
69        iotests.log('==Attach two SCSI disks using the same block device but different iothreads==')
70
71        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0', read_only=True)
72
73        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
74        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread1")
75
76        self.vm.qmp_log('device_add', id='scsi0', driver='virtio-scsi', iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
77        self.vm.qmp_log('device_add', id='scsi1', driver='virtio-scsi', iothread='iothread1', filters=[iotests.filter_qmp_virtio_scsi])
78
79        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0', bus="scsi0.0")
80        self.vm.qmp_log('device_add', id='scsi-hd1', driver='scsi-hd', drive='hd0', bus="scsi1.0")
81
82        self.vm.qmp_log('device_del', id='scsi-hd0')
83        self.vm.event_wait('DEVICE_DELETED')
84        self.vm.qmp_log('device_add', id='scsi-hd1', driver='scsi-hd', drive='hd0', bus="scsi1.0")
85
86        self.vm.qmp_log('device_del', id='scsi-hd1')
87        self.vm.event_wait('DEVICE_DELETED')
88        self.vm.qmp_log('blockdev-del', node_name='hd0')
89
90    def test4(self):
91        iotests.log('==Attach a SCSI disks using the same block device as a NBD server==')
92
93        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0', read_only=True)
94
95        self.vm.qmp_log('nbd-server-start',
96                        filters=[iotests.filter_qmp_testfiles],
97                        addr={'type':'unix', 'data':{'path':nbd_sock}})
98
99        self.vm.qmp_log('nbd-server-add', device='hd0')
100
101        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
102        self.vm.qmp_log('device_add', id='scsi0', driver='virtio-scsi', iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
103        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
104
105if __name__ == '__main__':
106    iotests.activate_logging()
107    iotests.main()
108