testconfdb.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <signal.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <sys/types.h>
  42. #include <sys/un.h>
  43. #include <corosync/corotypes.h>
  44. #include <corosync/confdb.h>
  45. #define INCDEC_VALUE 45
  46. /* Callbacks are not supported yet */
  47. confdb_callbacks_t callbacks = {
  48. .confdb_key_change_notify_fn = NULL,
  49. .confdb_object_create_change_notify_fn = NULL,
  50. .confdb_object_delete_change_notify_fn = NULL
  51. };
  52. /* Recursively dump the object tree */
  53. static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, int depth)
  54. {
  55. hdb_handle_t object_handle;
  56. char object_name[1024];
  57. size_t object_name_len;
  58. char key_name[1024];
  59. size_t key_name_len;
  60. char key_value[1024];
  61. size_t key_value_len;
  62. int res;
  63. int i;
  64. /* Show the keys */
  65. res = confdb_key_iter_start(handle, parent_object_handle);
  66. if (res != CS_OK) {
  67. printf( "error resetting key iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
  68. return;
  69. }
  70. while ( (res = confdb_key_iter(handle, parent_object_handle, key_name, &key_name_len,
  71. key_value, &key_value_len)) == CS_OK) {
  72. key_name[key_name_len] = '\0';
  73. key_value[key_value_len] = '\0';
  74. for (i=0; i<depth; i++) printf(" ");
  75. printf(" KEY %s=%s\n", key_name, key_value);
  76. }
  77. /* Show sub-objects */
  78. res = confdb_object_iter_start(handle, parent_object_handle);
  79. if (res != CS_OK) {
  80. printf( "error resetting object iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
  81. return;
  82. }
  83. while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == CS_OK) {
  84. hdb_handle_t parent;
  85. res = confdb_object_parent_get(handle, object_handle, &parent);
  86. if (res != CS_OK) {
  87. printf( "error getting parent for object "HDB_X_FORMAT": %d\n", object_handle, res);
  88. return;
  89. }
  90. for (i=0; i<depth; i++) printf(" ");
  91. object_name[object_name_len] = '\0';
  92. printf("OBJECT: %s ("HDB_X_FORMAT", parent: "HDB_X_FORMAT")\n", object_name, object_handle, parent);
  93. /* Down we go ... */
  94. print_config_tree(handle, object_handle, depth+1);
  95. }
  96. }
  97. static void do_write_tests(confdb_handle_t handle)
  98. {
  99. int res;
  100. unsigned int incdec_value;
  101. hdb_handle_t object_handle;
  102. char error_string[1024];
  103. /* Add a scratch object and put some keys into it */
  104. res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, "testconfdb", strlen("testconfdb"), &object_handle);
  105. if (res != CS_OK) {
  106. printf( "error creating 'testconfdb' object: %d\n", res);
  107. return;
  108. }
  109. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "one", strlen("one"));
  110. if (res != CS_OK) {
  111. printf( "error creating 'testconfdb' key 1: %d\n", res);
  112. return;
  113. }
  114. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"));
  115. if (res != CS_OK) {
  116. printf( "error creating 'testconfdb' key 2: %d\n", res);
  117. return;
  118. }
  119. res = confdb_key_create(handle, object_handle, "grot", strlen("grot"), "perrins", strlen("perrins"));
  120. if (res != CS_OK) {
  121. printf( "error creating 'testconfdb' key 3: %d\n", res);
  122. return;
  123. }
  124. res = confdb_key_replace(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"),
  125. "newtwo", strlen("newtwo"));
  126. if (res != CS_OK) {
  127. printf( "error replace 'testconfdb' key 2: %d\n", res);
  128. return;
  129. }
  130. /* Print it for verification */
  131. print_config_tree(handle, object_handle, 0);
  132. incdec_value = INCDEC_VALUE;
  133. res = confdb_key_create(handle, object_handle, "incdec", strlen("incdec"), &incdec_value, sizeof(incdec_value));
  134. if (res != CS_OK) {
  135. printf( "error creating 'testconfdb' key 4: %d\n", res);
  136. return;
  137. }
  138. res = confdb_key_increment(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
  139. if (res != CS_OK) {
  140. printf( "error incrementing 'testconfdb' key 4: %d\n", res);
  141. return;
  142. }
  143. if (incdec_value == INCDEC_VALUE+1)
  144. printf("incremented value = %d\n", incdec_value);
  145. else
  146. printf("ERROR: incremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE+1);
  147. res = confdb_key_decrement(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
  148. if (res != CS_OK) {
  149. printf( "error decrementing 'testconfdb' key 4: %d\n", res);
  150. return;
  151. }
  152. if (incdec_value == INCDEC_VALUE)
  153. printf("decremented value = %d\n", incdec_value);
  154. else
  155. printf("ERROR: decremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE);
  156. printf("-------------------------\n");
  157. /* Remove it.
  158. Check that it doesn't exist when the full tree dump runs next */
  159. res = confdb_object_destroy(handle, object_handle);
  160. if (res != CS_OK) {
  161. printf( "error destroying 'testconfdb' object: %d\n", res);
  162. return;
  163. }
  164. res = confdb_write(handle, error_string, sizeof error_string);
  165. printf("confdb_write returned %d: %s\n", res, error_string);
  166. }
  167. int main (int argc, char *argv[]) {
  168. confdb_handle_t handle;
  169. int result;
  170. hdb_handle_t totem_handle;
  171. char key_value[256];
  172. size_t value_len;
  173. result = confdb_initialize (&handle, &callbacks);
  174. if (result != CS_OK) {
  175. printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result);
  176. exit (1);
  177. }
  178. if (argv[1] && strcmp(argv[1], "write")==0)
  179. do_write_tests(handle);
  180. if (argv[1] && strcmp(argv[1], "reload")==0) {
  181. /* Test reload interface */
  182. result = confdb_reload(handle, 0, key_value, sizeof key_value);
  183. printf ("Try to reload the config (noflush): %d (should be 1)\n", result);
  184. result = confdb_reload(handle, 1, key_value, sizeof key_value);
  185. printf ("Try to reload the config (flush): %d (should be 1)\n", result);
  186. }
  187. /* Test iterators */
  188. print_config_tree(handle, OBJECT_PARENT_HANDLE, 0);
  189. /* Find "totem" and dump bits of it again, to test the direct APIs */
  190. result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
  191. if (result != CS_OK) {
  192. printf ("Could not start object_find %d\n", result);
  193. exit (1);
  194. }
  195. result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
  196. if (result != CS_OK) {
  197. printf ("Could not object_find \"totem\": %d\n", result);
  198. exit (1);
  199. }
  200. result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
  201. if (result != CS_OK) {
  202. printf ("Could not get \"version\" key: %d\n", result);
  203. exit (1);
  204. }
  205. key_value[value_len] = '\0';
  206. printf("totem/version = '%s'\n", key_value);
  207. result = confdb_key_get(handle, totem_handle, "secauth", strlen("secauth"), key_value, &value_len);
  208. if (result != CS_OK) {
  209. printf ("Could not get \"secauth\" key: %d\n", result);
  210. exit (1);
  211. }
  212. key_value[value_len] = '\0';
  213. printf("totem/secauth = '%s'\n", key_value);
  214. /* Try a call that fails */
  215. result = confdb_key_get(handle, totem_handle, "grot", strlen("grot"), key_value, &value_len);
  216. printf ("Get \"grot\" key returned: %d (should fail)\n", result);
  217. result = confdb_finalize (handle);
  218. printf ("Finalize result is %d (should be 1)\n", result);
  219. return (0);
  220. }