]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemuxml2argvtest: Add support for populating 'fds' in private data
authorPeter Krempa <pkrempa@redhat.com>
Tue, 11 Oct 2022 13:52:54 +0000 (15:52 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 9 Jan 2023 13:59:42 +0000 (14:59 +0100)
Introduce a new argument type for testQemuInfoSetArgs named ARG_FD_GROUP
which allows users to instantiate tests with populated FD passing hash
table.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/conf/storage_source_conf.c
src/conf/storage_source_conf.h
tests/qemuxml2argvtest.c
tests/testutilsqemu.c
tests/testutilsqemu.h

index 8e2a85968dc86c9de6838005a886d06aa6336507..ad9ff36ff135efdabce7c35d04d0982ff9b5a8ba 100644 (file)
@@ -1384,6 +1384,7 @@ virStorageSourceFDTupleFinalize(GObject *object)
         VIR_FORCE_CLOSE(fdt->fds[i]);
 
     g_free(fdt->fds);
+    g_free(fdt->testfds);
     G_OBJECT_CLASS(vir_storage_source_fd_tuple_parent_class)->finalize(object);
 }
 
index 9cd1a0c137a31349bc793c1a32d4f83ec1ec15ad..7c99ac89764418c1412705d7a28bcbe66814dbe4 100644 (file)
@@ -262,6 +262,7 @@ struct _virStorageSourceFDTuple {
     GObject parent;
     int *fds;
     size_t nfds;
+    int *testfds; /* populated by tests to ensure stable FDs */
 
     bool writable;
     bool tryRestoreLabel;
index 44ad0f70494a18926fd73d50bd0204d894907e0e..e339c20091f9ac42c4d461446d8af4d95a0a5a64 100644 (file)
@@ -705,6 +705,11 @@ testCompareXMLToArgv(const void *data)
     }
     priv = vm->privateData;
 
+    if (info->args.fds) {
+        g_clear_pointer(&priv->fds, g_hash_table_unref);
+        priv->fds = g_steal_pointer(&info->args.fds);
+    }
+
     if (virBitmapParse("0-3", &priv->autoNodeset, 4) < 0)
         goto cleanup;
 
index 6d3decdc16b4c09039682c1b89ef7c38d58fc50d..396803c40b184ab045d2a61da4d1603670e2da5b 100644 (file)
@@ -932,6 +932,38 @@ testQemuInfoSetArgs(struct testQemuInfo *info,
             info->args.hostOS = va_arg(argptr, int);
             break;
 
+        case ARG_FD_GROUP: {
+            virStorageSourceFDTuple *new = virStorageSourceFDTupleNew();
+            const char *fdname = va_arg(argptr, char *);
+            VIR_AUTOCLOSE fakefd = open("/dev/zero", O_RDWR);
+            size_t i;
+
+            new->nfds = va_arg(argptr, unsigned int);
+            new->fds = g_new0(int, new->nfds);
+            new->testfds = g_new0(int, new->nfds);
+
+            for (i = 0; i < new->nfds; i++) {
+                new->testfds[i] = va_arg(argptr, unsigned int);
+
+                if (fcntl(new->testfds[i], F_GETFD) != -1) {
+                    fprintf(stderr, "fd '%d' is already in use\n", new->fds[i]);
+                    abort();
+                }
+
+                if ((new->fds[i] = dup(fakefd)) < 0) {
+                    fprintf(stderr, "failed to duplicate fake fd: %s",
+                            g_strerror(errno));
+                    abort();
+                }
+            }
+
+            if (!info->args.fds)
+                info->args.fds = virHashNew(g_object_unref);
+
+            g_hash_table_insert(info->args.fds, g_strdup(fdname), new);
+            break;
+        }
+
         case ARG_END:
         default:
             info->args.invalidarg = true;
@@ -1037,6 +1069,7 @@ testQemuInfoClear(struct testQemuInfo *info)
     VIR_FREE(info->errfile);
     virObjectUnref(info->qemuCaps);
     g_clear_pointer(&info->args.fakeCaps, virObjectUnref);
+    g_clear_pointer(&info->args.fds, g_hash_table_unref);
 }
 
 
index 943958d02a98533035c4ad6f1434a2622202bd76..51c072cb13795f20e18b622fcbc0b3342155d0d6 100644 (file)
@@ -52,6 +52,7 @@ typedef enum {
     ARG_CAPS_VER,
     ARG_CAPS_HOST_CPU_MODEL,
     ARG_HOST_OS,
+    ARG_FD_GROUP, /* name, nfds, fd[0], ... fd[n-1] */
     ARG_END,
 } testQemuInfoArgName;
 
@@ -87,6 +88,7 @@ struct testQemuArgs {
     qemuTestCPUDef capsHostCPUModel;
     int gic;
     testQemuHostOS hostOS;
+    GHashTable *fds;
     bool invalidarg;
 };