Code

store: Use the JSON writer when querying the in-memory store.
[sysdb.git] / t / unit / core / store_json_test.c
index 113d3a0010d0f5669e53d68aa6dbd2fdad753858..b880f7b99a43c5bf9c5381491b9649d43bd53c5a 100644 (file)
 #undef SDB_INTERVAL_SECOND
 #define SDB_INTERVAL_SECOND 1000000000L
 
+static sdb_store_t *store;
+
 static void
 populate(void)
 {
        sdb_data_t datum;
 
-       sdb_store_host("h1", 1 * SDB_INTERVAL_SECOND);
-       sdb_store_host("h2", 3 * SDB_INTERVAL_SECOND);
+       store = sdb_store_create();
+       ck_assert(store != NULL);
+
+       sdb_store_host(store, "h1", 1 * SDB_INTERVAL_SECOND);
+       sdb_store_host(store, "h2", 3 * SDB_INTERVAL_SECOND);
 
        datum.type = SDB_TYPE_STRING;
        datum.data.string = "v1";
-       sdb_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
+       sdb_store_attribute(store, "h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
        datum.data.string = "v2";
-       sdb_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
+       sdb_store_attribute(store, "h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
        datum.data.string = "v3";
-       sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
+       sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
 
        /* make sure that older updates don't overwrite existing values */
        datum.data.string = "fail";
-       sdb_store_attribute("h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
-       sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
+       sdb_store_attribute(store, "h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
+       sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
 
-       sdb_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
-       sdb_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
-       sdb_store_metric("h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
+       sdb_store_metric(store, "h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
+       sdb_store_metric(store, "h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
+       sdb_store_metric(store, "h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
 
-       sdb_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND);
-       sdb_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND);
+       sdb_store_service(store, "h2", "s1", 1 * SDB_INTERVAL_SECOND);
+       sdb_store_service(store, "h2", "s2", 2 * SDB_INTERVAL_SECOND);
 
        datum.type = SDB_TYPE_INTEGER;
        datum.data.integer = 42;
-       sdb_store_metric_attr("h1", "m1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
+       sdb_store_metric_attr(store, "h1", "m1", "k3",
+                       &datum, 2 * SDB_INTERVAL_SECOND);
 
        datum.data.integer = 123;
-       sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
+       sdb_store_service_attr(store, "h2", "s2", "k1",
+                       &datum, 2 * SDB_INTERVAL_SECOND);
        datum.data.integer = 4711;
-       sdb_store_service_attr("h2", "s2", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
+       sdb_store_service_attr(store, "h2", "s2", "k2",
+                       &datum, 1 * SDB_INTERVAL_SECOND);
 
        /* don't overwrite k1 */
        datum.data.integer = 666;
-       sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
+       sdb_store_service_attr(store, "h2", "s2", "k1",
+                       &datum, 2 * SDB_INTERVAL_SECOND);
 } /* populate */
 
+static void
+turndown(void)
+{
+       sdb_object_deref(SDB_OBJ(store));
+       store = NULL;
+} /* turndown */
+
 static int
 scan_tojson(sdb_store_obj_t *obj,
                sdb_store_matcher_t __attribute__((unused)) *filter,
                void *user_data)
 {
-       sdb_store_json_formatter_t *f = user_data;
-       return sdb_store_json_emit(f, obj);
+       return sdb_store_emit(obj, &sdb_store_json_writer, user_data);
 } /* scan_tojson */
 
 static int
 scan_tojson_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
                void *user_data)
 {
-       sdb_store_json_formatter_t *f = user_data;
-       return sdb_store_json_emit_full(f, obj, filter);
+       return sdb_store_emit_full(obj, filter, &sdb_store_json_writer, user_data);
 } /* scan_tojson_full */
 
 static void
@@ -156,9 +170,6 @@ struct {
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k3\", \"value\": 42, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []}"
@@ -166,12 +177,7 @@ struct {
                                        "{\"name\": \"m2\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]},"
                        "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
                                "\"update_interval\": \"0s\", \"backends\": [], "
@@ -179,29 +185,16 @@ struct {
                                        "{\"name\": \"m1\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "], "
                                "\"services\": ["
                                        "{\"name\": \"s1\", "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]},"
+                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                        "{\"name\": \"s2\", "
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k1\", \"value\": 123, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []},"
@@ -237,9 +230,6 @@ struct {
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k1\", \"value\": 123, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []}"
@@ -261,12 +251,7 @@ struct {
                                        "{\"name\": \"m2\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]}"
                "]" },
        { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
@@ -284,65 +269,42 @@ struct {
        { { NULL, 0, SDB_DATA_INIT },
                SDB_SERVICE, scan_tojson_full,
                "["
-                       "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
+                       "{\"name\": \"s1\", "
+                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []},"
+                       "{\"name\": \"s2\", "
+                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"services\": ["
-                                       "{\"name\": \"s1\", "
-                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]},"
-                                       "{\"name\": \"s2\", "
+                               "\"attributes\": ["
+                                       "{\"name\": \"k1\", \"value\": 123, "
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
-                                                       "{\"name\": \"k1\", \"value\": 123, "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
-                                                       "{\"name\": \"k2\", \"value\": 4711, "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []},"
+                                       "{\"name\": \"k2\", \"value\": 4711, "
+                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]}"
                "]" },
        { { NULL, 0, SDB_DATA_INIT },
                SDB_SERVICE, scan_tojson,
                "["
-                       "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
-                               "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"services\": ["
-                                       "{\"name\": \"s1\", "
-                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": []},"
-                                       "{\"name\": \"s2\", "
-                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                               "]}"
+                       "{\"name\": \"s1\", "
+                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []},"
+                       "{\"name\": \"s2\", "
+                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []}"
                "]" },
        { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
                        { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
                SDB_SERVICE, scan_tojson_full,
                "["
-                       "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
+                       "{\"name\": \"s2\", "
+                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"services\": ["
-                                       "{\"name\": \"s2\", "
+                               "\"attributes\": ["
+                                       "{\"name\": \"k1\", \"value\": 123, "
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
-                                                       "{\"name\": \"k1\", \"value\": 123, "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]}"
                "]" },
        { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
@@ -352,86 +314,48 @@ struct {
        { { NULL, 0, SDB_DATA_INIT },
                SDB_METRIC, scan_tojson_full,
                "["
-                       "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                       "{\"name\": \"m1\", "
+                               "\"timeseries\": false, "
+                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"metrics\": ["
-                                       "{\"name\": \"m1\", "
-                                               "\"timeseries\": false, "
+                               "\"attributes\": ["
+                                       "{\"name\": \"k3\", \"value\": 42, "
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
-                                                       "{\"name\": \"k3\", \"value\": 42, "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]},"
-                                       "{\"name\": \"m2\", "
-                                               "\"timeseries\": false, "
-                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]},"
-                       "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
-                               "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"metrics\": ["
-                                       "{\"name\": \"m1\", "
-                                               "\"timeseries\": false, "
-                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
-                               "]}"
+                       "{\"name\": \"m2\", "
+                               "\"timeseries\": false, "
+                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []},"
+                       "{\"name\": \"m1\", "
+                               "\"timeseries\": false, "
+                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []}"
                "]" },
        { { NULL, 0, SDB_DATA_INIT },
                SDB_METRIC, scan_tojson,
                "["
-                       "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                               "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"metrics\": ["
-                                       "{\"name\": \"m1\", "
-                                               "\"timeseries\": false, "
-                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": []},"
-                                       "{\"name\": \"m2\", "
-                                               "\"timeseries\": false, "
-                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                               "]},"
-                       "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
-                               "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"metrics\": ["
-                                       "{\"name\": \"m1\", "
-                                               "\"timeseries\": false, "
-                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                               "]}"
+                       "{\"name\": \"m1\", "
+                               "\"timeseries\": false, "
+                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []},"
+                       "{\"name\": \"m2\", "
+                               "\"timeseries\": false, "
+                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []},"
+                       "{\"name\": \"m1\", "
+                               "\"timeseries\": false, "
+                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []}"
                "]" },
        { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
                        { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
                SDB_METRIC, scan_tojson_full,
                "["
-                       "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                               "\"update_interval\": \"0s\", \"backends\": [], "
-                               "\"metrics\": ["
-                                       "{\"name\": \"m2\", "
-                                               "\"timeseries\": false, "
-                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
-                               "]}"
+                       "{\"name\": \"m2\", "
+                               "\"timeseries\": false, "
+                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
+                               "\"update_interval\": \"0s\", \"backends\": []}"
                "]" },
        { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
                        { SDB_TYPE_DATETIME, { .datetime = 0 } } },
@@ -466,11 +390,10 @@ START_TEST(test_store_tojson)
        }
 
        sdb_strbuf_clear(buf);
