index c55184f63670bc00c85b48ffadf4f8f6a1501789..9f486836aaec3daf85818ed07c7d17c67c327c63 100644 (file)
# include "config.h"
#endif
+#include "core/plugin.h"
#include "core/store.h"
#include "core/store-private.h"
-#include "frontend/parser.h"
+#include "parser/parser.h"
#include "testutils.h"
-#include <assert.h>
-
#include <check.h>
#include <string.h>
+static sdb_store_t *store;
+
static void
populate(void)
{
size_t i;
+ store = sdb_store_create();
+ ck_assert(store != NULL);
+
for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
- int status = sdb_store_host(hosts[i], 1);
+ int status = sdb_store_host(store, hosts[i], 1);
fail_unless(status == 0,
"sdb_store_host(%s, 1) = %d; expected: 0",
hosts[i], status);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
- int status = sdb_store_metric(metrics[i].host,
+ int status = sdb_store_metric(store, metrics[i].host,
metrics[i].metric, /* store */ NULL, 1);
fail_unless(status == 0,
"sdb_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
- int status = sdb_store_service(services[i].host,
+ int status = sdb_store_service(store, services[i].host,
services[i].service, 1);
fail_unless(status == 0,
"sdb_store_service(%s, %s, 1) = %d; expected: 0",
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
- int status = sdb_store_attribute(attrs[i].host,
+ int status = sdb_store_attribute(store, attrs[i].host,
attrs[i].name, &attrs[i].value, 1);
fail_unless(status == 0,
"sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
}
} /* populate */
+static void
+turndown(void)
+{
+ sdb_object_deref(SDB_OBJ(store));
+ store = NULL;
+} /* turndown */
+
struct {
int type;
char *name;
{
sdb_store_obj_t *host;
sdb_data_t datum;
- sdb_store_expr_t *obj, *value;
+ sdb_store_expr_t *obj = NULL, *value;
sdb_store_matcher_t *m, *n;
int status;
- host = sdb_store_get_host("a");
+ host = sdb_store_get_host(store, "a");
fail_unless(host != NULL,
"sdb_store_get_host(a) = NULL; expected: <host>");
datum.type = SDB_TYPE_STRING;
datum.data.string = cmp_name_data[_i].name;
- obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
- fail_unless(obj != NULL,
- "sdb_store_expr_fieldvalue(SDB_STORE_NAME) = NULL; "
- "expected: <expr>");
+ if (cmp_name_data[_i].type == SDB_HOST) {
+ obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
+ fail_unless(obj != NULL,
+ "sdb_store_expr_fieldvalue(SDB_STORE_NAME) = NULL; "
+ "expected: <expr>");
+ }
value = sdb_store_expr_constvalue(&datum);
fail_unless(value != NULL,
"sdb_store_expr_constvalue(%s) = NULL; "
m = sdb_store_regex_matcher(obj, value);
else
m = sdb_store_eq_matcher(obj, value);
+
if (cmp_name_data[_i].type != SDB_HOST) {
+ sdb_store_expr_t *iter;
sdb_store_matcher_t *tmp;
- tmp = sdb_store_any_matcher(cmp_name_data[_i].type, m);
+ obj = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
+ iter = sdb_store_expr_typed(cmp_name_data[_i].type, obj);
+ tmp = sdb_store_any_matcher(iter, m);
+ ck_assert(iter && m);
+ sdb_object_deref(SDB_OBJ(iter));
sdb_object_deref(SDB_OBJ(m));
m = tmp;
}
};
const char *op_str[] = { "<", "<=", "=", ">=", ">" };
- assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
+ ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
- host = sdb_store_get_host("a");
+ host = sdb_store_get_host(store, "a");
fail_unless(host != NULL,
"sdb_store_get_host(a) = NULL; expected: <host>");
};
char *op_str[] = { "<", "<=", "=", ">=", ">" };
- assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
+ ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
- host = sdb_store_get_host(cmp_obj_data[_i].host);
+ host = sdb_store_get_host(store, cmp_obj_data[_i].host);
fail_unless(host != NULL,
"sdb_store_get_host(%s) = NULL; expected: <host>",
cmp_obj_data[_i].host);
sdb_data_t d = { SDB_TYPE_STRING, { .string = "a" } };
sdb_store_expr_t *e = sdb_store_expr_constvalue(&d);
- sdb_store_matcher_t *always = sdb_store_isnnull_matcher(e);
sdb_store_matcher_t *never = sdb_store_isnull_matcher(e);
+ sdb_store_matcher_t *always = sdb_store_inv_matcher(never);
struct {
const char *op;
int status;
size_t i;
- obj = sdb_store_get_host("a");
+ obj = sdb_store_get_host(store, "a");
status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
fail_unless(status == 1,
int expected;
} scan_data[] = {
/* TODO: check the name of the expected hosts */
- { "name = 'a'", NULL, 1 },
- { "name = 'a'", "name = 'x'", 0 }, /* filter never matches */
+ { "name = 'a'", NULL, 1 },
+ { "name = 'a'", "name = 'x'", 0 }, /* filter never matches */
{ "name = 'a'",
- "NOT attribute['x'] = ''", 1 }, /* filter always matches */
- { "name =~ 'a|b'", NULL, 2 },
- { "name =~ 'host'", NULL, 0 },
- { "name =~ '.'", NULL, 3 },
- { "ANY backend = 'backend'", NULL, 0 },
- { "ALL backend = ''", NULL, 3 }, /* backend is empty */
- { "backend = ['backend']", NULL, 0 },
- { "backend != ['backend']", NULL, 3 },
- { "backend < ['backend']", NULL, 3 },
- { "backend <= ['backend']", NULL, 3 },
- { "backend >= ['backend']", NULL, 0 },
- { "backend > ['backend']", NULL, 0 },
- { "ANY metric = 'm1'", NULL, 2 },
- { "ANY metric= 'm1'", "name = 'x'", 0 }, /* filter never matches */
- { "ANY metric = 'm1'",
- "NOT attribute['x'] = ''", 2 }, /* filter always matches */
- { "ANY metric =~ 'm'", NULL, 2 },
- { "ALL metric =~ 'm'", NULL, 3 },
- { "ANY metric =~ 'm'", "name !~ '1'", 1 },
- { "ANY metric =~ 'm'", "name !~ 'm'", 0 },
- { "ALL metric =~ '1'", NULL, 2 },
- { "ALL metric =~ '2'", NULL, 1 },
- { "ANY metric !~ 'm'", NULL, 0 },
- { "ALL metric !~ 'm'", NULL, 1 },
- { "ANY metric =~ 'x'", NULL, 0 },
- { "ANY service = 's1'", NULL, 2 },
- { "ANY service = 's1'", "name = 'x'", 0 }, /* filter never matches */
- { "ANY service = 's1'",
- "NOT attribute['x'] = ''", 2 }, /* filter always matches */
- { "ANY service =~ 's'", NULL, 2 },
- { "ANY service =~ 's'", "name !~ 's'", 0 },
- { "ANY service =~ 's'", "name !~ '1'", 2 },
- { "ANY service !~ 's'", NULL, 0 },
- { "ANY attribute = 'k1'", NULL, 2 },
- { "ANY attribute = 'k1'", "name = 'x'",0 }, /* filter never matches */
- { "ANY attribute = 'k1'",
+ "NOT attribute['x'] = ''", 1 }, /* filter always matches */
+ { "name =~ 'a|b'", NULL, 2 },
+ { "name =~ 'host'", NULL, 0 },
+ { "name =~ '.'", NULL, 3 },
+ { "ANY backend = 'backend'", NULL, 0 },
+ { "ALL backend = ''", NULL, 3 }, /* backend is empty */
+ { "backend = ['backend']", NULL, 0 },
+ { "backend != ['backend']", NULL, 3 },
+ { "backend < ['backend']", NULL, 3 },
+ { "backend <= ['backend']", NULL, 3 },
+ { "backend >= ['backend']", NULL, 0 },
+ { "backend > ['backend']", NULL, 0 },
+ { "ANY metric.name = 'm1'", NULL, 2 },
+ { "ANY metric.name = 'm1'", "name = 'x'", 0 }, /* filter never matches */
+ { "ANY metric.name = 'm1'",
+ "NOT attribute['x'] = ''", 2 }, /* filter always matches */
+ { "ANY metric.name =~ 'm'", NULL, 2 },
+ { "ALL metric.name =~ 'm'", NULL, 3 },
+ { "ANY metric.name =~ 'm'", "name !~ '1'", 1 },
+ { "ANY metric.name =~ 'm'", "name !~ 'm'", 0 },
+ { "ALL metric.name =~ '1'", NULL, 2 },
+ { "ALL metric.name =~ '2'", NULL, 1 },
+ { "ANY metric.name !~ 'm'", NULL, 0 },
+ { "ALL metric.name !~ 'm'", NULL, 1 },
+ { "ANY metric.name =~ 'x'", NULL, 0 },
+ { "ANY service.name = 's1'", NULL, 2 },
+ { "ANY service.name = 's1'", "name = 'x'", 0 }, /* filter never matches */
+ { "ANY service.name = 's1'",
+ "NOT attribute['x'] = ''", 2 }, /* filter always matches */
+ { "ANY service.name =~ 's'", NULL, 2 },
+ { "ANY service.name =~ 's'", "name !~ 's'", 0 },
+ { "ANY service.name =~ 's'", "name !~ '1'", 2 },
+ { "ANY service.name !~ 's'", NULL, 0 },
+ { "ANY attribute.name = 'k1'", NULL, 2 },
+ { "ANY attribute.name = 'k1'", "name = 'x'", 0 }, /* filter never matches */
+ { "ANY attribute.name = 'k1'",
"NOT attribute['x'] = ''", 2 }, /* filter always matches */
- { "ANY attribute =~ 'k'", NULL, 2 },
- { "ANY attribute =~ 'k'",
+ { "ANY attribute.name =~ 'k'", NULL, 2 },
+ { "ANY attribute.name =~ 'k'",
"name !~ '1'", 1 },
- { "ANY attribute =~ 'k'",
+ { "ANY attribute.name =~ 'k'",
"name !~ 'k'", 0 },
- { "ANY attribute =~ '1'", NULL, 2 },
- { "ANY attribute =~ '2'", NULL, 1 },
- { "ANY attribute = 'x'", NULL, 0 },
- { "ANY attribute =~ 'x'", NULL, 0 },
- { "ALL attribute = 'k1'", NULL, 2 },
+ { "ANY attribute.name =~ '1'", NULL, 2 },
+ { "ANY attribute.name =~ '2'", NULL, 1 },
+ { "ANY attribute.name = 'x'", NULL, 0 },
+ { "ANY attribute.name =~ 'x'", NULL, 0 },
+ { "ALL attribute.name = 'k1'", NULL, 2 },
+ { "ANY attribute.value = 'v1'", NULL, 1 },
+ { "ANY attribute.value =~ 'v'", NULL, 2 },
+ { "ANY attribute.value = 123", NULL, 1 },
{ "host.name = 'a'", NULL, 1 },
{ "host.attribute['k1'] =~ 'v1'",
NULL, 1 },
{ "host.attribute['x1'] IS NULL",
NULL, 3 },
+ /* not a boolean so neither TRUE nor FALSE: */
+ { "attribute['k1'] IS TRUE", NULL, 0 },
+ { "attribute['k1'] IS FALSE", NULL, 0 },
{ "attribute['k1'] = 'v1'", NULL, 1 },
{ "attribute['k1'] = 'v1'",
"name != 'k1'", 0 },
{ "attribute['x1'] !~ 'v'", NULL, 0 },
{ "attribute['k1'] IS NULL", NULL, 1 },
{ "attribute['x1'] IS NULL", NULL, 3 },
+ { "attribute['k1'] IS TRUE", NULL, 0 },
+ { "attribute['x1'] IS TRUE", NULL, 0 },
+ { "attribute['k1'] IS FALSE", NULL, 0 },
+ { "attribute['x1'] IS FALSE", NULL, 0 },
{ "attribute['k1'] IS NOT NULL", NULL, 2 },
{ "attribute['x1'] IS NOT NULL", NULL, 0 },
+ { "attribute['x1'] IS NOT TRUE", NULL, 3 },
{ "attribute['k2'] < 123", NULL, 0 },
{ "attribute['k2'] <= 123", NULL, 1 },
{ "attribute['k2'] >= 123", NULL, 1 },
{ "attribute['k2'] != 123", NULL, 0 },
{ "attribute['k1'] != 'v1'", NULL, 1 },
{ "attribute['k1'] != 'v2'", NULL, 1 },
- { "ANY attribute != 'x' "
+ { "ANY attribute.name != 'x' "
"AND attribute['k1'] !~ 'x'", NULL, 2 },
};
{
sdb_strbuf_t *errbuf = sdb_strbuf_create(64);
sdb_store_matcher_t *m, *filter = NULL;
+ sdb_ast_node_t *ast;
int check, n;
n = 0;
- check = sdb_store_scan(SDB_HOST, /* matcher */ NULL, /* filter */ NULL,
+ check = sdb_store_scan(store, SDB_HOST,
+ /* matcher */ NULL, /* filter */ NULL,
scan_cb, &n);
fail_unless(check == 0,
"sdb_store_scan() = %d; expected: 0", check);
fail_unless(n == 3,
"sdb_store_scan called callback %d times; expected: 3", (int)n);
- m = sdb_fe_parse_matcher(scan_data[_i].query, -1, errbuf);
+ ast = sdb_parser_parse_conditional(scan_data[_i].query, -1, errbuf);
+ m = sdb_store_query_prepare_matcher(ast);
+ sdb_object_deref(SDB_OBJ(ast));
fail_unless(m != NULL,
- "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher> "
+ "sdb_parser_parse_conditional(%s, -1) = NULL; expected: <ast> "
"(parser error: %s)", scan_data[_i].query,
sdb_strbuf_string(errbuf));
if (scan_data[_i].filter) {
- filter = sdb_fe_parse_matcher(scan_data[_i].filter, -1, errbuf);
+ ast = sdb_parser_parse_conditional(scan_data[_i].filter, -1, errbuf);
+ filter = sdb_store_query_prepare_matcher(ast);
+ sdb_object_deref(SDB_OBJ(ast));
fail_unless(filter != NULL,
- "sdb_fe_parse_matcher(%s, -1) = NULL; "
- "expected: <matcher> (parser error: %s)",
+ "sdb_parser_parse_conditional(%s, -1) = NULL; "
+ "expected: <ast> (parser error: %s)",
scan_data[_i].filter, sdb_strbuf_string(errbuf));
}
n = 0;
- sdb_store_scan(SDB_HOST, m, filter, scan_cb, &n);
+ sdb_store_scan(store, SDB_HOST, m, filter, scan_cb, &n);
fail_unless(n == scan_data[_i].expected,
"sdb_store_scan(HOST, matcher{%s}, filter{%s}) "
"found %d hosts; expected: %d", scan_data[_i].query,
TEST_MAIN("core::store_lookup")
{
TCase *tc = tcase_create("core");
- tcase_add_checked_fixture(tc, populate, sdb_store_clear);
+ tcase_add_checked_fixture(tc, populate, turndown);
TC_ADD_LOOP_TEST(tc, cmp_name);
TC_ADD_LOOP_TEST(tc, cmp_attr);
TC_ADD_LOOP_TEST(tc, cmp_obj);