Debian Codegraph @

test/integration/test_node.c

raw
1 #include "../lib/fs.h"
2 #include "../lib/heap.h"
3 #include "../lib/runner.h"
4 #include "../lib/sqlite.h"
5
6 #include "../../include/cowsql.h"
7 #include "../../src/protocol.h"
8 #include "../../src/utils.h"
9
10 /******************************************************************************
11 *
12 * Fixture
13 *
14 ******************************************************************************/
15
16 struct fixture
17 {
18 char *dir; /* Data directory. */
19 cowsql_node *node; /* Node instance. */
20 };
21
22 static void *setUp(const MunitParameter params[], void *user_data)
23 {
24 struct fixture *f = munit_malloc(sizeof *f);
25 int rv;
26 test_heap_setup(params, user_data);
27 test_sqlite_setup(params);
28
29 f->dir = test_dir_setup();
30
31 rv = cowsql_node_create(1, "1", f->dir, &f->node);
32 munit_assert_int(rv, ==, 0);
33
34 rv = cowsql_node_set_bind_address(f->node, "@123");
35 munit_assert_int(rv, ==, 0);
36
37 return f;
38 }
39
40 static void *setUpInet(const MunitParameter params[], void *user_data)
41 {
42 struct fixture *f = munit_malloc(sizeof *f);
43 int rv;
44 test_heap_setup(params, user_data);
45 test_sqlite_setup(params);
46
47 f->dir = test_dir_setup();
48
49 rv = cowsql_node_create(1, "1", f->dir, &f->node);
50 munit_assert_int(rv, ==, 0);
51
52 rv = cowsql_node_set_bind_address(f->node, "127.0.0.1:9001");
53 munit_assert_int(rv, ==, 0);
54
55 return f;
56 }
57
58 /* Tests if node starts/stops successfully and also performs some memory cleanup
59 */
60 static void startStopNode(struct fixture *f)
61 {
62 munit_assert_int(cowsql_node_start(f->node), ==, 0);
63 munit_assert_int(cowsql_node_stop(f->node), ==, 0);
64 }
65
66 /* Recovery only works if a node has been started regularly for a first time. */
67 static void *setUpForRecovery(const MunitParameter params[], void *user_data)
68 {
69 int rv;
70 struct fixture *f = setUp(params, user_data);
71 startStopNode(f);
72 cowsql_node_destroy(f->node);
73
74 rv = cowsql_node_create(1, "1", f->dir, &f->node);
75 munit_assert_int(rv, ==, 0);
76
77 rv = cowsql_node_set_bind_address(f->node, "@123");
78 munit_assert_int(rv, ==, 0);
79
80 return f;
81 }
82
83 static void tearDown(void *data)
84 {
85 struct fixture *f = data;
86
87 cowsql_node_destroy(f->node);
88
89 test_dir_tear_down(f->dir);
90 test_sqlite_tear_down();
91 test_heap_tear_down(data);
92 free(f);
93 }
94
95 SUITE(node);
96
97 /******************************************************************************
98 *
99 * cowsql_node_start
100 *
101 ******************************************************************************/
102
103 TEST(node, start, setUp, tearDown, 0, NULL)
104 {
105 struct fixture *f = data;
106 int rv;
107
108 rv = cowsql_node_start(f->node);
109 munit_assert_int(rv, ==, 0);
110
111 rv = cowsql_node_stop(f->node);
112 munit_assert_int(rv, ==, 0);
113
114 return MUNIT_OK;
115 }
116
117 TEST(node, startInet, setUpInet, tearDown, 0, NULL)
118 {
119 struct fixture *f = data;
120 int rv;
121
122 rv = cowsql_node_start(f->node);
123 munit_assert_int(rv, ==, 0);
124
125 rv = cowsql_node_stop(f->node);
126 munit_assert_int(rv, ==, 0);
127
128 return MUNIT_OK;
129 }
130
131 TEST(node, snapshotParams, setUp, tearDown, 0, NULL)
132 {
133 struct fixture *f = data;
134 int rv;
135
136 rv = cowsql_node_set_snapshot_params(f->node, 2048, 2048);
137 munit_assert_int(rv, ==, 0);
138
139 startStopNode(f);
140 return MUNIT_OK;
141 }
142
143 TEST(node, snapshotParamsRunning, setUp, tearDown, 0, NULL)
144 {
145 struct fixture *f = data;
146 int rv;
147
148 rv = cowsql_node_start(f->node);
149 munit_assert_int(rv, ==, 0);
150
151 rv = cowsql_node_set_snapshot_params(f->node, 2048, 2048);
152 munit_assert_int(rv, !=, 0);
153
154 rv = cowsql_node_stop(f->node);
155 munit_assert_int(rv, ==, 0);
156
157 return MUNIT_OK;
158 }
159
160 TEST(node, snapshotParamsTrailingTooSmall, setUp, tearDown, 0, NULL)
161 {
162 struct fixture *f = data;
163 int rv;
164
165 rv = cowsql_node_set_snapshot_params(f->node, 2, 2);
166 munit_assert_int(rv, !=, 0);
167
168 startStopNode(f);
169 return MUNIT_OK;
170 }
171
172 TEST(node, snapshotParamsThresholdLargerThanTrailing, setUp, tearDown, 0, NULL)
173 {
174 struct fixture *f = data;
175 int rv;
176
177 rv = cowsql_node_set_snapshot_params(f->node, 2049, 2048);
178 munit_assert_int(rv, !=, 0);
179
180 startStopNode(f);
181 return MUNIT_OK;
182 }
183
184 TEST(node, networkLatency, setUp, tearDown, 0, NULL)
185 {
186 struct fixture *f = data;
187 int rv;
188
189 rv = cowsql_node_set_network_latency(f->node, 3600000000000ULL);
190 munit_assert_int(rv, ==, 0);
191
192 startStopNode(f);
193 return MUNIT_OK;
194 }
195
196 TEST(node, networkLatencyRunning, setUp, tearDown, 0, NULL)
197 {
198 struct fixture *f = data;
199 int rv;
200
201 rv = cowsql_node_start(f->node);
202 munit_assert_int(rv, ==, 0);
203
204 rv = cowsql_node_set_network_latency(f->node, 3600000000000ULL);
205 munit_assert_int(rv, ==, COWSQL_MISUSE);
206
207 rv = cowsql_node_stop(f->node);
208 munit_assert_int(rv, ==, 0);
209
210 return MUNIT_OK;
211 }
212
213 TEST(node, networkLatencyTooLarge, setUp, tearDown, 0, NULL)
214 {
215 struct fixture *f = data;
216 int rv;
217
218 rv = cowsql_node_set_network_latency(f->node, 3600000000000ULL + 1ULL);
219 munit_assert_int(rv, ==, COWSQL_MISUSE);
220
221 startStopNode(f);
222 return MUNIT_OK;
223 }
224
225 TEST(node, networkLatencyMs, setUp, tearDown, 0, NULL)
226 {
227 struct fixture *f = data;
228 int rv;
229
230 rv = cowsql_node_set_network_latency_ms(f->node, 5);
231 munit_assert_int(rv, ==, 0);
232 rv = cowsql_node_set_network_latency_ms(f->node, (3600U * 1000U));
233 munit_assert_int(rv, ==, 0);
234
235 startStopNode(f);
236 return MUNIT_OK;
237 }
238
239 TEST(node, networkLatencyMsRunning, setUp, tearDown, 0, NULL)
240 {
241 struct fixture *f = data;
242 int rv;
243
244 rv = cowsql_node_start(f->node);
245 munit_assert_int(rv, ==, 0);
246
247 rv = cowsql_node_set_network_latency_ms(f->node, 2);
248 munit_assert_int(rv, ==, COWSQL_MISUSE);
249
250 rv = cowsql_node_stop(f->node);
251 munit_assert_int(rv, ==, 0);
252
253 return MUNIT_OK;
254 }
255
256 TEST(node, networkLatencyMsTooSmall, setUp, tearDown, 0, NULL)
257 {
258 struct fixture *f = data;
259 int rv;
260
261 rv = cowsql_node_set_network_latency_ms(f->node, 0);
262 munit_assert_int(rv, ==, COWSQL_MISUSE);
263
264 startStopNode(f);
265 return MUNIT_OK;
266 }
267
268 TEST(node, networkLatencyMsTooLarge, setUp, tearDown, 0, NULL)
269 {
270 struct fixture *f = data;
271 int rv;
272
273 rv = cowsql_node_set_network_latency_ms(f->node, (3600U * 1000U) + 1);
274 munit_assert_int(rv, ==, COWSQL_MISUSE);
275
276 startStopNode(f);
277 return MUNIT_OK;
278 }
279
280 TEST(node, blockSize, setUp, tearDown, 0, NULL)
281 {
282 struct fixture *f = data;
283 int rv;
284
285 rv = cowsql_node_set_block_size(f->node, 0);
286 munit_assert_int(rv, ==, COWSQL_ERROR);
287 rv = cowsql_node_set_block_size(f->node, 1);
288 munit_assert_int(rv, ==, COWSQL_ERROR);
289 rv = cowsql_node_set_block_size(f->node, 511);
290 munit_assert_int(rv, ==, COWSQL_ERROR);
291 rv = cowsql_node_set_block_size(f->node, 1024 * 512);
292 munit_assert_int(rv, ==, COWSQL_ERROR);
293 rv = cowsql_node_set_block_size(f->node, 64 * 1024);
294 munit_assert_int(rv, ==, 0);
295
296 startStopNode(f);
297 return MUNIT_OK;
298 }
299
300 TEST(node, blockSizeRunning, setUp, tearDown, 0, NULL)
301 {
302 struct fixture *f = data;
303 int rv;
304
305 rv = cowsql_node_start(f->node);
306 munit_assert_int(rv, ==, 0);
307
308 rv = cowsql_node_set_block_size(f->node, 64 * 1024);
309 munit_assert_int(rv, ==, COWSQL_MISUSE);
310
311 rv = cowsql_node_stop(f->node);
312 munit_assert_int(rv, ==, 0);
313
314 return MUNIT_OK;
315 }
316
317 /******************************************************************************
318 *
319 * cowsql_node_recover
320 *
321 ******************************************************************************/
322 TEST(node, recover, setUpForRecovery, tearDown, 0, NULL)
323 {
324 struct fixture *f = data;
325 int rv;
326
327 /* Setup the infos structs */
328 static struct cowsql_node_info infos[2] = {0};
329 infos[0].id = 1;
330 infos[0].address = "1";
331 infos[1].id = 2;
332 infos[1].address = "2";
333
334 rv = cowsql_node_recover(f->node, infos, 2);
335 munit_assert_int(rv, ==, 0);
336
337 startStopNode(f);
338 return MUNIT_OK;
339 }
340
341 TEST(node, recoverExt, setUpForRecovery, tearDown, 0, NULL)
342 {
343 struct fixture *f = data;
344 int rv;
345
346 /* Setup the infos structs */
347 static struct cowsql_node_info_ext infos[2] = {0};
348 infos[0].size = sizeof(*infos);
349 infos[0].id = cowsql_generate_node_id("1");
350 infos[0].address = PTR_TO_UINT64("1");
351 infos[0].cowsql_role = COWSQL_VOTER;
352 infos[1].size = sizeof(*infos);
353 infos[1].id = cowsql_generate_node_id("2");
354 ;
355 infos[1].address = PTR_TO_UINT64("2");
356 infos[1].cowsql_role = COWSQL_SPARE;
357
358 rv = cowsql_node_recover_ext(f->node, infos, 2);
359 munit_assert_int(rv, ==, 0);
360
361 startStopNode(f);
362 return MUNIT_OK;
363 }
364
365 TEST(node, recoverExtUnaligned, setUpForRecovery, tearDown, 0, NULL)
366 {
367 struct fixture *f = data;
368 int rv;
369
370 /* Setup the infos structs */
371 static struct cowsql_node_info_ext infos[1] = {0};
372 infos[0].size = sizeof(*infos) + 1; /* Unaligned */
373 infos[0].id = 1;
374 infos[0].address = PTR_TO_UINT64("1");
375 infos[0].cowsql_role = COWSQL_VOTER;
376
377 rv = cowsql_node_recover_ext(f->node, infos, 1);
378 munit_assert_int(rv, ==, COWSQL_MISUSE);
379
380 startStopNode(f);
381 return MUNIT_OK;
382 }
383
384 TEST(node, recoverExtTooSmall, setUpForRecovery, tearDown, 0, NULL)
385 {
386 struct fixture *f = data;
387 int rv;
388
389 /* Setup the infos structs */
390 static struct cowsql_node_info_ext infos[1] = {0};
391 infos[0].size = COWSQL_NODE_INFO_EXT_SZ_ORIG - 1;
392 infos[0].id = 1;
393 infos[0].address = PTR_TO_UINT64("1");
394 infos[0].cowsql_role = COWSQL_VOTER;
395
396 rv = cowsql_node_recover_ext(f->node, infos, 1);
397 munit_assert_int(rv, ==, COWSQL_MISUSE);
398
399 startStopNode(f);
400 return MUNIT_OK;
401 }
402
403 struct cowsql_node_info_ext_new
404 {
405 struct cowsql_node_info_ext orig;
406 uint64_t new1;
407 uint64_t new2;
408 };
409
410 TEST(node, recoverExtNewFields, setUpForRecovery, tearDown, 0, NULL)
411 {
412 struct fixture *f = data;
413 int rv;
414
415 /* Setup the infos structs */
416 static struct cowsql_node_info_ext_new infos[1] = {0};
417 infos[0].orig.size = sizeof(*infos);
418 infos[0].orig.id = 1;
419 infos[0].orig.address = PTR_TO_UINT64("1");
420 infos[0].orig.cowsql_role = COWSQL_VOTER;
421 infos[0].new1 = 0;
422 infos[0].new2 = 0;
423
424 rv = cowsql_node_recover_ext(f->node,
425 (struct cowsql_node_info_ext *)infos, 1);
426 munit_assert_int(rv, ==, 0);
427
428 startStopNode(f);
429 return MUNIT_OK;
430 }
431
432 TEST(node, recoverExtNewFieldsNotZero, setUpForRecovery, tearDown, 0, NULL)
433 {
434 struct fixture *f = data;
435 int rv;
436
437 /* Setup the infos structs */
438 static struct cowsql_node_info_ext_new infos[1] = {0};
439 infos[0].orig.size = sizeof(*infos);
440 infos[0].orig.id = 1;
441 infos[0].orig.address = PTR_TO_UINT64("1");
442 infos[0].orig.cowsql_role = COWSQL_VOTER;
443 infos[0].new1 = 0;
444 infos[0].new2 = 1; /* This will cause a failure */
445
446 rv = cowsql_node_recover_ext(f->node,
447 (struct cowsql_node_info_ext *)infos, 1);
448 munit_assert_int(rv, ==, COWSQL_MISUSE);
449
450 startStopNode(f);
451 return MUNIT_OK;
452 }
453
454 /******************************************************************************
455 *
456 * cowsql_node_errmsg
457 *
458 ******************************************************************************/
459
460 TEST(node, errMsgNodeNull, NULL, NULL, 0, NULL)
461 {
462 munit_assert_string_equal(cowsql_node_errmsg(NULL), "node is NULL");
463 return MUNIT_OK;
464 }
465
466 TEST(node, errMsg, setUp, tearDown, 0, NULL)
467 {
468 struct fixture *f = data;
469 int rv;
470
471 munit_assert_string_equal(cowsql_node_errmsg(f->node), "");
472
473 rv = cowsql_node_start(f->node);
474 munit_assert_int(rv, ==, 0);
475
476 rv = cowsql_node_stop(f->node);
477 munit_assert_int(rv, ==, 0);
478
479 return MUNIT_OK;
480 }