[libvirt] [PATCH v2 5/9] qemu: add helper for querying timezone info

Jonathon Jongsma jjongsma at redhat.com
Wed Aug 21 22:15:55 UTC 2019


This function queries timezone information within the guest and adds
the information to an array of typed parameters with field names
intended to be returned to virDomainGetGuestInfo()

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
 src/qemu/qemu_agent.c | 46 ++++++++++++++++++++++++++
 src/qemu/qemu_agent.h |  1 +
 tests/qemuagenttest.c | 76 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+)

diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index cd4105bfb8..d7c00250ea 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -2384,3 +2384,49 @@ qemuAgentGetOSInfo(qemuAgentPtr mon,
     virJSONValueFree(reply);
     return ret;
 }
+
+int
+qemuAgentGetTimezone(qemuAgentPtr mon,
+                     virTypedParameterPtr *params,
+                     int *nparams,
+                     int *maxparams)
+{
+    int ret = -1;
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+    virJSONValuePtr data = NULL;
+    const char *name;
+    int offset;
+
+    if (!(cmd = qemuAgentMakeCommand("guest-get-timezone", NULL)))
+        return -1;
+
+    if (qemuAgentCommand(mon, cmd, &reply, true,
+                         VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
+        goto cleanup;
+
+    if (!(data = virJSONValueObjectGetObject(reply, "return"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("guest-get-timezone reply was missing return data"));
+        goto cleanup;
+    }
+
+    if ((name = virJSONValueObjectGetString(data, "zone")) == NULL)
+        goto cleanup;
+    if (virTypedParamsAddString(params, nparams, maxparams,
+                                "timezone.name", name) < 0)
+        goto cleanup;
+
+    if ((virJSONValueObjectGetNumberInt(data, "offset", &offset)) < 0)
+        goto cleanup;
+    if (virTypedParamsAddInt(params, nparams, maxparams,
+                             "timezone.offset", offset) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
diff --git a/src/qemu/qemu_agent.h b/src/qemu/qemu_agent.h
index ee019455e5..69b0176855 100644
--- a/src/qemu/qemu_agent.h
+++ b/src/qemu/qemu_agent.h
@@ -123,3 +123,4 @@ int qemuAgentSetUserPassword(qemuAgentPtr mon,
 
 int qemuAgentGetUsers(qemuAgentPtr mon, virTypedParameterPtr *params, int *nparams, int *maxparams);
 int qemuAgentGetOSInfo(qemuAgentPtr mon, virTypedParameterPtr *params, int *nparams, int *maxparams);
+int qemuAgentGetTimezone(qemuAgentPtr mon, virTypedParameterPtr *params, int *nparams, int *maxparams);
diff --git a/tests/qemuagenttest.c b/tests/qemuagenttest.c
index 9467c51d54..aa1e993649 100644
--- a/tests/qemuagenttest.c
+++ b/tests/qemuagenttest.c
@@ -1189,7 +1189,82 @@ testQemuAgentOSInfo(const void *data)
     return ret;
 }
 
+static const char testQemuAgentTimezoneResponse1[] =
+"{\"return\":{\"zone\":\"IST\",\"offset\":19800}}";
+static const char testQemuAgentTimezoneResponse2[] =
+"{\"return\":{\"zone\":\"CEST\",\"offset\":7200}}";
+static const char testQemuAgentTimezoneResponse3[] =
+"{\"return\":{\"zone\":\"NDT\",\"offset\":-9000}}";
+static const char testQemuAgentTimezoneResponse4[] =
+"{\"return\":{\"zone\":\"PDT\",\"offset\":-25200}}";
 
+static int
+testQemuAgentTimezone(const void *data)
+{
+    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
+    qemuMonitorTestPtr test = qemuMonitorTestNewAgent(xmlopt);
+    int ret = -1;
+
+    if (!test)
+        return -1;
+
+#define VALIDATE_TIMEZONE(response_, expected_name_, expected_offset_) \
+    do { \
+        virTypedParameterPtr params_ = NULL; \
+        int nparams_ = 0; \
+        int maxparams_ = 0; \
+        const char *name_ = NULL; \
+        int offset_; \
+        if (qemuMonitorTestAddAgentSyncResponse(test) < 0) \
+        goto cleanup; \
+        if (qemuMonitorTestAddItem(test, "guest-get-timezone", \
+                                   response_) < 0) \
+        goto cleanup; \
+        if (qemuAgentGetTimezone(qemuMonitorTestGetAgent(test), \
+                                 &params_, &nparams_, &maxparams_) < 0) \
+            goto cleanup; \
+        if (nparams_ != 2) { \
+            virReportError(VIR_ERR_INTERNAL_ERROR, \
+                           "Expected 2 params, got %d", nparams_); \
+            goto cleanup; \
+        } \
+        if (virTypedParamsGetString(params_, nparams_, \
+                                    "timezone.name", &name_) < 0) { \
+            virReportError(VIR_ERR_INTERNAL_ERROR, "missing param '%s'", \
+                           "tiemzone.name"); \
+            goto cleanup; \
+        } \
+        if (STRNEQ(name_, expected_name_)) { \
+            virReportError(VIR_ERR_INTERNAL_ERROR, \
+                           "Expected name '%s', got '%s'", expected_name_, name_); \
+            goto cleanup; \
+        } \
+        if (virTypedParamsGetInt(params_, nparams_, \
+                                 "timezone.offset", &offset_) < 0) { \
+            virReportError(VIR_ERR_INTERNAL_ERROR, "missing param '%s'", \
+                           "tiemzone.offset"); \
+            goto cleanup; \
+        } \
+        if (offset_ != expected_offset_) { \
+            virReportError(VIR_ERR_INTERNAL_ERROR, \
+                           "Expected offset '%i', got '%i'", offset_, \
+                           expected_offset_); \
+            goto cleanup; \
+        } \
+        virTypedParamsFree(params_, nparams_); \
+    } while (0)
+
+    VALIDATE_TIMEZONE(testQemuAgentTimezoneResponse1, "IST", 19800);
+    VALIDATE_TIMEZONE(testQemuAgentTimezoneResponse2, "CEST", 7200);
+    VALIDATE_TIMEZONE(testQemuAgentTimezoneResponse3, "NDT", -9000);
+    VALIDATE_TIMEZONE(testQemuAgentTimezoneResponse4, "PDT", -25200);
+
+    ret = 0;
+
+ cleanup:
+    qemuMonitorTestFree(test);
+    return ret;
+}
 static int
 mymain(void)
 {
@@ -1221,6 +1296,7 @@ mymain(void)
     DO_TEST(GetInterfaces);
     DO_TEST(Users);
     DO_TEST(OSInfo);
+    DO_TEST(Timezone);
 
     DO_TEST(Timeout); /* Timeout should always be called last */
 
-- 
2.21.0




More information about the libvir-list mailing list