提交 2dcacc0e 编写于 作者: J Jiri Denemark

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: NJiri Denemark <jdenemar@redhat.com>
上级 2a8d40f4
...@@ -35,56 +35,6 @@ struct _testQemuData { ...@@ -35,56 +35,6 @@ struct _testQemuData {
const char *base; 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 static int
testQemuCaps(const void *opaque) testQemuCaps(const void *opaque)
...@@ -93,7 +43,6 @@ testQemuCaps(const void *opaque) ...@@ -93,7 +43,6 @@ testQemuCaps(const void *opaque)
const testQemuData *data = opaque; const testQemuData *data = opaque;
char *repliesFile = NULL; char *repliesFile = NULL;
char *capsFile = NULL; char *capsFile = NULL;
char *replies = NULL;
qemuMonitorTestPtr mon = NULL; qemuMonitorTestPtr mon = NULL;
virQEMUCapsPtr capsActual = NULL; virQEMUCapsPtr capsActual = NULL;
char *actual = NULL; char *actual = NULL;
...@@ -104,10 +53,7 @@ testQemuCaps(const void *opaque) ...@@ -104,10 +53,7 @@ testQemuCaps(const void *opaque)
abs_srcdir, data->base, data->archName) < 0) abs_srcdir, data->base, data->archName) < 0)
goto cleanup; goto cleanup;
if (virTestLoadFile(repliesFile, &replies) < 0) if (!(mon = qemuMonitorTestNewFromFile(repliesFile, data->xmlopt)))
goto cleanup;
if (!(mon = testQemuFeedMonitor(replies, data->xmlopt)))
goto cleanup; goto cleanup;
if (!(capsActual = virQEMUCapsNew()) || if (!(capsActual = virQEMUCapsNew()) ||
...@@ -125,7 +71,6 @@ testQemuCaps(const void *opaque) ...@@ -125,7 +71,6 @@ testQemuCaps(const void *opaque)
cleanup: cleanup:
VIR_FREE(repliesFile); VIR_FREE(repliesFile);
VIR_FREE(capsFile); VIR_FREE(capsFile);
VIR_FREE(replies);
VIR_FREE(actual); VIR_FREE(actual);
qemuMonitorTestFree(mon); qemuMonitorTestFree(mon);
virObjectUnref(capsActual); virObjectUnref(capsActual);
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "testutils.h"
#include "qemumonitortestutils.h" #include "qemumonitortestutils.h"
#include "virthread.h" #include "virthread.h"
...@@ -926,6 +927,67 @@ qemuMonitorTestNew(bool json, ...@@ -926,6 +927,67 @@ qemuMonitorTestNew(bool json,
return NULL; 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 qemuMonitorTestPtr
qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt) qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt)
{ {
......
...@@ -69,6 +69,9 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json, ...@@ -69,6 +69,9 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json,
virQEMUDriverPtr driver, virQEMUDriverPtr driver,
const char *greeting); const char *greeting);
qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName,
virDomainXMLOptionPtr xmlopt);
qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt); qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册