sam_test_agent.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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, "%s 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, "%s 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, "%s 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, "%s 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, "%s 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, "%s 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, "%s 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, "%s 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, "%s 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, "%s %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, "%s 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, "%s 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, "%s 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, "%s 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_log (LOG_INFO, "%s");
  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, "%s 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, "%s iid %d: sleep 1", instance_id);
  560. sleep (1);
  561. qb_log (LOG_INFO, "%s 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, "%s iid %d: wait for delivery of signal", instance_id);
  568. while (!test6_sig_delivered) {
  569. sleep (1);
  570. }
  571. qb_log (LOG_INFO, "%s iid %d: wait for real kill", instance_id);
  572. sleep (3);
  573. qb_log (LOG_INFO, "%s 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, "%s 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, "%s iid %d: sleep 1", instance_id);
  599. sleep (1);
  600. qb_log (LOG_INFO, "%s 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, "%s iid %d: wait for delivery of signal", instance_id);
  607. while (!test6_sig_delivered) {
  608. sleep (1);
  609. }
  610. qb_log (LOG_INFO, "%s 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. static void *test7_thread (void *arg)
  628. {
  629. /* Wait 5s */
  630. sleep (5);
  631. exit (0);
  632. }
  633. /*
  634. * Test quorum
  635. */
  636. static int test_quorum (void) {
  637. cmap_handle_t cmap_handle;
  638. cs_error_t err;
  639. unsigned int instance_id;
  640. pthread_t kill_thread;
  641. char *str;
  642. err = cmap_initialize (&cmap_handle);
  643. if (err != CS_OK) {
  644. qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err);
  645. return (1);
  646. }
  647. if (cmap_get_string(cmap_handle, "quorum.provider", &str) != CS_OK) {
  648. qb_log (LOG_INFO, "Could not get \"provider\" key: %d. Test skipped", err);
  649. return (1);
  650. }
  651. if (strcmp(str, "testquorum") != 0) {
  652. qb_log (LOG_INFO, "Provider is not testquorum. Test skipped");
  653. return (1);
  654. }
  655. free(str);
  656. /*
  657. * Set to not quorate
  658. */
  659. err = cmap_set_uint8(cmap_handle, "quorum.quorate", 0);
  660. if (err != CS_OK) {
  661. qb_log (LOG_INFO, "Can't set map key. Error %d", err);
  662. return (2);
  663. }
  664. qb_log (LOG_INFO, " initialize");
  665. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUORUM_RESTART);
  666. if (err != CS_OK) {
  667. qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err);
  668. return 2;
  669. }
  670. qb_log (LOG_INFO, " register");
  671. err = sam_register (&instance_id);
  672. if (err != CS_OK) {
  673. qb_log (LOG_ERR, "Can't register. Error %d", err);
  674. return 2;
  675. }
  676. if (instance_id == 1) {
  677. /*
  678. * Sam start should block forever, but 10s for us should be enough
  679. */
  680. pthread_create (&kill_thread, NULL, test7_thread, NULL);
  681. qb_log (LOG_INFO, "%s iid %d: start - should block forever (waiting 5s)", instance_id);
  682. err = sam_start ();
  683. if (err != CS_OK) {
  684. qb_log (LOG_ERR, "Can't start hc. Error %d", err);
  685. return 2;
  686. }
  687. qb_log (LOG_INFO, "%s iid %d: wasn't killed", instance_id);
  688. return (2);
  689. }
  690. if (instance_id == 2) {
  691. /*
  692. * Set to quorate
  693. */
  694. err = cmap_set_uint8(cmap_handle, "quorum.quorate", 1);
  695. if (err != CS_OK) {
  696. qb_log (LOG_INFO, "Can't set map key. Error %d", err);
  697. return (2);
  698. }
  699. qb_log (LOG_INFO, "%s iid %d: start", instance_id);
  700. err = sam_start ();
  701. if (err != CS_OK) {
  702. qb_log (LOG_ERR, "Can't start hc. Error %d", err);
  703. return 2;
  704. }
  705. /*
  706. * Set corosync unquorate
  707. */
  708. err = cmap_set_uint8(cmap_handle, "quorum.quorate", 0);
  709. if (err != CS_OK) {
  710. qb_log (LOG_INFO, "Can't set map key. Error %d", err);
  711. return (2);
  712. }
  713. qb_log (LOG_INFO, "%s iid %d: sleep 3", instance_id);
  714. sleep (3);
  715. qb_log (LOG_INFO, "%s iid %d: wasn't killed", instance_id);
  716. return (2);
  717. }
  718. if (instance_id == 3) {
  719. return (0);
  720. }
  721. return (2);
  722. }
  723. /*
  724. * Test cmap integration + quit policy
  725. */
  726. static int test8 (pid_t pid, pid_t old_pid, int test_n) {
  727. cmap_handle_t cmap_handle;
  728. cs_error_t err;
  729. uint64_t tstamp1, tstamp2;
  730. int32_t msec_diff;
  731. unsigned int instance_id;
  732. char key_name[CMAP_KEYNAME_MAXLEN];
  733. char *str;
  734. err = cmap_initialize (&cmap_handle);
  735. if (err != CS_OK) {
  736. qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err);
  737. return (1);
  738. }
  739. qb_log (LOG_INFO, "%s test %d", test_n);
  740. if (test_n == 2) {
  741. /*
  742. * Object should not exist
  743. */
  744. qb_log (LOG_INFO, "%s Testing if object exists (it shouldn't)");
  745. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  746. err = cmap_get_string(cmap_handle, key_name, &str);
  747. if (err == CS_OK) {
  748. qb_log (LOG_INFO, "Could find key \"%s\": %d.", key_name, err);
  749. free(str);
  750. return (2);
  751. }
  752. }
  753. if (test_n == 1 || test_n == 2) {
  754. qb_log (LOG_INFO, " initialize");
  755. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CMAP);
  756. if (err != CS_OK) {
  757. qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err);
  758. return 2;
  759. }
  760. qb_log (LOG_INFO, " register");
  761. err = sam_register (&instance_id);
  762. if (err != CS_OK) {
  763. qb_log (LOG_ERR, "Can't register. Error %d", err);
  764. return 2;
  765. }
  766. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid);
  767. err = cmap_get_string(cmap_handle, key_name, &str);
  768. if (err != CS_OK) {
  769. qb_log (LOG_INFO, "Could not get \"recovery\" key: %d.", err);
  770. return (2);
  771. }
  772. if (strcmp(str, "quit") != 0) {
  773. qb_log (LOG_INFO, "Recovery key \"%s\" is not \"quit\".", key_name);
  774. return (2);
  775. }
  776. free(str);
  777. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  778. err = cmap_get_string(cmap_handle, key_name, &str);
  779. if (err != CS_OK) {
  780. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  781. return (2);
  782. }
  783. if (strcmp(str, "stopped") != 0) {
  784. qb_log (LOG_INFO, "State key is not \"stopped\".");
  785. return (2);
  786. }
  787. free(str);
  788. qb_log (LOG_INFO, "%s iid %d: start", instance_id);
  789. err = sam_start ();
  790. if (err != CS_OK) {
  791. qb_log (LOG_ERR, "Can't start hc. Error %d", err);
  792. return 2;
  793. }
  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, "running") != 0) {
  800. qb_log (LOG_INFO, "State key is not \"running\".");
  801. return (2);
  802. }
  803. free(str);
  804. qb_log (LOG_INFO, "%s iid %d: stop", instance_id);
  805. err = sam_stop ();
  806. if (err != CS_OK) {
  807. qb_log (LOG_ERR, "Can't stop hc. Error %d", err);
  808. return 2;
  809. }
  810. err = cmap_get_string(cmap_handle, key_name, &str);
  811. if (err != CS_OK) {
  812. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  813. return (2);
  814. }
  815. if (strcmp(str, "stopped") != 0) {
  816. qb_log (LOG_INFO, "State key is not \"stopped\".");
  817. return (2);
  818. }
  819. free(str);
  820. qb_log (LOG_INFO, "%s iid %d: sleeping 5", instance_id);
  821. sleep (5);
  822. err = cmap_get_string(cmap_handle, key_name, &str);
  823. if (err != CS_OK) {
  824. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  825. return (2);
  826. }
  827. if (strcmp(str, "stopped") != 0) {
  828. qb_log (LOG_INFO, "State key is not \"stopped\".");
  829. return (2);
  830. }
  831. free(str);
  832. qb_log (LOG_INFO, "%s iid %d: start 2", instance_id);
  833. err = sam_start ();
  834. if (err != CS_OK) {
  835. qb_log (LOG_ERR, "Can't start hc. Error %d", err);
  836. return 2;
  837. }
  838. err = cmap_get_string(cmap_handle, key_name, &str);
  839. if (err != CS_OK) {
  840. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  841. return (2);
  842. }
  843. if (strcmp(str, "running") != 0) {
  844. qb_log (LOG_INFO, "State key is not \"running\".");
  845. return (2);
  846. }
  847. free(str);
  848. if (test_n == 2) {
  849. qb_log (LOG_INFO, "%s iid %d: sleeping 5. Should be killed", instance_id);
  850. sleep (5);
  851. return (2);
  852. } else {
  853. qb_log (LOG_INFO, "%s iid %d: Test HC", instance_id);
  854. err = sam_hc_send ();
  855. if (err != CS_OK) {
  856. qb_log (LOG_ERR, "Can't send hc. Error %d", err);
  857. return 2;
  858. }
  859. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.last_updated", pid);
  860. err = cmap_get_uint64(cmap_handle, key_name, &tstamp1);
  861. if (err != CS_OK) {
  862. qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err);
  863. return (2);
  864. }
  865. qb_log (LOG_INFO, "%s iid %d: Sleep 1", instance_id);
  866. sleep (1);
  867. err = sam_hc_send ();
  868. if (err != CS_OK) {
  869. qb_log (LOG_ERR, "Can't send hc. Error %d", err);
  870. return 2;
  871. }
  872. sleep (1);
  873. err = cmap_get_uint64(cmap_handle, key_name, &tstamp2);
  874. if (err != CS_OK) {
  875. qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err);
  876. return (2);
  877. }
  878. msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC;
  879. if (msec_diff < 500 || msec_diff > 2000) {
  880. qb_log (LOG_INFO, "Difference %d is not within <500, 2000> interval.", msec_diff);
  881. return (2);
  882. }
  883. qb_log (LOG_INFO, "%s iid %d: stop 2", instance_id);
  884. err = sam_stop ();
  885. if (err != CS_OK) {
  886. qb_log (LOG_ERR, "Can't stop hc. Error %d", err);
  887. return 2;
  888. }
  889. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  890. err = cmap_get_string(cmap_handle, key_name, &str);
  891. if (err != CS_OK) {
  892. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  893. return (2);
  894. }
  895. if (strcmp(str, "stopped") != 0) {
  896. qb_log (LOG_INFO, "State key is not \"stopped\".");
  897. return (2);
  898. }
  899. free(str);
  900. qb_log (LOG_INFO, "%s iid %d: exiting", instance_id);
  901. return (0);
  902. }
  903. }
  904. if (test_n == 3) {
  905. qb_log (LOG_INFO, "%s Testing if status is failed");
  906. /*
  907. * Previous should be FAILED
  908. */
  909. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  910. err = cmap_get_string(cmap_handle, key_name, &str);
  911. if (err != CS_OK) {
  912. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  913. return (2);
  914. }
  915. if (strcmp(str, "failed") != 0) {
  916. qb_log (LOG_INFO, "State key is not \"failed\".");
  917. return (2);
  918. }
  919. return (0);
  920. }
  921. return (2);
  922. }
  923. /*
  924. * Test cmap integration + restart policy
  925. */
  926. static int test9 (pid_t pid, pid_t old_pid, int test_n) {
  927. cs_error_t err;
  928. cmap_handle_t cmap_handle;
  929. unsigned int instance_id;
  930. char *str;
  931. char key_name[CMAP_KEYNAME_MAXLEN];
  932. err = cmap_initialize (&cmap_handle);
  933. if (err != CS_OK) {
  934. qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err);
  935. return (1);
  936. }
  937. qb_log (LOG_INFO, "%s test %d", test_n);
  938. if (test_n == 1) {
  939. qb_log (LOG_INFO, " initialize");
  940. err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | 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. qb_log (LOG_INFO, " iid %d", instance_id);
  952. if (instance_id < 3) {
  953. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid);
  954. err = cmap_get_string(cmap_handle, key_name, &str);
  955. if (err != CS_OK) {
  956. qb_log (LOG_INFO, "Could not get \"recovery\" key: %d.", err);
  957. return (2);
  958. }
  959. if (strcmp(str, "restart") != 0) {
  960. qb_log (LOG_INFO, "Recovery key \"%s\" is not \"restart\".", str);
  961. return (2);
  962. }
  963. free(str);
  964. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  965. err = cmap_get_string(cmap_handle, key_name, &str);
  966. if (err != CS_OK) {
  967. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  968. return (2);
  969. }
  970. if (strcmp(str, "stopped") != 0) {
  971. qb_log (LOG_INFO, "State key is not \"stopped\".");
  972. return (2);
  973. }
  974. free(str);
  975. qb_log (LOG_INFO, "%s iid %d: start", instance_id);
  976. err = sam_start ();
  977. if (err != CS_OK) {
  978. qb_log (LOG_ERR, "Can't start hc. Error %d", err);
  979. return 2;
  980. }
  981. err = cmap_get_string(cmap_handle, key_name, &str);
  982. if (err != CS_OK) {
  983. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  984. return (2);
  985. }
  986. if (strcmp(str, "running") != 0) {
  987. qb_log (LOG_INFO, "State key is not \"running\".");
  988. return (2);
  989. }
  990. free(str);
  991. qb_log (LOG_INFO, "%s iid %d: waiting for kill", instance_id);
  992. sleep (10);
  993. return (2);
  994. }
  995. if (instance_id == 3) {
  996. qb_log (LOG_INFO, "%s iid %d: mark failed", instance_id);
  997. if (err != CS_OK) {
  998. qb_log (LOG_ERR, "Can't start hc. Error %d", err);
  999. return 2;
  1000. }
  1001. err = sam_mark_failed ();
  1002. if (err != CS_OK) {
  1003. qb_log (LOG_ERR, "Can't mark failed. Error %d", err);
  1004. return 2;
  1005. }
  1006. sleep (10);
  1007. return (2);
  1008. }
  1009. return (2);
  1010. }
  1011. if (test_n == 2) {
  1012. qb_log (LOG_INFO, "%s Testing if status is failed");
  1013. /*
  1014. * Previous should be FAILED
  1015. */
  1016. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid);
  1017. err = cmap_get_string(cmap_handle, key_name, &str);
  1018. if (err != CS_OK) {
  1019. qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err);
  1020. return (2);
  1021. }
  1022. if (strcmp(str, "failed") != 0) {
  1023. qb_log (LOG_INFO, "State key is not \"failed\".");
  1024. return (2);
  1025. }
  1026. free(str);
  1027. return (0);
  1028. }
  1029. return (2);
  1030. }
  1031. static int hc_allways_respond_cb(void)
  1032. {
  1033. qb_log (LOG_INFO, "%s() -> health check OK.");
  1034. return 0;
  1035. }
  1036. static int setup_hc (void)
  1037. {
  1038. cs_error_t err;
  1039. unsigned int instance_id;
  1040. err = sam_initialize (1000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CMAP);
  1041. if (err != CS_OK) {
  1042. qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err);
  1043. return 2;
  1044. }
  1045. qb_log (LOG_INFO, " register");
  1046. err = sam_register (&instance_id);
  1047. if (err != CS_OK) {
  1048. qb_log (LOG_ERR, "Can't register. Error %d", err);
  1049. return 2;
  1050. }
  1051. err = sam_hc_callback_register (hc_allways_respond_cb);
  1052. qb_log (LOG_INFO, "%s instance id %d: start", instance_id);
  1053. err = sam_start ();
  1054. if (err != CS_OK) {
  1055. qb_log (LOG_ERR, "Can't start hc. Error %d", err);
  1056. return 2;
  1057. }
  1058. return (0);
  1059. }
  1060. static int do_test_command(int sock, char* func, char*args[], int num_args)
  1061. {
  1062. int err = 0;
  1063. pid_t pid;
  1064. int stat;
  1065. pid = fork ();
  1066. if (pid == -1) {
  1067. qb_log (LOG_ERR, "Can't fork");
  1068. return -1;
  1069. }
  1070. if (pid == 0) {
  1071. if (strcmp ("test1", func) == 0) {
  1072. err = test1 ();
  1073. } else if (strcmp ("test2", func) == 0) {
  1074. err = test2 ();
  1075. } else if (strcmp ("test3", func) == 0) {
  1076. err = test3 ();
  1077. } else if (strcmp ("test4", func) == 0) {
  1078. err = test4 ();
  1079. } else if (strcmp ("test5", func) == 0) {
  1080. err = test5 ();
  1081. } else if (strcmp ("test6", func) == 0) {
  1082. err = test6 ();
  1083. } else if (strcmp ("test_quorum", func) == 0) {
  1084. err = test_quorum ();
  1085. } else if (strcmp ("test8", func) == 0) {
  1086. err = test8 (getpid(), 0, 1);
  1087. } else if (strcmp ("test9", func) == 0) {
  1088. err = test9 (getpid(), 0, 1);
  1089. }
  1090. sam_finalize ();
  1091. exit(err);
  1092. }
  1093. if (pid > 0) {
  1094. waitpid (pid, &stat, 0);
  1095. return WEXITSTATUS (stat);
  1096. }
  1097. return -1;
  1098. }
  1099. static void do_command (int sock, char* func, char*args[], int num_args)
  1100. {
  1101. char response[100];
  1102. int err = 0;
  1103. if (parse_debug) {
  1104. qb_log (LOG_INFO, "RPC:%s() called.", func);
  1105. }
  1106. if (strncmp ("test", func, 4) == 0) {
  1107. err = do_test_command(sock, func, args, num_args);
  1108. } else if (strcmp ("setup_hc", func) == 0) {
  1109. err = setup_hc ();
  1110. } else if (strcmp ("sam_stop", func) == 0) {
  1111. err = sam_stop ();
  1112. if (err != CS_OK) {
  1113. err = -1;
  1114. qb_log (LOG_ERR,"%s RPC:%s sam_stop failed!", __func__, func);
  1115. snprintf (response, 100, "%s", FAIL_STR);
  1116. }
  1117. err = sam_finalize();
  1118. } else {
  1119. err = -1;
  1120. qb_log (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  1121. snprintf (response, 100, "%s", NOT_SUPPORTED_STR);
  1122. }
  1123. if (err == 0) {
  1124. snprintf (response, 100, "%s", OK_STR);
  1125. } else if (err == 1) {
  1126. snprintf (response, 100, "%s", FAIL_STR);
  1127. qb_log (LOG_ERR, "%s() test skipped?! (%d).", func, err);
  1128. } else {
  1129. snprintf (response, 100, "%s", FAIL_STR);
  1130. qb_log (LOG_ERR, "%s() failed (%d).", func, err);
  1131. }
  1132. send (sock, response, strlen (response) + 1, 0);
  1133. }
  1134. int
  1135. main (int argc, char *argv[])
  1136. {
  1137. parse_debug = 1;
  1138. return test_agent_run ("sam_test_agent", 9036, do_command, NULL);
  1139. }