1eb5262ffaa758c2e49be9489ad2b93094acc9d6
1 /*
2 * SysDB - t/unit/core/store_test.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "core/store.h"
33 #include "core/store-private.h"
34 #include "testutils.h"
36 #include <check.h>
37 #include <string.h>
38 #include <strings.h>
40 static void
41 populate(void)
42 {
43 sdb_data_t datum;
45 sdb_store_host("h1", 1);
46 sdb_store_host("h2", 3);
48 datum.type = SDB_TYPE_STRING;
49 datum.data.string = "v1";
50 sdb_store_attribute("h1", "k1", &datum, 1);
51 datum.data.string = "v2";
52 sdb_store_attribute("h1", "k2", &datum, 2);
53 datum.data.string = "v3";
54 sdb_store_attribute("h1", "k3", &datum, 2);
56 /* make sure that older updates don't overwrite existing values */
57 datum.data.string = "fail";
58 sdb_store_attribute("h1", "k2", &datum, 1);
59 sdb_store_attribute("h1", "k3", &datum, 2);
61 sdb_store_metric("h1", "m1", /* store */ NULL, 2);
62 sdb_store_metric("h1", "m2", /* store */ NULL, 1);
63 sdb_store_metric("h2", "m1", /* store */ NULL, 1);
65 sdb_store_service("h2", "s1", 1);
66 sdb_store_service("h2", "s2", 2);
68 datum.type = SDB_TYPE_INTEGER;
69 datum.data.integer = 42;
70 sdb_store_metric_attr("h1", "m1", "k3", &datum, 2);
72 datum.data.integer = 123;
73 sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
74 datum.data.integer = 4711;
75 sdb_store_service_attr("h2", "s2", "k2", &datum, 1);
77 /* don't overwrite k1 */
78 datum.data.integer = 666;
79 sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
80 } /* populate */
82 START_TEST(test_store_host)
83 {
84 struct {
85 const char *name;
86 sdb_time_t last_update;
87 int expected;
88 } golden_data[] = {
89 { "a", 1, 0 },
90 { "a", 2, 0 },
91 { "a", 1, 1 },
92 { "b", 1, 0 },
93 { "b", 1, 1 },
94 { "A", 1, 1 }, /* case-insensitive */
95 { "A", 3, 0 },
96 };
98 struct {
99 const char *name;
100 _Bool has;
101 } golden_hosts[] = {
102 { "a", 1 == 1 },
103 { "b", 1 == 1 },
104 { "c", 0 == 1 },
105 { "A", 1 == 1 },
106 };
108 size_t i;
110 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
111 int status;
113 status = sdb_store_host(golden_data[i].name,
114 golden_data[i].last_update);
115 fail_unless(status == golden_data[i].expected,
116 "sdb_store_host(%s, %d) = %d; expected: %d",
117 golden_data[i].name, (int)golden_data[i].last_update,
118 status, golden_data[i].expected);
119 }
121 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
122 _Bool has;
124 has = sdb_store_has_host(golden_hosts[i].name);
125 fail_unless(has == golden_hosts[i].has,
126 "sdb_store_has_host(%s) = %d; expected: %d",
127 golden_hosts[i].name, has, golden_hosts[i].has);
128 }
129 }
130 END_TEST
132 START_TEST(test_store_get_host)
133 {
134 char *golden_hosts[] = { "a", "b", "c" };
135 char *unknown_hosts[] = { "x", "y", "z" };
136 size_t i;
138 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
139 int status = sdb_store_host(golden_hosts[i], 1);
140 fail_unless(status >= 0,
141 "sdb_store_host(%s) = %d; expected: >=0",
142 golden_hosts[i], status);
143 }
145 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
146 sdb_store_obj_t *sobj1, *sobj2;
147 int ref_cnt;
149 fail_unless(sdb_store_has_host(golden_hosts[i]),
150 "sdb_store_has_host(%s) = FALSE; expected: TRUE",
151 golden_hosts[i]);
153 sobj1 = sdb_store_get_host(golden_hosts[i]);
154 fail_unless(sobj1 != NULL,
155 "sdb_store_get_host(%s) = NULL; expected: <host>",
156 golden_hosts[i]);
157 ref_cnt = SDB_OBJ(sobj1)->ref_cnt;
159 fail_unless(ref_cnt > 1,
160 "sdb_store_get_host(%s) did not increment ref count: "
161 "got: %d; expected: >1", golden_hosts[i], ref_cnt);
163 sobj2 = sdb_store_get_host(golden_hosts[i]);
164 fail_unless(sobj2 != NULL,
165 "sdb_store_get_host(%s) = NULL; expected: <host>",
166 golden_hosts[i]);
168 fail_unless(sobj1 == sobj2,
169 "sdb_store_get_host(%s) returned different objects "
170 "in successive calls", golden_hosts[i]);
171 fail_unless(SDB_OBJ(sobj2)->ref_cnt == ref_cnt + 1,
172 "sdb_store_get_hosts(%s) did not increment ref count "
173 "(first call: %d; second call: %d)",
174 golden_hosts[i], ref_cnt, SDB_OBJ(sobj2)->ref_cnt);
176 sdb_object_deref(SDB_OBJ(sobj1));
177 sdb_object_deref(SDB_OBJ(sobj2));
178 }
179 for (i = 0; i < SDB_STATIC_ARRAY_LEN(unknown_hosts); ++i) {
180 sdb_store_obj_t *sobj;
182 fail_unless(!sdb_store_has_host(unknown_hosts[i]),
183 "sdb_store_has_host(%s) = TRUE; expected: FALSE",
184 unknown_hosts[i]);
186 sobj = sdb_store_get_host(unknown_hosts[i]);
187 fail_unless(!sobj, "sdb_store_get_host(%s) = <host:%s>; expected: NULL",
188 unknown_hosts[i], sobj ? SDB_OBJ(sobj)->name : "NULL");
189 }
190 }
191 END_TEST
193 START_TEST(test_store_attr)
194 {
195 struct {
196 const char *host;
197 const char *key;
198 char *value;
199 sdb_time_t last_update;
200 int expected;
201 } golden_data[] = {
202 { "k", "k", "v", 1, -1 },
203 { "k", "k", "v", 1, -1 }, /* retry to ensure the host is not created */
204 { "l", "k1", "v1", 1, 0 },
205 { "l", "k1", "v2", 2, 0 },
206 { "l", "k1", "v3", 2, 1 },
207 { "l", "k2", "v1", 1, 0 },
208 { "m", "k", "v1", 1, 0 },
209 { "m", "k", "v2", 1, 1 },
210 };
212 size_t i;
214 sdb_store_host("l", 1);
215 sdb_store_host("m", 1);
216 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
217 sdb_data_t datum;
218 int status;
220 /* XXX: test other types as well */
221 datum.type = SDB_TYPE_STRING;
222 datum.data.string = golden_data[i].value;
224 status = sdb_store_attribute(golden_data[i].host,
225 golden_data[i].key, &datum,
226 golden_data[i].last_update);
227 fail_unless(status == golden_data[i].expected,
228 "sdb_store_attribute(%s, %s, %s, %d) = %d; expected: %d",
229 golden_data[i].host, golden_data[i].key, golden_data[i].value,
230 golden_data[i].last_update, status, golden_data[i].expected);
231 }
232 }
233 END_TEST
235 START_TEST(test_store_metric)
236 {
237 sdb_metric_store_t store1 = { "dummy-type1", "dummy-id1" };
238 sdb_metric_store_t store2 = { "dummy-type2", "dummy-id2" };
240 struct {
241 const char *host;
242 const char *metric;
243 sdb_metric_store_t *store;
244 sdb_time_t last_update;
245 int expected;
246 } golden_data[] = {
247 { "k", "m", NULL, 1, -1 },
248 { "k", "m", NULL, 1, -1 }, /* retry to ensure the host is not created */
249 { "k", "m", &store1, 1, -1 },
250 { "l", "m1", NULL, 1, 0 },
251 { "l", "m1", &store1, 2, 0 },
252 { "l", "m1", &store1, 3, 0 },
253 { "l", "m1", NULL, 3, 1 },
254 { "l", "m2", &store1, 1, 0 },
255 { "l", "m2", &store2, 2, 0 },
256 { "l", "m2", NULL, 3, 0 },
257 { "m", "m", &store1, 1, 0 },
258 { "m", "m", NULL, 2, 0 },
259 { "m", "m", NULL, 2, 1 },
260 { "m", "m", &store1, 3, 0 },
261 { "m", "m", &store2, 4, 0 },
262 { "m", "m", NULL, 5, 0 },
263 };
265 size_t i;
267 sdb_store_host("m", 1);
268 sdb_store_host("l", 1);
269 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
270 int status;
272 status = sdb_store_metric(golden_data[i].host,
273 golden_data[i].metric, golden_data[i].store,
274 golden_data[i].last_update);
275 fail_unless(status == golden_data[i].expected,
276 "sdb_store_metric(%s, %s, %p, %d) = %d; expected: %d",
277 golden_data[i].host, golden_data[i].metric,
278 golden_data[i].store, golden_data[i].last_update,
279 status, golden_data[i].expected);
280 }
281 }
282 END_TEST
284 START_TEST(test_store_metric_attr)
285 {
286 struct {
287 const char *host;
288 const char *metric;
289 const char *attr;
290 const sdb_data_t value;
291 sdb_time_t last_update;
292 int expected;
293 } golden_data[] = {
294 { "k", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
295 /* retry, it should still fail */
296 { "k", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
297 { "l", "mX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
298 /* retry, it should still fail */
299 { "l", "mX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
300 { "l", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
301 { "l", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 1 },
302 { "l", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 2, 0 },
303 { "l", "m1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
304 { "l", "m1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 1 },
305 { "l", "m2", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
306 { "m", "m1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
307 };
309 size_t i;
311 sdb_store_host("m", 1);
312 sdb_store_host("l", 1);
313 sdb_store_metric("m", "m1", NULL, 1);
314 sdb_store_metric("l", "m1", NULL, 1);
315 sdb_store_metric("l", "m2", NULL, 1);
317 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
318 int status;
320 status = sdb_store_metric_attr(golden_data[i].host,
321 golden_data[i].metric, golden_data[i].attr,
322 &golden_data[i].value, golden_data[i].last_update);
323 fail_unless(status == golden_data[i].expected,
324 "sdb_store_metric_attr(%s, %s, %s, %d, %d) = %d; "
325 "expected: %d", golden_data[i].host, golden_data[i].metric,
326 golden_data[i].attr, golden_data[i].value.data.integer,
327 golden_data[i].last_update, status, golden_data[i].expected);
328 }
329 }
330 END_TEST
332 START_TEST(test_store_service)
333 {
334 struct {
335 const char *host;
336 const char *svc;
337 sdb_time_t last_update;
338 int expected;
339 } golden_data[] = {
340 { "k", "s", 1, -1 },
341 { "k", "s", 1, -1 }, /* retry to ensure the host is not created */
342 { "l", "s1", 1, 0 },
343 { "l", "s1", 2, 0 },
344 { "l", "s1", 2, 1 },
345 { "l", "s2", 1, 0 },
346 { "m", "s", 1, 0 },
347 { "m", "s", 1, 1 },
348 };
350 size_t i;
352 sdb_store_host("m", 1);
353 sdb_store_host("l", 1);
354 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
355 int status;
357 status = sdb_store_service(golden_data[i].host,
358 golden_data[i].svc, golden_data[i].last_update);
359 fail_unless(status == golden_data[i].expected,
360 "sdb_store_service(%s, %s, %d) = %d; expected: %d",
361 golden_data[i].host, golden_data[i].svc,
362 golden_data[i].last_update, status, golden_data[i].expected);
363 }
364 }
365 END_TEST
367 START_TEST(test_store_service_attr)
368 {
369 struct {
370 const char *host;
371 const char *svc;
372 const char *attr;
373 const sdb_data_t value;
374 sdb_time_t last_update;
375 int expected;
376 } golden_data[] = {
377 { "k", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
378 /* retry, it should still fail */
379 { "k", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
380 { "l", "sX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
381 /* retry, it should still fail */
382 { "l", "sX", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, -1 },
383 { "l", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
384 { "l", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 1 },
385 { "l", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 2, 0 },
386 { "l", "s1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
387 { "l", "s1", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 1 },
388 { "l", "s2", "a2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
389 { "m", "s1", "a1", { SDB_TYPE_INTEGER, { .integer = 123 } }, 1, 0 },
390 };
392 size_t i;
394 sdb_store_host("m", 1);
395 sdb_store_host("l", 1);
396 sdb_store_service("m", "s1", 1);
397 sdb_store_service("l", "s1", 1);
398 sdb_store_service("l", "s2", 1);
400 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
401 int status;
403 status = sdb_store_service_attr(golden_data[i].host,
404 golden_data[i].svc, golden_data[i].attr,
405 &golden_data[i].value, golden_data[i].last_update);
406 fail_unless(status == golden_data[i].expected,
407 "sdb_store_service_attr(%s, %s, %s, %d, %d) = %d; "
408 "expected: %d", golden_data[i].host, golden_data[i].svc,
409 golden_data[i].attr, golden_data[i].value.data.integer,
410 golden_data[i].last_update, status, golden_data[i].expected);
411 }
412 }
413 END_TEST
415 static struct {
416 const char *hostname;
417 int field;
418 int expected;
419 sdb_data_t value;
420 } get_field_data[] = {
421 { NULL, 0, -1, { SDB_TYPE_NULL, { 0 } } },
422 { NULL, SDB_FIELD_LAST_UPDATE, -1, { SDB_TYPE_NULL, { 0 } } },
423 { NULL, SDB_FIELD_NAME, -1, { SDB_TYPE_NULL, { 0 } } },
424 { "host", SDB_FIELD_LAST_UPDATE, 0, { SDB_TYPE_DATETIME, { .datetime = 20 } } },
425 { "host", SDB_FIELD_INTERVAL, 0, { SDB_TYPE_DATETIME, { .datetime = 10 } } },
426 /* the test will handle AGE specially */
427 { "host", SDB_FIELD_AGE, 0, { SDB_TYPE_NULL, { 0 } } },
428 { "host", SDB_FIELD_NAME, 0, { SDB_TYPE_STRING, { .string = "host" } } },
429 { "host", SDB_FIELD_BACKEND, 0, { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } } },
430 };
432 START_TEST(test_get_field)
433 {
434 sdb_store_obj_t *host = NULL;
435 sdb_data_t value = SDB_DATA_INIT;
436 char value_str[128], expected_value_str[128];
437 sdb_time_t now = sdb_gettime();
438 int check;
440 sdb_store_host("host", 10);
441 sdb_store_host("host", 20);
443 if (get_field_data[_i].hostname) {
444 host = sdb_store_get_host(get_field_data[_i].hostname);
445 ck_assert(host != NULL);
446 }
448 check = sdb_store_get_field(host, get_field_data[_i].field, NULL);
449 fail_unless(check == get_field_data[_i].expected,
450 "sdb_store_get_field(%s, %s, NULL) = %d; expected: %d",
451 host ? "<host>" : "NULL", SDB_FIELD_TO_NAME(get_field_data[_i].field),
452 check, get_field_data[_i].expected);
453 check = sdb_store_get_field(host, get_field_data[_i].field, &value);
454 fail_unless(check == get_field_data[_i].expected,
455 "sdb_store_get_field(%s, %s, <value>) = %d; expected: %d",
456 host ? "<host>" : "NULL", SDB_FIELD_TO_NAME(get_field_data[_i].field),
457 check, get_field_data[_i].expected);
459 if (get_field_data[_i].expected)
460 return;
462 if (get_field_data[_i].field == SDB_FIELD_AGE) {
463 get_field_data[_i].value.type = SDB_TYPE_DATETIME;
464 get_field_data[_i].value.data.datetime = now;
465 }
467 sdb_data_format(&value, value_str, sizeof(value_str), 0);
468 sdb_data_format(&get_field_data[_i].value, expected_value_str,
469 sizeof(expected_value_str), 0);
471 if (get_field_data[_i].field == SDB_FIELD_AGE) {
472 fail_unless((value.type == SDB_TYPE_DATETIME)
473 && (value.data.datetime >= now),
474 "sdb_store_get_field(<host>, %s, <value>) "
475 "returned value %s; expected >=%s",
476 SDB_FIELD_TO_NAME(get_field_data[_i].field),
477 value_str, expected_value_str);
478 }
479 else {
480 fail_unless(! sdb_data_cmp(&value, &get_field_data[_i].value),
481 "sdb_store_get_field(<host>, %s, <value>) "
482 "returned value %s; expected %s",
483 SDB_FIELD_TO_NAME(get_field_data[_i].field),
484 value_str, expected_value_str);
485 }
486 sdb_data_free_datum(&value);
487 }
488 END_TEST
490 START_TEST(test_get_child)
491 {
492 struct {
493 const char *host;
494 const char *name;
495 int type;
496 int expected;
497 } golden_data[] = {
498 { "h1", NULL, SDB_HOST, 0 },
499 { "h1", NULL, SDB_SERVICE, -1 },
500 { "h1", NULL, SDB_METRIC, -1 },
501 { "h1", NULL, SDB_ATTRIBUTE, -1 },
502 { "h2", NULL, SDB_HOST, 0 },
503 { "h2", NULL, SDB_SERVICE, -1 },
504 { "h2", NULL, SDB_METRIC, -1 },
505 { "h2", NULL, SDB_ATTRIBUTE, -1 },
506 { "h3", NULL, SDB_HOST, -1 },
507 { "h1", "k1", SDB_ATTRIBUTE, 0 },
508 { "h1", "x1", SDB_ATTRIBUTE, -1 },
509 { "h2", "k1", SDB_ATTRIBUTE, -1 },
510 { "h1", "k1", SDB_SERVICE, -1 },
511 { "h1", "k1", SDB_METRIC, -1 },
512 { "h1", "s1", SDB_SERVICE, -1 },
513 { "h2", "s1", SDB_SERVICE, 0 },
514 { "h1", "m2", SDB_METRIC, 0 },
515 { "h2", "m2", SDB_METRIC, -1 },
516 };
518 size_t i;
520 populate();
522 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
523 sdb_store_obj_t *obj;
524 const char *expected_name = golden_data[i].host;
526 obj = sdb_store_get_host(golden_data[i].host);
527 if (golden_data[i].expected && (golden_data[i].type == SDB_HOST))
528 fail_unless(obj == NULL,
529 "sdb_store_get_host(%s) = %p; expected: NULL",
530 golden_data[i].host, obj);
531 else
532 fail_unless(obj != NULL,
533 "sdb_store_get_host(%s) = NULL; expected: <host>",
534 golden_data[i].host);
536 if (golden_data[i].type != SDB_HOST) {
537 sdb_store_obj_t *tmp;
539 expected_name = golden_data[i].name;
541 tmp = sdb_store_get_child(obj,
542 golden_data[i].type, golden_data[i].name);
543 if (golden_data[i].expected)
544 fail_unless(tmp == NULL,
545 "sdb_store_get_child(<%s>, %s, %s) = %p; "
546 "expected: NULL", golden_data[i].host,
547 SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
548 golden_data[i].name, tmp);
549 else
550 fail_unless(tmp != NULL,
551 "sdb_store_get_child(<%s>, %s, %s) = NULL; "
552 "expected: <obj>", golden_data[i].host,
553 SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
554 golden_data[i].name);
556 sdb_object_deref(SDB_OBJ(obj));
557 obj = tmp;
558 }
560 if (golden_data[i].expected)
561 continue;
563 fail_unless(obj->type == golden_data[i].type,
564 "sdb_store_get_<%s>(%s, %s) returned object of type %d; "
565 "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
566 golden_data[i].host, golden_data[i].name, obj->type,
567 golden_data[i].type);
568 fail_unless(! strcasecmp(SDB_OBJ(obj)->name, expected_name),
569 "sdb_store_get_<%s>(%s, %s) returned object named '%s'; "
570 "expected: '%s'", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
571 golden_data[i].host, golden_data[i].name, SDB_OBJ(obj)->name,
572 expected_name);
574 sdb_object_deref(SDB_OBJ(obj));
575 }
576 }
577 END_TEST
579 START_TEST(test_interval)
580 {
581 sdb_store_obj_t *host;
583 /* 10 us interval */
584 sdb_store_host("host", 10);
585 sdb_store_host("host", 20);
586 sdb_store_host("host", 30);
587 sdb_store_host("host", 40);
589 host = sdb_store_get_host("host");
590 fail_unless(host != NULL,
591 "INTERNAL ERROR: store doesn't have host after adding it");
593 fail_unless(host->interval == 10,
594 "sdb_store_host() did not calculate interval correctly: "
595 "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 10);
597 /* multiple updates for the same timestamp don't modify the interval */
598 sdb_store_host("host", 40);
599 sdb_store_host("host", 40);
600 sdb_store_host("host", 40);
601 sdb_store_host("host", 40);
603 fail_unless(host->interval == 10,
604 "sdb_store_host() changed interval when doing multiple updates "
605 "using the same timestamp; got: %"PRIsdbTIME"; "
606 "expected: %"PRIsdbTIME, host->interval, 10);
608 /* multiple updates using an timestamp don't modify the interval */
609 sdb_store_host("host", 20);
610 sdb_store_host("host", 20);
611 sdb_store_host("host", 20);
612 sdb_store_host("host", 20);
614 fail_unless(host->interval == 10,
615 "sdb_store_host() changed interval when doing multiple updates "
616 "using an old timestamp; got: %"PRIsdbTIME"; expected: %"PRIsdbTIME,
617 host->interval, 10);
619 /* new interval: 20 us */
620 sdb_store_host("host", 60);
621 fail_unless(host->interval == 11,
622 "sdb_store_host() did not calculate interval correctly: "
623 "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
625 /* new interval: 40 us */
626 sdb_store_host("host", 100);
627 fail_unless(host->interval == 13,
628 "sdb_store_host() did not calculate interval correctly: "
629 "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
631 sdb_object_deref(SDB_OBJ(host));
632 }
633 END_TEST
635 static int
636 scan_count(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
637 {
638 intptr_t *i = user_data;
640 if (! sdb_store_matcher_matches(filter, obj, NULL))
641 return 0;
643 fail_unless(obj != NULL,
644 "sdb_store_scan callback received NULL obj; expected: "
645 "<store base obj>");
646 fail_unless(i != NULL,
647 "sdb_store_scan callback received NULL user_data; "
648 "expected: <pointer to data>");
650 ++(*i);
651 return 0;
652 } /* scan_count */
654 static int
655 scan_error(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
656 {
657 intptr_t *i = user_data;
659 if (! sdb_store_matcher_matches(filter, obj, NULL))
660 return 0;
662 fail_unless(obj != NULL,
663 "sdb_store_scan callback received NULL obj; expected: "
664 "<store base obj>");
665 fail_unless(i != NULL,
666 "sdb_store_scan callback received NULL user_data; "
667 "expected: <pointer to data>");
669 ++(*i);
670 return -1;
671 } /* scan_error */
673 START_TEST(test_scan)
674 {
675 intptr_t i = 0;
676 int check;
678 /* empty store */
679 check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
680 scan_count, &i);
681 fail_unless(check == 0,
682 "sdb_store_scan(HOST), empty store = %d; expected: 0", check);
683 fail_unless(i == 0,
684 "sdb_store_scan(HOST) called callback %d times; "
685 "expected: 0", (int)i);
687 populate();
689 check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
690 scan_count, &i);
691 fail_unless(check == 0,
692 "sdb_store_scan(HOST) = %d; expected: 0", check);
693 fail_unless(i == 2,
694 "sdb_store_scan(HOST) called callback %d times; "
695 "expected: 1", (int)i);
697 i = 0;
698 check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
699 scan_error, &i);
700 fail_unless(check == -1,
701 "sdb_store_scan(HOST), error callback = %d; expected: -1", check);
702 fail_unless(i == 1,
703 "sdb_store_scan(HOST) called callback %d times "
704 "(callback returned error); expected: 1", (int)i);
706 i = 0;
707 check = sdb_store_scan(SDB_SERVICE, /* m, filter = */ NULL, NULL,
708 scan_count, &i);
709 fail_unless(check == 0,
710 "sdb_store_scan(SERVICE) = %d; expected: 0", check);
711 fail_unless(i == 2,
712 "sdb_store_scan(SERVICE) called callback %d times; "
713 "expected: 2", (int)i);
715 i = 0;
716 check = sdb_store_scan(SDB_METRIC, /* m, filter = */ NULL, NULL,
717 scan_count, &i);
718 fail_unless(check == 0,
719 "sdb_store_scan(METRIC) = %d; expected: 0", check);
720 fail_unless(i == 3,
721 "sdb_store_scan(METRIC) called callback %d times; "
722 "expected: 3", (int)i);
723 }
724 END_TEST
726 TEST_MAIN("core::store")
727 {
728 TCase *tc = tcase_create("core");
729 tcase_add_test(tc, test_store_host);
730 tcase_add_test(tc, test_store_get_host);
731 tcase_add_test(tc, test_store_attr);
732 tcase_add_test(tc, test_store_metric);
733 tcase_add_test(tc, test_store_metric_attr);
734 tcase_add_test(tc, test_store_service);
735 tcase_add_test(tc, test_store_service_attr);
736 TC_ADD_LOOP_TEST(tc, get_field);
737 tcase_add_test(tc, test_get_child);
738 tcase_add_test(tc, test_interval);
739 tcase_add_test(tc, test_scan);
740 tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear);
741 ADD_TCASE(tc);
742 }
743 TEST_MAIN_END
745 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */