]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/execute_tests.c
Update the blackbox test infrastructure.
[meshlink] / test / blackbox / run_blackbox_tests / execute_tests.c
1 /*
2     execute_tests.c -- Utility functions for black box test execution
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program 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
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <setjmp.h>
23 #include <cmocka.h>
24 #include <assert.h>
25 #include "execute_tests.h"
26 #include "../common/common_handlers.h"
27 #include "../common/containers.h"
28 #include "../common/test_step.h"
29
30 int setup_test(void **state) {
31         int i;
32
33         fprintf(stderr, "Setting up Containers\n");
34         state_ptr = (black_box_state_t *)(*state);
35
36         for(i = 0; i < state_ptr->num_nodes; i++) {
37                 meta_conn_status[i] = false;
38         }
39
40         setup_containers(state);
41
42         return EXIT_SUCCESS;
43 }
44
45 void execute_test(test_step_func_t step_func, void **state) {
46         black_box_state_t *test_state = (black_box_state_t *)(*state);
47
48         fprintf(stderr, "\n\x1b[32mRunning Test\x1b[0m : \x1b[34m%s\x1b[0m\n", test_state->test_case_name);
49         test_state->test_result = step_func();
50
51         if(!test_state->test_result) {
52                 fail();
53         }
54 }
55
56 int teardown_test(void **state) {
57         black_box_state_t *test_state = (black_box_state_t *)(*state);
58         char container_old_name[100], container_new_name[100];
59         int i;
60
61         if(test_state->test_result) {
62                 PRINT_TEST_CASE_MSG("Test successful! Shutting down nodes.\n");
63
64                 for(i = 0; i < test_state->num_nodes; i++) {
65                         /* Shut down node */
66                         node_step_in_container(test_state->node_names[i], "SIGTERM");
67                         /* Rename Container to run_<node-name> - this allows it to be re-used for the
68                             next test, otherwise it will be ignored assuming that it has been saved
69                             for debugging */
70                         assert(snprintf(container_old_name, sizeof(container_old_name), "%s_%s",
71                                         test_state->test_case_name, test_state->node_names[i]) >= 0);
72                         assert(snprintf(container_new_name, sizeof(container_new_name), "run_%s",
73                                         test_state->node_names[i]) >= 0);
74                         rename_container(container_old_name, container_new_name);
75                 }
76         }
77
78         state_ptr = NULL;
79
80         return EXIT_SUCCESS;
81 }