[2/2] tests: Add tests for query-netdevs command
diff mbox series

Message ID 20191113212516.13606-3-lekiravi@yandex-team.ru
State New
Headers show
Series
  • Introducing QMP query-netdevs command
Related show

Commit Message

Alexey Kirillov Nov. 13, 2019, 9:25 p.m. UTC
Signed-off-by: Alexey Kirillov <lekiravi@yandex-team.ru>
---
 tests/Makefile.include     |   2 +
 tests/test-query-netdevs.c | 114 +++++++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 tests/test-query-netdevs.c

Comments

Thomas Huth Nov. 14, 2019, 11:28 a.m. UTC | #1
On 13/11/2019 22.25, Alexey Kirillov wrote:
> Signed-off-by: Alexey Kirillov <lekiravi@yandex-team.ru>
> ---
>  tests/Makefile.include     |   2 +
>  tests/test-query-netdevs.c | 114 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 116 insertions(+)
>  create mode 100644 tests/test-query-netdevs.c
> 
> diff --git a/tests/Makefile.include b/tests/Makefile.include
> index 534ee48743..4d199e463b 100644
> --- a/tests/Makefile.include
> +++ b/tests/Makefile.include
> @@ -297,6 +297,7 @@ check-qtest-s390x-y += tests/migration-test$(EXESUF)
>  check-qtest-generic-y += tests/machine-none-test$(EXESUF)
>  check-qtest-generic-y += tests/qom-test$(EXESUF)
>  check-qtest-generic-y += tests/test-hmp$(EXESUF)
> +check-qtest-generic-y += tests/test-query-netdevs$(EXESUF)

Please use -$(CONFIG_SLIRP) instead of "-y" here.

