[PATCH 18/20] qemuagenttest: Introduce testing of shutdown commands

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch exports a few utility functions and adds testing of shutdown
commands of the guest agent.
---
 tests/qemuagenttest.c        | 119 +++++++++++++++++++++++++++++++++++++++++++
 tests/qemumonitortestutils.c |  21 ++++----
 tests/qemumonitortestutils.h |  28 ++++++++--
 3 files changed, 153 insertions(+), 15 deletions(-)

diff --git a/tests/qemuagenttest.c b/tests/qemuagenttest.c
index d0b450e..e314bb0 100644
--- a/tests/qemuagenttest.c
+++ b/tests/qemuagenttest.c
@@ -207,6 +207,124 @@ cleanup:
 }


+struct qemuAgentShutdownTestData {
+    const char *mode;
+    qemuAgentEvent event;
+};
+
+
+static int
+qemuAgentShutdownTestMonitorHandler(qemuMonitorTestPtr test,
+                                    qemuMonitorTestItemPtr item,
+                                    const char *cmdstr)
+{
+    struct qemuAgentShutdownTestData *data;
+    virJSONValuePtr val = NULL;
+    virJSONValuePtr args;
+    const char *cmdname;
+    const char *mode;
+    int ret = -1;
+
+    data = qemuMonitorTestItemGetPrivateData(item);
+
+    if (!(val = virJSONValueFromString(cmdstr)))
+        return -1;
+
+    if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) {
+        ret = qemuMonitorReportError(test, "Missing command name in %s", cmdstr);
+        goto cleanup;
+    }
+
+    if (STRNEQ(cmdname, "guest-shutdown")) {
+        ret = qemuMonitorTestAddUnexpectedErrorResponse(test);
+        goto cleanup;
+    }
+
+    if (!(args = virJSONValueObjectGet(val, "arguments"))) {
+        ret = qemuMonitorReportError(test,
+                                     "Missing arguments section");
+        goto cleanup;
+    }
+
+    if (!(mode = virJSONValueObjectGetString(args, "mode"))) {
+        ret = qemuMonitorReportError(test, "Missing shutdown mode");
+        goto cleanup;
+    }
+
+    /* now don't reply but return a qemu agent event */
+    qemuAgentNotifyEvent(qemuMonitorTestGetAgent(test),
+                         data->event);
+
+    ret = 0;
+
+cleanup:
+    virJSONValueFree(val);
+    return ret;
+
+}
+
+
+static int
+testQemuAgentShutdown(const void *data)
+{
+    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
+    qemuMonitorTestPtr test = qemuMonitorTestNewAgent(xmlopt);
+    struct qemuAgentShutdownTestData priv;
+    int ret = -1;
+
+    if (!test)
+        return -1;
+
+    if (qemuMonitorTestAddAgentSyncResponse(test) < 0)
+        goto cleanup;
+
+    priv.event = QEMU_AGENT_EVENT_SHUTDOWN;
+    priv.mode = "shutdown";
+
+    if (qemuMonitorTestAddHandler(test, qemuAgentShutdownTestMonitorHandler,
+                                  &priv, NULL) < 0)
+        goto cleanup;
+
+    if (qemuAgentShutdown(qemuMonitorTestGetAgent(test),
+                          QEMU_AGENT_SHUTDOWN_HALT) < 0)
+        goto cleanup;
+
+    if (qemuMonitorTestAddAgentSyncResponse(test) < 0)
+        goto cleanup;
+
+    priv.event = QEMU_AGENT_EVENT_SHUTDOWN;
+    priv.mode = "powerdown";
+
+    if (qemuMonitorTestAddHandler(test, qemuAgentShutdownTestMonitorHandler,
+                                  &priv, NULL) < 0)
+        goto cleanup;
+
+    if (qemuAgentShutdown(qemuMonitorTestGetAgent(test),
+                          QEMU_AGENT_SHUTDOWN_POWERDOWN) < 0)
+        goto cleanup;
+
+    if (qemuMonitorTestAddAgentSyncResponse(test) < 0)
+        goto cleanup;
+
+    priv.event = QEMU_AGENT_EVENT_RESET;
+    priv.mode = "reboot";
+
+    if (qemuMonitorTestAddHandler(test, qemuAgentShutdownTestMonitorHandler,
+                                  &priv, NULL) < 0)
+        goto cleanup;
+
+    if (qemuAgentShutdown(qemuMonitorTestGetAgent(test),
+                          QEMU_AGENT_SHUTDOWN_REBOOT) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+cleanup:
+    qemuMonitorTestFree(test);
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -232,6 +350,7 @@ mymain(void)
     DO_TEST(FSThaw);
     DO_TEST(FSTrim);
     DO_TEST(Suspend);
+    DO_TEST(Shutdown);

     virObjectUnref(xmlopt);

diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index f383915..32217a3 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -37,12 +37,6 @@

 #define VIR_FROM_THIS VIR_FROM_NONE

-typedef struct _qemuMonitorTestItem qemuMonitorTestItem;
-typedef qemuMonitorTestItem *qemuMonitorTestItemPtr;
-typedef int (*qemuMonitorTestResponseCallback)(qemuMonitorTestPtr test,
-                                               qemuMonitorTestItemPtr item,
-                                               const char *message);
-
 struct _qemuMonitorTestItem {
     qemuMonitorTestResponseCallback cb;
     void *opaque;
@@ -96,7 +90,7 @@ qemuMonitorTestItemFree(qemuMonitorTestItemPtr item)
 /*
  * Appends data for a reply to the outgoing buffer
  */
-static int
+int
 qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
                           const char *response)
 {
@@ -119,7 +113,7 @@ qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
 }


-static int
+int
 qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test)
 {
     if (test->agent || test->json) {
@@ -133,7 +127,7 @@ qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test)
 }


-static int
+int
 qemuMonitorReportError(qemuMonitorTestPtr test, const char *errmsg, ...)
 {
     va_list msgargs;
@@ -167,7 +161,6 @@ cleanup:
 }


-
 static int
 qemuMonitorTestProcessCommand(qemuMonitorTestPtr test,
                               const char *cmdstr)
@@ -383,7 +376,7 @@ qemuMonitorTestFree(qemuMonitorTestPtr test)
 }


