diff --git a/t/core/store_test.c b/t/core/store_test.c
index c27310d4e6720dd5819483e00af7668f3e498287..855c8c09e857b4b821e022bda2ec5f23d525856f 100644 (file)
--- a/t/core/store_test.c
+++ b/t/core/store_test.c
}
END_TEST
+START_TEST(test_store_get_host)
+{
+ char *golden_hosts[] = { "a", "b", "c" };
+ char *unknown_hosts[] = { "x", "y", "z" };
+ size_t i;
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
+ int status = sdb_store_host(golden_hosts[i], 1);
+ fail_unless(status >= 0,
+ "sdb_store_host(%s) = %d; expected: >=0",
+ golden_hosts[i], status);
+ }
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
+ sdb_store_base_t *sobj1, *sobj2;
+ int ref_cnt;
+
+ fail_unless(sdb_store_has_host(golden_hosts[i]),
+ "sdb_store_has_host(%s) = FALSE; expected: TRUE",
+ golden_hosts[i]);
+
+ sobj1 = sdb_store_get_host(golden_hosts[i]);
+ fail_unless(sobj1 != NULL,
+ "sdb_store_get_host(%s) = NULL; expected: <host>",
+ golden_hosts[i]);
+ ref_cnt = SDB_OBJ(sobj1)->ref_cnt;
+
+ fail_unless(ref_cnt > 1,
+ "sdb_store_get_host(%s) did not increment ref count: "
+ "got: %d; expected: >1", golden_hosts[i], ref_cnt);
+
+ sobj2 = sdb_store_get_host(golden_hosts[i]);
+ fail_unless(sobj2 != NULL,
+ "sdb_store_get_host(%s) = NULL; expected: <host>",
+ golden_hosts[i]);
+
+ fail_unless(sobj1 == sobj2,
+ "sdb_store_get_host(%s) returned different objects "
+ "in successive calls", golden_hosts[i]);
+ fail_unless(SDB_OBJ(sobj2)->ref_cnt == ref_cnt + 1,
+ "sdb_store_get_hosts(%s) did not increment ref count "
+ "(first call: %d; second call: %d)",
+ golden_hosts[i], ref_cnt, SDB_OBJ(sobj2)->ref_cnt);
+
+ sdb_object_deref(SDB_OBJ(sobj1));
+ sdb_object_deref(SDB_OBJ(sobj2));
+ }
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(unknown_hosts); ++i) {
+ sdb_store_base_t *sobj;
+
+ fail_unless(!sdb_store_has_host(unknown_hosts[i]),
+ "sdb_store_has_host(%s) = TRUE; expected: FALSE",
+ unknown_hosts[i]);
+
+ sobj = sdb_store_get_host(unknown_hosts[i]);
+ fail_unless(!sobj, "sdb_store_get_host(%s) = <host:%s>; expected: NULL",
+ unknown_hosts[i], sobj ? SDB_OBJ(sobj)->name : "NULL");
+ }
+}
+END_TEST
+
START_TEST(test_store_attr)
{
struct {
const char *host;
const char *key;
- const char *value;
+ char *value;
sdb_time_t last_update;
int expected;
} golden_data[] = {
sdb_store_host("l", 1);
sdb_store_host("m", 1);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_data_t datum;
int status;
+ /* XXX: test other types as well */
+ datum.type = SDB_TYPE_STRING;
+ datum.data.string = golden_data[i].value;
+
status = sdb_store_attribute(golden_data[i].host,
- golden_data[i].key, golden_data[i].value,
+ golden_data[i].key, &datum,
golden_data[i].last_update);
fail_unless(status == golden_data[i].expected,
"sdb_store_attribute(%s, %s, %s, %d) = %d; expected: %d",
}
END_TEST
-START_TEST(test_store_tojson)
+static void
+verify_json_output(sdb_strbuf_t *buf, const char *expected, int flags)
{
- sdb_strbuf_t *buf;
-
- int status, pos;
+ int pos;
size_t len1, len2;
size_t i;
- const char *expected = "{\"hosts\":["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
- "\"attributes\": ["
- "{\"name\": \"k1\", \"value\": \"v1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
- "{\"name\": \"k2\", \"value\": \"v2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
- "{\"name\": \"k3\", \"value\": \"v3\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
- "], "
- "\"services\": []},"
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
- "\"attributes\": [], "
- "\"services\": ["
- "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
- "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
- "]}"
- "]}";
-
- sdb_store_host("h1", 1);
- sdb_store_host("h2", 1);
-
- sdb_store_attribute("h1", "k1", "v1", 1);
- sdb_store_attribute("h1", "k2", "v2", 1);
- sdb_store_attribute("h1", "k3", "v3", 1);
-
- sdb_store_service("h2", "s1", 1);
- sdb_store_service("h2", "s2", 1);
-
- buf = sdb_strbuf_create(0);
- status = sdb_store_tojson(buf);
- fail_unless(status == 0,
- "sdb_store_tojson() = %d; expected: 0", status);
-
len1 = strlen(sdb_strbuf_string(buf));
len2 = strlen(expected);
}
fail_unless(pos == -1,
- "sdb_store_tojson() returned unexpected result\n"
+ "sdb_store_tojson(%x) returned unexpected result\n"
" got: %s\n %*s\n expected: %s",
- sdb_strbuf_string(buf), pos + 1, "^", expected);
+ flags, sdb_strbuf_string(buf), pos + 1, "^", expected);
+} /* verify_json_output */
+
+START_TEST(test_store_tojson)
+{
+ sdb_strbuf_t *buf;
+ sdb_data_t datum;
+ size_t i;
+
+ struct {
+ int flags;
+ const char *expected;
+ } golden_data[] = {
+ { 0, "{\"hosts\":["
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"attributes\": ["
+ "{\"name\": \"k1\", \"value\": \"v1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
+ "{\"name\": \"k2\", \"value\": \"v2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
+ "{\"name\": \"k3\", \"value\": \"v3\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+ "], "
+ "\"services\": []},"
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"attributes\": [], "
+ "\"services\": ["
+ "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
+ "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+ "]}"
+ "]}" },
+ { SDB_SKIP_SERVICES,
+ "{\"hosts\":["
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"attributes\": ["
+ "{\"name\": \"k1\", \"value\": \"v1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
+ "{\"name\": \"k2\", \"value\": \"v2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
+ "{\"name\": \"k3\", \"value\": \"v3\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+ "]},"
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"attributes\": []}"
+ "]}" },
+ { SDB_SKIP_ATTRIBUTES,
+ "{\"hosts\":["
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"services\": []},"
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"services\": ["
+ "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
+ "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+ "]}"
+ "]}" },
+ { SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES,
+ "{\"hosts\":["
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+ "]}" },
+ };
+
+ sdb_store_host("h1", 1);
+ sdb_store_host("h2", 1);
+
+ datum.type = SDB_TYPE_STRING;
+ datum.data.string = "v1";
+ sdb_store_attribute("h1", "k1", &datum, 1);
+ datum.data.string = "v2";
+ sdb_store_attribute("h1", "k2", &datum, 1);
+ datum.data.string = "v3";
+ sdb_store_attribute("h1", "k3", &datum, 1);
+
+ sdb_store_service("h2", "s1", 1);
+ sdb_store_service("h2", "s2", 1);
+
+ buf = sdb_strbuf_create(0);
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ int status;
+
+ sdb_strbuf_clear(buf);
+
+ status = sdb_store_tojson(buf, golden_data[i].flags);
+ fail_unless(status == 0,
+ "sdb_store_tojson(%x) = %d; expected: 0",
+ golden_data[i].flags, status);
+
+ verify_json_output(buf, golden_data[i].expected, golden_data[i].flags);
+ }
+ sdb_strbuf_destroy(buf);
}
END_TEST
* even when using CK_NOFORK */
tcase_add_test(tc, test_store_tojson);
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_service);
suite_add_tcase(s, tc);