sam_test_agent.c 30 KB

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