index c6eeb6bc3ae4d83b2cc6c323063795c5fe04083d..82688833f2f1e1c3fa76cbc12a72bcac12f89d04 100644 (file)
}
END_TEST
-START_TEST(test_store_cond)
+START_TEST(test_attr_cond)
{
sdb_store_obj_t *obj;
struct {
sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
- int *expected;
+ 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_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,
"sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
- fail_unless(status == *tests[j].expected,
+ 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);
+ status, tests[j].expected);
sdb_object_deref(SDB_OBJ(m));
}
}
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_obj_t *obj;
}
END_TEST
+START_TEST(test_parse_field_cmp)
+{
+ sdb_data_t datetime = { SDB_TYPE_DATETIME, { .datetime = 1 } };
+ sdb_data_t string = { SDB_TYPE_STRING, { .string = "s" } };
+
+ struct {
+ const char *field;
+ const char *op;
+ const sdb_data_t *value;
+ int expected;
+ } golden_data[] = {
+ { "last_update", "<", &datetime, MATCHER_LT },
+ { "last_update", "<=", &datetime, MATCHER_LE },
+ { "last_update", "=", &datetime, MATCHER_EQ },
+ { "last_update", ">=", &datetime, MATCHER_GE },
+ { "last_update", ">", &datetime, MATCHER_GT },
+ { "last_update", "!=", &datetime, MATCHER_NOT },
+ { "age", "<", &datetime, MATCHER_LT },
+ { "age", "<=", &datetime, MATCHER_LE },
+ { "age", "=", &datetime, MATCHER_EQ },
+ { "age", ">=", &datetime, MATCHER_GE },
+ { "age", ">", &datetime, MATCHER_GT },
+ { "age", "!=", &datetime, MATCHER_NOT },
+ { "interval", "<", &datetime, MATCHER_LT },
+ { "interval", "<=", &datetime, MATCHER_LE },
+ { "interval", "=", &datetime, MATCHER_EQ },
+ { "interval", ">=", &datetime, MATCHER_GE },
+ { "interval", ">", &datetime, MATCHER_GT },
+ { "interval", "!=", &datetime, MATCHER_NOT },
+ { "backend", "=", &string, MATCHER_EQ },
+ { "backend", "!=", &string, MATCHER_NOT },
+ /* the behavior for other operators on :backend
+ * is currently unspecified */
+ { "last_update", "=", NULL, -1 },
+ { "last_update", "IS", NULL, -1 },
+ { "age", "=", NULL, -1 },
+ { "interval", "=", NULL, -1 },
+ { "backend", "=", NULL, -1 },
+ { "backend", "=~", &string, -1 },
+ };
+
+ size_t i;
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_store_matcher_t *check;
+ 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_field_cmp(golden_data[i].field,
+ 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_field_cmp(%s, %s, expr{%s}) = %p; "
+ "expected: NULL", golden_data[i].field,
+ golden_data[i].op, buf, check);
+ continue;
+ }
+
+ fail_unless(check != NULL,
+ "sdb_store_matcher_parse_field_cmp(%s, %s, %s) = %p; "
+ "expected: NULL", golden_data[i].field,
+ golden_data[i].op, buf, check);
+ fail_unless(M(check)->type == golden_data[i].expected,
+ "sdb_store_matcher_parse_field_cmp(%s, %s, %s) returned "
+ "matcher of type %d; expected: %d", golden_data[i].field,
+ golden_data[i].op, buf, M(check)->type,
+ golden_data[i].expected);
+
+ sdb_object_deref(SDB_OBJ(check));
+ }
+}
+END_TEST
+
static int
scan_cb(sdb_store_obj_t *obj, void *user_data)
{
} golden_data[] = {
{ "host = 'a'", NULL, 1,
"OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
- { "host = 'a'", "host = 'b'", 0,
+ { "host = 'a'", "host = 'x'", 0, /* filter never matches */
"OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
{ "host = 'a'",
- "attribute.x IS NULL", 1,
+ "NOT attribute.x = ''", 1, /* filter always matches */
"OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
{ "host =~ 'a|b'", NULL, 2,
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
{ "service = 's1'", NULL, 2,
"OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
+ { "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 !~ 's'", NULL, 1,
"\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
{ "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 = 'x'", NULL, 0,
"OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
{ "attribute.k1 = 'v1'", NULL, 1,
n = 0;
sdb_store_scan(m, filter, scan_cb, &n);
fail_unless(n == golden_data[i].expected,
- "sdb_store_scan(matcher{%s}, filter{NULL}) 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));
}
tcase_add_checked_fixture(tc, populate, sdb_store_clear);
tcase_add_test(tc, test_store_match_name);
tcase_add_test(tc, test_store_match_attr);
- tcase_add_test(tc, test_store_cond);
+ 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_parse_field_cmp);
tcase_add_test(tc, test_scan);
suite_add_tcase(s, tc);