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