Code

t/unit/: Create one test binary for each *_test.c file.
[sysdb.git] / t / unit / core / store_test.c
index 6715a5b0868a49714b3a9e3664ff586e6455166c..7ea53bcaf76d25bb8efd70e12ea4f13db6dadb8d 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
 #include "core/store.h"
 #include "core/store-private.h"
-#include "libsysdb_test.h"
+#include "testutils.h"
 
 #include <check.h>
 #include <string.h>
+#include <strings.h>
 
 static void
 populate(void)
@@ -38,18 +43,40 @@ 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_metric("h1", "m1", /* store */ NULL, 2);
+       sdb_store_metric("h1", "m2", /* store */ NULL, 1);
+       sdb_store_metric("h2", "m1", /* store */ NULL, 1);
 
        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 = 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;
+       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)
@@ -205,6 +232,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 {
@@ -288,130 +412,186 @@ START_TEST(test_store_service_attr)
 }
 END_TEST
 
-static void
-verify_json_output(sdb_strbuf_t *buf, const char *expected, int flags)
+START_TEST(test_get_field)
 {
-       int pos;
-       size_t len1, len2;
-       size_t i;
+       sdb_store_obj_t *host;
+       sdb_data_t value = SDB_DATA_INIT;
+       int check;
 
-       len1 = strlen(sdb_strbuf_string(buf));
-       len2 = strlen(expected);
+       sdb_store_host("host", 10);
+       sdb_store_host("host", 20);
 
-       pos = -1;
-       if (len1 != len2)
-               pos = (int)(len1 <= len2 ? len1 : len2);
+       host = sdb_store_get_host("host");
+       fail_unless(host != NULL,
+                       "INTERNAL ERROR: store doesn't have host after adding it");
 
-       for (i = 0; i < (len1 <= len2 ? len1 : len2); ++i) {
-               if (sdb_strbuf_string(buf)[i] != expected[i]) {
-                       pos = (int)i;
-                       break;
-               }
-       }
+       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(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");
 
-       fail_unless(pos == -1,
-                       "sdb_store_tojson(%x) returned unexpected result\n"
-                       "         got: %s\n              %*s\n    expected: %s",
-                       flags, sdb_strbuf_string(buf), pos + 1, "^", expected);
-} /* verify_json_output */
+       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>) = "
+                       "%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);
+
+       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
 
-START_TEST(test_store_tojson)
+START_TEST(test_get_child)
 {
-       sdb_strbuf_t *buf;
-       size_t i;
-
        struct {
-               int flags;
-               const char *expected;
+               const char *host;
+               const char *name;
+               int type;
+               int expected;
        } golden_data[] = {
-               { 0, "{\"hosts\":["
-                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", \"backends\": [], "
-                                       "\"attributes\": ["
-                                               "{\"name\": \"k1\", \"value\": \"v1\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []},"
-                                               "{\"name\": \"k2\", \"value\": \"v2\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []},"
-                                               "{\"name\": \"k3\", \"value\": \"v3\", "
-                                                       "\"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\": [], "
-                                       "\"attributes\": [], "
-                                       "\"services\": ["
-                                               "{\"name\": \"s1\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []},"
-                                               "{\"name\": \"s2\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []}"
-                                       "]}"
-                       "]}" },
-               { SDB_SKIP_SERVICES,
-                       "{\"hosts\":["
-                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                       "\"update_interval\": \"0s\", \"backends\": [], "
-                                       "\"attributes\": ["
-                                               "{\"name\": \"k1\", \"value\": \"v1\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []},"
-                                               "{\"name\": \"k2\", \"value\": \"v2\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []},"
-                                               "{\"name\": \"k3\", \"value\": \"v3\", "
-                                                       "\"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\": [], "
-                                       "\"attributes\": []}"
-                       "]}" },
-               { SDB_SKIP_ATTRIBUTES,
-                       "{\"hosts\":["
-                               "{\"name\": \"h1\", \"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\": [], "
-                                       "\"services\": ["
-                                               "{\"name\": \"s1\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []},"
-                                               "{\"name\": \"s2\", "
-                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []}"
-                                       "]}"
-                       "]}" },
-               { 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\": []}"
-                       "]}" },
+               { "h1", NULL, SDB_HOST,       0 },
+               { "h1", NULL, SDB_SERVICE,   -1 },
+               { "h1", NULL, SDB_METRIC,    -1 },
+               { "h1", NULL, SDB_ATTRIBUTE, -1 },
+               { "h2", NULL, SDB_HOST,       0 },
+               { "h2", NULL, SDB_SERVICE,   -1 },
+               { "h2", NULL, SDB_METRIC,    -1 },
+               { "h2", NULL, SDB_ATTRIBUTE, -1 },
+               { "h3", NULL, SDB_HOST,      -1 },
+               { "h1", "k1", SDB_ATTRIBUTE,  0 },
+               { "h1", "x1", SDB_ATTRIBUTE, -1 },
+               { "h2", "k1", SDB_ATTRIBUTE, -1 },
+               { "h1", "k1", SDB_SERVICE,   -1 },
+               { "h1", "k1", SDB_METRIC,    -1 },
+               { "h1", "s1", SDB_SERVICE,   -1 },
+               { "h2", "s1", SDB_SERVICE,    0 },
+               { "h1", "m2", SDB_METRIC,     0 },
+               { "h2", "m2", SDB_METRIC,    -1 },
        };
 
