diff mbox

[4/4] test -m option parameters

Message ID 1403696543-2458-5-git-send-email-imammedo@redhat.com
State New
Headers show

Commit Message

Igor Mammedov June 25, 2014, 11:42 a.m. UTC
adds a base for memory hotplug tests, starting with
checking that -m option accepts expected options.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 tests/Makefile     |    2 +
 tests/memhp-test.c |  145 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+), 0 deletions(-)
 create mode 100644 tests/memhp-test.c
diff mbox

Patch

diff --git a/tests/Makefile b/tests/Makefile
index 7e53d0d..24c7e2b 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -154,6 +154,7 @@  gcov-files-i386-y += hw/pci-bridge/i82801b11.c
 check-qtest-i386-y += tests/ioh3420-test$(EXESUF)
 gcov-files-i386-y += hw/pci-bridge/ioh3420.c
 check-qtest-i386-y += tests/usb-hcd-ehci-test$(EXESUF)
+check-qtest-i386-y += tests/memhp-test$(EXESUF)
 gcov-files-i386-y += hw/usb/hcd-ehci.c
 gcov-files-i386-y += hw/usb/hcd-uhci.c
 gcov-files-i386-y += hw/usb/dev-hid.c
@@ -293,6 +294,7 @@  libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
 
+tests/memhp-test$(EXESUF): tests/memhp-test.o $(libqos-obj-y)
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
 tests/endianness-test$(EXESUF): tests/endianness-test.o
diff --git a/tests/memhp-test.c b/tests/memhp-test.c
new file mode 100644
index 0000000..4698854
--- /dev/null
+++ b/tests/memhp-test.c
@@ -0,0 +1,145 @@ 
+/*
+ * memory hotplug test cases.
+ *
+ * Copyright (c) 2014 Red Hat Inc.
+ *
+ * Authors:
+ *  Igor Mammedov <imammedo@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <glib.h>
+#include "libqtest.h"
+#include "include/qemu/option.h"
+
+typedef struct MOption {
+    const char *size;
+    const char *slots;
+    const char *maxmem;
+} MOption;
+
+typedef struct TestData TestData;
+struct TestData {
+    const char *args;
+    MOption m_option;
+    void (*test)(const TestData *data);
+};
+
+static void test_machine(gconstpointer opaque)
+{
+    TestData *s = (TestData *)opaque;
+    char *args = g_strdup_printf("%s", s->args);
+
+    if (s->m_option.size) {
+        char *old = args;
+        args = g_strdup_printf("%s -m %s", args, s->m_option.size);
+        g_free(old);
+    }
+
+    if (s->m_option.slots) {
+        char *old = args;
+        args = g_strdup_printf("%s -m slots=%s", args, s->m_option.slots);
+        g_free(old);
+    }
+
+    if (s->m_option.maxmem) {
+        char *old = args;
+        args = g_strdup_printf("%s -m maxmem=%s", args, s->m_option.maxmem);
+        g_free(old);
+    }
+
+    qtest_start(args);
+    s->test(s);
+    qtest_end();
+    g_free(args);
+}
+
+#define DEFARGS "-net none -display none -machine accel=qtest "
+
+#define ADD_COMMON(name, cmdline, sz, slots_nr, max_mem, func)               \
+    {                                                                        \
+        static const TestData d = {                                          \
+        .args = DEFARGS cmdline,                                             \
+        .m_option.size = sz,                                                 \
+        .m_option.slots = slots_nr,                                          \
+        .m_option.maxmem = max_mem,                                          \
+        .test = func};                                                       \
+        char *path;                                                          \
+        path = g_strdup_printf("/memhp/%s/[%s %s%s%s%s%s%s]", name, cmdline, \
+            d.m_option.size ? " -m size=" : "",                              \
+            d.m_option.size ? d.m_option.size : "",                          \
+            d.m_option.slots ? " -m slots=" : "",                            \
+            d.m_option.slots ? d.m_option.slots : "",                        \
+            d.m_option.maxmem ? " -m maxmem=" : "",                          \
+            d.m_option.maxmem ? d.m_option.maxmem : "");                     \
+        g_test_add_data_func(path, &d, test_machine);                        \
+        g_free(path);                                                        \
+    }
+
+#define ADD_X86_COMMON(name, cmdline, sz, slots_nr, max_mem, func)           \
+    if (strcmp(qtest_get_arch(), "i386") == 0 ||                             \
+        strcmp(qtest_get_arch(), "x86_64") == 0) {                           \
+        ADD_COMMON(name, cmdline, sz, slots_nr, max_mem, func)               \
+    }
+#define ADD_440FX_TEST(name, cmdline, sz, slots_nr, max_mem, func)           \
+    ADD_X86_COMMON(name "/pc", cmdline "-M pc", sz, slots_nr, max_mem, func)
+
+#define ADD_Q35_TEST(name, cmdline, sz, slots_nr, max_mem, func)             \
+    ADD_X86_COMMON(name "/q35" , cmdline "-M q35", sz, slots_nr, max_mem, func)
+
+#define ADD_TESTS(name, cmdline, sz, slots_nr, max_mem, func)                \
+    {                                                                        \
+        ADD_440FX_TEST(name, cmdline, sz, slots_nr, max_mem, func);          \
+        ADD_Q35_TEST(name, cmdline, sz, slots_nr, max_mem, func);            \
+    }
+
+static void test_num_prop_value(const char *path, const char *prop,
+                                const char *value)
+{
+    QDict *response;
+    uint64_t ret, num_value;
+
+    response = qmp("{ 'execute': 'qom-get',"
+                   "  'arguments': { 'path': '%s',"
+                   "                 'property': '%s' } }",
+                   path, prop);
+    /* qom-get may fail but should not, e.g., segfault. */
+    g_assert(qdict_haskey(response, "return"));
+    ret = qdict_get_int(response, "return");
+    QDECREF(response);
+
+    parse_option_size(prop, value, &num_value, &error_abort);
+    g_assert(ret == num_value);
+}
+
+static void test_args(const TestData *data)
+{
+    if (data->m_option.size) {
+        test_num_prop_value("/machine", "memory-size", data->m_option.size);
+    }
+
+    if (data->m_option.slots) {
+        test_num_prop_value("/machine", "memory-slots", data->m_option.slots);
+    }
+
+    if (data->m_option.maxmem) {
+        test_num_prop_value("/machine", "maxmem", data->m_option.maxmem);
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+    ADD_TESTS("args", "", "8M", NULL, NULL, test_args);
+    ADD_TESTS("args", "", "8M", "1", "16M", test_args);
+    ADD_TESTS("args", "", "8M", "256", "16M", test_args);
+    ADD_TESTS("args", "", "8M", "1", "1G", test_args);
+    ADD_TESTS("args", "", "8M", "1", "1T", test_args);
+
+    return g_test_run();
+}