static void *hc_callback_thread (void *unused_param) { int poll_error; int status; ssize_t bytes_readed; char command; int time_interval, tmp_time_interval; int counter; struct pollfd pfds; status = 0; counter = 0; time_interval = sam_internal_data.time_interval >> 2; while (1) { pfds.fd = sam_internal_data.cb_rpipe_fd; pfds.events = POLLIN; pfds.revents = 0; if (status == 1) { tmp_time_interval = time_interval; } else { tmp_time_interval = -1; } poll_error = poll (&pfds, 1, tmp_time_interval); if (poll_error == 0) { sam_hc_send (); counter++; if (counter >= 4) { if (sam_internal_data.hc_callback () != 0) { status = 3; } counter = 0; } } if (poll_error > 0) { bytes_readed = read (sam_internal_data.cb_rpipe_fd, &command, 1); if (bytes_readed > 0) { if (status == 0 && command == SAM_COMMAND_START) status = 1; if (status == 1 && command == SAM_COMMAND_STOP) status = 0; } } } /* * This makes compiler happy, it's same as return (NULL); */ return (unused_param); }
/* * This tests recovery policy quit and callback. */ static int test2 (void) { cs_error_t error; unsigned int instance_id; qb_enter(); error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT); 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) { signal (SIGTERM, test2_signal); 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 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; } qb_log (LOG_INFO, "iid %d: wait for delivery of signal", instance_id); while (!test2_sig_delivered) { sleep (1); } qb_log (LOG_INFO, "iid %d: wait for real kill", instance_id); sleep (3); } return 1; }
/* * This tests recovery policy quit and callback. */ static int test2 (void) { cs_error_t error; unsigned int instance_id; printf ("%s: initialize\n", __FUNCTION__); error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT); 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) { signal (SIGTERM, test2_signal); 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 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; } printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id); while (!test2_sig_delivered) { sleep (1); } printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id); sleep (3); } 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; }
/* * Test cmap integration + quit policy */ static int test8 (pid_t pid, pid_t old_pid, int test_n) { cmap_handle_t cmap_handle; cs_error_t err; uint64_t tstamp1, tstamp2; int32_t msec_diff; unsigned int instance_id; char key_name[CMAP_KEYNAME_MAXLEN]; char *str; err = cmap_initialize (&cmap_handle); if (err != CS_OK) { qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err); return (1); } qb_log (LOG_INFO, "test %d", test_n); if (test_n == 2) { /* * Object should not exist */ qb_log (LOG_INFO, "Testing if object exists (it shouldn't)"); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err == CS_OK) { qb_log (LOG_INFO, "Could find key \"%s\": %d.", key_name, err); free(str); return (2); } } if (test_n == 1 || test_n == 2) { qb_log (LOG_INFO, " initialize"); err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CMAP); if (err != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err); return 2; } qb_log (LOG_INFO, " register"); err = sam_register (&instance_id); if (err != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"recovery\" key: %d.", err); return (2); } if (strcmp(str, "quit") != 0) { qb_log (LOG_INFO, "Recovery key \"%s\" is not \"quit\".", key_name); return (2); } free(str); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: stop", instance_id); err = sam_stop (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: sleeping 5", instance_id); sleep (5); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start 2", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); if (test_n == 2) { qb_log (LOG_INFO, "iid %d: sleeping 5. Should be killed", instance_id); sleep (5); return (2); } else { qb_log (LOG_INFO, "iid %d: Test HC", instance_id); err = sam_hc_send (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.last_updated", pid); err = cmap_get_uint64(cmap_handle, key_name, &tstamp1); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err); return (2); } qb_log (LOG_INFO, "iid %d: Sleep 1", instance_id); sleep (1); err = sam_hc_send (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", err); return 2; } sleep (1); err = cmap_get_uint64(cmap_handle, key_name, &tstamp2); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err); return (2); } msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC; if (msec_diff < 500 || msec_diff > 2000) { qb_log (LOG_INFO, "Difference %d is not within <500, 2000> interval.", msec_diff); return (2); } qb_log (LOG_INFO, "iid %d: stop 2", instance_id); err = sam_stop (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: exiting", instance_id); return (0); } } if (test_n == 3) { qb_log (LOG_INFO, "Testing if status is failed"); /* * Previous should be FAILED */ snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "failed") != 0) { qb_log (LOG_INFO, "State key is not \"failed\"."); return (2); } return (0); } return (2); }
/* * Test warn signal set. */ static int test6 (void) { cs_error_t error; unsigned int instance_id; int test6_sig_del; qb_log (LOG_INFO, " initialize"); 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) { error = sam_warn_signal_set (SIGUSR1); if (error != CS_OK) { qb_log (LOG_ERR, "Can't set warn signal. Error %d", error); return 1; } signal (SIGUSR1, test6_signal); 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 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; } qb_log (LOG_INFO, "iid %d: wait for delivery of signal", instance_id); while (!test6_sig_delivered) { sleep (1); } qb_log (LOG_INFO, "iid %d: wait for real kill", instance_id); sleep (3); qb_log (LOG_INFO, "iid %d: wasn't killed", instance_id); return (1); } if (instance_id == 2) { error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del)); if (error != CS_OK) { qb_log (LOG_ERR, "Can't restore data. Error %d", error); return 1; } if (test6_sig_del != 1) { qb_log (LOG_ERR, "Previous test failed. Signal was not delivered"); return 1; } error = sam_warn_signal_set (SIGKILL); if (error != CS_OK) { qb_log (LOG_ERR, "Can't set warn signal. Error %d", error); return 1; } signal (SIGUSR1, test6_signal); 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 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; } qb_log (LOG_INFO, "iid %d: wait for delivery of signal", instance_id); while (!test6_sig_delivered) { sleep (1); } qb_log (LOG_INFO, "iid %d: wasn't killed", instance_id); return (1); } if (instance_id == 3) { error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del)); if (error != CS_OK) { qb_log (LOG_ERR, "Can't restore data. Error %d", error); return 1; } if (test6_sig_del != 1) { qb_log (LOG_ERR, "Previous test failed. Signal WAS delivered"); return 1; } return (0); } return 1; }
/* * Test warn signal set. */ static int sam_test6 (void) { cs_error_t error; unsigned int instance_id; int test6_sig_del; setlinebuf(stdout); 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) { error = sam_warn_signal_set (SIGUSR1); if (error != CS_OK) { fprintf (stderr, "Can't set warn signal. Error %d\n", error); return 1; } signal (SIGUSR1, test6_signal); 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 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; } printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id); while (!test6_sig_delivered) { sleep (1); } printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id); sleep (3); printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id); return (1); } if (instance_id == 2) { error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del)); if (error != CS_OK) { fprintf (stderr, "Can't restore data. Error %d\n", error); return 1; } if (test6_sig_del != 1) { fprintf (stderr, "Previous test failed. Signal was not delivered\n"); return 1; } error = sam_warn_signal_set (SIGKILL); if (error != CS_OK) { fprintf (stderr, "Can't set warn signal. Error %d\n", error); return 1; } signal (SIGUSR1, test6_signal); 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 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; } printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id); while (!test6_sig_delivered) { sleep (1); } printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id); return (1); } if (instance_id == 3) { error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del)); if (error != CS_OK) { fprintf (stderr, "Can't restore data. Error %d\n", error); return 1; } if (test6_sig_del != 1) { fprintf (stderr, "Previous test failed. Signal WAS delivered\n"); return 1; } return (0); } return 1; }
/* * Test confdb integration + quit policy */ static int test8 (pid_t pid, pid_t old_pid, int test_n) { confdb_handle_t cdb_handle; cs_error_t err; hdb_handle_t res_handle, proc_handle, pid_handle; size_t value_len; uint64_t tstamp1, tstamp2; int32_t msec_diff; char key_value[256]; unsigned int instance_id; char tmp_obj[PATH_MAX]; confdb_value_types_t cdbtype; err = confdb_initialize (&cdb_handle, NULL); if (err != CS_OK) { printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err); return (1); } printf ("%s test %d\n", __FUNCTION__, test_n); if (test_n == 2) { /* * Object should not exist */ printf ("%s Testing if object exists (it shouldn't)\n", __FUNCTION__); err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err == CS_OK) { printf ("Could find object \"%s\": %d.\n", tmp_obj, err); return (2); } } if (test_n == 1 || test_n == 2) { printf ("%s: initialize\n", __FUNCTION__); err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CONFDB); if (err != CS_OK) { fprintf (stderr, "Can't initialize SAM API. Error %d\n", err); return 2; } printf ("%s: register\n", __FUNCTION__); err = sam_register (&instance_id); if (err != CS_OK) { fprintf (stderr, "Can't register. Error %d\n", err); return 2; } err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err != CS_OK) { printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "recovery", strlen("recovery"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"recovery\" key: %d.\n", err); return (2); } if (value_len != strlen ("quit") || memcmp (key_value, "quit", value_len) != 0) { printf ("Recovery key \"%s\" is not \"watchdog\".\n", key_value); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: start\n", __FUNCTION__, instance_id); err = sam_start (); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) { printf ("State key is not \"running\".\n"); return (2); } printf ("%s iid %d: stop\n", __FUNCTION__, instance_id); err = sam_stop (); if (err != CS_OK) { fprintf (stderr, "Can't stop hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: sleeping 5\n", __FUNCTION__, instance_id); sleep (5); err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: start 2\n", __FUNCTION__, instance_id); err = sam_start (); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) { printf ("State key is not \"running\".\n"); return (2); } if (test_n == 2) { printf ("%s iid %d: sleeping 5. Should be killed\n", __FUNCTION__, instance_id); sleep (5); return (2); } else { printf ("%s iid %d: Test HC\n", __FUNCTION__, instance_id); err = sam_hc_send (); if (err != CS_OK) { fprintf (stderr, "Can't send hc. Error %d\n", err); return 2; } err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp1, &value_len, &cdbtype); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } printf ("%s iid %d: Sleep 1\n", __FUNCTION__, instance_id); sleep (1); err = sam_hc_send (); if (err != CS_OK) { fprintf (stderr, "Can't send hc. Error %d\n", err); return 2; } sleep (1); err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp2, &value_len, &cdbtype); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC; if (msec_diff < 500 || msec_diff > 2000) { printf ("Difference %d is not within <500, 2000> interval.\n", msec_diff); return (2); } printf ("%s iid %d: stop 2\n", __FUNCTION__, instance_id); err = sam_stop (); if (err != CS_OK) { fprintf (stderr, "Can't stop hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: exiting\n", __FUNCTION__, instance_id); return (0); } } if (test_n == 3) { printf ("%s Testing if status is failed\n", __FUNCTION__); /* * Previous should be FAILED */ err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err != CS_OK) { printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("failed") || memcmp (key_value, "failed", value_len) != 0) { printf ("State key is not \"failed\".\n"); return (2); } return (0); } return (2); }
/* * 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; }