index defa6fcfc45d9aaeb021e3ddffdfe9cadd2dc147..e8c77c7b04dc0c649fdc7cb0a00985f2fd760354 100644 (file)
--- a/t/unit/core/store_test.c
+++ b/t/unit/core/store_test.c
sdb_store_attribute("h1", "k2", &datum, 1);
sdb_store_attribute("h1", "k3", &datum, 2);
sdb_store_attribute("h1", "k2", &datum, 1);
sdb_store_attribute("h1", "k3", &datum, 2);
+ sdb_store_metric("h1", "m1", /* store */ NULL, 2);
+ sdb_store_metric("h1", "m2", /* store */ NULL, 1);
+
sdb_store_service("h2", "s1", 1);
sdb_store_service("h2", "s2", 2);
datum.type = SDB_TYPE_INTEGER;
sdb_store_service("h2", "s1", 1);
sdb_store_service("h2", "s2", 2);
datum.type = SDB_TYPE_INTEGER;
+ datum.data.integer = 42;
+ sdb_store_metric_attr("h1", "m1", "k3", &datum, 2);
+
datum.data.integer = 123;
sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
datum.data.integer = 4711;
datum.data.integer = 123;
sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
datum.data.integer = 4711;
}
END_TEST
}
END_TEST
+START_TEST(test_store_metric)
+{
+ sdb_metric_store_t store1 = { "dummy-type1", "dummy-id1" };
+ sdb_metric_store_t store2 = { "dummy-type2", "dummy-id2" };
+
+ struct {
+ const char *host;
+ const char *metric;
+ sdb_metric_store_t *store;
+ sdb_time_t last_update;
+ int expected;
+ } golden_data[] = {
+ { "k", "m", NULL, 1, -1 },
+ { "k", "m", NULL, 1, -1 }, /* retry to ensure the host is not created */
+ { "k", "m", &store1, 1, -1 },
+ { "l", "m1", NULL, 1, 0 },
+ { "l", "m1", &store1, 2, 0 },
+ { "l", "m1", &store1, 3, 0 },
+ { "l", "m1", NULL, 3, 1 },
+ { "l", "m2", &store1, 1, 0 },
+ { "l", "m2", &store2, 2, 0 },
+ { "l", "m2", NULL, 3, 0 },
+ { "m", "m", &store1, 1, 0 },
+ { "m", "m", NULL, 2, 0 },
+ { "m", "m", NULL, 2, 1 },
+ { "m", "m", &store1, 3, 0 },
+ { "m", "m", &store2, 4, 0 },
+ { "m", "m", NULL, 5, 0 },
+ };
+
+ size_t i;
+
+ sdb_store_host("m", 1);
+ sdb_store_host("l", 1);
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ int status;
+
+ status = sdb_store_metric(golden_data[i].host,
+ golden_data[i].metric, golden_data[i].store,
+ golden_data[i].last_update);
+ fail_unless(status == golden_data[i].expected,
+ "sdb_store_metric(%s, %s, %p, %d) = %d; expected: %d",
+ golden_data[i].host, golden_data[i].metric,
+ golden_data[i].store, golden_data[i].last_update,
+ status, golden_data[i].expected);
+ }
+}
+END_TEST
+
+START_TEST(test_store_metric_attr)
+{
+ struct {
+ const char *host;
+ const char *metric;
+ const char *attr;
+ const sdb_data_t value;
+ sdb_time_t last_update;
+ int expected;
+ } golden_data[] = {
+ { "k", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+ /* retry, it should still fail */
+ { "k", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+ { "l", "mX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+ /* retry, it should still fail */
+ { "l", "mX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+ { "l", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
+ { "l", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 1 },
+ { "l", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 2, 0 },
+ { "l", "m1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
+ { "l", "m1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 1 },
+ { "l", "m2", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
+ { "m", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
+ };
+
+ size_t i;
+
+ sdb_store_host("m", 1);
+ sdb_store_host("l", 1);
+ sdb_store_metric("m", "m1", NULL, 1);
+ sdb_store_metric("l", "m1", NULL, 1);
+ sdb_store_metric("l", "m2", NULL, 1);
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ int status;
+
+ status = sdb_store_metric_attr(golden_data[i].host,
+ golden_data[i].metric, golden_data[i].attr,
+ &golden_data[i].value, golden_data[i].last_update);
+ fail_unless(status == golden_data[i].expected,
+ "sdb_store_metric_attr(%s, %s, %s, %d, %d) = %d; "
+ "expected: %d", golden_data[i].host, golden_data[i].metric,
+ golden_data[i].attr, golden_data[i].value.data.integer,
+ golden_data[i].last_update, status, golden_data[i].expected);
+ }
+}
+END_TEST
+
START_TEST(test_store_service)
{
struct {
START_TEST(test_store_service)
{
struct {
END_TEST
static void
END_TEST
static void
-verify_json_output(sdb_strbuf_t *buf, const char *expected, int flags)
+verify_json_output(sdb_strbuf_t *buf, const char *expected,
+ const char *filter_str, int flags)
{
int pos;
size_t len1, len2;
{
int pos;
size_t len1, len2;
}
fail_unless(pos == -1,
}
fail_unless(pos == -1,
- "sdb_store_tojson(%x) returned unexpected result\n"
+ "sdb_store_tojson(<buf>, %s, %x) returned unexpected result\n"
" got: %s\n %*s\n expected: %s",
" got: %s\n %*s\n expected: %s",
- flags, sdb_strbuf_string(buf), pos + 1, "^", expected);
+ filter_str, flags, sdb_strbuf_string(buf), pos + 1, "^",
+ expected);
} /* verify_json_output */
START_TEST(test_store_tojson)
} /* verify_json_output */
START_TEST(test_store_tojson)
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"], "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"], "
+ "\"metrics\": ["
+ "{\"name\": \"m1\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": ["
+ "{\"name\": \"k3\", \"value\": 42, "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []}"
+ "]},"
+ "{\"name\": \"m2\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": []}"
+ "], "
"\"services\": []},"
"{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": [], "
"\"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\", "
"\"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\": []}"
"{\"name\": \"k3\", \"value\": \"v3\", "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
+ "], "
+ "\"metrics\": ["
+ "{\"name\": \"m1\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": ["
+ "{\"name\": \"k3\", \"value\": 42, "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []}"
+ "]},"
+ "{\"name\": \"m2\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": []}"
"]},"
"{\"name\": \"h2\", \"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\": []}"
+ "\"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\": [], "
+ "\"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\": [], "
"]}" },
{ { 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\": ["
+ "{\"name\": \"m1\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": []},"
+ "{\"name\": \"m2\", "
+ "\"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\": [], "
"\"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\", "
"\"services\": ["
"{\"name\": \"s1\", "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}" },
"\"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\": []},"
"{\"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\": [], "
"{\"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\", "
"\"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\": []},"
"], "
"\"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"], "
+ "\"metrics\": ["
+ "{\"name\": \"m2\", "
+ "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"update_interval\": \"0s\", \"backends\": [], "
+ "\"attributes\": []}"
+ "], "
"\"services\": []}"
"]}" },
{ { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
"\"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\": [], "
"{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": [], "
+ "\"metrics\": [], "
"\"services\": []}"
"]}" },
};
"\"services\": []}"
"]}" },
};
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
sdb_store_matcher_t *filter = NULL;
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
sdb_store_matcher_t *filter = NULL;
+ char filter_str[1024] = "<none>";
int status;
sdb_strbuf_clear(buf);
int status;
sdb_strbuf_clear(buf);
sdb_object_deref(SDB_OBJ(c));
fail_unless(filter != NULL,
"INTERNAL ERROR: sdb_store_*_matcher() = NULL");
sdb_object_deref(SDB_OBJ(c));
fail_unless(filter != NULL,
"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,
}
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);
+ verify_json_output(buf, golden_data[i].expected,
+ filter_str, golden_data[i].flags);
sdb_object_deref(SDB_OBJ(filter));
}
sdb_strbuf_destroy(buf);
sdb_object_deref(SDB_OBJ(filter));
}
sdb_strbuf_destroy(buf);
tcase_add_test(tc, test_store_host);
tcase_add_test(tc, test_store_get_host);
tcase_add_test(tc, test_store_attr);
tcase_add_test(tc, test_store_host);
tcase_add_test(tc, test_store_get_host);
tcase_add_test(tc, test_store_attr);
+ tcase_add_test(tc, test_store_metric);
+ tcase_add_test(tc, test_store_metric_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_store_service);
tcase_add_test(tc, test_store_service_attr);
tcase_add_test(tc, test_get_field);