2 Tests for ctdb_takeover.c
4 Copyright (C) Martin Schwenke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "ctdbd_test.c"
22 /* This is lazy... but it is test code! */
23 #define CTDB_TEST_MAX_NODES 256
24 #define CTDB_TEST_MAX_IPS 1024
26 /* Format of each line is "IP pnn" - the separator has to be at least
27 * 1 space (not a tab or whatever - a space!).
29 static struct public_ip_list *
30 read_ctdb_public_ip_list(TALLOC_CTX *ctx)
36 struct public_ip_list *last = NULL;
38 struct public_ip_list *ret = NULL;
40 while (fgets(line, sizeof(line), stdin) != NULL) {
42 if ((t = strchr(line, ' ')) != NULL) {
43 /* Make line contain just the address */
45 /* Point to PNN or leading whitespace... */
47 pnn = (int) strtol(t, (char **) NULL, 10);
49 /* Assume just an IP address, default to PNN -1 */
50 if ((t = strchr(line, '\n')) != NULL) {
56 if (parse_ip(line, NULL, 0, &addr)) {
58 last = talloc(ctx, struct public_ip_list);
60 last->next = talloc(ctx, struct public_ip_list);
65 memcpy(&(last->addr), &addr, sizeof(addr));
70 DEBUG(DEBUG_ERR, (__location__ " ERROR, bad address :%s\n", line));
77 static void print_ctdb_public_ip_list(struct public_ip_list * ips)
80 printf("%s %d\n", ctdb_addr_to_str(&(ips->addr)), ips->pnn);
85 /* Read some IPs from stdin, 1 per line, parse them and then print
87 static void ctdb_test_read_ctdb_public_ip_list(void)
89 struct public_ip_list *l;
91 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
93 l = read_ctdb_public_ip_list(tmp_ctx);
95 print_ctdb_public_ip_list(l);
100 /* Format of each line is "IP CURRENT_PNN ALLOWED_PNN,...".
103 read_ctdb_public_ip_info(TALLOC_CTX *ctx,
105 struct public_ip_list ** all_ips,
106 struct ctdb_public_ip_list_old *** avail)
111 struct public_ip_list * ta;
112 int pnn, numips, curr, n, i;
113 struct ctdb_public_ip_list_old * a;
115 struct public_ip_list *last = NULL;
117 *avail = talloc_array_size(ctx, sizeof(struct ctdb_public_ip_list_old *),
118 CTDB_TEST_MAX_NODES);
120 sizeof(struct ctdb_public_ip_list_old *) * CTDB_TEST_MAX_NODES);
124 while (fgets(line, sizeof(line), stdin) != NULL) {
126 /* Get rid of pesky newline */
127 if ((t = strchr(line, '\n')) != NULL) {
131 /* Exit on an empty line */
132 if (line[0] == '\0') {
136 /* Get the IP address */
137 tok = strtok(line, " \t");
139 DEBUG(DEBUG_ERR, (__location__ " WARNING, bad line ignored :%s\n", line));
143 if (!parse_ip(tok, NULL, 0, &addr)) {
144 DEBUG(DEBUG_ERR, (__location__ " ERROR, bad address :%s\n", tok));
149 if (numips > CTDB_TEST_MAX_IPS) {
150 DEBUG(DEBUG_ERR, ("ERROR: Exceeding CTDB_TEST_MAX_IPS: %d\n", CTDB_TEST_MAX_IPS));
156 tok = strtok(NULL, " \t");
158 pnn = (int) strtol(tok, (char **) NULL, 10);
161 /* Add address + pnn to all_ips */
163 last = talloc(ctx, struct public_ip_list);
165 last->next = talloc(ctx, struct public_ip_list);
170 memcpy(&(last->addr), &addr, sizeof(addr));
171 if (*all_ips == NULL) {
175 tok = strtok(NULL, " \t#");
180 /* Handle allowed nodes for addr */
181 t = strtok(tok, ",");
183 n = (int) strtol(t, (char **) NULL, 10);
184 if ((*avail)[n] == NULL) {
185 (*avail)[n] = talloc_array(ctx, struct ctdb_public_ip_list_old, CTDB_TEST_MAX_IPS);
186 (*avail)[n]->num = 0;
188 curr = (*avail)[n]->num;
189 (*avail)[n]->ips[curr].pnn = pnn;
190 memcpy(&((*avail)[n]->ips[curr].addr),
191 &addr, sizeof(addr));
193 t = strtok(NULL, ",");
198 /* Build list of all allowed IPs */
199 a = talloc_array(ctx, struct ctdb_public_ip_list_old, CTDB_TEST_MAX_IPS);
201 for (ta = *all_ips, i=0; ta != NULL && i < numips ; ta = ta->next, i++) {
202 a->ips[i].pnn = ta->pnn;
203 memcpy(&(a->ips[i].addr), &(ta->addr), sizeof(ta->addr));
206 /* Assign it to any nodes that don't have a list assigned */
207 for (n = 0; n < numnodes; n++) {
208 if ((*avail)[n] == NULL) {
216 static void print_ctdb_available_ips(int numnodes,
217 struct ctdb_public_ip_list_old **avail)
221 for (n = 0; n < numnodes; n++) {
222 if ((avail[n] != NULL) && (avail[n]->num > 0)) {
224 for (i = 0; i < avail[n]->num; i++) {
226 (i == 0) ? " " : ", ",
227 ctdb_addr_to_str(&(avail[n]->ips[i].addr)));
234 static void ctdb_test_read_ctdb_public_ip_info(const char nodestates[])
237 struct public_ip_list *l;
238 struct ctdb_public_ip_list_old **avail;
241 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
243 /* Avoid that const */
244 ns = talloc_strdup(tmp_ctx, nodestates);
247 tok = strtok(ns, ",");
248 while (tok != NULL) {
250 if (numnodes > CTDB_TEST_MAX_NODES) {
251 DEBUG(DEBUG_ERR, ("ERROR: Exceeding CTDB_TEST_MAX_NODES: %d\n", CTDB_TEST_MAX_NODES));
254 tok = strtok(NULL, ",");
257 read_ctdb_public_ip_info(tmp_ctx, numnodes, &l, &avail);
259 print_ctdb_public_ip_list(l);
260 print_ctdb_available_ips(numnodes, avail);
262 talloc_free(tmp_ctx);
265 /* Read 2 IPs from stdin, calculate the IP distance and print it. */
266 static void ctdb_test_ip_distance(void)
268 struct public_ip_list *l;
271 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
273 l = read_ctdb_public_ip_list(tmp_ctx);
276 distance = ip_distance(&(l->addr), &(l->next->addr));
277 printf ("%lu\n", (unsigned long) distance);
280 talloc_free(tmp_ctx);
283 /* Read some IPs from stdin, calculate the sum of the squares of the
284 * IP distances between the 1st argument and those read that are on
285 * the given node. The given IP must one of the ones in the list. */
286 static void ctdb_test_ip_distance_2_sum(const char ip[], int pnn)
288 struct public_ip_list *l;
289 struct public_ip_list *t;
293 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
296 l = read_ctdb_public_ip_list(tmp_ctx);
298 if (l && parse_ip(ip, NULL, 0, &addr)) {
299 /* find the entry for the specified IP */
300 for (t=l; t!=NULL; t=t->next) {
301 if (ctdb_same_ip(&(t->addr), &addr)) {
307 fprintf(stderr, "IP NOT PRESENT IN LIST");
311 distance = ip_distance_2_sum(&(t->addr), l, pnn);
312 printf ("%lu\n", (unsigned long) distance);
314 fprintf(stderr, "BAD INPUT");
318 talloc_free(tmp_ctx);
321 /* Read some IPs from stdin, calculate the sume of the squares of the
322 * IP distances between the first and the rest, and print it. */
323 static void ctdb_test_lcp2_imbalance(int pnn)
325 struct public_ip_list *l;
328 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
330 l = read_ctdb_public_ip_list(tmp_ctx);
332 imbalance = lcp2_imbalance(l, pnn);
333 printf ("%lu\n", (unsigned long) imbalance);
335 talloc_free(tmp_ctx);
338 static uint32_t *get_tunable_values(TALLOC_CTX *tmp_ctx,
344 uint32_t *tvals = talloc_zero_array(tmp_ctx, uint32_t, numnodes);
345 char *t = getenv(tunable);
348 if (strcmp(t, "1") == 0) {
349 for (i=0; i<numnodes; i++) {
353 tok = strtok(t, ",");
355 while (tok != NULL) {
357 (uint32_t) strtol(tok, NULL, 0);
359 tok = strtok(NULL, ",");
362 fprintf(stderr, "ERROR: Wrong number of values in %s\n", tunable);
371 static enum ctdb_runstate *get_runstate(TALLOC_CTX *tmp_ctx,
376 enum ctdb_runstate *runstate =
377 talloc_zero_array(tmp_ctx, enum ctdb_runstate, numnodes);
378 char *t = getenv("CTDB_TEST_RUNSTATE");
381 for (i=0; i<numnodes; i++) {
382 runstate[i] = CTDB_RUNSTATE_RUNNING;
385 tvals = get_tunable_values(tmp_ctx, numnodes, "CTDB_TEST_RUNSTATE");
386 for (i=0; i<numnodes; i++) {
387 runstate[i] = (enum ctdb_runstate) tvals[i];
395 /* Fake up enough CTDB state to be able to run the IP allocation
396 * algorithm. Usually this sets up some standard state, sets the node
397 * states from the command-line and reads the current IP layout from
400 * However, if read_ips_for_multiple_nodes is true then each node's
401 * idea of the IP layout is read separately from stdin. In this mode
402 * is doesn't make much sense to use read_ctdb_public_ip_info's
403 * optional ALLOWED_PNN,... list in the input, since each node is
404 * being handled separately anyway. IPs for each node are separated
405 * by a blank line. This mode is for testing weird behaviours where
406 * the IP layouts differs across nodes and we want to improve
407 * create_merged_ip_list(), so should only be used in tests of
408 * ctdb_takeover_run_core(). Yes, it is a hack... :-)
410 static void ctdb_test_init(const char nodestates[],
411 struct ctdb_context **ctdb,
412 struct public_ip_list **all_ips,
413 struct ctdb_ipflags **ipflags,
414 bool read_ips_for_multiple_nodes)
416 struct ctdb_public_ip_list_old **avail;
418 uint32_t nodeflags[CTDB_TEST_MAX_NODES];
420 struct ctdb_node_map_old *nodemap;
421 uint32_t *tval_noiptakeover;
422 uint32_t *tval_noiptakeoverondisabled;
423 enum ctdb_runstate *runstate;
425 *ctdb = talloc_zero(NULL, struct ctdb_context);
427 /* Avoid that const */
428 ns = talloc_strdup(*ctdb, nodestates);
431 tok = strtok(ns, ",");
432 while (tok != NULL) {
433 nodeflags[numnodes] = (uint32_t) strtol(tok, NULL, 0);
435 if (numnodes >= CTDB_TEST_MAX_NODES) {
436 DEBUG(DEBUG_ERR, ("ERROR: Exceeding CTDB_TEST_MAX_NODES: %d\n", CTDB_TEST_MAX_NODES));
439 tok = strtok(NULL, ",");
442 /* Fake things up... */
443 (*ctdb)->num_nodes = numnodes;
445 /* Default to LCP2 */
446 (*ctdb)->tunable.lcp2_public_ip_assignment = 1;
447 (*ctdb)->tunable.deterministic_public_ips = 0;
448 (*ctdb)->tunable.disable_ip_failover = 0;
449 (*ctdb)->tunable.no_ip_failback = 0;
451 if ((t = getenv("CTDB_IP_ALGORITHM"))) {
452 if (strcmp(t, "lcp2") == 0) {
453 (*ctdb)->tunable.lcp2_public_ip_assignment = 1;
454 } else if (strcmp(t, "nondet") == 0) {
455 (*ctdb)->tunable.lcp2_public_ip_assignment = 0;
456 } else if (strcmp(t, "det") == 0) {
457 (*ctdb)->tunable.lcp2_public_ip_assignment = 0;
458 (*ctdb)->tunable.deterministic_public_ips = 1;
460 fprintf(stderr, "ERROR: unknown IP algorithm %s\n", t);
465 tval_noiptakeover = get_tunable_values(*ctdb, numnodes,
466 "CTDB_SET_NoIPTakeover");
467 tval_noiptakeoverondisabled =
468 get_tunable_values(*ctdb, numnodes,
469 "CTDB_SET_NoIPHostOnAllDisabled");
471 runstate = get_runstate(*ctdb, numnodes);
473 nodemap = talloc_array(*ctdb, struct ctdb_node_map_old, numnodes);
474 nodemap->num = numnodes;
476 if (!read_ips_for_multiple_nodes) {
477 read_ctdb_public_ip_info(*ctdb, numnodes, all_ips, &avail);
480 (*ctdb)->nodes = talloc_array(*ctdb, struct ctdb_node *, numnodes); // FIXME: bogus size, overkill
482 for (i=0; i < numnodes; i++) {
483 nodemap->nodes[i].pnn = i;
484 nodemap->nodes[i].flags = nodeflags[i];
485 /* nodemap->nodes[i].sockaddr is uninitialised */
487 if (read_ips_for_multiple_nodes) {
488 read_ctdb_public_ip_info(*ctdb, numnodes,
492 (*ctdb)->nodes[i] = talloc(*ctdb, struct ctdb_node);
493 (*ctdb)->nodes[i]->pnn = i;
494 (*ctdb)->nodes[i]->flags = nodeflags[i];
495 (*ctdb)->nodes[i]->available_public_ips = avail[i];
496 (*ctdb)->nodes[i]->known_public_ips = avail[i];
499 *ipflags = set_ipflags_internal(*ctdb, *ctdb, nodemap,
501 tval_noiptakeoverondisabled,
505 /* IP layout is read from stdin. */
506 static void ctdb_test_lcp2_allocate_unassigned(const char nodestates[])
508 struct ctdb_context *ctdb;
509 struct public_ip_list *all_ips;
510 struct ctdb_ipflags *ipflags;
512 uint32_t *lcp2_imbalances;
515 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags, false);
517 lcp2_init(ctdb, ipflags, all_ips, NULL,
518 &lcp2_imbalances, &newly_healthy);
520 lcp2_allocate_unassigned(ctdb, ipflags,
521 all_ips, lcp2_imbalances);
523 print_ctdb_public_ip_list(all_ips);
528 /* IP layout is read from stdin. */
529 static void ctdb_test_lcp2_failback(const char nodestates[])
531 struct ctdb_context *ctdb;
532 struct public_ip_list *all_ips;
533 struct ctdb_ipflags *ipflags;
535 uint32_t *lcp2_imbalances;
538 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags, false);
540 lcp2_init(ctdb, ipflags, all_ips, NULL,
541 &lcp2_imbalances, &newly_healthy);
543 lcp2_failback(ctdb, ipflags,
544 all_ips, lcp2_imbalances, newly_healthy);
546 print_ctdb_public_ip_list(all_ips);
551 /* IP layout is read from stdin. */
552 static void ctdb_test_lcp2_failback_loop(const char nodestates[])
554 struct ctdb_context *ctdb;
555 struct public_ip_list *all_ips;
556 struct ctdb_ipflags *ipflags;
558 uint32_t *lcp2_imbalances;
561 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags, false);
563 lcp2_init(ctdb, ipflags, all_ips, NULL,
564 &lcp2_imbalances, &newly_healthy);
566 lcp2_failback(ctdb, ipflags,
567 all_ips, lcp2_imbalances, newly_healthy);
569 print_ctdb_public_ip_list(all_ips);
574 /* IP layout is read from stdin. See comment for ctdb_test_init() for
575 * explanation of read_ips_for_multiple_nodes.
577 static void ctdb_test_ctdb_takeover_run_core(const char nodestates[],
578 bool read_ips_for_multiple_nodes)
580 struct ctdb_context *ctdb;
581 struct public_ip_list *all_ips;
582 struct ctdb_ipflags *ipflags;
584 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags,
585 read_ips_for_multiple_nodes);
587 ctdb_takeover_run_core(ctdb, ipflags, &all_ips, NULL);
589 print_ctdb_public_ip_list(all_ips);
594 static void usage(void)
596 fprintf(stderr, "usage: ctdb_takeover_tests <op>\n");
600 int main(int argc, const char *argv[])
602 DEBUGLEVEL = DEBUG_DEBUG;
603 if (getenv("CTDB_TEST_LOGLEVEL")) {
604 DEBUGLEVEL = atoi(getenv("CTDB_TEST_LOGLEVEL"));
611 if (strcmp(argv[1], "ip_list") == 0) {
612 ctdb_test_read_ctdb_public_ip_list();
613 } else if (argc == 3 && strcmp(argv[1], "ip_info") == 0) {
614 ctdb_test_read_ctdb_public_ip_info(argv[2]);
615 } else if (strcmp(argv[1], "ip_distance") == 0) {
616 ctdb_test_ip_distance();
617 } else if (argc == 4 && strcmp(argv[1], "ip_distance_2_sum") == 0) {
618 ctdb_test_ip_distance_2_sum(argv[2], atoi(argv[3]));
619 } else if (argc >= 3 && strcmp(argv[1], "lcp2_imbalance") == 0) {
620 ctdb_test_lcp2_imbalance(atoi(argv[2]));
621 } else if (argc == 3 && strcmp(argv[1], "lcp2_allocate_unassigned") == 0) {
622 ctdb_test_lcp2_allocate_unassigned(argv[2]);
623 } else if (argc == 3 && strcmp(argv[1], "lcp2_failback") == 0) {
624 ctdb_test_lcp2_failback(argv[2]);
625 } else if (argc == 3 && strcmp(argv[1], "lcp2_failback_loop") == 0) {
626 ctdb_test_lcp2_failback_loop(argv[2]);
627 } else if (argc == 3 &&
628 strcmp(argv[1], "ctdb_takeover_run_core") == 0) {
629 ctdb_test_ctdb_takeover_run_core(argv[2], false);
630 } else if (argc == 4 &&
631 strcmp(argv[1], "ctdb_takeover_run_core") == 0 &&
632 strcmp(argv[3], "multi") == 0) {
633 ctdb_test_ctdb_takeover_run_core(argv[2], true);