[libvirt] [PATCH 13/20] qemumonitortestutils: Improve error reporting from mock qemu monitor

Peter Krempa pkrempa at redhat.com
Tue Jul 30 13:05:48 UTC 2013


Use the JSON error messages to report errors back to the caller in
addition to erroring out. The error reported from the event loop from
the mock function of the monitor was later overwritten by the call to
the monitor/agent interaction API. This will also allow testing of error
reporting.
---
 tests/qemumonitortestutils.c | 56 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index 96cb054..f421b9e 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -103,6 +103,8 @@ qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
     size_t want = strlen(response) + 2;
     size_t have = test->outgoingCapacity - test->outgoingLength;

+    VIR_DEBUG("Adding response to monitor command: '%s", response);
+
     if (have < want) {
         size_t need = want - have;
         if (VIR_EXPAND_N(test->outgoing, test->outgoingCapacity, need) < 0)
@@ -132,11 +134,48 @@ qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test)


 static int
+qemuMonitorReportError(qemuMonitorTestPtr test, const char *errmsg, ...)
+{
+    va_list msgargs;
+    char *msg = NULL;
+    char *jsonmsg = NULL;
+    int ret = -1;
+
+    va_start(msgargs, errmsg);
+
+    if (virVasprintf(&msg, errmsg, msgargs) < 0)
+        goto cleanup;
+
+    if (test->agent || test->json) {
+        if (virAsprintf(&jsonmsg, "{ \"error\": "
+                                  " { \"desc\": \"%s\", "
+                                  "   \"class\": \"UnexpectedCommand\" } }",
+                                  msg) < 0)
+            goto cleanup;
+    } else {
+        if (virAsprintf(&jsonmsg, "error: '%s'", msg) < 0)
+            goto cleanup;
+    }
+
+    ret = qemuMonitorTestAddReponse(test, jsonmsg);
+
+cleanup:
+    va_end(msgargs);
+    VIR_FREE(msg);
+    VIR_FREE(jsonmsg);
+    return ret;
+}
+
+
+
+static int
 qemuMonitorTestProcessCommand(qemuMonitorTestPtr test,
                               const char *cmdstr)
 {
     int ret;

+    VIR_DEBUG("Processing string from monitor handler: '%s", cmdstr);
+
     if (test->nitems == 0) {
         return qemuMonitorTestAddUnexpectedErrorResponse(test);
     } else {
@@ -399,8 +438,7 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
             return -1;

         if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           "Missing command name in %s", cmdstr);
+            ret = qemuMonitorReportError(test, "Missing command name in %s", cmdstr);
             goto cleanup;
         }
     } else {
@@ -410,8 +448,9 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
         cmdname = cmdcopy;

         if (!(tmp = strchr(cmdcopy, ' '))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           "Cannot find command name in '%s'", cmdstr);
+            ret = qemuMonitorReportError(test,
+                                         "Cannot find command name in '%s'",
+                                         cmdstr);
             goto cleanup;
         }
         *tmp = '\0';
@@ -465,8 +504,7 @@ qemuMonitorTestProcessGuestAgentSync(qemuMonitorTestPtr test,
         return -1;

     if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       "Missing guest-sync command name");
+        ret = qemuMonitorReportError(test, "Missing guest-sync command name");
         goto cleanup;
     }

@@ -476,14 +514,12 @@ qemuMonitorTestProcessGuestAgentSync(qemuMonitorTestPtr test,
     }

     if (!(args = virJSONValueObjectGet(val, "arguments"))) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       "Missing arguments for guest-sync");
+        ret = qemuMonitorReportError(test, "Missing arguments for guest-sync");
         goto cleanup;
     }

     if (virJSONValueObjectGetNumberUlong(args, "id", &id)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       "Missing id for guest sync");
+        ret = qemuMonitorReportError(test, "Missing id for guest sync");
         goto cleanup;
     }

-- 
1.8.3.2




More information about the libvir-list mailing list