[libvirt] [PATCH 0/2] qemumonitorjsontest improvements

Just a couple of tests. Michal Privoznik (2): qemumonitorjsontest: Test CPU state handling code qemumonitorjsontest: Introduce DO_TEST_SIMPLE tests/qemumonitorjsontest.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) -- 1.8.1.5

Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/qemumonitorjsontest.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index b287747..0a3f717 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -942,6 +942,61 @@ cleanup: return ret; } +static int +testQemuMonitorJSONCPU(const void *data) +{ + const virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data; + qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt); + int ret = -1; + bool running = false; + virDomainPausedReason reason = 0; + + if (qemuMonitorTestAddItem(test, "stop", "{\"return\": {}}") < 0 || + qemuMonitorTestAddItem(test, "query-status", + "{\"return\": {" + " \"status\": \"paused\"," + " \"singlestep\": false," + " \"running\": false}}") < 0 || + qemuMonitorTestAddItem(test, "cont", "{\"return\": {}}") < 0 || + qemuMonitorTestAddItem(test, "query-status", + "{\"return\": {" + " \"status\": \"running\"," + " \"singlestep\": false," + " \"running\": true}}") < 0) + goto cleanup; + + if (qemuMonitorJSONStopCPUs(qemuMonitorTestGetMonitor(test)) < 0) + goto cleanup; + + if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test), + &running, &reason) < 0) + goto cleanup; + + if (running) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "Running was not false"); + goto cleanup; + } + + if (qemuMonitorJSONStartCPUs(qemuMonitorTestGetMonitor(test), NULL) < 0) + goto cleanup; + + if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test), + &running, &reason) < 0) + goto cleanup; + + if (!running) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "Running was not true"); + goto cleanup; + } + + ret = 0; + +cleanup: + qemuMonitorTestFree(test); + return ret; +} static int mymain(void) @@ -977,6 +1032,7 @@ mymain(void) DO_TEST(GetObjectProperty); DO_TEST(SetObjectProperty); DO_TEST(GetDeviceAliases); + DO_TEST(CPU); virObjectUnref(xmlopt); -- 1.8.1.5

On 09/18/2013 12:16 PM, Michal Privoznik wrote:
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/qemumonitorjsontest.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index b287747..0a3f717 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -942,6 +942,61 @@ cleanup: return ret; }
+static int +testQemuMonitorJSONCPU(const void *data) +{ + const virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data; + qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
Coverity complains: (1) Event returned_null: Function "qemuMonitorTestNew(bool, virDomainXMLOptionPtr, virDomainObjPtr, virQEMUDriverPtr)" returns null (checked 14 out of 16 times). [details] (11) Event var_assigned: Assigning: "test" = null return value from "qemuMonitorTestNew(bool, virDomainXMLOptionPtr, virDomainObjPtr, virQEMUDriverPtr)".
+ int ret = -1; + bool running = false; + virDomainPausedReason reason = 0; + + if (qemuMonitorTestAddItem(test, "stop", "{\"return\": {}}") < 0 ||
(12) Event dereference: Dereferencing a pointer that might be null "test" when calling "qemuMonitorTestAddItem(qemuMonitorTestPtr, char const *, char const *)". [details] John
+ qemuMonitorTestAddItem(test, "query-status", + "{\"return\": {" + " \"status\": \"paused\"," + " \"singlestep\": false," + " \"running\": false}}") < 0 || + qemuMonitorTestAddItem(test, "cont", "{\"return\": {}}") < 0 || + qemuMonitorTestAddItem(test, "query-status", + "{\"return\": {" + " \"status\": \"running\"," + " \"singlestep\": false," + " \"running\": true}}") < 0) + goto cleanup; + + if (qemuMonitorJSONStopCPUs(qemuMonitorTestGetMonitor(test)) < 0) + goto cleanup; + + if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test), + &running, &reason) < 0) + goto cleanup; + + if (running) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "Running was not false"); + goto cleanup; + } + + if (qemuMonitorJSONStartCPUs(qemuMonitorTestGetMonitor(test), NULL) < 0) + goto cleanup; + + if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test), + &running, &reason) < 0) + goto cleanup; + + if (!running) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "Running was not true"); + goto cleanup; + } + + ret = 0; + +cleanup: + qemuMonitorTestFree(test); + return ret; +}
static int mymain(void) @@ -977,6 +1032,7 @@ mymain(void) DO_TEST(GetObjectProperty); DO_TEST(SetObjectProperty); DO_TEST(GetDeviceAliases); + DO_TEST(CPU);
virObjectUnref(xmlopt);

This macro is there to test the simplest monitor functions we have, those in the form of: int ( *func) (qemuMonitorPtr). So far, we have seven such functions. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/qemumonitorjsontest.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 0a3f717..b451e8e 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -31,6 +31,15 @@ #define VIR_FROM_THIS VIR_FROM_NONE +typedef struct _testQemuMonitorJSONSimpleFuncData testQemuMonitorJSONSimpleFuncData; +typedef testQemuMonitorJSONSimpleFuncData *testQemuMonitorJSONSimpleFuncDataPtr; +struct _testQemuMonitorJSONSimpleFuncData { + const char *cmd; + int (* func) (qemuMonitorPtr mon); + virDomainXMLOptionPtr xmlopt; + const char *reply; +}; + static int testQemuMonitorJSONGetStatus(const void *data) { @@ -999,10 +1008,35 @@ cleanup: } static int +testQemuMonitorJSONSimpleFunc(const void *opaque) +{ + const testQemuMonitorJSONSimpleFuncDataPtr data = (const testQemuMonitorJSONSimpleFuncDataPtr) opaque; + virDomainXMLOptionPtr xmlopt = data->xmlopt; + qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt); + const char *reply = data->reply; + int ret = -1; + + if (!reply) + reply = "{\"return\":{}}"; + + if (qemuMonitorTestAddItem(test, data->cmd, reply) < 0) + goto cleanup; + + if (data->func(qemuMonitorTestGetMonitor(test)) < 0) + goto cleanup; + + ret = 0; +cleanup: + qemuMonitorTestFree(test); + return ret; +} + +static int mymain(void) { int ret = 0; virDomainXMLOptionPtr xmlopt; + testQemuMonitorJSONSimpleFuncData simpleFunc; #if !WITH_YAJL fputs("libvirt not compiled with yajl, skipping this test\n", stderr); @@ -1019,6 +1053,12 @@ mymain(void) if (virtTestRun(# name, 1, testQemuMonitorJSON ## name, xmlopt) < 0) \ ret = -1 +#define DO_TEST_SIMPLE(CMD, FNC, ...) \ + simpleFunc = (testQemuMonitorJSONSimpleFuncData) {.cmd = CMD, .func = FNC, \ + .xmlopt = xmlopt, __VA_ARGS__ }; \ + if (virtTestRun(# FNC, 1, testQemuMonitorJSONSimpleFunc, &simpleFunc) < 0) \ + ret = -1 + DO_TEST(GetStatus); DO_TEST(GetVersion); DO_TEST(GetMachines); @@ -1033,6 +1073,13 @@ mymain(void) DO_TEST(SetObjectProperty); DO_TEST(GetDeviceAliases); DO_TEST(CPU); + DO_TEST_SIMPLE("qmp_capabilities", qemuMonitorJSONSetCapabilities); + DO_TEST_SIMPLE("system_powerdown", qemuMonitorSystemPowerdown); + DO_TEST_SIMPLE("system_reset", qemuMonitorJSONSystemReset); + DO_TEST_SIMPLE("migrate_cancel", qemuMonitorJSONMigrateCancel); + DO_TEST_SIMPLE("inject-nmi", qemuMonitorJSONInjectNMI); + DO_TEST_SIMPLE("system_wakeup", qemuMonitorJSONSystemWakeup); + DO_TEST_SIMPLE("nbd-server-stop", qemuMonitorJSONNBDServerStop); virObjectUnref(xmlopt); -- 1.8.1.5

