6ae9ed
From 684d63adc58a29183d7ff4161b3ed93f09896375 Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <684d63adc58a29183d7ff4161b3ed93f09896375@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Wed, 24 Aug 2016 16:11:25 -0400
6ae9ed
Subject: [PATCH] tests: Add test infrastructure for qemuMonitorGetCPUInfo
6ae9ed
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1097930
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1224341
6ae9ed
6ae9ed
As the combination algorithm is rather complex and ugly it's necessary
6ae9ed
to make sure it works properly. Add test suite infrastructure for
6ae9ed
testing it along with a basic test based on x86_64 platform.
6ae9ed
6ae9ed
(cherry picked from commit f8638471d7f203a654e3b157dfd042a2239e2cb0)
6ae9ed
---
6ae9ed
 ...nitorjson-cpuinfo-x86-basic-pluggable-cpus.json |  50 ++++++++
6ae9ed
 ...orjson-cpuinfo-x86-basic-pluggable-hotplug.json |  82 +++++++++++++
6ae9ed
 ...emumonitorjson-cpuinfo-x86-basic-pluggable.data |  39 ++++++
6ae9ed
 tests/qemumonitorjsontest.c                        | 134 +++++++++++++++++++++
6ae9ed
 4 files changed, 305 insertions(+)
6ae9ed
 create mode 100644 tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-cpus.json
6ae9ed
 create mode 100644 tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-hotplug.json
6ae9ed
 create mode 100644 tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable.data