-       buf = sdb_strbuf_create(0);
-       fail_unless(buf != NULL, "INTERNAL ERROR: failed to create string buffer");
+       size_t i;
+
        populate();
 
        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);
+               sdb_store_obj_t *obj;
+               const char *expected_name = golden_data[i].host;
+
+               obj = sdb_store_get_host(golden_data[i].host);
+               if (golden_data[i].expected && (golden_data[i].type == SDB_HOST))
+                       fail_unless(obj == NULL,
+                                       "sdb_store_get_host(%s) = %p; expected: NULL",
+                                       golden_data[i].host, obj);
+               else
+                       fail_unless(obj != NULL,
+                                       "sdb_store_get_host(%s) = NULL; expected: <host>",
+                                       golden_data[i].host);
+
+               if (golden_data[i].type != SDB_HOST) {
+                       sdb_store_obj_t *tmp;
+
+                       expected_name = golden_data[i].name;
+
+                       tmp = sdb_store_get_child(obj,
+                                       golden_data[i].type, golden_data[i].name);
+                       if (golden_data[i].expected)
+                               fail_unless(tmp == NULL,
+                                               "sdb_store_get_child(<%s>, %s, %s) = %p; "
+                                               "expected: NULL", golden_data[i].host,
+                                               SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
+                                               golden_data[i].name, tmp);
+                       else
+                               fail_unless(tmp != NULL,
+                                               "sdb_store_get_child(<%s>, %s, %s) = NULL; "
+                                               "expected: <obj>", golden_data[i].host,
+                                               SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
+                                               golden_data[i].name);
+
+                       sdb_object_deref(SDB_OBJ(obj));
+                       obj = tmp;
+               }
 
-               verify_json_output(buf, golden_data[i].expected, golden_data[i].flags);
+               if (golden_data[i].expected)
+                       continue;
+
+               fail_unless(obj->type == golden_data[i].type,
+                               "sdb_store_get_<%s>(%s, %s) returned object of type %d; "
+                               "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
+                               golden_data[i].host, golden_data[i].name, obj->type,
+                               golden_data[i].type);
+               fail_unless(! strcasecmp(SDB_OBJ(obj)->name, expected_name),
+                               "sdb_store_get_<%s>(%s, %s) returned object named '%s'; "
+                               "expected: '%s'", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
+                               golden_data[i].host, golden_data[i].name, SDB_OBJ(obj)->name,
+                               expected_name);
+
+               sdb_object_deref(SDB_OBJ(obj));
        }
-       sdb_strbuf_destroy(buf);
 }
 END_TEST
 
@@ -431,7 +611,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);
@@ -441,8 +621,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);
@@ -452,107 +632,134 @@ 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));
 }
 END_TEST
 
 static int
-iter_incr(sdb_store_obj_t *obj, void *user_data)
+scan_count(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
 {
        intptr_t *i = user_data;
 
+       if (! sdb_store_matcher_matches(filter, obj, NULL))
+               return 0;
+
        fail_unless(obj != NULL,
-                       "sdb_store_iterate callback received NULL obj; expected: "
+                       "sdb_store_scan callback received NULL obj; expected: "
                        "<store base obj>");
        fail_unless(i != NULL,
-                       "sdb_store_iterate callback received NULL user_data; "
+                       "sdb_store_scan callback received NULL user_data; "
                        "expected: <pointer to data>");
 
        ++(*i);
        return 0;
-} /* iter_incr */
+} /* scan_count */
 
 static int
