/**
- * 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++;
*(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. */
}
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;
}