static void do_command (int sock, char* func, char*args[], int num_args) { char response[100]; int err = 0; qb_log (LOG_INFO, "RPC:%s() called.", func); if (strncmp ("test", func, 4) == 0) { err = do_test_command(sock, func, args, num_args); } else if (strcmp ("setup_hc", func) == 0) { err = setup_hc (); } else if (strcmp ("sam_stop", func) == 0) { err = sam_stop (); if (err != CS_OK) { err = -1; qb_log (LOG_ERR,"RPC:%s sam_stop failed!", func); snprintf (response, 100, "%s", FAIL_STR); } err = sam_finalize(); } else { err = -1; qb_log (LOG_ERR,"RPC:%s not supported!", func); snprintf (response, 100, "%s", NOT_SUPPORTED_STR); } if (err == 0) { snprintf (response, 100, "%s", OK_STR); } else if (err == 1) { snprintf (response, 100, "%s", FAIL_STR); qb_log (LOG_ERR, "%s() test skipped?! (%d).", func, err); } else { snprintf (response, 100, "%s", FAIL_STR); qb_log (LOG_ERR, "%s() failed (%d).", func, err); } send (sock, response, strlen (response) + 1, 0); }
static int do_test_command(int sock, char* func, char*args[], int num_args) { int err = 0; pid_t pid; int stat; pid = fork (); if (pid == -1) { qb_log (LOG_ERR, "Can't fork"); return -1; } if (pid == 0) { if (strcmp ("test1", func) == 0) { err = test1 (); } else if (strcmp ("test2", func) == 0) { err = test2 (); } else if (strcmp ("test3", func) == 0) { err = test3 (); } else if (strcmp ("test4", func) == 0) { err = test4 (); } else if (strcmp ("test5", func) == 0) { err = test5 (); } else if (strcmp ("test6", func) == 0) { err = test6 (); } else if (strcmp ("test8", func) == 0) { err = test8 (getpid(), 0, 1); } else if (strcmp ("test9", func) == 0) { err = test9 (getpid(), 0, 1); } sam_finalize (); exit(err); } if (pid > 0) { waitpid (pid, &stat, 0); return WEXITSTATUS (stat); } return -1; }
/* * First test will just register SAM, with policy restart. First instance will * sleep one second, send hc and sleep another 3 seconds. This should force restart. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds. * Then start hc again and sleep 3 seconds. This should force restart again. * Last instance just calls initialize again. This should end with error. * Then call start, followed by stop and start again. Finally, we will call finalize * twice. One should succeed, second should fail. After this, we will call every function * (none should succeed). */ static int test1 (void) { cs_error_t error; unsigned int instance_id; int i; qb_enter(); error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART); if (error != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", error); return 1; } qb_log (LOG_INFO, "register"); error = sam_register (&instance_id); if (error != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", error); return 1; } if (instance_id == 1 || instance_id == 2) { qb_log (LOG_INFO, "iid %d: start", instance_id); error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } for (i = 0; i < 10; i++) { qb_log (LOG_INFO, "iid %d: sleep 1", instance_id); sleep (1); qb_log (LOG_INFO, "iid %d: hc send", instance_id); error = sam_hc_send (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", error); return 1; } } if (instance_id == 2) { qb_log (LOG_INFO, "iid %d: stop", instance_id); error = sam_stop (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", error); return 1; } } qb_log (LOG_INFO, "iid %d: sleep 3", instance_id); sleep (3); qb_log (LOG_INFO, "iid %d: start", instance_id); error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } qb_log (LOG_INFO, "iid %d: sleep 3", instance_id); sleep (3); return 0; } if (instance_id == 3) { error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART); if (error == CS_OK) { qb_log (LOG_ERR, "Can initialize SAM API after initialization"); return 1; } error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } error = sam_stop (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", error); return 1; } error = sam_finalize (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't finalize sam. Error %d", error); return 1; } error = sam_finalize (); if (error == CS_OK) { qb_log (LOG_ERR, "Can finalize sam after finalization!"); return 1; } if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK || sam_start () == CS_OK || sam_stop () == CS_OK || sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK || sam_hc_callback_register (NULL) == CS_OK) { qb_log (LOG_ERR, "Can call one of function after finalization!"); return 1; } return 0; } return 1; }
/* * First test will just register SAM, with policy restart. First instance will * sleep one second, send hc and sleep another 3 seconds. This should force restart. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds. * Then start hc again and sleep 3 seconds. This should force restart again. * Last instance just calls initialize again. This should end with error. * Then call start, followed by stop and start again. Finally, we will call finalize * twice. One should succeed, second should fail. After this, we will call every function * (none should succeed). */ static int test1 (void) { cs_error_t error; unsigned int instance_id; int i; printf ("%s: initialize\n", __FUNCTION__); error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART); if (error != CS_OK) { fprintf (stderr, "Can't initialize SAM API. Error %d\n", error); return 1; } printf ("%s: register\n", __FUNCTION__); error = sam_register (&instance_id); if (error != CS_OK) { fprintf (stderr, "Can't register. Error %d\n", error); return 1; } if (instance_id == 1 || instance_id == 2) { printf ("%s iid %d: start\n", __FUNCTION__, instance_id); error = sam_start (); if (error != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", error); return 1; } for (i = 0; i < 10; i++) { printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id); sleep (1); printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id); error = sam_hc_send (); if (error != CS_OK) { fprintf (stderr, "Can't send hc. Error %d\n", error); return 1; } } if (instance_id == 2) { printf ("%s iid %d: stop\n", __FUNCTION__, instance_id); error = sam_stop (); if (error != CS_OK) { fprintf (stderr, "Can't send hc. Error %d\n", error); return 1; } } printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id); sleep (3); printf ("%s iid %d: start\n", __FUNCTION__, instance_id); error = sam_start (); if (error != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", error); return 1; } printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id); sleep (3); return 0; } if (instance_id == 3) { error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART); if (error == CS_OK) { fprintf (stderr, "Can initialize SAM API after initialization"); return 1; } error = sam_start (); if (error != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", error); return 1; } error = sam_stop (); if (error != CS_OK) { fprintf (stderr, "Can't stop hc. Error %d\n", error); return 1; } error = sam_finalize (); if (error != CS_OK) { fprintf (stderr, "Can't finalize sam. Error %d\n", error); return 1; } error = sam_finalize (); if (error == CS_OK) { fprintf (stderr, "Can finalize sam after finalization!\n"); return 1; } if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK || sam_start () == CS_OK || sam_stop () == CS_OK || sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK || sam_hc_callback_register (NULL) == CS_OK) { fprintf (stderr, "Can call one of function after finalization!\n"); return 1; } return 0; } return 1; }
int main(int argc, char *argv[]) { pid_t pid, old_pid; int err; int stat; int all_passed = 1; int no_skipped = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 1; } if (pid == 0) { err = test1 (); sam_finalize (); return err; } waitpid (pid, &stat, 0); fprintf (stderr, "test1 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed")); if (WEXITSTATUS (stat) != 0) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 1; } if (pid == 0) { err = test2 (); sam_finalize (); return (err); } waitpid (pid, &stat, 0); fprintf (stderr, "test2 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed")); if (WEXITSTATUS (stat) != 0) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 1; } if (pid == 0) { err = test3 (); sam_finalize (); return (err); } waitpid (pid, &stat, 0); fprintf (stderr, "test3 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed")); if (WEXITSTATUS (stat) != 0) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 1; } if (pid == 0) { err = test4 (); sam_finalize (); return (err); } waitpid (pid, &stat, 0); fprintf (stderr, "test4 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed")); if (WEXITSTATUS (stat) != 0) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 1; } if (pid == 0) { err = test5 (); sam_finalize (); return (err); } waitpid (pid, &stat, 0); fprintf (stderr, "test5 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed")); if (WEXITSTATUS (stat) != 0) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 1; } if (pid == 0) { err = test6 (); sam_finalize (); return (err); } waitpid (pid, &stat, 0); fprintf (stderr, "test6 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed")); if (WEXITSTATUS (stat) != 0) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 2; } if (pid == 0) { err = test7 (); sam_finalize (); return (err); } waitpid (pid, &stat, 0); fprintf (stderr, "test7 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : (WEXITSTATUS (stat) == 1 ? "skipped" : "failed"))); if (WEXITSTATUS (stat) == 1) no_skipped++; if (WEXITSTATUS (stat) > 1) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 2; } if (pid == 0) { err = test8 (getpid (), 0, 1); sam_finalize (); return (err); } waitpid (pid, &stat, 0); old_pid = pid; if (WEXITSTATUS (stat) == 0) { pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 2; } if (pid == 0) { err = test8 (getpid (), old_pid, 2); sam_finalize (); return (err); } waitpid (pid, &stat, 0); old_pid = pid; if (WEXITSTATUS (stat) == 0) { pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 2; } if (pid == 0) { err = test8 (old_pid, 0, 3); sam_finalize (); return (err); } waitpid (pid, &stat, 0); } } if (WEXITSTATUS (stat) == 1) no_skipped++; if (WEXITSTATUS (stat) > 1) all_passed = 0; pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 2; } if (pid == 0) { err = test9 (getpid (), 0, 1); sam_finalize (); return (err); } waitpid (pid, &stat, 0); old_pid = pid; if (WEXITSTATUS (stat) == 0) { pid = fork (); if (pid == -1) { fprintf (stderr, "Can't fork\n"); return 2; } if (pid == 0) { err = test9 (old_pid, 0, 2); sam_finalize (); return (err); } waitpid (pid, &stat, 0); } fprintf (stderr, "test9 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : (WEXITSTATUS (stat) == 1 ? "skipped" : "failed"))); if (WEXITSTATUS (stat) == 1) no_skipped++; if (WEXITSTATUS (stat) > 1) all_passed = 0; if (all_passed) fprintf (stderr, "All tests passed (%d skipped)\n", no_skipped); return (all_passed ? 0 : 1); }