Debian Codegraph @

test/integration/test_server.c

raw
1 #include "../../include/cowsql.h"
2 #include "../../src/server.h"
3 #include "../lib/fs.h"
4 #include "../lib/munit.h"
5 #include "../lib/runner.h"
6
7 #include <stdio.h>
8 #include <time.h>
9
10 SUITE(server);
11
12 #define N_SERVERS 3
13
14 struct fixture
15 {
16 char *dirs[N_SERVERS];
17 cowsql_server *servers[N_SERVERS];
18 };
19
20 static void *setup(const MunitParameter params[], void *user_data)
21 {
22 (void)params;
23 (void)user_data;
24 struct fixture *f = munit_malloc(sizeof *f);
25 unsigned i;
26 int rv;
27
28 for (i = 0; i < N_SERVERS; i += 1) {
29 f->dirs[i] = test_dir_setup();
30 rv = cowsql_server_create(f->dirs[i], &f->servers[i]);
31 munit_assert_int(rv, ==, 0);
32 }
33
34 return f;
35 }
36
37 static void teardown(void *data)
38 {
39 struct fixture *f = data;
40 unsigned i;
41
42 for (i = 0; i < N_SERVERS; i += 1) {
43 cowsql_server_destroy(f->servers[i]);
44 test_dir_tear_down(f->dirs[i]);
45 }
46 free(f);
47 }
48
49 #define PREPARE_FILE(i, name, ...) \
50 do { \
51 char path[100]; \
52 snprintf(path, 100, "%s/%s", f->dirs[i], name); \
53 FILE *fp = fopen(path, "w+"); \
54 fprintf(fp, __VA_ARGS__); \
55 fclose(fp); \
56 } while (0)
57
58 #define NODE(x) x
59
60 #define NODE0_ID "3297041220608546238"
61
62 void start_each_server(struct fixture *f)
63 {
64 const char *addrs[] = {"127.0.0.1:8880", "127.0.0.1:8881"};
65 int rv;
66
67 rv = cowsql_server_set_address(f->servers[0], "127.0.0.1:8880");
68 munit_assert_int(rv, ==, 0);
69 rv = cowsql_server_set_auto_bootstrap(f->servers[0], true);
70 munit_assert_int(rv, ==, 0);
71 f->servers[0]->refresh_period = 100;
72 rv = cowsql_server_start(f->servers[0]);
73 munit_assert_int(rv, ==, 0);
74
75 rv = cowsql_server_set_address(f->servers[1], "127.0.0.1:8881");
76 munit_assert_int(rv, ==, 0);
77 rv = cowsql_server_set_auto_join(f->servers[1], addrs, 1);
78 munit_assert_int(rv, ==, 0);
79 f->servers[1]->refresh_period = 100;
80 rv = cowsql_server_start(f->servers[1]);
81 munit_assert_int(rv, ==, 0);
82
83 rv = cowsql_server_set_address(f->servers[2], "127.0.0.1:8882");
84 munit_assert_int(rv, ==, 0);
85 rv = cowsql_server_set_auto_join(f->servers[2], addrs, 2);
86 munit_assert_int(rv, ==, 0);
87 f->servers[2]->refresh_period = 100;
88 rv = cowsql_server_start(f->servers[2]);
89 munit_assert_int(rv, ==, 0);
90 }
91
92 void stop_each_server(struct fixture *f)
93 {
94 int rv;
95
96 rv = cowsql_server_stop(f->servers[2]);
97 munit_assert_int(rv, ==, 0);
98 rv = cowsql_server_stop(f->servers[1]);
99 munit_assert_int(rv, ==, 0);
100 rv = cowsql_server_stop(f->servers[0]);
101 munit_assert_int(rv, ==, 0);
102 }
103
104 TEST(server, restart_follower, setup, teardown, 0, NULL)
105 {
106 struct fixture *f = data;
107 struct timespec ts = {0};
108 int rv;
109
110 /* Between operations we sleep for 200 milliseconds, twice
111 * the configured refresh period, so that the refresh task
112 * has a chance to be triggered. */
113 ts.tv_nsec = 200 * 1000 * 1000;
114
115 start_each_server(f);
116
117 nanosleep(&ts, NULL);
118
119 rv = cowsql_server_stop(f->servers[1]);
120 munit_assert_int(rv, ==, 0);
121
122 nanosleep(&ts, NULL);
123
124 rv = cowsql_server_start(f->servers[1]);
125 munit_assert_int(rv, ==, 0);
126
127 nanosleep(&ts, NULL);
128
129 stop_each_server(f);
130
131 return MUNIT_OK;
132 }
133
134 TEST(server, restart_leader, setup, teardown, 0, NULL)
135 {
136 struct fixture *f = data;
137 struct timespec ts = {0};
138 int rv;
139
140 /* Between operations we sleep for 200 milliseconds, twice
141 * the configured refresh period, so that the refresh task
142 * has a chance to be triggered. */
143 ts.tv_nsec = 200 * 1000 * 1000;
144
145 start_each_server(f);
146
147 nanosleep(&ts, NULL);
148
149 rv = cowsql_server_stop(f->servers[0]);
150 munit_assert_int(rv, ==, 0);
151
152 nanosleep(&ts, NULL);
153
154 rv = cowsql_server_start(f->servers[0]);
155 munit_assert_int(rv, ==, 0);
156
157 nanosleep(&ts, NULL);
158
159 stop_each_server(f);
160
161 return MUNIT_OK;
162 }
163
164 TEST(server, bad_info_file, setup, teardown, 0, NULL)
165 {
166 struct fixture *f = data;
167 int rv;
168
169 PREPARE_FILE(NODE(0), "server-info", "blah");
170
171 rv = cowsql_server_set_address(f->servers[0], "127.0.0.1:8880");
172 munit_assert_int(rv, ==, 0);
173 rv = cowsql_server_set_auto_bootstrap(f->servers[0], true);
174 munit_assert_int(rv, ==, 0);
175 rv = cowsql_server_start(f->servers[0]);
176 munit_assert_int(rv, !=, 0);
177
178 return MUNIT_OK;
179 }
180
181 TEST(server, bad_node_store, setup, teardown, 0, NULL)
182 {
183 struct fixture *f = data;
184 int rv;
185
186 PREPARE_FILE(NODE(0), "server-info",
187 "v1\n127.0.0.1:8880\n" NODE0_ID "\n");
188 PREPARE_FILE(NODE(0), "node-store", "blah");
189
190 rv = cowsql_server_set_address(f->servers[0], "127.0.0.1:8880");
191 munit_assert_int(rv, ==, 0);
192 rv = cowsql_server_set_auto_bootstrap(f->servers[0], true);
193 munit_assert_int(rv, ==, 0);
194 rv = cowsql_server_start(f->servers[0]);
195 munit_assert_int(rv, !=, 0);
196
197 return MUNIT_OK;
198 }
199
200 TEST(server, node_store_but_no_info, setup, teardown, 0, NULL)
201 {
202 struct fixture *f = data;
203 int rv;
204
205 PREPARE_FILE(NODE(0), "node-store",
206 "v1\n127.0.0.1:8880\n" NODE0_ID "\nvoter\n");
207
208 rv = cowsql_server_set_address(f->servers[0], "127.0.0.1:8880");
209 munit_assert_int(rv, ==, 0);
210 rv = cowsql_server_set_auto_bootstrap(f->servers[0], true);
211 munit_assert_int(rv, ==, 0);
212 rv = cowsql_server_start(f->servers[0]);
213 munit_assert_int(rv, !=, 0);
214
215 return MUNIT_OK;
216 }
217
218 TEST(server, missing_bootstrap, setup, teardown, 0, NULL)
219 {
220 struct fixture *f = data;
221 const char *addrs[] = {"127.0.0.1:8880"};
222 int rv;
223
224 rv = cowsql_server_set_address(f->servers[1], "127.0.0.1:8881");
225 munit_assert_int(rv, ==, 0);
226 rv = cowsql_server_set_auto_join(f->servers[1], addrs, 1);
227 munit_assert_int(rv, ==, 0);
228 rv = cowsql_server_start(f->servers[1]);
229 munit_assert_int(rv, !=, 0);
230
231 return MUNIT_OK;
232 }
233
234 TEST(server, start_twice, setup, teardown, 0, NULL)
235 {
236 struct fixture *f = data;
237 int rv;
238
239 rv = cowsql_server_set_address(f->servers[0], "127.0.0.1:8880");
240 munit_assert_int(rv, ==, 0);
241 rv = cowsql_server_set_auto_bootstrap(f->servers[0], true);
242 munit_assert_int(rv, ==, 0);
243 rv = cowsql_server_start(f->servers[0]);
244 munit_assert_int(rv, ==, 0);
245 rv = cowsql_server_start(f->servers[0]);
246 munit_assert_int(rv, !=, 0);
247 rv = cowsql_server_stop(f->servers[0]);
248 munit_assert_int(rv, ==, 0);
249
250 return MUNIT_OK;
251 }
252
253 TEST(server, stop_twice, setup, teardown, 0, NULL)
254 {
255 struct fixture *f = data;
256 int rv;
257
258 rv = cowsql_server_set_address(f->servers[0], "127.0.0.1:8880");
259 munit_assert_int(rv, ==, 0);
260 rv = cowsql_server_set_auto_bootstrap(f->servers[0], true);
261 munit_assert_int(rv, ==, 0);
262 rv = cowsql_server_start(f->servers[0]);
263 munit_assert_int(rv, ==, 0);
264 rv = cowsql_server_stop(f->servers[0]);
265 munit_assert_int(rv, ==, 0);
266 rv = cowsql_server_stop(f->servers[0]);
267 munit_assert_int(rv, !=, 0);
268
269 return MUNIT_OK;
270 }