index e5383b43c60599c5d5ff8d9316f510081a7802ad..fb2c24bf20d596450c36f67e5b7791a96c2b5122 100644 (file)
--- a/t/unit/core/store_test.c
+++ b/t/unit/core/store_test.c
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"], "
+ "\"metrics\": [], "
"\"services\": []},"
"{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": [], "
+ "\"metrics\": [], "
"\"services\": ["
"{\"name\": \"s1\", "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"{\"name\": \"k3\", \"value\": \"v3\", "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
- "]},"
+ "], "
+ "\"metrics\": []},"
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": [], "
+ "\"metrics\": []}"
+ "]}" },
+ { { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_METRICS,
+ "{\"hosts\":["
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": ["
+ "{\"name\": \"k1\", \"value\": \"v1\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []},"
+ "{\"name\": \"k2\", \"value\": \"v2\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []},"
+ "{\"name\": \"k3\", \"value\": \"v3\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []}"
+ "], "
+ "\"services\": []},"
"{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
- "\"attributes\": []}"
+ "\"attributes\": [], "
+ "\"services\": ["
+ "{\"name\": \"s1\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": []},"
+ "{\"name\": \"s2\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": ["
+ "{\"name\": \"k1\", \"value\": 123, "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []},"
+ "{\"name\": \"k2\", \"value\": 4711, "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []}"
+ "]}"
+ "]}"
"]}" },
{ { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_ATTRIBUTES,
"{\"hosts\":["
"{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
+ "\"metrics\": [], "
"\"services\": []},"
"{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
+ "\"metrics\": [], "
"\"services\": ["
"{\"name\": \"s1\", "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}" },
- { { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES,
+ { { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_ALL,
"{\"hosts\":["
"{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": [], "
+ "\"metrics\": [], "
"\"services\": ["
"{\"name\": \"s2\", "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"], "
+ "\"metrics\": [], "
"\"services\": []}"
"]}" },
{ { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
"{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": [], "
+ "\"metrics\": [], "
"\"services\": []}"
"]}" },
};
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
sdb_store_matcher_t *filter = NULL;
+ char filter_str[1024];
int status;
sdb_strbuf_clear(buf);
"INTERNAL ERROR: sdb_store_*_matcher() = NULL");
}
+ if (sdb_store_matcher_tostring(filter, filter_str, sizeof(filter_str)))
+ snprintf(filter_str, sizeof(filter_str), "ERR");
+
status = sdb_store_tojson(buf, filter, golden_data[i].flags);
fail_unless(status == 0,
- "sdb_store_tojson(%x) = %d; expected: 0",
- golden_data[i].flags, status);
+ "sdb_store_tojson(<buf>, %s, %x) = %d; expected: 0",
+ filter_str, golden_data[i].flags, status);
verify_json_output(buf, golden_data[i].expected, golden_data[i].flags);
sdb_object_deref(SDB_OBJ(filter));
}
END_TEST
+START_TEST(test_get_field)
+{
+ sdb_store_obj_t *host;
+ sdb_data_t value = SDB_DATA_INIT;
+ int check;
+
+ sdb_store_host("host", 10);
+ sdb_store_host("host", 20);
+
+ host = sdb_store_get_host("host");
+ fail_unless(host != NULL,
+ "INTERNAL ERROR: store doesn't have host after adding it");
+
+ check = sdb_store_get_field(NULL, 0, NULL);
+ fail_unless(check < 0,
+ "sdb_store_get_field(NULL, 0, NULL) = %d; expected: <0");
+ check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL);
+ fail_unless(check < 0,
+ "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+ "expected: <0");
+ check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
+ fail_unless(check < 0,
+ "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+ "expected: <0");
+ check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, &value);
+ fail_unless(check < 0,
+ "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, <value>) = %d; "
+ "expected: <0");
+
+ check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, &value);
+ fail_unless(check == 0,
+ "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, <value>) = "
+ "%d; expected: 0");
+ fail_unless((value.type == SDB_TYPE_DATETIME)
+ && (value.data.datetime == 20),
+ "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, <value>) "
+ "returned value {%d, %lu}; expected {%d, 20}",
+ value.type, value.data.datetime, SDB_TYPE_DATETIME);
+
+ check = sdb_store_get_field(host, SDB_FIELD_AGE, &value);
+ fail_unless(check == 0,
+ "sdb_store_get_field(<host>, SDB_FIELD_AGE, <value>) = "
+ "%d; expected: 0");
+ /* let's assume we're at least in year 1980 ;-) */
+ fail_unless((value.type == SDB_TYPE_DATETIME)
+ && (value.data.datetime > 10L * SDB_INTERVAL_YEAR),
+ "sdb_store_get_field(<host>, SDB_FIELD_AGE, <value>) "
+ "returned value {%d, %lu}; expected {%d, >%lu}",
+ value.type, value.data.datetime,
+ SDB_TYPE_DATETIME, 10L * SDB_INTERVAL_YEAR);
+
+ check = sdb_store_get_field(host, SDB_FIELD_INTERVAL, &value);
+ fail_unless(check == 0,
+ "sdb_store_get_field(<host>, SDB_FIELD_INTERVAL, <value>) = "
+ "%d; expected: 0");
+ fail_unless((value.type == SDB_TYPE_DATETIME)
+ && (value.data.datetime == 10),
+ "sdb_store_get_field(<host>, SDB_FIELD_INTERVAL, <value>) "
+ "returned value {%d, %lu}; expected {%d, 10}",
+ value.type, value.data.datetime, SDB_TYPE_DATETIME);
+}
+END_TEST
+
START_TEST(test_interval)
{
sdb_store_obj_t *host;
fail_unless(host->interval == 10,
"sdb_store_host() did not calculate interval correctly: "
- "got: %"PRIscTIME"; expected: %"PRIscTIME, host->interval, 10);
+ "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 10);
/* multiple updates for the same timestamp don't modify the interval */
sdb_store_host("host", 40);
fail_unless(host->interval == 10,
"sdb_store_host() changed interval when doing multiple updates "
- "using the same timestamp; got: %"PRIscTIME"; "
- "expected: %"PRIscTIME, host->interval, 10);
+ "using the same timestamp; got: %"PRIsdbTIME"; "
+ "expected: %"PRIsdbTIME, host->interval, 10);
/* multiple updates using an timestamp don't modify the interval */
sdb_store_host("host", 20);
fail_unless(host->interval == 10,
"sdb_store_host() changed interval when doing multiple updates "
- "using an old timestamp; got: %"PRIscTIME"; expected: %"PRIscTIME,
+ "using an old timestamp; got: %"PRIsdbTIME"; expected: %"PRIsdbTIME,
host->interval, 10);
/* new interval: 20 us */
sdb_store_host("host", 60);
fail_unless(host->interval == 11,
"sdb_store_host() did not calculate interval correctly: "
- "got: %"PRIscTIME"; expected: %"PRIscTIME, host->interval, 11);
+ "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
/* new interval: 40 us */
sdb_store_host("host", 100);
fail_unless(host->interval == 13,
"sdb_store_host() did not calculate interval correctly: "
- "got: %"PRIscTIME"; expected: %"PRIscTIME, host->interval, 11);
+ "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
sdb_object_deref(SDB_OBJ(host));
}
tcase_add_test(tc, test_store_attr);
tcase_add_test(tc, test_store_service);
tcase_add_test(tc, test_store_service_attr);
+ tcase_add_test(tc, test_get_field);
tcase_add_test(tc, test_interval);
tcase_add_test(tc, test_iterate);
tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear);