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 ctdb_public_ip_list *
30 read_ctdb_public_ip_list(TALLOC_CTX *ctx)
36 struct ctdb_public_ip_list *last = NULL;
38 struct ctdb_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 ctdb_public_ip_list);
60 last->next = talloc(ctx, struct ctdb_public_ip_list);
65 memcpy(&(last->addr), &addr, sizeof(addr));
70 DEBUG(DEBUG_ERR, (__location__ " ERROR, bad address :%s\n", line));
77 void print_ctdb_public_ip_list(struct ctdb_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 void ctdb_test_read_ctdb_public_ip_list(void)
89 struct ctdb_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 ctdb_public_ip_list ** all_ips,
106 struct ctdb_all_public_ips *** avail)
111 struct ctdb_public_ip_list * ta;
112 int pnn, numips, curr, n, i;
113 struct ctdb_all_public_ips * a;
115 struct ctdb_public_ip_list *last = NULL;
117 *avail = talloc_array_size(ctx, sizeof(struct ctdb_all_public_ips *), CTDB_TEST_MAX_NODES);
119 sizeof(struct ctdb_all_public_ips *) * CTDB_TEST_MAX_NODES);
123 while (fgets(line, sizeof(line), stdin) != NULL) {
125 /* Get rid of pesky newline */
126 if ((t = strchr(line, '\n')) != NULL) {
130 /* Exit on an empty line */
131 if (line[0] == '\0') {
135 /* Get the IP address */
136 tok = strtok(line, " \t");
138 DEBUG(DEBUG_ERR, (__location__ " WARNING, bad line ignored :%s\n", line));
142 if (!parse_ip(tok, NULL, 0, &addr)) {
143 DEBUG(DEBUG_ERR, (__location__ " ERROR, bad address :%s\n", tok));
148 if (numips > CTDB_TEST_MAX_IPS) {
149 DEBUG(DEBUG_ERR, ("ERROR: Exceeding CTDB_TEST_MAX_IPS: %d\n", CTDB_TEST_MAX_IPS));
155 tok = strtok(NULL, " \t");
157 pnn = (int) strtol(tok, (char **) NULL, 10);
160 /* Add address + pnn to all_ips */
162 last = talloc(ctx, struct ctdb_public_ip_list);
164 last->next = talloc(ctx, struct ctdb_public_ip_list);
169 memcpy(&(last->addr), &addr, sizeof(addr));
170 if (*all_ips == NULL) {
174 tok = strtok(NULL, " \t#");
179 /* Handle allowed nodes for addr */
180 t = strtok(tok, ",");
182 n = (int) strtol(t, (char **) NULL, 10);
183 if ((*avail)[n] == NULL) {
184 (*avail)[n] = talloc_array(ctx, struct ctdb_all_public_ips, CTDB_TEST_MAX_IPS);
185 (*avail)[n]->num = 0;
187 curr = (*avail)[n]->num;
188 (*avail)[n]->ips[curr].pnn = pnn;
189 memcpy(&((*avail)[n]->ips[curr].addr),
190 &addr, sizeof(addr));
192 t = strtok(NULL, ",");
197 /* Build list of all allowed IPs */
198 a = talloc_array(ctx, struct ctdb_all_public_ips, CTDB_TEST_MAX_IPS);
200 for (ta = *all_ips, i=0; ta != NULL && i < numips ; ta = ta->next, i++) {
201 a->ips[i].pnn = ta->pnn;
202 memcpy(&(a->ips[i].addr), &(ta->addr), sizeof(ta->addr));
205 /* Assign it to any nodes that don't have a list assigned */
206 for (n = 0; n < numnodes; n++) {
207 if ((*avail)[n] == NULL) {
215 void print_ctdb_available_ips(int numnodes, struct ctdb_all_public_ips **avail)
219 for (n = 0; n < numnodes; n++) {
220 if ((avail[n] != NULL) && (avail[n]->num > 0)) {
222 for (i = 0; i < avail[n]->num; i++) {
224 (i == 0) ? " " : ", ",
225 ctdb_addr_to_str(&(avail[n]->ips[i].addr)));
232 void ctdb_test_read_ctdb_public_ip_info(const char nodestates[])
235 struct ctdb_public_ip_list *l;
236 struct ctdb_all_public_ips **avail;
239 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
241 /* Avoid that const */
242 ns = talloc_strdup(tmp_ctx, nodestates);
245 tok = strtok(ns, ",");
246 while (tok != NULL) {
248 if (numnodes > CTDB_TEST_MAX_NODES) {
249 DEBUG(DEBUG_ERR, ("ERROR: Exceeding CTDB_TEST_MAX_NODES: %d\n", CTDB_TEST_MAX_NODES));
252 tok = strtok(NULL, ",");
255 read_ctdb_public_ip_info(tmp_ctx, numnodes, &l, &avail);
257 print_ctdb_public_ip_list(l);
258 print_ctdb_available_ips(numnodes, avail);
260 talloc_free(tmp_ctx);
263 /* Read 2 IPs from stdin, calculate the IP distance and print it. */
264 void ctdb_test_ip_distance(void)
266 struct ctdb_public_ip_list *l;
269 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
271 l = read_ctdb_public_ip_list(tmp_ctx);
274 distance = ip_distance(&(l->addr), &(l->next->addr));
275 printf ("%lu\n", (unsigned long) distance);
278 talloc_free(tmp_ctx);
281 /* Read some IPs from stdin, calculate the sum of the squares of the
282 * IP distances between the 1st argument and those read that are on
283 * the given node. The given IP must one of the ones in the list. */
284 void ctdb_test_ip_distance_2_sum(const char ip[], int pnn)
286 struct ctdb_public_ip_list *l;
287 struct ctdb_public_ip_list *t;
291 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
294 l = read_ctdb_public_ip_list(tmp_ctx);
296 if (l && parse_ip(ip, NULL, 0, &addr)) {
297 /* find the entry for the specified IP */
298 for (t=l; t!=NULL; t=t->next) {
299 if (ctdb_same_ip(&(t->addr), &addr)) {
305 fprintf(stderr, "IP NOT PRESENT IN LIST");
309 distance = ip_distance_2_sum(&(t->addr), l, pnn);
310 printf ("%lu\n", (unsigned long) distance);
312 fprintf(stderr, "BAD INPUT");
316 talloc_free(tmp_ctx);
319 /* Read some IPs from stdin, calculate the sume of the squares of the
320 * IP distances between the first and the rest, and print it. */
321 void ctdb_test_lcp2_imbalance(int pnn)
323 struct ctdb_public_ip_list *l;
326 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
328 l = read_ctdb_public_ip_list(tmp_ctx);
330 imbalance = lcp2_imbalance(l, pnn);
331 printf ("%lu\n", (unsigned long) imbalance);
333 talloc_free(tmp_ctx);
336 static uint32_t *get_tunable_values(TALLOC_CTX *tmp_ctx,
342 uint32_t *tvals = talloc_zero_array(tmp_ctx, uint32_t, numnodes);
343 char *t = getenv(tunable);
346 if (strcmp(t, "1") == 0) {
347 for (i=0; i<numnodes; i++) {
351 tok = strtok(t, ",");
353 while (tok != NULL) {
355 (uint32_t) strtol(tok, NULL, 0);
357 tok = strtok(NULL, ",");
360 fprintf(stderr, "ERROR: Wrong number of values in %s\n", tunable);
369 static enum ctdb_runstate *get_runstate(TALLOC_CTX *tmp_ctx,
374 enum ctdb_runstate *runstate =
375 talloc_zero_array(tmp_ctx, enum ctdb_runstate, numnodes);
376 char *t = getenv("CTDB_TEST_RUNSTATE");
379 for (i=0; i<numnodes; i++) {
380 runstate[i] = CTDB_RUNSTATE_RUNNING;
383 tvals = get_tunable_values(tmp_ctx, numnodes, "CTDB_TEST_RUNSTATE");
384 for (i=0; i<numnodes; i++) {
385 runstate[i] = (enum ctdb_runstate) tvals[i];
393 /* Fake up enough CTDB state to be able to run the IP allocation
394 * algorithm. Usually this sets up some standard state, sets the node
395 * states from the command-line and reads the current IP layout from
398 * However, if read_ips_for_multiple_nodes is true then each node's
399 * idea of the IP layout is read separately from stdin. In this mode
400 * is doesn't make much sense to use read_ctdb_public_ip_info's
401 * optional ALLOWED_PNN,... list in the input, since each node is
402 * being handled separately anyway. IPs for each node are separated
403 * by a blank line. This mode is for testing weird behaviours where
404 * the IP layouts differs across nodes and we want to improve
405 * create_merged_ip_list(), so should only be used in tests of
406 * ctdb_takeover_run_core(). Yes, it is a hack... :-)
408 void ctdb_test_init(const char nodestates[],
409 struct ctdb_context **ctdb,
410 struct ctdb_public_ip_list **all_ips,
411 struct ctdb_ipflags **ipflags,
412 bool read_ips_for_multiple_nodes)
414 struct ctdb_all_public_ips **avail;
416 uint32_t nodeflags[CTDB_TEST_MAX_NODES];
418 struct ctdb_node_map *nodemap;
419 uint32_t *tval_noiptakeover;
420 uint32_t *tval_noiptakeoverondisabled;
421 enum ctdb_runstate *runstate;
423 *ctdb = talloc_zero(NULL, struct ctdb_context);
425 /* Avoid that const */
426 ns = talloc_strdup(*ctdb, nodestates);
429 tok = strtok(ns, ",");
430 while (tok != NULL) {
431 nodeflags[numnodes] = (uint32_t) strtol(tok, NULL, 0);
433 if (numnodes > CTDB_TEST_MAX_NODES) {
434 DEBUG(DEBUG_ERR, ("ERROR: Exceeding CTDB_TEST_MAX_NODES: %d\n", CTDB_TEST_MAX_NODES));
437 tok = strtok(NULL, ",");
440 /* Fake things up... */
441 (*ctdb)->num_nodes = numnodes;
443 /* Default to LCP2 */
444 (*ctdb)->tunable.lcp2_public_ip_assignment = 1;
445 (*ctdb)->tunable.deterministic_public_ips = 0;
446 (*ctdb)->tunable.disable_ip_failover = 0;
447 (*ctdb)->tunable.no_ip_failback = 0;
449 if ((t = getenv("CTDB_IP_ALGORITHM"))) {
450 if (strcmp(t, "lcp2") == 0) {
451 (*ctdb)->tunable.lcp2_public_ip_assignment = 1;
452 } else if (strcmp(t, "nondet") == 0) {
453 (*ctdb)->tunable.lcp2_public_ip_assignment = 0;
454 } else if (strcmp(t, "det") == 0) {
455 (*ctdb)->tunable.lcp2_public_ip_assignment = 0;
456 (*ctdb)->tunable.deterministic_public_ips = 1;
458 fprintf(stderr, "ERROR: unknown IP algorithm %s\n", t);
463 tval_noiptakeover = get_tunable_values(*ctdb, numnodes,
464 "CTDB_SET_NoIPTakeover");
465 tval_noiptakeoverondisabled =
466 get_tunable_values(*ctdb, numnodes,
467 "CTDB_SET_NoIPHostOnAllDisabled");
469 runstate = get_runstate(*ctdb, numnodes);
471 nodemap = talloc_array(*ctdb, struct ctdb_node_map, numnodes);
472 nodemap->num = numnodes;
474 if (!read_ips_for_multiple_nodes) {
475 read_ctdb_public_ip_info(*ctdb, numnodes, all_ips, &avail);
478 (*ctdb)->nodes = talloc_array(*ctdb, struct ctdb_node *, numnodes); // FIXME: bogus size, overkill
480 for (i=0; i < numnodes; i++) {
481 nodemap->nodes[i].pnn = i;
482 nodemap->nodes[i].flags = nodeflags[i];
483 /* nodemap->nodes[i].sockaddr is uninitialised */
485 if (read_ips_for_multiple_nodes) {
486 read_ctdb_public_ip_info(*ctdb, numnodes,
490 (*ctdb)->nodes[i] = talloc(*ctdb, struct ctdb_node);
491 (*ctdb)->nodes[i]->pnn = i;
492 (*ctdb)->nodes[i]->flags = nodeflags[i];
493 (*ctdb)->nodes[i]->available_public_ips = avail[i];
494 (*ctdb)->nodes[i]->known_public_ips = avail[i];
497 *ipflags = set_ipflags_internal(*ctdb, *ctdb, nodemap,
499 tval_noiptakeoverondisabled,
503 /* IP layout is read from stdin. */
504 void ctdb_test_lcp2_allocate_unassigned(const char nodestates[])
506 struct ctdb_context *ctdb;
507 struct ctdb_public_ip_list *all_ips;
508 struct ctdb_ipflags *ipflags;
510 uint32_t *lcp2_imbalances;
513 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags, false);
515 lcp2_init(ctdb, ipflags, all_ips, NULL,
516 &lcp2_imbalances, &newly_healthy);
518 lcp2_allocate_unassigned(ctdb, ipflags,
519 all_ips, lcp2_imbalances);
521 print_ctdb_public_ip_list(all_ips);
526 /* IP layout is read from stdin. */
527 void ctdb_test_lcp2_failback(const char nodestates[])
529 struct ctdb_context *ctdb;
530 struct ctdb_public_ip_list *all_ips;
531 struct ctdb_ipflags *ipflags;
533 uint32_t *lcp2_imbalances;
536 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags, false);
538 lcp2_init(ctdb, ipflags, all_ips, NULL,
539 &lcp2_imbalances, &newly_healthy);
541 lcp2_failback(ctdb, ipflags,
542 all_ips, lcp2_imbalances, newly_healthy);
544 print_ctdb_public_ip_list(all_ips);
549 /* IP layout is read from stdin. */
550 void ctdb_test_lcp2_failback_loop(const char nodestates[])
552 struct ctdb_context *ctdb;
553 struct ctdb_public_ip_list *all_ips;
554 struct ctdb_ipflags *ipflags;
556 uint32_t *lcp2_imbalances;
559 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags, false);
561 lcp2_init(ctdb, ipflags, all_ips, NULL,
562 &lcp2_imbalances, &newly_healthy);
564 lcp2_failback(ctdb, ipflags,
565 all_ips, lcp2_imbalances, newly_healthy);
567 print_ctdb_public_ip_list(all_ips);
572 /* IP layout is read from stdin. See comment for ctdb_test_init() for
573 * explanation of read_ips_for_multiple_nodes.
575 void ctdb_test_ctdb_takeover_run_core(const char nodestates[],
576 bool read_ips_for_multiple_nodes)
578 struct ctdb_context *ctdb;
579 struct ctdb_public_ip_list *all_ips;
580 struct ctdb_ipflags *ipflags;
582 ctdb_test_init(nodestates, &ctdb, &all_ips, &ipflags,
583 read_ips_for_multiple_nodes);
585 ctdb_takeover_run_core(ctdb, ipflags, &all_ips, NULL);
587 print_ctdb_public_ip_list(all_ips);
594 fprintf(stderr, "usage: ctdb_takeover_tests <op>\n");
598 int main(int argc, const char *argv[])
600 LogLevel = DEBUG_DEBUG;
601 if (getenv("CTDB_TEST_LOGLEVEL")) {
602 LogLevel = atoi(getenv("CTDB_TEST_LOGLEVEL"));
609 if (strcmp(argv[1], "ip_list") == 0) {
610 ctdb_test_read_ctdb_public_ip_list();
611 } else if (argc == 3 && strcmp(argv[1], "ip_info") == 0) {
612 ctdb_test_read_ctdb_public_ip_info(argv[2]);
613 } else if (strcmp(argv[1], "ip_distance") == 0) {
614 ctdb_test_ip_distance();
615 } else if (argc == 4 && strcmp(argv[1], "ip_distance_2_sum") == 0) {
616 ctdb_test_ip_distance_2_sum(argv[2], atoi(argv[3]));
617 } else if (argc >= 3 && strcmp(argv[1], "lcp2_imbalance") == 0) {
618 ctdb_test_lcp2_imbalance(atoi(argv[2]));
619 } else if (argc == 3 && strcmp(argv[1], "lcp2_allocate_unassigned") == 0) {
620 ctdb_test_lcp2_allocate_unassigned(argv[2]);
621 } else if (argc == 3 && strcmp(argv[1], "lcp2_failback") == 0) {
622 ctdb_test_lcp2_failback(argv[2]);
623 } else if (argc == 3 && strcmp(argv[1], "lcp2_failback_loop") == 0) {
624 ctdb_test_lcp2_failback_loop(argv[2]);
625 } else if (argc == 3 &&
626 strcmp(argv[1], "ctdb_takeover_run_core") == 0) {
627 ctdb_test_ctdb_takeover_run_core(argv[2], false);
628 } else if (argc == 4 &&
629 strcmp(argv[1], "ctdb_takeover_run_core") == 0 &&
630 strcmp(argv[3], "multi") == 0) {
631 ctdb_test_ctdb_takeover_run_core(argv[2], true);