>  qapi-schema += alternate-any.json
>  qapi-schema += alternate-array.json
> @@ -844,6 +845,7 @@ tests/numa-test$(EXESUF): tests/numa-test.o
>  tests/vmgenid-test$(EXESUF): tests/vmgenid-test.o tests/boot-sector.o tests/acpi-utils.o
>  tests/cdrom-test$(EXESUF): tests/cdrom-test.o tests/boot-sector.o $(libqos-obj-y)
>  tests/arm-cpu-features$(EXESUF): tests/arm-cpu-features.o
> +tests/test-query-netdevs$(EXESUF): tests/test-query-netdevs.o
>  
>  tests/migration/stress$(EXESUF): tests/migration/stress.o
>  	$(call quiet-command, $(LINKPROG) -static -O3 $(PTHREAD_LIB) -o $@ $< ,"LINK","$(TARGET_DIR)$@")
> diff --git a/tests/test-query-netdevs.c b/tests/test-query-netdevs.c
> new file mode 100644
> index 0000000000..2afde36114
> --- /dev/null
> +++ b/tests/test-query-netdevs.c
> @@ -0,0 +1,114 @@
> +/*
> + * QTest testcase for the query-netdevs
> + *
> + * Copyright Yandex N.V., 2019
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2.  See

Please use "GNU GPL, version 2 or later" for new code (as described in
the bottom part of the LICENSE file in the top directory).

> + * the COPYING file in the top-level directory.
> + *
> + */
[...]
> +static void test_query_netdevs(void)
> +{
> +    const char *arch = qtest_get_arch();
> +    size_t correction = 0;
> +    QObject *resp;
> +    QTestState *state;
> +
> +    if (strcmp(arch, "arm") == 0 ||
> +        strcmp(arch, "aarch64") == 0 ||
> +        strcmp(arch, "tricore") == 0) {
> +        g_test_skip("Not supported without machine type");

Since you're doing the check for the target architecture anyway, you
could also use "-M virt" for arm/aarch64 and "-M tricore_testboard" for
tricore, I think.

> +        return;
> +    }
> +
> +    /* Archs with default not unpluggable netdev */

maybe better: "Archs which still have a netdev despite of -nodefaults" ?

> +    if (strcmp(arch, "cris") == 0 ||
> +        strcmp(arch, "microblaze") == 0 ||
> +        strcmp(arch, "microblazeel") == 0 ||
> +        strcmp(arch, "sparc") == 0) {
> +        correction = 1;
> +    }

Just a matter of taste, but I like "g_str_equal()" more than the
old-fashioned "strcmp() == 0".

 Thomas

Patch
diff mbox series

diff --git a/tests/Makefile.include b/tests/Makefile.include
index 534ee48743..4d199e463b 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -297,6 +297,7 @@  check-qtest-s390x-y += tests/migration-test$(EXESUF)
 check-qtest-generic-y += tests/machine-none-test$(EXESUF)
 check-qtest-generic-y += tests/qom-test$(EXESUF)
 check-qtest-generic-y += tests/test-hmp$(EXESUF)
+check-qtest-generic-y += tests/test-query-netdevs$(EXESUF)
 
 qapi-schema += alternate-any.json
 qapi-schema += alternate-array.json
@@ -844,6 +845,7 @@  tests/numa-test$(EXESUF): tests/numa-test.o
 tests/vmgenid-test$(EXESUF): tests/vmgenid-test.o tests/boot-sector.o tests/acpi-utils.o
 tests/cdrom-test$(EXESUF): tests/cdrom-test.o tests/boot-sector.o $(libqos-obj-y)
 tests/arm-cpu-features$(EXESUF): tests/arm-cpu-features.o
+tests/test-query-netdevs$(EXESUF): tests/test-query-netdevs.o
 
 tests/migration/stress$(EXESUF): tests/migration/stress.o
 	$(call quiet-command, $(LINKPROG) -static -O3 $(PTHREAD_LIB) -o $@ $< ,"LINK","$(TARGET_DIR)$@")
diff --git a/tests/test-query-netdevs.c b/tests/test-query-netdevs.c
new file mode 100644
index 0000000000..2afde36114
--- /dev/null
+++ b/tests/test-query-netdevs.c
@@ -0,0 +1,114 @@ 
+/*
+ * QTest testcase for the query-netdevs
+ *
+ * Copyright Yandex N.V., 2019
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+
+#include "libqtest.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qlist.h"
+
+/*
+ * Events can get in the way of responses we are actually waiting for.
+ */
+GCC_FMT_ATTR(2, 3)
+static QObject *wait_command(QTestState *who, const char *command, ...)
+{
+    va_list ap;
+    QDict *response;
+    QObject *result;
+
+    va_start(ap, command);
+    qtest_qmp_vsend(who, command, ap);
+    va_end(ap);
+
+    response = qtest_qmp_receive(who);
+
+    result = qdict_get(response, "return");
+    g_assert(result);
+    qobject_ref(result);
+    qobject_unref(response);
+
+    return result;
+}
+
+static void qmp_query_netdevs_no_error(QTestState *qts,
+                                       size_t netdevs_count)
+{
+    QObject *resp;
+    QList *netdevs;
+
+    resp = wait_command(qts, "{'execute': 'x-query-netdevs'}");
+
+    netdevs = qobject_to(QList, resp);
+    g_assert(netdevs);
+    g_assert(qlist_size(netdevs) == netdevs_count);
+
+    qobject_unref(resp);
+}
+
+static void test_query_netdevs(void)
+{
+    const char *arch = qtest_get_arch();
+    size_t correction = 0;
+    QObject *resp;
+    QTestState *state;
+
+    if (strcmp(arch, "arm") == 0 ||
+        strcmp(arch, "aarch64") == 0 ||
+        strcmp(arch, "tricore") == 0) {
+        g_test_skip("Not supported without machine type");
+        return;
+    }
+
+    /* Archs with default not unpluggable netdev */
+    if (strcmp(arch, "cris") == 0 ||
+        strcmp(arch, "microblaze") == 0 ||
+        strcmp(arch, "microblazeel") == 0 ||
+        strcmp(arch, "sparc") == 0) {
+        correction = 1;
+    }
+
+    state = qtest_init(
+        "-nodefaults "
+        "-netdev user,id=slirp0");
+    g_assert(state);
+
+    qmp_query_netdevs_no_error(state, 1 + correction);
+
+    resp = wait_command(state,
+        "{'execute': 'netdev_add', 'arguments': {"
+        " 'id': 'slirp1',"
+        " 'type': 'user'}}");
+    qobject_unref(resp);
+
+    qmp_query_netdevs_no_error(state, 2 + correction);
+
+    resp = wait_command(state,
+        "{'execute': 'netdev_del', 'arguments': {"
+        " 'id': 'slirp1'}}");
+    qobject_unref(resp);
+
+    qmp_query_netdevs_no_error(state, 1 + correction);
+
+    qtest_quit(state);
+}
+
+int main(int argc, char **argv)
+{
+    int ret = 0;
+    g_test_init(&argc, &argv, NULL);
+
+    qtest_add_func("/net/qapi/query_netdevs",
+        test_query_netdevs);
+
+    ret = g_test_run();
+
+    return ret;
+}