From: Peter Krempa <[email protected]>

'virTestDummyFDContext' was a copy of GHashTable to be able to register
a cleanup function. Upcoming patches will want to track more data
together with the hash table so turn virTestDummyFDContext into a proper
struct which contains the hash table.

Signed-off-by: Peter Krempa <[email protected]>
---
 tests/qemuxmlconftest.c |  2 +-
 tests/testutils.c       | 23 ++++++++++++++++-------
 tests/testutils.h       |  5 ++++-
 tests/testutilsqemu.h   |  4 +++-
 4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/tests/qemuxmlconftest.c b/tests/qemuxmlconftest.c
index 8e0f4bcb4f..3d30bf6ccc 100644
--- a/tests/qemuxmlconftest.c
+++ b/tests/qemuxmlconftest.c
@@ -919,7 +919,7 @@ testCompareXMLToArgv(const void *data)
     if (testCompareXMLToArgvValidateSchema(cmd, info) < 0)
         goto cleanup;

-    testCompareXMLToArgvStabilizeArgs(cmd, info->fdsubsts);
+    testCompareXMLToArgvStabilizeArgs(cmd, info->fdsubsts->hints);

     if (virCommandToStringBuf(cmd, &actualBuf, true, false) < 0)
         goto cleanup;
diff --git a/tests/testutils.c b/tests/testutils.c
index 5d6c918fd7..a66a07da36 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -298,7 +298,12 @@ static virTestDummyFDContext *dummyFDContext;
 void
 virTestDummyFDContextFree(virTestDummyFDContext *ctxt G_GNUC_UNUSED)
 {
-    g_clear_pointer(&dummyFDContext, g_hash_table_unref);
+    if (!dummyFDContext)
+        return;
+
+    g_clear_pointer(&dummyFDContext->hints, g_hash_table_unref);
+
+    g_clear_pointer(&dummyFDContext, g_free);
 }


@@ -307,9 +312,7 @@ virTestDummyFDContextFree(virTestDummyFDContext *ctxt 
G_GNUC_UNUSED)
  *
  * Create a new context for marking dummy FDs so that they can be later
  * cross-referenced and stripped from test output. Marked FDs are recorded
- * in the returned GHashTable (virTestDummyFDContext type is a direct alias
- * of GHashTable, allowing for registering custom autoptr cleanup function
- * so that the context can be properly disposed), where keys are stringified
+ * in the returned context's 'hints' field, where keys are stringified
  * FD numbers and the passed 'hint' strings are recorded as values.
  *
  * The context uses a global variable 'dummyFDContext' so that mocked functions
@@ -322,7 +325,13 @@ virTestDummyFDContextFree(virTestDummyFDContext *ctxt 
G_GNUC_UNUSED)
 virTestDummyFDContext *
 virTestDummyFDContextNew(void)
 {
-    return dummyFDContext = virHashNew(g_free);
+    if (dummyFDContext)
+        return dummyFDContext;
+
+    dummyFDContext = g_new0(virTestDummyFDContext, 1);
+    dummyFDContext->hints = virHashNew(g_free);
+
+    return dummyFDContext;
 }


@@ -342,7 +351,7 @@ virTestDummyFDContextMarkFD(int fd,
         return;
     }

-    g_hash_table_insert(dummyFDContext, g_strdup_printf("%d", fd), hint);
+    g_hash_table_insert(dummyFDContext->hints, g_strdup_printf("%d", fd), 
hint);
 }


@@ -366,7 +375,7 @@ virTestMakeDummyMarkDup(int newfd,

     oldlabel = g_strdup_printf("%d", oldfd);

-    if (!(oldhint = g_hash_table_lookup(dummyFDContext, oldlabel)))
+    if (!(oldhint = g_hash_table_lookup(dummyFDContext->hints, oldlabel)))
         return;

     virTestDummyFDContextMarkFD(newfd, g_strdup_printf("%s-dup", oldhint));
diff --git a/tests/testutils.h b/tests/testutils.h
index 84003ba0ac..a7cc0b16be 100644
--- a/tests/testutils.h
+++ b/tests/testutils.h
@@ -106,8 +106,11 @@ const char *virTestCounterNext(void);
 char *virTestFakeRootDirInit(void);
 void virTestFakeRootDirCleanup(char *fakerootdir);

+struct _virTestDummyFDContext {
+    GHashTable *hints;
+};

-typedef GHashTable virTestDummyFDContext;
+typedef struct _virTestDummyFDContext virTestDummyFDContext;

 void virTestDummyFDContextFree(virTestDummyFDContext *ctxt);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virTestDummyFDContext, 
virTestDummyFDContextFree);
diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h
index f30ddad3b6..9fe3c2d1b4 100644
--- a/tests/testutilsqemu.h
+++ b/tests/testutilsqemu.h
@@ -22,6 +22,8 @@
 #include "qemu/qemu_capabilities.h"
 #include "qemu/qemu_conf.h"

+#include "testutils.h"
+
 #define TEST_QEMU_CAPS_PATH abs_srcdir "/qemucapabilitiesdata"
 #define TEST_TPM_ENV_VAR "VIR_TEST_MOCK_FAKE_TPM_VERSION"
 #define TPM_VER_1_2 "1.2"
@@ -119,7 +121,7 @@ struct _testQemuInfo {
     struct testQemuArgs args;
     struct testQemuConf *conf;

-    GHashTable *fdsubsts;
+    virTestDummyFDContext *fdsubsts;
 };

 typedef struct _testQemuInfo testQemuInfo;
-- 
2.54.0

Reply via email to