]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: add helper for querying timezone info
authorJonathon Jongsma <jjongsma@redhat.com>
Fri, 23 Aug 2019 16:31:19 +0000 (11:31 -0500)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 26 Aug 2019 15:27:40 +0000 (17:27 +0200)
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@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/qemu/qemu_agent.c
src/qemu/qemu_agent.h
tests/qemuagenttest.c

index 8eb19ac3becd103f13e506a3f3305c3e06433493..a92e530dd14d160cc9b24e4c6e0c6d788ece7353 100644 (file)
@@ -2372,3 +2372,46 @@ qemuAgentGetOSInfo(qemuAgentPtr mon,
 
     return 0;
 }
+
+int
+qemuAgentGetTimezone(qemuAgentPtr mon,
+                     virTypedParameterPtr *params,
+                     int *nparams,
+                     int *maxparams)
+{
+    VIR_AUTOPTR(virJSONValue) cmd = NULL;
+    VIR_AUTOPTR(virJSONValue) 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)
+        return -1;
+
+    if (!(data = virJSONValueObjectGetObject(reply, "return"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("guest-get-timezone reply was missing return data"));
+        return -1;
+    }
+
+    if ((name = virJSONValueObjectGetString(data, "zone")) &&
+        virTypedParamsAddString(params, nparams, maxparams,
+                                "timezone.name", name) < 0)
+        return -1;
+
+    if ((virJSONValueObjectGetNumberInt(data, "offset", &offset)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("'offset' missing in reply of guest-get-timezone"));
+        return -1;
+    }
+
+    if (virTypedParamsAddInt(params, nparams, maxparams,
+                             "timezone.offset", offset) < 0)
+        return -1;
+
+    return 0;
+}
index 558f5cc7fdab045a6b127f4d733523b273193429..1224efcadc026a8475f25bf8293e42ddeb2c1775 100644 (file)
@@ -130,3 +130,8 @@ int qemuAgentGetOSInfo(qemuAgentPtr mon,
                        virTypedParameterPtr *params,
                        int *nparams,
                        int *maxparams);
+
+int qemuAgentGetTimezone(qemuAgentPtr mon,
+                         virTypedParameterPtr *params,
+                         int *nparams,
+                         int *maxparams);
index d0603f9efbe32a23dfa80fe68df66b5e3c8339ce..e6846efc13327baf70eb01d760af1c273ccac207 100644 (file)
@@ -1169,7 +1169,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)
 {
@@ -1201,6 +1276,7 @@ mymain(void)
     DO_TEST(GetInterfaces);
     DO_TEST(Users);
     DO_TEST(OSInfo);
+    DO_TEST(Timezone);
 
     DO_TEST(Timeout); /* Timeout should always be called last */