Abstract out the function used to generate the commandline for 'mdevctl
start' since they take the same arguments. Add tests to ensure that
we're generating the command properly.
Signed-off-by: Jonathon Jongsma <jjongsma(a)redhat.com>
---
src/node_device/node_device_driver.c | 28 ++++++--
src/node_device/node_device_driver.h | 5 ++
...19_36ea_4111_8f0a_8c9a70e21366-define.argv | 1 +
...19_36ea_4111_8f0a_8c9a70e21366-define.json | 1 +
...39_495e_4243_ad9f_beb3f14c23d9-define.argv | 1 +
...39_495e_4243_ad9f_beb3f14c23d9-define.json | 1 +
...16_1ca8_49ac_b176_871d16c13076-define.argv | 1 +
...16_1ca8_49ac_b176_871d16c13076-define.json | 1 +
tests/nodedevmdevctltest.c | 68 ++++++++++++++-----
9 files changed, 85 insertions(+), 22 deletions(-)
create mode 100644
tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv
create mode 100644
tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.json
create mode 100644
tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv
create mode 100644
tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.json
create mode 100644
tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv
create mode 100644
tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.json
diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c
index 598cd865c5..c84f6ef03f 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -698,9 +698,13 @@ nodeDeviceFindAddressByName(const char *name)
}
-virCommandPtr
-nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def,
- char **uuid_out)
+/* the mdevctl 'start' and 'define' commands accept almost the exact
same
+ * arguments, so provide a common implementation that can be wrapped by a more
+ * specific function */
+static virCommandPtr
+nodeDeviceGetMdevctlDefineStartCommand(virNodeDeviceDefPtr def,
+ const char *subcommand,
+ char **uuid_out)
{
virCommandPtr cmd;
g_autofree char *json = NULL;
@@ -718,7 +722,7 @@ nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def,
return NULL;
}
- cmd = virCommandNewArgList(MDEVCTL, "start",
+ cmd = virCommandNewArgList(MDEVCTL, subcommand,
"-p", parent_addr,
"--jsonfile", "/dev/stdin",
NULL);
@@ -729,6 +733,22 @@ nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def,
return cmd;
}
+virCommandPtr
+nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def,
+ char **uuid_out)
+{
+ return nodeDeviceGetMdevctlDefineStartCommand(def, "start", uuid_out);
+}
+
+virCommandPtr
+nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDefPtr def,
+ char **uuid_out)
+{
+ return nodeDeviceGetMdevctlDefineStartCommand(def, "define", uuid_out);
+}
+
+
+
static int
virMdevctlStart(virNodeDeviceDefPtr def, char **uuid)
{
diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_device_driver.h
index d8837e4ef8..04081a871d 100644
--- a/src/node_device/node_device_driver.h
+++ b/src/node_device/node_device_driver.h
@@ -116,6 +116,11 @@ nodeConnectNodeDeviceEventDeregisterAny(virConnectPtr conn,
virCommandPtr
nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def,
char **uuid_out);
+
+virCommandPtr
+nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDefPtr def,
+ char **uuid_out);
+
virCommandPtr
nodeDeviceGetMdevctlStopCommand(const char *uuid);
diff --git
a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv
b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv
new file mode 100644
index 0000000000..773e98b963
--- /dev/null
+++ b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv
@@ -0,0 +1 @@
+$MDEVCTL_BINARY$ define -p 0000:00:02.0 --jsonfile /dev/stdin
diff --git
a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.json
b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.json
new file mode 100644
index 0000000000..bfc6dcace3
--- /dev/null
+++ b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.json
@@ -0,0 +1 @@
+{"mdev_type":"i915-GVTg_V5_8","start":"manual"}
\ No newline at end of file
diff --git
a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv
b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv
new file mode 100644
index 0000000000..773e98b963
--- /dev/null
+++ b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv
@@ -0,0 +1 @@
+$MDEVCTL_BINARY$ define -p 0000:00:02.0 --jsonfile /dev/stdin
diff --git
a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.json
b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.json
new file mode 100644
index 0000000000..e5b22b2c44
--- /dev/null
+++ b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.json
@@ -0,0 +1 @@
+{"mdev_type":"i915-GVTg_V5_8","start":"manual","attrs":[{"example-attribute-1":"attribute-value-1"},{"example-attribute-2":"attribute-value-2"}]}
\ No newline at end of file
diff --git
a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv
b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv
new file mode 100644
index 0000000000..773e98b963
--- /dev/null
+++ b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv
@@ -0,0 +1 @@
+$MDEVCTL_BINARY$ define -p 0000:00:02.0 --jsonfile /dev/stdin
diff --git
a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.json
b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.json
new file mode 100644
index 0000000000..2e03d0bd8e
--- /dev/null
+++ b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.json
@@ -0,0 +1 @@
+{"mdev_type":"i915-GVTg_V5_8","start":"manual","attrs":[{"example-attribute":"attribute-value"}]}
\ No newline at end of file
diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c
index 8bc464d59f..1f48a605b3 100644
--- a/tests/nodedevmdevctltest.c
+++ b/tests/nodedevmdevctltest.c
@@ -10,10 +10,16 @@
#define VIR_FROM_THIS VIR_FROM_NODEDEV
+typedef enum {
+ MDEVCTL_CMD_START,
+ MDEVCTL_CMD_DEFINE,
+} MdevctlCmd;
+
struct startTestInfo {
const char *virt_type;
int create;
const char *filename;
+ MdevctlCmd command;
};
/* capture stdin passed to command */
@@ -45,12 +51,17 @@ nodedevCompareToFile(const char *actual,
return virTestCompareToFile(replacedCmdline, filename);
}
+
+typedef virCommandPtr (*MdevctlCmdFunc)(virNodeDeviceDefPtr, char **);
+
+
static int
testMdevctlStart(const char *virt_type,
int create,
+ MdevctlCmdFunc mdevctl_cmd_func,
const char *mdevxml,
- const char *startcmdfile,
- const char *startjsonfile)
+ const char *cmdfile,
+ const char *jsonfile)
{
g_autoptr(virNodeDeviceDef) def = NULL;
virNodeDeviceObjPtr obj = NULL;
@@ -66,7 +77,7 @@ testMdevctlStart(const char *virt_type,
/* this function will set a stdin buffer containing the json configuration
* of the device. The json value is captured in the callback above */
- cmd = nodeDeviceGetMdevctlStartCommand(def, &uuid);
+ cmd = mdevctl_cmd_func(def, &uuid);
if (!cmd)
goto cleanup;
@@ -78,10 +89,10 @@ testMdevctlStart(const char *virt_type,
if (!(actualCmdline = virBufferCurrentContent(&buf)))
goto cleanup;
- if (nodedevCompareToFile(actualCmdline, startcmdfile) < 0)
+ if (nodedevCompareToFile(actualCmdline, cmdfile) < 0)
goto cleanup;
- if (virTestCompareToFile(stdinbuf, startjsonfile) < 0)
+ if (virTestCompareToFile(stdinbuf, jsonfile) < 0)
goto cleanup;
ret = 0;
@@ -96,17 +107,31 @@ static int
testMdevctlStartHelper(const void *data)
{
const struct startTestInfo *info = data;
+ const char *cmd;
+ MdevctlCmdFunc func;
+ g_autofree char *mdevxml = NULL;
+ g_autofree char *cmdlinefile = NULL;
+ g_autofree char *jsonfile = NULL;
+
+ if (info->command == MDEVCTL_CMD_START) {
+ cmd = "start";
+ func = nodeDeviceGetMdevctlStartCommand;
+ } else if (info->command == MDEVCTL_CMD_DEFINE) {
+ cmd = "define";
+ func = nodeDeviceGetMdevctlDefineCommand;
+ } else {
+ return -1;
+ }
- g_autofree char *mdevxml = g_strdup_printf("%s/nodedevschemadata/%s.xml",
- abs_srcdir, info->filename);
- g_autofree char *cmdlinefile =
g_strdup_printf("%s/nodedevmdevctldata/%s-start.argv",
- abs_srcdir, info->filename);
- g_autofree char *jsonfile =
g_strdup_printf("%s/nodedevmdevctldata/%s-start.json",
- abs_srcdir, info->filename);
+ mdevxml = g_strdup_printf("%s/nodedevschemadata/%s.xml", abs_srcdir,
+ info->filename);
+ cmdlinefile = g_strdup_printf("%s/nodedevmdevctldata/%s-%s.argv",
+ abs_srcdir, info->filename, cmd);
+ jsonfile = g_strdup_printf("%s/nodedevmdevctldata/%s-%s.json", abs_srcdir,
+ info->filename, cmd);
- return testMdevctlStart(info->virt_type,
- info->create, mdevxml, cmdlinefile,
- jsonfile);
+ return testMdevctlStart(info->virt_type, info->create, func,
+ mdevxml, cmdlinefile, jsonfile);
}
static int
@@ -347,15 +372,18 @@ mymain(void)
if (virTestRun(desc, func, info) < 0) \
ret = -1;
-#define DO_TEST_START_FULL(virt_type, create, filename) \
+#define DO_TEST_CMD(desc, virt_type, create, filename, command) \
do { \
- struct startTestInfo info = { virt_type, create, filename }; \
- DO_TEST_FULL("mdevctl start " filename, testMdevctlStartHelper,
&info); \
+ struct startTestInfo info = { virt_type, create, filename, command }; \
+ DO_TEST_FULL(desc, testMdevctlStartHelper, &info); \
} \
while (0)
#define DO_TEST_START(filename) \
- DO_TEST_START_FULL("QEMU", CREATE_DEVICE, filename)
+ DO_TEST_CMD("mdevctl start " filename, "QEMU", CREATE_DEVICE,
filename, MDEVCTL_CMD_START)
+
+#define DO_TEST_DEFINE(filename) \
+ DO_TEST_CMD("mdevctl define " filename, "QEMU", CREATE_DEVICE,
filename, MDEVCTL_CMD_DEFINE)
#define DO_TEST_STOP(uuid) \
DO_TEST_FULL("mdevctl stop " uuid, testMdevctlStop, uuid)
@@ -378,6 +406,10 @@ mymain(void)
DO_TEST_PARSE_JSON("mdevctl-list-multiple");
+ DO_TEST_DEFINE("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366");
+ DO_TEST_DEFINE("mdev_fedc4916_1ca8_49ac_b176_871d16c13076");
+ DO_TEST_DEFINE("mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9");
+
done:
nodedevTestDriverFree(driver);
--
2.26.2