xref: /openbmc/qemu/tests/qemu-iotests/240 (revision c00506aa)
1#!/usr/bin/env python3
2
3# Test hot plugging and unplugging with iothreads
4#
5# Copyright (C) 2019 Igalia, S.L.
6# Author: Alberto Garcia <berto@igalia.com>
7#
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.
12#
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.
17#
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/>.
20
21import iotests
22import os
23
24nbd_sock = iotests.file_path('nbd.sock', base_dir=iotests.sock_dir)
25
26class TestCase(iotests.QMPTestCase):
27    test_driver = "null-co"
28
29    def required_drivers(self):
30        return [self.test_driver]
31
32    @iotests.skip_if_unsupported(required_drivers)
33    def setUp(self):
34        self.vm = iotests.VM()
35        self.vm.launch()
36
37    def tearDown(self):
38        self.vm.shutdown()
39
40    def test1(self):
41        iotests.log('==Unplug a SCSI disk and then plug it again==')
42        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0')
43        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
44        self.vm.qmp_log('device_add', id='scsi0', driver=iotests.get_virtio_scsi_device(), iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
45        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
46        self.vm.qmp_log('device_del', id='scsi-hd0')
47        self.vm.event_wait('DEVICE_DELETED')
48        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
49        self.vm.qmp_log('device_del', id='scsi-hd0')
50        self.vm.event_wait('DEVICE_DELETED')
51        self.vm.qmp_log('blockdev-del', node_name='hd0')
52
53    def test2(self):
54        iotests.log('==Attach two SCSI disks using the same block device and the same iothread==')
55        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0', read_only=True)
56        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
57        self.vm.qmp_log('device_add', id='scsi0', driver=iotests.get_virtio_scsi_device(), iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
58
59        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
60        self.vm.qmp_log('device_add', id='scsi-hd1', driver='scsi-hd', drive='hd0')
61        self.vm.qmp_log('device_del', id='scsi-hd0')
62        self.vm.event_wait('DEVICE_DELETED')
63        self.vm.qmp_log('device_del', id='scsi-hd1')
64        self.vm.event_wait('DEVICE_DELETED')
65        self.vm.qmp_log('blockdev-del', node_name='hd0')
66
67    def test3(self):
68        iotests.log('==Attach two SCSI disks using the same block device but different iothreads==')
69
70        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0', read_only=True)
71
72        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
73        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread1")
74
75        self.vm.qmp_log('device_add', id='scsi0', driver=iotests.get_virtio_scsi_device(), iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
76        self.vm.qmp_log('device_add', id='scsi1', driver=iotests.get_virtio_scsi_device(), iothread='iothread1', filters=[iotests.filter_qmp_virtio_scsi])
77
78        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0', bus="scsi0.0")
79        self.vm.qmp_log('device_add', id='scsi-hd1', driver='scsi-hd', drive='hd0', bus="scsi1.0")
80
81        self.vm.qmp_log('device_del', id='scsi-hd0')
82        self.vm.event_wait('DEVICE_DELETED')
83        self.vm.qmp_log('device_add', id='scsi-hd1', driver='scsi-hd', drive='hd0', bus="scsi1.0")
84
85        self.vm.qmp_log('device_del', id='scsi-hd1')
86        self.vm.event_wait('DEVICE_DELETED')
87        self.vm.qmp_log('blockdev-del', node_name='hd0')
88
89    def test4(self):
90        iotests.log('==Attach a SCSI disks using the same block device as a NBD server==')
91
92        self.vm.qmp_log('blockdev-add', driver='null-co', read_zeroes=True, node_name='hd0', read_only=True)
93
94        self.vm.qmp_log('nbd-server-start',
95                        filters=[iotests.filter_qmp_testfiles],
96                        addr={'type':'unix', 'data':{'path':nbd_sock}})
97
98        self.vm.qmp_log('nbd-server-add', device='hd0')
99
100        self.vm.qmp_log('object-add', qom_type='iothread', id="iothread0")
101        self.vm.qmp_log('device_add', id='scsi0', driver=iotests.get_virtio_scsi_device(), iothread='iothread0', filters=[iotests.filter_qmp_virtio_scsi])
102        self.vm.qmp_log('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')
103
104if __name__ == '__main__':
105    iotests.activate_logging()
106    iotests.main()
107