ctdb-util: Rename db_wrap to tdb_wrap and make it a build subsystem
[Samba.git] / ctdb / tests / src / ctdb_trackingdb_test.c
blob18bc174382f6abc5c33c527b8cd550091d0a5e38
1 /*
2 simple trackingdb test tool
4 This program is used to test the funcitons to manipulate and enumerate
5 the trackingdb records :
6 ctdb_trackingdb_add_pnn()
7 ctdb_trackingdb_traverse()
9 Copyright (C) Ronnie Sahlberg 2009
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "system/filesys.h"
27 #include "system/time.h"
28 #include "popt.h"
29 #include "cmdline.h"
30 #include "ctdb_private.h"
31 #include "lib/tdb_wrap/tdb_wrap.h"
33 #define MAXINDEX 64
34 char indices[MAXINDEX];
36 static void vn_cb(struct ctdb_context *ctdb, uint32_t pnn, void *private_data)
38 char *ind = private_data;
40 printf("Callback for node %d\n", pnn);
41 if (ind[pnn] == 0) {
42 printf("ERROR, node %d from callback was never added\n", pnn);
43 exit(10);
45 ind[pnn] = 0;
48 static void verify_nodes(struct ctdb_context *ctdb, TDB_DATA data)
50 int i;
52 printf("Verify the nodes\n");
53 ctdb_trackingdb_traverse(ctdb, data, vn_cb, indices);
54 for(i = 0; i < MAXINDEX; i++) {
55 if (indices[i] != 0) {
56 printf("Callback for %d was never invoked\n", i);
57 exit(0);
64 static void add_node(struct ctdb_context *ctdb, TDB_DATA *data, int pnn)
66 printf("Add node %d\n", pnn);
67 if (ctdb_trackingdb_add_pnn(ctdb, data, pnn)) {
68 printf("Failed to add tracking db data\n");
69 exit(10);
71 indices[pnn] = 1;
74 static void trackdb_test(struct ctdb_context *ctdb)
76 TDB_DATA data = {NULL,0};
77 int i;
79 printf("Add 10 nodes\n");
80 srandom(time(NULL));
81 for(i=0; i<10; i++) {
82 add_node(ctdb, &data, random()%MAXINDEX);
85 verify_nodes(ctdb, data);
86 printf("OK all seems well\n");
90 main program
92 int main(int argc, const char *argv[])
94 struct ctdb_context *ctdb;
96 struct poptOption popt_options[] = {
97 POPT_AUTOHELP
98 POPT_CTDB_CMDLINE
99 POPT_TABLEEND
101 int opt;
102 const char **extra_argv;
103 int extra_argc = 0;
104 poptContext pc;
105 struct event_context *ev;
107 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
109 while ((opt = poptGetNextOpt(pc)) != -1) {
110 switch (opt) {
111 default:
112 fprintf(stderr, "Invalid option %s: %s\n",
113 poptBadOption(pc, 0), poptStrerror(opt));
114 exit(1);
118 /* setup the remaining options for the main program to use */
119 extra_argv = poptGetArgs(pc);
120 if (extra_argv) {
121 extra_argv++;
122 while (extra_argv[extra_argc]) extra_argc++;
125 ev = event_context_init(NULL);
127 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(5, 0));
128 if (ctdb == NULL) {
129 exit(1);
132 trackdb_test(ctdb);
134 return 0;