index 4bbfcc23864aa17e3a85a3d4e0563a7da4928eb3..89ef451aba32dc285c3a6fd47c5ef61f7c13c782 100644 (file)
sdb_data_t value;
} attrs[] = {
{ "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
+ { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
};
size_t i;
}
} /* populate */
-START_TEST(test_store_match)
+START_TEST(test_store_match_name)
{
- sdb_store_base_t *obj;
+ sdb_store_obj_t *obj;
struct {
- const char *hostname;
- const char *hostname_re;
-
- const char *attr_name;
- const char *attr_value;
+ int type;
+ const char *name;
_Bool re;
int expected;
} golden_data[] = {
- {
- /* host */ NULL, NULL,
- /* attr */ NULL, NULL, 0, 1
- },
- {
- /* host */ NULL, NULL,
- /* attr */ NULL, NULL, 1, 1
- },
- {
- /* host */ "a", NULL,
- /* attr */ NULL, NULL, 0, 1
- },
- {
- /* host */ "b", NULL,
- /* attr */ NULL, NULL, 0, 0
- },
- {
- /* host */ NULL, "^a$",
- /* attr */ NULL, NULL, 0, 1
- },
- {
- /* host */ NULL, "^b$",
- /* attr */ NULL, NULL, 0, 0
- },
- {
- /* host */ "a", "^a$",
- /* attr */ NULL, NULL, 0, 1
- },
- {
- /* host */ "a", "^b$",
- /* attr */ NULL, NULL, 0, 0
- },
- {
- /* host */ "b", "^a$",
- /* attr */ NULL, NULL, 0, 0
- },
- {
- /* host */ "a", "^a$",
- /* attr */ "k1", NULL, 0, 1
- },
- {
- /* host */ "a", "^a$",
- /* attr */ NULL, "v1", 0, 1
- },
- {
- /* host */ "a", "^a$",
- /* attr */ NULL, "^v1$", 1, 1
- },
+ { SDB_HOST, NULL, 0, 1 },
+ { SDB_HOST, NULL, 1, 1 },
+ { SDB_HOST, "a", 0, 1 },
+ { SDB_HOST, "a", 1, 1 },
+ { SDB_HOST, "b", 0, 0 },
+ { SDB_HOST, "b", 1, 0 },
+ { SDB_HOST, "^a$", 1, 1 },
+ { SDB_HOST, "^b$", 1, 0 },
+ { SDB_HOST, "^a$", 0, 0 },
+ { SDB_HOST, "^b$", 0, 0 },
+ { SDB_SERVICE, NULL, 0, 1 },
+ { SDB_SERVICE, NULL, 1, 1 },
+ { SDB_SERVICE, "s1", 0, 1 },
+ { SDB_SERVICE, "s2", 0, 1 },
+ { SDB_SERVICE, "s3", 0, 0 },
+ { SDB_SERVICE, "s4", 0, 0 },
+ { SDB_SERVICE, "^s1$", 1, 1 },
+ { SDB_SERVICE, "^s1$", 0, 0 },
+ { SDB_SERVICE, "x1", 0, 0 },
+ { SDB_SERVICE, "x1", 1, 0 },
+ { SDB_SERVICE, "x", 1, 0 },
+ { SDB_ATTRIBUTE, NULL, 0, 1 },
+ { SDB_ATTRIBUTE, NULL, 1, 1 },
+ { SDB_ATTRIBUTE, "k1", 0, 1 },
+ { SDB_ATTRIBUTE, "k2", 0, 1 },
+ { SDB_ATTRIBUTE, "k3", 0, 0 },
+ { SDB_ATTRIBUTE, "k4", 0, 0 },
+ { SDB_ATTRIBUTE, "k", 1, 1 },
+ { SDB_ATTRIBUTE, "1", 1, 1 },
+ { SDB_ATTRIBUTE, "3", 1, 0 },
};
size_t i;
"sdb_store_get_host(a) = NULL; expected: <host>");
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
- sdb_store_matcher_t *h, *a, *n;
+ sdb_store_matcher_t *m, *n;
char buf[1024];
int status;
- a = sdb_store_attr_matcher(golden_data[i].attr_name,
- golden_data[i].attr_value, golden_data[i].re);
- fail_unless(a != NULL,
- "sdb_store_attr_matcher() = NULL; expected: <matcher>");
-
- h = sdb_store_host_matcher(golden_data[i].hostname,
- golden_data[i].hostname_re, a);
- fail_unless(h != NULL,
- "sdb_store_host_matcher() = NULL: expected: <matcher>");
- /* pass ownership to the host matcher */
- sdb_object_deref(SDB_OBJ(a));
+ m = sdb_store_name_matcher(golden_data[i].type,
+ golden_data[i].name, golden_data[i].re);
+ fail_unless(m != NULL,
+ "sdb_store_service_matcher(%d, %s, %d) = NULL; "
+ "expected: <matcher>", golden_data[i].type,
+ golden_data[i].name, golden_data[i].re);
- status = sdb_store_matcher_matches(h, obj);
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
fail_unless(status == golden_data[i].expected,
- "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
- sdb_store_matcher_tostring(h, buf, sizeof(buf)),
+ "sdb_store_matcher_matches(%s, <host a>, NULL) = %d; "
+ "expected: %d",
+ sdb_store_matcher_tostring(m, buf, sizeof(buf)),
status, golden_data[i].expected);
- n = sdb_store_inv_matcher(h);
+ n = sdb_store_inv_matcher(m);
fail_unless(n != NULL,
"sdb_store_inv_matcher() = NULL; expected: <matcher>");
- sdb_object_deref(SDB_OBJ(h));
+ sdb_object_deref(SDB_OBJ(m));
/* now match the inverted set of objects */
- status = sdb_store_matcher_matches(n, obj);
+ status = sdb_store_matcher_matches(n, obj, /* filter */ NULL);
fail_unless(status == !golden_data[i].expected,
- "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
+ "sdb_store_matcher_matches(%s, <host a>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(n, buf, sizeof(buf)),
status, !golden_data[i].expected);
}
END_TEST
-START_TEST(test_store_match_name)
+START_TEST(test_store_match_attr)
{
- sdb_store_base_t *obj;
+ sdb_store_obj_t *obj;
struct {
- int type;
- const char *name;
+ const char *attr_name;
+ const char *attr_value;
_Bool re;
int expected;
} golden_data[] = {
- { SDB_HOST, NULL, 0, 1 },
- { SDB_HOST, NULL, 1, 1 },
- { SDB_HOST, "a", 0, 1 },
- { SDB_HOST, "a", 1, 1 },
- { SDB_HOST, "b", 0, 0 },
- { SDB_HOST, "b", 1, 0 },
- { SDB_HOST, "^a$", 1, 1 },
- { SDB_HOST, "^b$", 1, 0 },
- { SDB_HOST, "^a$", 0, 0 },
- { SDB_HOST, "^b$", 0, 0 },
- { SDB_SERVICE, NULL, 0, 1 },
- { SDB_SERVICE, NULL, 1, 1 },
- { SDB_SERVICE, "s1", 0, 1 },
- { SDB_SERVICE, "s2", 0, 1 },
- { SDB_SERVICE, "s3", 0, 0 },
- { SDB_SERVICE, "s4", 0, 0 },
- { SDB_SERVICE, "^s1$", 1, 1 },
- { SDB_SERVICE, "^s1$", 0, 0 },
- { SDB_SERVICE, "x1", 0, 0 },
- { SDB_SERVICE, "x1", 1, 0 },
- { SDB_SERVICE, "x", 1, 0 },
- { SDB_ATTRIBUTE, NULL, 0, 1 },
- { SDB_ATTRIBUTE, NULL, 1, 1 },
- { SDB_ATTRIBUTE, "k1", 0, 1 },
- { SDB_ATTRIBUTE, "k2", 0, 0 },
- { SDB_ATTRIBUTE, "k3", 0, 0 },
- { SDB_ATTRIBUTE, "k", 1, 1 },
- { SDB_ATTRIBUTE, "1", 1, 1 },
- { SDB_ATTRIBUTE, "2", 1, 0 },
+ { "k1", NULL, 0, 1 },
+ { "k", NULL, 1, 0 },
+ { "1", NULL, 1, 0 },
+ { "k3", NULL, 0, 0 },
+ { "k3", NULL, 1, 0 },
+ { "k1", "v1", 0, 1 },
+ { "k1", "v1", 1, 1 },
+ { "k1", "^v1$", 1, 1 },
+ { "k1", "v", 1, 1 },
+ { "k1", "1", 1, 1 },
+ { "k1", "v2", 0, 0 },
+ { "k1", "v2", 1, 0 },
+ { "k", "v1", 0, 0 },
+ { "k", "v1", 1, 0 },
+ { "k3", "1", 0, 0 },
+ { "k3", "1", 1, 0 },
};
size_t i;
fail_unless(obj != NULL,
"sdb_store_get_host(a) = NULL; expected: <host>");
+ fail_unless(sdb_store_attr_matcher(NULL, "re", 0) == NULL,
+ "sdb_store_attr_matcher(NULL, \"re\", 0) = <m>; expected: NULL");
+ fail_unless(sdb_store_attr_matcher(NULL, "re", 1) == NULL,
+ "sdb_store_attr_matcher(NULL, \"re\", 1) = <m>; expected: NULL");
+
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
sdb_store_matcher_t *m, *n;
char buf[1024];
int status;
- m = sdb_store_name_matcher(golden_data[i].type,
- golden_data[i].name, golden_data[i].re);
+ m = sdb_store_attr_matcher(golden_data[i].attr_name,
+ golden_data[i].attr_value, golden_data[i].re);
fail_unless(m != NULL,
- "sdb_store_service_matcher(%d, %s, %d) = NULL; expected: <matcher>",
- golden_data[i].type, golden_data[i].name, golden_data[i].re);
+ "sdb_store_attr_matcher() = NULL; expected: <matcher>");
- status = sdb_store_matcher_matches(m, obj);
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
fail_unless(status == golden_data[i].expected,
- "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
+ "sdb_store_matcher_matches({%s, <host a>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(m, buf, sizeof(buf)),
status, golden_data[i].expected);
sdb_object_deref(SDB_OBJ(m));
/* now match the inverted set of objects */
- status = sdb_store_matcher_matches(n, obj);
+ status = sdb_store_matcher_matches(n, obj, /* filter */ NULL);
fail_unless(status == !golden_data[i].expected,
- "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
+ "sdb_store_matcher_matches({%s, <host a>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(n, buf, sizeof(buf)),
status, !golden_data[i].expected);
}
END_TEST
+START_TEST(test_attr_cond)
+{
+ sdb_store_obj_t *obj;
+
+ struct {
+ const char *attr;
+ const sdb_data_t value;
+ int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
+ } golden_data[] = {
+ { "k1", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 1, 1, 1, 0 },
+ { "k1", { SDB_TYPE_STRING, { .string = "v2" } }, 1, 1, 0, 0, 0 },
+ { "k1", { SDB_TYPE_STRING, { .string = "v0" } }, 0, 0, 0, 1, 1 },
+ { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 1, 1, 1, 0 },
+ { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } }, 1, 1, 0, 0, 0 },
+ { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } }, 0, 0, 0, 1, 1 },
+ /* key does not exist */
+ { "k3", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 0, 0, 0, 0 },
+ { "k3", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
+ { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 0, 0, 0, 0 },
+ /* type mismatch */
+ { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+ { "k2", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
+ };
+
+ int status;
+ size_t i;
+
+ obj = sdb_store_get_host("a");
+ fail_unless(obj != NULL,
+ "sdb_store_get_host(a) = NULL; expected: <host>");
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_store_expr_t *expr;
+ sdb_store_cond_t *c;
+ char buf[1024];
+ size_t j;
+
+ struct {
+ sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
+ int expected;
+ } tests[] = {
+ { sdb_store_lt_matcher, golden_data[i].expected_lt },
+ { sdb_store_le_matcher, golden_data[i].expected_le },
+ { sdb_store_eq_matcher, golden_data[i].expected_eq },
+ { sdb_store_ge_matcher, golden_data[i].expected_ge },
+ { sdb_store_gt_matcher, golden_data[i].expected_gt },
+ };
+
+ sdb_data_format(&golden_data[i].value,
+ buf, sizeof(buf), SDB_UNQUOTED);
+
+ expr = sdb_store_expr_constvalue(&golden_data[i].value);
+ fail_unless(expr != NULL,
+ "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
+ buf);
+
+ c = sdb_store_attr_cond(golden_data[i].attr, expr);
+ sdb_object_deref(SDB_OBJ(expr));
+ fail_unless(c != NULL,
+ "sdb_store_attr_cond(%s, expr{%s}) = NULL; expected: <cond>",
+ golden_data[i].attr, buf);
+
+ for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
+ sdb_store_matcher_t *m;
+ char m_str[1024];
+
+ m = tests[j].matcher(c);
+ fail_unless(m != NULL,
+ "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
+
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
+ fail_unless(status == tests[j].expected,
+ "sdb_store_matcher_matches(%s, <obj>, NULL) = %d; "
+ "expected: %d",
+ sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
+ status, tests[j].expected);
+
+ sdb_object_deref(SDB_OBJ(m));
+ }
+
+ sdb_object_deref(SDB_OBJ(c));
+ }
+
+ sdb_object_deref(SDB_OBJ(obj));
+}
+END_TEST
+
+START_TEST(test_obj_cond)
+{
+ struct {
+ const char *host;
+ int field;
+ const sdb_data_t value;
+ int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
+ } golden_data[] = {
+ /* last-update = 1 for all objects */
+ { "a", SDB_FIELD_LAST_UPDATE,
+ { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 },
+ { "a", SDB_FIELD_LAST_UPDATE,
+ { SDB_TYPE_DATETIME, { .datetime = 2 } }, 1, 1, 0, 0, 0 },
+ { "a", SDB_FIELD_LAST_UPDATE,
+ { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
+ /* age > 0 for all objects */
+ { "a", SDB_FIELD_AGE,
+ { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
+ /* interval = 0 for all objects */
+ { "a", SDB_FIELD_INTERVAL,
+ { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 1, 1, 1, 0 },
+ { "a", SDB_FIELD_INTERVAL,
+ { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
+ /* type mismatch */
+ { "a", SDB_FIELD_LAST_UPDATE,
+ { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+ { "a", SDB_FIELD_AGE,
+ { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+ { "a", SDB_FIELD_INTERVAL,
+ { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+ { "a", SDB_FIELD_BACKEND,
+ { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+ };
+
+ int status;
+ size_t i;
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_store_obj_t *obj;
+ sdb_store_expr_t *expr;
+ sdb_store_cond_t *c;
+ char buf[1024];
+ size_t j;
+
+ struct {
+ sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
+ int expected;
+ } tests[] = {
+ { sdb_store_lt_matcher, golden_data[i].expected_lt },
+ { sdb_store_le_matcher, golden_data[i].expected_le },
+ { sdb_store_eq_matcher, golden_data[i].expected_eq },
+ { sdb_store_ge_matcher, golden_data[i].expected_ge },
+ { sdb_store_gt_matcher, golden_data[i].expected_gt },
+ };
+
+ obj = sdb_store_get_host(golden_data[i].host);
+ fail_unless(obj != NULL,
+ "sdb_store_get_host(%s) = NULL; expected: <host>",
+ golden_data[i].host);
+
+ sdb_data_format(&golden_data[i].value,
+ buf, sizeof(buf), SDB_UNQUOTED);
+
+ expr = sdb_store_expr_constvalue(&golden_data[i].value);
+ fail_unless(expr != NULL,
+ "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
+ buf);
+
+ c = sdb_store_obj_cond(golden_data[i].field, expr);
+ sdb_object_deref(SDB_OBJ(expr));
+ fail_unless(c != NULL,
+ "sdb_store_obj_cond(%d, expr{%s}) = NULL; expected: <cond>",
+ golden_data[i].field, buf);
+
+ for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
+ sdb_store_matcher_t *m;
+ char m_str[1024];
+
+ m = tests[j].matcher(c);
+ fail_unless(m != NULL,
+ "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
+
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
+ fail_unless(status == tests[j].expected,
+ "sdb_store_matcher_matches(%s, <obj>, NULL) = %d; "
+ "expected: %d",
+ sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
+ status, tests[j].expected);
+
+ sdb_object_deref(SDB_OBJ(m));
+ }
+
+ sdb_object_deref(SDB_OBJ(c));
+ sdb_object_deref(SDB_OBJ(obj));
+ }
+}
+END_TEST
+
START_TEST(test_store_match_op)
{
- sdb_store_base_t *obj;
+ sdb_store_obj_t *obj;
- sdb_store_matcher_t *always = sdb_store_host_matcher(NULL, NULL, NULL);
- sdb_store_matcher_t *never = sdb_store_host_matcher("a", "b", NULL);
+ sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
+ sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
struct {
const char *op;
obj = sdb_store_get_host("a");
- status = sdb_store_matcher_matches(always, obj);
+ status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
fail_unless(status == 1,
"INTERNAL ERROR: 'always' did not match host");
- status = sdb_store_matcher_matches(never, obj);
+ status = sdb_store_matcher_matches(never, obj, /* filter */ NULL);
fail_unless(status == 0,
"INTERNAL ERROR: 'never' matches host");
#define TO_NAME(v) (((v) == always) ? "always" \
: ((v) == never) ? "never" : "<unknown>")
- status = sdb_store_matcher_matches(m, obj);
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
fail_unless(status == golden_data[i].expected,
- "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
+ "%s(%s, %s, NULL) = %d; expected: %d", golden_data[i].op,
TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
status, golden_data[i].expected);
START_TEST(test_parse_cmp)
{
+ sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
+ sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } };
+ sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
+
sdb_store_matcher_t *check;
size_t i;
const char *obj_type;
const char *attr;
const char *op;
- const char *value;
+ const sdb_data_t *value;
int expected;
} golden_data[] = {
- { "host", "name", "=", "hostname", MATCHER_NAME },
- { "host", "name", "!=", "hostname", MATCHER_NOT },
- { "host", "name", "=~", "hostname", MATCHER_NAME },
- { "host", "name", "!~", "hostname", MATCHER_NOT },
- { "host", "attr", "=", "hostname", -1 },
- { "host", "attr", "!=", "hostname", -1 },
- { "host", "name", "&^", "hostname", -1 },
- { "service", "name", "=", "srvname", MATCHER_NAME },
- { "service", "name", "!=", "srvname", MATCHER_NOT },
- { "service", "name", "=~", "srvname", MATCHER_NAME },
- { "service", "name", "!~", "srvname", MATCHER_NOT },
- { "service", "attr", "=", "srvname", -1 },
- { "service", "attr", "!=", "srvname", -1 },
- { "service", "name", "&^", "srvname", -1 },
- { "attribute", "name", "=", "attrname", MATCHER_NAME },
- { "attribute", "name", "!=", "attrname", MATCHER_NOT },
- { "attribute", "name", "=~", "attrname", MATCHER_NAME },
- { "attribute", "name", "!~", "attrname", MATCHER_NOT },
- { "attribute", "attr", "=", "attrname", MATCHER_ATTR },
- { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
- { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
- { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
- { "attribute", "attr", "&^", "attrname", -1 },
+ { "host", NULL, "=", &hostname, MATCHER_NAME },
+ { "host", NULL, "!=", &hostname, MATCHER_NOT },
+ { "host", NULL, "=~", &hostname, MATCHER_NAME },
+ { "host", NULL, "!~", &hostname, MATCHER_NOT },
+ { "host", "attr", "=", &hostname, -1 },
+ { "host", "attr", "!=", &hostname, -1 },
+ { "host", NULL, "&^", &hostname, -1 },
+ { "host", NULL, "<", &hostname, -1 },
+ { "host", NULL, "<=", &hostname, -1 },
+ { "host", NULL, ">=", &hostname, -1 },
+ { "host", NULL, ">", &hostname, -1 },
+ { "host", NULL, "=", NULL, -1 },
+ { "service", NULL, "=", &srvname, MATCHER_NAME },
+ { "service", NULL, "!=", &srvname, MATCHER_NOT },
+ { "service", NULL, "=~", &srvname, MATCHER_NAME },
+ { "service", NULL, "!~", &srvname, MATCHER_NOT },
+ { "service", "attr", "=", &srvname, -1 },
+ { "service", "attr", "!=", &srvname, -1 },
+ { "service", NULL, "&^", &srvname, -1 },
+ { "service", NULL, "<", &srvname, -1 },
+ { "service", NULL, "<=", &srvname, -1 },
+ { "service", NULL, ">=", &srvname, -1 },
+ { "service", NULL, ">", &srvname, -1 },
+ { "service", NULL, "=", NULL, -1 },
+ { "attribute", NULL, "=", &attrname, MATCHER_NAME },
+ { "attribute", NULL, "!=", &attrname, MATCHER_NOT },
+ { "attribute", NULL, "=~", &attrname, MATCHER_NAME },
+ { "attribute", NULL, "!~", &attrname, MATCHER_NOT },
+ { "attribute", NULL, "<", &attrname, -1 },
+ { "attribute", NULL, "<=", &attrname, -1 },
+ { "attribute", NULL, ">=", &attrname, -1 },
+ { "attribute", NULL, ">", &attrname, -1 },
+ { "attribute", NULL, "=", NULL, -1 },
+ { "attribute", "attr", "=", &attrname, MATCHER_ATTR },
+ { "attribute", "attr", "!=", &attrname, MATCHER_NOT },
+ { "attribute", "attr", "=~", &attrname, MATCHER_ATTR },
+ { "attribute", "attr", "!~", &attrname, MATCHER_NOT },
+ { "attribute", "attr", "&^", &attrname, -1 },
+ { "attribute", "attr", "<", NULL, -1 },
+ { "attribute", "attr", "<", &attrname, MATCHER_LT },
+ { "attribute", "attr", "<=", &attrname, MATCHER_LE },
+/* { "attribute", "attr", "=", &attrname, MATCHER_EQ }, */
+ { "attribute", "attr", ">=", &attrname, MATCHER_GE },
+ { "attribute", "attr", ">", &attrname, MATCHER_GT },
+ { "attribute", "attr", "IS", NULL, MATCHER_ISNULL },
+ { "attribute", "attr", "IS", &attrname, -1 },
+ { "foo", NULL, "=", &attrname, -1 },
+ { "foo", "attr", "=", &attrname, -1 },
};
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_store_expr_t *expr;
+ char buf[1024];
+
+ if (sdb_data_format(golden_data[i].value,
+ buf, sizeof(buf), SDB_UNQUOTED) < 0)
+ snprintf(buf, sizeof(buf), "ERR");
+
+ expr = sdb_store_expr_constvalue(golden_data[i].value);
+ fail_unless(expr != NULL || golden_data[i].value == NULL,
+ "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
+ buf);
+
check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op, golden_data[i].value);
+ golden_data[i].attr, golden_data[i].op, expr);
+ sdb_object_deref(SDB_OBJ(expr));
if (golden_data[i].expected == -1) {
fail_unless(check == NULL,
- "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
+ "sdb_store_matcher_parse_cmp(%s, %s, %s, expr{%s}) = %p; "
"expected: NULL", golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op,
- golden_data[i].value, check);
+ golden_data[i].attr, golden_data[i].op, buf, check);
continue;
}
fail_unless(check != NULL,
"sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
"expected: NULL", golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op,
- golden_data[i].value, check);
+ golden_data[i].attr, golden_data[i].op, buf, check);
fail_unless(M(check)->type == golden_data[i].expected,
"sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
"of type %d; expected: %d", golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op, golden_data[i].value,
+ golden_data[i].attr, golden_data[i].op, buf,
M(check)->type, golden_data[i].expected);
sdb_object_deref(SDB_OBJ(check));
END_TEST
static int
-lookup_cb(sdb_store_base_t *obj, void *user_data)
+scan_cb(sdb_store_obj_t *obj, void *user_data)
{
int *i = user_data;
fail_unless(obj != NULL,
- "sdb_store_lookup callback received NULL obj; expected: "
+ "sdb_store_scan callback received NULL obj; expected: "
"<store base obj>");
fail_unless(i != NULL,
- "sdb_store_lookup callback received NULL user_data; "
+ "sdb_store_scan callback received NULL user_data; "
"expected: <pointer to data>");
++(*i);
return 0;
-} /* lookup_cb */
+} /* scan_cb */
-START_TEST(test_lookup)
+START_TEST(test_scan)
{
#define PTR_RE "0x[0-9a-f]+"
struct {
const char *query;
+ const char *filter;
int expected;
const char *tostring_re;
} golden_data[] = {
- { "host.name = 'a'", 1,
+ { "host = 'a'", NULL, 1,
+ "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
+ { "host = 'a'", "host = 'x'", 0, /* filter never matches */
+ "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
+ { "host = 'a'",
+ "NOT attribute.x = ''", 1, /* filter always matches */
"OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
- { "host.name =~ 'a|b'", 2,
+ { "host =~ 'a|b'", NULL, 2,
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
- { "host.name =~ 'host'", 0,
+ { "host =~ 'host'", NULL, 0,
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
- { "host.name =~ '.'", 3,
+ { "host =~ '.'", NULL, 3,
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
- { "service.name = 's1'", 2,
+ { "service = 's1'", NULL, 2,
"OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
- { "service.name =~ 's'", 2,
+ { "service = 's1'", "host = 'x'", 0, /* filter never matches */
+ "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
+ { "service = 's1'",
+ "NOT attribute.x = ''", 2, /* filter always matches */
+ "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
+ { "service =~ 's'", NULL, 2,
"OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
- { "service.name !~ 's'", 1,
+ { "service !~ 's'", NULL, 1,
"\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
- { "attribute.name = 'k1'", 1,
+ { "attribute = 'k1'", NULL, 1,
+ "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
+ { "attribute = 'k1'", "host = 'x'", 0, /* filter never matches */
+ "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
+ { "attribute = 'k1'",
+ "NOT attribute.x = ''", 1, /* filter always matches */
"OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
- { "attribute.name = 'x'", 0,
+ { "attribute = 'x'", NULL, 0,
"OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
- { "attribute.k1 = 'v1'", 1,
+ { "attribute.k1 = 'v1'", NULL, 1,
"ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
- { "attribute.k1 != 'v1'", 2,
+ { "attribute.k1 IS NULL", NULL, 2,
+ "\\(IS NULL, ATTR\\[k1\\]\\)" },
+ { "attribute.x1 IS NULL", NULL, 3,
+ "\\(IS NULL, ATTR\\[x1\\]\\)" },
+ { "attribute.k1 IS NOT NULL", NULL, 1,
+ "\\(NOT, \\(IS NULL, ATTR\\[k1\\]\\)\\)" },
+ { "attribute.x1 IS NOT NULL", NULL, 0,
+ "\\(NOT, \\(IS NULL, ATTR\\[x1\\]\\)\\)" },
+ { "attribute.k2 < 123", NULL, 0,
+ "ATTR\\[k2\\]\\{ < 123 \\}" },
+ { "attribute.k2 <= 123", NULL, 1,
+ "ATTR\\[k2\\]\\{ <= 123 \\}" },
+ { "attribute.k2 >= 123", NULL, 1,
+ "ATTR\\[k2\\]\\{ >= 123 \\}" },
+ { "attribute.k2 > 123", NULL, 0,
+ "ATTR\\[k2\\]\\{ > 123 \\}" },
+ { "attribute.k2 = 123", NULL, 1,
+ "ATTR\\[k2\\]\\{ = 123 \\}" },
+ { "attribute.k2 != 123", NULL, 2,
+ "\\(NOT, ATTR\\[k2\\]\\{ = 123 \\}\\)" },
+ { "attribute.k1 != 'v1'", NULL, 2,
"\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
- { "attribute.k1 != 'v2'", 3,
+ { "attribute.k1 != 'v2'", NULL, 3,
"\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
- { "attribute.name != 'x' "
- "AND attribute.y !~ 'x'", 3,
+ { "attribute != 'x' "
+ "AND attribute.y !~ 'x'", NULL, 3,
"\\(AND, "
"\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
"\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
size_t i;
n = 0;
- check = sdb_store_lookup(NULL, lookup_cb, &n);
+ check = sdb_store_scan(/* matcher */ NULL, /* filter */ NULL,
+ scan_cb, &n);
fail_unless(check == 0,
- "sdb_store_lookup() = %d; expected: 0", check);
+ "sdb_store_scan() = %d; expected: 0", check);
fail_unless(n == 3,
- "sdb_store_lookup called callback %d times; expected: 3", (int)n);
+ "sdb_store_scan called callback %d times; expected: 3", (int)n);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
- sdb_store_matcher_t *m;
+ sdb_store_matcher_t *m, *filter = NULL;
char buf[4096];
m = sdb_fe_parse_matcher(golden_data[i].query, -1);
fail_unless(m != NULL,
"sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
golden_data[i].query);
+
+ if (golden_data[i].filter) {
+ filter = sdb_fe_parse_matcher(golden_data[i].filter, -1);
+ fail_unless(filter != NULL,
+ "sdb_fe_parse_matcher(%s, -1) = NULL; "
+ "expected: <matcher>", golden_data[i].filter);
+ }
+
fail_unless(sdb_regmatches(golden_data[i].tostring_re,
sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
"sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
golden_data[i].tostring_re);
n = 0;
- sdb_store_lookup(m, lookup_cb, &n);
+ sdb_store_scan(m, filter, scan_cb, &n);
fail_unless(n == golden_data[i].expected,
- "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
- golden_data[i].query, n, golden_data[i].expected);
+ "sdb_store_scan(matcher{%s}, filter{%s}) found %d hosts; "
+ "expected: %d", golden_data[i].query, golden_data[i].filter,
+ n, golden_data[i].expected);
+ sdb_object_deref(SDB_OBJ(filter));
sdb_object_deref(SDB_OBJ(m));
}
}
tc = tcase_create("core");
tcase_add_checked_fixture(tc, populate, sdb_store_clear);
- tcase_add_test(tc, test_store_match);
tcase_add_test(tc, test_store_match_name);
+ tcase_add_test(tc, test_store_match_attr);
+ tcase_add_test(tc, test_attr_cond);
+ tcase_add_test(tc, test_obj_cond);
tcase_add_test(tc, test_store_match_op);
tcase_add_test(tc, test_parse_cmp);
- tcase_add_test(tc, test_lookup);
+ tcase_add_test(tc, test_scan);
suite_add_tcase(s, tc);
return s;