6ae9ed
6ae9ed
diff --git a/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-cpus.json b/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-cpus.json
6ae9ed
new file mode 100644
6ae9ed
index 0000000..7a49731
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-cpus.json
6ae9ed
@@ -0,0 +1,50 @@
6ae9ed
+{
6ae9ed
+  "return": [
6ae9ed
+    {
6ae9ed
+      "arch": "x86",
6ae9ed
+      "current": true,
6ae9ed
+      "CPU": 0,
6ae9ed
+      "qom_path": "/machine/unattached/device[0]",
6ae9ed
+      "pc": -2130415978,
6ae9ed
+      "halted": true,
6ae9ed
+      "thread_id": 518291
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "arch": "x86",
6ae9ed
+      "current": false,
6ae9ed
+      "CPU": 1,
6ae9ed
+      "qom_path": "/machine/unattached/device[2]",
6ae9ed
+      "pc": -2130415978,
6ae9ed
+      "halted": true,
6ae9ed
+      "thread_id": 518292
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "arch": "x86",
6ae9ed
+      "current": false,
6ae9ed
+      "CPU": 2,
6ae9ed
+      "qom_path": "/machine/unattached/device[3]",
6ae9ed
+      "pc": -2130415978,
6ae9ed
+      "halted": true,
6ae9ed
+      "thread_id": 518294
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "arch": "x86",
6ae9ed
+      "current": false,
6ae9ed
+      "CPU": 3,
6ae9ed
+      "qom_path": "/machine/unattached/device[4]",
6ae9ed
+      "pc": -2130415978,
6ae9ed
+      "halted": true,
6ae9ed
+      "thread_id": 518295
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "arch": "x86",
6ae9ed
+      "current": false,
6ae9ed
+      "CPU": 4,
6ae9ed
+      "qom_path": "/machine/unattached/device[5]",
6ae9ed
+      "pc": -2130415978,
6ae9ed
+      "halted": true,
6ae9ed
+      "thread_id": 518296
6ae9ed
+    }
6ae9ed
+  ],
6ae9ed
+  "id": "libvirt-22"
6ae9ed
+}
6ae9ed
diff --git a/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-hotplug.json b/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-hotplug.json
6ae9ed
new file mode 100644
6ae9ed
index 0000000..3f35018
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable-hotplug.json
6ae9ed
@@ -0,0 +1,82 @@
6ae9ed
+{
6ae9ed
+  "return": [
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 1,
6ae9ed
+        "thread-id": 1,
6ae9ed
+        "socket-id": 1
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 1,
6ae9ed
+        "thread-id": 0,
6ae9ed
+        "socket-id": 1
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 0,
6ae9ed
+        "thread-id": 1,
6ae9ed
+        "socket-id": 1
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 0,
6ae9ed
+        "thread-id": 0,
6ae9ed
+        "socket-id": 1
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "qom-path": "/machine/unattached/device[5]",
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 1,
6ae9ed
+        "thread-id": 1,
6ae9ed
+        "socket-id": 0
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "qom-path": "/machine/unattached/device[4]",
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 1,
6ae9ed
+        "thread-id": 0,
6ae9ed
+        "socket-id": 0
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "qom-path": "/machine/unattached/device[3]",
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 0,
6ae9ed
+        "thread-id": 1,
6ae9ed
+        "socket-id": 0
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "qom-path": "/machine/unattached/device[2]",
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    },
6ae9ed
+    {
6ae9ed
+      "props": {
6ae9ed
+        "core-id": 0,
6ae9ed
+        "thread-id": 0,
6ae9ed
+        "socket-id": 0
6ae9ed
+      },
6ae9ed
+      "vcpus-count": 1,
6ae9ed
+      "qom-path": "/machine/unattached/device[0]",
6ae9ed
+      "type": "qemu64-x86_64-cpu"
6ae9ed
+    }
6ae9ed
+  ],
6ae9ed
+  "id": "libvirt-23"
6ae9ed
+}
6ae9ed
diff --git a/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable.data b/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable.data
6ae9ed
new file mode 100644
6ae9ed
index 0000000..a367a09
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/qemumonitorjsondata/qemumonitorjson-cpuinfo-x86-basic-pluggable.data
6ae9ed
@@ -0,0 +1,39 @@
6ae9ed
+[vcpu libvirt-id='0']
6ae9ed
+    thread-id='518291'
6ae9ed
+    qemu-id='1'
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    qom_path='/machine/unattached/device[0]'
6ae9ed
+    topology: socket='0' core='0' thread='0' vcpus='1'
6ae9ed
+[vcpu libvirt-id='1']
6ae9ed
+    thread-id='518292'
6ae9ed
+    qemu-id='2'
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    qom_path='/machine/unattached/device[2]'
6ae9ed
+    topology: socket='0' core='0' thread='1' vcpus='1'
6ae9ed
+[vcpu libvirt-id='2']
6ae9ed
+    thread-id='518294'
6ae9ed
+    qemu-id='3'
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    qom_path='/machine/unattached/device[3]'
6ae9ed
+    topology: socket='0' core='1' thread='0' vcpus='1'
6ae9ed
+[vcpu libvirt-id='3']
6ae9ed
+    thread-id='518295'
6ae9ed
+    qemu-id='4'
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    qom_path='/machine/unattached/device[4]'
6ae9ed
+    topology: socket='0' core='1' thread='1' vcpus='1'
6ae9ed
+[vcpu libvirt-id='4']
6ae9ed
+    thread-id='518296'
6ae9ed
+    qemu-id='5'
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    qom_path='/machine/unattached/device[5]'
6ae9ed
+    topology: socket='1' core='0' thread='0' vcpus='1'
6ae9ed
+[vcpu libvirt-id='5']
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    topology: socket='1' core='0' thread='1' vcpus='1'
6ae9ed
+[vcpu libvirt-id='6']
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    topology: socket='1' core='1' thread='0' vcpus='1'
6ae9ed
+[vcpu libvirt-id='7']
6ae9ed
+    type='qemu64-x86_64-cpu'
6ae9ed
+    topology: socket='1' core='1' thread='1' vcpus='1'
6ae9ed
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
6ae9ed
index 8c31005..22b4ccc 100644
6ae9ed
--- a/tests/qemumonitorjsontest.c
6ae9ed
+++ b/tests/qemumonitorjsontest.c
6ae9ed
@@ -2336,6 +2336,130 @@ testQemuMonitorJSONGetIOThreads(const void *data)
6ae9ed
     return ret;
