xref: /openbmc/qemu/tests/avocado/hotplug_blk.py (revision 816d4201)
199a28bd5SVladimir Sementsov-Ogievskiy# Functional test that hotplugs a virtio blk disk and checks it on a Linux
299a28bd5SVladimir Sementsov-Ogievskiy# guest
399a28bd5SVladimir Sementsov-Ogievskiy#
499a28bd5SVladimir Sementsov-Ogievskiy# Copyright (c) 2021 Red Hat, Inc.
599a28bd5SVladimir Sementsov-Ogievskiy# Copyright (c) Yandex
699a28bd5SVladimir Sementsov-Ogievskiy#
799a28bd5SVladimir Sementsov-Ogievskiy# This work is licensed under the terms of the GNU GPL, version 2 or
899a28bd5SVladimir Sementsov-Ogievskiy# later.  See the COPYING file in the top-level directory.
999a28bd5SVladimir Sementsov-Ogievskiy
1099a28bd5SVladimir Sementsov-Ogievskiyimport time
1199a28bd5SVladimir Sementsov-Ogievskiy
12*816d4201SThomas Huthfrom avocado_qemu.linuxtest import LinuxTest
1399a28bd5SVladimir Sementsov-Ogievskiy
1499a28bd5SVladimir Sementsov-Ogievskiy
1599a28bd5SVladimir Sementsov-Ogievskiyclass HotPlug(LinuxTest):
1699a28bd5SVladimir Sementsov-Ogievskiy    def blockdev_add(self) -> None:
1799a28bd5SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', **{
1899a28bd5SVladimir Sementsov-Ogievskiy            'driver': 'null-co',
1999a28bd5SVladimir Sementsov-Ogievskiy            'size': 1073741824,
2099a28bd5SVladimir Sementsov-Ogievskiy            'node-name': 'disk'
2199a28bd5SVladimir Sementsov-Ogievskiy        })
2299a28bd5SVladimir Sementsov-Ogievskiy
2399a28bd5SVladimir Sementsov-Ogievskiy    def assert_vda(self) -> None:
2499a28bd5SVladimir Sementsov-Ogievskiy        self.ssh_command('test -e /sys/block/vda')
2599a28bd5SVladimir Sementsov-Ogievskiy
2699a28bd5SVladimir Sementsov-Ogievskiy    def assert_no_vda(self) -> None:
2799a28bd5SVladimir Sementsov-Ogievskiy        with self.assertRaises(AssertionError):
2899a28bd5SVladimir Sementsov-Ogievskiy            self.assert_vda()
2999a28bd5SVladimir Sementsov-Ogievskiy
3099a28bd5SVladimir Sementsov-Ogievskiy    def plug(self) -> None:
3199a28bd5SVladimir Sementsov-Ogievskiy        args = {
3299a28bd5SVladimir Sementsov-Ogievskiy            'driver': 'virtio-blk-pci',
3399a28bd5SVladimir Sementsov-Ogievskiy            'drive': 'disk',
3499a28bd5SVladimir Sementsov-Ogievskiy            'id': 'virtio-disk0',
3599a28bd5SVladimir Sementsov-Ogievskiy            'bus': 'pci.1',
3699a28bd5SVladimir Sementsov-Ogievskiy            'addr': 1
3799a28bd5SVladimir Sementsov-Ogievskiy        }
3899a28bd5SVladimir Sementsov-Ogievskiy
3999a28bd5SVladimir Sementsov-Ogievskiy        self.assert_no_vda()
4099a28bd5SVladimir Sementsov-Ogievskiy        self.vm.cmd('device_add', args)
4199a28bd5SVladimir Sementsov-Ogievskiy        try:
4299a28bd5SVladimir Sementsov-Ogievskiy            self.assert_vda()
4399a28bd5SVladimir Sementsov-Ogievskiy        except AssertionError:
4499a28bd5SVladimir Sementsov-Ogievskiy            time.sleep(1)
4599a28bd5SVladimir Sementsov-Ogievskiy            self.assert_vda()
4699a28bd5SVladimir Sementsov-Ogievskiy
4799a28bd5SVladimir Sementsov-Ogievskiy    def unplug(self) -> None:
4899a28bd5SVladimir Sementsov-Ogievskiy        self.vm.cmd('device_del', id='virtio-disk0')
4999a28bd5SVladimir Sementsov-Ogievskiy
5099a28bd5SVladimir Sementsov-Ogievskiy        self.vm.event_wait('DEVICE_DELETED', 1.0,
5199a28bd5SVladimir Sementsov-Ogievskiy                           match={'data': {'device': 'virtio-disk0'}})
5299a28bd5SVladimir Sementsov-Ogievskiy
5399a28bd5SVladimir Sementsov-Ogievskiy        self.assert_no_vda()
5499a28bd5SVladimir Sementsov-Ogievskiy
5599a28bd5SVladimir Sementsov-Ogievskiy    def test(self) -> None:
5699a28bd5SVladimir Sementsov-Ogievskiy        """
5799a28bd5SVladimir Sementsov-Ogievskiy        :avocado: tags=arch:x86_64
5899a28bd5SVladimir Sementsov-Ogievskiy        :avocado: tags=machine:q35
5999a28bd5SVladimir Sementsov-Ogievskiy        :avocado: tags=accel:kvm
6099a28bd5SVladimir Sementsov-Ogievskiy        """
6199a28bd5SVladimir Sementsov-Ogievskiy        self.require_accelerator('kvm')
6299a28bd5SVladimir Sementsov-Ogievskiy        self.vm.add_args('-accel', 'kvm')
6399a28bd5SVladimir Sementsov-Ogievskiy        self.vm.add_args('-device', 'pcie-pci-bridge,id=pci.1,bus=pcie.0')
6499a28bd5SVladimir Sementsov-Ogievskiy
6599a28bd5SVladimir Sementsov-Ogievskiy        self.launch_and_wait()
6699a28bd5SVladimir Sementsov-Ogievskiy        self.blockdev_add()
6799a28bd5SVladimir Sementsov-Ogievskiy
6899a28bd5SVladimir Sementsov-Ogievskiy        self.plug()
6999a28bd5SVladimir Sementsov-Ogievskiy        self.unplug()
70