From 2dcacc0ecf14cf7c333a56ba052c03a5a1a76889 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Mon, 6 Jun 2016 13:46:41 +0200 Subject: [PATCH] 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 --- tests/qemucapabilitiestest.c | 57 +-------------------------------- tests/qemumonitortestutils.c | 62 ++++++++++++++++++++++++++++++++++++ tests/qemumonitortestutils.h | 3 ++ 3 files changed, 66 insertions(+), 56 deletions(-) diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c index 8ee930996c..5a27b3624e 100644 --- a/tests/qemucapabilitiestest.c +++ b/tests/qemucapabilitiestest.c @@ -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); diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 3d34942b95..2b94cf2b76 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -24,6 +24,7 @@ #include #include +#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) { diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h index 3f07f65575..ca8c143287 100644 --- a/tests/qemumonitortestutils.h +++ b/tests/qemumonitortestutils.h @@ -69,6 +69,9 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json, virQEMUDriverPtr driver, const char *greeting); +qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName, + virDomainXMLOptionPtr xmlopt); + qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt);