[libvirt PATCH 00/11] Some additional cleanups to mdev support

Erik recommended several changes to the mdev series that was just merged and suggested to address them in a follow-up patch series. They're mostly related to simplifying the testing, and shouldn't actually change any behavior (aside from switching to using the long commandline options when executing mdevctl). Erik Skultety (4): nodedev: driver: Swap virMdevctlStart and virMdevctlCreate nodedev: driver: Introduce internal mdevctl commands enum nodedev: driver: Create a generic mdevctl command translator tests: nodedev: Make the mdevctl test function and helper generic Jonathon Jongsma (7): nodedev: don't log error in nodeDeviceFindAddressByName() nodedev: avoid use of VIR_ERR_NO_* errors internally tests: nodedev: switch all test macros to accept a filename nodedev: Switch to using long options for mdevctl nodedev: Remove GetMdevctl*Command() wrappers tests: nodedev: simplify test macros tests: nodedev: remove unused variable src/node_device/node_device_driver.c | 181 ++++++++--------- src/node_device/node_device_driver.h | 43 ++-- ...19_36ea_4111_8f0a_8c9a70e21366-create.argv | 2 + ...9_36ea_4111_8f0a_8c9a70e21366-create.json} | 0 ...19_36ea_4111_8f0a_8c9a70e21366-define.argv | 2 +- ...019_36ea_4111_8f0a_8c9a70e21366-start.argv | 3 +- ...d019_36ea_4111_8f0a_8c9a70e21366-stop.argv | 1 + ..._36ea_4111_8f0a_8c9a70e21366-undefine.argv | 1 + ...39_495e_4243_ad9f_beb3f14c23d9-create.argv | 1 + ...9_495e_4243_ad9f_beb3f14c23d9-create.json} | 0 ...39_495e_4243_ad9f_beb3f14c23d9-define.argv | 2 +- ...d39_495e_4243_ad9f_beb3f14c23d9-start.argv | 1 - ...16_1ca8_49ac_b176_871d16c13076-create.argv | 1 + ...6_1ca8_49ac_b176_871d16c13076-create.json} | 0 ...16_1ca8_49ac_b176_871d16c13076-define.argv | 2 +- ...916_1ca8_49ac_b176_871d16c13076-start.argv | 1 - tests/nodedevmdevctldata/mdevctl-create.argv | 1 - tests/nodedevmdevctldata/mdevctl-stop.argv | 1 - .../nodedevmdevctldata/mdevctl-undefine.argv | 1 - tests/nodedevmdevctltest.c | 192 ++++++------------ 20 files changed, 174 insertions(+), 262 deletions(-) create mode 100644 tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9= a70e21366-create.argv rename tests/nodedevmdevctldata/{mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-s= tart.json =3D> mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.json} (100%) create mode 100644 tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9= a70e21366-stop.argv create mode 100644 tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9= a70e21366-undefine.argv create mode 100644 tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb= 3f14c23d9-create.argv rename tests/nodedevmdevctldata/{mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-s= tart.json =3D> mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.json} (100%) delete mode 100644 tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb= 3f14c23d9-start.argv create mode 100644 tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871= d16c13076-create.argv rename tests/nodedevmdevctldata/{mdev_fedc4916_1ca8_49ac_b176_871d16c13076-s= tart.json =3D> mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.json} (100%) delete mode 100644 tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871= d16c13076-start.argv delete mode 100644 tests/nodedevmdevctldata/mdevctl-create.argv delete mode 100644 tests/nodedevmdevctldata/mdevctl-stop.argv delete mode 100644 tests/nodedevmdevctldata/mdevctl-undefine.argv --=20 2.26.3

The calling function will log the error. Just return NULL if a device cannot be found. Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- src/node_device/node_device_driver.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index c11b8d89ab..cb90401d0a 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -630,11 +630,8 @@ nodeDeviceFindAddressByName(const char *name) char *addr = NULL; virNodeDeviceObjPtr dev = virNodeDeviceObjListFindByName(driver->devs, name); - if (!dev) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("could not find device '%s'"), name); + if (!dev) return NULL; - } def = virNodeDeviceObjGetDef(dev); for (caps = def->caps; caps != NULL; caps = caps->next) { -- 2.26.3

These errors are demoted to debug statements[1] since they're only intended to be used as return values for public APIs. This makes it difficult to debug the problem when something goes wrong since no error message is logged. Switch instead to VIR_ERR_INTERNAL_ERROR so that the error is logged as expected. [1] See the implementation of daemonErrorLogFilter() for details: https://gitlab.com/libvirt/libvirt/-/blob/e2f82a3704f680fbb37a733476d870c192... Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- src/node_device/node_device_driver.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index cb90401d0a..8578f50e93 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -709,7 +709,7 @@ nodeDeviceGetMdevctlDefineStartCommand(virNodeDeviceDef *def, g_autofree char *parent_addr = nodeDeviceFindAddressByName(def->parent); if (!parent_addr) { - virReportError(VIR_ERR_NO_NODE_DEVICE, + virReportError(VIR_ERR_INTERNAL_ERROR, _("unable to find parent device '%s'"), def->parent); return NULL; } -- 2.26.3

Rather than specifying a UUID string to some test macros, just pass a filename to an xml definition. This helps work toward unifying the test macros and making it more maintainable. Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- tests/nodedevmdevctldata/mdevctl-create.argv | 2 +- tests/nodedevmdevctldata/mdevctl-stop.argv | 2 +- .../nodedevmdevctldata/mdevctl-undefine.argv | 2 +- tests/nodedevmdevctltest.c | 34 ++++++++++++------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/tests/nodedevmdevctldata/mdevctl-create.argv b/tests/nodedevmdevctldata/mdevctl-create.argv index 802109340c..dc0ae7be0e 100644 --- a/tests/nodedevmdevctldata/mdevctl-create.argv +++ b/tests/nodedevmdevctldata/mdevctl-create.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ start -u 8a05ad83-3472-497d-8631-8142f31460e8 +$MDEVCTL_BINARY$ start -u d069d019-36ea-4111-8f0a-8c9a70e21366 diff --git a/tests/nodedevmdevctldata/mdevctl-stop.argv b/tests/nodedevmdevctldata/mdevctl-stop.argv index 3dbaab671b..5bdb213a32 100644 --- a/tests/nodedevmdevctldata/mdevctl-stop.argv +++ b/tests/nodedevmdevctldata/mdevctl-stop.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ stop -u e2451f73-c95b-4124-b900-e008af37c576 +$MDEVCTL_BINARY$ stop -u d069d019-36ea-4111-8f0a-8c9a70e21366 diff --git a/tests/nodedevmdevctldata/mdevctl-undefine.argv b/tests/nodedevmdevctldata/mdevctl-undefine.argv index 54717455f7..c2236727ef 100644 --- a/tests/nodedevmdevctldata/mdevctl-undefine.argv +++ b/tests/nodedevmdevctldata/mdevctl-undefine.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ undefine -u d76a6b78-45ed-4149-a325-005f9abc5281 +$MDEVCTL_BINARY$ undefine -u d069d019-36ea-4111-8f0a-8c9a70e21366 diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index 050424116f..a95bdf6d6e 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -140,20 +140,25 @@ testMdevctlStartOrDefineHelper(const void *data) typedef virCommand* (*GetStopUndefineCmdFunc)(const char *uuid, char **errbuf); struct UuidCommandTestInfo { - const char *uuid; + const char *filename; MdevctlCmd command; }; static int -testMdevctlUuidCommand(const char *uuid, GetStopUndefineCmdFunc func, const char *outfile) +testMdevctlUuidCommand(GetStopUndefineCmdFunc func, + const char *mdevxml, const char *outfile) { + g_autoptr(virNodeDeviceDef) def = NULL; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; const char *actualCmdline = NULL; int ret = -1; g_autoptr(virCommand) cmd = NULL; g_autofree char *errmsg = NULL; - cmd = func(uuid, &errmsg); + if (!(def = virNodeDeviceDefParseFile(mdevxml, EXISTING_DEVICE, "QEMU"))) + goto cleanup; + + cmd = func(def->caps->data.mdev.uuid, &errmsg); if (!cmd) goto cleanup; @@ -182,6 +187,7 @@ testMdevctlUuidCommandHelper(const void *data) GetStopUndefineCmdFunc func; const char *cmd; g_autofree char *cmdlinefile = NULL; + g_autofree char *mdevxml = NULL; if (info->command == MDEVCTL_CMD_STOP) { cmd = "stop"; @@ -196,10 +202,12 @@ testMdevctlUuidCommandHelper(const void *data) return -1; } + mdevxml = g_strdup_printf("%s/nodedevschemadata/%s.xml", abs_srcdir, + info->filename); cmdlinefile = g_strdup_printf("%s/nodedevmdevctldata/mdevctl-%s.argv", abs_srcdir, cmd); - return testMdevctlUuidCommand(info->uuid, func, cmdlinefile); + return testMdevctlUuidCommand(func, mdevxml, cmdlinefile); } static int @@ -427,14 +435,14 @@ mymain(void) } \ while (0) -#define DO_TEST_STOP(uuid) \ - DO_TEST_UUID_COMMAND_FULL("mdevctl stop " uuid, uuid, MDEVCTL_CMD_STOP) +#define DO_TEST_STOP(filename) \ + DO_TEST_UUID_COMMAND_FULL("mdevctl stop " filename, filename, MDEVCTL_CMD_STOP) -#define DO_TEST_UNDEFINE(uuid) \ - DO_TEST_UUID_COMMAND_FULL("mdevctl undefine " uuid, uuid, MDEVCTL_CMD_UNDEFINE) +#define DO_TEST_UNDEFINE(filename) \ + DO_TEST_UUID_COMMAND_FULL("mdevctl undefine " filename, filename, MDEVCTL_CMD_UNDEFINE) -#define DO_TEST_CREATE(uuid) \ - DO_TEST_UUID_COMMAND_FULL("mdevctl create " uuid, uuid, MDEVCTL_CMD_CREATE) +#define DO_TEST_CREATE(filename) \ + DO_TEST_UUID_COMMAND_FULL("mdevctl create " filename, filename, MDEVCTL_CMD_CREATE) #define DO_TEST_LIST_DEFINED() \ DO_TEST_FULL("mdevctl list --defined", testMdevctlListDefined, NULL) @@ -448,7 +456,7 @@ mymain(void) DO_TEST_START("mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9"); /* Test mdevctl stop command, pass an arbitrary uuid */ - DO_TEST_STOP("e2451f73-c95b-4124-b900-e008af37c576"); + DO_TEST_STOP("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); DO_TEST_LIST_DEFINED(); @@ -458,9 +466,9 @@ mymain(void) DO_TEST_DEFINE("mdev_fedc4916_1ca8_49ac_b176_871d16c13076"); DO_TEST_DEFINE("mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9"); - DO_TEST_UNDEFINE("d76a6b78-45ed-4149-a325-005f9abc5281"); + DO_TEST_UNDEFINE("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); - DO_TEST_CREATE("8a05ad83-3472-497d-8631-8142f31460e8"); + DO_TEST_CREATE("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); done: nodedevTestDriverFree(driver); -- 2.26.3

