]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/execute_tests.c
Add the blackbox channel connection tests.
[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                 node_reachable_status[i] = false;
39         }
40
41         setup_containers(state);
42
43         return EXIT_SUCCESS;
44 }
45
46 void execute_test(test_step_func_t step_func, void **state) {
47         black_box_state_t *test_state = (black_box_state_t *)(*state);
48
49         fprintf(stderr, "\n\x1b[32mRunning Test\x1b[0m : \x1b[34m%s\x1b[0m\n", test_state->test_case_name);
50         test_state->test_result = step_func();
51
52         if(!test_state->test_result) {
53                 fail();
54         }
55 }
56
57 int teardown_test(void **state) {
58         black_box_state_t *test_state = (black_box_state_t *)(*state);
59         char container_old_name[100], container_new_name[100];
60         int i;
61
62         if(test_state->test_result) {
63                 PRINT_TEST_CASE_MSG("Test successful! Shutting down nodes.\n");
64
65                 for(i = 0; i < test_state->num_nodes; i++) {
66                         /* Shut down node */
67                         node_step_in_container(test_state->node_names[i], "SIGTERM");
68                         /* Rename Container to run_<node-name> - this allows it to be re-used for the
69                             next test, otherwise it will be ignored assuming that it has been saved
70                             for debugging */
71                         assert(snprintf(container_old_name, sizeof(container_old_name), "%s_%s",
72                                         test_state->test_case_name, test_state->node_names[i]) >= 0);
73                         assert(snprintf(container_new_name, sizeof(container_new_name), "run_%s",
74                                         test_state->node_names[i]) >= 0);
75                         rename_container(container_old_name, container_new_name);
76                 }
77         }
78
79         state_ptr = NULL;
80
81         return EXIT_SUCCESS;
82 }