4
0

testconfdb.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2008, 2009 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/un.h>
  42. #include <corosync/corotypes.h>
  43. #include <corosync/confdb.h>
  44. #define INCDEC_VALUE 45
  45. /* Callbacks are not supported yet */
  46. confdb_callbacks_t callbacks = {
  47. .confdb_key_change_notify_fn = NULL,
  48. .confdb_object_create_change_notify_fn = NULL,
  49. .confdb_object_delete_change_notify_fn = NULL
  50. };
  51. /* Recursively dump the object tree */
  52. static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, int depth)
  53. {
  54. hdb_handle_t object_handle;
  55. char object_name[1024];
  56. size_t object_name_len;
  57. char key_name[1024];
  58. size_t key_name_len;
  59. char key_value[1024];
  60. size_t key_value_len;
  61. int res;
  62. int i;
  63. /* Show the keys */
  64. res = confdb_key_iter_start(handle, parent_object_handle);
  65. if (res != CS_OK) {
  66. printf( "error resetting key iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
  67. return;
  68. }
  69. while ( (res = confdb_key_iter(handle, parent_object_handle, key_name, &key_name_len,
  70. key_value, &key_value_len)) == CS_OK) {
  71. key_name[key_name_len] = '\0';
  72. key_value[key_value_len] = '\0';
  73. for (i=0; i<depth; i++) printf(" ");
  74. printf(" KEY %s=%s\n", key_name, key_value);
  75. }
  76. /* Show sub-objects */
  77. res = confdb_object_iter_start(handle, parent_object_handle);
  78. if (res != CS_OK) {
  79. printf( "error resetting object iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
  80. return;
  81. }
  82. while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == CS_OK) {
  83. hdb_handle_t parent;
  84. res = confdb_object_parent_get(handle, object_handle, &parent);
  85. if (res != CS_OK) {
  86. printf( "error getting parent for object "HDB_X_FORMAT": %d\n", object_handle, res);
  87. return;
  88. }
  89. for (i=0; i<depth; i++) printf(" ");
  90. object_name[object_name_len] = '\0';
  91. printf("OBJECT: %s ("HDB_X_FORMAT", parent: "HDB_X_FORMAT")\n", object_name, object_handle, parent);
  92. /* Down we go ... */
  93. print_config_tree(handle, object_handle, depth+1);
  94. }
  95. }
  96. static void do_write_tests(confdb_handle_t handle)
  97. {
  98. int res;
  99. unsigned int incdec_value;
  100. hdb_handle_t object_handle;
  101. char error_string[1024];
  102. /* Add a scratch object and put some keys into it */
  103. res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, "testconfdb", strlen("testconfdb"), &object_handle);
  104. if (res != CS_OK) {
  105. printf( "error creating 'testconfdb' object: %d\n", res);
  106. return;
  107. }
  108. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "one", strlen("one"));
  109. if (res != CS_OK) {
  110. printf( "error creating 'testconfdb' key 1: %d\n", res);
  111. return;
  112. }
  113. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"));
  114. if (res != CS_OK) {
  115. printf( "error creating 'testconfdb' key 2: %d\n", res);
  116. return;
  117. }
  118. res = confdb_key_create(handle, object_handle, "grot", strlen("grot"), "perrins", strlen("perrins"));
  119. if (res != CS_OK) {
  120. printf( "error creating 'testconfdb' key 3: %d\n", res);
  121. return;
  122. }
  123. res = confdb_key_replace(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"),
  124. "newtwo", strlen("newtwo"));
  125. if (res != CS_OK) {
  126. printf( "error replace 'testconfdb' key 2: %d\n", res);
  127. return;
  128. }
  129. /* Print it for verification */
  130. print_config_tree(handle, object_handle, 0);
  131. incdec_value = INCDEC_VALUE;
  132. res = confdb_key_create(handle, object_handle, "incdec", strlen("incdec"), &incdec_value, sizeof(incdec_value));
  133. if (res != CS_OK) {
  134. printf( "error creating 'testconfdb' key 4: %d\n", res);
  135. return;
  136. }
  137. res = confdb_key_increment(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
  138. if (res != CS_OK) {
  139. printf( "error incrementing 'testconfdb' key 4: %d\n", res);
  140. return;
  141. }
  142. if (incdec_value == INCDEC_VALUE+1)
  143. printf("incremented value = %d\n", incdec_value);
  144. else
  145. printf("ERROR: incremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE+1);
  146. res = confdb_key_decrement(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
  147. if (res != CS_OK) {
  148. printf( "error decrementing 'testconfdb' key 4: %d\n", res);
  149. return;
  150. }
  151. if (incdec_value == INCDEC_VALUE)
  152. printf("decremented value = %d\n", incdec_value);
  153. else
  154. printf("ERROR: decremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE);
  155. printf("-------------------------\n");
  156. /* Remove it.
  157. Check that it doesn't exist when the full tree dump runs next */
  158. res = confdb_object_destroy(handle, object_handle);
  159. if (res != CS_OK) {
  160. printf( "error destroying 'testconfdb' object: %d\n", res);
  161. return;
  162. }
  163. res = confdb_write(handle, error_string, sizeof error_string);
  164. printf("confdb_write returned %d: %s\n", res, error_string);
  165. }
  166. int main (int argc, char *argv[]) {
  167. confdb_handle_t handle;
  168. int result;
  169. hdb_handle_t totem_handle;
  170. char key_value[256];
  171. size_t value_len;
  172. result = confdb_initialize (&handle, &callbacks);
  173. if (result != CS_OK) {
  174. printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result);
  175. exit (1);
  176. }
  177. if (argv[1] && strcmp(argv[1], "write")==0)
  178. do_write_tests(handle);
  179. if (argv[1] && strcmp(argv[1], "reload")==0) {
  180. /* Test reload interface */
  181. result = confdb_reload(handle, 0, key_value, sizeof key_value);
  182. printf ("Try to reload the config (noflush): %d (should be 1)\n", result);
  183. result = confdb_reload(handle, 1, key_value, sizeof key_value);
  184. printf ("Try to reload the config (flush): %d (should be 1)\n", result);
  185. }
  186. /* Test iterators */
  187. print_config_tree(handle, OBJECT_PARENT_HANDLE, 0);
  188. /* Find "totem" and dump bits of it again, to test the direct APIs */
  189. result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
  190. if (result != CS_OK) {
  191. printf ("Could not start object_find %d\n", result);
  192. exit (1);
  193. }
  194. result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
  195. if (result != CS_OK) {
  196. printf ("Could not object_find \"totem\": %d\n", result);
  197. exit (1);
  198. }
  199. result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
  200. if (result != CS_OK) {
  201. printf ("Could not get \"version\" key: %d\n", result);
  202. exit (1);
  203. }
  204. key_value[value_len] = '\0';
  205. printf("totem/version = '%s'\n", key_value);
  206. result = confdb_key_get(handle, totem_handle, "secauth", strlen("secauth"), key_value, &value_len);
  207. if (result != CS_OK) {
  208. printf ("Could not get \"secauth\" key: %d\n", result);
  209. exit (1);
  210. }
  211. key_value[value_len] = '\0';
  212. printf("totem/secauth = '%s'\n", key_value);
  213. /* Try a call that fails */
  214. result = confdb_key_get(handle, totem_handle, "grot", strlen("grot"), key_value, &value_len);
  215. printf ("Get \"grot\" key returned: %d (should fail)\n", result);
  216. result = confdb_finalize (handle);
  217. printf ("Finalize result is %d (should be 1)\n", result);
  218. return (0);
  219. }