7ea53bcaf76d25bb8efd70e12ea4f13db6dadb8d
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 START_TEST(test_get_field)
416 {
417 sdb_store_obj_t *host;
418 sdb_data_t value = SDB_DATA_INIT;
419 int check;
421 sdb_store_host("host", 10);
422 sdb_store_host("host", 20);
424 host = sdb_store_get_host("host");
425 fail_unless(host != NULL,
426 "INTERNAL ERROR: store doesn't have host after adding it");
428 check = sdb_store_get_field(NULL, 0, NULL);
429 fail_unless(check < 0,
430 "sdb_store_get_field(NULL, 0, NULL) = %d; expected: <0");
431 check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL);
432 fail_unless(check < 0,
433 "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
434 "expected: <0");
435 check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, &value);
436 fail_unless(check < 0,
437 "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, <value>) = %d; "
438 "expected: <0");
440 check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
441 fail_unless(check == 0,
442 "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
443 "expected: 0");
444 /* 'name' is dynamically allocated; make sure it's not leaked even
445 * if there is no result parameter */
446 check = sdb_store_get_field(host, SDB_FIELD_NAME, NULL);
447 fail_unless(check == 0,
448 "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
449 "expected: 0");
451 check = sdb_store_get_field(host, SDB_FIELD_NAME, &value);
452 fail_unless(check == 0,
453 "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) = "
454 "%d; expected: 0");
455 fail_unless((value.type == SDB_TYPE_STRING)
456 && (! strcmp(value.data.string, "host")),
457 "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) "
458 "returned value {%d, %s}; expected {%d, host}",
459 value.type, value.data.string, SDB_TYPE_STRING);
460 sdb_data_free_datum(&value);
462 check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, &value);
463 fail_unless(check == 0,
464 "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, <value>) = "
465 "%d; expected: 0");
466 fail_unless((value.type == SDB_TYPE_DATETIME)
467 && (value.data.datetime == 20),
468 "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, <value>) "
469 "returned value {%d, %lu}; expected {%d, 20}",
470 value.type, value.data.datetime, SDB_TYPE_DATETIME);
472 check = sdb_store_get_field(host, SDB_FIELD_AGE, &value);
473 fail_unless(check == 0,
474 "sdb_store_get_field(<host>, SDB_FIELD_AGE, <value>) = "
475 "%d; expected: 0");
476 /* let's assume we're at least in year 1980 ;-) */
477 fail_unless((value.type == SDB_TYPE_DATETIME)
478 && (value.data.datetime > 10L * SDB_INTERVAL_YEAR),
479 "sdb_store_get_field(<host>, SDB_FIELD_AGE, <value>) "
480 "returned value {%d, %lu}; expected {%d, >%lu}",
481 value.type, value.data.datetime,
482 SDB_TYPE_DATETIME, 10L * SDB_INTERVAL_YEAR);
484 check = sdb_store_get_field(host, SDB_FIELD_INTERVAL, &value);
485 fail_unless(check == 0,
486 "sdb_store_get_field(<host>, SDB_FIELD_INTERVAL, <value>) = "
487 "%d; expected: 0");
488 fail_unless((value.type == SDB_TYPE_DATETIME)
489 && (value.data.datetime == 10),
490 "sdb_store_get_field(<host>, SDB_FIELD_INTERVAL, <value>) "
491 "returned value {%d, %lu}; expected {%d, 10}",
492 value.type, value.data.datetime, SDB_TYPE_DATETIME);
494 check = sdb_store_get_field(host, SDB_FIELD_BACKEND, &value);
495 fail_unless(check == 0,
496 "sdb_store_get_field(<host>, SDB_FIELD_BACKEND, <value>) = "
497 "%d; expected: 0");
498 /* there are no backends in this test */
499 fail_unless((value.type == (SDB_TYPE_ARRAY | SDB_TYPE_STRING))
500 && (value.data.array.length == 0)
501 && (value.data.array.values == NULL),
502 "sdb_store_get_field(<host>, SDB_FIELD_BACKEND, <value>) "
503 "returned value {%d, %lu, %p}; expected {%d, 0, NULL}",
504 value.type, value.data.array.length, value.data.array.values,
505 SDB_TYPE_ARRAY | SDB_TYPE_STRING);
506 }
507 END_TEST
509 START_TEST(test_get_child)
510 {
511 struct {
512 const char *host;
513 const char *name;
514 int type;
515 int expected;
516 } golden_data[] = {
517 { "h1", NULL, SDB_HOST, 0 },
518 { "h1", NULL, SDB_SERVICE, -1 },
519 { "h1", NULL, SDB_METRIC, -1 },
520 { "h1", NULL, SDB_ATTRIBUTE, -1 },
521 { "h2", NULL, SDB_HOST, 0 },
522 { "h2", NULL, SDB_SERVICE, -1 },
523 { "h2", NULL, SDB_METRIC, -1 },
524 { "h2", NULL, SDB_ATTRIBUTE, -1 },
525 { "h3", NULL, SDB_HOST, -1 },
526 { "h1", "k1", SDB_ATTRIBUTE, 0 },
527 { "h1", "x1", SDB_ATTRIBUTE, -1 },
528 { "h2", "k1", SDB_ATTRIBUTE, -1 },
529 { "h1", "k1", SDB_SERVICE, -1 },
530 { "h1", "k1", SDB_METRIC, -1 },
531 { "h1", "s1", SDB_SERVICE, -1 },
532 { "h2", "s1", SDB_SERVICE, 0 },
533 { "h1", "m2", SDB_METRIC, 0 },
534 { "h2", "m2", SDB_METRIC, -1 },
535 };
537 size_t i;
539 populate();
541 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
542 sdb_store_obj_t *obj;
543 const char *expected_name = golden_data[i].host;
545 obj = sdb_store_get_host(golden_data[i].host);
546 if (golden_data[i].expected && (golden_data[i].type == SDB_HOST))
547 fail_unless(obj == NULL,
548 "sdb_store_get_host(%s) = %p; expected: NULL",
549 golden_data[i].host, obj);
550 else
551 fail_unless(obj != NULL,
552 "sdb_store_get_host(%s) = NULL; expected: <host>",
553 golden_data[i].host);
555 if (golden_data[i].type != SDB_HOST) {
556 sdb_store_obj_t *tmp;
558 expected_name = golden_data[i].name;
560 tmp = sdb_store_get_child(obj,
561 golden_data[i].type, golden_data[i].name);
562 if (golden_data[i].expected)
563 fail_unless(tmp == NULL,
564 "sdb_store_get_child(<%s>, %s, %s) = %p; "
565 "expected: NULL", golden_data[i].host,
566 SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
567 golden_data[i].name, tmp);
568 else
569 fail_unless(tmp != NULL,
570 "sdb_store_get_child(<%s>, %s, %s) = NULL; "
571 "expected: <obj>", golden_data[i].host,
572 SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
573 golden_data[i].name);
575 sdb_object_deref(SDB_OBJ(obj));
576 obj = tmp;
577 }
579 if (golden_data[i].expected)
580 continue;
582 fail_unless(obj->type == golden_data[i].type,
583 "sdb_store_get_<%s>(%s, %s) returned object of type %d; "
584 "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
585 golden_data[i].host, golden_data[i].name, obj->type,
586 golden_data[i].type);
587 fail_unless(! strcasecmp(SDB_OBJ(obj)->name, expected_name),
588 "sdb_store_get_<%s>(%s, %s) returned object named '%s'; "
589 "expected: '%s'", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
590 golden_data[i].host, golden_data[i].name, SDB_OBJ(obj)->name,
591 expected_name);
593 sdb_object_deref(SDB_OBJ(obj));
594 }
595 }
596 END_TEST
598 START_TEST(test_interval)
599 {
600 sdb_store_obj_t *host;
602 /* 10 us interval */
603 sdb_store_host("host", 10);
604 sdb_store_host("host", 20);
605 sdb_store_host("host", 30);
606 sdb_store_host("host", 40);
608 host = sdb_store_get_host("host");
609 fail_unless(host != NULL,
610 "INTERNAL ERROR: store doesn't have host after adding it");
612 fail_unless(host->interval == 10,
613 "sdb_store_host() did not calculate interval correctly: "
614 "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 10);
616 /* multiple updates for the same timestamp don't modify the interval */
617 sdb_store_host("host", 40);
618 sdb_store_host("host", 40);
619 sdb_store_host("host", 40);
620 sdb_store_host("host", 40);
622 fail_unless(host->interval == 10,
623 "sdb_store_host() changed interval when doing multiple updates "
624 "using the same timestamp; got: %"PRIsdbTIME"; "
625 "expected: %"PRIsdbTIME, host->interval, 10);
627 /* multiple updates using an timestamp don't modify the interval */
628 sdb_store_host("host", 20);
629 sdb_store_host("host", 20);
630 sdb_store_host("host", 20);
631 sdb_store_host("host", 20);
633 fail_unless(host->interval == 10,
634 "sdb_store_host() changed interval when doing multiple updates "
635 "using an old timestamp; got: %"PRIsdbTIME"; expected: %"PRIsdbTIME,
636 host->interval, 10);
638 /* new interval: 20 us */
639 sdb_store_host("host", 60);
640 fail_unless(host->interval == 11,
641 "sdb_store_host() did not calculate interval correctly: "
642 "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
644 /* new interval: 40 us */
645 sdb_store_host("host", 100);
646 fail_unless(host->interval == 13,
647 "sdb_store_host() did not calculate interval correctly: "
648 "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
650 sdb_object_deref(SDB_OBJ(host));
651 }
652 END_TEST
654 static int
655 scan_count(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 0;
671 } /* scan_count */
673 static int
674 scan_error(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
675 {
676 intptr_t *i = user_data;
678 if (! sdb_store_matcher_matches(filter, obj, NULL))
679 return 0;
681 fail_unless(obj != NULL,
682 "sdb_store_scan callback received NULL obj; expected: "
683 "<store base obj>");
684 fail_unless(i != NULL,
685 "sdb_store_scan callback received NULL user_data; "
686 "expected: <pointer to data>");
688 ++(*i);
689 return -1;
690 } /* scan_error */
692 START_TEST(test_scan)
693 {
694 intptr_t i = 0;
695 int check;
697 /* empty store */
698 check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
699 scan_count, &i);
700 fail_unless(check == 0,
701 "sdb_store_scan(HOST), empty store = %d; expected: 0", check);
702 fail_unless(i == 0,
703 "sdb_store_scan(HOST) called callback %d times; "
704 "expected: 0", (int)i);
706 populate();
708 check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
709 scan_count, &i);
710 fail_unless(check == 0,
711 "sdb_store_scan(HOST) = %d; expected: 0", check);
712 fail_unless(i == 2,
713 "sdb_store_scan(HOST) called callback %d times; "
714 "expected: 1", (int)i);
716 i = 0;
717 check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
718 scan_error, &i);
719 fail_unless(check == -1,
720 "sdb_store_scan(HOST), error callback = %d; expected: -1", check);
721 fail_unless(i == 1,
722 "sdb_store_scan(HOST) called callback %d times "
723 "(callback returned error); expected: 1", (int)i);
725 i = 0;
726 check = sdb_store_scan(SDB_SERVICE, /* m, filter = */ NULL, NULL,
727 scan_count, &i);
728 fail_unless(check == 0,
729 "sdb_store_scan(SERVICE) = %d; expected: 0", check);
730 fail_unless(i == 2,
731 "sdb_store_scan(SERVICE) called callback %d times; "
732 "expected: 2", (int)i);
734 i = 0;
735 check = sdb_store_scan(SDB_METRIC, /* m, filter = */ NULL, NULL,
736 scan_count, &i);
737 fail_unless(check == 0,
738 "sdb_store_scan(METRIC) = %d; expected: 0", check);
739 fail_unless(i == 3,
740 "sdb_store_scan(METRIC) called callback %d times; "
741 "expected: 3", (int)i);
742 }
743 END_TEST
745 TEST_MAIN("core::store")
746 {
747 TCase *tc = tcase_create("core");
748 tcase_add_test(tc, test_store_host);
749 tcase_add_test(tc, test_store_get_host);
750 tcase_add_test(tc, test_store_attr);
751 tcase_add_test(tc, test_store_metric);
752 tcase_add_test(tc, test_store_metric_attr);
753 tcase_add_test(tc, test_store_service);
754 tcase_add_test(tc, test_store_service_attr);
755 tcase_add_test(tc, test_get_field);
756 tcase_add_test(tc, test_get_child);
757 tcase_add_test(tc, test_interval);
758 tcase_add_test(tc, test_scan);
759 tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear);
760 ADD_TCASE(tc);
761 }
762 TEST_MAIN_END
764 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */