qemuxml2argvtest: Add support for populating 'fds' in private data

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>
This commit is contained in:
Peter Krempa 2022-10-11 15:52:54 +02:00
parent f762f87534
commit 0fcdb512d4
5 changed files with 42 additions and 0 deletions

View 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);
}

View 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;

View 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;

View 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);
}

View 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;
};