Code

store: Added sdb_store_get_field().
[sysdb.git] / t / unit / core / store_test.c
index cca3968c4674df076a948d641a71d211c1257e3d..defa6fcfc45d9aaeb021e3ddffdfe9cadd2dc147 100644 (file)
@@ -38,18 +38,33 @@ populate(void)
        sdb_data_t datum;
 
        sdb_store_host("h1", 1);
-       sdb_store_host("h2", 1);
+       sdb_store_host("h2", 3);
 
        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);
+       sdb_store_attribute("h1", "k2", &datum, 2);
        datum.data.string = "v3";
-       sdb_store_attribute("h1", "k3", &datum, 1);
+       sdb_store_attribute("h1", "k3", &datum, 2);
+
+       /* make sure that older updates don't overwrite existing values */
+       datum.data.string = "fail";
+       sdb_store_attribute("h1", "k2", &datum, 1);
+       sdb_store_attribute("h1", "k3", &datum, 2);
 
        sdb_store_service("h2", "s1", 1);
-       sdb_store_service("h2", "s2", 1);
+       sdb_store_service("h2", "s2", 2);
+
+       datum.type = SDB_TYPE_INTEGER;
+       datum.data.integer = 123;
+       sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
+       datum.data.integer = 4711;
+       sdb_store_service_attr("h2", "s2", "k2", &datum, 1);
+
+       /* don't overwrite k1 */
+       datum.data.integer = 666;
+       sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
 } /* populate */
 
 START_TEST(test_store_host)
@@ -59,13 +74,13 @@ START_TEST(test_store_host)
                sdb_time_t  last_update;
                int         expected;
        } golden_data[] = {
+               { "a", 1, 0 },
                { "a", 2, 0 },
-               { "a", 3, 0 },
                { "a", 1, 1 },
-               { "b", 2, 0 },
+               { "b", 1, 0 },
                { "b", 1, 1 },
                { "A", 1, 1 }, /* case-insensitive */
-               { "A", 4, 0 },
+               { "A", 3, 0 },
        };
 
        struct {
@@ -172,14 +187,14 @@ START_TEST(test_store_attr)
                sdb_time_t  last_update;
                int         expected;
        } golden_data[] = {
-               { "k", "k", "v", 1, -1 },
-               { "k", "k", "v", 1, -1 }, /* retry to ensure the host is not created */
-               { "l", "k1", "v1", 1, 0 },
-               { "l", "k1", "v2", 2, 0 },
-               { "l", "k1", "v3", 1, 1 },
-               { "l", "k2", "v1", 1, 0 },
-               { "m", "k", "v1", 2, 0 },
-               { "m", "k", "v2", 1, 1 },
+               { "k", "k",  "v",  1, -1 },
+               { "k", "k",  "v",  1, -1 }, /* retry to ensure the host is not created */
+               { "l", "k1", "v1", 1,  0 },
+               { "l", "k1", "v2", 2,  0 },
+               { "l", "k1", "v3", 2,  1 },
+               { "l", "k2", "v1", 1,  0 },
+               { "m", "k",  "v1", 1,  0 },
+               { "m", "k",  "v2", 1,  1 },
        };
 
        size_t i;
@@ -213,14 +228,14 @@ START_TEST(test_store_service)
                sdb_time_t  last_update;
                int         expected;
        } golden_data[] = {
-               { "k", "s", 1, -1 },
-               { "k", "s", 1, -1 }, /* retry to ensure the host is not created */
-               { "l", "s1", 1, 0 },
-               { "l", "s1", 2, 0 },
-               { "l", "s1", 1, 1 },
-               { "l", "s2", 1, 0 },
-               { "m", "s", 2, 0 },
-               { "m", "s", 1, 1 },
+               { "k", "s",  1, -1 },
+               { "k", "s",  1, -1 }, /* retry to ensure the host is not created */
+               { "l", "s1", 1,  0 },
+               { "l", "s1", 2,  0 },
+               { "l", "s1", 2,  1 },
+               { "l", "s2", 1,  0 },
+               { "m", "s",  1,  0 },
+               { "m", "s",  1,  1 },
        };
 
        size_t i;
@@ -240,6 +255,54 @@ START_TEST(test_store_service)
 }
 END_TEST
 
