TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / tests / src / ctdb_trackingdb_test.c
1 /* 
2    simple trackingdb test tool
3
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()
8
9    Copyright (C) Ronnie Sahlberg 2009
10
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.
15    
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.
20    
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/>.
23 */
24
25 #include <stdlib.h>
26 #include <time.h>
27 #include "includes.h"
28 #include "lib/tevent/tevent.h"
29 #include "system/filesys.h"
30 #include "popt.h"
31 #include "cmdline.h"
32 #include "ctdb_private.h"
33 #include "db_wrap.h"
34
35 #define MAXINDEX 64
36 char indices[MAXINDEX];
37
38 void vn_cb(struct ctdb_context *ctdb, uint32_t pnn, void *private_data)
39 {
40         char *ind = private_data;
41
42         printf("Callback for node %d\n", pnn);
43         if (ind[pnn] == 0) {
44                 printf("ERROR, node %d from callback was never added\n", pnn);
45                 exit(10);
46         }
47         ind[pnn] = 0;
48 }
49
50 void verify_nodes(struct ctdb_context *ctdb, TDB_DATA data)
51 {
52         int i;
53
54         printf("Verify the nodes\n");
55         ctdb_trackingdb_traverse(ctdb, data, vn_cb, indices);
56         for(i = 0; i < MAXINDEX; i++) {
57                 if (indices[i] != 0) {
58                         printf("Callback for %d was never invoked\n", i);
59                         exit(0);
60                 }
61         }
62 }
63
64         
65         
66 void add_node(struct ctdb_context *ctdb, TDB_DATA *data, int pnn)
67 {
68         printf("Add node %d\n", pnn);
69         if (ctdb_trackingdb_add_pnn(ctdb, data, pnn)) {
70                 printf("Failed to add tracking db data\n");
71                 exit(10);
72         }
73         indices[pnn] = 1;
74 }
75
76 static void trackdb_test(struct ctdb_context *ctdb)
77 {
78         TDB_DATA data = {NULL,0};
79         int i;
80
81         printf("Add 10 nodes\n");
82         srandom(time(NULL));
83         for(i=0; i<10; i++) {
84                 add_node(ctdb, &data, random()%MAXINDEX);
85         }
86
87         verify_nodes(ctdb, data);
88         printf("OK all seems well\n");
89 }
90
91 /*
92   main program
93 */
94 int main(int argc, const char *argv[])
95 {
96         struct ctdb_context *ctdb;
97
98         struct poptOption popt_options[] = {
99                 POPT_AUTOHELP
100                 POPT_CTDB_CMDLINE
101                 POPT_TABLEEND
102         };
103         int opt;
104         const char **extra_argv;
105         int extra_argc = 0;
106         poptContext pc;
107         struct event_context *ev;
108
109         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
110
111         while ((opt = poptGetNextOpt(pc)) != -1) {
112                 switch (opt) {
113                 default:
114                         fprintf(stderr, "Invalid option %s: %s\n", 
115                                 poptBadOption(pc, 0), poptStrerror(opt));
116                         exit(1);
117                 }
118         }
119
120         /* setup the remaining options for the main program to use */
121         extra_argv = poptGetArgs(pc);
122         if (extra_argv) {
123                 extra_argv++;
124                 while (extra_argv[extra_argc]) extra_argc++;
125         }
126
127         ev = event_context_init(NULL);
128
129         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(5, 0));
130
131         trackdb_test(ctdb);
132
133         return 0;
134 }