]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/execute_tests.c
Ensure NDEBUG is not set in the test suite.
[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 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <setjmp.h>
27 #include <cmocka.h>
28 #include <assert.h>
29 #include "execute_tests.h"
30 #include "../common/common_handlers.h"
31 #include "../common/containers.h"
32 #include "../common/test_step.h"
33
34 int setup_test(void **state) {
35         int i;
36
37         fprintf(stderr, "Setting up Containers\n");
38         state_ptr = (black_box_state_t *)(*state);
39
40         for(i = 0; i < state_ptr->num_nodes; i++) {
41                 meta_conn_status[i] = false;
42         }
43
44         setup_containers(state);
45
46         return EXIT_SUCCESS;
47 }
48
49 void execute_test(test_step_func_t step_func, void **state) {
50         black_box_state_t *test_state = (black_box_state_t *)(*state);
51
52         fprintf(stderr, "\n\x1b[32mRunning Test\x1b[0m : \x1b[34m%s\x1b[0m\n", test_state->test_case_name);
53         test_state->test_result = step_func();
54
55         if(!test_state->test_result) {
56                 fail();
57         }
58 }
59
60 int teardown_test(void **state) {
61         black_box_state_t *test_state = (black_box_state_t *)(*state);
62         char container_old_name[100], container_new_name[100];
63         int i;
64
65         if(test_state->test_result) {
66                 PRINT_TEST_CASE_MSG("Test successful! Shutting down nodes.\n");
67
68                 for(i = 0; i < test_state->num_nodes; i++) {
69                         /* Shut down node */
70                         node_step_in_container(test_state->node_names[i], "SIGTERM");
71                         /* Rename Container to run_<node-name> - this allows it to be re-used for the
72                             next test, otherwise it will be ignored assuming that it has been saved
73                             for debugging */
74                         assert(snprintf(container_old_name, sizeof(container_old_name), "%s_%s",
75                                         test_state->test_case_name, test_state->node_names[i]) >= 0);
76                         assert(snprintf(container_new_name, sizeof(container_new_name), "run_%s",
77                                         test_state->node_names[i]) >= 0);
78                         rename_container(container_old_name, container_new_name);
79                 }
80         }
81
82         state_ptr = NULL;
83
84         return EXIT_SUCCESS;
85 }
86
87 bool change_state(node_status_t *status, mesh_event_t currentEv) {
88
89         if(status->current_index == status->max_events) {
90                 return false;
91         }
92
93         if(currentEv == status->expected_events[status->current_index]) {
94                 status->current_index = status->current_index + 1;
95
96                 return true;
97         } else {
98                 return false;
99         }
100 }
101
102 void signal_node_start(node_status_t *node_status, int start, int end, char *node_ids[]) {
103         int i, index;
104
105         for(i = start; i <= end; i++) {
106                 index = node_status[i].current_index;
107
108                 if(index < 1 || NODE_JOINED != node_status[i].expected_events[index - 1]) {
109                         return;
110                 }
111         }
112
113
114         for(i = start; i <= end; i++) {
115                 fprintf(stderr, "\tSending signals to '%s'\n", node_ids[i]);
116                 node_step_in_container(node_ids[i], "SIGIO");
117         }
118
119         return;
120 }
121
122 bool check_nodes_finished(node_status_t *node_status, int length) {
123         for(int i = 0; i < length; i++) {
124                 if(node_status[i].current_index != node_status[i].max_events) {
125                         return false;
126                 }
127         }
128
129         return true;
130 }