From: Shivaprasad G Bhat <sbhat(a)linux.vnet.ibm.com>
This patch adds hostdev test cases in qemuhotplugtest.c.
Note: the small tweak inside virpcimock.c was needed because
the new tests added a code path in which virHostHasIOMMU()
(virutil.c) started being called, and the mocked '/sys/kernel/'
prefix that is mocked in virpcimock.c wasn't being considered
in the opendir() mock. An alternative to avoid these situations
in virpcimock.c is implemented in the next patch.
Reviewed-by: Michal Privoznik <mprivozn(a)redhat.com>
Signed-off-by: Shivaprasad G Bhat <sbhat(a)linux.vnet.ibm.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413(a)gmail.com>
---
src/util/virprocess.h | 2 +-
tests/Makefile.am | 7 +++
tests/qemuhotplugtest.c | 40 ++++++++++++-
.../qemuhotplug-hostdev-pci.xml | 6 ++
.../qemuhotplug-base-live+hostdev-pci.xml | 58 +++++++++++++++++++
...uhotplug-pseries-base-live+hostdev-pci.xml | 51 ++++++++++++++++
.../qemuhotplug-pseries-base-live.xml | 43 ++++++++++++++
tests/virpcimock.c | 3 +-
tests/virprocessmock.c | 28 +++++++++
9 files changed, 235 insertions(+), 3 deletions(-)
create mode 100644 tests/qemuhotplugtestdevices/qemuhotplug-hostdev-pci.xml
create mode 100644 tests/qemuhotplugtestdomains/qemuhotplug-base-live+hostdev-pci.xml
create mode 100644
tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live+hostdev-pci.xml
create mode 100644 tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live.xml
create mode 100644 tests/virprocessmock.c
diff --git a/src/util/virprocess.h b/src/util/virprocess.h
index 003ba1edf4..4806c592da 100644
--- a/src/util/virprocess.h
+++ b/src/util/virprocess.h
@@ -75,7 +75,7 @@ int virProcessGetNamespaces(pid_t pid,
int virProcessSetNamespaces(size_t nfdlist,
int *fdlist);
-int virProcessSetMaxMemLock(pid_t pid, unsigned long long bytes);
+int virProcessSetMaxMemLock(pid_t pid, unsigned long long bytes) ATTRIBUTE_NOINLINE;
int virProcessSetMaxProcesses(pid_t pid, unsigned int procs);
int virProcessSetMaxFiles(pid_t pid, unsigned int files);
int virProcessSetMaxCoreSize(pid_t pid, unsigned long long bytes);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f92710db43..8c343857a5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -216,6 +216,7 @@ test_libraries = libshunload.la \
libvirpcimock.la \
libvirnetdevmock.la \
libvirrandommock.la \
+ libvirprocessmock.la \
libvirhostcpumock.la \
libdomaincapsmock.la \
libvirfilecachemock.la \
@@ -1196,6 +1197,12 @@ libvirrandommock_la_SOURCES = \
libvirrandommock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
libvirrandommock_la_LIBADD = $(MOCKLIBS_LIBS)
+libvirprocessmock_la_SOURCES = \
+ virprocessmock.c
+libvirprocessmock_la_CFLAGS = $(AM_CFLAGS)
+libvirprocessmock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
+libvirprocessmock_la_LIBADD = $(MOCKLIBS_LIBS)
+
libvirhostcpumock_la_SOURCES = \
virhostcpumock.c
libvirhostcpumock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c
index b6aad330a9..3c177c6622 100644
--- a/tests/qemuhotplugtest.c
+++ b/tests/qemuhotplugtest.c
@@ -28,6 +28,7 @@
#include "testutils.h"
#include "testutilsqemu.h"
#include "testutilsqemuschema.h"
+#include "virhostdev.h"
#include "virerror.h"
#include "virstring.h"
#include "virthread.h"
@@ -79,6 +80,8 @@ qemuHotplugCreateObjects(virDomainXMLOptionPtr xmlopt,
virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE_IVSHMEM_PLAIN);
virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE_IVSHMEM_DOORBELL);
virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_SCSI_DISK_WWN);
+ virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI);
+ virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE_SPAPR_PCI_HOST_BRIDGE);
if (qemuTestCapsCacheInsert(driver.qemuCapsCache, priv->qemuCaps) < 0)
goto cleanup;
@@ -130,6 +133,9 @@ testQemuHotplugAttach(virDomainObjPtr vm,
case VIR_DOMAIN_DEVICE_WATCHDOG:
ret = qemuDomainAttachWatchdog(&driver, vm, dev->data.watchdog);
break;
+ case VIR_DOMAIN_DEVICE_HOSTDEV:
+ ret = qemuDomainAttachHostDevice(&driver, vm, dev->data.hostdev);
+ break;
default:
VIR_TEST_VERBOSE("device type '%s' cannot be attached",
virDomainDeviceTypeToString(dev->type));
@@ -151,6 +157,7 @@ testQemuHotplugDetach(virDomainObjPtr vm,
case VIR_DOMAIN_DEVICE_CHR:
case VIR_DOMAIN_DEVICE_SHMEM:
case VIR_DOMAIN_DEVICE_WATCHDOG:
+ case VIR_DOMAIN_DEVICE_HOSTDEV:
ret = qemuDomainDetachDeviceLive(vm, dev, &driver, async);
break;
default:
@@ -578,6 +585,7 @@ testQemuHotplugCpuIndividual(const void *opaque)
return ret;
}
+#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int
@@ -587,6 +595,19 @@ mymain(void)
int ret = 0;
struct qemuHotplugTestData data = {0};
struct testQemuHotplugCpuParams cpudata;
+ char *fakerootdir;
+
+ if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) {
+ fprintf(stderr, "Out of memory\n");
+ abort();
+ }
+
+ if (!mkdtemp(fakerootdir)) {
+ fprintf(stderr, "Cannot create fakerootdir");
+ abort();
+ }
+
+ setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1);
#if !WITH_YAJL
fputs("libvirt not compiled with JSON support, skipping this test\n",
stderr);
@@ -621,6 +642,8 @@ mymain(void)
if (!driver.lockManager)
return EXIT_FAILURE;
+ driver.hostdevMgr = virHostdevManagerGetDefault();
+
/* wait only 100ms for DEVICE_DELETED event */
qemuDomainRemoveDeviceWaitTime = 100;
@@ -789,6 +812,15 @@ mymain(void)
"human-monitor-command", HMP("OK\\r\\n"),
"device_add", QMP_OK);
+ DO_TEST_ATTACH("base-live", "hostdev-pci", false, true,
+ "device_add", QMP_OK);
+ DO_TEST_DETACH("base-live", "hostdev-pci", false, false,
+ "device_del", QMP_DEVICE_DELETED("hostdev0")
QMP_OK);
+ DO_TEST_ATTACH("pseries-base-live", "hostdev-pci", false, true,
+ "device_add", QMP_OK);
+ DO_TEST_DETACH("pseries-base-live", "hostdev-pci", false, false,
+ "device_del", QMP_DEVICE_DELETED("hostdev0")
QMP_OK);
+
DO_TEST_ATTACH("base-live", "watchdog", false, true,
"watchdog-set-action", QMP_OK,
"device_add", QMP_OK);
@@ -846,9 +878,15 @@ mymain(void)
DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "16-22", true,
true, true);
DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "17", true,
true, true);
+ if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
+ virFileDeleteTree(fakerootdir);
+ VIR_FREE(fakerootdir);
+
qemuTestDriverFree(&driver);
virObjectUnref(data.vm);
return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
-VIR_TEST_MAIN(mymain)
+VIR_TEST_MAIN_PRELOAD(mymain,
+ VIR_TEST_MOCK("virpci"),
+ VIR_TEST_MOCK("virprocess"));
diff --git a/tests/qemuhotplugtestdevices/qemuhotplug-hostdev-pci.xml
b/tests/qemuhotplugtestdevices/qemuhotplug-hostdev-pci.xml
new file mode 100644
index 0000000000..6f7c99c943
--- /dev/null
+++ b/tests/qemuhotplugtestdevices/qemuhotplug-hostdev-pci.xml
@@ -0,0 +1,6 @@
+<hostdev mode='subsystem' type='pci' managed='yes'>
+ <driver name='vfio'/>
+ <source>
+ <address domain='0x0000' bus='0x06' slot='0x12'
function='0x2'/>
+ </source>
+</hostdev>
diff --git a/tests/qemuhotplugtestdomains/qemuhotplug-base-live+hostdev-pci.xml
b/tests/qemuhotplugtestdomains/qemuhotplug-base-live+hostdev-pci.xml
new file mode 100644
index 0000000000..40dcc33595
--- /dev/null
+++ b/tests/qemuhotplugtestdomains/qemuhotplug-base-live+hostdev-pci.xml
@@ -0,0 +1,58 @@
+<domain type='kvm' id='7'>
+ <name>hotplug</name>
+ <uuid>d091ea82-29e6-2e34-3005-f02617b36e87</uuid>
+ <memory unit='KiB'>4194304</memory>
+ <currentMemory unit='KiB'>4194304</currentMemory>
+ <vcpu placement='static'>4</vcpu>
+ <os>
+ <type arch='x86_64' machine='pc'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <features>
+ <acpi/>
+ <apic/>
+ <pae/>
+ </features>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>restart</on_crash>
+ <devices>
+ <emulator>/usr/bin/qemu-system-x86_64</emulator>
+ <controller type='usb' index='0'>
+ <alias name='usb'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x2'/>
+ </controller>
+ <controller type='ide' index='0'>
+ <alias name='ide'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x1'/>
+ </controller>
+ <controller type='scsi' index='0' model='virtio-scsi'>
+ <alias name='scsi0'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x03' function='0x0'/>
+ </controller>
+ <controller type='pci' index='0' model='pci-root'>
+ <alias name='pci'/>
+ </controller>
+ <controller type='virtio-serial' index='0'>
+ <alias name='virtio-serial0'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x04' function='0x0'/>
+ </controller>
+ <input type='mouse' bus='ps2'>
+ <alias name='input0'/>
+ </input>
+ <input type='keyboard' bus='ps2'>
+ <alias name='input1'/>
+ </input>
+ <hostdev mode='subsystem' type='pci' managed='yes'>
+ <driver name='vfio'/>
+ <source>
+ <address domain='0x0000' bus='0x06' slot='0x12'
function='0x2'/>
+ </source>
+ <alias name='hostdev0'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x05' function='0x0'/>
+ </hostdev>
+ <memballoon model='none'/>
+ </devices>
+ <seclabel type='none' model='none'/>
+</domain>
diff --git a/tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live+hostdev-pci.xml
b/tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live+hostdev-pci.xml
new file mode 100644
index 0000000000..483cb5d8f0
--- /dev/null
+++ b/tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live+hostdev-pci.xml
@@ -0,0 +1,51 @@
+<domain type='kvm' id='7'>
+ <name>hotplug</name>
+ <uuid>d091ea82-29e6-2e34-3005-f02617b36e87</uuid>
+ <memory unit='KiB'>4194304</memory>
+ <currentMemory unit='KiB'>4194304</currentMemory>
+ <vcpu placement='static'>4</vcpu>
+ <os>
+ <type arch='ppc64' machine='pseries'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <emulator>/usr/bin/qemu-system-ppc64</emulator>
+ <controller type='pci' index='0' model='pci-root'>
+ <model name='spapr-pci-host-bridge'/>
+ <target index='0'/>
+ <alias name='pci.0'/>
+ </controller>
+ <controller type='pci' index='1' model='pci-root'>
+ <model name='spapr-pci-host-bridge'/>
+ <target index='1'/>
+ <alias name='pci.1'/>
+ </controller>
+ <controller type='usb' index='0'>
+ <alias name='usb'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x0'/>
+ </controller>
+ <input type='keyboard' bus='usb'>
+ <alias name='input0'/>
+ <address type='usb' bus='0' port='1'/>
+ </input>
+ <input type='mouse' bus='usb'>
+ <alias name='input1'/>
+ <address type='usb' bus='0' port='2'/>
+ </input>
+ <hostdev mode='subsystem' type='pci' managed='yes'>
+ <driver name='vfio'/>
+ <source>
+ <address domain='0x0000' bus='0x06' slot='0x12'
function='0x2'/>
+ </source>
+ <alias name='hostdev0'/>
+ <address type='pci' domain='0x0000' bus='0x01'
slot='0x01' function='0x0'/>
+ </hostdev>
+ <memballoon model='none'/>
+ <panic model='pseries'/>
+ </devices>
+ <seclabel type='none' model='none'/>
+</domain>
diff --git a/tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live.xml
b/tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live.xml
new file mode 100644
index 0000000000..1dcc35f626
--- /dev/null
+++ b/tests/qemuhotplugtestdomains/qemuhotplug-pseries-base-live.xml
@@ -0,0 +1,43 @@
+<domain type='kvm' id='7'>
+ <name>hotplug</name>
+ <uuid>d091ea82-29e6-2e34-3005-f02617b36e87</uuid>
+ <memory unit='KiB'>4194304</memory>
+ <currentMemory unit='KiB'>4194304</currentMemory>
+ <vcpu placement='static'>4</vcpu>
+ <os>
+ <type arch='ppc64' machine='pseries'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <emulator>/usr/bin/qemu-system-ppc64</emulator>
+ <controller type='pci' index='0' model='pci-root'>
+ <model name='spapr-pci-host-bridge'/>
+ <target index='0'/>
+ <alias name='pci.0'/>
+ </controller>
+ <controller type='pci' index='1' model='pci-root'>
+ <model name='spapr-pci-host-bridge'/>
+ <target index='1'/>
+ <alias name='pci.1'/>
+ </controller>
+ <controller type='usb' index='0'>
+ <alias name='usb'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x0'/>
+ </controller>
+ <input type='keyboard' bus='usb'>
+ <alias name='input0'/>
+ <address type='usb' bus='0' port='1'/>
+ </input>
+ <input type='mouse' bus='usb'>
+ <alias name='input1'/>
+ <address type='usb' bus='0' port='2'/>
+ </input>
+ <memballoon model='none'/>
+ <panic model='pseries'/>
+ </devices>
+ <seclabel type='none' model='none'/>
+</domain>
diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index b1651617a6..e362121899 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -1150,7 +1150,8 @@ opendir(const char *path)
init_syms();
- if (STRPREFIX(path, SYSFS_PCI_PREFIX) &&
+ if ((STRPREFIX(path, SYSFS_PCI_PREFIX) ||
+ STRPREFIX(path, "/sys/kernel/")) &&
getrealpath(&newpath, path) < 0)
return NULL;
diff --git a/tests/virprocessmock.c b/tests/virprocessmock.c
new file mode 100644
index 0000000000..79af1e8064
--- /dev/null
+++ b/tests/virprocessmock.c
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2019 Red Hat, Inc.
+ * Copyright (C) 2019 IBM Corp.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <
http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <config.h>
+#include "virprocess.h"
+
+int
+virProcessSetMaxMemLock(pid_t pid ATTRIBUTE_UNUSED, unsigned long long bytes
ATTRIBUTE_UNUSED)
+{
+ return 0;
+}
--
2.21.0