-static int
+int
 qemuMonitorTestAddHandler(qemuMonitorTestPtr test,
                           qemuMonitorTestResponseCallback cb,
                           void *opaque,
@@ -414,6 +407,12 @@ error:
     return -1;
 }

+void *
+qemuMonitorTestItemGetPrivateData(qemuMonitorTestItemPtr item)
+{
+    return item ? item->opaque : NULL;
+}
+

 typedef struct _qemuMonitorTestCommandArgs qemuMonitorTestCommandArgs;
 typedef qemuMonitorTestCommandArgs *qemuMonitorTestCommandArgsPtr;
diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h
index 5ec5707..845f567 100644
--- a/tests/qemumonitortestutils.h
+++ b/tests/qemumonitortestutils.h
@@ -27,10 +27,30 @@
 typedef struct _qemuMonitorTest qemuMonitorTest;
 typedef qemuMonitorTest *qemuMonitorTestPtr;

-int
-qemuMonitorTestAddItem(qemuMonitorTestPtr test,
-                       const char *command_name,
-                       const char *response);
+typedef struct _qemuMonitorTestItem qemuMonitorTestItem;
+typedef qemuMonitorTestItem *qemuMonitorTestItemPtr;
+typedef int (*qemuMonitorTestResponseCallback)(qemuMonitorTestPtr test,
+                                               qemuMonitorTestItemPtr item,
+                                               const char *message);
+
+int qemuMonitorTestAddHandler(qemuMonitorTestPtr test,
+                              qemuMonitorTestResponseCallback cb,
+                              void *opaque,
+                              virFreeCallback freecb);
+
+int qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
+                              const char *response);
+
+int qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test);
+
+int qemuMonitorReportError(qemuMonitorTestPtr test, const char *errmsg, ...);
+
+void *qemuMonitorTestItemGetPrivateData(qemuMonitorTestItemPtr item);
+
+
+int qemuMonitorTestAddItem(qemuMonitorTestPtr test,
+                           const char *command_name,
+                           const char *response);

 int qemuMonitorTestAddAgentSyncResponse(qemuMonitorTestPtr test);

-- 
1.8.3.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]