[libvirt PATCH v4 17/25] nodedev: refactor tests to support mdev undefine

Jonathon Jongsma jjongsma at redhat.com
Wed Feb 3 17:39:01 UTC 2021


mdevctl 'stop' and 'undefine' commands take the same uuid parameter, so
refactor the test infrastructure to share common implementation for both
of these commands. The 'undefine' command will be introduced in a
following patch.

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
 tests/nodedevmdevctltest.c | 48 +++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 8 deletions(-)

diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c
index 1f48a605b3..84ae1b67be 100644
--- a/tests/nodedevmdevctltest.c
+++ b/tests/nodedevmdevctltest.c
@@ -12,7 +12,9 @@
 
 typedef enum {
     MDEVCTL_CMD_START,
+    MDEVCTL_CMD_STOP,
     MDEVCTL_CMD_DEFINE,
+    MDEVCTL_CMD_UNDEFINE
 } MdevctlCmd;
 
 struct startTestInfo {
@@ -134,19 +136,21 @@ testMdevctlStartHelper(const void *data)
                             mdevxml, cmdlinefile, jsonfile);
 }
 
+typedef virCommandPtr (*GetStopUndefineCmdFunc)(const char *uuid);
+struct UuidCommandTestInfo {
+    const char *uuid;
+    MdevctlCmd command;
+};
+
 static int
-testMdevctlStop(const void *data)
+testMdevctlUuidCommand(const char *uuid, GetStopUndefineCmdFunc func, const char *outfile)
 {
-    const char *uuid = data;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     const char *actualCmdline = NULL;
     int ret = -1;
     g_autoptr(virCommand) cmd = NULL;
-    g_autofree char *cmdlinefile =
-        g_strdup_printf("%s/nodedevmdevctldata/mdevctl-stop.argv",
-                        abs_srcdir);
 
-    cmd = nodeDeviceGetMdevctlStopCommand(uuid);
+    cmd = func(uuid);
 
     if (!cmd)
         goto cleanup;
@@ -158,7 +162,7 @@ testMdevctlStop(const void *data)
     if (!(actualCmdline = virBufferCurrentContent(&buf)))
         goto cleanup;
 
-    if (nodedevCompareToFile(actualCmdline, cmdlinefile) < 0)
+    if (nodedevCompareToFile(actualCmdline, outfile) < 0)
         goto cleanup;
 
     ret = 0;
@@ -168,6 +172,27 @@ testMdevctlStop(const void *data)
     return ret;
 }
 
+static int
+testMdevctlUuidCommandHelper(const void *data)
+{
+    const struct UuidCommandTestInfo *info = data;
+    GetStopUndefineCmdFunc func;
+    const char *cmd;
+    g_autofree char *cmdlinefile = NULL;
+
+    if (info->command == MDEVCTL_CMD_STOP) {
+        cmd = "stop";
+        func = nodeDeviceGetMdevctlStopCommand;
+    } else {
+        return -1;
+    }
+
+    cmdlinefile = g_strdup_printf("%s/nodedevmdevctldata/mdevctl-%s.argv",
+                                  abs_srcdir, cmd);
+
+    return testMdevctlUuidCommand(info->uuid, func, cmdlinefile);
+}
+
 static int
 testMdevctlListDefined(const void *data G_GNUC_UNUSED)
 {
@@ -385,8 +410,15 @@ mymain(void)
 #define DO_TEST_DEFINE(filename) \
     DO_TEST_CMD("mdevctl define " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_DEFINE)
 
+#define DO_TEST_UUID_COMMAND_FULL(desc, uuid, command) \
+    do { \
+        struct UuidCommandTestInfo info = { uuid, command }; \
+        DO_TEST_FULL(desc, testMdevctlUuidCommandHelper, &info); \
+       } \
+    while (0)
+
 #define DO_TEST_STOP(uuid) \
-    DO_TEST_FULL("mdevctl stop " uuid, testMdevctlStop, uuid)
+    DO_TEST_UUID_COMMAND_FULL("mdevctl stop " uuid, uuid, MDEVCTL_CMD_STOP)
 
 #define DO_TEST_LIST_DEFINED() \
     DO_TEST_FULL("mdevctl list --defined", testMdevctlListDefined, NULL)
-- 
2.26.2




More information about the libvir-list mailing list