4
0

testsam.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@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 Red Hat, 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. /*
  35. * Provides test of SAM API
  36. */
  37. #include <config.h>
  38. #include <sys/types.h>
  39. #include <stdio.h>
  40. #include <stdint.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <corosync/corotypes.h>
  44. #include <corosync/sam.h>
  45. #include <signal.h>
  46. #include <sys/wait.h>
  47. static int test2_sig_delivered = 0;
  48. static int test4_hc_cb_count = 0;
  49. /*
  50. * First test will just register SAM, with policy restart. First instance will
  51. * sleep one second, send hc and sleep another 3 seconds. This should force restart.
  52. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
  53. * Then start hc again and sleep 3 seconds. This should force restart again.
  54. * Last instance just calls initialize again. This should end with error.
  55. * Then call start, followed by stop and start again. Finally, we will call finalize
  56. * twice. One should succeed, second should fail. After this, we will call every function
  57. * (none should succeed).
  58. */
  59. static int test1 (void)
  60. {
  61. cs_error_t error;
  62. unsigned int instance_id;
  63. int i;
  64. printf ("%s: initialize\n", __FUNCTION__);
  65. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  66. if (error != CS_OK) {
  67. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  68. return 1;
  69. }
  70. printf ("%s: register\n", __FUNCTION__);
  71. error = sam_register (&instance_id);
  72. if (error != CS_OK) {
  73. fprintf (stderr, "Can't register. Error %d\n", error);
  74. return 1;
  75. }
  76. if (instance_id == 1 || instance_id == 2) {
  77. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  78. error = sam_start ();
  79. if (error != CS_OK) {
  80. fprintf (stderr, "Can't start hc. Error %d\n", error);
  81. return 1;
  82. }
  83. for (i = 0; i < 10; i++) {
  84. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  85. sleep (1);
  86. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  87. error = sam_hc_send ();
  88. if (error != CS_OK) {
  89. fprintf (stderr, "Can't send hc. Error %d\n", error);
  90. return 1;
  91. }
  92. }
  93. if (instance_id == 2) {
  94. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  95. error = sam_stop ();
  96. if (error != CS_OK) {
  97. fprintf (stderr, "Can't send hc. Error %d\n", error);
  98. return 1;
  99. }
  100. }
  101. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  102. sleep (3);
  103. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  104. error = sam_start ();
  105. if (error != CS_OK) {
  106. fprintf (stderr, "Can't start hc. Error %d\n", error);
  107. return 1;
  108. }
  109. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  110. sleep (3);
  111. return 0;
  112. }
  113. if (instance_id == 3) {
  114. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  115. if (error == CS_OK) {
  116. fprintf (stderr, "Can initialize SAM API after initialization");
  117. return 1;
  118. }
  119. error = sam_start ();
  120. if (error != CS_OK) {
  121. fprintf (stderr, "Can't start hc. Error %d\n", error);
  122. return 1;
  123. }
  124. error = sam_stop ();
  125. if (error != CS_OK) {
  126. fprintf (stderr, "Can't stop hc. Error %d\n", error);
  127. return 1;
  128. }
  129. error = sam_finalize ();
  130. if (error != CS_OK) {
  131. fprintf (stderr, "Can't finalize sam. Error %d\n", error);
  132. return 1;
  133. }
  134. error = sam_finalize ();
  135. if (error == CS_OK) {
  136. fprintf (stderr, "Can finalize sam after finalization!\n");
  137. return 1;
  138. }
  139. if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
  140. sam_start () == CS_OK || sam_stop () == CS_OK ||
  141. sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
  142. sam_hc_callback_register (NULL) == CS_OK) {
  143. fprintf (stderr, "Can call one of function after finalization!\n");
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static void test2_signal (int sig) {
  151. printf ("%s\n", __FUNCTION__);
  152. test2_sig_delivered = 1;
  153. }
  154. /*
  155. * This tests recovery policy quit and callback.
  156. */
  157. static int test2 (void) {
  158. cs_error_t error;
  159. unsigned int instance_id;
  160. printf ("%s: initialize\n", __FUNCTION__);
  161. error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
  162. if (error != CS_OK) {
  163. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  164. return 1;
  165. }
  166. printf ("%s: register\n", __FUNCTION__);
  167. error = sam_register (&instance_id);
  168. if (error != CS_OK) {
  169. fprintf (stderr, "Can't register. Error %d\n", error);
  170. return 1;
  171. }
  172. if (instance_id == 1) {
  173. signal (SIGTERM, test2_signal);
  174. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  175. error = sam_start ();
  176. if (error != CS_OK) {
  177. fprintf (stderr, "Can't start hc. Error %d\n", error);
  178. return 1;
  179. }
  180. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  181. sleep (1);
  182. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  183. error = sam_hc_send ();
  184. if (error != CS_OK) {
  185. fprintf (stderr, "Can't send hc. Error %d\n", error);
  186. return 1;
  187. }
  188. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  189. while (!test2_sig_delivered) {
  190. sleep (1);
  191. }
  192. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  193. sleep (3);
  194. }
  195. return 1;
  196. }
  197. /*
  198. * Smoke test. Better to turn off coredump ;) This has no time limit, just restart process
  199. * when it dies.
  200. */
  201. static int test3 (void) {
  202. cs_error_t error;
  203. unsigned int instance_id;
  204. int tmp1, tmp2, tmp3;
  205. printf ("%s: initialize\n", __FUNCTION__);
  206. error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  207. if (error != CS_OK) {
  208. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  209. return 1;
  210. }
  211. printf ("%s: register\n", __FUNCTION__);
  212. error = sam_register (&instance_id);
  213. if (error != CS_OK) {
  214. fprintf (stderr, "Can't register. Error %d\n", error);
  215. return 1;
  216. }
  217. if (instance_id < 100) {
  218. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  219. error = sam_start ();
  220. if (error != CS_OK) {
  221. fprintf (stderr, "Can't start hc. Error %d\n", error);
  222. return 1;
  223. }
  224. printf ("%s iid %d: divide by zero\n", __FUNCTION__, instance_id);
  225. tmp2 = rand ();
  226. tmp3 = 0;
  227. tmp1 = tmp2 / tmp3;
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static int test4_hc_cb (void)
  233. {
  234. printf ("%s %d\n", __FUNCTION__, ++test4_hc_cb_count);
  235. if (test4_hc_cb_count > 10)
  236. return 1;
  237. return 0;
  238. }
  239. /*
  240. * Test event driven healtchecking.
  241. */
  242. static int test4 (void)
  243. {
  244. cs_error_t error;
  245. unsigned int instance_id;
  246. printf ("%s: initialize\n", __FUNCTION__);
  247. error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
  248. if (error != CS_OK) {
  249. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  250. return 1;
  251. }
  252. printf ("%s: register\n", __FUNCTION__);
  253. error = sam_register (&instance_id);
  254. if (error != CS_OK) {
  255. fprintf (stderr, "Can't register. Error %d\n", error);
  256. return 1;
  257. }
  258. if (instance_id == 1) {
  259. printf ("%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
  260. error = sam_hc_callback_register (test4_hc_cb);
  261. if (error != CS_OK) {
  262. fprintf (stderr, "Can't register hc cb. Error %d\n", error);
  263. return 1;
  264. }
  265. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  266. error = sam_start ();
  267. if (error != CS_OK) {
  268. fprintf (stderr, "Can't start hc. Error %d\n", error);
  269. return 1;
  270. }
  271. sleep (2);
  272. printf ("%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  273. return 1;
  274. }
  275. if (instance_id == 2) {
  276. return 0;
  277. }
  278. return 1;
  279. }
  280. int main(int argc, char *argv[])
  281. {
  282. pid_t pid;
  283. int err;
  284. int stat;
  285. int all_passed = 1;
  286. pid = fork ();
  287. if (pid == -1) {
  288. fprintf (stderr, "Can't fork\n");
  289. return 1;
  290. }
  291. if (pid == 0) {
  292. err = test1 ();
  293. fprintf (stderr, "test1 %s\n", (err == 0 ? "passed" : "failed"));
  294. if (err != 0)
  295. all_passed = 0;
  296. return err;
  297. }
  298. waitpid (pid, NULL, 0);
  299. pid = fork ();
  300. if (pid == -1) {
  301. fprintf (stderr, "Can't fork\n");
  302. return 1;
  303. }
  304. if (pid == 0) {
  305. err = test2 ();
  306. return err;
  307. }
  308. waitpid (pid, &stat, 0);
  309. fprintf (stderr, "test2 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  310. if (WEXITSTATUS (stat) != 0)
  311. all_passed = 0;
  312. pid = fork ();
  313. if (pid == -1) {
  314. fprintf (stderr, "Can't fork\n");
  315. return 1;
  316. }
  317. if (pid == 0) {
  318. err = test3 ();
  319. fprintf (stderr, "test3 %s\n", (err == 0 ? "passed" : "failed"));
  320. if (err != 0)
  321. all_passed = 0;
  322. return err;
  323. }
  324. waitpid (pid, NULL, 0);
  325. pid = fork ();
  326. if (pid == -1) {
  327. fprintf (stderr, "Can't fork\n");
  328. return 1;
  329. }
  330. if (pid == 0) {
  331. err = test4 ();
  332. fprintf (stderr, "test4 %s\n", (err == 0 ? "passed" : "failed"));
  333. if (err != 0)
  334. all_passed = 0;
  335. return err;
  336. }
  337. waitpid (pid, NULL, 0);
  338. if (all_passed)
  339. fprintf (stderr, "All tests passed\n");
  340. return (all_passed ? 0 : 1);
  341. }