-       f = sdb_store_json_formatter(buf,
-                       store_tojson_data[_i].type, SDB_WANT_ARRAY);
+       f = sdb_store_json_formatter(buf, store_tojson_data[_i].type, SDB_WANT_ARRAY);
        ck_assert(f != NULL);
 
-       status = sdb_store_scan(store_tojson_data[_i].type,
+       status = sdb_store_scan(store, store_tojson_data[_i].type,
                        /* m = */ NULL, filter, store_tojson_data[_i].f, f);
        fail_unless(status == 0,
                        "sdb_store_scan(HOST, ..., tojson) = %d; expected: 0",
@@ -479,8 +402,8 @@ START_TEST(test_store_tojson)
 
        verify_json_output(buf, store_tojson_data[_i].expected);
 
-       free(f);
        sdb_object_deref(SDB_OBJ(filter));
+       sdb_object_deref(SDB_OBJ(f));
        sdb_strbuf_destroy(buf);
 }
 END_TEST
@@ -488,8 +411,8 @@ END_TEST
 TEST_MAIN("core::store_json")
 {
        TCase *tc = tcase_create("core");
+       tcase_add_unchecked_fixture(tc, populate, turndown);
        TC_ADD_LOOP_TEST(tc, store_tojson);
-       tcase_add_unchecked_fixture(tc, populate, sdb_store_clear);
        ADD_TCASE(tc);
 }
 TEST_MAIN_END