diff mbox series

[RFC,v2,1/5] tests: add machine 'none' with -cpu test

Message ID 1516296842-136976-2-git-send-email-imammedo@redhat.com
State New
Headers show
Series [RFC,v2,1/5] tests: add machine 'none' with -cpu test | expand

Commit Message

Igor Mammedov Jan. 18, 2018, 5:33 p.m. UTC
Check that "$QEMU -M none -cpu FOO" starts QEMU without error

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 tests/Makefile.include |  2 ++
 tests/machine-none.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 tests/machine-none.c

Comments

Thomas Huth Jan. 18, 2018, 7:01 p.m. UTC | #1
On 18.01.2018 18:33, Igor Mammedov wrote:
> Check that "$QEMU -M none -cpu FOO" starts QEMU without error
[...]
> diff --git a/tests/machine-none.c b/tests/machine-none.c
> new file mode 100644
> index 0000000..d16c283
> --- /dev/null
> +++ b/tests/machine-none.c

Could you please name the file with a "-test.c" suffix, so that the
"*-test" wildcard in tests/.gitignore catches this, too? Otherwise
you've got to add "machine-none" explicitly to the gitignore file.

> @@ -0,0 +1,71 @@
> +/*
> + * Machine 'none' tests.
> + *
> + * Copyright (c) 2018 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 "qemu/osdep.h"
> +
> +#include "qemu-common.h"
> +#include "qemu/cutils.h"
> +#include "libqtest.h"
> +#include "qapi/qmp/types.h"
> +
> +struct arch2cpu {
> +    const char *arch;
> +    const char *cpu_model;
> +};
> +
> +static struct arch2cpu cpus_map[] = {
> +    /* tested targets list */
> +};
> +
> +static const char *get_cpu_model_by_arch(const char *arch)
> +{
> +    int i;
> +
> +    for (i = 0; i < ARRAY_SIZE(cpus_map); i++) {
> +        if (!strcmp(arch, cpus_map[i].arch)) {
> +            return cpus_map[i].cpu_model;
> +        }
> +    }
> +    return NULL;
> +}
> +
> +static void test_machine_cpu_cli(void)
> +{
> +    char *args;
> +    QDict *response;
> +    const char *arch = qtest_get_arch();
> +    const char *cpu_model = get_cpu_model_by_arch(arch);
> +
> +    if (!cpu_model) {
> +        fprintf(stderr, "WARNING: cpu name for target '%s' isn't defined,"
> +                " add it to cpus_map\n", arch);
> +        return; /* TODO: die here to force all targets have a test */
> +    }
> +    args = g_strdup_printf("-machine none -cpu %s", cpu_model);
> +    qtest_start(args);

May I suggest to use qtest_startf() instead?

> +
> +    response = qmp("{ 'execute': 'quit' }");
> +    g_assert(qdict_haskey(response, "return"));
> +    QDECREF(response);
> +
> +    qtest_end();

... and that should be qtest_quit() if you use qtest_startf().

> +    g_free(args);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    g_test_init(&argc, &argv, NULL);
> +
> +    qtest_add_func("machine/none/cpu_option", test_machine_cpu_cli);
> +
> +    return g_test_run();
> +}
> 

 Thomas
diff mbox series

Patch

diff --git a/tests/Makefile.include b/tests/Makefile.include
index 8883274..6661149 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -380,6 +380,7 @@  check-qtest-s390x-y += tests/virtio-balloon-test$(EXESUF)
 check-qtest-s390x-y += tests/virtio-console-test$(EXESUF)
 check-qtest-s390x-y += tests/virtio-serial-test$(EXESUF)
 
+check-qtest-generic-y += tests/machine-none$(EXESUF)
 check-qtest-generic-y += tests/qom-test$(EXESUF)
 check-qtest-generic-y += tests/test-hmp$(EXESUF)
 
@@ -782,6 +783,7 @@  tests/display-vga-test$(EXESUF): tests/display-vga-test.o
 tests/ipoctal232-test$(EXESUF): tests/ipoctal232-test.o
 tests/qom-test$(EXESUF): tests/qom-test.o
 tests/test-hmp$(EXESUF): tests/test-hmp.o
+tests/machine-none$(EXESUF): tests/machine-none.o
 tests/drive_del-test$(EXESUF): tests/drive_del-test.o $(libqos-virtio-obj-y)
 tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y)
 tests/nvme-test$(EXESUF): tests/nvme-test.o
diff --git a/tests/machine-none.c b/tests/machine-none.c
new file mode 100644
index 0000000..d16c283
--- /dev/null
+++ b/tests/machine-none.c
@@ -0,0 +1,71 @@ 
+/*
+ * Machine 'none' tests.
+ *
+ * Copyright (c) 2018 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 "qemu/osdep.h"
+
+#include "qemu-common.h"
+#include "qemu/cutils.h"
+#include "libqtest.h"
+#include "qapi/qmp/types.h"
+
+struct arch2cpu {
+    const char *arch;
+    const char *cpu_model;
+};
+
+static struct arch2cpu cpus_map[] = {
+    /* tested targets list */
+};
+
+static const char *get_cpu_model_by_arch(const char *arch)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(cpus_map); i++) {
+        if (!strcmp(arch, cpus_map[i].arch)) {
+            return cpus_map[i].cpu_model;
+        }
+    }
+    return NULL;
+}
+
+static void test_machine_cpu_cli(void)
+{
+    char *args;
+    QDict *response;
+    const char *arch = qtest_get_arch();
+    const char *cpu_model = get_cpu_model_by_arch(arch);
+
+    if (!cpu_model) {
+        fprintf(stderr, "WARNING: cpu name for target '%s' isn't defined,"
+                " add it to cpus_map\n", arch);
+        return; /* TODO: die here to force all targets have a test */
+    }
+    args = g_strdup_printf("-machine none -cpu %s", cpu_model);
+    qtest_start(args);
+
+    response = qmp("{ 'execute': 'quit' }");
+    g_assert(qdict_haskey(response, "return"));
+    QDECREF(response);
+
+    qtest_end();
+    g_free(args);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    qtest_add_func("machine/none/cpu_option", test_machine_cpu_cli);
+
+    return g_test_run();
+}