-iter_error(sdb_store_obj_t *obj, void *user_data)
+scan_error(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
 {
        intptr_t *i = user_data;
 
+       if (! sdb_store_matcher_matches(filter, obj, NULL))
+               return 0;
+
        fail_unless(obj != NULL,
-                       "sdb_store_iterate callback received NULL obj; expected: "
+                       "sdb_store_scan callback received NULL obj; expected: "
                        "<store base obj>");
        fail_unless(i != NULL,
-                       "sdb_store_iterate callback received NULL user_data; "
+                       "sdb_store_scan callback received NULL user_data; "
                        "expected: <pointer to data>");
 
        ++(*i);
        return -1;
-} /* iter_error */
+} /* scan_error */
 
-START_TEST(test_iterate)
+START_TEST(test_scan)
 {
        intptr_t i = 0;
        int check;
 
        /* empty store */
-       check = sdb_store_iterate(iter_incr, &i);
-       fail_unless(check == -1,
-                       "sdb_store_iterate(), empty store = %d; expected: -1", check);
+       check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
+                       scan_count, &i);
+       fail_unless(check == 0,
+                       "sdb_store_scan(HOST), empty store = %d; expected: 0", check);
        fail_unless(i == 0,
-                       "sdb_store_iterate called callback %d times; expected: 0", (int)i);
+                       "sdb_store_scan(HOST) called callback %d times; "
+                       "expected: 0", (int)i);
 
        populate();
 
-       check = sdb_store_iterate(iter_incr, &i);
+       check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
+                       scan_count, &i);
        fail_unless(check == 0,
-                       "sdb_store_iterate() = %d; expected: 0", check);
+                       "sdb_store_scan(HOST) = %d; expected: 0", check);
        fail_unless(i == 2,
-                       "sdb_store_iterate called callback %d times; expected: 1", (int)i);
+                       "sdb_store_scan(HOST) called callback %d times; "
+                       "expected: 1", (int)i);
 
        i = 0;
-       check = sdb_store_iterate(iter_error, &i);
+       check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
+                       scan_error, &i);
        fail_unless(check == -1,
-                       "sdb_store_iterate(), error callback = %d; expected: -1", check);
+                       "sdb_store_scan(HOST), error callback = %d; expected: -1", check);
        fail_unless(i == 1,
-                       "sdb_store_iterate called callback %d times "
+                       "sdb_store_scan(HOST) called callback %d times "
                        "(callback returned error); expected: 1", (int)i);
+
+       i = 0;
+       check = sdb_store_scan(SDB_SERVICE, /* m, filter = */ NULL, NULL,
+                       scan_count, &i);
+       fail_unless(check == 0,
+                       "sdb_store_scan(SERVICE) = %d; expected: 0", check);
+       fail_unless(i == 2,
+                       "sdb_store_scan(SERVICE) called callback %d times; "
+                       "expected: 2", (int)i);
+
+       i = 0;
+       check = sdb_store_scan(SDB_METRIC, /* m, filter = */ NULL, NULL,
+                       scan_count, &i);
+       fail_unless(check == 0,
+                       "sdb_store_scan(METRIC) = %d; expected: 0", check);
+       fail_unless(i == 3,
+                       "sdb_store_scan(METRIC) called callback %d times; "
+                       "expected: 3", (int)i);
 }
 END_TEST
 
-Suite *
-core_store_suite(void)
+TEST_MAIN("core::store")
 {
-       Suite *s = suite_create("core::store");
-       TCase *tc;
-
-       tc = tcase_create("core");
-       tcase_add_test(tc, test_store_tojson);
+       TCase *tc = tcase_create("core");
        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);
+       tcase_add_test(tc, test_get_child);
        tcase_add_test(tc, test_interval);
-       tcase_add_test(tc, test_iterate);
+       tcase_add_test(tc, test_scan);
        tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear);
-       suite_add_tcase(s, tc);
-
-       return s;
-} /* core_store_suite */
+       ADD_TCASE(tc);
+}
+TEST_MAIN_END
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */