| 1 | /* |
| 2 | * libqos driver framework |
| 3 | * |
| 4 | * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License version 2.1 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, see <http://www.gnu.org/licenses/> |
| 17 | */ |
| 18 | |
| 19 | #include "qemu/osdep.h" |
| 20 | #include <getopt.h> |
| 21 | #include "libqtest-single.h" |
| 22 | #include "qapi/error.h" |
| 23 | #include "qobject/qdict.h" |
| 24 | #include "qemu/module.h" |
| 25 | #include "qapi/qobject-input-visitor.h" |
| 26 | #include "qapi/qapi-visit-machine.h" |
| 27 | #include "qapi/qapi-visit-qom.h" |
| 28 | #include "libqos/libqos-malloc.h" |
| 29 | #include "libqos/qgraph.h" |
| 30 | #include "libqos/qgraph_internal.h" |
| 31 | #include "libqos/qos_external.h" |
| 32 | |
| 33 | static char *old_path; |
| 34 | static GSList *path_vecs; |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * qos_set_machines_devices_available(): sets availability of qgraph |
| 39 | * machines and devices. |
| 40 | * |
| 41 | * This function firstly starts QEMU with "-machine none" option, |
| 42 | * and then executes the QMP protocol asking for the list of devices |
| 43 | * and machines available. |
| 44 | * |
| 45 | * for each of these items, it looks up the corresponding qgraph node, |
| 46 | * setting it as available. The list currently returns all devices that |
| 47 | * are either machines or QEDGE_CONSUMED_BY other nodes. |
| 48 | * Therefore, in order to mark all other nodes, it recursively sets |
| 49 | * all its QEDGE_CONTAINS and QEDGE_PRODUCES child as available too. |
| 50 | */ |
| 51 | static void qos_set_machines_devices_available(void) |
| 52 | { |
| 53 | QDict *response; |
| 54 | QDict *args = qdict_new(); |
| 55 | QObject *ret; |
| 56 | Visitor *v; |
| 57 | MachineInfoList *mach_info; |
| 58 | ObjectTypeInfoList *type_info; |
| 59 | |
| 60 | qtest_start("-machine none"); |
| 61 | response = qmp("{ 'execute': 'query-machines' }"); |
| 62 | ret = qdict_get(response, "return"); |
| 63 | |
| 64 | v = qobject_input_visitor_new(ret); |
| 65 | visit_type_MachineInfoList(v, NULL, &mach_info, &error_abort); |
| 66 | visit_free(v); |
| 67 | machines_apply_to_node(mach_info); |
| 68 | qapi_free_MachineInfoList(mach_info); |
| 69 | |
| 70 | qobject_unref(response); |
| 71 | |
| 72 | qdict_put_bool(args, "abstract", true); |
| 73 | qdict_put_str(args, "implements", "device"); |
| 74 | |
| 75 | response = qmp("{'execute': 'qom-list-types'," |
| 76 | " 'arguments': %p }", args); |
| 77 | ret = qdict_get(response, "return"); |
| 78 | |
| 79 | v = qobject_input_visitor_new(ret); |
| 80 | visit_type_ObjectTypeInfoList(v, NULL, &type_info, &error_abort); |
| 81 | visit_free(v); |
| 82 | types_apply_to_node(type_info); |
| 83 | qapi_free_ObjectTypeInfoList(type_info); |
| 84 | |
| 85 | qtest_end(); |
| 86 | qobject_unref(response); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | static void restart_qemu_or_continue(char *path) |
| 91 | { |
| 92 | if (g_test_verbose()) { |
| 93 | qos_printf("Run QEMU with: '%s'\n", path); |
| 94 | } |
| 95 | /* compares the current command line with the |
| 96 | * one previously executed: if they are the same, |
| 97 | * don't restart QEMU, if they differ, stop previous |
| 98 | * QEMU subprocess (if active) and start over with |
| 99 | * the new command line |
| 100 | */ |
| 101 | if (g_strcmp0(old_path, path)) { |
| 102 | qtest_end(); |
| 103 | qos_invalidate_command_line(); |
| 104 | old_path = g_strdup(path); |
| 105 | qtest_start(path); |
| 106 | } else { /* if cmd line is the same, reset the guest */ |
| 107 | qtest_system_reset(global_qtest); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void qos_invalidate_command_line(void) |
| 112 | { |
| 113 | g_free(old_path); |
| 114 | old_path = NULL; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /* The argument to run_one_test, which is the test function that is registered |
| 119 | * with GTest, is a vector of strings. The first item is the initial command |
| 120 | * line (before it is modified by the test's "before" function), the remaining |
| 121 | * items are node names forming the path to the test node. |
| 122 | */ |
| 123 | static char **current_path; |
| 124 | |
| 125 | const char *qos_get_current_command_line(void) |
| 126 | { |
| 127 | return current_path[0]; |
| 128 | } |
| 129 | |
| 130 | void *qos_allocate_objects(QTestState *qts, QGuestAllocator **p_alloc) |
| 131 | { |
| 132 | return allocate_objects(qts, current_path + 1, p_alloc); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * run_one_test(): given an array of nodes @arg, |
| 137 | * walks the path invoking all constructors and |
| 138 | * passing the corresponding parameter in order to |
| 139 | * continue the objects allocation. |
| 140 | * Once the test is reached, its function is executed. |
| 141 | * |
| 142 | * Since the machine and QEDGE_CONSUMED_BY nodes allocate |
| 143 | * memory in the constructor, g_test_queue_destroy is used so |
| 144 | * that after execution they can be safely free'd. The test's |
| 145 | * ->before callback is also welcome to use g_test_queue_destroy. |
| 146 | * |
| 147 | * Note: as specified in walk_path() too, @arg is an array of |
| 148 | * char *, where arg[0] is a pointer to the command line |
| 149 | * string that will be used to properly start QEMU when executing |
| 150 | * the test, and the remaining elements represent the actual objects |
| 151 | * that will be allocated. |
| 152 | * |
| 153 | * The order of execution is the following: |
| 154 | * 1) @before test function as defined in the given QOSGraphTestOptions |
| 155 | * 2) start QEMU |
| 156 | * 3) call all nodes constructor and get_driver/get_device depending on edge, |
| 157 | * start the hardware (*_device_enable functions) |
| 158 | * 4) start test |
| 159 | */ |
| 160 | static void run_one_test(const void *arg) |
| 161 | { |
| 162 | QOSGraphNode *test_node; |
| 163 | QGuestAllocator *alloc = NULL; |
| 164 | void *obj; |
| 165 | char **path = (char **) arg; |
| 166 | GString *cmd_line = g_string_new(path[0]); |
| 167 | void *test_arg; |
| 168 | |
| 169 | /* Before test */ |
| 170 | current_path = path; |
| 171 | test_node = qos_graph_get_node(path[(g_strv_length(path) - 1)]); |
| 172 | test_arg = test_node->u.test.arg; |
| 173 | if (test_node->u.test.before) { |
| 174 | test_arg = test_node->u.test.before(cmd_line, test_arg); |
| 175 | } |
| 176 | |
| 177 | restart_qemu_or_continue(cmd_line->str); |
| 178 | g_string_free(cmd_line, true); |
| 179 | |
| 180 | obj = qos_allocate_objects(global_qtest, &alloc); |
| 181 | test_node->u.test.function(obj, test_arg, alloc); |
| 182 | } |
| 183 | |
| 184 | static void subprocess_run_one_test(const void *arg) |
| 185 | { |
| 186 | char **path_vec = (char **) arg; |
| 187 | gchar *path = g_strjoinv("/", path_vec + 1); |
| 188 | gchar *subprocess_path = g_strdup_printf("/%s/subprocess", path); |
| 189 | |
| 190 | g_test_trap_subprocess(subprocess_path, 180 * G_USEC_PER_SEC, |
| 191 | G_TEST_SUBPROCESS_INHERIT_STDOUT | |
| 192 | G_TEST_SUBPROCESS_INHERIT_STDERR); |
| 193 | g_test_trap_assert_passed(); |
| 194 | g_free(path); |
| 195 | g_free(subprocess_path); |
| 196 | } |
| 197 | |
| 198 | static void destroy_pathv(void *arg) |
| 199 | { |
| 200 | g_free(((char **)arg)[0]); |
| 201 | g_free(arg); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * in this function, 2 path will be built: |
| 206 | * path_str, a one-string path (ex "pc/i440FX-pcihost/...") |
| 207 | * path_vec, a string-array path (ex [0] = "pc", [1] = "i440FX-pcihost"). |
| 208 | * |
| 209 | * path_str will be only used to build the test name, and won't need the |
| 210 | * architecture name at beginning, since it will be added by qtest_add_func(). |
| 211 | * |
| 212 | * path_vec is used to allocate all constructors of the path nodes. |
| 213 | * Each name in this array except position 0 must correspond to a valid |
| 214 | * QOSGraphNode name. |
| 215 | * Position 0 is special, initially contains just the <machine> name of |
| 216 | * the node, (ex for "x86_64/pc" it will be "pc"), used to build the test |
| 217 | * path (see below). After it will contain the command line used to start |
| 218 | * qemu with all required devices. |
| 219 | * |
| 220 | * Note that the machine node name must be with format <arch>/<machine> |
| 221 | * (ex "x86_64/pc"), because it will identify the node "x86_64/pc" |
| 222 | * and start QEMU with "-M pc". For this reason, |
| 223 | * when building path_str, path_vec |
| 224 | * initially contains the <machine> at position 0 ("pc"), |
| 225 | * and the node name at position 1 (<arch>/<machine>) |
| 226 | * ("x86_64/pc"), followed by the rest of the nodes. |
| 227 | */ |
| 228 | static void walk_path(QOSGraphNode *orig_path, int len) |
| 229 | { |
| 230 | QOSGraphNode *path; |
| 231 | QOSGraphEdge *edge; |
| 232 | |
| 233 | /* etype set to QEDGE_CONSUMED_BY so that machine can add to the command line */ |
| 234 | QOSEdgeType etype = QEDGE_CONSUMED_BY; |
| 235 | |
| 236 | /* twice QOS_PATH_MAX_ELEMENT_SIZE since each edge can have its arg */ |
| 237 | char **path_vec = g_new0(char *, (QOS_PATH_MAX_ELEMENT_SIZE * 2)); |
| 238 | int path_vec_size = 0; |
| 239 | |
| 240 | char *after_cmd, *before_cmd, *after_device; |
| 241 | GString *after_device_str = g_string_new(""); |
| 242 | char *node_name = orig_path->name, *path_str; |
| 243 | |
| 244 | GString *cmd_line = g_string_new(""); |
| 245 | GString *cmd_line2 = g_string_new(""); |
| 246 | |
| 247 | path_vecs = g_slist_append(path_vecs, path_vec); |
| 248 | path = qos_graph_get_node(node_name); /* root */ |
| 249 | node_name = qos_graph_edge_get_dest(path->path_edge); /* machine name */ |
| 250 | |
| 251 | path_vec[path_vec_size++] = node_name; |
| 252 | path_vec[path_vec_size++] = qos_get_machine_type(node_name); |
| 253 | |
| 254 | for (;;) { |
| 255 | path = qos_graph_get_node(node_name); |
| 256 | if (!path->path_edge) { |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | node_name = qos_graph_edge_get_dest(path->path_edge); |
| 261 | |
| 262 | /* append node command line + previous edge command line */ |
| 263 | if (path->command_line && etype == QEDGE_CONSUMED_BY) { |
| 264 | g_string_append(cmd_line, path->command_line); |
| 265 | g_string_append(cmd_line, after_device_str->str); |
| 266 | g_string_truncate(after_device_str, 0); |
| 267 | } |
| 268 | |
| 269 | path_vec[path_vec_size++] = qos_graph_edge_get_name(path->path_edge); |
| 270 | /* detect if edge has command line args */ |
| 271 | after_cmd = qos_graph_edge_get_after_cmd_line(path->path_edge); |
| 272 | after_device = qos_graph_edge_get_extra_device_opts(path->path_edge); |
| 273 | before_cmd = qos_graph_edge_get_before_cmd_line(path->path_edge); |
| 274 | edge = qos_graph_get_edge(path->name, node_name); |
| 275 | etype = qos_graph_edge_get_type(edge); |
| 276 | |
| 277 | if (before_cmd) { |
| 278 | g_string_append(cmd_line, before_cmd); |
| 279 | } |
| 280 | if (after_cmd) { |
| 281 | g_string_append(cmd_line2, after_cmd); |
| 282 | } |
| 283 | if (after_device) { |
| 284 | g_string_append(after_device_str, after_device); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | path_vec[path_vec_size++] = NULL; |
| 289 | g_string_append(cmd_line, after_device_str->str); |
| 290 | g_string_free(after_device_str, true); |
| 291 | |
| 292 | g_string_append(cmd_line, cmd_line2->str); |
| 293 | g_string_free(cmd_line2, true); |
| 294 | |
| 295 | /* here position 0 has <arch>/<machine>, position 1 has <machine>. |
| 296 | * The path must not have the <arch>, qtest_add_data_func adds it. |
| 297 | */ |
| 298 | path_str = g_strjoinv("/", path_vec + 1); |
| 299 | |
| 300 | /* put arch/machine in position 1 so run_one_test can do its work |
| 301 | * and add the command line at position 0. |
| 302 | */ |
| 303 | path_vec[1] = path_vec[0]; |
| 304 | path_vec[0] = g_string_free(cmd_line, false); |
| 305 | |
| 306 | if (path->u.test.subprocess) { |
| 307 | gchar *subprocess_path = g_strdup_printf("%s/%s", path_str, |
| 308 | "subprocess"); |
| 309 | |
| 310 | qtest_add_data_func(path_str, path_vec, subprocess_run_one_test); |
| 311 | qtest_add_data_func(subprocess_path, path_vec, run_one_test); |
| 312 | |
| 313 | g_free(subprocess_path); |
| 314 | } else { |
| 315 | qtest_add_data_func(path_str, path_vec, run_one_test); |
| 316 | } |
| 317 | |
| 318 | g_free(path_str); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |
| 323 | /** |
| 324 | * main(): heart of the qgraph framework. |
| 325 | * |
| 326 | * - Initializes the glib test framework |
| 327 | * - Creates the graph by invoking the various _init constructors |
| 328 | * - Starts QEMU to mark the available devices |
| 329 | * - Walks the graph, and each path is added to |
| 330 | * the glib test framework (walk_path) |
| 331 | * - Runs the tests, calling allocate_object() and allocating the |
| 332 | * machine/drivers/test objects |
| 333 | * - Cleans up everything |
| 334 | */ |
| 335 | int main(int argc, char **argv, char** envp) |
| 336 | { |
| 337 | g_test_init(&argc, &argv, NULL); |
| 338 | if (g_test_verbose()) { |
| 339 | qos_printf("ENVIRONMENT VARIABLES: {\n"); |
| 340 | for (char **env = envp; *env != 0; env++) { |
| 341 | qos_printf("\t%s\n", *env); |
| 342 | } |
| 343 | qos_printf("}\n"); |
| 344 | } |
| 345 | qos_graph_init(); |
| 346 | module_call_init(MODULE_INIT_QOM); |
| 347 | module_call_init(MODULE_INIT_LIBQOS); |
| 348 | qos_set_machines_devices_available(); |
| 349 | |
| 350 | /* |
| 351 | * Even if this invocation was done to run a single test in a |
| 352 | * subprocess (i.e. g_test_subprocess() is true), gtester doesn't |
| 353 | * expose the test name, so w still need to execute the whole |
| 354 | * thing as normal, including walking the QOS graph to add all |
| 355 | * the tests, in order for g_test_run() to find the one /subprocess |
| 356 | * test that it is going to execute. |
| 357 | */ |
| 358 | qos_graph_foreach_test_path(walk_path); |
| 359 | if (g_test_verbose()) { |
| 360 | qos_dump_graph(); |
| 361 | } |
| 362 | g_test_run(); |
| 363 | qtest_end(); |
| 364 | qos_graph_destroy(); |
| 365 | g_free(old_path); |
| 366 | g_slist_free_full(path_vecs, (GDestroyNotify)destroy_pathv); |
| 367 | return 0; |
| 368 | } |