testsam.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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_quorum_thread (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 10s for us should be enough
  693. */
  694. pthread_create (&kill_thread, NULL, test_quorum_thread, 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. /*
  738. * Test cmap integration + quit policy
  739. */
  740. static int test_cmap_quit (pid_t pid, pid_t old_pid, int test_n)
  741. {
  742. cmap_handle_t cmap_handle;
  743. cs_error_t err;
  744. uint64_t tstamp1, tstamp2;
  745. int32_t msec_diff;
  746. unsigned int instance_id;
  747. char key_name[CMAP_KEYNAME_MAXLEN];
  748. char *str;
  749. err = cmap_initialize (&cmap_handle);
  750. if (err != CS_OK) {
  751. printf ("Could not initialize Cluster Map API instance error %d. Test skipped\n", err);
  752. return (1);
  753. }
  754. printf ("%s test %d\n", __FUNCTION__, test_n);
  755. if (test_n == 2) {
  756. /*
  757. * Object should not exist
  758. */
  759. printf ("%s Testing if object exists (it shouldn't)\n", __FUNCTION__);
  760. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  761. err = cmap_get_string(cmap_handle, key_name, &str);
  762. if (err == CS_OK) {
  763. printf ("Could find key \"%s\": %d.\n", key_name, err);
  764. free(str);
  765. return (2);
  766. }
  767. }
  768. if (test_n == 1 || test_n == 2) {
  769. printf ("%s: initialize\n", __FUNCTION__);
  770. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CMAP);
  771. if (err != CS_OK) {
  772. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  773. return 2;
  774. }
  775. printf ("%s: register\n", __FUNCTION__);
  776. err = sam_register (&instance_id);
  777. if (err != CS_OK) {
  778. fprintf (stderr, "Can't register. Error %d\n", err);
  779. return 2;
  780. }
  781. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid);
  782. err = cmap_get_string(cmap_handle, key_name, &str);
  783. if (err != CS_OK) {
  784. printf ("Could not get \"recovery\" key: %d.\n", err);
  785. return (2);
  786. }
  787. if (strcmp(str, "quit") != 0) {
  788. printf ("Recovery key \"%s\" is not \"quit\".\n", key_name);
  789. free(str);
  790. return (2);
  791. }
  792. free(str);
  793. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  794. err = cmap_get_string(cmap_handle, key_name, &str);
  795. if (err != CS_OK) {
  796. printf ("Could not get \"state\" key: %d.\n", err);
  797. return (2);
  798. }
  799. if (strcmp(str, "stopped") != 0) {
  800. printf ("State key is not \"stopped\".\n");
  801. free(str);
  802. return (2);
  803. }
  804. free(str);
  805. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  806. err = sam_start ();
  807. if (err != CS_OK) {
  808. fprintf (stderr, "Can't start hc. Error %d\n", err);
  809. return 2;
  810. }
  811. err = cmap_get_string(cmap_handle, key_name, &str);
  812. if (err != CS_OK) {
  813. printf ("Could not get \"state\" key: %d.\n", err);
  814. return (2);
  815. }
  816. if (strcmp(str, "running") != 0) {
  817. printf ("State key is not \"running\".\n");
  818. free(str);
  819. return (2);
  820. }
  821. free(str);
  822. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  823. err = sam_stop ();
  824. if (err != CS_OK) {
  825. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  826. return 2;
  827. }
  828. err = cmap_get_string(cmap_handle, key_name, &str);
  829. if (err != CS_OK) {
  830. printf ("Could not get \"state\" key: %d.\n", err);
  831. return (2);
  832. }
  833. if (strcmp(str, "stopped") != 0) {
  834. printf ("State key is not \"stopped\".\n");
  835. free(str);
  836. return (2);
  837. }
  838. free(str);
  839. printf ("%s iid %d: sleeping 5\n", __FUNCTION__, instance_id);
  840. sleep (5);
  841. err = cmap_get_string(cmap_handle, key_name, &str);
  842. if (err != CS_OK) {
  843. printf ("Could not get \"state\" key: %d.\n", err);
  844. return (2);
  845. }
  846. if (strcmp(str, "stopped") != 0) {
  847. printf ("State key is not \"stopped\".\n");
  848. free(str);
  849. return (2);
  850. }
  851. free(str);
  852. printf ("%s iid %d: start 2\n", __FUNCTION__, instance_id);
  853. err = sam_start ();
  854. if (err != CS_OK) {
  855. fprintf (stderr, "Can't start hc. Error %d\n", err);
  856. return 2;
  857. }
  858. err = cmap_get_string(cmap_handle, key_name, &str);
  859. if (err != CS_OK) {
  860. printf ("Could not get \"state\" key: %d.\n", err);
  861. return (2);
  862. }
  863. if (strcmp(str, "running") != 0) {
  864. printf ("State key is not \"running\".\n");
  865. free(str);
  866. return (2);
  867. }
  868. free(str);
  869. if (test_n == 2) {
  870. printf ("%s iid %d: sleeping 5. Should be killed\n", __FUNCTION__, instance_id);
  871. sleep (5);
  872. return (2);
  873. } else {
  874. printf ("%s iid %d: Test HC\n", __FUNCTION__, instance_id);
  875. err = sam_hc_send ();
  876. if (err != CS_OK) {
  877. fprintf (stderr, "Can't send hc. Error %d\n", err);
  878. return 2;
  879. }
  880. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.last_updated", pid);
  881. err = cmap_get_uint64(cmap_handle, key_name, &tstamp1);
  882. if (err != CS_OK) {
  883. printf ("Could not get \"last_updated\" key: %d.\n", err);
  884. return (2);
  885. }
  886. printf ("%s iid %d: Sleep 1\n", __FUNCTION__, instance_id);
  887. sleep (1);
  888. err = sam_hc_send ();
  889. if (err != CS_OK) {
  890. fprintf (stderr, "Can't send hc. Error %d\n", err);
  891. return 2;
  892. }
  893. sleep (1);
  894. err = cmap_get_uint64(cmap_handle, key_name, &tstamp2);
  895. if (err != CS_OK) {
  896. printf ("Could not get \"last_updated\" key: %d.\n", err);
  897. return (2);
  898. }
  899. msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC;
  900. if (msec_diff < 500 || msec_diff > 2000) {
  901. printf ("Difference %d is not within <500, 2000> interval.\n", msec_diff);
  902. return (2);
  903. }
  904. printf ("%s iid %d: stop 2\n", __FUNCTION__, instance_id);
  905. err = sam_stop ();
  906. if (err != CS_OK) {
  907. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  908. return 2;
  909. }
  910. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  911. err = cmap_get_string(cmap_handle, key_name, &str);
  912. if (err != CS_OK) {
  913. printf ("Could not get \"state\" key: %d.\n", err);
  914. return (2);
  915. }
  916. if (strcmp(str, "stopped") != 0) {
  917. printf ("State key is not \"stopped\".\n");
  918. free(str);
  919. return (2);
  920. }
  921. free(str);
  922. printf ("%s iid %d: exiting\n", __FUNCTION__, instance_id);
  923. return (0);
  924. }
  925. }
  926. if (test_n == 3) {
  927. printf ("%s Testing if status is failed\n", __FUNCTION__);
  928. /*
  929. * Previous should be FAILED
  930. */
  931. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", old_pid);
  932. err = cmap_get_string(cmap_handle, key_name, &str);
  933. if (err != CS_OK) {
  934. printf ("Could not get \"state\" key: %d.\n", err);
  935. return (2);
  936. }
  937. if (strcmp(str, "failed") != 0) {
  938. printf ("State key is not \"failed\".\n");
  939. free(str);
  940. return (2);
  941. }
  942. free(str);
  943. return (0);
  944. }
  945. return (2);
  946. }
  947. /*
  948. * Test cmap integration + restart policy
  949. */
  950. static int test_cmap_restart (pid_t pid, pid_t old_pid, int test_n) {
  951. cs_error_t err;
  952. cmap_handle_t cmap_handle;
  953. unsigned int instance_id;
  954. char *str;
  955. char key_name[CMAP_KEYNAME_MAXLEN];
  956. err = cmap_initialize (&cmap_handle);
  957. if (err != CS_OK) {
  958. printf ("Could not initialize Cluster Map API instance error %d. Test skipped\n", err);
  959. return (1);
  960. }
  961. printf ("%s test %d\n", __FUNCTION__, test_n);
  962. if (test_n == 1) {
  963. printf ("%s: initialize\n", __FUNCTION__);
  964. err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | SAM_RECOVERY_POLICY_CMAP);
  965. if (err != CS_OK) {
  966. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  967. return 2;
  968. }
  969. printf ("%s: register\n", __FUNCTION__);
  970. err = sam_register (&instance_id);
  971. if (err != CS_OK) {
  972. fprintf (stderr, "Can't register. Error %d\n", err);
  973. return 2;
  974. }
  975. printf ("%s: iid %d\n", __FUNCTION__, instance_id);
  976. if (instance_id < 3) {
  977. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid);
  978. err = cmap_get_string(cmap_handle, key_name, &str);
  979. if (err != CS_OK) {
  980. printf ("Could not get \"recovery\" key: %d.\n", err);
  981. return (2);
  982. }
  983. if (strcmp(str, "restart") != 0) {
  984. printf ("Recovery key \"%s\" is not \"restart\".\n", str);
  985. free(str);
  986. return (2);
  987. }
  988. free(str);
  989. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  990. err = cmap_get_string(cmap_handle, key_name, &str);
  991. if (err != CS_OK) {
  992. printf ("Could not get \"state\" key: %d.\n", err);
  993. return (2);
  994. }
  995. if (strcmp(str, "stopped") != 0) {
  996. printf ("State key is not \"stopped\".\n");
  997. free(str);
  998. return (2);
  999. }
  1000. free(str);
  1001. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  1002. err = sam_start ();
  1003. if (err != CS_OK) {
  1004. fprintf (stderr, "Can't start hc. Error %d\n", err);
  1005. return 2;
  1006. }
  1007. err = cmap_get_string(cmap_handle, key_name, &str);
  1008. if (err != CS_OK) {
  1009. printf ("Could not get \"state\" key: %d.\n", err);
  1010. return (2);
  1011. }
  1012. if (strcmp(str, "running") != 0) {
  1013. printf ("State key is not \"running\".\n");
  1014. free(str);
  1015. return (2);
  1016. }
  1017. free(str);
  1018. printf ("%s iid %d: waiting for kill\n", __FUNCTION__, instance_id);
  1019. sleep (10);
  1020. return (2);
  1021. }
  1022. if (instance_id == 3) {
  1023. printf ("%s iid %d: mark failed\n", __FUNCTION__, instance_id);
  1024. err = sam_mark_failed ();
  1025. if (err != CS_OK) {
  1026. fprintf (stderr, "Can't mark failed. Error %d\n", err);
  1027. return 2;
  1028. }
  1029. sleep (10);
  1030. return (2);
  1031. }
  1032. return (2);
  1033. }
  1034. if (test_n == 2) {
  1035. printf ("%s Testing if status is failed\n", __FUNCTION__);
  1036. /*
  1037. * Previous should be FAILED
  1038. */
  1039. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", old_pid);
  1040. err = cmap_get_string(cmap_handle, key_name, &str);
  1041. if (err != CS_OK) {
  1042. printf ("Could not get \"state\" key: %d.\n", err);
  1043. return (2);
  1044. }
  1045. if (strcmp(str, "failed") != 0) {
  1046. printf ("State key is not \"failed\".\n");
  1047. free(str);
  1048. return (2);
  1049. }
  1050. free(str);
  1051. return (0);
  1052. }
  1053. return (2);
  1054. }
  1055. static int run_test(const char *test_name, int (test_func)(void), int skippable, int *all_passed, int *no_skipped)
  1056. {
  1057. pid_t pid;
  1058. int err;
  1059. int stat;
  1060. const char *res_str;
  1061. fprintf (stderr, "Running %s test\n", test_name);
  1062. pid = fork ();
  1063. if (pid == -1) {
  1064. fprintf (stderr, "Can't fork\n");
  1065. exit(1);
  1066. }
  1067. if (pid == 0) {
  1068. /*
  1069. * Child runs tests and sets exit code to result of test
  1070. */
  1071. err = test_func ();
  1072. /*
  1073. * After sam_register this is child of child
  1074. */
  1075. sam_finalize ();
  1076. exit (err);
  1077. }
  1078. /*
  1079. * Parent waits for child to run the test and return exit code
  1080. */
  1081. waitpid (pid, &stat, 0);
  1082. err = WEXITSTATUS (stat);
  1083. if (!skippable) {
  1084. res_str = (err == 0 ? "passed" : "failed");
  1085. if (err != 0) {
  1086. (*all_passed) = 0;
  1087. }
  1088. } else {
  1089. res_str = (err == 0 ? "passed" : (err == 1 ? "skipped" : "failed"));
  1090. if (err == 1) {
  1091. (*no_skipped)++;
  1092. } else if (err > 1) {
  1093. (*all_passed) = 0;
  1094. }
  1095. }
  1096. fprintf (stderr, "%s test %s\n", test_name, res_str);
  1097. return (err);
  1098. }
  1099. static int run_cmap_test(const char *test_name,
  1100. int (test_func)(pid_t pid, pid_t old_pid, int test_n),
  1101. int no_tests, int *all_passed, int *no_skipped)
  1102. {
  1103. pid_t pid, old_pid;
  1104. int err;
  1105. int stat;
  1106. const char *res_str;
  1107. int n;
  1108. old_pid = 0;
  1109. fprintf (stderr, "Running %s test\n", test_name);
  1110. for (n = 1; n <= no_tests; n++) {
  1111. pid = fork ();
  1112. if (pid == -1) {
  1113. fprintf (stderr, "Can't fork\n");
  1114. exit (2);
  1115. }
  1116. if (pid == 0) {
  1117. err = test_func (getpid (), old_pid, n);
  1118. sam_finalize ();
  1119. exit (err);
  1120. }
  1121. waitpid (pid, &stat, 0);
  1122. old_pid = pid;
  1123. err = WEXITSTATUS (stat);
  1124. if (err != 0) {
  1125. break;
  1126. }
  1127. }
  1128. res_str = (err == 0 ? "passed" : (err == 1 ? "skipped" : "failed"));
  1129. if (err == 1) {
  1130. (*no_skipped)++;
  1131. } else if (err > 1) {
  1132. (*all_passed) = 0;
  1133. }
  1134. fprintf (stderr, "%s test %s\n", test_name, res_str);
  1135. return (err);
  1136. }
  1137. int main(int argc, char *argv[])
  1138. {
  1139. int all_passed = 1;
  1140. int no_skipped = 0;
  1141. setlinebuf(stdout);
  1142. run_test("basics", test_basics, 0, &all_passed, &no_skipped);
  1143. run_test("signal", test_signal, 0, &all_passed, &no_skipped);
  1144. run_test("smoke", test_smoke, 0, &all_passed, &no_skipped);
  1145. run_test("data_store/restore", test_data_store_restore, 0, &all_passed, &no_skipped);
  1146. run_test("hc", test_hc, 0, &all_passed, &no_skipped);
  1147. run_test("warn_signal", test_warn_signal, 0, &all_passed, &no_skipped);
  1148. run_test("quorum", test_quorum, 1, &all_passed, &no_skipped);
  1149. run_cmap_test("cmap quit", test_cmap_quit, 3, &all_passed, &no_skipped);
  1150. run_cmap_test("cmap restart", test_cmap_restart, 2, &all_passed, &no_skipped);
  1151. if (all_passed)
  1152. fprintf (stderr, "All tests passed (%d skipped)\n", no_skipped);
  1153. return (all_passed ? 0 : 1);
  1154. }