On 09/18/2013 12:16 PM, Michal Privoznik wrote:
This macro is there to test the simplest monitor functions we have, those in the form of: int ( *func) (qemuMonitorPtr). So far, we have seven such functions.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/qemumonitorjsontest.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 0a3f717..b451e8e 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -31,6 +31,15 @@
#define VIR_FROM_THIS VIR_FROM_NONE
+typedef struct _testQemuMonitorJSONSimpleFuncData testQemuMonitorJSONSimpleFuncData; +typedef testQemuMonitorJSONSimpleFuncData *testQemuMonitorJSONSimpleFuncDataPtr; +struct _testQemuMonitorJSONSimpleFuncData { + const char *cmd; + int (* func) (qemuMonitorPtr mon); + virDomainXMLOptionPtr xmlopt; + const char *reply; +}; + static int testQemuMonitorJSONGetStatus(const void *data) { @@ -999,10 +1008,35 @@ cleanup: }
static int +testQemuMonitorJSONSimpleFunc(const void *opaque) +{ + const testQemuMonitorJSONSimpleFuncDataPtr data = (const testQemuMonitorJSONSimpleFuncDataPtr) opaque; + virDomainXMLOptionPtr xmlopt = data->xmlopt; + qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
Same here: (1) Event returned_null: Function "qemuMonitorTestNew(bool, virDomainXMLOptionPtr, virDomainObjPtr, virQEMUDriverPtr)" returns null (checked 14 out of 16 times). [details] (11) Event var_assigned: Assigning: "test" = null return value from "qemuMonitorTestNew(bool, virDomainXMLOptionPtr, virDomainObjPtr, virQEMUDriverPtr)".
+ const char *reply = data->reply; + int ret = -1; + + if (!reply) + reply = "{\"return\":{}}"; + + if (qemuMonitorTestAddItem(test, data->cmd, reply) < 0)
(13) Event dereference: Dereferencing a pointer that might be null "test" when calling "qemuMonitorTestAddItem(qemuMonitorTestPtr, char const *, char const *)". [details]
+ goto cleanup; + + if (data->func(qemuMonitorTestGetMonitor(test)) < 0) + goto cleanup; + + ret = 0; +cleanup: + qemuMonitorTestFree(test); + return ret; +} + +static int mymain(void) { int ret = 0; virDomainXMLOptionPtr xmlopt; + testQemuMonitorJSONSimpleFuncData simpleFunc;
#if !WITH_YAJL fputs("libvirt not compiled with yajl, skipping this test\n", stderr); @@ -1019,6 +1053,12 @@ mymain(void) if (virtTestRun(# name, 1, testQemuMonitorJSON ## name, xmlopt) < 0) \ ret = -1
+#define DO_TEST_SIMPLE(CMD, FNC, ...) \ + simpleFunc = (testQemuMonitorJSONSimpleFuncData) {.cmd = CMD, .func = FNC, \ + .xmlopt = xmlopt, __VA_ARGS__ }; \ + if (virtTestRun(# FNC, 1, testQemuMonitorJSONSimpleFunc, &simpleFunc) < 0) \ + ret = -1 + DO_TEST(GetStatus); DO_TEST(GetVersion); DO_TEST(GetMachines); @@ -1033,6 +1073,13 @@ mymain(void) DO_TEST(SetObjectProperty); DO_TEST(GetDeviceAliases); DO_TEST(CPU); + DO_TEST_SIMPLE("qmp_capabilities", qemuMonitorJSONSetCapabilities); + DO_TEST_SIMPLE("system_powerdown", qemuMonitorSystemPowerdown); + DO_TEST_SIMPLE("system_reset", qemuMonitorJSONSystemReset); + DO_TEST_SIMPLE("migrate_cancel", qemuMonitorJSONMigrateCancel); + DO_TEST_SIMPLE("inject-nmi", qemuMonitorJSONInjectNMI); + DO_TEST_SIMPLE("system_wakeup", qemuMonitorJSONSystemWakeup); + DO_TEST_SIMPLE("nbd-server-stop", qemuMonitorJSONNBDServerStop);
virObjectUnref(xmlopt);

On 09/20/2013 12:32 PM, John Ferlan wrote:
On 09/18/2013 12:16 PM, Michal Privoznik wrote:
static int +testQemuMonitorJSONSimpleFunc(const void *opaque) +{ + const testQemuMonitorJSONSimpleFuncDataPtr data = (const testQemuMonitorJSONSimpleFuncDataPtr) opaque; + virDomainXMLOptionPtr xmlopt = data->xmlopt; + qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
Same here:
(1) Event returned_null: Function "qemuMonitorTestNew(bool, virDomainXMLOptionPtr, virDomainObjPtr, virQEMUDriverPtr)" returns null (checked 14 out of 16 times). [details] (11) Event var_assigned: Assigning: "test" = null return value from "qemuMonitorTestNew(bool, virDomainXMLOptionPtr, virDomainObjPtr, virQEMUDriverPtr)".
+ const char *reply = data->reply; + int ret = -1; + + if (!reply) + reply = "{\"return\":{}}"; + + if (qemuMonitorTestAddItem(test, data->cmd, reply) < 0)
(13) Event dereference: Dereferencing a pointer that might be null "test" when calling "qemuMonitorTestAddItem(qemuMonitorTestPtr, char const *, char const *)". [details]
Both should be fixed by: 5b36ab9 Don't dereference NULL in qemumonitorjsontest Jan

On 09/18/2013 06:16 PM, Michal Privoznik wrote:
Just a couple of tests.
Michal Privoznik (2): qemumonitorjsontest: Test CPU state handling code qemumonitorjsontest: Introduce DO_TEST_SIMPLE
tests/qemumonitorjsontest.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+)
ACK
participants (3)
-
John Ferlan
-
Ján Tomko
-
Michal Privoznik