[libvirt] [PATCH] virpcitst: Introduce testVirPCIDeviceReset

Michal Privoznik mprivozn at redhat.com
Tue Nov 5 14:56:34 UTC 2013


This addition, however, requires some refactoring to be done.  First of
all, to match the best practice we should detach the device prior
resetting it. That's why testVirPCIDeviceDetach is detaching all devices
within 0000:00:01.0 and 0000:00:03.0 range. Then, the brand new test
will reset the 0000:00:02.0 device, so the last testVirPCIDeviceReattach
can reattach all the devices back.

In order to perform a PCI device reset, the dummy config file is not
sufficient anymore and must be replaced with real PCI config (binary
mess). Such config files are to be stored under tests/virpcitestdata/
and ought to have '.config' suffix.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 cfg.mk                                   |   2 +-
 tests/virpcimock.c                       |  19 ++++-
 tests/virpcitest.c                       | 116 +++++++++++++++++++------------
 tests/virpcitestdata/0000:00:01.0.config | Bin 0 -> 4096 bytes
 tests/virpcitestdata/0000:00:02.0.config | Bin 0 -> 256 bytes
 tests/virpcitestdata/0000:00:03.0.config | Bin 0 -> 4096 bytes
 6 files changed, 91 insertions(+), 46 deletions(-)
 create mode 100644 tests/virpcitestdata/0000:00:01.0.config
 create mode 100644 tests/virpcitestdata/0000:00:02.0.config
 create mode 100644 tests/virpcitestdata/0000:00:03.0.config

diff --git a/cfg.mk b/cfg.mk
index ed47a9b..befd231 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -971,7 +971,7 @@ exclude_file_name_regexp--sc_prohibit_close = \
   (\.p[yl]$$|^docs/|^(src/util/virfile\.c|src/libvirt\.c|tests/vir(cgroup|pci)mock\.c)$$)
 
 exclude_file_name_regexp--sc_prohibit_empty_lines_at_EOF = \
-  (^tests/(qemuhelp|nodeinfo)data/|\.(gif|ico|png|diff)$$)
+  (^tests/(qemuhelp|nodeinfo|virpcitest)data/|\.(gif|ico|png|diff)$$)
 
 _src2=src/(util/vircommand|libvirt|lxc/lxc_controller|locking/lock_daemon)
 exclude_file_name_regexp--sc_prohibit_fork_wrappers = \
diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index 2944780..b0b9457 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -302,9 +302,12 @@ pci_device_new_from_stub(const struct pciDevice *data)
 {
     struct pciDevice *dev;
     char *devpath;
+    char *configSrc, *configDst;
     char tmp[32];
+    struct stat sb;
 
     if (VIR_ALLOC_QUIET(dev) < 0 ||
+        virAsprintfQuiet(&configSrc, "%s/virpcitestdata/%s.config", abs_builddir, data->id) < 0 ||
         virAsprintfQuiet(&devpath, "%s/devices/%s", fakesysfsdir, data->id) < 0)
         ABORT_OOM();
 
@@ -313,7 +316,21 @@ pci_device_new_from_stub(const struct pciDevice *data)
     if (virFileMakePath(devpath) < 0)
         ABORT("Unable to create: %s", devpath);
 
-    make_file(devpath, "config", "some dummy config");
+    /* If there is a config file for the device within virpcitestdata dir,
+     * symlink it. Otherwise create a dummy config file. */
+    if ((realstat && realstat(configSrc, &sb) == 0) ||
+        (real__xstat && real__xstat(_STAT_VER, configSrc, &sb) == 0)) {
+        /* On success make symlink to @configSrc */
+        if (virAsprintfQuiet(&configDst, "%s/config", devpath) < 0)
+            ABORT_OOM();
+
+        if (symlink(configSrc, configDst) < 0)
+            ABORT("Unable to create symlink: %s", configDst);
+    } else {
+        /* If there's no config data in the virpcitestdata dir, create a dummy
+         * config file */
+        make_file(devpath, "config", "some dummy config");
+    }
 
     if (snprintf(tmp, sizeof(tmp),  "0x%.4x", dev->vendor) < 0)
         ABORT("@tmp overflow");
diff --git a/tests/virpcitest.c b/tests/virpcitest.c
index d301a94..5fe6d49 100644
--- a/tests/virpcitest.c
+++ b/tests/virpcitest.c
@@ -61,7 +61,7 @@ cleanup:
     if ((count = virPCIDeviceListCount(list)) != cnt) {                 \
         virReportError(VIR_ERR_INTERNAL_ERROR,                          \
                        "Unexpected count of items in " #list ": %d, "   \
-                       "expecting " #cnt, count);                       \
+                       "expecting %zu", count, (size_t) cnt);           \
         goto cleanup;                                                   \
     }
 
@@ -69,88 +69,112 @@ static int
 testVirPCIDeviceDetach(const void *oaque ATTRIBUTE_UNUSED)
 {
     int ret = -1;
-    virPCIDevicePtr dev = NULL, unbindedDev = NULL;
+    virPCIDevicePtr dev[] = {NULL, NULL, NULL};
+    size_t i, nDev = ARRAY_CARDINALITY(dev);
     virPCIDeviceListPtr activeDevs = NULL, inactiveDevs = NULL;
     int count;
 
-    if (!(dev = virPCIDeviceNew(0, 0, 1, 0)) ||
-        !(unbindedDev = virPCIDeviceNew(0, 0, 3, 0)) ||
-        !(activeDevs = virPCIDeviceListNew()) ||
+    if (!(activeDevs = virPCIDeviceListNew()) ||
         !(inactiveDevs = virPCIDeviceListNew()))
         goto cleanup;
 
     CHECK_LIST_COUNT(activeDevs, 0);
     CHECK_LIST_COUNT(inactiveDevs, 0);
 
-    if (virPCIDeviceSetStubDriver(dev, "pci-stub") < 0 ||
-        virPCIDeviceSetStubDriver(unbindedDev, "pci-stub") < 0)
-        goto cleanup;
+    for (i = 0; i < nDev; i++) {
+        if (!(dev[i] = virPCIDeviceNew(0, 0, i + 1, 0)) ||
+            virPCIDeviceSetStubDriver(dev[i], "pci-stub") < 0)
+            goto cleanup;
 
-    if (virPCIDeviceDetach(dev, activeDevs, inactiveDevs) < 0)
-        goto cleanup;
+        if (virPCIDeviceDetach(dev[i], activeDevs, inactiveDevs) < 0)
+            goto cleanup;
 
-    CHECK_LIST_COUNT(activeDevs, 0);
-    CHECK_LIST_COUNT(inactiveDevs, 1);
+        CHECK_LIST_COUNT(activeDevs, 0);
+        CHECK_LIST_COUNT(inactiveDevs, i + 1);
+    }
+
+    ret = 0;
+cleanup:
+    for (i = 0; i < nDev; i++)
+        virPCIDeviceFree(dev[i]);
+    virObjectUnref(activeDevs);
+    virObjectUnref(inactiveDevs);
+    return ret;
+}
 
-    if (virPCIDeviceDetach(unbindedDev, activeDevs, inactiveDevs) < 0)
+static int
+testVirPCIDeviceReset(const void *opaque ATTRIBUTE_UNUSED)
+{
+    int ret = -1;
+    virPCIDevicePtr dev[] = {NULL, NULL, NULL};
+    size_t i, nDev = ARRAY_CARDINALITY(dev);
+    virPCIDeviceListPtr activeDevs = NULL, inactiveDevs = NULL;
+    int count;
+
+    if (!(activeDevs = virPCIDeviceListNew()) ||
+        !(inactiveDevs = virPCIDeviceListNew()))
         goto cleanup;
 
     CHECK_LIST_COUNT(activeDevs, 0);
-    CHECK_LIST_COUNT(inactiveDevs, 2);
+    CHECK_LIST_COUNT(inactiveDevs, 0);
+
+    for (i = 0; i < nDev; i++) {
+        if (!(dev[i] = virPCIDeviceNew(0, 0, i + 1, 0)) ||
+            virPCIDeviceSetStubDriver(dev[i], "pci-stub") < 0)
+            goto cleanup;
+
+        if (virPCIDeviceReset(dev[i], activeDevs, inactiveDevs) < 0)
+            goto cleanup;
+    }
 
     ret = 0;
 cleanup:
-    virPCIDeviceFree(dev);
-    virPCIDeviceFree(unbindedDev);
+    for (i = 0; i < nDev; i++)
+        virPCIDeviceFree(dev[i]);
     virObjectUnref(activeDevs);
     virObjectUnref(inactiveDevs);
     return ret;
 }
 
-# define FAKESYSFSDIRTEMPLATE abs_builddir "/fakesysfsdir-XXXXXX"
-
 static int
-testVirPCIDeviceReattach(const void *oaque ATTRIBUTE_UNUSED)
+testVirPCIDeviceReattach(const void *opaque ATTRIBUTE_UNUSED)
 {
     int ret = -1;
-    virPCIDevicePtr dev = NULL, unbindedDev = NULL;
+    virPCIDevicePtr dev[] = {NULL, NULL, NULL};
+    size_t i, nDev = ARRAY_CARDINALITY(dev);
     virPCIDeviceListPtr activeDevs = NULL, inactiveDevs = NULL;
     int count;
 
-    if (!(dev = virPCIDeviceNew(0, 0, 1, 0)) ||
-        !(unbindedDev = virPCIDeviceNew(0, 0, 3, 0)) ||
-        !(activeDevs = virPCIDeviceListNew()) ||
+    if (!(activeDevs = virPCIDeviceListNew()) ||
         !(inactiveDevs = virPCIDeviceListNew()))
         goto cleanup;
 
-    if (virPCIDeviceListAdd(inactiveDevs, dev) < 0) {
-        virPCIDeviceFree(dev);
-        virPCIDeviceFree(unbindedDev);
-        goto cleanup;
-    }
-
-    if (virPCIDeviceListAdd(inactiveDevs, unbindedDev) < 0) {
-        virPCIDeviceFree(unbindedDev);
-        goto cleanup;
-    }
+    for (i = 0; i < nDev; i++) {
+        if (!(dev[i] = virPCIDeviceNew(0, 0, i + 1, 0)))
+            goto cleanup;
 
-    CHECK_LIST_COUNT(activeDevs, 0);
-    CHECK_LIST_COUNT(inactiveDevs, 2);
+        if (virPCIDeviceListAdd(inactiveDevs, dev[i]) < 0) {
+            virPCIDeviceFree(dev[i]);
+            goto cleanup;
+        }
 
-    if (virPCIDeviceSetStubDriver(dev, "pci-stub") < 0)
-        goto cleanup;
+        CHECK_LIST_COUNT(activeDevs, 0);
+        CHECK_LIST_COUNT(inactiveDevs, i + 1);
 
-    if (virPCIDeviceReattach(dev, activeDevs, inactiveDevs) < 0)
-        goto cleanup;
+        if (virPCIDeviceSetStubDriver(dev[i], "pci-stub") < 0)
+            goto cleanup;
+    }
 
     CHECK_LIST_COUNT(activeDevs, 0);
-    CHECK_LIST_COUNT(inactiveDevs, 1);
+    CHECK_LIST_COUNT(inactiveDevs, nDev);
 
-    if (virPCIDeviceReattach(unbindedDev, activeDevs, inactiveDevs) < 0)
-        goto cleanup;
+    for (i = 0; i < nDev; i++) {
+        if (virPCIDeviceReattach(dev[i], activeDevs, inactiveDevs) < 0)
+            goto cleanup;
 
-    CHECK_LIST_COUNT(activeDevs, 0);
-    CHECK_LIST_COUNT(inactiveDevs, 0);
+        CHECK_LIST_COUNT(activeDevs, 0);
+        CHECK_LIST_COUNT(inactiveDevs, nDev - i - 1);
+    }
 
     ret = 0;
 cleanup:
@@ -158,6 +182,9 @@ cleanup:
     virObjectUnref(inactiveDevs);
     return ret;
 }
+
+# define FAKESYSFSDIRTEMPLATE abs_builddir "/fakesysfsdir-XXXXXX"
+
 static int
 mymain(void)
 {
@@ -184,6 +211,7 @@ mymain(void)
 
     DO_TEST(testVirPCIDeviceNew);
     DO_TEST(testVirPCIDeviceDetach);
+    DO_TEST(testVirPCIDeviceReset);
     DO_TEST(testVirPCIDeviceReattach);
 
     if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
diff --git a/tests/virpcitestdata/0000:00:01.0.config b/tests/virpcitestdata/0000:00:01.0.config
new file mode 100644
index 0000000000000000000000000000000000000000..2de3f8effb1e87a572d644ce6da0c60ed5527387
GIT binary patch
literal 4096
zcmZo`uyA5y6<{!BXkZdxU|?WjaQFnIu!6P*K|zqj38*YLBUWVtBFK0_8RRTp)(4FY
zJOU5?fgEwsk%2*gfsx at rAA^7ZhX8}%95x0gMnRA_;ee5WQ3S>jRA7TJ6<{KSX=b4L
zUjp*~|G%z6Sn;Te(GVC7fzc2c4S~@R7!85Z5Eu=C(GVC7fzc2c4S~@R7@!aU07AtR
An*aa+

literal 0
HcmV?d00001

diff --git a/tests/virpcitestdata/0000:00:02.0.config b/tests/virpcitestdata/0000:00:02.0.config
new file mode 100644
index 0000000000000000000000000000000000000000..f60b222017d5b4a3d96e776d2c1a02c41849660e
GIT binary patch
literal 256
zcmZo`aARO+nZUrrz`)D^1S|{;pFlJZ1H%On%_spPAz+nwlp=^V0m|oQWMJTA;9*ot
zRAlG?it&q!i!(3;FmpoXfz&<*1}31<!~@m~j0`*<9{gMA2+<=bD99kpzy@@iNP;Sh
ll>yu&9HufZQGzRBV1y_FI+KB+0U-=xt>%|tka$qO8vvZn5}5!1

literal 0
HcmV?d00001

diff --git a/tests/virpcitestdata/0000:00:03.0.config b/tests/virpcitestdata/0000:00:03.0.config
new file mode 100644
index 0000000000000000000000000000000000000000..8c1d6e7aaa0ffb0e1d2d0f6e80480fbffccee4e6
GIT binary patch
literal 4096
zcmZn=RC>Y2D8RtTz{(-O(7?dJ5by~|U<IqhjTJ!>0Z>_PMhGJzu?fmzU|_9ifXbp$
zf)x-xOd(?f^FIa$1{;vg2mViB70}>h5X at m`bYc_~0I58{jH(~R1KH1lB)kB`HUMIv
z<AEfX5r~fpCT=o%0JhQrDsI5=4{W{!0|OICh>3x*0V)Nj{xg6B0?r?0jE2By2#kin
xXb6mkz;FuzMquR$1BME03=9fD9uO}8Qlnrr1V%$(Gz3ONU^E0qLtuD at 005R_7jpmr

literal 0
HcmV?d00001

-- 
1.8.1.5




More information about the libvir-list mailing list