testsam.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Provides test of SAM API
  36. */
  37. #include <config.h>
  38. #include <limits.h>
  39. #include <pthread.h>
  40. #include <sys/types.h>
  41. #include <stdio.h>
  42. #include <stdint.h>
  43. #include <stdlib.h>
  44. #include <unistd.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/confdb.h>
  47. #include <corosync/sam.h>
  48. #include <signal.h>
  49. #include <string.h>
  50. #include <sys/wait.h>
  51. extern const char *__progname;
  52. static int test2_sig_delivered = 0;
  53. static int test5_hc_cb_count = 0;
  54. static int test6_sig_delivered = 0;
  55. /*
  56. * First test will just register SAM, with policy restart. First instance will
  57. * sleep one second, send hc and sleep another 3 seconds. This should force restart.
  58. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
  59. * Then start hc again and sleep 3 seconds. This should force restart again.
  60. * Last instance just calls initialize again. This should end with error.
  61. * Then call start, followed by stop and start again. Finally, we will call finalize
  62. * twice. One should succeed, second should fail. After this, we will call every function
  63. * (none should succeed).
  64. */
  65. static int test1 (void)
  66. {
  67. cs_error_t error;
  68. unsigned int instance_id;
  69. int i;
  70. printf ("%s: initialize\n", __FUNCTION__);
  71. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  72. if (error != CS_OK) {
  73. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  74. return 1;
  75. }
  76. printf ("%s: register\n", __FUNCTION__);
  77. error = sam_register (&instance_id);
  78. if (error != CS_OK) {
  79. fprintf (stderr, "Can't register. Error %d\n", error);
  80. return 1;
  81. }
  82. if (instance_id == 1 || instance_id == 2) {
  83. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  84. error = sam_start ();
  85. if (error != CS_OK) {
  86. fprintf (stderr, "Can't start hc. Error %d\n", error);
  87. return 1;
  88. }
  89. for (i = 0; i < 10; i++) {
  90. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  91. sleep (1);
  92. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  93. error = sam_hc_send ();
  94. if (error != CS_OK) {
  95. fprintf (stderr, "Can't send hc. Error %d\n", error);
  96. return 1;
  97. }
  98. }
  99. if (instance_id == 2) {
  100. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  101. error = sam_stop ();
  102. if (error != CS_OK) {
  103. fprintf (stderr, "Can't send hc. Error %d\n", error);
  104. return 1;
  105. }
  106. }
  107. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  108. sleep (3);
  109. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  110. error = sam_start ();
  111. if (error != CS_OK) {
  112. fprintf (stderr, "Can't start hc. Error %d\n", error);
  113. return 1;
  114. }
  115. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  116. sleep (3);
  117. return 0;
  118. }
  119. if (instance_id == 3) {
  120. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  121. if (error == CS_OK) {
  122. fprintf (stderr, "Can initialize SAM API after initialization");
  123. return 1;
  124. }
  125. error = sam_start ();
  126. if (error != CS_OK) {
  127. fprintf (stderr, "Can't start hc. Error %d\n", error);
  128. return 1;
  129. }
  130. error = sam_stop ();
  131. if (error != CS_OK) {
  132. fprintf (stderr, "Can't stop hc. Error %d\n", error);
  133. return 1;
  134. }
  135. error = sam_finalize ();
  136. if (error != CS_OK) {
  137. fprintf (stderr, "Can't finalize sam. Error %d\n", error);
  138. return 1;
  139. }
  140. error = sam_finalize ();
  141. if (error == CS_OK) {
  142. fprintf (stderr, "Can finalize sam after finalization!\n");
  143. return 1;
  144. }
  145. if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
  146. sam_start () == CS_OK || sam_stop () == CS_OK ||
  147. sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
  148. sam_hc_callback_register (NULL) == CS_OK) {
  149. fprintf (stderr, "Can call one of function after finalization!\n");
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. static void test2_signal (int sig) {
  157. printf ("%s\n", __FUNCTION__);
  158. test2_sig_delivered = 1;
  159. }
  160. /*
  161. * This tests recovery policy quit and callback.
  162. */
  163. static int test2 (void) {
  164. cs_error_t error;
  165. unsigned int instance_id;
  166. printf ("%s: initialize\n", __FUNCTION__);
  167. error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
  168. if (error != CS_OK) {
  169. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  170. return 1;
  171. }
  172. printf ("%s: register\n", __FUNCTION__);
  173. error = sam_register (&instance_id);
  174. if (error != CS_OK) {
  175. fprintf (stderr, "Can't register. Error %d\n", error);
  176. return 1;
  177. }
  178. if (instance_id == 1) {
  179. signal (SIGTERM, test2_signal);
  180. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  181. error = sam_start ();
  182. if (error != CS_OK) {
  183. fprintf (stderr, "Can't start hc. Error %d\n", error);
  184. return 1;
  185. }
  186. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  187. sleep (1);
  188. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  189. error = sam_hc_send ();
  190. if (error != CS_OK) {
  191. fprintf (stderr, "Can't send hc. Error %d\n", error);
  192. return 1;
  193. }
  194. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  195. while (!test2_sig_delivered) {
  196. sleep (1);
  197. }
  198. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  199. sleep (3);
  200. }
  201. return 1;
  202. }
  203. /*
  204. * Smoke test. Better to turn off coredump ;) This has no time limit, just restart process
  205. * when it dies.
  206. */
  207. static int test3 (void) {
  208. cs_error_t error;
  209. unsigned int instance_id;
  210. int tmp1, tmp2, tmp3;
  211. printf ("%s: initialize\n", __FUNCTION__);
  212. error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  213. if (error != CS_OK) {
  214. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  215. return 1;
  216. }
  217. printf ("%s: register\n", __FUNCTION__);
  218. error = sam_register (&instance_id);
  219. if (error != CS_OK) {
  220. fprintf (stderr, "Can't register. Error %d\n", error);
  221. return 1;
  222. }
  223. if (instance_id < 100) {
  224. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  225. error = sam_start ();
  226. if (error != CS_OK) {
  227. fprintf (stderr, "Can't start hc. Error %d\n", error);
  228. return 1;
  229. }
  230. printf ("%s iid %d: divide by zero\n", __FUNCTION__, instance_id);
  231. tmp2 = rand ();
  232. tmp3 = 0;
  233. tmp1 = tmp2 / tmp3;
  234. tmp3 = tmp1;
  235. return 1;
  236. }
  237. return 0;
  238. }
  239. /*
  240. * Test sam_data_store, sam_data_restore and sam_data_getsize
  241. */
  242. static int test4 (void)
  243. {
  244. size_t size;
  245. cs_error_t err;
  246. int i;
  247. unsigned int instance_id;
  248. char saved_data[128];
  249. char saved_data2[128];
  250. printf ("%s: sam_data_getsize 1\n", __FUNCTION__);
  251. err = sam_data_getsize (&size);
  252. if (err != CS_ERR_BAD_HANDLE) {
  253. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  254. return 1;
  255. }
  256. printf ("%s: sam_data_getsize 2\n", __FUNCTION__);
  257. err = sam_data_getsize (NULL);
  258. if (err != CS_ERR_INVALID_PARAM) {
  259. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  260. return 1;
  261. }
  262. printf ("%s: sam_data_store 1\n", __FUNCTION__);
  263. err = sam_data_store (NULL, 0);
  264. if (err != CS_ERR_BAD_HANDLE) {
  265. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  266. return 1;
  267. }
  268. printf ("%s: sam_data_restore 1\n", __FUNCTION__);
  269. err = sam_data_restore (saved_data, sizeof (saved_data));
  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_initialize\n", __FUNCTION__);
  275. err = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  276. if (err != CS_OK) {
  277. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  278. return 1;
  279. }
  280. printf ("%s: sam_data_getsize 3\n", __FUNCTION__);
  281. err = sam_data_getsize (&size);
  282. if (err != CS_OK) {
  283. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  284. return 1;
  285. }
  286. if (size != 0) {
  287. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  288. return 1;
  289. }
  290. printf ("%s: sam_data_restore 2\n", __FUNCTION__);
  291. err = sam_data_restore (NULL, sizeof (saved_data));
  292. if (err != CS_ERR_INVALID_PARAM) {
  293. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  294. return 1;
  295. }
  296. /*
  297. * Store some real data
  298. */
  299. for (i = 0; i < sizeof (saved_data); i++) {
  300. saved_data[i] = (char)(i + 5);
  301. }
  302. printf ("%s: sam_data_store 2\n", __FUNCTION__);
  303. err = sam_data_store (saved_data, sizeof (saved_data));
  304. if (err != CS_OK) {
  305. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  306. return 1;
  307. }
  308. printf ("%s: sam_data_getsize 4\n", __FUNCTION__);
  309. err = sam_data_getsize (&size);
  310. if (err != CS_OK) {
  311. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  312. return 1;
  313. }
  314. if (size != sizeof (saved_data)) {
  315. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  316. return 1;
  317. }
  318. printf ("%s: sam_data_restore 3\n", __FUNCTION__);
  319. err = sam_data_restore (saved_data2, sizeof (saved_data2) - 1);
  320. if (err != CS_ERR_INVALID_PARAM) {
  321. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  322. return 1;
  323. }
  324. printf ("%s: sam_data_restore 4\n", __FUNCTION__);
  325. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  326. if (err != CS_OK) {
  327. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  328. return 1;
  329. }
  330. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  331. fprintf (stderr, "Retored data are not same\n");
  332. return 1;
  333. }
  334. memset (saved_data2, 0, sizeof (saved_data2));
  335. printf ("%s: sam_data_store 3\n", __FUNCTION__);
  336. err = sam_data_store (NULL, 1);
  337. if (err != CS_OK) {
  338. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  339. return 1;
  340. }
  341. printf ("%s: sam_data_getsize 5\n", __FUNCTION__);
  342. err = sam_data_getsize (&size);
  343. if (err != CS_OK) {
  344. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  345. return 1;
  346. }
  347. if (size != 0) {
  348. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  349. return 1;
  350. }
  351. printf ("%s: sam_data_store 4\n", __FUNCTION__);
  352. err = sam_data_store (saved_data, sizeof (saved_data));
  353. if (err != CS_OK) {
  354. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  355. return 1;
  356. }
  357. printf ("%s: register\n", __FUNCTION__);
  358. err = sam_register (&instance_id);
  359. if (err != CS_OK) {
  360. fprintf (stderr, "Can't register. Error %d\n", err);
  361. return 1;
  362. }
  363. if (instance_id == 1) {
  364. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  365. err = sam_start ();
  366. if (err != CS_OK) {
  367. fprintf (stderr, "Can't start hc. Error %d\n", err);
  368. return 1;
  369. }
  370. printf ("%s iid %d: sam_data_getsize 6\n", __FUNCTION__, instance_id);
  371. err = sam_data_getsize (&size);
  372. if (err != CS_OK) {
  373. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  374. return 1;
  375. }
  376. if (size != sizeof (saved_data2)) {
  377. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  378. return 1;
  379. }
  380. printf ("%s iid %d: sam_data_restore 5\n", __FUNCTION__, instance_id);
  381. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  382. if (err != CS_OK) {
  383. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  384. return 1;
  385. }
  386. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  387. fprintf (stderr, "Retored data are not same\n");
  388. return 1;
  389. }
  390. for (i = 0; i < sizeof (saved_data); i++) {
  391. saved_data[i] = (char)(i - 5);
  392. }
  393. printf ("%s iid %d: sam_data_store 5\n", __FUNCTION__, instance_id);
  394. err = sam_data_store (saved_data, sizeof (saved_data) - 7);
  395. if (err != CS_OK) {
  396. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  397. return 1;
  398. }
  399. exit (1);
  400. }
  401. if (instance_id == 2) {
  402. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  403. err = sam_start ();
  404. if (err != CS_OK) {
  405. fprintf (stderr, "Can't start hc. Error %d\n", err);
  406. return 1;
  407. }
  408. printf ("%s iid %d: sam_data_getsize 7\n", __FUNCTION__, instance_id);
  409. err = sam_data_getsize (&size);
  410. if (err != CS_OK) {
  411. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  412. return 1;
  413. }
  414. if (size != sizeof (saved_data2) - 7) {
  415. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  416. return 1;
  417. }
  418. printf ("%s iid %d: sam_data_restore 6\n", __FUNCTION__, instance_id);
  419. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  420. if (err != CS_OK) {
  421. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  422. return 1;
  423. }
  424. for (i = 0; i < sizeof (saved_data); i++) {
  425. saved_data[i] = (char)(i - 5);
  426. }
  427. if (memcmp (saved_data, saved_data2, sizeof (saved_data2) - 7) != 0) {
  428. fprintf (stderr, "Retored data are not same\n");
  429. return 1;
  430. }
  431. printf ("%s iid %d: sam_data_store 6\n", __FUNCTION__, instance_id);
  432. err = sam_data_store (NULL, 0);
  433. if (err != CS_OK) {
  434. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  435. return 1;
  436. }
  437. exit (1);
  438. }
  439. if (instance_id == 3) {
  440. printf ("%s iid %d: sam_data_getsize 8\n", __FUNCTION__, instance_id);
  441. err = sam_data_getsize (&size);
  442. if (err != CS_OK) {
  443. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  444. return 1;
  445. }
  446. if (size != 0) {
  447. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  448. return 1;
  449. }
  450. }
  451. return (0);
  452. }
  453. static int test5_hc_cb (void)
  454. {
  455. printf ("%s %d\n", __FUNCTION__, ++test5_hc_cb_count);
  456. sam_data_store (&test5_hc_cb_count, sizeof (test5_hc_cb_count));
  457. if (test5_hc_cb_count > 10)
  458. return 1;
  459. return 0;
  460. }
  461. /*
  462. * Test event driven healtchecking.
  463. */
  464. static int test5 (void)
  465. {
  466. cs_error_t error;
  467. unsigned int instance_id;
  468. int hc_cb_count;
  469. printf ("%s: initialize\n", __FUNCTION__);
  470. error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
  471. if (error != CS_OK) {
  472. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  473. return 1;
  474. }
  475. printf ("%s: register\n", __FUNCTION__);
  476. error = sam_register (&instance_id);
  477. if (error != CS_OK) {
  478. fprintf (stderr, "Can't register. Error %d\n", error);
  479. return 1;
  480. }
  481. if (instance_id == 1) {
  482. printf ("%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
  483. error = sam_hc_callback_register (test5_hc_cb);
  484. if (error != CS_OK) {
  485. fprintf (stderr, "Can't register hc cb. Error %d\n", error);
  486. return 1;
  487. }
  488. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  489. error = sam_start ();
  490. if (error != CS_OK) {
  491. fprintf (stderr, "Can't start hc. Error %d\n", error);
  492. return 1;
  493. }
  494. sleep (2);
  495. printf ("%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  496. return 1;
  497. }
  498. if (instance_id == 2) {
  499. error = sam_data_restore (&hc_cb_count, sizeof (hc_cb_count));
  500. if (error != CS_OK) {
  501. fprintf (stderr, "sam_data_restore should return CS_OK. Error returned %d\n", error);
  502. return 1;
  503. }
  504. if (hc_cb_count != 11) {
  505. fprintf (stderr, "%s iid %d: Premature killed. hc_cb_count should be 11 and it is %d\n",
  506. __FUNCTION__, instance_id - 1, hc_cb_count);
  507. return 1;
  508. }
  509. return 0;
  510. }
  511. return 1;
  512. }
  513. static void test6_signal (int sig) {
  514. cs_error_t error;
  515. printf ("%s\n", __FUNCTION__);
  516. test6_sig_delivered++;
  517. if ((error = sam_data_store (&test6_sig_delivered, sizeof (test6_sig_delivered))) != CS_OK) {
  518. fprintf (stderr, "Can't store data! Error : %d\n", error);
  519. }
  520. }
  521. /*
  522. * Test warn signal set.
  523. */
  524. static int test6 (void) {
  525. cs_error_t error;
  526. unsigned int instance_id;
  527. int test6_sig_del;
  528. printf ("%s: initialize\n", __FUNCTION__);
  529. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  530. if (error != CS_OK) {
  531. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  532. return 1;
  533. }
  534. printf ("%s: register\n", __FUNCTION__);
  535. error = sam_register (&instance_id);
  536. if (error != CS_OK) {
  537. fprintf (stderr, "Can't register. Error %d\n", error);
  538. return 1;
  539. }
  540. if (instance_id == 1) {
  541. error = sam_warn_signal_set (SIGUSR1);
  542. if (error != CS_OK) {
  543. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  544. return 1;
  545. }
  546. signal (SIGUSR1, test6_signal);
  547. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  548. error = sam_start ();
  549. if (error != CS_OK) {
  550. fprintf (stderr, "Can't start hc. Error %d\n", error);
  551. return 1;
  552. }
  553. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  554. sleep (1);
  555. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  556. error = sam_hc_send ();
  557. if (error != CS_OK) {
  558. fprintf (stderr, "Can't send hc. Error %d\n", error);
  559. return 1;
  560. }
  561. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  562. while (!test6_sig_delivered) {
  563. sleep (1);
  564. }
  565. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  566. sleep (3);
  567. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  568. return (1);
  569. }
  570. if (instance_id == 2) {
  571. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  572. if (error != CS_OK) {
  573. fprintf (stderr, "Can't restore data. Error %d\n", error);
  574. return 1;
  575. }
  576. if (test6_sig_del != 1) {
  577. fprintf (stderr, "Previous test failed. Signal was not delivered\n");
  578. return 1;
  579. }
  580. error = sam_warn_signal_set (SIGKILL);
  581. if (error != CS_OK) {
  582. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  583. return 1;
  584. }
  585. signal (SIGUSR1, test6_signal);
  586. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  587. error = sam_start ();
  588. if (error != CS_OK) {
  589. fprintf (stderr, "Can't start hc. Error %d\n", error);
  590. return 1;
  591. }
  592. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  593. sleep (1);
  594. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  595. error = sam_hc_send ();
  596. if (error != CS_OK) {
  597. fprintf (stderr, "Can't send hc. Error %d\n", error);
  598. return 1;
  599. }
  600. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  601. while (!test6_sig_delivered) {
  602. sleep (1);
  603. }
  604. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  605. return (1);
  606. }
  607. if (instance_id == 3) {
  608. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  609. if (error != CS_OK) {
  610. fprintf (stderr, "Can't restore data. Error %d\n", error);
  611. return 1;
  612. }
  613. if (test6_sig_del != 1) {
  614. fprintf (stderr, "Previous test failed. Signal WAS delivered\n");
  615. return 1;
  616. }
  617. return (0);
  618. }
  619. return 1;
  620. }
  621. static void *test7_thread (void *arg)
  622. {
  623. /* Wait 5s */
  624. sleep (5);
  625. exit (0);
  626. }
  627. /*
  628. * Test quorum
  629. */
  630. static int test7 (void) {
  631. confdb_handle_t cdb_handle;
  632. cs_error_t err;
  633. hdb_handle_t quorum_handle;
  634. size_t value_len;
  635. char key_value[256];
  636. unsigned int instance_id;
  637. pthread_t kill_thread;
  638. err = confdb_initialize (&cdb_handle, NULL);
  639. if (err != CS_OK) {
  640. printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err);
  641. return (1);
  642. }
  643. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  644. if (err != CS_OK) {
  645. printf ("Could not start object_find %d. Test skipped\n", err);
  646. return (1);
  647. }
  648. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "quorum", strlen("quorum"), &quorum_handle);
  649. if (err != CS_OK) {
  650. printf ("Could not object_find \"quorum\": %d. Test skipped\n", err);
  651. return (1);
  652. }
  653. err = confdb_key_get(cdb_handle, quorum_handle, "provider", strlen("provider"), key_value, &value_len);
  654. if (err != CS_OK) {
  655. printf ("Could not get \"provider\" key: %d. Test skipped\n", err);
  656. return (1);
  657. }
  658. if (!(value_len - 1 == strlen ("testquorum") && memcmp (key_value, "testquorum", value_len - 1) == 0)) {
  659. printf ("Provider is not testquorum. Test skipped\n");
  660. return (1);
  661. }
  662. /*
  663. * Set to not quorate
  664. */
  665. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0"));
  666. if (err != CS_OK) {
  667. printf ("Can't create confdb key. Error %d\n", err);
  668. return (2);
  669. }
  670. printf ("%s: initialize\n", __FUNCTION__);
  671. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUORUM_RESTART);
  672. if (err != CS_OK) {
  673. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  674. return 2;
  675. }
  676. printf ("%s: register\n", __FUNCTION__);
  677. err = sam_register (&instance_id);
  678. if (err != CS_OK) {
  679. fprintf (stderr, "Can't register. Error %d\n", err);
  680. return 2;
  681. }
  682. if (instance_id == 1) {
  683. /*
  684. * Sam start should block forever, but 10s for us should be enough
  685. */
  686. pthread_create (&kill_thread, NULL, test7_thread, NULL);
  687. printf ("%s iid %d: start - should block forever (waiting 5s)\n", __FUNCTION__, instance_id);
  688. err = sam_start ();
  689. if (err != CS_OK) {
  690. fprintf (stderr, "Can't start hc. Error %d\n", err);
  691. return 2;
  692. }
  693. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  694. return (2);
  695. }
  696. if (instance_id == 2) {
  697. /*
  698. * Set to quorate
  699. */
  700. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "1", strlen("1"));
  701. if (err != CS_OK) {
  702. printf ("Can't create confdb key. Error %d\n", err);
  703. return (2);
  704. }
  705. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  706. err = sam_start ();
  707. if (err != CS_OK) {
  708. fprintf (stderr, "Can't start hc. Error %d\n", err);
  709. return 2;
  710. }
  711. /*
  712. * Set corosync unquorate
  713. */
  714. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0"));
  715. if (err != CS_OK) {
  716. printf ("Can't create confdb key. Error %d\n", err);
  717. return (2);
  718. }
  719. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  720. sleep (3);
  721. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  722. return (2);
  723. }
  724. if (instance_id == 3) {
  725. return (0);
  726. }
  727. return (2);
  728. }
  729. /*
  730. * Test confdb integration + quit policy
  731. */
  732. static int test8 (pid_t pid, pid_t old_pid, int test_n) {
  733. confdb_handle_t cdb_handle;
  734. cs_error_t err;
  735. hdb_handle_t res_handle, proc_handle, pid_handle;
  736. size_t value_len;
  737. uint64_t tstamp1, tstamp2;
  738. int32_t msec_diff;
  739. char key_value[256];
  740. unsigned int instance_id;
  741. char tmp_obj[PATH_MAX];
  742. confdb_value_types_t cdbtype;
  743. err = confdb_initialize (&cdb_handle, NULL);
  744. if (err != CS_OK) {
  745. printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err);
  746. return (1);
  747. }
  748. printf ("%s test %d\n", __FUNCTION__, test_n);
  749. if (test_n == 2) {
  750. /*
  751. * Object should not exist
  752. */
  753. printf ("%s Testing if object exists (it shouldn't)\n", __FUNCTION__);
  754. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  755. if (err != CS_OK) {
  756. printf ("Could not start object_find %d.\n", err);
  757. return (2);
  758. }
  759. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  760. if (err != CS_OK) {
  761. printf ("Could not object_find \"resources\": %d.\n", err);
  762. return (2);
  763. }
  764. err = confdb_object_find_start(cdb_handle, res_handle);
  765. if (err != CS_OK) {
  766. printf ("Could not start object_find %d.\n", err);
  767. return (2);
  768. }
  769. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  770. if (err != CS_OK) {
  771. printf ("Could not object_find \"process\": %d.\n", err);
  772. return (2);
  773. }
  774. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  775. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  776. }
  777. err = confdb_object_find_start(cdb_handle, proc_handle);
  778. if (err != CS_OK) {
  779. printf ("Could not start object_find %d.\n", err);
  780. return (2);
  781. }
  782. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  783. if (err == CS_OK) {
  784. printf ("Could find object \"%s\": %d.\n", tmp_obj, err);
  785. return (2);
  786. }
  787. }
  788. if (test_n == 1 || test_n == 2) {
  789. printf ("%s: initialize\n", __FUNCTION__);
  790. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CONFDB);
  791. if (err != CS_OK) {
  792. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  793. return 2;
  794. }
  795. printf ("%s: register\n", __FUNCTION__);
  796. err = sam_register (&instance_id);
  797. if (err != CS_OK) {
  798. fprintf (stderr, "Can't register. Error %d\n", err);
  799. return 2;
  800. }
  801. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  802. if (err != CS_OK) {
  803. printf ("Could not start object_find %d.\n", err);
  804. return (2);
  805. }
  806. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  807. if (err != CS_OK) {
  808. printf ("Could not object_find \"resources\": %d.\n", err);
  809. return (2);
  810. }
  811. err = confdb_object_find_start(cdb_handle, res_handle);
  812. if (err != CS_OK) {
  813. printf ("Could not start object_find %d.\n", err);
  814. return (2);
  815. }
  816. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  817. if (err != CS_OK) {
  818. printf ("Could not object_find \"process\": %d.\n", err);
  819. return (2);
  820. }
  821. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  822. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  823. }
  824. err = confdb_object_find_start(cdb_handle, proc_handle);
  825. if (err != CS_OK) {
  826. printf ("Could not start object_find %d.\n", err);
  827. return (2);
  828. }
  829. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  830. if (err != CS_OK) {
  831. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  832. return (2);
  833. }
  834. err = confdb_key_get(cdb_handle, pid_handle, "recovery", strlen("recovery"), key_value, &value_len);
  835. if (err != CS_OK) {
  836. printf ("Could not get \"recovery\" key: %d.\n", err);
  837. return (2);
  838. }
  839. if (value_len != strlen ("quit") || memcmp (key_value, "quit", value_len) != 0) {
  840. printf ("Recovery key \"%s\" is not \"watchdog\".\n", key_value);
  841. return (2);
  842. }
  843. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  844. if (err != CS_OK) {
  845. printf ("Could not get \"state\" key: %d.\n", err);
  846. return (2);
  847. }
  848. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  849. printf ("State key is not \"stopped\".\n");
  850. return (2);
  851. }
  852. printf ("%s iid %d: start\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 = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  859. if (err != CS_OK) {
  860. printf ("Could not get \"state\" key: %d.\n", err);
  861. return (2);
  862. }
  863. if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) {
  864. printf ("State key is not \"running\".\n");
  865. return (2);
  866. }
  867. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  868. err = sam_stop ();
  869. if (err != CS_OK) {
  870. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  871. return 2;
  872. }
  873. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  874. if (err != CS_OK) {
  875. printf ("Could not get \"state\" key: %d.\n", err);
  876. return (2);
  877. }
  878. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  879. printf ("State key is not \"stopped\".\n");
  880. return (2);
  881. }
  882. printf ("%s iid %d: sleeping 5\n", __FUNCTION__, instance_id);
  883. sleep (5);
  884. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  885. if (err != CS_OK) {
  886. printf ("Could not get \"state\" key: %d.\n", err);
  887. return (2);
  888. }
  889. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  890. printf ("State key is not \"stopped\".\n");
  891. return (2);
  892. }
  893. printf ("%s iid %d: start 2\n", __FUNCTION__, instance_id);
  894. err = sam_start ();
  895. if (err != CS_OK) {
  896. fprintf (stderr, "Can't start hc. Error %d\n", err);
  897. return 2;
  898. }
  899. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  900. if (err != CS_OK) {
  901. printf ("Could not get \"state\" key: %d.\n", err);
  902. return (2);
  903. }
  904. if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) {
  905. printf ("State key is not \"running\".\n");
  906. return (2);
  907. }
  908. if (test_n == 2) {
  909. printf ("%s iid %d: sleeping 5. Should be killed\n", __FUNCTION__, instance_id);
  910. sleep (5);
  911. return (2);
  912. } else {
  913. printf ("%s iid %d: Test HC\n", __FUNCTION__, instance_id);
  914. err = sam_hc_send ();
  915. if (err != CS_OK) {
  916. fprintf (stderr, "Can't send hc. Error %d\n", err);
  917. return 2;
  918. }
  919. err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp1, &value_len, &cdbtype);
  920. if (err != CS_OK) {
  921. printf ("Could not get \"state\" key: %d.\n", err);
  922. return (2);
  923. }
  924. printf ("%s iid %d: Sleep 1\n", __FUNCTION__, instance_id);
  925. sleep (1);
  926. err = sam_hc_send ();
  927. if (err != CS_OK) {
  928. fprintf (stderr, "Can't send hc. Error %d\n", err);
  929. return 2;
  930. }
  931. sleep (1);
  932. err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp2, &value_len, &cdbtype);
  933. if (err != CS_OK) {
  934. printf ("Could not get \"state\" key: %d.\n", err);
  935. return (2);
  936. }
  937. msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC;
  938. if (msec_diff < 500 || msec_diff > 2000) {
  939. printf ("Difference %d is not within <500, 2000> interval.\n", msec_diff);
  940. return (2);
  941. }
  942. printf ("%s iid %d: stop 2\n", __FUNCTION__, instance_id);
  943. err = sam_stop ();
  944. if (err != CS_OK) {
  945. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  946. return 2;
  947. }
  948. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  949. if (err != CS_OK) {
  950. printf ("Could not get \"state\" key: %d.\n", err);
  951. return (2);
  952. }
  953. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  954. printf ("State key is not \"stopped\".\n");
  955. return (2);
  956. }
  957. printf ("%s iid %d: exiting\n", __FUNCTION__, instance_id);
  958. return (0);
  959. }
  960. }
  961. if (test_n == 3) {
  962. printf ("%s Testing if status is failed\n", __FUNCTION__);
  963. /*
  964. * Previous should be FAILED
  965. */
  966. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  967. if (err != CS_OK) {
  968. printf ("Could not start object_find %d.\n", err);
  969. return (2);
  970. }
  971. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  972. if (err != CS_OK) {
  973. printf ("Could not object_find \"resources\": %d.\n", err);
  974. return (2);
  975. }
  976. err = confdb_object_find_start(cdb_handle, res_handle);
  977. if (err != CS_OK) {
  978. printf ("Could not start object_find %d.\n", err);
  979. return (2);
  980. }
  981. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  982. if (err != CS_OK) {
  983. printf ("Could not object_find \"process\": %d.\n", err);
  984. return (2);
  985. }
  986. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  987. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  988. }
  989. err = confdb_object_find_start(cdb_handle, proc_handle);
  990. if (err != CS_OK) {
  991. printf ("Could not start object_find %d.\n", err);
  992. return (2);
  993. }
  994. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  995. if (err != CS_OK) {
  996. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  997. return (2);
  998. }
  999. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1000. if (err != CS_OK) {
  1001. printf ("Could not get \"state\" key: %d.\n", err);
  1002. return (2);
  1003. }
  1004. if (value_len != strlen ("failed") || memcmp (key_value, "failed", value_len) != 0) {
  1005. printf ("State key is not \"failed\".\n");
  1006. return (2);
  1007. }
  1008. return (0);
  1009. }
  1010. return (2);
  1011. }
  1012. /*
  1013. * Test confdb integration + restart policy
  1014. */
  1015. static int test9 (pid_t pid, pid_t old_pid, int test_n) {
  1016. confdb_handle_t cdb_handle;
  1017. cs_error_t err;
  1018. hdb_handle_t res_handle, proc_handle, pid_handle;
  1019. size_t value_len;
  1020. char key_value[256];
  1021. unsigned int instance_id;
  1022. char tmp_obj[PATH_MAX];
  1023. err = confdb_initialize (&cdb_handle, NULL);
  1024. if (err != CS_OK) {
  1025. printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err);
  1026. return (1);
  1027. }
  1028. printf ("%s test %d\n", __FUNCTION__, test_n);
  1029. if (test_n == 1) {
  1030. printf ("%s: initialize\n", __FUNCTION__);
  1031. err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | SAM_RECOVERY_POLICY_CONFDB);
  1032. if (err != CS_OK) {
  1033. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  1034. return 2;
  1035. }
  1036. printf ("%s: register\n", __FUNCTION__);
  1037. err = sam_register (&instance_id);
  1038. if (err != CS_OK) {
  1039. fprintf (stderr, "Can't register. Error %d\n", err);
  1040. return 2;
  1041. }
  1042. printf ("%s: iid %d\n", __FUNCTION__, instance_id);
  1043. if (instance_id < 3) {
  1044. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  1045. if (err != CS_OK) {
  1046. printf ("Could not start object_find %d.\n", err);
  1047. return (2);
  1048. }
  1049. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"),
  1050. &res_handle);
  1051. if (err != CS_OK) {
  1052. printf ("Could not object_find \"resources\": %d.\n", err);
  1053. return (2);
  1054. }
  1055. err = confdb_object_find_start(cdb_handle, res_handle);
  1056. if (err != CS_OK) {
  1057. printf ("Could not start object_find %d.\n", err);
  1058. return (2);
  1059. }
  1060. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  1061. if (err != CS_OK) {
  1062. printf ("Could not object_find \"process\": %d.\n", err);
  1063. return (2);
  1064. }
  1065. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  1066. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  1067. }
  1068. err = confdb_object_find_start(cdb_handle, proc_handle);
  1069. if (err != CS_OK) {
  1070. printf ("Could not start object_find %d.\n", err);
  1071. return (2);
  1072. }
  1073. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  1074. if (err != CS_OK) {
  1075. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  1076. return (2);
  1077. }
  1078. err = confdb_key_get(cdb_handle, pid_handle, "recovery", strlen("recovery"), key_value, &value_len);
  1079. if (err != CS_OK) {
  1080. printf ("Could not get \"recovery\" key: %d.\n", err);
  1081. return (2);
  1082. }
  1083. if (value_len != strlen ("restart") || memcmp (key_value, "restart", value_len) != 0) {
  1084. printf ("Recovery key \"%s\" is not \"restart\".\n", key_value);
  1085. return (2);
  1086. }
  1087. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1088. if (err != CS_OK) {
  1089. printf ("Could not get \"state\" key: %d.\n", err);
  1090. return (2);
  1091. }
  1092. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  1093. printf ("State key is not \"stopped\".\n");
  1094. return (2);
  1095. }
  1096. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  1097. err = sam_start ();
  1098. if (err != CS_OK) {
  1099. fprintf (stderr, "Can't start hc. Error %d\n", err);
  1100. return 2;
  1101. }
  1102. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1103. if (err != CS_OK) {
  1104. printf ("Could not get \"state\" key: %d.\n", err);
  1105. return (2);
  1106. }
  1107. if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) {
  1108. printf ("State key is not \"running\".\n");
  1109. return (2);
  1110. }
  1111. printf ("%s iid %d: waiting for kill\n", __FUNCTION__, instance_id);
  1112. sleep (10);
  1113. return (2);
  1114. }
  1115. if (instance_id == 3) {
  1116. printf ("%s iid %d: mark failed\n", __FUNCTION__, instance_id);
  1117. if (err != CS_OK) {
  1118. fprintf (stderr, "Can't start hc. Error %d\n", err);
  1119. return 2;
  1120. }
  1121. err = sam_mark_failed ();
  1122. if (err != CS_OK) {
  1123. fprintf (stderr, "Can't mark failed. Error %d\n", err);
  1124. return 2;
  1125. }
  1126. sleep (10);
  1127. return (2);
  1128. }
  1129. return (2);
  1130. }
  1131. if (test_n == 2) {
  1132. printf ("%s Testing if status is failed\n", __FUNCTION__);
  1133. /*
  1134. * Previous should be FAILED
  1135. */
  1136. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  1137. if (err != CS_OK) {
  1138. printf ("Could not start object_find %d.\n", err);
  1139. return (2);
  1140. }
  1141. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  1142. if (err != CS_OK) {
  1143. printf ("Could not object_find \"resources\": %d.\n", err);
  1144. return (2);
  1145. }
  1146. err = confdb_object_find_start(cdb_handle, res_handle);
  1147. if (err != CS_OK) {
  1148. printf ("Could not start object_find %d.\n", err);
  1149. return (2);
  1150. }
  1151. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  1152. if (err != CS_OK) {
  1153. printf ("Could not object_find \"process\": %d.\n", err);
  1154. return (2);
  1155. }
  1156. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  1157. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  1158. }
  1159. err = confdb_object_find_start(cdb_handle, proc_handle);
  1160. if (err != CS_OK) {
  1161. printf ("Could not start object_find %d.\n", err);
  1162. return (2);
  1163. }
  1164. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  1165. if (err != CS_OK) {
  1166. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  1167. return (2);
  1168. }
  1169. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1170. if (err != CS_OK) {
  1171. printf ("Could not get \"state\" key: %d.\n", err);
  1172. return (2);
  1173. }
  1174. if (value_len != strlen ("failed") || memcmp (key_value, "failed", value_len) != 0) {
  1175. printf ("State key is not \"failed\".\n");
  1176. return (2);
  1177. }
  1178. return (0);
  1179. }
  1180. return (2);
  1181. }
  1182. int main(int argc, char *argv[])
  1183. {
  1184. pid_t pid, old_pid;
  1185. int err;
  1186. int stat;
  1187. int all_passed = 1;
  1188. int no_skipped = 0;
  1189. pid = fork ();
  1190. if (pid == -1) {
  1191. fprintf (stderr, "Can't fork\n");
  1192. return 1;
  1193. }
  1194. if (pid == 0) {
  1195. err = test1 ();
  1196. sam_finalize ();
  1197. return err;
  1198. }
  1199. waitpid (pid, &stat, 0);
  1200. fprintf (stderr, "test1 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1201. if (WEXITSTATUS (stat) != 0)
  1202. all_passed = 0;
  1203. pid = fork ();
  1204. if (pid == -1) {
  1205. fprintf (stderr, "Can't fork\n");
  1206. return 1;
  1207. }
  1208. if (pid == 0) {
  1209. err = test2 ();
  1210. sam_finalize ();
  1211. return (err);
  1212. }
  1213. waitpid (pid, &stat, 0);
  1214. fprintf (stderr, "test2 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1215. if (WEXITSTATUS (stat) != 0)
  1216. all_passed = 0;
  1217. pid = fork ();
  1218. if (pid == -1) {
  1219. fprintf (stderr, "Can't fork\n");
  1220. return 1;
  1221. }
  1222. if (pid == 0) {
  1223. err = test3 ();
  1224. sam_finalize ();
  1225. return (err);
  1226. }
  1227. waitpid (pid, &stat, 0);
  1228. fprintf (stderr, "test3 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1229. if (WEXITSTATUS (stat) != 0)
  1230. all_passed = 0;
  1231. pid = fork ();
  1232. if (pid == -1) {
  1233. fprintf (stderr, "Can't fork\n");
  1234. return 1;
  1235. }
  1236. if (pid == 0) {
  1237. err = test4 ();
  1238. sam_finalize ();
  1239. return (err);
  1240. }
  1241. waitpid (pid, &stat, 0);
  1242. fprintf (stderr, "test4 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1243. if (WEXITSTATUS (stat) != 0)
  1244. all_passed = 0;
  1245. pid = fork ();
  1246. if (pid == -1) {
  1247. fprintf (stderr, "Can't fork\n");
  1248. return 1;
  1249. }
  1250. if (pid == 0) {
  1251. err = test5 ();
  1252. sam_finalize ();
  1253. return (err);
  1254. }
  1255. waitpid (pid, &stat, 0);
  1256. fprintf (stderr, "test5 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1257. if (WEXITSTATUS (stat) != 0)
  1258. all_passed = 0;
  1259. pid = fork ();
  1260. if (pid == -1) {
  1261. fprintf (stderr, "Can't fork\n");
  1262. return 1;
  1263. }
  1264. if (pid == 0) {
  1265. err = test6 ();
  1266. sam_finalize ();
  1267. return (err);
  1268. }
  1269. waitpid (pid, &stat, 0);
  1270. fprintf (stderr, "test6 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1271. if (WEXITSTATUS (stat) != 0)
  1272. all_passed = 0;
  1273. pid = fork ();
  1274. if (pid == -1) {
  1275. fprintf (stderr, "Can't fork\n");
  1276. return 2;
  1277. }
  1278. if (pid == 0) {
  1279. err = test7 ();
  1280. sam_finalize ();
  1281. return (err);
  1282. }
  1283. waitpid (pid, &stat, 0);
  1284. fprintf (stderr, "test7 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : (WEXITSTATUS (stat) == 1 ? "skipped" : "failed")));
  1285. if (WEXITSTATUS (stat) == 1)
  1286. no_skipped++;
  1287. if (WEXITSTATUS (stat) > 1)
  1288. all_passed = 0;
  1289. pid = fork ();
  1290. if (pid == -1) {
  1291. fprintf (stderr, "Can't fork\n");
  1292. return 2;
  1293. }
  1294. if (pid == 0) {
  1295. err = test8 (getpid (), 0, 1);
  1296. sam_finalize ();
  1297. return (err);
  1298. }
  1299. waitpid (pid, &stat, 0);
  1300. old_pid = pid;
  1301. if (WEXITSTATUS (stat) == 0) {
  1302. pid = fork ();
  1303. if (pid == -1) {
  1304. fprintf (stderr, "Can't fork\n");
  1305. return 2;
  1306. }
  1307. if (pid == 0) {
  1308. err = test8 (getpid (), old_pid, 2);
  1309. sam_finalize ();
  1310. return (err);
  1311. }
  1312. waitpid (pid, &stat, 0);
  1313. old_pid = pid;
  1314. if (WEXITSTATUS (stat) == 0) {
  1315. pid = fork ();
  1316. if (pid == -1) {
  1317. fprintf (stderr, "Can't fork\n");
  1318. return 2;
  1319. }
  1320. if (pid == 0) {
  1321. err = test8 (old_pid, 0, 3);
  1322. sam_finalize ();
  1323. return (err);
  1324. }
  1325. waitpid (pid, &stat, 0);
  1326. }
  1327. }
  1328. if (WEXITSTATUS (stat) == 1)
  1329. no_skipped++;
  1330. if (WEXITSTATUS (stat) > 1)
  1331. all_passed = 0;
  1332. pid = fork ();
  1333. if (pid == -1) {
  1334. fprintf (stderr, "Can't fork\n");
  1335. return 2;
  1336. }
  1337. if (pid == 0) {
  1338. err = test9 (getpid (), 0, 1);
  1339. sam_finalize ();
  1340. return (err);
  1341. }
  1342. waitpid (pid, &stat, 0);
  1343. old_pid = pid;
  1344. if (WEXITSTATUS (stat) == 0) {
  1345. pid = fork ();
  1346. if (pid == -1) {
  1347. fprintf (stderr, "Can't fork\n");
  1348. return 2;
  1349. }
  1350. if (pid == 0) {
  1351. err = test9 (old_pid, 0, 2);
  1352. sam_finalize ();
  1353. return (err);
  1354. }
  1355. waitpid (pid, &stat, 0);
  1356. }
  1357. fprintf (stderr, "test9 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : (WEXITSTATUS (stat) == 1 ? "skipped" : "failed")));
  1358. if (WEXITSTATUS (stat) == 1)
  1359. no_skipped++;
  1360. if (WEXITSTATUS (stat) > 1)
  1361. all_passed = 0;
  1362. if (all_passed)
  1363. fprintf (stderr, "All tests passed (%d skipped)\n", no_skipped);
  1364. return (all_passed ? 0 : 1);
  1365. }