Debian Codegraph @

test/integration/test_role_management.c

raw
1 #include "../../src/client/protocol.h"
2 #include "../../src/server.h"
3 #include "../lib/client.h"
4 #include "../lib/endpoint.h"
5 #include "../lib/fs.h"
6 #include "../lib/heap.h"
7 #include "../lib/runner.h"
8 #include "../lib/server.h"
9 #include "../lib/sqlite.h"
10 #include "../lib/util.h"
11
12 #ifndef FLAKY
13
14 #define N_SERVERS 5
15 #define FIXTURE \
16 struct test_server servers[N_SERVERS]; \
17 struct client_proto *client; \
18 struct rows rows;
19
20 #define SETUP \
21 unsigned i_; \
22 test_heap_setup(params, user_data); \
23 test_sqlite_setup(params); \
24 for (i_ = 0; i_ < N_SERVERS; i_++) { \
25 struct test_server *server = &f->servers[i_]; \
26 test_server_setup(server, i_ + 1, params); \
27 } \
28 test_server_network(f->servers, N_SERVERS); \
29 for (i_ = 0; i_ < N_SERVERS; i_++) { \
30 struct test_server *server = &f->servers[i_]; \
31 test_server_start(server, params); \
32 } \
33 SELECT(1)
34
35 #define TEAR_DOWN \
36 unsigned i_; \
37 for (i_ = 0; i_ < N_SERVERS; i_++) { \
38 tracef("test_server_tear_down(%u)", i_); \
39 test_server_tear_down(&f->servers[i_]); \
40 } \
41 test_sqlite_tear_down(); \
42 test_heap_tear_down(data)
43
44 #define SELECT(ID) f->client = test_server_client(&f->servers[ID - 1])
45
46 #define TRIES 5
47
48 static char *trueonly[] = {"1", NULL};
49
50 static char *threeonly[] = {"3", NULL};
51
52 static MunitParameterEnum role_management_params[] = {
53 {"role_management", trueonly},
54 {"target_voters", threeonly},
55 {"target_standbys", threeonly},
56 {NULL, NULL},
57 };
58
59 SUITE(role_management)
60
61 struct fixture
62 {
63 FIXTURE;
64 };
65
66 static void *setUp(const MunitParameter params[], void *user_data)
67 {
68 struct fixture *f = munit_malloc(sizeof *f);
69 SETUP;
70 return f;
71 }
72
73 static void tearDown(void *data)
74 {
75 struct fixture *f = data;
76 TEAR_DOWN;
77 free(f);
78 }
79
80 static bool hasRole(struct fixture *f, cowsql_node_id id, int role)
81 {
82 struct client_node_info *servers;
83 uint64_t n_servers;
84 struct client_context context;
85 unsigned i;
86 bool ret = false;
87 int rv;
88
89 clientContextMillis(&context, 5000);
90 rv = clientSendCluster(f->client, &context);
91 munit_assert_int(rv, ==, 0);
92 rv = clientRecvServers(f->client, &servers, &n_servers, &context);
93 munit_assert_int(rv, ==, 0);
94 for (i = 0; i < n_servers; i += 1) {
95 if (servers[i].id == id) {
96 ret = servers[i].role == role;
97 break;
98 }
99 }
100 for (i = 0; i < n_servers; i += 1) {
101 free(servers[i].addr);
102 }
103 free(servers);
104
105 return ret;
106 }
107
108 TEST(role_management, promote, setUp, tearDown, 0, role_management_params)
109 {
110 struct fixture *f = data;
111 unsigned id = 2;
112 const char *address = "@2";
113 int tries;
114
115 /* TODO: this test is time-dependant and flaky on some platforms */
116 if (getenv("CI") == NULL) {
117 return MUNIT_SKIP;
118 }
119
120 HANDSHAKE;
121
122 id = 2;
123 address = "@2";
124 ADD(id, address);
125 for (tries = 0; tries < TRIES && !hasRole(f, 2, COWSQL_VOTER);
126 tries += 1) {
127 sleep(1);
128 }
129 if (tries == TRIES) {
130 return MUNIT_FAIL;
131 };
132
133 id = 3;
134 address = "@3";
135 ADD(id, address);
136 for (tries = 0; tries < TRIES && !hasRole(f, 3, COWSQL_VOTER);
137 tries += 1) {
138 sleep(1);
139 }
140 if (tries == TRIES) {
141 return MUNIT_FAIL;
142 };
143
144 id = 4;
145 address = "@4";
146 ADD(id, address);
147 for (tries = 0; tries < TRIES && !hasRole(f, 4, COWSQL_STANDBY);
148 tries += 1) {
149 sleep(1);
150 }
151 if (tries == TRIES) {
152 return MUNIT_FAIL;
153 };
154
155 id = 5;
156 address = "@5";
157 ADD(id, address);
158 for (tries = 0; tries < TRIES && !hasRole(f, 5, COWSQL_STANDBY);
159 tries += 1) {
160 sleep(1);
161 }
162 if (tries == TRIES) {
163 return MUNIT_FAIL;
164 };
165
166 return MUNIT_OK;
167 }
168
169 #endif /* not FLAKY */