Code

store: Removed sdb_store_matcher_tostring().
[sysdb.git] / t / unit / core / store_test.c
index fb2c24bf20d596450c36f67e5b7791a96c2b5122..ddf5ff29a1bb62385fc3f7158cc8e8d22a4fbd8c 100644 (file)
@@ -53,10 +53,16 @@ populate(void)
        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;
+       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;
@@ -220,6 +226,103 @@ START_TEST(test_store_attr)
 }
 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 {
@@ -304,7 +407,8 @@ START_TEST(test_store_service_attr)
 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,
+               sdb_store_matcher_t *filter, int flags)
 {
        int pos;
        size_t len1, len2;
@@ -325,9 +429,10 @@ verify_json_output(sdb_strbuf_t *buf, const char *expected, int flags)
        }
 
        fail_unless(pos == -1,
-                       "sdb_store_tojson(%x) returned unexpected result\n"
+                       "sdb_store_tojson(<buf>, %p, %x) returned unexpected result\n"
                        "         got: %s\n              %*s\n    expected: %s",
-                       flags, sdb_strbuf_string(buf), pos + 1, "^", expected);
+                       filter, flags, sdb_strbuf_string(buf), pos + 1, "^",
+                       expected);
 } /* verify_json_output */
 
 START_TEST(test_store_tojson)
@@ -345,7 +450,7 @@ START_TEST(test_store_tojson)
                const char *expected;
        } golden_data[] = {
                { { NULL, 0, SDB_DATA_INIT }, 0,
-                       "{\"hosts\":["
+                       "["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                        "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": ["
@@ -359,7 +464,20 @@ START_TEST(test_store_tojson)
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                                        "\"update_interval\": \"0s\", \"backends\": []}"
                                        "], "
-                                       "\"metrics\": [], "
+                                       "\"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\": [], "
@@ -382,9 +500,9 @@ START_TEST(test_store_tojson)
                                                                        "\"update_interval\": \"0s\", \"backends\": []}"
                                                        "]}"
                                        "]}"
-                       "]}" },
+                       "]" },
                { { NULL, 0, SDB_DATA_INIT }, SDB_SKIP_SERVICES,
-                       "{\"hosts\":["
+                       "["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                        "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": ["
@@ -398,14 +516,27 @@ START_TEST(test_store_tojson)
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                                        "\"update_interval\": \"0s\", \"backends\": []}"
                                        "], "
-                                       "\"metrics\": []},"
+                                       "\"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\": [], "
                                        "\"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\": ["
@@ -440,12 +571,19 @@ START_TEST(test_store_tojson)
                                                                        "\"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\": [], "
+                                       "\"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\": [], "
@@ -458,17 +596,24 @@ START_TEST(test_store_tojson)
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                                        "\"update_interval\": \"0s\", \"backends\": []}"
                                        "]}"
-                       "]}" },
+                       "]" },
                { { 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\": []}"
-                       "]}" },
+                       "]" },
+               { { sdb_store_eq_matcher, SDB_FIELD_NAME,
+                               { SDB_TYPE_STRING, { .string = "h1" } } }, 0,
+                       "["
+                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                       "\"attributes\": [], \"metrics\": [], \"services\": []}"
+                       "]" },
                { { 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\": [], "
@@ -483,10 +628,10 @@ START_TEST(test_store_tojson)
                                                                        "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "]}"
                                        "]}"
-                       "]}" },
+                       "]" },
                { { 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\", \"backends\": [], "
                                        "\"attributes\": ["
@@ -494,18 +639,23 @@ START_TEST(test_store_tojson)
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                                        "\"update_interval\": \"0s\", \"backends\": []},"
                                        "], "
-                                       "\"metrics\": [], "
+                                       "\"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,
                                { SDB_TYPE_DATETIME, { .datetime = 3 } } }, 0,
-                       "{\"hosts\":["
+                       "["
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                        "\"update_interval\": \"0s\", \"backends\": [], "
                                        "\"attributes\": [], "
                                        "\"metrics\": [], "
                                        "\"services\": []}"
-                       "]}" },
+                       "]" },
        };
 
        buf = sdb_strbuf_create(0);
@@ -514,7 +664,6 @@ START_TEST(test_store_tojson)
 
        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);
@@ -536,15 +685,13 @@ START_TEST(test_store_tojson)
                                        "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(<buf>, %s, %x) = %d; expected: 0",
-                               filter_str, golden_data[i].flags, status);
+                               "sdb_store_tojson(<buf>, %p, %x) = %d; expected: 0",
+                               filter, 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, golden_data[i].flags);
                sdb_object_deref(SDB_OBJ(filter));
        }
        sdb_strbuf_destroy(buf);
@@ -571,15 +718,33 @@ START_TEST(test_get_field)
        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, NULL);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+                       "expected: 0");
+       /* 'name' is dynamically allocated; make sure it's not leaked even
+        * if there is no result parameter */
+       check = sdb_store_get_field(host, SDB_FIELD_NAME, NULL);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+                       "expected: 0");
+
+       check = sdb_store_get_field(host, SDB_FIELD_NAME, &value);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) = "
+                       "%d; expected: 0");
+       fail_unless((value.type == SDB_TYPE_STRING)
+                       && (! strcmp(value.data.string, "host")),
+                       "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) "
+                       "returned value {%d, %s}; expected {%d, host}",
+                       value.type, value.data.string, SDB_TYPE_STRING);
+       sdb_data_free_datum(&value);
+
        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>) = "
@@ -611,6 +776,19 @@ START_TEST(test_get_field)
                        "sdb_store_get_field(<host>, SDB_FIELD_INTERVAL, <value>) "
                        "returned value {%d, %lu}; expected {%d, 10}",
                        value.type, value.data.datetime, SDB_TYPE_DATETIME);
+
+       check = sdb_store_get_field(host, SDB_FIELD_BACKEND, &value);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_BACKEND, <value>) = "
+                       "%d; expected: 0");
+       /* there are no backends in this test */
+       fail_unless((value.type == (SDB_TYPE_ARRAY | SDB_TYPE_STRING))
+                       && (value.data.array.length == 0)
+                       && (value.data.array.values == NULL),
+                       "sdb_store_get_field(<host>, SDB_FIELD_BACKEND, <value>) "
+                       "returned value {%d, %lu, %p}; expected {%d, 0, NULL}",
+                       value.type, value.data.array.length, value.data.array.values,
+                       SDB_TYPE_ARRAY | SDB_TYPE_STRING);
 }
 END_TEST
 
@@ -743,6 +921,8 @@ core_store_suite(void)
        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);