tests: Introduce qemuMonitorTestNewFromFile

It's a convenient wrapper around qemuMonitorTestNew which feeds the test
monitor with QMP replies from a specified file.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Jiri Denemark 2016-06-06 13:46:41 +02:00
parent 2a8d40f4ec
commit 2dcacc0ecf
3 changed files with 66 additions and 56 deletions

View File

@ -35,56 +35,6 @@ struct _testQemuData {
const char *base;
};
static qemuMonitorTestPtr
testQemuFeedMonitor(char *replies,
virDomainXMLOptionPtr xmlopt)
{
qemuMonitorTestPtr test = NULL;
char *tmp = replies;
char *singleReply = tmp;
/* Our JSON parser expects replies to be separated by a newline character.
* Hence we must preprocess the file a bit. */
while ((tmp = strchr(tmp, '\n'))) {
/* It is safe to touch tmp[1] since all strings ends with '\0'. */
bool eof = !tmp[1];
if (*(tmp + 1) != '\n') {
*tmp = ' ';
tmp++;
} else {
/* Cut off a single reply. */
*(tmp + 1) = '\0';
if (test) {
if (qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
goto error;
} else {
/* Create new mocked monitor with our greeting */
if (!(test = qemuMonitorTestNew(true, xmlopt, NULL, NULL, singleReply)))
goto error;
}
if (!eof) {
/* Move the @tmp and @singleReply. */
tmp += 2;
singleReply = tmp;
}
}
if (eof)
break;
}
if (test && qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
goto error;
return test;
error:
qemuMonitorTestFree(test);
return NULL;
}
static int
testQemuCaps(const void *opaque)
@ -93,7 +43,6 @@ testQemuCaps(const void *opaque)
const testQemuData *data = opaque;
char *repliesFile = NULL;
char *capsFile = NULL;
char *replies = NULL;
qemuMonitorTestPtr mon = NULL;
virQEMUCapsPtr capsActual = NULL;
char *actual = NULL;
@ -104,10 +53,7 @@ testQemuCaps(const void *opaque)
abs_srcdir, data->base, data->archName) < 0)
goto cleanup;
if (virTestLoadFile(repliesFile, &replies) < 0)
goto cleanup;
if (!(mon = testQemuFeedMonitor(replies, data->xmlopt)))
if (!(mon = qemuMonitorTestNewFromFile(repliesFile, data->xmlopt)))
goto cleanup;
if (!(capsActual = virQEMUCapsNew()) ||
@ -125,7 +71,6 @@ testQemuCaps(const void *opaque)
cleanup:
VIR_FREE(repliesFile);
VIR_FREE(capsFile);
VIR_FREE(replies);
VIR_FREE(actual);
qemuMonitorTestFree(mon);
virObjectUnref(capsActual);

View File

@ -24,6 +24,7 @@
#include <string.h>
#include <time.h>
#include "testutils.h"
#include "qemumonitortestutils.h"
#include "virthread.h"
@ -926,6 +927,67 @@ qemuMonitorTestNew(bool json,
return NULL;
}
qemuMonitorTestPtr
qemuMonitorTestNewFromFile(const char *fileName,
virDomainXMLOptionPtr xmlopt)
{
qemuMonitorTestPtr test = NULL;
char *json = NULL;
char *tmp;
char *singleReply;
if (virTestLoadFile(fileName, &json) < 0)
goto cleanup;
/* Our JSON parser expects replies to be separated by a newline character.
* Hence we must preprocess the file a bit. */
tmp = singleReply = json;
while ((tmp = strchr(tmp, '\n'))) {
/* It is safe to touch tmp[1] since all strings ends with '\0'. */
bool eof = !tmp[1];
if (*(tmp + 1) != '\n') {
*tmp = ' ';
tmp++;
} else {
/* Cut off a single reply. */
*(tmp + 1) = '\0';
if (test) {
if (qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
goto error;
} else {
/* Create new mocked monitor with our greeting */
if (!(test = qemuMonitorTestNew(true, xmlopt, NULL, NULL, singleReply)))
goto error;
}
if (!eof) {
/* Move the @tmp and @singleReply. */
tmp += 2;
singleReply = tmp;
}
}
if (eof)
break;
}
if (test && qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
goto error;
cleanup:
VIR_FREE(json);
return test;
error:
qemuMonitorTestFree(test);
test = NULL;
goto cleanup;
}
qemuMonitorTestPtr
qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt)
{

View File

@ -69,6 +69,9 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json,
virQEMUDriverPtr driver,
const char *greeting);
qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName,
virDomainXMLOptionPtr xmlopt);
qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt);