From nobody Tue Nov 4 22:00:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1531127872492831.8056094756763; Mon, 9 Jul 2018 02:17:52 -0700 (PDT) Received: from localhost ([::1]:40010 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fcSIn-0000hA-Ui for importer@patchew.org; Mon, 09 Jul 2018 05:17:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38468) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fcSE3-0005Tg-Ul for qemu-devel@nongnu.org; Mon, 09 Jul 2018 05:12:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fcSE1-0007Kg-VU for qemu-devel@nongnu.org; Mon, 09 Jul 2018 05:12:55 -0400 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]:40822) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fcSE1-0007K5-KF for qemu-devel@nongnu.org; Mon, 09 Jul 2018 05:12:53 -0400 Received: by mail-ed1-x541.google.com with SMTP id e19-v6so13341184edq.7 for ; Mon, 09 Jul 2018 02:12:53 -0700 (PDT) Received: from localhost.localdomain ([194.230.159.64]) by smtp.gmail.com with ESMTPSA id c6-v6sm6865091edt.41.2018.07.09.02.12.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Jul 2018 02:12:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xPlnjz0xU33vk3wXlhFlf0VVxHs5haKv+NiZ8a+Tb10=; b=lLM0e3iNG2NvPL6YapipAk5PuekGDR7J2SlkbPYIZQwty5XC0pSCfRT2I9MRqCgsIn VruPpVZs+YEClk/fYsxiM9WHD/ELYj+NVwndsYziMv/YX6a4S8XDIibUC1wXrkF4iUOw GaqtWDwt58l+QT1hUKrkZo8MwWvNEC+qetIFCxbRJ883VnmKEe/KA+Ev1udr571fDrzt Z6S9nZQJm5Dh73jVYCkYpEpl2+QAJAKuDGFpcYcJR4TOThrYqZfLSKTokFU4Al5XNkPm 0u9XSBlF8j3TcPEXwBMJgpq13OZGleImleTvPZb9gRGR5v1/tjo2rAZ2BhpTCTTYnME7 A+ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xPlnjz0xU33vk3wXlhFlf0VVxHs5haKv+NiZ8a+Tb10=; b=pX45gppKS+gcN+29pkpSoclNglCJ+OAc6mfcBeIE2/caD5mTNVYQCy25kYPhbl/zGP J4l0akny2toOUJgYeYXiNtFPRC1pgr5W+Xqtk6y3V2nFCOnqYv2WynU2NjpFDCVUWuMF c1FCN6WOjpuCZwZU/hJvev1MMxRdXF7moYnp0sxXzwcVj9IhHxloVxbtUWtY2xCD79Vo 4GVjwW+rpqmcxK/Y2XQblG/iFVj2UEDdvKKQ+7OIxbGhyexKHPzXwVD4enVTlV3gTNYD vre28+Jf4yRnr5IB85I6jkZN8hQqsAFXSw3jSey2HxxS5HQcF8zwiYKxrTaYs5bqTfAH Sp3Q== X-Gm-Message-State: APt69E2Cw5oLRkF+y13Bzc43BVQUHc8eaw6660Ob197qxqYVGMgfgpvo Xu1DkajyYELz9ZC0KO+wQzQ= X-Google-Smtp-Source: AAOMgpeE7rDVlmxdfx14FVk0B56JAi4EcwNhnvJJyhpNl65m5UVpcPbS8XzJbXPFyJ0pjgp+fORSbA== X-Received: by 2002:a50:8327:: with SMTP id 36-v6mr1606704edh.305.1531127572508; Mon, 09 Jul 2018 02:12:52 -0700 (PDT) From: Emanuele Giuseppe Esposito To: Paolo Bonzini Date: Mon, 9 Jul 2018 11:11:35 +0200 Message-Id: <20180709091136.28849-7-e.emanuelegiuseppe@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180709091136.28849-1-e.emanuelegiuseppe@gmail.com> References: <20180709091136.28849-1-e.emanuelegiuseppe@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::541 Subject: [Qemu-devel] [PATCH 6/7] tests/qgraph: gtest integration X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Emanuele Giuseppe Esposito , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add main executable that takes care of starting the framework, create the nodes, set the available drivers/machines, discover the path and run tests. Signed-off-by: Emanuele Giuseppe Esposito --- tests/Makefile.include | 3 + tests/qos-test.c | 310 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 313 insertions(+) create mode 100644 tests/qos-test.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 706ac39ea5..e2db3e9d09 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -776,6 +776,9 @@ libqgraph-pc-obj-y +=3D tests/libqos/raspi2-machine.o t= ests/libqos/x86_64_pc-machi check-unit-y +=3D tests/test-qgraph$(EXESUF) tests/test-qgraph$(EXESUF): tests/test-qgraph.o $(libqgraph-obj-y) =20 +check-qtest-pci-y +=3D tests/qos-test$(EXESUF) +tests/qos-test$(EXESUF): tests/qos-test.o $(libqgraph-pc-obj-y) + tests/qmp-test$(EXESUF): tests/qmp-test.o tests/device-introspect-test$(EXESUF): tests/device-introspect-test.o tests/rtc-test$(EXESUF): tests/rtc-test.o diff --git a/tests/qos-test.c b/tests/qos-test.c new file mode 100644 index 0000000000..f4748c44a4 --- /dev/null +++ b/tests/qos-test.c @@ -0,0 +1,310 @@ +/* + * libqos driver framework + * + * Copyright (c) 2018 Emanuele Giuseppe Esposito + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + */ + +#include "qemu/osdep.h" +#include "libqtest.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qstring.h" +#include "qapi/qmp/qlist.h" +#include "libqos/qgraph.h" +#include "libqos/qgraph_extra.h" + +/** + * create_machine_name(): appends the architecture to @name if + * @is_machine is valid. + */ +static void create_machine_name(const char **name, bool is_machine) +{ + const char *arch; + if (!is_machine) { + return; + } + arch =3D qtest_get_arch(); + *name =3D g_strconcat(arch, "/", *name, NULL); +} + +/** + * destroy_machine_name(): frees the given @name if + * @is_machine is valid. + */ +static void destroy_machine_name(const char *name, bool is_machine) +{ + if (!is_machine) { + return; + } + g_free((char *)name); +} + +/** + * apply_to_qlist(): using QMP queries QEMU for a list of + * machines and devices available, and sets the respective node + * as TRUE. If a node is found, also all its produced and contained + * child are marked available. + * + * See qos_graph_node_set_availability() for more info + */ +static void apply_to_qlist(QList *list, bool is_machine) +{ + const QListEntry *p; + const char *name; + QDict *minfo; + QObject *qobj; + QString *qstr; + + for (p =3D qlist_first(list); p; p =3D qlist_next(p)) { + minfo =3D qobject_to(QDict, qlist_entry_obj(p)); + g_assert(minfo); + qobj =3D qdict_get(minfo, "name"); + g_assert(qobj); + qstr =3D qobject_to(QString, qobj); + g_assert(qstr); + name =3D qstring_get_str(qstr); + create_machine_name(&name, is_machine); + qos_graph_node_set_availability(name, TRUE); + destroy_machine_name(name, is_machine); + qobj =3D qdict_get(minfo, "alias"); + if (qobj) { + g_assert(qobj); + qstr =3D qobject_to(QString, qobj); + g_assert(qstr); + name =3D qstring_get_str(qstr); + create_machine_name(&name, is_machine); + qos_graph_node_set_availability(name, TRUE); + destroy_machine_name(name, is_machine); + } + } +} + +/** + * qos_set_machines_devices_available(): sets availability of qgraph + * machines and devices. + * + * This function firstly starts QEMU with "-machine none" option, + * and then executes the QMP protocol asking for the list of devices + * and machines available. + * + * for each of these items, it looks up the corresponding qgraph node, + * setting it as available. The list currently returns all devices that + * are either machines or CONSUMED_BY other nodes. + * Therefore, in order to mark all other nodes, it recursively sets + * all its CONTAINS and PRODUCES child as available too. + */ +static void qos_set_machines_devices_available(void) +{ + QDict *response; + QDict *args =3D qdict_new(); + QList *list; + + qtest_start("-machine none"); + response =3D qmp("{ 'execute': 'query-machines' }"); + g_assert(response); + list =3D qdict_get_qlist(response, "return"); + g_assert(list); + + apply_to_qlist(list, TRUE); + + qobject_unref(response); + + qdict_put_bool(args, "abstract", TRUE); + qdict_put_str(args, "implements", "device"); + + response =3D qmp("{'execute': 'qom-list-types'," + " 'arguments': %p }", args); + g_assert(qdict_haskey(response, "return")); + list =3D qdict_get_qlist(response, "return"); + + apply_to_qlist(list, FALSE); + + qtest_end(); + qobject_unref(response); + +} + +/** + * allocate_objects(): given an array of nodes @arg, + * walks the path invoking all constructors and + * passing the corresponding parameter in order to + * continue the objects allocation. + * Once the test is reached, its function is executed. + * + * Since only the machine and CONSUMED_BY nodes actually + * allocate something in the constructor, a garbage collector + * saves their pointer in an array, so that after execution + * they can be safely free'd. + * + * Note: as specified in walk_path() too, @arg is an array of + * char *, where arg[0] is a pointer to the command line + * string that will be used to properly start QEMU when executing + * the test, and the remaining elements represent the actual objects + * that will be allocated. + */ +static void allocate_objects(const void *arg) +{ + QOSGraphObject *garbage_collector[(QOS_PATH_MAX_ELEMENT_SIZE * 2)]; + int garbage_size =3D 0; + QOSEdgeType etype; + QOSGraphNode *node; + QOSGraphObject *o; + int current =3D 1, has_to_allocate =3D 0; + void *obj =3D NULL; + char **path =3D (char **) arg; + + node =3D qos_graph_get_node(path[current]); + + while (current < QOS_PATH_MAX_ELEMENT_SIZE) { + + if (node->type =3D=3D MACHINE) { + global_qtest =3D qtest_start(path[0]); + + obj =3D node->u.machine.constructor(); + garbage_collector[garbage_size++] =3D obj; + + } else if (has_to_allocate && node->type =3D=3D DRIVER) { + obj =3D node->u.driver.constructor(obj, NULL); + garbage_collector[garbage_size++] =3D obj; + + } else if (!path[(current + 1)] && node->type =3D=3D TEST) { + node->u.test.function(obj, node->u.test.arg); + break; + } + + etype =3D qos_graph_get_edge_type(path[current], path[(current + 1= )]); + current++; + node =3D qos_graph_get_node(path[current]); + + o =3D obj; + + switch (etype) { + case PRODUCES: + obj =3D o->get_driver(obj, path[current]); + break; + case CONSUMED_BY: + has_to_allocate =3D 1; + break; + case CONTAINS: + obj =3D o->get_device(obj, path[current]); + break; + } + + } + + g_free(path[0]); + g_free(path); + for (int i =3D 0; i < garbage_size; i++) { + garbage_collector[i]->destructor(garbage_collector[i]); + } + qtest_end(); +} + +/* + * in this function, 2 path will be built: + * str_path, a one-string path (ex "pc/i440FX-pcihost/...") + * ro_path, a string-array path (ex [0] =3D "pc", [1] =3D "i440FX-pcihost"= ). + * + * str_path will be only used to build the test name, and won't need the + * architecture name at beginning, since it will be added by qtest_add_fun= c(). + * + * ro_path is used to allocate all constructors of the path nodes. + * Each name in this array except position 0 must correspond to a valid + * QOSGraphNode name. + * Position 0 is special, initially contains just the name of + * the node, (ex for "x86_64/pc" it will be "pc"), used to build the test + * path (see below). After it will contain the command line used to start + * qemu with all required devices. + * + * Note that the machine node name must be with format / + * (ex "x86_64/pc"), because it will identify the node "x86_64/pc" + * and start QEMU with "-M pc". For this reason, + * when building str_path, ro_path + * initially contains the at position 0 ("pc"), + * and the node name at position 1 (/) + * ("x86_64/pc"), followed by the rest of the nodes. + */ +static void walk_path(QOSGraphNode *orig_path, int len) +{ + QOSGraphNode *path; + /* twice QOS_PATH_MAX_ELEMENT_SIZE since each edge can have its arg */ + char **ro_path =3D g_new0(char *, (QOS_PATH_MAX_ELEMENT_SIZE * 2)); + int ro_path_size =3D 0; + char *machine =3D NULL, *arch =3D NULL, *tmp_cmd =3D NULL, *str_path; + char *tmp =3D orig_path->name, *gfreed; + GString *cmd_line =3D g_string_new(""); + + do { + path =3D qos_graph_get_node(tmp); + tmp =3D qos_graph_get_edge_dest(path->path_edge); + + /* append node command line + previous edge command line */ + if (path->command_line && tmp_cmd) { + g_string_append(cmd_line, path->command_line); + g_string_append_printf(cmd_line, "%s ", tmp_cmd); + } + + if (path->type =3D=3D MACHINE) { + qos_separate_arch_machine(path->name, &arch, &machine); + ro_path[ro_path_size++] =3D arch; + ro_path[ro_path_size++] =3D machine; + g_string_append_printf(cmd_line, "%s ", path->command_line); + } else { + /* detect if edge has command line args */ + ro_path[ro_path_size++] =3D path->name; + tmp_cmd =3D qos_graph_get_edge_arg(path->path_edge); + } + + } while (path->path_edge); + + + /* here position 0 has /, position 1 . + * the path must not have the , that's why ro_path + 1 + */ + str_path =3D g_strjoinv("/", (ro_path + 1)); + gfreed =3D g_string_free(cmd_line, FALSE); + /* put arch/machine in position 1 so allocate_objects can do its work + * and add the command line at position 0. + */ + ro_path[0] =3D g_strdup(gfreed); + ro_path[1] =3D arch; + + qtest_add_data_func(str_path, ro_path, allocate_objects); + g_free(str_path); +} + +/** + * main(): heart of the qgraph framework. + * + * - Initializes the glib test framework + * - Creates the graph by invoking the various _init constructors + * - Starts QEMU to mark the available devices + * - Walks the graph, and each path is added to + * the glib test framework (walk_path) + * - Runs the tests, calling allocate_object() and allocating the + * machine/drivers/test objects + * - Cleans up everything + */ +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + qos_graph_init(); + module_call_init(MODULE_INIT_LIBQOS); + qos_set_machines_devices_available(); + + qos_graph_foreach_test_path(walk_path); + g_test_run(); + qos_graph_destroy(); + return 0; +} --=20 2.17.1