testsam.c 42 KB

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