From: Erik Skultety <eskultet@redhat.com> "start" in libvirt means - "take this object and create an instance out of it" "create" in libvirt most of the time means - "take and XML description, make an object out of it and use it to create an instance" This gets confusing with mdevctl which uses "start" for both. So, this patch proposes to use virMdevctlStart in cases where from libvirt's POV we're starting a defined device (unlike mdevctl). Analogically, use virMdevctlCreate in scenarios where XML description is passed to libvirt and a transient device is supposed to be created. Signed-off-by: Erik Skultety <eskultet@redhat.com> Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- src/node_device/node_device_driver.c | 34 ++++++------- src/node_device/node_device_driver.h | 10 ++-- ...9_36ea_4111_8f0a_8c9a70e21366-create.argv} | 0 ...9_36ea_4111_8f0a_8c9a70e21366-create.json} | 0 ...9_495e_4243_ad9f_beb3f14c23d9-create.argv} | 0 ...9_495e_4243_ad9f_beb3f14c23d9-create.json} | 0 ...6_1ca8_49ac_b176_871d16c13076-create.argv} | 0 ...6_1ca8_49ac_b176_871d16c13076-create.json} | 0 ...mdevctl-create.argv => mdevctl-start.argv} | 0 tests/nodedevmdevctltest.c | 49 +++++++++---------- 10 files changed, 46 insertions(+), 47 deletions(-) rename tests/nodedevmdevctldata/{mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.argv => mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.argv} (100%) rename tests/nodedevmdevctldata/{mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.json => mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.json} (100%) rename tests/nodedevmdevctldata/{mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-start.argv => mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.argv} (100%) rename tests/nodedevmdevctldata/{mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-start.json => mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.json} (100%) rename tests/nodedevmdevctldata/{mdev_fedc4916_1ca8_49ac_b176_871d16c13076-start.argv => mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.argv} (100%) rename tests/nodedevmdevctldata/{mdev_fedc4916_1ca8_49ac_b176_871d16c13076-start.json => mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.json} (100%) rename tests/nodedevmdevctldata/{mdevctl-create.argv => mdevctl-start.argv} (100%) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index 8578f50e93..3236dd7bc3 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -699,10 +699,10 @@ nodeDeviceFindAddressByName(const char *name) * arguments, so provide a common implementation that can be wrapped by a more * specific function */ static virCommand* -nodeDeviceGetMdevctlDefineStartCommand(virNodeDeviceDef *def, - const char *subcommand, - char **uuid_out, - char **errmsg) +nodeDeviceGetMdevctlDefineCreateCommand(virNodeDeviceDef *def, + const char *subcommand, + char **uuid_out, + char **errmsg) { virCommandPtr cmd; g_autofree char *json = NULL; @@ -737,12 +737,12 @@ nodeDeviceGetMdevctlDefineStartCommand(virNodeDeviceDef *def, } virCommand* -nodeDeviceGetMdevctlStartCommand(virNodeDeviceDef *def, +nodeDeviceGetMdevctlCreateCommand(virNodeDeviceDef *def, char **uuid_out, char **errmsg) { - return nodeDeviceGetMdevctlDefineStartCommand(def, "start", uuid_out, - errmsg); + return nodeDeviceGetMdevctlDefineCreateCommand(def, "start", uuid_out, + errmsg); } virCommand* @@ -750,18 +750,18 @@ nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, char **uuid_out, char **errmsg) { - return nodeDeviceGetMdevctlDefineStartCommand(def, "define", uuid_out, - errmsg); + return nodeDeviceGetMdevctlDefineCreateCommand(def, "define", uuid_out, + errmsg); } static int -virMdevctlStart(virNodeDeviceDefPtr def, char **uuid, char **errmsg) +virMdevctlCreate(virNodeDeviceDefPtr def, char **uuid, char **errmsg) { int status; - g_autoptr(virCommand) cmd = nodeDeviceGetMdevctlStartCommand(def, uuid, - errmsg); + g_autoptr(virCommand) cmd = nodeDeviceGetMdevctlCreateCommand(def, uuid, + errmsg); if (!cmd) return -1; @@ -811,7 +811,7 @@ nodeDeviceCreateXMLMdev(virConnectPtr conn, return NULL; } - if (virMdevctlStart(def, &uuid, &errmsg) < 0) { + if (virMdevctlCreate(def, &uuid, &errmsg) < 0) { if (errmsg) virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to start mediated device '%s': %s"), @@ -910,7 +910,7 @@ nodeDeviceGetMdevctlUndefineCommand(const char *uuid, char **errmsg) } virCommand* -nodeDeviceGetMdevctlCreateCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlStartCommand(const char *uuid, char **errmsg) { virCommand *cmd = virCommandNewArgList(MDEVCTL, "start", @@ -953,12 +953,12 @@ virMdevctlUndefine(virNodeDeviceDef *def, char **errmsg) static int -virMdevctlCreate(virNodeDeviceDef *def, char **errmsg) +virMdevctlStart(virNodeDeviceDef *def, char **errmsg) { int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlCreateCommand(def->caps->data.mdev.uuid, errmsg); + cmd = nodeDeviceGetMdevctlStartCommand(def->caps->data.mdev.uuid, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; @@ -1412,7 +1412,7 @@ nodeDeviceCreate(virNodeDevice *device, if (nodeDeviceHasCapability(def, VIR_NODE_DEV_CAP_MDEV)) { g_autofree char *errmsg = NULL; - if (virMdevctlCreate(def, &errmsg) < 0) { + if (virMdevctlStart(def, &errmsg) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to create mediated device: %s"), errmsg && errmsg[0] ? errmsg : "Unknown Error"); diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_device_driver.h index 8a935ffed6..0e3f6dbf28 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -123,9 +123,9 @@ nodeConnectNodeDeviceEventDeregisterAny(virConnectPtr conn, int callbackID); virCommandPtr -nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def, - char **uuid_out, - char **errmsg); +nodeDeviceGetMdevctlCreateCommand(virNodeDeviceDefPtr def, + char **uuid_out, + char **errmsg); virCommand* nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, @@ -162,8 +162,8 @@ bool nodeDeviceDefCopyFromMdevctl(virNodeDeviceDef *dst, virNodeDeviceDef *src); virCommand* -nodeDeviceGetMdevctlCreateCommand(const char *uuid, - char **errmsg); +nodeDeviceGetMdevctlStartCommand(const char *uuid, + char **errmsg); int nodeDeviceCreate(virNodeDevice *dev, diff --git a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.argv b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.argv similarity index 100% rename from tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.argv rename to tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.argv diff --git a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.json b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.json similarity index 100% rename from tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.json rename to tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.json diff --git a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-start.argv b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.argv similarity index 100% rename from tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-start.argv rename to tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.argv diff --git a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-start.json b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.json similarity index 100% rename from tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-start.json rename to tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.json diff --git a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-start.argv b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.argv similarity index 100% rename from tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-start.argv rename to tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.argv diff --git a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-start.json b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.json similarity index 100% rename from tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-start.json rename to tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.json diff --git a/tests/nodedevmdevctldata/mdevctl-create.argv b/tests/nodedevmdevctldata/mdevctl-start.argv similarity index 100% rename from tests/nodedevmdevctldata/mdevctl-create.argv rename to tests/nodedevmdevctldata/mdevctl-start.argv diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index a95bdf6d6e..427299e171 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -59,12 +59,12 @@ typedef virCommand* (*MdevctlCmdFunc)(virNodeDeviceDef *, char **, char **); static int -testMdevctlStartOrDefine(const char *virt_type, - int create, - MdevctlCmdFunc mdevctl_cmd_func, - const char *mdevxml, - const char *cmdfile, - const char *jsonfile) +testMdevctlCreateOrDefine(const char *virt_type, + int create, + MdevctlCmdFunc mdevctl_cmd_func, + const char *mdevxml, + const char *cmdfile, + const char *jsonfile) { g_autoptr(virNodeDeviceDef) def = NULL; virNodeDeviceObjPtr obj = NULL; @@ -108,7 +108,7 @@ testMdevctlStartOrDefine(const char *virt_type, } static int -testMdevctlStartOrDefineHelper(const void *data) +testMdevctlCreateOrDefineHelper(const void *data) { const struct startTestInfo *info = data; const char *cmd; @@ -117,9 +117,9 @@ testMdevctlStartOrDefineHelper(const void *data) g_autofree char *cmdlinefile = NULL; g_autofree char *jsonfile = NULL; - if (info->command == MDEVCTL_CMD_START) { - cmd = "start"; - func = nodeDeviceGetMdevctlStartCommand; + if (info->command == MDEVCTL_CMD_CREATE) { + cmd = "create"; + func = nodeDeviceGetMdevctlCreateCommand; } else if (info->command == MDEVCTL_CMD_DEFINE) { cmd = "define"; func = nodeDeviceGetMdevctlDefineCommand; @@ -134,8 +134,8 @@ testMdevctlStartOrDefineHelper(const void *data) jsonfile = g_strdup_printf("%s/nodedevmdevctldata/%s-%s.json", abs_srcdir, info->filename, cmd); - return testMdevctlStartOrDefine(info->virt_type, info->create, func, - mdevxml, cmdlinefile, jsonfile); + return testMdevctlCreateOrDefine(info->virt_type, info->create, func, + mdevxml, cmdlinefile, jsonfile); } typedef virCommand* (*GetStopUndefineCmdFunc)(const char *uuid, char **errbuf); @@ -195,9 +195,9 @@ testMdevctlUuidCommandHelper(const void *data) } else if (info->command == MDEVCTL_CMD_UNDEFINE) { cmd = "undefine"; func = nodeDeviceGetMdevctlUndefineCommand; - }else if (info->command == MDEVCTL_CMD_CREATE) { - cmd = "create"; - func = nodeDeviceGetMdevctlCreateCommand; + }else if (info->command == MDEVCTL_CMD_START) { + cmd = "start"; + func = nodeDeviceGetMdevctlStartCommand; } else { return -1; } @@ -418,12 +418,12 @@ mymain(void) #define DO_TEST_CMD(desc, virt_type, create, filename, command) \ do { \ struct startTestInfo info = { virt_type, create, filename, command }; \ - DO_TEST_FULL(desc, testMdevctlStartOrDefineHelper, &info); \ + DO_TEST_FULL(desc, testMdevctlCreateOrDefineHelper, &info); \ } \ while (0) -#define DO_TEST_START(filename) \ - DO_TEST_CMD("mdevctl start " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_START) +#define DO_TEST_CREATE(filename) \ + DO_TEST_CMD("mdevctl create " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_CREATE) #define DO_TEST_DEFINE(filename) \ DO_TEST_CMD("mdevctl define " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_DEFINE) @@ -441,8 +441,8 @@ mymain(void) #define DO_TEST_UNDEFINE(filename) \ DO_TEST_UUID_COMMAND_FULL("mdevctl undefine " filename, filename, MDEVCTL_CMD_UNDEFINE) -#define DO_TEST_CREATE(filename) \ - DO_TEST_UUID_COMMAND_FULL("mdevctl create " filename, filename, MDEVCTL_CMD_CREATE) +#define DO_TEST_START(filename) \ + DO_TEST_UUID_COMMAND_FULL("mdevctl start " filename, filename, MDEVCTL_CMD_START) #define DO_TEST_LIST_DEFINED() \ DO_TEST_FULL("mdevctl list --defined", testMdevctlListDefined, NULL) @@ -450,10 +450,9 @@ mymain(void) #define DO_TEST_PARSE_JSON(filename) \ DO_TEST_FULL("parse mdevctl json " filename, testMdevctlParse, filename) - /* Test mdevctl start commands */ - DO_TEST_START("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); - DO_TEST_START("mdev_fedc4916_1ca8_49ac_b176_871d16c13076"); - DO_TEST_START("mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9"); + DO_TEST_CREATE("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); + DO_TEST_CREATE("mdev_fedc4916_1ca8_49ac_b176_871d16c13076"); + DO_TEST_CREATE("mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9"); /* Test mdevctl stop command, pass an arbitrary uuid */ DO_TEST_STOP("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); @@ -468,7 +467,7 @@ mymain(void) DO_TEST_UNDEFINE("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); - DO_TEST_CREATE("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); + DO_TEST_START("mdev_d069d019_36ea_4111_8f0a_8c9a70e21366"); done: nodedevTestDriverFree(driver); -- 2.26.3

rather than using short opentions (e.g. "-p 0000:00:02.0"), use long options everywhere (e.g. "--parent=0000:00:02.0") Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- src/node_device/node_device_driver.c | 28 ++++++------------- ...19_36ea_4111_8f0a_8c9a70e21366-create.argv | 2 +- ...19_36ea_4111_8f0a_8c9a70e21366-define.argv | 2 +- ...39_495e_4243_ad9f_beb3f14c23d9-create.argv | 2 +- ...39_495e_4243_ad9f_beb3f14c23d9-define.argv | 2 +- ...16_1ca8_49ac_b176_871d16c13076-create.argv | 2 +- ...16_1ca8_49ac_b176_871d16c13076-define.argv | 2 +- tests/nodedevmdevctldata/mdevctl-start.argv | 2 +- tests/nodedevmdevctldata/mdevctl-stop.argv | 2 +- .../nodedevmdevctldata/mdevctl-undefine.argv | 2 +- 10 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index 3236dd7bc3..e9147ec0f9 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -720,10 +720,9 @@ nodeDeviceGetMdevctlDefineCreateCommand(virNodeDeviceDef *def, return NULL; } - cmd = virCommandNewArgList(MDEVCTL, subcommand, - "-p", parent_addr, - "--jsonfile", "/dev/stdin", - NULL); + cmd = virCommandNewArgList(MDEVCTL, subcommand, NULL); + virCommandAddArgPair(cmd, "--parent", parent_addr); + virCommandAddArgPair(cmd, "--jsonfile", "/dev/stdin"); virCommandSetInputBuffer(cmd, json); @@ -887,11 +886,8 @@ nodeDeviceCreateXML(virConnectPtr conn, virCommandPtr nodeDeviceGetMdevctlStopCommand(const char *uuid, char **errmsg) { - virCommandPtr cmd = virCommandNewArgList(MDEVCTL, - "stop", - "-u", - uuid, - NULL); + virCommandPtr cmd = virCommandNewArgList(MDEVCTL, "stop", NULL); + virCommandAddArgPair(cmd, "--uuid", uuid); virCommandSetErrorBuffer(cmd, errmsg); return cmd; @@ -900,11 +896,8 @@ nodeDeviceGetMdevctlStopCommand(const char *uuid, char **errmsg) virCommand * nodeDeviceGetMdevctlUndefineCommand(const char *uuid, char **errmsg) { - virCommand *cmd = virCommandNewArgList(MDEVCTL, - "undefine", - "-u", - uuid, - NULL); + virCommand *cmd = virCommandNewArgList(MDEVCTL, "undefine", NULL); + virCommandAddArgPair(cmd, "--uuid", uuid); virCommandSetErrorBuffer(cmd, errmsg); return cmd; } @@ -912,11 +905,8 @@ nodeDeviceGetMdevctlUndefineCommand(const char *uuid, char **errmsg) virCommand* nodeDeviceGetMdevctlStartCommand(const char *uuid, char **errmsg) { - virCommand *cmd = virCommandNewArgList(MDEVCTL, - "start", - "-u", - uuid, - NULL); + virCommand *cmd = virCommandNewArgList(MDEVCTL, "start", NULL); + virCommandAddArgPair(cmd, "--uuid", uuid); virCommandSetErrorBuffer(cmd, errmsg); return cmd; } diff --git a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.argv b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.argv index 129f438e4a..90a12cdd61 100644 --- a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.argv +++ b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-create.argv @@ -1,2 +1,2 @@ -$MDEVCTL_BINARY$ start -p 0000:00:02.0 --jsonfile /dev/stdin \ +$MDEVCTL_BINARY$ start --parent=0000:00:02.0 --jsonfile=/dev/stdin \ --uuid=d069d019-36ea-4111-8f0a-8c9a70e21366 diff --git a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv index 118ec7a8da..2dbde45872 100644 --- a/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv +++ b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-define.argv @@ -1,2 +1,2 @@ -$MDEVCTL_BINARY$ define -p 0000:00:02.0 --jsonfile /dev/stdin \ +$MDEVCTL_BINARY$ define --parent=0000:00:02.0 --jsonfile=/dev/stdin \ --uuid=d069d019-36ea-4111-8f0a-8c9a70e21366 diff --git a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.argv b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.argv index eb7262035e..fc392f0c39 100644 --- a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.argv +++ b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-create.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ start -p 0000:00:02.0 --jsonfile /dev/stdin +$MDEVCTL_BINARY$ start --parent=0000:00:02.0 --jsonfile=/dev/stdin diff --git a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv index 773e98b963..8a40b1037b 100644 --- a/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv +++ b/tests/nodedevmdevctldata/mdev_d2441d39_495e_4243_ad9f_beb3f14c23d9-define.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ define -p 0000:00:02.0 --jsonfile /dev/stdin +$MDEVCTL_BINARY$ define --parent=0000:00:02.0 --jsonfile=/dev/stdin diff --git a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.argv b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.argv index eb7262035e..fc392f0c39 100644 --- a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.argv +++ b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-create.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ start -p 0000:00:02.0 --jsonfile /dev/stdin +$MDEVCTL_BINARY$ start --parent=0000:00:02.0 --jsonfile=/dev/stdin diff --git a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv index 773e98b963..8a40b1037b 100644 --- a/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv +++ b/tests/nodedevmdevctldata/mdev_fedc4916_1ca8_49ac_b176_871d16c13076-define.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ define -p 0000:00:02.0 --jsonfile /dev/stdin +$MDEVCTL_BINARY$ define --parent=0000:00:02.0 --jsonfile=/dev/stdin diff --git a/tests/nodedevmdevctldata/mdevctl-start.argv b/tests/nodedevmdevctldata/mdevctl-start.argv index dc0ae7be0e..dd92de8527 100644 --- a/tests/nodedevmdevctldata/mdevctl-start.argv +++ b/tests/nodedevmdevctldata/mdevctl-start.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ start -u d069d019-36ea-4111-8f0a-8c9a70e21366 +$MDEVCTL_BINARY$ start --uuid=d069d019-36ea-4111-8f0a-8c9a70e21366 diff --git a/tests/nodedevmdevctldata/mdevctl-stop.argv b/tests/nodedevmdevctldata/mdevctl-stop.argv index 5bdb213a32..7549b6ff74 100644 --- a/tests/nodedevmdevctldata/mdevctl-stop.argv +++ b/tests/nodedevmdevctldata/mdevctl-stop.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ stop -u d069d019-36ea-4111-8f0a-8c9a70e21366 +$MDEVCTL_BINARY$ stop --uuid=d069d019-36ea-4111-8f0a-8c9a70e21366 diff --git a/tests/nodedevmdevctldata/mdevctl-undefine.argv b/tests/nodedevmdevctldata/mdevctl-undefine.argv index c2236727ef..8d5217beba 100644 --- a/tests/nodedevmdevctldata/mdevctl-undefine.argv +++ b/tests/nodedevmdevctldata/mdevctl-undefine.argv @@ -1 +1 @@ -$MDEVCTL_BINARY$ undefine -u d069d019-36ea-4111-8f0a-8c9a70e21366 +$MDEVCTL_BINARY$ undefine --uuid=d069d019-36ea-4111-8f0a-8c9a70e21366 -- 2.26.3

From: Erik Skultety <eskultet@redhat.com> This is not a 1:1 mapping to mdevctl because mdevctl doesn't support a 'create' command. It uses 'start' for both starting a defined device as well as creating a transient one. To make our code more readable in that regard and not second-guess what does a specific "start" instance mean, use "create" internally instead only to translate it to "start" just before executing mdevctl. Signed-off-by: Erik Skultety <eskultet@redhat.com> Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- src/node_device/node_device_driver.c | 7 +++++++ src/node_device/node_device_driver.h | 19 +++++++++++++++++++ tests/nodedevmdevctltest.c | 12 ++---------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index e9147ec0f9..c25558cf05 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -47,6 +47,13 @@ virNodeDeviceDriverStatePtr driver; + +VIR_ENUM_IMPL(virMdevctlCommand, + MDEVCTL_CMD_LAST, + "start", "stop", "define", "undefine", "create" +); + + virDrvOpenStatus nodeConnectOpen(virConnectPtr conn, virConnectAuthPtr auth G_GNUC_UNUSED, diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_device_driver.h index 0e3f6dbf28..aaeed588e8 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -33,6 +33,25 @@ int udevNodeRegister(void); #endif + +typedef enum { + MDEVCTL_CMD_START, + MDEVCTL_CMD_STOP, + MDEVCTL_CMD_DEFINE, + MDEVCTL_CMD_UNDEFINE, + + /* mdevctl actually doesn't have a 'create' command, it will be replaced + * with 'start' eventually in nodeDeviceGetMdevctlCommand, but this clear + * separation makes our code more readable in terms of knowing when we're + * starting a defined device and when we're creating a transient one */ + MDEVCTL_CMD_CREATE, + + MDEVCTL_CMD_LAST, +} virMdevctlCommand; + +VIR_ENUM_DECL(virMdevctlCommand); + + void nodeDeviceLock(void); diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index 427299e171..8a37dd2e80 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -10,19 +10,11 @@ #define VIR_FROM_THIS VIR_FROM_NODEDEV -typedef enum { - MDEVCTL_CMD_START, - MDEVCTL_CMD_STOP, - MDEVCTL_CMD_DEFINE, - MDEVCTL_CMD_UNDEFINE, - MDEVCTL_CMD_CREATE, -} MdevctlCmd; - struct startTestInfo { const char *virt_type; int create; const char *filename; - MdevctlCmd command; + virMdevctlCommand command; }; /* capture stdin passed to command */ @@ -141,7 +133,7 @@ testMdevctlCreateOrDefineHelper(const void *data) typedef virCommand* (*GetStopUndefineCmdFunc)(const char *uuid, char **errbuf); struct UuidCommandTestInfo { const char *filename; - MdevctlCmd command; + virMdevctlCommand command; }; static int -- 2.26.3

From: Erik Skultety <eskultet@redhat.com> Currently there are dedicated wrappers to construct mdevctl command. These are mostly fine except for the one that translates both "start" and "define" commands, only because mdevctl takes the same set of arguments. Instead, keep the wrappers, but let them call a single global translator that handles all the mdevctl command differences and commonalities. Signed-off-by: Erik Skultety <eskultet@redhat.com> Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- src/node_device/node_device_driver.c | 125 ++++++++++++++++----------- src/node_device/node_device_driver.h | 6 +- tests/nodedevmdevctltest.c | 4 +- 3 files changed, 79 insertions(+), 56 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index c25558cf05..0fddfdde86 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -702,42 +702,75 @@ nodeDeviceFindAddressByName(const char *name) } -/* 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 virCommand* -nodeDeviceGetMdevctlDefineCreateCommand(virNodeDeviceDef *def, - const char *subcommand, - char **uuid_out, - char **errmsg) +static virCommand * +nodeDeviceGetMdevctlCommand(virNodeDeviceDefPtr def, + virMdevctlCommand cmd_type, + char **outbuf, + char **errbuf) { - virCommandPtr cmd; - g_autofree char *json = NULL; - g_autofree char *parent_addr = nodeDeviceFindAddressByName(def->parent); - - if (!parent_addr) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("unable to find parent device '%s'"), def->parent); - return NULL; + g_autofree char *parent_addr = NULL; + virCommand *cmd = NULL; + const char *subcommand = virMdevctlCommandTypeToString(cmd_type); + g_autofree char *inbuf = NULL; + + switch (cmd_type) { + case MDEVCTL_CMD_CREATE: + /* now is the time to make sure "create" is replaced with "start" on + * mdevctl cmdline */ + cmd = virCommandNewArgList(MDEVCTL, "start", NULL); + break; + case MDEVCTL_CMD_STOP: + case MDEVCTL_CMD_START: + case MDEVCTL_CMD_DEFINE: + case MDEVCTL_CMD_UNDEFINE: + cmd = virCommandNewArgList(MDEVCTL, subcommand, NULL); + break; + case MDEVCTL_CMD_LAST: + default: + /* SHOULD NEVER HAPPEN */ + break; } - if (nodeDeviceDefToMdevctlConfig(def, &json) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("couldn't convert node device def to mdevctl JSON")); - return NULL; - } + switch (cmd_type) { + case MDEVCTL_CMD_CREATE: + case MDEVCTL_CMD_DEFINE: + parent_addr = nodeDeviceFindAddressByName(def->parent); - cmd = virCommandNewArgList(MDEVCTL, subcommand, NULL); - virCommandAddArgPair(cmd, "--parent", parent_addr); - virCommandAddArgPair(cmd, "--jsonfile", "/dev/stdin"); + if (!parent_addr) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unable to find parent device '%s'"), def->parent); + return NULL; + } + + if (nodeDeviceDefToMdevctlConfig(def, &inbuf) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("couldn't convert node device def to mdevctl JSON")); + return NULL; + } - virCommandSetInputBuffer(cmd, json); + virCommandAddArgPair(cmd, "--parent", parent_addr); + virCommandAddArgPair(cmd, "--jsonfile", "/dev/stdin"); + + virCommandSetInputBuffer(cmd, inbuf); + virCommandSetOutputBuffer(cmd, outbuf); + break; + + case MDEVCTL_CMD_UNDEFINE: + case MDEVCTL_CMD_STOP: + case MDEVCTL_CMD_START: + /* No special handling here, we only need to pass UUID with these */ + break; + case MDEVCTL_CMD_LAST: + default: + /* SHOULD NEVER HAPPEN */ + break; + } + /* Fill in UUID for commands that need it */ if (def->caps->data.mdev.uuid) virCommandAddArgPair(cmd, "--uuid", def->caps->data.mdev.uuid); - virCommandSetOutputBuffer(cmd, uuid_out); - virCommandSetErrorBuffer(cmd, errmsg); + virCommandSetErrorBuffer(cmd, errbuf); return cmd; } @@ -747,8 +780,7 @@ nodeDeviceGetMdevctlCreateCommand(virNodeDeviceDef *def, char **uuid_out, char **errmsg) { - return nodeDeviceGetMdevctlDefineCreateCommand(def, "start", uuid_out, - errmsg); + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_CREATE, uuid_out, errmsg); } virCommand* @@ -756,8 +788,7 @@ nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, char **uuid_out, char **errmsg) { - return nodeDeviceGetMdevctlDefineCreateCommand(def, "define", uuid_out, - errmsg); + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_DEFINE, uuid_out, errmsg); } @@ -891,31 +922,24 @@ nodeDeviceCreateXML(virConnectPtr conn, virCommandPtr -nodeDeviceGetMdevctlStopCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlStopCommand(virNodeDeviceDefPtr def, + char **errmsg) { - virCommandPtr cmd = virCommandNewArgList(MDEVCTL, "stop", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; - + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_STOP, NULL, errmsg); } virCommand * -nodeDeviceGetMdevctlUndefineCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDefPtr def, + char **errmsg) { - virCommand *cmd = virCommandNewArgList(MDEVCTL, "undefine", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_UNDEFINE, NULL, errmsg); } virCommand* -nodeDeviceGetMdevctlStartCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlStartCommand(virNodeDeviceDef *def, + char **errmsg) { - virCommand *cmd = virCommandNewArgList(MDEVCTL, "start", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_START, NULL, errmsg); } static int @@ -924,7 +948,7 @@ virMdevctlStop(virNodeDeviceDefPtr def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlStopCommand(def->caps->data.mdev.uuid, errmsg); + cmd = nodeDeviceGetMdevctlStopCommand(def, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; @@ -939,8 +963,7 @@ virMdevctlUndefine(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlUndefineCommand(def->caps->data.mdev.uuid, - errmsg); + cmd = nodeDeviceGetMdevctlUndefineCommand(def, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; @@ -955,7 +978,7 @@ virMdevctlStart(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlStartCommand(def->caps->data.mdev.uuid, errmsg); + cmd = nodeDeviceGetMdevctlStartCommand(def, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_device_driver.h index aaeed588e8..0fea75a118 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -152,11 +152,11 @@ nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, char **errmsg); virCommandPtr -nodeDeviceGetMdevctlStopCommand(const char *uuid, +nodeDeviceGetMdevctlStopCommand(virNodeDeviceDefPtr def, char **errmsg); virCommand * -nodeDeviceGetMdevctlUndefineCommand(const char *uuid, +nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDefPtr def, char **errmsg); virCommandPtr @@ -181,7 +181,7 @@ bool nodeDeviceDefCopyFromMdevctl(virNodeDeviceDef *dst, virNodeDeviceDef *src); virCommand* -nodeDeviceGetMdevctlStartCommand(const char *uuid, +nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def, char **errmsg); int diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index 8a37dd2e80..e766ae8f34 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -130,7 +130,7 @@ testMdevctlCreateOrDefineHelper(const void *data) mdevxml, cmdlinefile, jsonfile); } -typedef virCommand* (*GetStopUndefineCmdFunc)(const char *uuid, char **errbuf); +typedef virCommand* (*GetStopUndefineCmdFunc)(virNodeDeviceDef *def, char **errbuf); struct UuidCommandTestInfo { const char *filename; virMdevctlCommand command; @@ -150,7 +150,7 @@ testMdevctlUuidCommand(GetStopUndefineCmdFunc func, if (!(def = virNodeDeviceDefParseFile(mdevxml, EXISTING_DEVICE, "QEMU"))) goto cleanup; - cmd = func(def->caps->data.mdev.uuid, &errmsg); + cmd = func(def, &errmsg); if (!cmd) goto cleanup; -- 2.26.3

These per-command generator functions were only exposed in the header to allow the commandline generation to be tested. Now that we have a generic mdevctl command generator, we can get rid of the per-command wrappers and reduce the noise in the header. Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- src/node_device/node_device_driver.c | 56 ++++++---------------------- src/node_device/node_device_driver.h | 24 ++---------- tests/nodedevmdevctltest.c | 41 ++++---------------- 3 files changed, 23 insertions(+), 98 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index 0fddfdde86..76019b8631 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -702,7 +702,7 @@ nodeDeviceFindAddressByName(const char *name) } -static virCommand * +virCommand * nodeDeviceGetMdevctlCommand(virNodeDeviceDefPtr def, virMdevctlCommand cmd_type, char **outbuf, @@ -775,30 +775,15 @@ nodeDeviceGetMdevctlCommand(virNodeDeviceDefPtr def, return cmd; } -virCommand* -nodeDeviceGetMdevctlCreateCommand(virNodeDeviceDef *def, - char **uuid_out, - char **errmsg) -{ - return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_CREATE, uuid_out, errmsg); -} - -virCommand* -nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, - char **uuid_out, - char **errmsg) -{ - return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_DEFINE, uuid_out, errmsg); -} - - static int virMdevctlCreate(virNodeDeviceDefPtr def, char **uuid, char **errmsg) { int status; - g_autoptr(virCommand) cmd = nodeDeviceGetMdevctlCreateCommand(def, uuid, - errmsg); + g_autoptr(virCommand) cmd = nodeDeviceGetMdevctlCommand(def, + MDEVCTL_CMD_CREATE, + uuid, + errmsg); if (!cmd) return -1; @@ -818,7 +803,9 @@ static int virMdevctlDefine(virNodeDeviceDefPtr def, char **uuid, char **errmsg) { int status; - g_autoptr(virCommand) cmd = nodeDeviceGetMdevctlDefineCommand(def, uuid, errmsg); + g_autoptr(virCommand) cmd = nodeDeviceGetMdevctlCommand(def, + MDEVCTL_CMD_DEFINE, + uuid, errmsg); if (!cmd) return -1; @@ -921,34 +908,13 @@ nodeDeviceCreateXML(virConnectPtr conn, } -virCommandPtr -nodeDeviceGetMdevctlStopCommand(virNodeDeviceDefPtr def, - char **errmsg) -{ - return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_STOP, NULL, errmsg); -} - -virCommand * -nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDefPtr def, - char **errmsg) -{ - return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_UNDEFINE, NULL, errmsg); -} - -virCommand* -nodeDeviceGetMdevctlStartCommand(virNodeDeviceDef *def, - char **errmsg) -{ - return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_START, NULL, errmsg); -} - static int virMdevctlStop(virNodeDeviceDefPtr def, char **errmsg) { int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlStopCommand(def, errmsg); + cmd = nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_STOP, NULL, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; @@ -963,7 +929,7 @@ virMdevctlUndefine(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlUndefineCommand(def, errmsg); + cmd = nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_UNDEFINE, NULL, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; @@ -978,7 +944,7 @@ virMdevctlStart(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlStartCommand(def, errmsg); + cmd = nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_START, NULL, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_device_driver.h index 0fea75a118..482a5cfb3e 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -141,23 +141,11 @@ int nodeConnectNodeDeviceEventDeregisterAny(virConnectPtr conn, int callbackID); -virCommandPtr -nodeDeviceGetMdevctlCreateCommand(virNodeDeviceDefPtr def, - char **uuid_out, - char **errmsg); - -virCommand* -nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, - char **uuid_out, - char **errmsg); - -virCommandPtr -nodeDeviceGetMdevctlStopCommand(virNodeDeviceDefPtr def, - char **errmsg); - virCommand * -nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDefPtr def, - char **errmsg); +nodeDeviceGetMdevctlCommand(virNodeDeviceDefPtr def, + virMdevctlCommand cmd_type, + char **outbuf, + char **errbuf); virCommandPtr nodeDeviceGetMdevctlListCommand(bool defined, @@ -180,10 +168,6 @@ nodeDeviceGenerateName(virNodeDeviceDef *def, bool nodeDeviceDefCopyFromMdevctl(virNodeDeviceDef *dst, virNodeDeviceDef *src); -virCommand* -nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def, - char **errmsg); - int nodeDeviceCreate(virNodeDevice *dev, unsigned int flags); diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index e766ae8f34..c467c798e7 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -53,7 +53,7 @@ typedef virCommand* (*MdevctlCmdFunc)(virNodeDeviceDef *, char **, char **); static int testMdevctlCreateOrDefine(const char *virt_type, int create, - MdevctlCmdFunc mdevctl_cmd_func, + virMdevctlCommand cmd_type, const char *mdevxml, const char *cmdfile, const char *jsonfile) @@ -73,7 +73,7 @@ testMdevctlCreateOrDefine(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 = mdevctl_cmd_func(def, &uuid, &errmsg); + cmd = nodeDeviceGetMdevctlCommand(def, cmd_type, &uuid, &errmsg); if (!cmd) goto cleanup; @@ -103,22 +103,11 @@ static int testMdevctlCreateOrDefineHelper(const void *data) { const struct startTestInfo *info = data; - const char *cmd; - MdevctlCmdFunc func; + const char *cmd = virMdevctlCommandTypeToString(info->command); g_autofree char *mdevxml = NULL; g_autofree char *cmdlinefile = NULL; g_autofree char *jsonfile = NULL; - if (info->command == MDEVCTL_CMD_CREATE) { - cmd = "create"; - func = nodeDeviceGetMdevctlCreateCommand; - } else if (info->command == MDEVCTL_CMD_DEFINE) { - cmd = "define"; - func = nodeDeviceGetMdevctlDefineCommand; - } else { - return -1; - } - mdevxml = g_strdup_printf("%s/nodedevschemadata/%s.xml", abs_srcdir, info->filename); cmdlinefile = g_strdup_printf("%s/nodedevmdevctldata/%s-%s.argv", @@ -126,7 +115,7 @@ testMdevctlCreateOrDefineHelper(const void *data) jsonfile = g_strdup_printf("%s/nodedevmdevctldata/%s-%s.json", abs_srcdir, info->filename, cmd); - return testMdevctlCreateOrDefine(info->virt_type, info->create, func, + return testMdevctlCreateOrDefine(info->virt_type, info->create, info->command, mdevxml, cmdlinefile, jsonfile); } @@ -137,7 +126,7 @@ struct UuidCommandTestInfo { }; static int -testMdevctlUuidCommand(GetStopUndefineCmdFunc func, +testMdevctlUuidCommand(virMdevctlCommand command, const char *mdevxml, const char *outfile) { g_autoptr(virNodeDeviceDef) def = NULL; @@ -150,7 +139,7 @@ testMdevctlUuidCommand(GetStopUndefineCmdFunc func, if (!(def = virNodeDeviceDefParseFile(mdevxml, EXISTING_DEVICE, "QEMU"))) goto cleanup; - cmd = func(def, &errmsg); + cmd = nodeDeviceGetMdevctlCommand(def, command, NULL, &errmsg); if (!cmd) goto cleanup; @@ -176,30 +165,16 @@ static int testMdevctlUuidCommandHelper(const void *data) { const struct UuidCommandTestInfo *info = data; - GetStopUndefineCmdFunc func; - const char *cmd; + const char *cmd = virMdevctlCommandTypeToString(info->command); g_autofree char *cmdlinefile = NULL; g_autofree char *mdevxml = NULL; - if (info->command == MDEVCTL_CMD_STOP) { - cmd = "stop"; - func = nodeDeviceGetMdevctlStopCommand; - } else if (info->command == MDEVCTL_CMD_UNDEFINE) { - cmd = "undefine"; - func = nodeDeviceGetMdevctlUndefineCommand; - }else if (info->command == MDEVCTL_CMD_START) { - cmd = "start"; - func = nodeDeviceGetMdevctlStartCommand; - } else { - return -1; - } - mdevxml = g_strdup_printf("%s/nodedevschemadata/%s.xml", abs_srcdir, info->filename); cmdlinefile = g_strdup_printf("%s/nodedevmdevctldata/mdevctl-%s.argv", abs_srcdir, cmd); - return testMdevctlUuidCommand(func, mdevxml, cmdlinefile); + return testMdevctlUuidCommand(info->command, mdevxml, cmdlinefile); } static int -- 2.26.3

From: Erik Skultety <eskultet@redhat.com> Now that we have a generic mdevctl command generator, we can unify the test infrastructure as well. Signed-off-by: Erik Skultety <eskultet@redhat.com> --- ...19_36ea_4111_8f0a_8c9a70e21366-start.argv} | 0 ...019_36ea_4111_8f0a_8c9a70e21366-stop.argv} | 0 ...36ea_4111_8f0a_8c9a70e21366-undefine.argv} | 0 tests/nodedevmdevctltest.c | 119 +++++------------- 4 files changed, 30 insertions(+), 89 deletions(-) rename tests/nodedevmdevctldata/{mdevctl-start.argv => mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.argv} (100%) rename tests/nodedevmdevctldata/{mdevctl-stop.argv => mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-stop.argv} (100%) rename tests/nodedevmdevctldata/{mdevctl-undefine.argv => mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-undefine.argv} (100%) diff --git a/tests/nodedevmdevctldata/mdevctl-start.argv b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.argv similarity index 100% rename from tests/nodedevmdevctldata/mdevctl-start.argv rename to tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-start.argv diff --git a/tests/nodedevmdevctldata/mdevctl-stop.argv b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-stop.argv similarity index 100% rename from tests/nodedevmdevctldata/mdevctl-stop.argv rename to tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-stop.argv diff --git a/tests/nodedevmdevctldata/mdevctl-undefine.argv b/tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-undefine.argv similarity index 100% rename from tests/nodedevmdevctldata/mdevctl-undefine.argv rename to tests/nodedevmdevctldata/mdev_d069d019_36ea_4111_8f0a_8c9a70e21366-undefine.argv diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index c467c798e7..7e192ce118 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -10,7 +10,7 @@ #define VIR_FROM_THIS VIR_FROM_NODEDEV -struct startTestInfo { +struct TestInfo { const char *virt_type; int create; const char *filename; @@ -47,24 +47,24 @@ nodedevCompareToFile(const char *actual, } -typedef virCommand* (*MdevctlCmdFunc)(virNodeDeviceDef *, char **, char **); +typedef virCommand * (*MdevctlCmdFunc)(virNodeDeviceDef *, char **, char **); static int -testMdevctlCreateOrDefine(const char *virt_type, - int create, - virMdevctlCommand cmd_type, - const char *mdevxml, - const char *cmdfile, - const char *jsonfile) +testMdevctlCmd(const char *virt_type, + int create, + virMdevctlCommand cmd_type, + const char *mdevxml, + const char *cmdfile, + const char *jsonfile) { g_autoptr(virNodeDeviceDef) def = NULL; virNodeDeviceObjPtr obj = NULL; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; const char *actualCmdline = NULL; int ret = -1; - g_autofree char *uuid = NULL; - g_autofree char *errmsg = NULL; + g_autofree char *outbuf = NULL; + g_autofree char *errbuf = NULL; g_autofree char *stdinbuf = NULL; g_autoptr(virCommand) cmd = NULL; @@ -73,12 +73,16 @@ testMdevctlCreateOrDefine(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 = nodeDeviceGetMdevctlCommand(def, cmd_type, &uuid, &errmsg); + cmd = nodeDeviceGetMdevctlCommand(def, cmd_type, &outbuf, &errbuf); if (!cmd) goto cleanup; - virCommandSetDryRun(&buf, testCommandDryRunCallback, &stdinbuf); + if (create) + virCommandSetDryRun(&buf, testCommandDryRunCallback, &stdinbuf); + else + virCommandSetDryRun(&buf, NULL, NULL); + if (virCommandRun(cmd, NULL) < 0) goto cleanup; @@ -88,7 +92,7 @@ testMdevctlCreateOrDefine(const char *virt_type, if (nodedevCompareToFile(actualCmdline, cmdfile) < 0) goto cleanup; - if (virTestCompareToFile(stdinbuf, jsonfile) < 0) + if (create && virTestCompareToFile(stdinbuf, jsonfile) < 0) goto cleanup; ret = 0; @@ -99,10 +103,11 @@ testMdevctlCreateOrDefine(const char *virt_type, return ret; } + static int -testMdevctlCreateOrDefineHelper(const void *data) +testMdevctlHelper(const void *data) { - const struct startTestInfo *info = data; + const struct TestInfo *info = data; const char *cmd = virMdevctlCommandTypeToString(info->command); g_autofree char *mdevxml = NULL; g_autofree char *cmdlinefile = NULL; @@ -115,67 +120,10 @@ testMdevctlCreateOrDefineHelper(const void *data) jsonfile = g_strdup_printf("%s/nodedevmdevctldata/%s-%s.json", abs_srcdir, info->filename, cmd); - return testMdevctlCreateOrDefine(info->virt_type, info->create, info->command, - mdevxml, cmdlinefile, jsonfile); -} - -typedef virCommand* (*GetStopUndefineCmdFunc)(virNodeDeviceDef *def, char **errbuf); -struct UuidCommandTestInfo { - const char *filename; - virMdevctlCommand command; -}; - -static int -testMdevctlUuidCommand(virMdevctlCommand command, - const char *mdevxml, const char *outfile) -{ - g_autoptr(virNodeDeviceDef) def = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; - const char *actualCmdline = NULL; - int ret = -1; - g_autoptr(virCommand) cmd = NULL; - g_autofree char *errmsg = NULL; - - if (!(def = virNodeDeviceDefParseFile(mdevxml, EXISTING_DEVICE, "QEMU"))) - goto cleanup; - - cmd = nodeDeviceGetMdevctlCommand(def, command, NULL, &errmsg); - - if (!cmd) - goto cleanup; - - virCommandSetDryRun(&buf, NULL, NULL); - if (virCommandRun(cmd, NULL) < 0) - goto cleanup; - - if (!(actualCmdline = virBufferCurrentContent(&buf))) - goto cleanup; - - if (nodedevCompareToFile(actualCmdline, outfile) < 0) - goto cleanup; - - ret = 0; - - cleanup: - virCommandSetDryRun(NULL, NULL, NULL); - return ret; + return testMdevctlCmd(info->virt_type, info->create, info->command, + mdevxml, cmdlinefile, jsonfile); } -static int -testMdevctlUuidCommandHelper(const void *data) -{ - const struct UuidCommandTestInfo *info = data; - const char *cmd = virMdevctlCommandTypeToString(info->command); - g_autofree char *cmdlinefile = NULL; - g_autofree char *mdevxml = NULL; - - mdevxml = g_strdup_printf("%s/nodedevschemadata/%s.xml", abs_srcdir, - info->filename); - cmdlinefile = g_strdup_printf("%s/nodedevmdevctldata/mdevctl-%s.argv", - abs_srcdir, cmd); - - return testMdevctlUuidCommand(info->command, mdevxml, cmdlinefile); -} static int testMdevctlListDefined(const void *data G_GNUC_UNUSED) @@ -384,35 +332,28 @@ mymain(void) #define DO_TEST_CMD(desc, virt_type, create, filename, command) \ do { \ - struct startTestInfo info = { virt_type, create, filename, command }; \ - DO_TEST_FULL(desc, testMdevctlCreateOrDefineHelper, &info); \ + struct TestInfo info = { virt_type, create, filename, command }; \ + DO_TEST_FULL(desc, testMdevctlHelper, &info); \ } \ while (0) #define DO_TEST_CREATE(filename) \ - DO_TEST_CMD("mdevctl create " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_CREATE) + DO_TEST_CMD("create mdev " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_CREATE) #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) + DO_TEST_CMD("define mdev " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_DEFINE) #define DO_TEST_STOP(filename) \ - DO_TEST_UUID_COMMAND_FULL("mdevctl stop " filename, filename, MDEVCTL_CMD_STOP) + DO_TEST_CMD("stop mdev " filename, "QEMU", EXISTING_DEVICE, filename, MDEVCTL_CMD_STOP) #define DO_TEST_UNDEFINE(filename) \ - DO_TEST_UUID_COMMAND_FULL("mdevctl undefine " filename, filename, MDEVCTL_CMD_UNDEFINE) + DO_TEST_CMD("undefine mdev" filename, "QEMU", EXISTING_DEVICE, filename, MDEVCTL_CMD_UNDEFINE) #define DO_TEST_START(filename) \ - DO_TEST_UUID_COMMAND_FULL("mdevctl start " filename, filename, MDEVCTL_CMD_START) + DO_TEST_CMD("start mdev " filename, "QEMU", EXISTING_DEVICE, filename, MDEVCTL_CMD_START) #define DO_TEST_LIST_DEFINED() \ - DO_TEST_FULL("mdevctl list --defined", testMdevctlListDefined, NULL) + DO_TEST_FULL("list defined mdevs", testMdevctlListDefined, NULL) #define DO_TEST_PARSE_JSON(filename) \ DO_TEST_FULL("parse mdevctl json " filename, testMdevctlParse, filename) -- 2.26.3

We only use the virt_type "QEMU" in this tests, so simply hard-code it in the test function rather than specifying it in the test macro. In addition, we can figure out the appropriate value for 'create' from the command type, so push that into the test function rather than specifying it in the test macro. Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- tests/nodedevmdevctltest.c | 43 +++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index 7e192ce118..3a53254195 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -10,9 +10,9 @@ #define VIR_FROM_THIS VIR_FROM_NODEDEV +#define VIRT_TYPE "QEMU" + struct TestInfo { - const char *virt_type; - int create; const char *filename; virMdevctlCommand command; }; @@ -51,9 +51,7 @@ typedef virCommand * (*MdevctlCmdFunc)(virNodeDeviceDef *, char **, char **); static int -testMdevctlCmd(const char *virt_type, - int create, - virMdevctlCommand cmd_type, +testMdevctlCmd(virMdevctlCommand cmd_type, const char *mdevxml, const char *cmdfile, const char *jsonfile) @@ -67,8 +65,24 @@ testMdevctlCmd(const char *virt_type, g_autofree char *errbuf = NULL; g_autofree char *stdinbuf = NULL; g_autoptr(virCommand) cmd = NULL; + int create; + + switch (cmd_type) { + case MDEVCTL_CMD_CREATE: + case MDEVCTL_CMD_DEFINE: + create = CREATE_DEVICE; + break; + case MDEVCTL_CMD_START: + case MDEVCTL_CMD_STOP: + case MDEVCTL_CMD_UNDEFINE: + create = EXISTING_DEVICE; + break; + case MDEVCTL_CMD_LAST: + default: + goto cleanup; + } - if (!(def = virNodeDeviceDefParseFile(mdevxml, create, virt_type))) + if (!(def = virNodeDeviceDefParseFile(mdevxml, create, VIRT_TYPE))) goto cleanup; /* this function will set a stdin buffer containing the json configuration @@ -120,8 +134,7 @@ testMdevctlHelper(const void *data) jsonfile = g_strdup_printf("%s/nodedevmdevctldata/%s-%s.json", abs_srcdir, info->filename, cmd); - return testMdevctlCmd(info->virt_type, info->create, info->command, - mdevxml, cmdlinefile, jsonfile); + return testMdevctlCmd(info->command, mdevxml, cmdlinefile, jsonfile); } @@ -330,27 +343,27 @@ mymain(void) if (virTestRun(desc, func, info) < 0) \ ret = -1; -#define DO_TEST_CMD(desc, virt_type, create, filename, command) \ +#define DO_TEST_CMD(desc, filename, command) \ do { \ - struct TestInfo info = { virt_type, create, filename, command }; \ + struct TestInfo info = { filename, command }; \ DO_TEST_FULL(desc, testMdevctlHelper, &info); \ } \ while (0) #define DO_TEST_CREATE(filename) \ - DO_TEST_CMD("create mdev " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_CREATE) + DO_TEST_CMD("create mdev " filename, filename, MDEVCTL_CMD_CREATE) #define DO_TEST_DEFINE(filename) \ - DO_TEST_CMD("define mdev " filename, "QEMU", CREATE_DEVICE, filename, MDEVCTL_CMD_DEFINE) + DO_TEST_CMD("define mdev " filename, filename, MDEVCTL_CMD_DEFINE) #define DO_TEST_STOP(filename) \ - DO_TEST_CMD("stop mdev " filename, "QEMU", EXISTING_DEVICE, filename, MDEVCTL_CMD_STOP) + DO_TEST_CMD("stop mdev " filename, filename, MDEVCTL_CMD_STOP) #define DO_TEST_UNDEFINE(filename) \ - DO_TEST_CMD("undefine mdev" filename, "QEMU", EXISTING_DEVICE, filename, MDEVCTL_CMD_UNDEFINE) + DO_TEST_CMD("undefine mdev" filename, filename, MDEVCTL_CMD_UNDEFINE) #define DO_TEST_START(filename) \ - DO_TEST_CMD("start mdev " filename, "QEMU", EXISTING_DEVICE, filename, MDEVCTL_CMD_START) + DO_TEST_CMD("start mdev " filename, filename, MDEVCTL_CMD_START) #define DO_TEST_LIST_DEFINED() \ DO_TEST_FULL("list defined mdevs", testMdevctlListDefined, NULL) -- 2.26.3

This variable was leftover from previous changes but is no longer used. Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> --- tests/nodedevmdevctltest.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index 3a53254195..b2507e1567 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -57,7 +57,6 @@ testMdevctlCmd(virMdevctlCommand cmd_type, const char *jsonfile) { g_autoptr(virNodeDeviceDef) def = NULL; - virNodeDeviceObjPtr obj = NULL; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; const char *actualCmdline = NULL; int ret = -1; @@ -113,7 +112,6 @@ testMdevctlCmd(virMdevctlCommand cmd_type, cleanup: virCommandSetDryRun(NULL, NULL, NULL); - virNodeDeviceObjEndAPI(&obj); return ret; } -- 2.26.3

On Fri, Apr 09, 2021 at 04:32:55PM -0500, Jonathon Jongsma wrote:
Erik recommended several changes to the mdev series that was just merged and suggested to address them in a follow-up patch series. They're mostly related to simplifying the testing, and shouldn't actually change any behavior (aside from switching to using the long commandline options when executing mdevctl).
Erik Skultety (4): nodedev: driver: Swap virMdevctlStart and virMdevctlCreate nodedev: driver: Introduce internal mdevctl commands enum nodedev: driver: Create a generic mdevctl command translator tests: nodedev: Make the mdevctl test function and helper generic
Jonathon Jongsma (7): nodedev: don't log error in nodeDeviceFindAddressByName() nodedev: avoid use of VIR_ERR_NO_* errors internally tests: nodedev: switch all test macros to accept a filename nodedev: Switch to using long options for mdevctl nodedev: Remove GetMdevctl*Command() wrappers tests: nodedev: simplify test macros tests: nodedev: remove unused variable
This fails to apply: Applying: tests: nodedev: Make the mdevctl test function and helper generic error: sha1 information is lacking or useless (tests/nodedevmdevctltest.c). error: could not build fake ancestor Patch failed at 0009 tests: nodedev: Make the mdevctl test function and helper generic There were a bunch of other merge conflicts even before this patch, so please respin a rebased version. Regards Erik
participants (2)
-
Erik Skultety
-
Jonathon Jongsma