[libvirt PATCH v3 10/18] tests: add ability to test various nbdkit capabilities

Jonathon Jongsma jjongsma at redhat.com
Thu Oct 20 21:59:01 UTC 2022


Add new DO_TEST_CAPS_LATEST_NBDKIT macro to test xml2argv for various
nbdkit capability scenarios.

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
 src/qemu/qemu_nbdkit.c   | 20 +++++++++++++++++---
 tests/qemuxml2argvtest.c | 11 +++++++++++
 tests/testutilsqemu.c    | 27 +++++++++++++++++++++++++++
 tests/testutilsqemu.h    |  5 +++++
 4 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c
index c5b0762f8d..59c452a15e 100644
--- a/src/qemu/qemu_nbdkit.c
+++ b/src/qemu/qemu_nbdkit.c
@@ -263,10 +263,16 @@ virNbkditCapsCheckModdir(const char *moddir,
 
 static bool
 virNbdkitCapsIsValid(void *data,
-                     void *privData G_GNUC_UNUSED)
+                     void *privData)
 {
     qemuNbdkitCaps *nbdkitCaps = data;
     struct stat st;
+    /* when run under test, we will use privData as a signal to indicate that
+     * we shouldn't touch the filesystem */
+    bool skipValidation = (privData != NULL);
+
+    if (skipValidation)
+        return true;
 
     if (!nbdkitCaps->path)
         return true;
@@ -309,9 +315,17 @@ virNbdkitCapsIsValid(void *data,
 
 static void*
 virNbdkitCapsNewData(const char *binary,
-                     void *privData G_GNUC_UNUSED)
+                     void *privData)
 {
-    qemuNbdkitCaps *caps = qemuNbdkitCapsNew(binary);
+    /* when run under test, we will use privData as a signal to indicate that
+     * we shouldn't touch the filesystem */
+    bool skipNewData = (privData != NULL);
+    qemuNbdkitCaps *caps = NULL;
+
+    if (skipNewData)
+        return NULL;
+
+    caps = qemuNbdkitCapsNew(binary);
     qemuNbdkitCapsQuery(caps);
 
     return caps;
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index ef32cae2e9..0032bafdce 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -670,6 +670,14 @@ testCompareXMLToArgv(const void *data)
     if (rc < 0)
         goto cleanup;
 
+    if (info->nbdkitCaps) {
+        if (virFileCacheInsertData(driver.nbdkitCapsCache, TEST_NBDKIT_PATH,
+                                   g_object_ref(info->nbdkitCaps)) < 0) {
+            g_object_unref(info->nbdkitCaps);
+            goto cleanup;
+        }
+    }
+
     if (info->migrateFrom &&
         !(migrateURI = qemuMigrationDstGetURI(info->migrateFrom,
                                               info->migrateFd)))
@@ -927,6 +935,9 @@ mymain(void)
 # define DO_TEST_CAPS_ARCH_VER(name, arch, ver) \
     DO_TEST_CAPS_ARCH_VER_FULL(name, arch, ver, ARG_END)
 
+# define DO_TEST_CAPS_LATEST_NBDKIT(name, ...) \
+    DO_TEST_CAPS_ARCH_LATEST_FULL(name, "x86_64", ARG_NBDKIT_CAPS, __VA_ARGS__, QEMU_NBDKIT_CAPS_LAST, ARG_END)
+
 # define DO_TEST_CAPS_LATEST(name) \
     DO_TEST_CAPS_ARCH_LATEST(name, "x86_64")
 
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index 6d3decdc16..386042aa79 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -131,6 +131,10 @@ virFindFileInPath(const char *file)
         return g_strdup_printf("/usr/bin/%s", file);
     }
 
+    if (g_str_equal(file, "nbdkit")) {
+        return g_strdup(TEST_NBDKIT_PATH);
+    }
+
     /* Nothing in tests should be relying on real files
      * in host OS, so we return NULL to try to force
      * an error in such a case
@@ -422,6 +426,7 @@ void qemuTestDriverFree(virQEMUDriver *driver)
     virObjectUnref(driver->caps);
     virObjectUnref(driver->config);
     virObjectUnref(driver->securityManager);
+    g_clear_object(&driver->nbdkitCapsCache);
 
     virCPUDefFree(cpuDefault);
     virCPUDefFree(cpuHaswell);
@@ -665,6 +670,12 @@ int qemuTestDriverInit(virQEMUDriver *driver)
     if (!driver->qemuCapsCache)
         goto error;
 
+    driver->nbdkitCapsCache = qemuNbdkitCapsCacheNew("/dev/null");
+    /* the nbdkitCapsCache just interprets the presence of a non-null private
+     * data pointer as a signal to skip cache validation. This prevents the
+     * cache from trying to validate the plugindir mtime, etc during test */
+    virFileCacheSetPriv(driver->nbdkitCapsCache, GUINT_TO_POINTER(1));
+
     driver->xmlopt = virQEMUDriverCreateXMLConf(driver, "none");
     if (!driver->xmlopt)
         goto error;
@@ -885,6 +896,7 @@ testQemuInfoSetArgs(struct testQemuInfo *info,
 
     info->conf = conf;
     info->args.newargs = true;
+    info->args.fakeNbdkitCaps = qemuNbdkitCapsNew(TEST_NBDKIT_PATH);
 
     va_start(argptr, conf);
     while ((argname = va_arg(argptr, testQemuInfoArgName)) != ARG_END) {
@@ -896,6 +908,13 @@ testQemuInfoSetArgs(struct testQemuInfo *info,
                 virQEMUCapsSet(info->args.fakeCaps, flag);
             break;
 
+        case ARG_NBDKIT_CAPS:
+            info->args.fakeNbdkitCapsUsed = true;
+
+            while ((flag = va_arg(argptr, int)) < QEMU_NBDKIT_CAPS_LAST)
+                qemuNbdkitCapsSet(info->args.fakeNbdkitCaps, flag);
+            break;
+
         case ARG_GIC:
             info->args.gic = va_arg(argptr, int);
             break;
@@ -1020,6 +1039,12 @@ testQemuInfoInitArgs(struct testQemuInfo *info)
         info->qemuCaps = g_steal_pointer(&info->args.fakeCaps);
     }
 
+    if (info->args.fakeNbdkitCapsUsed)
+        info->nbdkitCaps = g_steal_pointer(&info->args.fakeNbdkitCaps);
+    else
+        /* empty caps */
+        info->nbdkitCaps = qemuNbdkitCapsNew(TEST_NBDKIT_PATH);
+
     if (info->args.gic != GIC_NONE &&
         testQemuCapsSetGIC(info->qemuCaps, info->args.gic) < 0)
         return -1;
@@ -1037,6 +1062,8 @@ testQemuInfoClear(struct testQemuInfo *info)
     VIR_FREE(info->errfile);
     virObjectUnref(info->qemuCaps);
     g_clear_pointer(&info->args.fakeCaps, virObjectUnref);
+    g_clear_object(&info->nbdkitCaps);
+    g_clear_object(&info->args.fakeNbdkitCaps);
 }
 
 
diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h
index 943958d02a..618837559c 100644
--- a/tests/testutilsqemu.h
+++ b/tests/testutilsqemu.h
@@ -28,6 +28,7 @@
 # define TEST_TPM_ENV_VAR "VIR_TEST_MOCK_FAKE_TPM_VERSION"
 # define TPM_VER_1_2 "1.2"
 # define TPM_VER_2_0 "2.0"
+# define TEST_NBDKIT_PATH "/usr/bin/nbdkit"
 
 enum {
     GIC_NONE = 0,
@@ -52,6 +53,7 @@ typedef enum {
     ARG_CAPS_VER,
     ARG_CAPS_HOST_CPU_MODEL,
     ARG_HOST_OS,
+    ARG_NBDKIT_CAPS,
     ARG_END,
 } testQemuInfoArgName;
 
@@ -82,6 +84,8 @@ struct testQemuArgs {
     bool newargs;
     virQEMUCaps *fakeCaps;
     bool fakeCapsUsed;
+    qemuNbdkitCaps *fakeNbdkitCaps;
+    bool fakeNbdkitCapsUsed;
     char *capsver;
     char *capsarch;
     qemuTestCPUDef capsHostCPUModel;
@@ -96,6 +100,7 @@ struct testQemuInfo {
     char *outfile;
     char *errfile;
     virQEMUCaps *qemuCaps;
+    qemuNbdkitCaps *nbdkitCaps;
     const char *migrateFrom;
     int migrateFd;
     unsigned int flags;
-- 
2.37.3



More information about the libvir-list mailing list