testsam.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*
  2. * Copyright (c) 2009-2025 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 <sys/resource.h>
  40. #include <limits.h>
  41. #include <pthread.h>
  42. #include <stdio.h>
  43. #include <stdint.h>
  44. #include <stdlib.h>
  45. #include <unistd.h>
  46. #include <corosync/corotypes.h>
  47. #include <corosync/sam.h>
  48. #include <signal.h>
  49. #include <string.h>
  50. #include <sys/wait.h>
  51. #include <corosync/cmap.h>
  52. static int test_sig_delivered = 0;
  53. static int test_hc_cb_count = 0;
  54. /*
  55. * First test will just register SAM, with policy restart. First instance will
  56. * sleep one second, send hc and sleep another 3 seconds. This should force restart.
  57. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
  58. * Then start hc again and sleep 3 seconds. This should force restart again.
  59. * Last instance just calls initialize again. This should end with error.
  60. * Then call start, followed by stop and start again. Finally, we will call finalize
  61. * twice. One should succeed, second should fail. After this, we will call every function
  62. * (none should succeed).
  63. */
  64. static int test_basics (void)
  65. {
  66. cs_error_t error;
  67. unsigned int instance_id;
  68. int i;
  69. printf ("%s: initialize\n", __FUNCTION__);
  70. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  71. if (error != CS_OK) {
  72. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  73. return 1;
  74. }
  75. printf ("%s: register\n", __FUNCTION__);
  76. error = sam_register (&instance_id);
  77. if (error != CS_OK) {
  78. fprintf (stderr, "Can't register. Error %d\n", error);
  79. return 1;
  80. }
  81. if (instance_id == 1 || instance_id == 2) {
  82. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  83. error = sam_start ();
  84. if (error != CS_OK) {
  85. fprintf (stderr, "Can't start hc. Error %d\n", error);
  86. return 1;
  87. }
  88. for (i = 0; i < 10; i++) {
  89. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  90. sleep (1);
  91. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  92. error = sam_hc_send ();
  93. if (error != CS_OK) {
  94. fprintf (stderr, "Can't send hc. Error %d\n", error);
  95. return 1;
  96. }
  97. }
  98. if (instance_id == 2) {
  99. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  100. error = sam_stop ();
  101. if (error != CS_OK) {
  102. fprintf (stderr, "Can't stop hc. Error %d\n", error);
  103. return 1;
  104. }
  105. }
  106. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  107. sleep (3);
  108. if (instance_id == 1) {
  109. printf ("%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  110. return 1;
  111. }
  112. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  113. error = sam_start ();
  114. if (error != CS_OK) {
  115. fprintf (stderr, "Can't start hc. Error %d\n", error);
  116. return 1;
  117. }
  118. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  119. sleep (3);
  120. return 0;
  121. }
  122. if (instance_id == 3) {
  123. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  124. if (error == CS_OK) {
  125. fprintf (stderr, "Can initialize SAM API after initialization");
  126. return 1;
  127. }
  128. error = sam_start ();
  129. if (error != CS_OK) {
  130. fprintf (stderr, "Can't start hc. Error %d\n", error);
  131. return 1;
  132. }
  133. error = sam_stop ();
  134. if (error != CS_OK) {
  135. fprintf (stderr, "Can't stop hc. Error %d\n", error);
  136. return 1;
  137. }
  138. error = sam_finalize ();
  139. if (error != CS_OK) {
  140. fprintf (stderr, "Can't finalize sam. Error %d\n", error);
  141. return 1;
  142. }
  143. error = sam_finalize ();
  144. if (error == CS_OK) {
  145. fprintf (stderr, "Can finalize sam after finalization!\n");
  146. return 1;
  147. }
  148. if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
  149. sam_start () == CS_OK || sam_stop () == CS_OK ||
  150. sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
  151. sam_hc_callback_register (NULL) == CS_OK) {
  152. fprintf (stderr, "Can call one of function after finalization!\n");
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. static void test_signal_handler (int sig) {
  160. printf ("%s\n", __FUNCTION__);
  161. test_sig_delivered = 1;
  162. }
  163. /*
  164. * This tests recovery policy quit and callback.
  165. */
  166. static int test_signal (void) {
  167. cs_error_t error;
  168. unsigned int instance_id;
  169. test_sig_delivered = 0;
  170. printf ("%s: initialize\n", __FUNCTION__);
  171. error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
  172. if (error != CS_OK) {
  173. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  174. return 1;
  175. }
  176. printf ("%s: register\n", __FUNCTION__);
  177. error = sam_register (&instance_id);
  178. if (error != CS_OK) {
  179. fprintf (stderr, "Can't register. Error %d\n", error);
  180. return 1;
  181. }
  182. if (instance_id == 1) {
  183. signal (SIGTERM, test_signal_handler);
  184. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  185. error = sam_start ();
  186. if (error != CS_OK) {
  187. fprintf (stderr, "Can't start hc. Error %d\n", error);
  188. return 1;
  189. }
  190. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  191. sleep (1);
  192. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  193. error = sam_hc_send ();
  194. if (error != CS_OK) {
  195. fprintf (stderr, "Can't send hc. Error %d\n", error);
  196. return 1;
  197. }
  198. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  199. while (!test_sig_delivered) {
  200. sleep (1);
  201. }
  202. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  203. sleep (3);
  204. }
  205. return 1;
  206. }
  207. /*
  208. * Smoke test. This has no time limit, just restart process
  209. * when it dies.
  210. */
  211. static int test_smoke (void) {
  212. cs_error_t error;
  213. unsigned int instance_id;
  214. struct rlimit lim;
  215. lim.rlim_cur = lim.rlim_max = 0;
  216. /*
  217. * Try to turn off core creation
  218. */
  219. setrlimit(RLIMIT_CORE, &lim);
  220. printf ("%s: initialize\n", __FUNCTION__);
  221. error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  222. if (error != CS_OK) {
  223. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  224. return 1;
  225. }
  226. printf ("%s: register\n", __FUNCTION__);
  227. error = sam_register (&instance_id);
  228. if (error != CS_OK) {
  229. fprintf (stderr, "Can't register. Error %d\n", error);
  230. return 1;
  231. }
  232. if (instance_id < 100) {
  233. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  234. error = sam_start ();
  235. if (error != CS_OK) {
  236. fprintf (stderr, "Can't start hc. Error %d\n", error);
  237. return 1;
  238. }
  239. printf ("%s iid %d: Sending signal\n", __FUNCTION__, instance_id);
  240. kill(getpid(), SIGSEGV);
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. /*
  246. * Test sam_data_store, sam_data_restore and sam_data_getsize
  247. */
  248. static int test_data_store_restore (void)
  249. {
  250. size_t size;
  251. cs_error_t err;
  252. int i;
  253. unsigned int instance_id;
  254. char saved_data[128];
  255. char saved_data2[128];
  256. printf ("%s: sam_data_getsize 1\n", __FUNCTION__);
  257. err = sam_data_getsize (&size);
  258. if (err != CS_ERR_BAD_HANDLE) {
  259. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  260. return 1;
  261. }
  262. printf ("%s: sam_data_getsize 2\n", __FUNCTION__);
  263. err = sam_data_getsize (NULL);
  264. if (err != CS_ERR_INVALID_PARAM) {
  265. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  266. return 1;
  267. }
  268. printf ("%s: sam_data_store 1\n", __FUNCTION__);
  269. err = sam_data_store (NULL, 0);
  270. if (err != CS_ERR_BAD_HANDLE) {
  271. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  272. return 1;
  273. }
  274. printf ("%s: sam_data_restore 1\n", __FUNCTION__);
  275. err = sam_data_restore (saved_data, sizeof (saved_data));
  276. if (err != CS_ERR_BAD_HANDLE) {
  277. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  278. return 1;
  279. }
  280. printf ("%s: sam_initialize\n", __FUNCTION__);
  281. err = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  282. if (err != CS_OK) {
  283. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  284. return 1;
  285. }
  286. printf ("%s: sam_data_getsize 3\n", __FUNCTION__);
  287. err = sam_data_getsize (&size);
  288. if (err != CS_OK) {
  289. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  290. return 1;
  291. }
  292. if (size != 0) {
  293. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  294. return 1;
  295. }
  296. printf ("%s: sam_data_restore 2\n", __FUNCTION__);
  297. err = sam_data_restore (NULL, sizeof (saved_data));
  298. if (err != CS_ERR_INVALID_PARAM) {
  299. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  300. return 1;
  301. }
  302. /*
  303. * Store some real data
  304. */
  305. for (i = 0; i < sizeof (saved_data); i++) {
  306. saved_data[i] = (char)(i + 5);
  307. }
  308. printf ("%s: sam_data_store 2\n", __FUNCTION__);
  309. err = sam_data_store (saved_data, sizeof (saved_data));
  310. if (err != CS_OK) {
  311. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  312. return 1;
  313. }
  314. printf ("%s: sam_data_getsize 4\n", __FUNCTION__);
  315. err = sam_data_getsize (&size);
  316. if (err != CS_OK) {
  317. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  318. return 1;
  319. }
  320. if (size != sizeof (saved_data)) {
  321. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  322. return 1;
  323. }
  324. printf ("%s: sam_data_restore 3\n", __FUNCTION__);
  325. err = sam_data_restore (saved_data2, sizeof (saved_data2) - 1);
  326. if (err != CS_ERR_INVALID_PARAM) {
  327. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  328. return 1;
  329. }
  330. printf ("%s: sam_data_restore 4\n", __FUNCTION__);
  331. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  332. if (err != CS_OK) {
  333. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  334. return 1;
  335. }
  336. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  337. fprintf (stderr, "Retored data are not same\n");
  338. return 1;
  339. }
  340. memset (saved_data2, 0, sizeof (saved_data2));
  341. printf ("%s: sam_data_store 3\n", __FUNCTION__);
  342. err = sam_data_store (NULL, 1);
  343. if (err != CS_OK) {
  344. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  345. return 1;
  346. }
  347. printf ("%s: sam_data_getsize 5\n", __FUNCTION__);
  348. err = sam_data_getsize (&size);
  349. if (err != CS_OK) {
  350. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  351. return 1;
  352. }
  353. if (size != 0) {
  354. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  355. return 1;
  356. }
  357. printf ("%s: sam_data_store 4\n", __FUNCTION__);
  358. err = sam_data_store (saved_data, sizeof (saved_data));
  359. if (err != CS_OK) {
  360. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  361. return 1;
  362. }
  363. printf ("%s: register\n", __FUNCTION__);
  364. err = sam_register (&instance_id);
  365. if (err != CS_OK) {
  366. fprintf (stderr, "Can't register. Error %d\n", err);
  367. return 1;
  368. }
  369. if (instance_id == 1) {
  370. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  371. err = sam_start ();
  372. if (err != CS_OK) {
  373. fprintf (stderr, "Can't start hc. Error %d\n", err);
  374. return 1;
  375. }
  376. printf ("%s iid %d: sam_data_getsize 6\n", __FUNCTION__, instance_id);
  377. err = sam_data_getsize (&size);
  378. if (err != CS_OK) {
  379. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  380. return 1;
  381. }
  382. if (size != sizeof (saved_data2)) {
  383. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  384. return 1;
  385. }
  386. printf ("%s iid %d: sam_data_restore 5\n", __FUNCTION__, instance_id);
  387. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  388. if (err != CS_OK) {
  389. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  390. return 1;
  391. }
  392. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  393. fprintf (stderr, "Retored data are not same\n");
  394. return 1;
  395. }
  396. for (i = 0; i < sizeof (saved_data); i++) {
  397. saved_data[i] = (char)(i - 5);
  398. }
  399. printf ("%s iid %d: sam_data_store 5\n", __FUNCTION__, instance_id);
  400. err = sam_data_store (saved_data, sizeof (saved_data) - 7);
  401. if (err != CS_OK) {
  402. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  403. return 1;
  404. }
  405. exit (1);
  406. }
  407. if (instance_id == 2) {
  408. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  409. err = sam_start ();
  410. if (err != CS_OK) {
  411. fprintf (stderr, "Can't start hc. Error %d\n", err);
  412. return 1;
  413. }
  414. printf ("%s iid %d: sam_data_getsize 7\n", __FUNCTION__, instance_id);
  415. err = sam_data_getsize (&size);
  416. if (err != CS_OK) {
  417. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  418. return 1;
  419. }
  420. if (size != sizeof (saved_data2) - 7) {
  421. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  422. return 1;
  423. }
  424. printf ("%s iid %d: sam_data_restore 6\n", __FUNCTION__, instance_id);
  425. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  426. if (err != CS_OK) {
  427. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  428. return 1;
  429. }
  430. for (i = 0; i < sizeof (saved_data); i++) {
  431. saved_data[i] = (char)(i - 5);
  432. }
  433. if (memcmp (saved_data, saved_data2, sizeof (saved_data2) - 7) != 0) {
  434. fprintf (stderr, "Retored data are not same\n");
  435. return 1;
  436. }
  437. printf ("%s iid %d: sam_data_store 6\n", __FUNCTION__, instance_id);
  438. err = sam_data_store (NULL, 0);
  439. if (err != CS_OK) {
  440. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  441. return 1;
  442. }
  443. exit (1);
  444. }
  445. if (instance_id == 3) {
  446. printf ("%s iid %d: sam_data_getsize 8\n", __FUNCTION__, instance_id);
  447. err = sam_data_getsize (&size);
  448. if (err != CS_OK) {
  449. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  450. return 1;
  451. }
  452. if (size != 0) {
  453. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  454. return 1;
  455. }
  456. }
  457. return (0);
  458. }
  459. static int test_hc_cb (void)
  460. {
  461. cs_error_t res;
  462. printf ("%s %d\n", __FUNCTION__, ++test_hc_cb_count);
  463. res = sam_data_store (&test_hc_cb_count, sizeof (test_hc_cb_count));
  464. if (res != CS_OK)
  465. return 1;
  466. if (test_hc_cb_count > 10)
  467. return 1;
  468. return 0;
  469. }
  470. /*
  471. * Test event driven healtchecking.
  472. */
  473. static int test_hc (void)
  474. {
  475. cs_error_t error;
  476. unsigned int instance_id;
  477. int hc_cb_count;
  478. printf ("%s: initialize\n", __FUNCTION__);
  479. error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
  480. if (error != CS_OK) {
  481. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  482. return 1;
  483. }
  484. printf ("%s: register\n", __FUNCTION__);
  485. error = sam_register (&instance_id);
  486. if (error != CS_OK) {
  487. fprintf (stderr, "Can't register. Error %d\n", error);
  488. return 1;
  489. }
  490. if (instance_id == 1) {
  491. printf ("%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
  492. error = sam_hc_callback_register (test_hc_cb);
  493. if (error != CS_OK) {
  494. fprintf (stderr, "Can't register hc cb. Error %d\n", error);
  495. return 1;
  496. }
  497. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  498. error = sam_start ();
  499. if (error != CS_OK) {
  500. fprintf (stderr, "Can't start hc. Error %d\n", error);
  501. return 1;
  502. }
  503. sleep (2);
  504. printf ("%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  505. return 1;
  506. }
  507. if (instance_id == 2) {
  508. error = sam_data_restore (&hc_cb_count, sizeof (hc_cb_count));
  509. if (error != CS_OK) {
  510. fprintf (stderr, "sam_data_restore should return CS_OK. Error returned %d\n", error);
  511. return 1;
  512. }
  513. if (hc_cb_count != 11) {
  514. fprintf (stderr, "%s iid %d: Premature killed. hc_cb_count should be 11 and it is %d\n",
  515. __FUNCTION__, instance_id - 1, hc_cb_count);
  516. return 1;
  517. }
  518. return 0;
  519. }
  520. return 1;
  521. }
  522. static void test_warn_signal_handler (int sig) {
  523. cs_error_t error;
  524. printf ("%s\n", __FUNCTION__);
  525. test_sig_delivered++;
  526. if ((error = sam_data_store (&test_sig_delivered, sizeof (test_sig_delivered))) != CS_OK) {
  527. fprintf (stderr, "Can't store data! Error : %d\n", error);
  528. }
  529. }
  530. /*
  531. * Test warn signal set.
  532. */
  533. static int test_warn_signal (void) {
  534. cs_error_t error;
  535. unsigned int instance_id;
  536. int tmp_sig_delivered;
  537. test_sig_delivered = 0;
  538. printf ("%s: initialize\n", __FUNCTION__);
  539. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  540. if (error != CS_OK) {
  541. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  542. return 1;
  543. }
  544. printf ("%s: register\n", __FUNCTION__);
  545. error = sam_register (&instance_id);
  546. if (error != CS_OK) {
  547. fprintf (stderr, "Can't register. Error %d\n", error);
  548. return 1;
  549. }
  550. if (instance_id == 1) {
  551. error = sam_warn_signal_set (SIGUSR1);
  552. if (error != CS_OK) {
  553. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  554. return 1;
  555. }
  556. signal (SIGUSR1, test_warn_signal_handler);
  557. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  558. error = sam_start ();
  559. if (error != CS_OK) {
  560. fprintf (stderr, "Can't start hc. Error %d\n", error);
  561. return 1;
  562. }
  563. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  564. sleep (1);
  565. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  566. error = sam_hc_send ();
  567. if (error != CS_OK) {
  568. fprintf (stderr, "Can't send hc. Error %d\n", error);
  569. return 1;
  570. }
  571. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  572. while (!test_sig_delivered) {
  573. sleep (1);
  574. }
  575. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  576. sleep (3);
  577. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  578. return (1);
  579. }
  580. if (instance_id == 2) {
  581. error = sam_data_restore (&tmp_sig_delivered, sizeof (tmp_sig_delivered));
  582. if (error != CS_OK) {
  583. fprintf (stderr, "Can't restore data. Error %d\n", error);
  584. return 1;
  585. }
  586. if (tmp_sig_delivered != 1) {
  587. fprintf (stderr, "Previous test failed. Signal was not delivered\n");
  588. return 1;
  589. }
  590. error = sam_warn_signal_set (SIGKILL);
  591. if (error != CS_OK) {
  592. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  593. return 1;
  594. }
  595. signal (SIGUSR1, test_warn_signal_handler);
  596. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  597. error = sam_start ();
  598. if (error != CS_OK) {
  599. fprintf (stderr, "Can't start hc. Error %d\n", error);
  600. return 1;
  601. }
  602. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  603. sleep (1);
  604. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  605. error = sam_hc_send ();
  606. if (error != CS_OK) {
  607. fprintf (stderr, "Can't send hc. Error %d\n", error);
  608. return 1;
  609. }
  610. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  611. while (!test_sig_delivered) {
  612. sleep (1);
  613. }
  614. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  615. return (1);
  616. }
  617. if (instance_id == 3) {
  618. error = sam_data_restore (&tmp_sig_delivered, sizeof (tmp_sig_delivered));
  619. if (error != CS_OK) {
  620. fprintf (stderr, "Can't restore data. Error %d\n", error);
  621. return 1;
  622. }
  623. if (tmp_sig_delivered != 1) {
  624. fprintf (stderr, "Previous test failed. Signal WAS delivered\n");
  625. return 1;
  626. }
  627. return (0);
  628. }
  629. return 1;
  630. }
  631. static void *test_thread_sleep_exit (void *arg)
  632. {
  633. /* Wait 5s */
  634. sleep (5);
  635. exit (0);
  636. }
  637. /*
  638. * Test quorum
  639. */
  640. static int test_quorum (void) {
  641. cmap_handle_t cmap_handle;
  642. cs_error_t err;
  643. unsigned int instance_id;
  644. pthread_t kill_thread;
  645. char *str;
  646. uint32_t expected_votes;
  647. err = cmap_initialize (&cmap_handle);
  648. if (err != CS_OK) {
  649. printf ("Could not initialize Cluster Map API instance error %d. Test skipped\n", err);
  650. return (1);
  651. }
  652. if (cmap_get_string(cmap_handle, "quorum.provider", &str) != CS_OK) {
  653. printf ("Could not get \"provider\" key: %d. Test skipped\n", err);
  654. return (1);
  655. }
  656. if (strcmp(str, "corosync_votequorum") != 0) {
  657. printf ("Provider is not corosync_votequorum. Test skipped\n");
  658. free(str);
  659. return (1);
  660. }
  661. free(str);
  662. if (cmap_get_uint32(cmap_handle, "quorum.expected_votes", &expected_votes) != CS_OK) {
  663. printf ("Could not get \"expected_votes\" key: %d. Test skipped\n", err);
  664. return (1);
  665. }
  666. if (expected_votes != 1) {
  667. printf ("Expected_votes is not 1. Test skipped\n");
  668. return (1);
  669. }
  670. /*
  671. * Set to not quorate
  672. */
  673. err = cmap_set_uint32(cmap_handle, "quorum.expected_votes", 2);
  674. if (err != CS_OK) {
  675. printf ("Can't set map key. Error %d\n", err);
  676. return (2);
  677. }
  678. printf ("%s: initialize\n", __FUNCTION__);
  679. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUORUM_RESTART);
  680. if (err != CS_OK) {
  681. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  682. return 2;
  683. }
  684. printf ("%s: register\n", __FUNCTION__);
  685. err = sam_register (&instance_id);
  686. if (err != CS_OK) {
  687. fprintf (stderr, "Can't register. Error %d\n", err);
  688. return 2;
  689. }
  690. if (instance_id == 1) {
  691. /*
  692. * Sam start should block forever, but 5s for us should be enough
  693. */
  694. pthread_create (&kill_thread, NULL, test_thread_sleep_exit, NULL);
  695. printf ("%s iid %d: start - should block forever (waiting 5s)\n", __FUNCTION__, instance_id);
  696. err = sam_start ();
  697. if (err != CS_OK) {
  698. fprintf (stderr, "Can't start hc. Error %d\n", err);
  699. return 2;
  700. }
  701. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  702. return (2);
  703. }
  704. if (instance_id == 2) {
  705. /*
  706. * Set to quorate
  707. */
  708. err = cmap_set_uint32(cmap_handle, "quorum.expected_votes", 1);
  709. if (err != CS_OK) {
  710. printf ("Can't set map key. Error %d\n", err);
  711. return (2);
  712. }
  713. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  714. err = sam_start ();
  715. if (err != CS_OK) {
  716. fprintf (stderr, "Can't start hc. Error %d\n", err);
  717. return 2;
  718. }
  719. /*
  720. * Set corosync unquorate
  721. */
  722. err = cmap_set_uint32(cmap_handle, "quorum.expected_votes", 2);
  723. if (err != CS_OK) {
  724. printf ("Can't set map key. Error %d\n", err);
  725. return (2);
  726. }
  727. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  728. sleep (3);
  729. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  730. return (2);
  731. }
  732. if (instance_id == 3) {
  733. return (0);
  734. }
  735. return (2);
  736. }
  737. static int test_finalize_deadlock (void) {
  738. cs_error_t error;
  739. unsigned int instance_id;
  740. pthread_t kill_thread;
  741. test_sig_delivered = 0;
  742. printf ("%s: initialize\n", __FUNCTION__);
  743. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  744. if (error != CS_OK) {
  745. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  746. return 1;
  747. }
  748. printf ("%s: register\n", __FUNCTION__);
  749. error = sam_register (&instance_id);
  750. if (error != CS_OK) {
  751. fprintf (stderr, "Can't register. Error %d\n", error);
  752. return 1;
  753. }
  754. if (instance_id == 1) {
  755. /*
  756. * Sam finalize might block forever, but 5s for us should be enough
  757. */
  758. pthread_create (&kill_thread, NULL, test_thread_sleep_exit, NULL);
  759. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  760. error = sam_start ();
  761. if (error != CS_OK) {
  762. fprintf (stderr, "Can't start hc. Error %d\n", error);
  763. return 1;
  764. }
  765. printf ("%s iid %d: call finalize (shouldn't deadlock)\n", __FUNCTION__, instance_id);
  766. error = sam_finalize ();
  767. if (error != CS_OK) {
  768. fprintf (stderr, "Can't finalize sam. Error %d\n", error);
  769. return 1;
  770. }
  771. printf ("%s iid %d: finalize succeeded\n", __FUNCTION__, instance_id);
  772. return 0;
  773. }
  774. if (instance_id == 2) {
  775. printf ("%s iid %d: previous sam_finalize deadlocked\n", __FUNCTION__, instance_id);
  776. }
  777. return 1;
  778. }
  779. /*
  780. * Test cmap integration + quit policy
  781. */
  782. static int test_cmap_quit (pid_t pid, pid_t old_pid, int test_n)
  783. {
  784. cmap_handle_t cmap_handle;
  785. cs_error_t err;
  786. uint64_t tstamp1, tstamp2;
  787. int32_t msec_diff;
  788. unsigned int instance_id;
  789. char key_name[CMAP_KEYNAME_MAXLEN];
  790. char *str;
  791. err = cmap_initialize (&cmap_handle);
  792. if (err != CS_OK) {
  793. printf ("Could not initialize Cluster Map API instance error %d. Test skipped\n", err);
  794. return (1);
  795. }
  796. printf ("%s test %d\n", __FUNCTION__, test_n);
  797. if (test_n == 2) {
  798. /*
  799. * Object should not exist
  800. */
  801. printf ("%s Testing if object exists (it shouldn't)\n", __FUNCTION__);
  802. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  803. err = cmap_get_string(cmap_handle, key_name, &str);
  804. if (err == CS_OK) {
  805. printf ("Could find key \"%s\": %d.\n", key_name, err);
  806. free(str);
  807. return (2);
  808. }
  809. }
  810. if (test_n == 1 || test_n == 2) {
  811. printf ("%s: initialize\n", __FUNCTION__);
  812. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CMAP);
  813. if (err != CS_OK) {
  814. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  815. return 2;
  816. }
  817. printf ("%s: register\n", __FUNCTION__);
  818. err = sam_register (&instance_id);
  819. if (err != CS_OK) {
  820. fprintf (stderr, "Can't register. Error %d\n", err);
  821. return 2;
  822. }
  823. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid);
  824. err = cmap_get_string(cmap_handle, key_name, &str);
  825. if (err != CS_OK) {
  826. printf ("Could not get \"recovery\" key: %d.\n", err);
  827. return (2);
  828. }
  829. if (strcmp(str, "quit") != 0) {
  830. printf ("Recovery key \"%s\" is not \"quit\".\n", key_name);
  831. free(str);
  832. return (2);
  833. }
  834. free(str);
  835. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  836. err = cmap_get_string(cmap_handle, key_name, &str);
  837. if (err != CS_OK) {
  838. printf ("Could not get \"state\" key: %d.\n", err);
  839. return (2);
  840. }
  841. if (strcmp(str, "stopped") != 0) {
  842. printf ("State key is not \"stopped\".\n");
  843. free(str);
  844. return (2);
  845. }
  846. free(str);
  847. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  848. err = sam_start ();
  849. if (err != CS_OK) {
  850. fprintf (stderr, "Can't start hc. Error %d\n", err);
  851. return 2;
  852. }
  853. err = cmap_get_string(cmap_handle, key_name, &str);
  854. if (err != CS_OK) {
  855. printf ("Could not get \"state\" key: %d.\n", err);
  856. return (2);
  857. }
  858. if (strcmp(str, "running") != 0) {
  859. printf ("State key is not \"running\".\n");
  860. free(str);
  861. return (2);
  862. }
  863. free(str);
  864. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  865. err = sam_stop ();
  866. if (err != CS_OK) {
  867. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  868. return 2;
  869. }
  870. err = cmap_get_string(cmap_handle, key_name, &str);
  871. if (err != CS_OK) {
  872. printf ("Could not get \"state\" key: %d.\n", err);
  873. return (2);
  874. }
  875. if (strcmp(str, "stopped") != 0) {
  876. printf ("State key is not \"stopped\".\n");
  877. free(str);
  878. return (2);
  879. }
  880. free(str);
  881. printf ("%s iid %d: sleeping 5\n", __FUNCTION__, instance_id);
  882. sleep (5);
  883. err = cmap_get_string(cmap_handle, key_name, &str);
  884. if (err != CS_OK) {
  885. printf ("Could not get \"state\" key: %d.\n", err);
  886. return (2);
  887. }
  888. if (strcmp(str, "stopped") != 0) {
  889. printf ("State key is not \"stopped\".\n");
  890. free(str);
  891. return (2);
  892. }
  893. free(str);
  894. printf ("%s iid %d: start 2\n", __FUNCTION__, instance_id);
  895. err = sam_start ();
  896. if (err != CS_OK) {
  897. fprintf (stderr, "Can't start hc. Error %d\n", err);
  898. return 2;
  899. }
  900. err = cmap_get_string(cmap_handle, key_name, &str);
  901. if (err != CS_OK) {
  902. printf ("Could not get \"state\" key: %d.\n", err);
  903. return (2);
  904. }
  905. if (strcmp(str, "running") != 0) {
  906. printf ("State key is not \"running\".\n");
  907. free(str);
  908. return (2);
  909. }
  910. free(str);
  911. if (test_n == 2) {
  912. printf ("%s iid %d: sleeping 5. Should be killed\n", __FUNCTION__, instance_id);
  913. sleep (5);
  914. return (2);
  915. } else {
  916. printf ("%s iid %d: Test HC\n", __FUNCTION__, instance_id);
  917. err = sam_hc_send ();
  918. if (err != CS_OK) {
  919. fprintf (stderr, "Can't send hc. Error %d\n", err);
  920. return 2;
  921. }
  922. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.last_updated", pid);
  923. err = cmap_get_uint64(cmap_handle, key_name, &tstamp1);
  924. if (err != CS_OK) {
  925. printf ("Could not get \"last_updated\" key: %d.\n", err);
  926. return (2);
  927. }
  928. printf ("%s iid %d: Sleep 1\n", __FUNCTION__, instance_id);
  929. sleep (1);
  930. err = sam_hc_send ();
  931. if (err != CS_OK) {
  932. fprintf (stderr, "Can't send hc. Error %d\n", err);
  933. return 2;
  934. }
  935. sleep (1);
  936. err = cmap_get_uint64(cmap_handle, key_name, &tstamp2);
  937. if (err != CS_OK) {
  938. printf ("Could not get \"last_updated\" key: %d.\n", err);
  939. return (2);
  940. }
  941. msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC;
  942. if (msec_diff < 500 || msec_diff > 2000) {
  943. printf ("Difference %d is not within <500, 2000> interval.\n", msec_diff);
  944. return (2);
  945. }
  946. printf ("%s iid %d: stop 2\n", __FUNCTION__, instance_id);
  947. err = sam_stop ();
  948. if (err != CS_OK) {
  949. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  950. return 2;
  951. }
  952. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  953. err = cmap_get_string(cmap_handle, key_name, &str);
  954. if (err != CS_OK) {
  955. printf ("Could not get \"state\" key: %d.\n", err);
  956. return (2);
  957. }
  958. if (strcmp(str, "stopped") != 0) {
  959. printf ("State key is not \"stopped\".\n");
  960. free(str);
  961. return (2);
  962. }
  963. free(str);
  964. printf ("%s iid %d: exiting\n", __FUNCTION__, instance_id);
  965. return (0);
  966. }
  967. }
  968. if (test_n == 3) {
  969. printf ("%s Testing if status is failed\n", __FUNCTION__);
  970. /*
  971. * Previous should be FAILED
  972. */
  973. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", old_pid);
  974. err = cmap_get_string(cmap_handle, key_name, &str);
  975. if (err != CS_OK) {
  976. printf ("Could not get \"state\" key: %d.\n", err);
  977. return (2);
  978. }
  979. if (strcmp(str, "failed") != 0) {
  980. printf ("State key is not \"failed\".\n");
  981. free(str);
  982. return (2);
  983. }
  984. free(str);
  985. return (0);
  986. }
  987. return (2);
  988. }
  989. /*
  990. * Test cmap integration + restart policy
  991. */
  992. static int test_cmap_restart (pid_t pid, pid_t old_pid, int test_n) {
  993. cs_error_t err;
  994. cmap_handle_t cmap_handle;
  995. unsigned int instance_id;
  996. char *str;
  997. char key_name[CMAP_KEYNAME_MAXLEN];
  998. err = cmap_initialize (&cmap_handle);
  999. if (err != CS_OK) {
  1000. printf ("Could not initialize Cluster Map API instance error %d. Test skipped\n", err);
  1001. return (1);
  1002. }
  1003. printf ("%s test %d\n", __FUNCTION__, test_n);
  1004. if (test_n == 1) {
  1005. printf ("%s: initialize\n", __FUNCTION__);
  1006. err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | SAM_RECOVERY_POLICY_CMAP);
  1007. if (err != CS_OK) {
  1008. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  1009. return 2;
  1010. }
  1011. printf ("%s: register\n", __FUNCTION__);
  1012. err = sam_register (&instance_id);
  1013. if (err != CS_OK) {
  1014. fprintf (stderr, "Can't register. Error %d\n", err);
  1015. return 2;
  1016. }
  1017. printf ("%s: iid %d\n", __FUNCTION__, instance_id);
  1018. if (instance_id < 3) {
  1019. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid);
  1020. err = cmap_get_string(cmap_handle, key_name, &str);
  1021. if (err != CS_OK) {
  1022. printf ("Could not get \"recovery\" key: %d.\n", err);
  1023. return (2);
  1024. }
  1025. if (strcmp(str, "restart") != 0) {
  1026. printf ("Recovery key \"%s\" is not \"restart\".\n", str);
  1027. free(str);
  1028. return (2);
  1029. }
  1030. free(str);
  1031. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  1032. err = cmap_get_string(cmap_handle, key_name, &str);
  1033. if (err != CS_OK) {
  1034. printf ("Could not get \"state\" key: %d.\n", err);
  1035. return (2);
  1036. }
  1037. if (strcmp(str, "stopped") != 0) {
  1038. printf ("State key is not \"stopped\".\n");
  1039. free(str);
  1040. return (2);
  1041. }
  1042. free(str);
  1043. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  1044. err = sam_start ();
  1045. if (err != CS_OK) {
  1046. fprintf (stderr, "Can't start hc. Error %d\n", err);
  1047. return 2;
  1048. }
  1049. err = cmap_get_string(cmap_handle, key_name, &str);
  1050. if (err != CS_OK) {
  1051. printf ("Could not get \"state\" key: %d.\n", err);
  1052. return (2);
  1053. }
  1054. if (strcmp(str, "running") != 0) {
  1055. printf ("State key is not \"running\".\n");
  1056. free(str);
  1057. return (2);
  1058. }
  1059. free(str);
  1060. printf ("%s iid %d: waiting for kill\n", __FUNCTION__, instance_id);
  1061. sleep (10);
  1062. return (2);
  1063. }
  1064. if (instance_id == 3) {
  1065. printf ("%s iid %d: mark failed\n", __FUNCTION__, instance_id);
  1066. err = sam_mark_failed ();
  1067. if (err != CS_OK) {
  1068. fprintf (stderr, "Can't mark failed. Error %d\n", err);
  1069. return 2;
  1070. }
  1071. sleep (10);
  1072. return (2);
  1073. }
  1074. return (2);
  1075. }
  1076. if (test_n == 2) {
  1077. printf ("%s Testing if status is failed\n", __FUNCTION__);
  1078. /*
  1079. * Previous should be FAILED
  1080. */
  1081. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", old_pid);
  1082. err = cmap_get_string(cmap_handle, key_name, &str);
  1083. if (err != CS_OK) {
  1084. printf ("Could not get \"state\" key: %d.\n", err);
  1085. return (2);
  1086. }
  1087. if (strcmp(str, "failed") != 0) {
  1088. printf ("State key is not \"failed\".\n");
  1089. free(str);
  1090. return (2);
  1091. }
  1092. free(str);
  1093. return (0);
  1094. }
  1095. return (2);
  1096. }
  1097. static int run_test(const char *test_name, int (test_func)(void), int skippable, int *all_passed, int *no_skipped)
  1098. {
  1099. pid_t pid;
  1100. int err;
  1101. int stat;
  1102. const char *res_str;
  1103. fprintf (stderr, "Running %s test\n", test_name);
  1104. pid = fork ();
  1105. if (pid == -1) {
  1106. fprintf (stderr, "Can't fork\n");
  1107. exit(1);
  1108. }
  1109. if (pid == 0) {
  1110. /*
  1111. * Child runs tests and sets exit code to result of test
  1112. */
  1113. err = test_func ();
  1114. /*
  1115. * After sam_register this is child of child
  1116. */
  1117. sam_finalize ();
  1118. exit (err);
  1119. }
  1120. /*
  1121. * Parent waits for child to run the test and return exit code
  1122. */
  1123. waitpid (pid, &stat, 0);
  1124. err = WEXITSTATUS (stat);
  1125. if (!skippable) {
  1126. res_str = (err == 0 ? "passed" : "failed");
  1127. if (err != 0) {
  1128. (*all_passed) = 0;
  1129. }
  1130. } else {
  1131. res_str = (err == 0 ? "passed" : (err == 1 ? "skipped" : "failed"));
  1132. if (err == 1) {
  1133. (*no_skipped)++;
  1134. } else if (err > 1) {
  1135. (*all_passed) = 0;
  1136. }
  1137. }
  1138. fprintf (stderr, "%s test %s\n", test_name, res_str);
  1139. return (err);
  1140. }
  1141. static int run_cmap_test(const char *test_name,
  1142. int (test_func)(pid_t pid, pid_t old_pid, int test_n),
  1143. int no_tests, int *all_passed, int *no_skipped)
  1144. {
  1145. pid_t pid, old_pid;
  1146. int err;
  1147. int stat;
  1148. const char *res_str;
  1149. int n;
  1150. old_pid = 0;
  1151. err = 0;
  1152. fprintf (stderr, "Running %s test\n", test_name);
  1153. for (n = 1; n <= no_tests; n++) {
  1154. pid = fork ();
  1155. if (pid == -1) {
  1156. fprintf (stderr, "Can't fork\n");
  1157. exit (2);
  1158. }
  1159. if (pid == 0) {
  1160. err = test_func (getpid (), old_pid, n);
  1161. sam_finalize ();
  1162. exit (err);
  1163. }
  1164. waitpid (pid, &stat, 0);
  1165. old_pid = pid;
  1166. err = WEXITSTATUS (stat);
  1167. if (err != 0) {
  1168. break;
  1169. }
  1170. }
  1171. res_str = (err == 0 ? "passed" : (err == 1 ? "skipped" : "failed"));
  1172. if (err == 1) {
  1173. (*no_skipped)++;
  1174. } else if (err > 1) {
  1175. (*all_passed) = 0;
  1176. }
  1177. fprintf (stderr, "%s test %s\n", test_name, res_str);
  1178. return (err);
  1179. }
  1180. int main(int argc, char *argv[])
  1181. {
  1182. int all_passed = 1;
  1183. int no_skipped = 0;
  1184. setlinebuf(stdout);
  1185. run_test("basics", test_basics, 0, &all_passed, &no_skipped);
  1186. run_test("signal", test_signal, 0, &all_passed, &no_skipped);
  1187. run_test("smoke", test_smoke, 0, &all_passed, &no_skipped);
  1188. run_test("data_store/restore", test_data_store_restore, 0, &all_passed, &no_skipped);
  1189. run_test("hc", test_hc, 0, &all_passed, &no_skipped);
  1190. run_test("warn_signal", test_warn_signal, 0, &all_passed, &no_skipped);
  1191. run_test("quorum", test_quorum, 1, &all_passed, &no_skipped);
  1192. run_test("finalize deadlock", test_finalize_deadlock, 0, &all_passed, &no_skipped);
  1193. run_cmap_test("cmap quit", test_cmap_quit, 3, &all_passed, &no_skipped);
  1194. run_cmap_test("cmap restart", test_cmap_restart, 2, &all_passed, &no_skipped);
  1195. if (all_passed)
  1196. fprintf (stderr, "All tests passed (%d skipped)\n", no_skipped);
  1197. return (all_passed ? 0 : 1);
  1198. }