xref: /openbmc/qemu/tests/avocado/hotplug_blk.py (revision d5f6cbb2)
1# Functional test that hotplugs a virtio blk disk and checks it on a Linux
2# guest
3#
4# Copyright (c) 2021 Red Hat, Inc.
5# Copyright (c) Yandex
6#
7# This work is licensed under the terms of the GNU GPL, version 2 or
8# later.  See the COPYING file in the top-level directory.
9
10import time
11
12from avocado_qemu.linuxtest import LinuxTest
13
14
15class HotPlug(LinuxTest):
16    def blockdev_add(self) -> None:
17        self.vm.cmd('blockdev-add', **{
18            'driver': 'null-co',
19            'size': 1073741824,
20            'node-name': 'disk'
21        })
22
23    def assert_vda(self) -> None:
24        self.ssh_command('test -e /sys/block/vda')
25
26    def assert_no_vda(self) -> None:
27        with self.assertRaises(AssertionError):
28            self.assert_vda()
29
30    def plug(self) -> None:
31        args = {
32            'driver': 'virtio-blk-pci',
33            'drive': 'disk',
34            'id': 'virtio-disk0',
35            'bus': 'pci.1',
36            'addr': 1
37        }
38
39        self.assert_no_vda()
40        self.vm.cmd('device_add', args)
41        try:
42            self.assert_vda()
43        except AssertionError:
44            time.sleep(1)
45            self.assert_vda()
46
47    def unplug(self) -> None:
48        self.vm.cmd('device_del', id='virtio-disk0')
49
50        self.vm.event_wait('DEVICE_DELETED', 1.0,
51                           match={'data': {'device': 'virtio-disk0'}})
52
53        self.assert_no_vda()
54
55    def test(self) -> None:
56        """
57        :avocado: tags=arch:x86_64
58        :avocado: tags=machine:q35
59        :avocado: tags=accel:kvm
60        """
61        self.require_accelerator('kvm')
62        self.vm.add_args('-accel', 'kvm')
63        self.vm.add_args('-device', 'pcie-pci-bridge,id=pci.1,bus=pcie.0')
64
65        self.launch_and_wait()
66        self.blockdev_add()
67
68        self.plug()
69        self.unplug()
70