~ubuntu-branches/ubuntu/precise/corosync/precise-proposed

« back to all changes in this revision

Viewing changes to test/testconfdb.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-21 09:29:56 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090821092956-w9qxxxx3zeoh8dem
Tags: 1.0.0-4ubuntu2
* debian/control:
  - 'Ubuntu Developers' instead of 'Ubuntu Core Developers'
    as maintainer
  - Bump debhelper dependecy to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2008 Red Hat Inc
 
2
 * Copyright (c) 2008, 2009 Red Hat Inc
3
3
 *
4
4
 * All rights reserved.
5
5
 *
32
32
 * THE POSSIBILITY OF SUCH DAMAGE.
33
33
 */
34
34
 
 
35
#include <config.h>
 
36
 
35
37
#include <stdio.h>
36
38
#include <stdlib.h>
37
39
#include <errno.h>
38
 
#include <signal.h>
39
40
#include <unistd.h>
40
41
#include <string.h>
41
42
#include <sys/types.h>
42
43
#include <sys/un.h>
43
44
 
44
 
#include <corosync/saAis.h>
 
45
#include <corosync/corotypes.h>
45
46
#include <corosync/confdb.h>
46
47
 
47
48
#define INCDEC_VALUE 45
54
55
};
55
56
 
56
57
/* Recursively dump the object tree */
57
 
static void print_config_tree(confdb_handle_t handle, unsigned int parent_object_handle, int depth)
 
