[PATCH 0/5] tests: Tool for programatic modification of qemucapabilitiesdata/*.replies

I was asked multiple times how to automate modification of the qemu capability dumps. I wrote a tool [1] that allowed doing that. Unfortunately as it wasn't in the repo it was not avalilable. Now I've finally set out to finish it. It masquerades as a test in default mode checking the ordering and numbering of the caps dump files as in the past there were few cases where manual edits broke it. In update mode it allows modification of the replies and gives few examples to do so. There's a lot of possible things users would want to do so there's no way around actually writing some code but it should be easier this way. Since this now does also numbering, the AWK/shell script 'qemucapsfixreplies' can be removed. [1]: Okay, okay. Copy&paste'd few bits from various places into one messy C file and didn't bother with memory cleanup. But it worked. Peter Krempa (5): util: json: Introduce virJSONValueObjectReplaceValue tests: qemumonitortestutils.h: Reformat header file qemumonitortestutils: Extract parser for the monitor conversation dump file tests: Tool for programatic modification of qemucapabilitiesdata/*.replies tests: Remove 'qemucapsfixreplies' src/libvirt_private.syms | 1 + src/util/virjson.c | 20 +++ src/util/virjson.h | 6 + tests/domaincapstest.c | 3 +- tests/meson.build | 1 + tests/qemucapabilitiesnumbering.c | 242 ++++++++++++++++++++++++++++++ tests/qemucapabilitiestest.c | 4 +- tests/qemucapsfixreplies | 26 ---- tests/qemumonitortestutils.c | 123 ++++++++++----- tests/qemumonitortestutils.h | 149 +++++++++++------- 10 files changed, 453 insertions(+), 122 deletions(-) create mode 100644 tests/qemucapabilitiesnumbering.c delete mode 100755 tests/qemucapsfixreplies -- 2.31.1

The new helper replaces the 'value' part of the key-value tuple in an object. The advantage of this new helper is that it preserves the ordering of the key in the object when compared to a combination of stealing the old key and adding a new value. This will be needed for a new test/helper for validating and modifying qemu capabilities data. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virjson.c | 20 ++++++++++++++++++++ src/util/virjson.h | 6 ++++++ 3 files changed, 27 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 435ee8054c..431075899d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2567,6 +2567,7 @@ virJSONValueObjectHasKey; virJSONValueObjectKeysNumber; virJSONValueObjectPrependString; virJSONValueObjectRemoveKey; +virJSONValueObjectReplaceValue; virJSONValueObjectStealArray; virJSONValueObjectStealObject; virJSONValueToBuffer; diff --git a/src/util/virjson.c b/src/util/virjson.c index 1c6fef22da..6e13e97e15 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1149,6 +1149,26 @@ virJSONValueObjectGetString(virJSONValue *object, } +void +virJSONValueObjectReplaceValue(virJSONValue *object, + const char *key, + virJSONValue **newval) +{ + size_t i; + + if (object->type != VIR_JSON_TYPE_OBJECT || + !*newval) + return; + + for (i = 0; i < object->data.object.npairs; i++) { + if (STREQ(object->data.object.pairs[i].key, key)) { + virJSONValueFree(object->data.object.pairs[i].value); + object->data.object.pairs[i].value = g_steal_pointer(newval); + } + } +} + + /** * virJSONValueObjectGetStringOrNumber: * @object: JSON value object diff --git a/src/util/virjson.h b/src/util/virjson.h index f0b8c419de..aced48a538 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -248,6 +248,12 @@ virJSONValueObjectRemoveKey(virJSONValue *object, virJSONValue **value) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); +void +virJSONValueObjectReplaceValue(virJSONValue *object, + const char *key, + virJSONValue **newval) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); + int virJSONValueArrayAppendString(virJSONValue *object, const char *value); -- 2.31.1

Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/qemumonitortestutils.h | 140 ++++++++++++++++++++--------------- 1 file changed, 81 insertions(+), 59 deletions(-) diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h index e572627e20..89c33c434b 100644 --- a/tests/qemumonitortestutils.h +++ b/tests/qemumonitortestutils.h @@ -31,76 +31,98 @@ typedef int (*qemuMonitorTestResponseCallback)(qemuMonitorTest *test, qemuMonitorTestItem *item, const char *message); -int qemuMonitorTestAddHandler(qemuMonitorTest *test, - const char *identifier, - qemuMonitorTestResponseCallback cb, - void *opaque, - virFreeCallback freecb); - -int qemuMonitorTestAddResponse(qemuMonitorTest *test, - const char *response); - -int qemuMonitorTestAddInvalidCommandResponse(qemuMonitorTest *test, - const char *expectedcommand, - const char *actualcommand); - -void *qemuMonitorTestItemGetPrivateData(qemuMonitorTestItem *item); - -int qemuMonitorTestAddErrorResponse(qemuMonitorTest *test, const char *errmsg, ...); - -void qemuMonitorTestAllowUnusedCommands(qemuMonitorTest *test); -void qemuMonitorTestSkipDeprecatedValidation(qemuMonitorTest *test, - bool allowRemoved); - -int qemuMonitorTestAddItem(qemuMonitorTest *test, - const char *command_name, +int +qemuMonitorTestAddHandler(qemuMonitorTest *test, + const char *identifier, + qemuMonitorTestResponseCallback cb, + void *opaque, + virFreeCallback freecb); + +int +qemuMonitorTestAddResponse(qemuMonitorTest *test, const char *response); -int qemuMonitorTestAddItemVerbatim(qemuMonitorTest *test, - const char *command, - const char *cmderr, - const char *response); +int +qemuMonitorTestAddInvalidCommandResponse(qemuMonitorTest *test, + const char *expectedcommand, + const char *actualcommand); + +void * +qemuMonitorTestItemGetPrivateData(qemuMonitorTestItem *item); + +int +qemuMonitorTestAddErrorResponse(qemuMonitorTest *test, + const char *errmsg, + ...); + +void +qemuMonitorTestAllowUnusedCommands(qemuMonitorTest *test); +void +qemuMonitorTestSkipDeprecatedValidation(qemuMonitorTest *test, + bool allowRemoved); + +int +qemuMonitorTestAddItem(qemuMonitorTest *test, + const char *command_name, + const char *response); + +int +qemuMonitorTestAddItemVerbatim(qemuMonitorTest *test, + const char *command, + const char *cmderr, + const char *response); -int qemuMonitorTestAddAgentSyncResponse(qemuMonitorTest *test); +int +qemuMonitorTestAddAgentSyncResponse(qemuMonitorTest *test); -int qemuMonitorTestAddItemParams(qemuMonitorTest *test, - const char *cmdname, - const char *response, - ...) +int +qemuMonitorTestAddItemParams(qemuMonitorTest *test, + const char *cmdname, + const char *response, + ...) G_GNUC_NULL_TERMINATED; -int qemuMonitorTestAddItemExpect(qemuMonitorTest *test, - const char *cmdname, - const char *cmdargs, - bool apostrophe, - const char *response); +int +qemuMonitorTestAddItemExpect(qemuMonitorTest *test, + const char *cmdname, + const char *cmdargs, + bool apostrophe, + const char *response); #define qemuMonitorTestNewSimple(xmlopt) \ qemuMonitorTestNew(xmlopt, NULL, NULL, NULL, NULL) #define qemuMonitorTestNewSchema(xmlopt, schema) \ qemuMonitorTestNew(xmlopt, NULL, NULL, NULL, schema) -qemuMonitorTest *qemuMonitorTestNew(virDomainXMLOption *xmlopt, - virDomainObj *vm, - virQEMUDriver *driver, - const char *greeting, - GHashTable *schema); - -qemuMonitorTest *qemuMonitorTestNewFromFile(const char *fileName, - virDomainXMLOption *xmlopt, - bool simple); -qemuMonitorTest *qemuMonitorTestNewFromFileFull(const char *fileName, - virQEMUDriver *driver, - virDomainObj *vm, - GHashTable *qmpschema); - -qemuMonitorTest *qemuMonitorTestNewAgent(virDomainXMLOption *xmlopt); - - -void qemuMonitorTestFree(qemuMonitorTest *test); - -qemuMonitor *qemuMonitorTestGetMonitor(qemuMonitorTest *test); -qemuAgent *qemuMonitorTestGetAgent(qemuMonitorTest *test); -virDomainObj *qemuMonitorTestGetDomainObj(qemuMonitorTest *test); +qemuMonitorTest * +qemuMonitorTestNew(virDomainXMLOption *xmlopt, + virDomainObj *vm, + virQEMUDriver *driver, + const char *greeting, + GHashTable *schema); + +qemuMonitorTest * +qemuMonitorTestNewFromFile(const char *fileName, + virDomainXMLOption *xmlopt, + bool simple); +qemuMonitorTest * +qemuMonitorTestNewFromFileFull(const char *fileName, + virQEMUDriver *driver, + virDomainObj *vm, + GHashTable *qmpschema); + +qemuMonitorTest * +qemuMonitorTestNewAgent(virDomainXMLOption *xmlopt); + + +void +qemuMonitorTestFree(qemuMonitorTest *test); + +qemuMonitor * +qemuMonitorTestGetMonitor(qemuMonitorTest *test); +qemuAgent * +qemuMonitorTestGetAgent(qemuMonitorTest *test); +virDomainObj * +qemuMonitorTestGetDomainObj(qemuMonitorTest *test); G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuMonitorTest, qemuMonitorTestFree); -- 2.31.1

Make the parser reusable by extracting it and making it parse into command,reply tuples. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/qemumonitortestutils.c | 123 ++++++++++++++++++++++++----------- tests/qemumonitortestutils.h | 13 ++++ 2 files changed, 99 insertions(+), 37 deletions(-) diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 0d99b45909..f7a0a37685 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -1278,45 +1278,33 @@ qemuMonitorTestFullAddItem(qemuMonitorTest *test, /** - * qemuMonitorTestNewFromFileFull: - * @fileName: File name to load monitor replies from - * @driver: qemu driver object - * @vm: domain object (may be null if it's not needed by the test) - * @qmpschema: QMP schema data hash table if QMP checking is required + * qemuMonitorTestProcessFileEntries: + * @inputstr: input file contents (modified) + * @fileName: File name of @inputstr (for error reporting) + * @items: filled with command, reply tuples + * @nitems: Count of elements in @items. * - * Create a JSON test monitor simulator object and fill it with expected command - * sequence and replies specified in @fileName. + * Process a monitor interaction file. * * The file contains a sequence of JSON commands and reply objects separated by - * empty lines. A command is followed by a reply. The QMP greeting is added - * automatically. - * - * Returns the monitor object on success; NULL on error. + * empty lines. A command is followed by a reply. */ -qemuMonitorTest * -qemuMonitorTestNewFromFileFull(const char *fileName, - virQEMUDriver *driver, - virDomainObj *vm, - GHashTable *qmpschema) +int +qemuMonitorTestProcessFileEntries(char *inputstr, + const char *fileName, + struct qemuMonitorTestCommandReplyTuple **items, + size_t *nitems) { - g_autoptr(qemuMonitorTest) ret = NULL; - g_autofree char *jsonstr = NULL; - char *tmp; + size_t nalloc = 0; + char *tmp = inputstr; size_t line = 0; - - char *command = NULL; + char *command = inputstr; char *response = NULL; size_t commandln = 0; - if (virTestLoadFile(fileName, &jsonstr) < 0) - return NULL; - - if (!(ret = qemuMonitorTestNew(driver->xmlopt, vm, driver, NULL, - qmpschema))) - return NULL; + *items = NULL; + *nitems = 0; - tmp = jsonstr; - command = tmp; while ((tmp = strchr(tmp, '\n'))) { line++; @@ -1335,11 +1323,16 @@ qemuMonitorTestNewFromFileFull(const char *fileName, *(tmp + 1) = '\0'; if (response) { - if (qemuMonitorTestFullAddItem(ret, fileName, command, - response, commandln) < 0) - return NULL; - command = NULL; - response = NULL; + struct qemuMonitorTestCommandReplyTuple *item; + + VIR_RESIZE_N(*items, nalloc, *nitems, 1); + + item = *items + *nitems; + + item->command = g_steal_pointer(&command); + item->reply = g_steal_pointer(&response); + item->line = commandln; + (*nitems)++; } /* Move the @tmp and @singleReply. */ @@ -1354,14 +1347,70 @@ qemuMonitorTestNewFromFileFull(const char *fileName, } if (command) { + struct qemuMonitorTestCommandReplyTuple *item; + if (!response) { virReportError(VIR_ERR_INTERNAL_ERROR, "missing response for command " "on line '%zu' in '%s'", commandln, fileName); - return NULL; + return -1; } - if (qemuMonitorTestFullAddItem(ret, fileName, command, - response, commandln) < 0) + VIR_RESIZE_N(*items, nalloc, *nitems, 1); + + item = *items + *nitems; + + item->command = g_steal_pointer(&command); + item->reply = g_steal_pointer(&response); + item->line = commandln; + (*nitems)++; + } + + return 0; +} + +/** + * qemuMonitorTestNewFromFileFull: + * @fileName: File name to load monitor replies from + * @driver: qemu driver object + * @vm: domain object (may be null if it's not needed by the test) + * @qmpschema: QMP schema data hash table if QMP checking is required + * + * Create a JSON test monitor simulator object and fill it with expected command + * sequence and replies specified in @fileName. + * + * The file contains a sequence of JSON commands and reply objects separated by + * empty lines. A command is followed by a reply. The QMP greeting is added + * automatically. + * + * Returns the monitor object on success; NULL on error. + */ +qemuMonitorTest * +qemuMonitorTestNewFromFileFull(const char *fileName, + virQEMUDriver *driver, + virDomainObj *vm, + GHashTable *qmpschema) +{ + g_autoptr(qemuMonitorTest) ret = NULL; + g_autofree char *jsonstr = NULL; + g_autofree struct qemuMonitorTestCommandReplyTuple *items = NULL; + size_t nitems = 0; + size_t i; + + if (virTestLoadFile(fileName, &jsonstr) < 0) + return NULL; + + if (!(ret = qemuMonitorTestNew(driver->xmlopt, vm, driver, NULL, + qmpschema))) + return NULL; + + if (qemuMonitorTestProcessFileEntries(jsonstr, fileName, &items, &nitems) < 0) + return NULL; + + for (i = 0; i < nitems; i++) { + struct qemuMonitorTestCommandReplyTuple *item = items + i; + + if (qemuMonitorTestFullAddItem(ret, fileName, item->command, item->reply, + item->line) < 0) return NULL; } diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h index 89c33c434b..56e3d56056 100644 --- a/tests/qemumonitortestutils.h +++ b/tests/qemumonitortestutils.h @@ -126,3 +126,16 @@ virDomainObj * qemuMonitorTestGetDomainObj(qemuMonitorTest *test); G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuMonitorTest, qemuMonitorTestFree); + +struct qemuMonitorTestCommandReplyTuple { + const char *command; + const char *reply; + size_t line; /* line number of @command */ +}; + + +int +qemuMonitorTestProcessFileEntries(char *inputstr, + const char *fileName, + struct qemuMonitorTestCommandReplyTuple **items, + size_t *nitems); -- 2.31.1

The tool is assembled from individual bits used for tests and actual capturing of the replies files. The tool ensures correct numbering and formatting of entries. In normal usage mode it masks as a test which validates formatting and numbering of the tests/qemucapabilitiesdata/*.replies files. This tool was actually used to produce commits 096ac87a1a46 and aa21615ccbc. In case a manual modification of the replies file is needed the 'modify()' function provides a convenient way to do programatic modification of the caps file. As an example the modify() function has commented-out code which provides a basic scaffold to do modifications along with a how-to. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/meson.build | 1 + tests/qemucapabilitiesnumbering.c | 242 ++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 tests/qemucapabilitiesnumbering.c diff --git a/tests/meson.build b/tests/meson.build index f75c248720..4792220b48 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -452,6 +452,7 @@ if conf.has('WITH_QEMU') { 'name': 'qemuagenttest', 'link_with': [ test_qemu_driver_lib, test_utils_qemu_monitor_lib ], 'link_whole': [ test_utils_qemu_lib ] }, { 'name': 'qemublocktest', 'include': [ storage_file_inc_dir ], 'link_with': [ test_qemu_driver_lib, test_utils_qemu_monitor_lib ], 'link_whole': [ test_utils_qemu_lib ] }, { 'name': 'qemucapabilitiestest', 'link_with': [ test_qemu_driver_lib, test_utils_qemu_monitor_lib ], 'link_whole': [ test_utils_qemu_lib ] }, + { 'name': 'qemucapabilitiesnumbering', 'link_with': [ test_qemu_driver_lib, test_utils_qemu_monitor_lib ], 'link_whole': [ test_utils_qemu_lib ] }, { 'name': 'qemucaps2xmltest', 'link_with': [ test_qemu_driver_lib ], 'link_whole': [ test_utils_qemu_lib ] }, { 'name': 'qemucommandutiltest', 'link_with': [ test_qemu_driver_lib, test_utils_qemu_monitor_lib ], 'link_whole': [ test_utils_qemu_lib ] }, { 'name': 'qemudomaincheckpointxml2xmltest', 'link_with': [ test_qemu_driver_lib ], 'link_whole': [ test_utils_qemu_lib ] }, diff --git a/tests/qemucapabilitiesnumbering.c b/tests/qemucapabilitiesnumbering.c new file mode 100644 index 0000000000..eff7dbe2a1 --- /dev/null +++ b/tests/qemucapabilitiesnumbering.c @@ -0,0 +1,242 @@ +/* + * qemucapabilitiesnumbering.c: swiss-army knife for qemu capability data manipulation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ +#include <config.h> + +#include "testutils.h" +#include "testutilsqemu.h" +#include "qemumonitortestutils.h" + +struct qmpTuple { + virJSONValue *command; + virJSONValue *reply; +}; + +struct qmpCommandList { + struct qmpTuple *items; + size_t nitems; +}; + +typedef struct qmpCommandList qmpCommandList; + + +static int +modify(struct qmpCommandList *list G_GNUC_UNUSED) +{ + /* in case you want to programmatically modify the replies file enable the + * following block and modify it to your needs. After compiling run this + * with: + * + * VIR_TEST_REGENERATE_OUTPUT=1 tests/qemucapabilitiesnumbering + * + * This applies the modification along with updating the files. Use git to + * your advantage to roll back mistakes. + */ +#if 0 + struct qmpTuple tmptuple = { NULL, NULL }; + size_t found = 0; + size_t i; + + for (i = 0; i < list->nitems; i++) { + struct qmpTuple *item = list->items + i; + const char *cmdname = virJSONValueObjectGetString(item->command, "execute"); + + if (STREQ_NULLABLE(cmdname, "qom-list-properties")) { + found = i; + // break; /* uncomment if you want to find the first occurence */ + } + } + + if (found == 0) { + fprintf(stderr, "entry not found!!!\n"); + return -1; + } + + tmptuple.command = virJSONValueFromString("{\"execute\":\"dummy\"}"); + tmptuple.reply = virJSONValueFromString("{\"return\":{}}"); + // tmptuple.reply = virTestLoadFileJSON("path/", "to/", "file.json"); + + ignore_value(VIR_INSERT_ELEMENT(list->items, found + 1, list->nitems, tmptuple)); +#endif + + return 0; +} + + +static void +qmpCommandListFree(qmpCommandList* list) +{ + size_t i; + + if (!list) + return; + + for (i = 0; i < list->nitems; i++) { + struct qmpTuple *item = list->items + i; + + virJSONValueFree(item->command); + virJSONValueFree(item->reply); + } + + g_free(list->items); + g_free(list); +} + + +G_DEFINE_AUTOPTR_CLEANUP_FUNC(qmpCommandList, qmpCommandListFree); + + +static qmpCommandList * +loadReplies(const char *filename) +{ + g_autofree char *replies = NULL; + g_autofree struct qemuMonitorTestCommandReplyTuple *items = NULL; + size_t nitems = 0; + g_autoptr(qmpCommandList) list = NULL; + size_t i; + + if (virTestLoadFile(filename, &replies) < 0) { + fprintf(stderr, "Failed to load '%s'\n", filename); + return NULL; + } + + if (qemuMonitorTestProcessFileEntries(replies, filename, &items, &nitems) < 0) + return NULL; + + list = g_new0(qmpCommandList, 1); + list->items = g_new0(struct qmpTuple, nitems); + + for (i = 0; i < nitems; i++) { + struct qemuMonitorTestCommandReplyTuple *item = items + i; + + if (!(list->items[list->nitems].command = virJSONValueFromString(item->command)) || + !(list->items[list->nitems++].reply = virJSONValueFromString(item->reply))) + return NULL; + } + + return g_steal_pointer(&list); +} + +/* see printLineSkipEmpty in tests/qemucapsprobemock.c */ +static void +printLineSkipEmpty(const char *p, + virBuffer *buf) +{ + for (; *p; p++) { + if (p[0] == '\n' && p[1] == '\n') + continue; + + virBufferAddChar(buf, *p); + } +} + + +static void +renumberItem(virJSONValue *val, + size_t num) +{ + g_autoptr(virJSONValue) label = virJSONValueNewString(g_strdup_printf("libvirt-%zu", num)); + + virJSONValueObjectReplaceValue(val, "id", &label); +} + + +static int +output(virBuffer *buf, + qmpCommandList *list) +{ + size_t commandindex = 1; + size_t i; + + for (i = 0; i < list->nitems; i++) { + struct qmpTuple *item = list->items + i; + g_autofree char *jsoncommand = NULL; + g_autofree char *jsonreply = NULL; + + if (STREQ_NULLABLE(virJSONValueObjectGetString(item->command, "execute"), "qmp_capabilities")) + commandindex = 1; + + /* fix numbering */ + renumberItem(item->command, commandindex); + renumberItem(item->reply, commandindex); + commandindex++; + + /* output formatting */ + if (!(jsoncommand = virJSONValueToString(item->command, true)) || + !(jsonreply = virJSONValueToString(item->reply, true))) + return -1; + + printLineSkipEmpty(jsoncommand, buf); + virBufferAddLit(buf, "\n"); + printLineSkipEmpty(jsonreply, buf); + virBufferAddLit(buf, "\n"); + } + + virBufferTrim(buf, "\n"); + + return 0; +} + + +static int +testCapsFile(const void *opaque) +{ + const char *repliesFile = opaque; + g_autoptr(qmpCommandList) list = NULL; + g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; + + if (!(list = loadReplies(repliesFile))) + return -1; + + if (virTestGetRegenerate() > 0) { + if (modify(list) < 0) + return -1; + } + + output(&buf, list); + + if (virTestCompareToFile(virBufferCurrentContent(&buf), repliesFile) < 0) + return -1; + + return 0; +} + + +static int +iterateCapsFile(const char *inputDir, + const char *prefix, + const char *version, + const char *archName, + const char *suffix, + void *opaque G_GNUC_UNUSED) +{ + g_autofree char *repliesFile = g_strdup_printf("%s/%s_%s.%s.%s", inputDir, prefix, version, archName, suffix); + + return virTestRun(repliesFile, testCapsFile, repliesFile); +} + + +static int +testmain(void) +{ + if (testQemuCapsIterate(".replies", iterateCapsFile, NULL) < 0) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} + +VIR_TEST_MAIN(testmain) -- 2.31.1

The 'qemucapabilitiesnumbering' tool now replaces the role of this script and provides way to programmatically modify the replies file on top. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/domaincapstest.c | 3 ++- tests/qemucapabilitiestest.c | 4 +++- tests/qemucapsfixreplies | 26 -------------------------- 3 files changed, 5 insertions(+), 28 deletions(-) delete mode 100755 tests/qemucapsfixreplies diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c index 479bcb1c35..da5c629fd4 100644 --- a/tests/domaincapstest.c +++ b/tests/domaincapstest.c @@ -422,7 +422,8 @@ mymain(void) * to generate updated or new *.replies data files. * * If you manually edit replies files you can run - * "tests/qemucapsfixreplies foo.replies" to fix the replies ids. + * VIR_TEST_REGENERATE_OUTPUT=1 tests/qemucapabilitiesnumbering + * to fix the replies ids. * * Once a replies file has been generated and tweaked if necessary, * you can drop it into tests/qemucapabilitiesdata/ (with a sensible diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c index 99534ab9a1..ae208f442c 100644 --- a/tests/qemucapabilitiestest.c +++ b/tests/qemucapabilitiestest.c @@ -212,7 +212,9 @@ mymain(void) * to generate updated or new *.replies data files. * * If you manually edit replies files you can run - * "tests/qemucapsfixreplies foo.replies" to fix the replies ids. + * VIR_TEST_REGENERATE_OUTPUT=1 tests/qemucapabilitiesnumbering + * to fix the replies ids. The tool also allows for programatic modification + * of the replies file. * * Once a replies file has been generated and tweaked if necessary, * you can drop it into tests/qemucapabilitiesdata/ (with a sensible diff --git a/tests/qemucapsfixreplies b/tests/qemucapsfixreplies deleted file mode 100755 index 597f9ecd6e..0000000000 --- a/tests/qemucapsfixreplies +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -if [ "$#" -ne 1 ] || [ "$1" = "--help" ] || [ ! -f "$1" ]; then - echo "This script fixes replies ids in QEMU replies files." - echo "" - echo " Usage: $0 path/to/qemu.replies" - exit 0 -fi - -awk -i inplace \ - 'BEGIN {last=0; pattern="libvirt-[0-9]+"} - { - if (match($0, "libvirt-1[^0-9]")) { - count=1; - } - if (match($0, pattern)) { - str="libvirt-" count; - sub(pattern, str, $0); - if (last != count) { - last=count; - } else { - count++; - } - } - print - }' "$1" -- 2.31.1

On a Wednesday in 2021, Peter Krempa wrote:
I was asked multiple times how to automate modification of the qemu capability dumps. I wrote a tool [1] that allowed doing that. Unfortunately as it wasn't in the repo it was not avalilable.
Now I've finally set out to finish it. It masquerades as a test in default mode checking the ordering and numbering of the caps dump files as in the past there were few cases where manual edits broke it.
In update mode it allows modification of the replies and gives few examples to do so. There's a lot of possible things users would want to do so there's no way around actually writing some code but it should be easier this way.
Since this now does also numbering, the AWK/shell script 'qemucapsfixreplies' can be removed.
[1]: Okay, okay. Copy&paste'd few bits from various places into one messy C file and didn't bother with memory cleanup. But it worked.
Peter Krempa (5): util: json: Introduce virJSONValueObjectReplaceValue tests: qemumonitortestutils.h: Reformat header file qemumonitortestutils: Extract parser for the monitor conversation dump file tests: Tool for programatic modification of qemucapabilitiesdata/*.replies tests: Remove 'qemucapsfixreplies'
src/libvirt_private.syms | 1 + src/util/virjson.c | 20 +++ src/util/virjson.h | 6 + tests/domaincapstest.c | 3 +- tests/meson.build | 1 + tests/qemucapabilitiesnumbering.c | 242 ++++++++++++++++++++++++++++++ tests/qemucapabilitiestest.c | 4 +- tests/qemucapsfixreplies | 26 ---- tests/qemumonitortestutils.c | 123 ++++++++++----- tests/qemumonitortestutils.h | 149 +++++++++++------- 10 files changed, 453 insertions(+), 122 deletions(-) create mode 100644 tests/qemucapabilitiesnumbering.c delete mode 100755 tests/qemucapsfixreplies
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (2)
-
Ján Tomko
-
Peter Krempa