+START_TEST(test_store_service_attr)
+{
+       struct {
+               const char *host;
+               const char *svc;
+               const char *attr;
+               const sdb_data_t value;
+               sdb_time_t  last_update;
+               int expected;
+       } golden_data[] = {
+               { "k", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+               /* retry, it should still fail */
+               { "k", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+               { "l", "sX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+               /* retry, it should still fail */
+               { "l", "sX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
+               { "l", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1,  0 },
+               { "l", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1,  1 },
+               { "l", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 2,  0 },
+               { "l", "s1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1,  0 },
+               { "l", "s1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1,  1 },
+               { "l", "s2", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1,  0 },
+               { "m", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1,  0 },
+       };
+
+       size_t i;
+
+       sdb_store_host("m", 1);
+       sdb_store_host("l", 1);
+       sdb_store_service("m", "s1", 1);
+       sdb_store_service("l", "s1", 1);
+       sdb_store_service("l", "s2", 1);
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               int status;
+
+               status = sdb_store_service_attr(golden_data[i].host,
+                               golden_data[i].svc, golden_data[i].attr,
+                               &golden_data[i].value, golden_data[i].last_update);
+               fail_unless(status == golden_data[i].expected,
+                               "sdb_store_service_attr(%s, %s, %s, %d, %d) = %d; "
+                               "expected: %d", golden_data[i].host, golden_data[i].svc,
+                               golden_data[i].attr, golden_data[i].value.data.integer,
+                               golden_data[i].last_update, status, golden_data[i].expected);
+       }
+}
+END_TEST
+
 static void
 verify_json_output(sdb_strbuf_t *buf, const char *expected, int flags)
 {
@@ -273,77 +336,129 @@ START_TEST(test_store_tojson)
        size_t i;
 
        struct {
+               struct {
+                       sdb_store_matcher_t *(*m)(sdb_store_cond_t *);
+                       int field;
+                       sdb_data_t value;
+               } filter;
                int flags;
                const char *expected;
        } golden_data[] = {
-               { 0, "{\"hosts\":["
+               { { NULL, 0, SDB_DATA_INIT }, 0,
+                       "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": ["
                                                "{\"name\": \"k1\", \"value\": \"v1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k2\", \"value\": \"v2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k3\", \"value\": \"v3\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"update_interval\": \"0s\", \"backends\": []}"
                                        "], "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": [], "
                                        "\"services\": ["
                                                "{\"name\": \"s1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                                       "\"attributes\": []},"
                                                "{\"name\": \"s2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"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\": []}"
+                                                       "]}"
                                        "]}"
                        "]}" },
-               { SDB_SKIP_SERVICES,
+               { { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_SERVICES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": ["
                                                "{\"name\": \"k1\", \"value\": \"v1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k2\", \"value\": \"v2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
                                                "{\"name\": \"k3\", \"value\": \"v3\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"update_interval\": \"0s\", \"backends\": []}"
                                        "]},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": []}"
                        "]}" },
-               { SDB_SKIP_ATTRIBUTES,
+               { { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_ATTRIBUTES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"services\": ["
                                                "{\"name\": \"s1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"},"
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
+                                               "{\"name\": \"s2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\", \"backends\": []}"
+                                       "]}"
+                       "]}" },
+               { { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES,
+                       "{\"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\": []}"
+                       "]}" },
+               { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
+                               { SDB_TYPE_DATETIME, { .datetime = 1 } } }, 0,
+                       "{\"hosts\":["
+                               "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                       "\"attributes\": [], "
+                                       "\"services\": ["
                                                "{\"name\": \"s2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\"}"
+                                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                                       "\"attributes\": ["
+                                                               "{\"name\": \"k1\", \"value\": 123, "
+                                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                                       "\"update_interval\": \"0s\", \"backends\": []},"
+                                                       "]}"
                                        "]}"
                        "]}" },
-               { SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES,
+               { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
+                               { SDB_TYPE_DATETIME, { .datetime = 1 } } }, 0,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\"},"
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                       "\"attributes\": ["
+                                               "{\"name\": \"k1\", \"value\": \"v1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\", \"backends\": []},"
+                                       "], "
+                                       "\"services\": []}"
+                       "]}" },
+               { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
+                               { SDB_TYPE_DATETIME, { .datetime = 3 } } }, 0,
+                       "{\"hosts\":["
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\"}"
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                       "\"attributes\": [], "
+                                       "\"services\": []}"
                        "]}" },
        };
 
@@ -352,21 +467,103 @@ START_TEST(test_store_tojson)
        populate();
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_store_matcher_t *filter = NULL;
                int status;
 
                sdb_strbuf_clear(buf);
 
-               status = sdb_store_tojson(buf, golden_data[i].flags);
+               if (golden_data[i].filter.m) {
+                       sdb_store_expr_t *expr;
+                       sdb_store_cond_t *c = NULL;
+
+                       expr = sdb_store_expr_constvalue(&golden_data[i].filter.value);
+                       fail_unless(expr != NULL,
+                                       "INTERNAL ERROR: sdb_store_expr_constvalue() = NULL");
+                       c = sdb_store_obj_cond(golden_data[i].filter.field, expr);
+                       sdb_object_deref(SDB_OBJ(expr));
+                       fail_unless(c != NULL,
+                                       "INTERNAL ERROR: sdb_store_obj_cond() = NULL");
+                       filter = golden_data[i].filter.m(c);
+                       sdb_object_deref(SDB_OBJ(c));
+                       fail_unless(filter != NULL,
+                                       "INTERNAL ERROR: sdb_store_*_matcher() = NULL");
+               }
+
+               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);
 
                verify_json_output(buf, golden_data[i].expected, golden_data[i].flags);
+               sdb_object_deref(SDB_OBJ(filter));
        }
        sdb_strbuf_destroy(buf);
 }
 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;
@@ -383,7 +580,7 @@ START_TEST(test_interval)
 
        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);
@@ -393,8 +590,8 @@ START_TEST(test_interval)
 
        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);
@@ -404,20 +601,20 @@ START_TEST(test_interval)
 
        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));
 }
@@ -497,6 +694,8 @@ core_store_suite(void)
        tcase_add_test(tc, test_store_get_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);