6ae9ed
 }
6ae9ed
 
6ae9ed
+struct testCPUInfoData {
6ae9ed
+    const char *name;
6ae9ed
+    size_t maxvcpus;
6ae9ed
+    virDomainXMLOptionPtr xmlopt;
6ae9ed
+};
6ae9ed
+
6ae9ed
+
6ae9ed
+static char *
6ae9ed
+testQemuMonitorCPUInfoFormat(qemuMonitorCPUInfoPtr vcpus,
6ae9ed
+                             size_t nvcpus)
6ae9ed
+{
6ae9ed
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
6ae9ed
+    qemuMonitorCPUInfoPtr vcpu;
6ae9ed
+    size_t i;
6ae9ed
+
6ae9ed
+    for (i = 0; i < nvcpus; i++) {
6ae9ed
+        vcpu = vcpus + i;
6ae9ed
+
6ae9ed
+        virBufferAsprintf(&buf, "[vcpu libvirt-id='%zu']\n", i);
6ae9ed
+        virBufferAdjustIndent(&buf, 4);
6ae9ed
+
6ae9ed
+        if (vcpu->tid)
6ae9ed
+            virBufferAsprintf(&buf, "thread-id='%llu'\n",
6ae9ed
+                              (unsigned long long) vcpu->tid);
6ae9ed
+
6ae9ed
+        if (vcpu->id != 0)
6ae9ed
+            virBufferAsprintf(&buf, "qemu-id='%d'\n", vcpu->id);
6ae9ed
+
6ae9ed
+        if (vcpu->type)
6ae9ed
+            virBufferAsprintf(&buf, "type='%s'\n", vcpu->type);
6ae9ed
+
6ae9ed
+        if (vcpu->alias)
6ae9ed
+            virBufferAsprintf(&buf, "alias='%s'\n", vcpu->alias);
6ae9ed
+        if (vcpu->qom_path)
6ae9ed
+            virBufferAsprintf(&buf, "qom_path='%s'\n", vcpu->qom_path);
6ae9ed
+
6ae9ed
+        if (vcpu->socket_id != -1 || vcpu->core_id != -1 ||
6ae9ed
+            vcpu->thread_id != -1 || vcpu->vcpus != 0) {
6ae9ed
+            virBufferAddLit(&buf, "topology:");
6ae9ed
+            if (vcpu->socket_id != -1)
6ae9ed
+                virBufferAsprintf(&buf, " socket='%d'", vcpu->socket_id);
6ae9ed
+            if (vcpu->core_id != -1)
6ae9ed
+                virBufferAsprintf(&buf, " core='%d'", vcpu->core_id);
6ae9ed
+            if (vcpu->thread_id != -1)
6ae9ed
+                virBufferAsprintf(&buf, " thread='%d'", vcpu->thread_id);
6ae9ed
+            if (vcpu->vcpus != 0)
6ae9ed
+                virBufferAsprintf(&buf, " vcpus='%u'", vcpu->vcpus);
6ae9ed
+            virBufferAddLit(&buf, "\n");
6ae9ed
+        }
6ae9ed
+
6ae9ed
+        virBufferAdjustIndent(&buf, -4);
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    return virBufferContentAndReset(&buf;;
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
+static int
6ae9ed
+testQemuMonitorCPUInfo(const void *opaque)
6ae9ed
+{
6ae9ed
+    const struct testCPUInfoData *data = opaque;
6ae9ed
+    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, data->xmlopt);
6ae9ed
+    char *queryCpusFile = NULL;
6ae9ed
+    char *queryHotpluggableFile = NULL;
6ae9ed
+    char *dataFile = NULL;
6ae9ed
+    char *queryCpusStr = NULL;
6ae9ed
+    char *queryHotpluggableStr = NULL;
6ae9ed
+    char *actual = NULL;
6ae9ed
+    qemuMonitorCPUInfoPtr vcpus = NULL;
6ae9ed
+    int rc;
6ae9ed
+    int ret = -1;
6ae9ed
+
6ae9ed
+    if (!test)
6ae9ed
+        return -1;
6ae9ed
+
6ae9ed
+    if (virAsprintf(&queryCpusFile,
6ae9ed
+                    "%s/qemumonitorjsondata/qemumonitorjson-cpuinfo-%s-cpus.json",
6ae9ed
+                    abs_srcdir, data->name) < 0 ||
6ae9ed
+        virAsprintf(&queryHotpluggableFile,
6ae9ed
+                    "%s/qemumonitorjsondata/qemumonitorjson-cpuinfo-%s-hotplug.json",
6ae9ed
+                    abs_srcdir, data->name) < 0 ||
6ae9ed
+        virAsprintf(&dataFile,
6ae9ed
+                    "%s/qemumonitorjsondata/qemumonitorjson-cpuinfo-%s.data",
6ae9ed
+                    abs_srcdir, data->name) < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    if (virTestLoadFile(queryCpusFile, &queryCpusStr) < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    if (virTestLoadFile(queryHotpluggableFile, &queryHotpluggableStr) < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    if (qemuMonitorTestAddItem(test, "query-hotpluggable-cpus",
6ae9ed
+                               queryHotpluggableStr) < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    if (qemuMonitorTestAddItem(test, "query-cpus", queryCpusStr) < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    rc = qemuMonitorGetCPUInfo(qemuMonitorTestGetMonitor(test),
6ae9ed
+                               &vcpus, data->maxvcpus, true);
6ae9ed
+
6ae9ed
+    if (rc < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    actual = testQemuMonitorCPUInfoFormat(vcpus, data->maxvcpus);
6ae9ed
+
6ae9ed
+    if (virTestCompareToFile(actual, dataFile) < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    ret = 0;
6ae9ed
+ cleanup:
6ae9ed
+    VIR_FREE(queryCpusFile);
6ae9ed
+    VIR_FREE(queryHotpluggableFile);
6ae9ed
+    VIR_FREE(dataFile);
6ae9ed
+    VIR_FREE(queryCpusStr);
6ae9ed
+    VIR_FREE(queryHotpluggableStr);
6ae9ed
+    VIR_FREE(actual);
6ae9ed
+    qemuMonitorCPUInfoFree(vcpus, data->maxvcpus);
6ae9ed
+    qemuMonitorTestFree(test);
6ae9ed
+    return ret;
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
 static int
6ae9ed
 mymain(void)
6ae9ed
 {
6ae9ed
@@ -2378,6 +2502,14 @@ mymain(void)
6ae9ed
             ret = -1;                                                     \
6ae9ed
     } while (0)
6ae9ed
 
6ae9ed
+#define DO_TEST_CPU_INFO(name, maxvcpus)                                       \
6ae9ed
+    do {                                                                       \
6ae9ed
+        struct testCPUInfoData data = {name, maxvcpus, driver.xmlopt};         \
6ae9ed
+        if (virTestRun("GetCPUInfo(" name ")", testQemuMonitorCPUInfo,         \
6ae9ed
+                       &data) < 0)                                             \
6ae9ed
+            ret = -1;                                                          \
6ae9ed
+    } while (0)
6ae9ed
+
6ae9ed
     DO_TEST(GetStatus);
6ae9ed
     DO_TEST(GetVersion);
6ae9ed
     DO_TEST(GetMachines);
6ae9ed
@@ -2452,6 +2584,8 @@ mymain(void)
6ae9ed
     DO_TEST_CPU_DATA("full");
6ae9ed
     DO_TEST_CPU_DATA("ecx");
6ae9ed
 
6ae9ed
+    DO_TEST_CPU_INFO("x86-basic-pluggable", 8);
6ae9ed
+
6ae9ed
     qemuTestDriverFree(&driver);
6ae9ed
 
6ae9ed
     return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
6ae9ed
-- 
6ae9ed
2.10.0
6ae9ed