X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=t%2Funit%2Fcore%2Fstore_json_test.c;h=ea33ea8416b085a3dbac9eb1b3685ba2540a62a1;hp=f8ebac913633fe025df01344c2e2ecab521874cc;hb=552e172997899745a55b081a3198ba1c6b85a765;hpb=90174606fdfac1dba1330b48cb16bb2ca1efb0a4 diff --git a/t/unit/core/store_json_test.c b/t/unit/core/store_json_test.c index f8ebac9..ea33ea8 100644 --- a/t/unit/core/store_json_test.c +++ b/t/unit/core/store_json_test.c @@ -29,6 +29,7 @@ # include "config.h" #endif +#include "core/memstore.h" #include "core/store.h" #include "testutils.h" @@ -39,55 +40,53 @@ #undef SDB_INTERVAL_SECOND #define SDB_INTERVAL_SECOND 1000000000L -static sdb_store_t *store; +static sdb_memstore_t *store; static void populate(void) { sdb_data_t datum; - store = sdb_store_create(); + store = sdb_memstore_create(); ck_assert(store != NULL); - sdb_store_host(store, "h1", 1 * SDB_INTERVAL_SECOND); - sdb_store_host(store, "h2", 3 * SDB_INTERVAL_SECOND); + sdb_memstore_host(store, "h1", 1 * SDB_INTERVAL_SECOND, 0); + sdb_memstore_host(store, "h2", 3 * SDB_INTERVAL_SECOND, 0); datum.type = SDB_TYPE_STRING; datum.data.string = "v1"; - sdb_store_attribute(store, "h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND); + sdb_memstore_attribute(store, "h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND, 0); datum.data.string = "v2"; - sdb_store_attribute(store, "h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND); + sdb_memstore_attribute(store, "h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND, 0); datum.data.string = "v3"; - sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); + sdb_memstore_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND, 0); +/* TODO: move these tests into generic store tests */ +#if 0 /* make sure that older updates don't overwrite existing values */ datum.data.string = "fail"; - sdb_store_attribute(store, "h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND); - sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); + sdb_memstore_attribute(store, "h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND, 0); + sdb_memstore_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND, 0); +#endif - 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_memstore_metric(store, "h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND, 0); + sdb_memstore_metric(store, "h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND, 0); + sdb_memstore_metric(store, "h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND, 0); - sdb_store_service(store, "h2", "s1", 1 * SDB_INTERVAL_SECOND); - sdb_store_service(store, "h2", "s2", 2 * SDB_INTERVAL_SECOND); + sdb_memstore_service(store, "h2", "s1", 1 * SDB_INTERVAL_SECOND, 0); + sdb_memstore_service(store, "h2", "s2", 2 * SDB_INTERVAL_SECOND, 0); datum.type = SDB_TYPE_INTEGER; datum.data.integer = 42; - sdb_store_metric_attr(store, "h1", "m1", "k3", - &datum, 2 * SDB_INTERVAL_SECOND); + sdb_memstore_metric_attr(store, "h1", "m1", "k3", + &datum, 2 * SDB_INTERVAL_SECOND, 0); datum.data.integer = 123; - sdb_store_service_attr(store, "h2", "s2", "k1", - &datum, 2 * SDB_INTERVAL_SECOND); + sdb_memstore_service_attr(store, "h2", "s2", "k1", + &datum, 2 * SDB_INTERVAL_SECOND, 0); datum.data.integer = 4711; - 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(store, "h2", "s2", "k1", - &datum, 2 * SDB_INTERVAL_SECOND); + sdb_memstore_service_attr(store, "h2", "s2", "k2", + &datum, 1 * SDB_INTERVAL_SECOND, 0); } /* populate */ static void @@ -98,56 +97,35 @@ turndown(void) } /* turndown */ static int -scan_tojson(sdb_store_obj_t *obj, - sdb_store_matcher_t __attribute__((unused)) *filter, +scan_tojson(sdb_memstore_obj_t *obj, + sdb_memstore_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_memstore_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, +scan_tojson_full(sdb_memstore_obj_t *obj, sdb_memstore_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_memstore_emit_full(obj, filter, &sdb_store_json_writer, user_data); } /* scan_tojson_full */ static void verify_json_output(sdb_strbuf_t *buf, const char *expected) { - const char *got = sdb_strbuf_string(buf); - size_t len1 = strlen(got); - size_t len2 = strlen(expected); - - size_t i; - int pos = -1; - - if (len1 != len2) - pos = (int)SDB_MIN(len1, len2); - - for (i = 0; i < SDB_MIN(len1, len2); ++i) { - if (got[i] != expected[i]) { - pos = (int)i; - break; - } - } - - fail_unless(pos == -1, - "Serializing hosts to JSON returned unexpected result\n" - " got: %s\n %*s\n expected: %s", - got, pos + 1, "^", expected); + sdb_diff_strings("Serializing hosts to JSON returned unexpected result", + sdb_strbuf_string(buf), expected); } /* verify_json_output */ struct { struct { - sdb_store_matcher_t *(*m)(sdb_store_expr_t *, sdb_store_expr_t *); + sdb_memstore_matcher_t *(*m)(sdb_memstore_expr_t *, sdb_memstore_expr_t *); int field; sdb_data_t value; } filter; int type; - int (*f)(sdb_store_obj_t *, sdb_store_matcher_t *, void *); + int (*f)(sdb_memstore_obj_t *, sdb_memstore_matcher_t *, void *); const char *expected; } store_tojson_data[] = { { { NULL, 0, SDB_DATA_INIT }, @@ -214,14 +192,14 @@ struct { "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" "]" }, - { { sdb_store_eq_matcher, SDB_FIELD_NAME, + { { sdb_memstore_eq_matcher, SDB_FIELD_NAME, { SDB_TYPE_STRING, { .string = "h1" } } }, SDB_HOST, scan_tojson_full, "[" "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" "]" }, - { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE, + { { sdb_memstore_gt_matcher, SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } }, SDB_HOST, scan_tojson_full, "[" @@ -238,7 +216,7 @@ struct { "]}" "]}" "]" }, - { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE, + { { sdb_memstore_le_matcher, SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } }, SDB_HOST, scan_tojson_full, "[" @@ -256,14 +234,14 @@ struct { "\"update_interval\": \"0s\", \"backends\": []}" "]}" "]" }, - { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE, + { { sdb_memstore_ge_matcher, SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 3 * SDB_INTERVAL_SECOND } } }, SDB_HOST, scan_tojson_full, "[" "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" "]" }, - { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE, + { { sdb_memstore_lt_matcher, SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 0 } } }, SDB_HOST, scan_tojson_full, "[]" }, @@ -271,127 +249,95 @@ 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\": []}," - "{\"name\": \"s2\", " + "\"attributes\": [" + "{\"name\": \"k1\", \"value\": 123, " "\"last_update\": \"1970-01-01 00:00:02 +0000\", " - "\"update_interval\": \"0s\", \"backends\": [], " - "\"attributes\": [" - "{\"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_memstore_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\": \"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, + { { sdb_memstore_lt_matcher, SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 0 } } }, SDB_SERVICE, scan_tojson_full, "[]" }, { { 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\": \"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\": []}" "]}," - "{\"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\": \"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_memstore_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\": []}" - "]}" + "{\"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_memstore_lt_matcher, SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 0 } } }, SDB_METRIC, scan_tojson_full, "[]" }, @@ -400,38 +346,37 @@ struct { START_TEST(test_store_tojson) { sdb_strbuf_t *buf = sdb_strbuf_create(0); - sdb_store_matcher_t *filter = NULL; + sdb_memstore_matcher_t *filter = NULL; sdb_store_json_formatter_t *f; int status; if (store_tojson_data[_i].filter.m) { - sdb_store_expr_t *field; - sdb_store_expr_t *value; + sdb_memstore_expr_t *field; + sdb_memstore_expr_t *value; - field = sdb_store_expr_fieldvalue(store_tojson_data[_i].filter.field); + field = sdb_memstore_expr_fieldvalue(store_tojson_data[_i].filter.field); fail_unless(field != NULL, - "INTERNAL ERROR: sdb_store_expr_fieldvalue() = NULL"); - value = sdb_store_expr_constvalue(&store_tojson_data[_i].filter.value); + "INTERNAL ERROR: sdb_memstore_expr_fieldvalue() = NULL"); + value = sdb_memstore_expr_constvalue(&store_tojson_data[_i].filter.value); fail_unless(value != NULL, - "INTERNAL ERROR: sdb_store_expr_constvalue() = NULL"); + "INTERNAL ERROR: sdb_memstore_expr_constvalue() = NULL"); filter = store_tojson_data[_i].filter.m(field, value); fail_unless(filter != NULL, - "INTERNAL ERROR: sdb_store_*_matcher() = NULL"); + "INTERNAL ERROR: sdb_memstore_*_matcher() = NULL"); sdb_object_deref(SDB_OBJ(field)); sdb_object_deref(SDB_OBJ(value)); } 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, store_tojson_data[_i].type, + status = sdb_memstore_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", + "sdb_memstore_scan(HOST, ..., tojson) = %d; expected: 0", status); sdb_store_json_finish(f);