58
static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, int depth)
58
59
{
59
 
        unsigned int object_handle;
 
60
        hdb_handle_t object_handle;
60
61
        char object_name[1024];
61
 
        int object_name_len;
 
62
        size_t object_name_len;
62
63
        char key_name[1024];
63
 
        int key_name_len;
 
64
        size_t key_name_len;
64
65
        char key_value[1024];
65
 
        int key_value_len;
 
66
        size_t key_value_len;
66
67
        int res;
67
68
        int i;
68
69
 
69
70
        /* Show the keys */
70
71
        res = confdb_key_iter_start(handle, parent_object_handle);
71
 
        if (res != SA_AIS_OK) {
72
 
                printf( "error resetting key iterator for object %d: %d\n", parent_object_handle, res);
 
72
        if (res != CS_OK) {
 
73
                printf( "error resetting key iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
73
74
                return;
74
75
        }
75
76
 
76
77
        while ( (res = confdb_key_iter(handle, parent_object_handle, key_name, &key_name_len,
77
 
                                       key_value, &key_value_len)) == SA_AIS_OK) {
 
78
                                       key_value, &key_value_len)) == CS_OK) {
78
79
                key_name[key_name_len] = '\0';
79
80
                key_value[key_value_len] = '\0';
80
81
                for (i=0; i<depth; i++) printf("  ");
83
84
 
84
85
        /* Show sub-objects */
85
86
        res = confdb_object_iter_start(handle, parent_object_handle);
86
 
        if (res != SA_AIS_OK) {
87
 
                printf( "error resetting object iterator for object %d: %d\n", parent_object_handle, res);
 
87
        if (res != CS_OK) {
 
88
                printf( "error resetting object iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
88
89
                return;
89
90
        }
90
91
 
91
 
        while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == SA_AIS_OK)   {
92
 
                unsigned int parent;
 
92
        while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == CS_OK)       {
 
93
                hdb_handle_t parent;
93
94
 
94
95
                res = confdb_object_parent_get(handle, object_handle, &parent);
95
 
                if (res != SA_AIS_OK) {
96
 
                        printf( "error getting parent for object %d: %d\n", object_handle, res);
 
96
                if (res != CS_OK) {
 
97
                        printf( "error getting parent for object "HDB_X_FORMAT": %d\n", object_handle, res);
97
98
                        return;
98
99
                }
99
100
 
100
101
                for (i=0; i<depth; i++) printf("  ");
101
102
 
102
103
                object_name[object_name_len] = '\0';
103
 
                printf("OBJECT: %s (%u, parent: %u)\n", object_name, object_handle, parent);
 
104
                printf("OBJECT: %s ("HDB_X_FORMAT", parent: "HDB_X_FORMAT")\n", object_name, object_handle, parent);
104
105
 
105
106
                /* Down we go ... */
106
107
                print_config_tree(handle, object_handle, depth+1);
111
112
{
112
113
        int res;
113
114
        unsigned int incdec_value;
114
 
        unsigned int object_handle;
 
115
        hdb_handle_t object_handle;
115
116
        char error_string[1024];
116
117
 
117
118
        /* Add a scratch object and put some keys into it */
118
 
        res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, (void *)"testconfdb", strlen("testconfdb"), &object_handle);
119
 
        if (res != SA_AIS_OK) {
 
119
        res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, "testconfdb", strlen("testconfdb"), &object_handle);
 
120
        if (res != CS_OK) {
120
121
                printf( "error creating 'testconfdb' object: %d\n", res);
121
122
                return;
122
123
        }
123
124
 
124
125
        res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "one", strlen("one"));
125
 
        if (res != SA_AIS_OK) {
 
126
        if (res != CS_OK) {
126
127
                printf( "error creating 'testconfdb' key 1: %d\n", res);
127
128
                return;
128
129
        }
129
130
 
130
131
        res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"));
131
 
        if (res != SA_AIS_OK) {
 
132
        if (res != CS_OK) {
132
133
                printf( "error creating 'testconfdb' key 2: %d\n", res);
133
134
                return;
134
135
        }
135
136
 
136
137
        res = confdb_key_create(handle, object_handle, "grot", strlen("grot"), "perrins", strlen("perrins"));
137
 
        if (res != SA_AIS_OK) {
 
138
        if (res != CS_OK) {
138
139
                printf( "error creating 'testconfdb' key 3: %d\n", res);
139
140
                return;
140
141
        }
142
143
        res = confdb_key_replace(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"),
143
144
                                 "newtwo", strlen("newtwo"));
144
145
 
145
 
        if (res != SA_AIS_OK) {
 
146
        if (res != CS_OK) {
146
147
                printf( "error replace 'testconfdb' key 2: %d\n", res);
147
148
                return;
148
149
        }
152
153
 
153
154
        incdec_value = INCDEC_VALUE;
154
155
        res = confdb_key_create(handle, object_handle, "incdec", strlen("incdec"), &incdec_value, sizeof(incdec_value));
155
 
        if (res != SA_AIS_OK) {
 
156
        if (res != CS_OK) {
156
157
                printf( "error creating 'testconfdb' key 4: %d\n", res);
157
158
                return;
158
159
        }
159
160
        res = confdb_key_increment(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
160
 
        if (res != SA_AIS_OK) {
 
161
        if (res != CS_OK) {
161
162
                printf( "error incrementing 'testconfdb' key 4: %d\n", res);
162
163
                return;
163
164
        }
167
168
                printf("ERROR: incremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE+1);
168
169
 
169
170
        res = confdb_key_decrement(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
170
 
        if (res != SA_AIS_OK) {
 
171
        if (res != CS_OK) {
171
172
                printf( "error decrementing 'testconfdb' key 4: %d\n", res);
172
173
                return;
173
174
        }
181
182
        /* Remove it.
182
183
           Check that it doesn't exist when the full tree dump runs next */
183
184
        res = confdb_object_destroy(handle, object_handle);
184
 
        if (res != SA_AIS_OK) {
 
185
        if (res != CS_OK) {
185
186
                printf( "error destroying 'testconfdb' object: %d\n", res);
186
187
                return;
187
188
        }
188
189
 
189
 
        res = confdb_write(handle, error_string);
 
190
        res = confdb_write(handle, error_string, sizeof error_string);
190
191
        printf("confdb_write returned %d: %s\n", res, error_string);
191
192
}
192
193
 
194
195
int main (int argc, char *argv[]) {
195
196
        confdb_handle_t handle;
196
197
        int result;
197
 
        unsigned int totem_handle;
 
198
        hdb_handle_t totem_handle;
198
199
        char key_value[256];
199
 
        int value_len;
 
200
        size_t value_len;
200
201
 
201
202
        result = confdb_initialize (&handle, &callbacks);
202
 
        if (result != SA_AIS_OK) {
 
203
        if (result != CS_OK) {
203
204
                printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result);
204
205
                exit (1);
205
206
        }
209
210
 
210
211
        if (argv[1] && strcmp(argv[1], "reload")==0) {
211
212
                /* Test reload interface */
212
 
                result = confdb_reload(handle, 0, key_value);
 
213
                result = confdb_reload(handle, 0, key_value, sizeof key_value);
213
214
                printf ("Try to reload the config (noflush): %d (should be 1)\n", result);
214
215
 
215
 
                result = confdb_reload(handle, 1, key_value);
 
216
                result = confdb_reload(handle, 1, key_value, sizeof key_value);
216
217
                printf ("Try to reload the config (flush): %d (should be 1)\n", result);
217
218
        }
218
219
 
221
222
 
222
223
        /* Find "totem" and dump bits of it again, to test the direct APIs */
223
224
        result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
224
 
        if (result != SA_AIS_OK) {
 
225
        if (result != CS_OK) {
225
226
                printf ("Could not start object_find %d\n", result);
226
227
                exit (1);
227
228
        }
228
229
 
229
230
        result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
230
 
        if (result != SA_AIS_OK) {
 
231
        if (result != CS_OK) {
231
232
                printf ("Could not object_find \"totem\": %d\n", result);
232
233
                exit (1);
233
234
        }
234
235
 
235
236
        result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
236
 
        if (result != SA_AIS_OK) {
 
237
        if (result != CS_OK) {
237
238
                printf ("Could not get \"version\" key: %d\n", result);
238
239
                exit (1);
239
240
        }
241
242
        printf("totem/version = '%s'\n", key_value);
242
243
 
243
244
        result = confdb_key_get(handle, totem_handle, "secauth", strlen("secauth"), key_value, &value_len);
244
 
        if (result != SA_AIS_OK) {
 
245
        if (result != CS_OK) {
245
246
                printf ("Could not get \"secauth\" key: %d\n", result);
246
247
                exit (1);
247
248
        }