testsam.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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/confdb.h>
  45. #include <corosync/sam.h>
  46. #include <signal.h>
  47. #include <string.h>
  48. #include <sys/wait.h>
  49. static int test2_sig_delivered = 0;
  50. static int test5_hc_cb_count = 0;
  51. static int test6_sig_delivered = 0;
  52. /*
  53. * First test will just register SAM, with policy restart. First instance will
  54. * sleep one second, send hc and sleep another 3 seconds. This should force restart.
  55. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
  56. * Then start hc again and sleep 3 seconds. This should force restart again.
  57. * Last instance just calls initialize again. This should end with error.
  58. * Then call start, followed by stop and start again. Finally, we will call finalize
  59. * twice. One should succeed, second should fail. After this, we will call every function
  60. * (none should succeed).
  61. */
  62. static int test1 (void)
  63. {
  64. cs_error_t error;
  65. unsigned int instance_id;
  66. int i;
  67. printf ("%s: initialize\n", __FUNCTION__);
  68. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  69. if (error != CS_OK) {
  70. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  71. return 1;
  72. }
  73. printf ("%s: register\n", __FUNCTION__);
  74. error = sam_register (&instance_id);
  75. if (error != CS_OK) {
  76. fprintf (stderr, "Can't register. Error %d\n", error);
  77. return 1;
  78. }
  79. if (instance_id == 1 || instance_id == 2) {
  80. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  81. error = sam_start ();
  82. if (error != CS_OK) {
  83. fprintf (stderr, "Can't start hc. Error %d\n", error);
  84. return 1;
  85. }
  86. for (i = 0; i < 10; i++) {
  87. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  88. sleep (1);
  89. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  90. error = sam_hc_send ();
  91. if (error != CS_OK) {
  92. fprintf (stderr, "Can't send hc. Error %d\n", error);
  93. return 1;
  94. }
  95. }
  96. if (instance_id == 2) {
  97. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  98. error = sam_stop ();
  99. if (error != CS_OK) {
  100. fprintf (stderr, "Can't send hc. Error %d\n", error);
  101. return 1;
  102. }
  103. }
  104. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  105. sleep (3);
  106. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  107. error = sam_start ();
  108. if (error != CS_OK) {
  109. fprintf (stderr, "Can't start hc. Error %d\n", error);
  110. return 1;
  111. }
  112. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  113. sleep (3);
  114. return 0;
  115. }
  116. if (instance_id == 3) {
  117. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  118. if (error == CS_OK) {
  119. fprintf (stderr, "Can initialize SAM API after initialization");
  120. return 1;
  121. }
  122. error = sam_start ();
  123. if (error != CS_OK) {
  124. fprintf (stderr, "Can't start hc. Error %d\n", error);
  125. return 1;
  126. }
  127. error = sam_stop ();
  128. if (error != CS_OK) {
  129. fprintf (stderr, "Can't stop hc. Error %d\n", error);
  130. return 1;
  131. }
  132. error = sam_finalize ();
  133. if (error != CS_OK) {
  134. fprintf (stderr, "Can't finalize sam. Error %d\n", error);
  135. return 1;
  136. }
  137. error = sam_finalize ();
  138. if (error == CS_OK) {
  139. fprintf (stderr, "Can finalize sam after finalization!\n");
  140. return 1;
  141. }
  142. if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
  143. sam_start () == CS_OK || sam_stop () == CS_OK ||
  144. sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
  145. sam_hc_callback_register (NULL) == CS_OK) {
  146. fprintf (stderr, "Can call one of function after finalization!\n");
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. static void test2_signal (int sig) {
  154. printf ("%s\n", __FUNCTION__);
  155. test2_sig_delivered = 1;
  156. }
  157. /*
  158. * This tests recovery policy quit and callback.
  159. */
  160. static int test2 (void) {
  161. cs_error_t error;
  162. unsigned int instance_id;
  163. printf ("%s: initialize\n", __FUNCTION__);
  164. error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
  165. if (error != CS_OK) {
  166. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  167. return 1;
  168. }
  169. printf ("%s: register\n", __FUNCTION__);
  170. error = sam_register (&instance_id);
  171. if (error != CS_OK) {
  172. fprintf (stderr, "Can't register. Error %d\n", error);
  173. return 1;
  174. }
  175. if (instance_id == 1) {
  176. signal (SIGTERM, test2_signal);
  177. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  178. error = sam_start ();
  179. if (error != CS_OK) {
  180. fprintf (stderr, "Can't start hc. Error %d\n", error);
  181. return 1;
  182. }
  183. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  184. sleep (1);
  185. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  186. error = sam_hc_send ();
  187. if (error != CS_OK) {
  188. fprintf (stderr, "Can't send hc. Error %d\n", error);
  189. return 1;
  190. }
  191. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  192. while (!test2_sig_delivered) {
  193. sleep (1);
  194. }
  195. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  196. sleep (3);
  197. }
  198. return 1;
  199. }
  200. /*
  201. * Smoke test. Better to turn off coredump ;) This has no time limit, just restart process
  202. * when it dies.
  203. */
  204. static int test3 (void) {
  205. cs_error_t error;
  206. unsigned int instance_id;
  207. int tmp1, tmp2, tmp3;
  208. printf ("%s: initialize\n", __FUNCTION__);
  209. error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  210. if (error != CS_OK) {
  211. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  212. return 1;
  213. }
  214. printf ("%s: register\n", __FUNCTION__);
  215. error = sam_register (&instance_id);
  216. if (error != CS_OK) {
  217. fprintf (stderr, "Can't register. Error %d\n", error);
  218. return 1;
  219. }
  220. if (instance_id < 100) {
  221. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  222. error = sam_start ();
  223. if (error != CS_OK) {
  224. fprintf (stderr, "Can't start hc. Error %d\n", error);
  225. return 1;
  226. }
  227. printf ("%s iid %d: divide by zero\n", __FUNCTION__, instance_id);
  228. tmp2 = rand ();
  229. tmp3 = 0;
  230. tmp1 = tmp2 / tmp3;
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. /*
  236. * Test sam_data_store, sam_data_restore and sam_data_getsize
  237. */
  238. static int test4 (void)
  239. {
  240. size_t size;
  241. cs_error_t err;
  242. int i;
  243. unsigned int instance_id;
  244. char saved_data[128];
  245. char saved_data2[128];
  246. printf ("%s: sam_data_getsize 1\n", __FUNCTION__);
  247. err = sam_data_getsize (&size);
  248. if (err != CS_ERR_BAD_HANDLE) {
  249. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  250. return 1;
  251. }
  252. printf ("%s: sam_data_getsize 2\n", __FUNCTION__);
  253. err = sam_data_getsize (NULL);
  254. if (err != CS_ERR_INVALID_PARAM) {
  255. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  256. return 1;
  257. }
  258. printf ("%s: sam_data_store 1\n", __FUNCTION__);
  259. err = sam_data_store (NULL, 0);
  260. if (err != CS_ERR_BAD_HANDLE) {
  261. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  262. return 1;
  263. }
  264. printf ("%s: sam_data_restore 1\n", __FUNCTION__);
  265. err = sam_data_restore (saved_data, sizeof (saved_data));
  266. if (err != CS_ERR_BAD_HANDLE) {
  267. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  268. return 1;
  269. }
  270. printf ("%s: sam_initialize\n", __FUNCTION__);
  271. err = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  272. if (err != CS_OK) {
  273. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  274. return 1;
  275. }
  276. printf ("%s: sam_data_getsize 3\n", __FUNCTION__);
  277. err = sam_data_getsize (&size);
  278. if (err != CS_OK) {
  279. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  280. return 1;
  281. }
  282. if (size != 0) {
  283. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  284. return 1;
  285. }
  286. printf ("%s: sam_data_restore 2\n", __FUNCTION__);
  287. err = sam_data_restore (NULL, sizeof (saved_data));
  288. if (err != CS_ERR_INVALID_PARAM) {
  289. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  290. return 1;
  291. }
  292. /*
  293. * Store some real data
  294. */
  295. for (i = 0; i < sizeof (saved_data); i++) {
  296. saved_data[i] = (char)(i + 5);
  297. }
  298. printf ("%s: sam_data_store 2\n", __FUNCTION__);
  299. err = sam_data_store (saved_data, sizeof (saved_data));
  300. if (err != CS_OK) {
  301. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  302. return 1;
  303. }
  304. printf ("%s: sam_data_getsize 4\n", __FUNCTION__);
  305. err = sam_data_getsize (&size);
  306. if (err != CS_OK) {
  307. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  308. return 1;
  309. }
  310. if (size != sizeof (saved_data)) {
  311. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  312. return 1;
  313. }
  314. printf ("%s: sam_data_restore 3\n", __FUNCTION__);
  315. err = sam_data_restore (saved_data2, sizeof (saved_data2) - 1);
  316. if (err != CS_ERR_INVALID_PARAM) {
  317. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  318. return 1;
  319. }
  320. printf ("%s: sam_data_restore 4\n", __FUNCTION__);
  321. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  322. if (err != CS_OK) {
  323. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  324. return 1;
  325. }
  326. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  327. fprintf (stderr, "Retored data are not same\n");
  328. return 1;
  329. }
  330. memset (saved_data2, 0, sizeof (saved_data2));
  331. printf ("%s: sam_data_store 3\n", __FUNCTION__);
  332. err = sam_data_store (NULL, 1);
  333. if (err != CS_OK) {
  334. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  335. return 1;
  336. }
  337. printf ("%s: sam_data_getsize 5\n", __FUNCTION__);
  338. err = sam_data_getsize (&size);
  339. if (err != CS_OK) {
  340. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  341. return 1;
  342. }
  343. if (size != 0) {
  344. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  345. return 1;
  346. }
  347. printf ("%s: sam_data_store 4\n", __FUNCTION__);
  348. err = sam_data_store (saved_data, sizeof (saved_data));
  349. if (err != CS_OK) {
  350. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  351. return 1;
  352. }
  353. printf ("%s: register\n", __FUNCTION__);
  354. err = sam_register (&instance_id);
  355. if (err != CS_OK) {
  356. fprintf (stderr, "Can't register. Error %d\n", err);
  357. return 1;
  358. }
  359. if (instance_id == 1) {
  360. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  361. err = sam_start ();
  362. if (err != CS_OK) {
  363. fprintf (stderr, "Can't start hc. Error %d\n", err);
  364. return 1;
  365. }
  366. printf ("%s iid %d: sam_data_getsize 6\n", __FUNCTION__, instance_id);
  367. err = sam_data_getsize (&size);
  368. if (err != CS_OK) {
  369. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  370. return 1;
  371. }
  372. if (size != sizeof (saved_data2)) {
  373. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  374. return 1;
  375. }
  376. printf ("%s iid %d: sam_data_restore 5\n", __FUNCTION__, instance_id);
  377. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  378. if (err != CS_OK) {
  379. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  380. return 1;
  381. }
  382. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  383. fprintf (stderr, "Retored data are not same\n");
  384. return 1;
  385. }
  386. for (i = 0; i < sizeof (saved_data); i++) {
  387. saved_data[i] = (char)(i - 5);
  388. }
  389. printf ("%s iid %d: sam_data_store 5\n", __FUNCTION__, instance_id);
  390. err = sam_data_store (saved_data, sizeof (saved_data) - 7);
  391. if (err != CS_OK) {
  392. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  393. return 1;
  394. }
  395. exit (1);
  396. }
  397. if (instance_id == 2) {
  398. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  399. err = sam_start ();
  400. if (err != CS_OK) {
  401. fprintf (stderr, "Can't start hc. Error %d\n", err);
  402. return 1;
  403. }
  404. printf ("%s iid %d: sam_data_getsize 7\n", __FUNCTION__, instance_id);
  405. err = sam_data_getsize (&size);
  406. if (err != CS_OK) {
  407. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  408. return 1;
  409. }
  410. if (size != sizeof (saved_data2) - 7) {
  411. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  412. return 1;
  413. }
  414. printf ("%s iid %d: sam_data_restore 6\n", __FUNCTION__, instance_id);
  415. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  416. if (err != CS_OK) {
  417. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  418. return 1;
  419. }
  420. for (i = 0; i < sizeof (saved_data); i++) {
  421. saved_data[i] = (char)(i - 5);
  422. }
  423. if (memcmp (saved_data, saved_data2, sizeof (saved_data2) - 7) != 0) {
  424. fprintf (stderr, "Retored data are not same\n");
  425. return 1;
  426. }
  427. printf ("%s iid %d: sam_data_store 6\n", __FUNCTION__, instance_id);
  428. err = sam_data_store (NULL, 0);
  429. if (err != CS_OK) {
  430. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  431. return 1;
  432. }
  433. exit (1);
  434. }
  435. if (instance_id == 3) {
  436. printf ("%s iid %d: sam_data_getsize 8\n", __FUNCTION__, instance_id);
  437. err = sam_data_getsize (&size);
  438. if (err != CS_OK) {
  439. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  440. return 1;
  441. }
  442. if (size != 0) {
  443. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  444. return 1;
  445. }
  446. }
  447. return (0);
  448. }
  449. static int test5_hc_cb (void)
  450. {
  451. printf ("%s %d\n", __FUNCTION__, ++test5_hc_cb_count);
  452. sam_data_store (&test5_hc_cb_count, sizeof (test5_hc_cb_count));
  453. if (test5_hc_cb_count > 10)
  454. return 1;
  455. return 0;
  456. }
  457. /*
  458. * Test event driven healtchecking.
  459. */
  460. static int test5 (void)
  461. {
  462. cs_error_t error;
  463. unsigned int instance_id;
  464. int hc_cb_count;
  465. printf ("%s: initialize\n", __FUNCTION__);
  466. error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
  467. if (error != CS_OK) {
  468. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  469. return 1;
  470. }
  471. printf ("%s: register\n", __FUNCTION__);
  472. error = sam_register (&instance_id);
  473. if (error != CS_OK) {
  474. fprintf (stderr, "Can't register. Error %d\n", error);
  475. return 1;
  476. }
  477. if (instance_id == 1) {
  478. printf ("%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
  479. error = sam_hc_callback_register (test5_hc_cb);
  480. if (error != CS_OK) {
  481. fprintf (stderr, "Can't register hc cb. Error %d\n", error);
  482. return 1;
  483. }
  484. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  485. error = sam_start ();
  486. if (error != CS_OK) {
  487. fprintf (stderr, "Can't start hc. Error %d\n", error);
  488. return 1;
  489. }
  490. sleep (2);
  491. printf ("%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  492. return 1;
  493. }
  494. if (instance_id == 2) {
  495. error = sam_data_restore (&hc_cb_count, sizeof (hc_cb_count));
  496. if (error != CS_OK) {
  497. fprintf (stderr, "sam_data_restore should return CS_OK. Error returned %d\n", error);
  498. return 1;
  499. }
  500. if (hc_cb_count != 11) {
  501. fprintf (stderr, "%s iid %d: Premature killed. hc_cb_count should be 11 and it is %d\n",
  502. __FUNCTION__, instance_id - 1, hc_cb_count);
  503. return 1;
  504. }
  505. return 0;
  506. }
  507. return 1;
  508. }
  509. static void test6_signal (int sig) {
  510. cs_error_t error;
  511. printf ("%s\n", __FUNCTION__);
  512. test6_sig_delivered++;
  513. if ((error = sam_data_store (&test6_sig_delivered, sizeof (test6_sig_delivered))) != CS_OK) {
  514. fprintf (stderr, "Can't store data! Error : %d\n", error);
  515. }
  516. }
  517. /*
  518. * Test warn signal set.
  519. */
  520. static int test6 (void) {
  521. cs_error_t error;
  522. unsigned int instance_id;
  523. int test6_sig_del;
  524. printf ("%s: initialize\n", __FUNCTION__);
  525. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  526. if (error != CS_OK) {
  527. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  528. return 1;
  529. }
  530. printf ("%s: register\n", __FUNCTION__);
  531. error = sam_register (&instance_id);
  532. if (error != CS_OK) {
  533. fprintf (stderr, "Can't register. Error %d\n", error);
  534. return 1;
  535. }
  536. if (instance_id == 1) {
  537. error = sam_warn_signal_set (SIGUSR1);
  538. if (error != CS_OK) {
  539. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  540. return 1;
  541. }
  542. signal (SIGUSR1, test6_signal);
  543. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  544. error = sam_start ();
  545. if (error != CS_OK) {
  546. fprintf (stderr, "Can't start hc. Error %d\n", error);
  547. return 1;
  548. }
  549. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  550. sleep (1);
  551. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  552. error = sam_hc_send ();
  553. if (error != CS_OK) {
  554. fprintf (stderr, "Can't send hc. Error %d\n", error);
  555. return 1;
  556. }
  557. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  558. while (!test6_sig_delivered) {
  559. sleep (1);
  560. }
  561. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  562. sleep (3);
  563. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  564. return (1);
  565. }
  566. if (instance_id == 2) {
  567. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  568. if (error != CS_OK) {
  569. fprintf (stderr, "Can't restore data. Error %d\n", error);
  570. return 1;
  571. }
  572. if (test6_sig_del != 1) {
  573. fprintf (stderr, "Previous test failed. Signal was not delivered\n");
  574. return 1;
  575. }
  576. error = sam_warn_signal_set (SIGKILL);
  577. if (error != CS_OK) {
  578. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  579. return 1;
  580. }
  581. signal (SIGUSR1, test6_signal);
  582. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  583. error = sam_start ();
  584. if (error != CS_OK) {
  585. fprintf (stderr, "Can't start hc. Error %d\n", error);
  586. return 1;
  587. }
  588. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  589. sleep (1);
  590. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  591. error = sam_hc_send ();
  592. if (error != CS_OK) {
  593. fprintf (stderr, "Can't send hc. Error %d\n", error);
  594. return 1;
  595. }
  596. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  597. while (!test6_sig_delivered) {
  598. sleep (1);
  599. }
  600. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  601. return (1);
  602. }
  603. if (instance_id == 3) {
  604. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  605. if (error != CS_OK) {
  606. fprintf (stderr, "Can't restore data. Error %d\n", error);
  607. return 1;
  608. }
  609. if (test6_sig_del != 1) {
  610. fprintf (stderr, "Previous test failed. Signal WAS delivered\n");
  611. return 1;
  612. }
  613. return (0);
  614. }
  615. return 1;
  616. }
  617. static void *test7_thread (void *arg)
  618. {
  619. /* Wait 5s */
  620. sleep (5);
  621. exit (0);
  622. }
  623. /*
  624. * Test quorum
  625. */
  626. static int test7 (void) {
  627. confdb_handle_t cdb_handle;
  628. cs_error_t err;
  629. hdb_handle_t quorum_handle;
  630. size_t value_len;
  631. char key_value[256];
  632. unsigned int instance_id;
  633. pthread_t kill_thread;
  634. err = confdb_initialize (&cdb_handle, NULL);
  635. if (err != CS_OK) {
  636. printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err);
  637. return (1);
  638. }
  639. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  640. if (err != CS_OK) {
  641. printf ("Could not start object_find %d. Test skipped\n", err);
  642. return (1);
  643. }
  644. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "quorum", strlen("quorum"), &quorum_handle);
  645. if (err != CS_OK) {
  646. printf ("Could not object_find \"quorum\": %d. Test skipped\n", err);
  647. return (1);
  648. }
  649. err = confdb_key_get(cdb_handle, quorum_handle, "provider", strlen("provider"), key_value, &value_len);
  650. if (err != CS_OK) {
  651. printf ("Could not get \"provider\" key: %d. Test skipped\n", err);
  652. return (1);
  653. }
  654. if (!(value_len - 1 == strlen ("testquorum") && memcmp (key_value, "testquorum", value_len - 1) == 0)) {
  655. printf ("Provider is not testquorum. Test skipped\n");
  656. return (1);
  657. }
  658. /*
  659. * Set to not quorate
  660. */
  661. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0"));
  662. if (err != CS_OK) {
  663. printf ("Can't create confdb key. Error %d\n", err);
  664. return (2);
  665. }
  666. printf ("%s: initialize\n", __FUNCTION__);
  667. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUORUM_RESTART);
  668. if (err != CS_OK) {
  669. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  670. return 2;
  671. }
  672. printf ("%s: register\n", __FUNCTION__);
  673. err = sam_register (&instance_id);
  674. if (err != CS_OK) {
  675. fprintf (stderr, "Can't register. Error %d\n", err);
  676. return 2;
  677. }
  678. if (instance_id == 1) {
  679. /*
  680. * Sam start should block forever, but 10s for us should be enough
  681. */
  682. pthread_create (&kill_thread, NULL, test7_thread, NULL);
  683. printf ("%s iid %d: start - should block forever (waiting 5s)\n", __FUNCTION__, instance_id);
  684. err = sam_start ();
  685. if (err != CS_OK) {
  686. fprintf (stderr, "Can't start hc. Error %d\n", err);
  687. return 2;
  688. }
  689. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  690. return (2);
  691. }
  692. if (instance_id == 2) {
  693. /*
  694. * Set to quorate
  695. */
  696. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "1", strlen("1"));
  697. if (err != CS_OK) {
  698. printf ("Can't create confdb key. Error %d\n", err);
  699. return (2);
  700. }
  701. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  702. err = sam_start ();
  703. if (err != CS_OK) {
  704. fprintf (stderr, "Can't start hc. Error %d\n", err);
  705. return 2;
  706. }
  707. /*
  708. * Set corosync unquorate
  709. */
  710. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0"));
  711. if (err != CS_OK) {
  712. printf ("Can't create confdb key. Error %d\n", err);
  713. return (2);
  714. }
  715. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  716. sleep (3);
  717. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  718. return (2);
  719. }
  720. if (instance_id == 3) {
  721. return (0);
  722. }
  723. return (2);
  724. }
  725. int main(int argc, char *argv[])
  726. {
  727. pid_t pid;
  728. int err;
  729. int stat;
  730. int all_passed = 1;
  731. int no_skipped = 0;
  732. pid = fork ();
  733. if (pid == -1) {
  734. fprintf (stderr, "Can't fork\n");
  735. return 1;
  736. }
  737. if (pid == 0) {
  738. err = test1 ();
  739. sam_finalize ();
  740. return err;
  741. }
  742. waitpid (pid, &stat, 0);
  743. fprintf (stderr, "test1 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  744. if (WEXITSTATUS (stat) != 0)
  745. all_passed = 0;
  746. pid = fork ();
  747. if (pid == -1) {
  748. fprintf (stderr, "Can't fork\n");
  749. return 1;
  750. }
  751. if (pid == 0) {
  752. err = test2 ();
  753. sam_finalize ();
  754. return (err);
  755. }
  756. waitpid (pid, &stat, 0);
  757. fprintf (stderr, "test2 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  758. if (WEXITSTATUS (stat) != 0)
  759. all_passed = 0;
  760. pid = fork ();
  761. if (pid == -1) {
  762. fprintf (stderr, "Can't fork\n");
  763. return 1;
  764. }
  765. if (pid == 0) {
  766. err = test3 ();
  767. sam_finalize ();
  768. return (err);
  769. }
  770. waitpid (pid, &stat, 0);
  771. fprintf (stderr, "test3 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  772. if (WEXITSTATUS (stat) != 0)
  773. all_passed = 0;
  774. pid = fork ();
  775. if (pid == -1) {
  776. fprintf (stderr, "Can't fork\n");
  777. return 1;
  778. }
  779. if (pid == 0) {
  780. err = test4 ();
  781. sam_finalize ();
  782. return (err);
  783. }
  784. waitpid (pid, &stat, 0);
  785. fprintf (stderr, "test4 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  786. if (WEXITSTATUS (stat) != 0)
  787. all_passed = 0;
  788. pid = fork ();
  789. if (pid == -1) {
  790. fprintf (stderr, "Can't fork\n");
  791. return 1;
  792. }
  793. if (pid == 0) {
  794. err = test5 ();
  795. sam_finalize ();
  796. return (err);
  797. }
  798. waitpid (pid, &stat, 0);
  799. fprintf (stderr, "test5 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  800. if (WEXITSTATUS (stat) != 0)
  801. all_passed = 0;
  802. pid = fork ();
  803. if (pid == -1) {
  804. fprintf (stderr, "Can't fork\n");
  805. return 1;
  806. }
  807. if (pid == 0) {
  808. err = test6 ();
  809. sam_finalize ();
  810. return (err);
  811. }
  812. waitpid (pid, &stat, 0);
  813. fprintf (stderr, "test6 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  814. if (WEXITSTATUS (stat) != 0)
  815. all_passed = 0;
  816. pid = fork ();
  817. if (pid == -1) {
  818. fprintf (stderr, "Can't fork\n");
  819. return 1;
  820. }
  821. if (pid == 0) {
  822. err = test7 ();
  823. sam_finalize ();
  824. return (err);
  825. }
  826. waitpid (pid, &stat, 0);
  827. fprintf (stderr, "test7 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : (WEXITSTATUS (stat) == 1 ? "skipped" : "failed")));
  828. if (WEXITSTATUS (stat) == 1)
  829. no_skipped++;
  830. if (WEXITSTATUS (stat) > 1)
  831. all_passed = 0;
  832. if (all_passed)
  833. fprintf (stderr, "All tests passed (%d skipped)\n", no_skipped);
  834. return (all_passed ? 0 : 1);
  835. }