2 * collectd - src/utils_vl_lookup_test.c
3 * Copyright (C) 2012 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <octo at collectd.org>
28 #include "utils_vl_lookup.h"
30 static _Bool expect_new_obj = 0;
31 static _Bool have_new_obj = 0;
33 static identifier_t last_class_ident;
34 static identifier_t last_obj_ident;
36 static data_source_t dsrc_test = { "value", DS_TYPE_DERIVE, 0.0, NAN };
37 static data_set_t const ds_test = { "test", 1, &dsrc_test };
39 static data_source_t dsrc_unknown = { "value", DS_TYPE_DERIVE, 0.0, NAN };
40 static data_set_t const ds_unknown = { "unknown", 1, &dsrc_unknown };
42 static int lookup_obj_callback (data_set_t const *ds,
43 value_list_t const *vl,
44 void *user_class, void *user_obj)
46 identifier_t *class = user_class;
47 identifier_t *obj = user_obj;
49 assert (expect_new_obj == have_new_obj);
51 memcpy (&last_class_ident, class, sizeof (last_class_ident));
52 memcpy (&last_obj_ident, obj, sizeof (last_obj_ident));
54 if (strcmp (obj->plugin_instance, "failure") == 0)
60 static void *lookup_class_callback (data_set_t const *ds,
61 value_list_t const *vl, void *user_class)
63 identifier_t *class = user_class;
66 assert (expect_new_obj);
68 memcpy (&last_class_ident, class, sizeof (last_class_ident));
70 obj = malloc (sizeof (*obj));
71 strncpy (obj->host, vl->host, sizeof (obj->host));
72 strncpy (obj->plugin, vl->plugin, sizeof (obj->plugin));
73 strncpy (obj->plugin_instance, vl->plugin_instance, sizeof (obj->plugin_instance));
74 strncpy (obj->type, vl->type, sizeof (obj->type));
75 strncpy (obj->type_instance, vl->type_instance, sizeof (obj->type_instance));
79 return ((void *) obj);
82 static void checked_lookup_add (lookup_t *obj, /* {{{ */
84 char const *plugin, char const *plugin_instance,
85 char const *type, char const *type_instance)
91 memset (&ident, 0, sizeof (ident));
92 strncpy (ident.host, host, sizeof (ident.host));
93 strncpy (ident.plugin, plugin, sizeof (ident.plugin));
94 strncpy (ident.plugin_instance, plugin_instance, sizeof (ident.plugin_instance));
95 strncpy (ident.type, type, sizeof (ident.type));
96 strncpy (ident.type_instance, type_instance, sizeof (ident.type_instance));
98 user_class = malloc (sizeof (ident));
99 memmove (user_class, &ident, sizeof (ident));
101 status = lookup_add (obj, &ident, user_class);
102 assert (status == 0);
103 } /* }}} void test_add */
105 static int checked_lookup_search (lookup_t *obj,
107 char const *plugin, char const *plugin_instance,
108 char const *type, char const *type_instance,
112 value_list_t vl = VALUE_LIST_STATIC;
113 data_set_t const *ds = &ds_unknown;
115 strncpy (vl.host, host, sizeof (vl.host));
116 strncpy (vl.plugin, plugin, sizeof (vl.plugin));
117 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
118 strncpy (vl.type, type, sizeof (vl.type));
119 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
121 if (strcmp (vl.type, "test") == 0)
124 expect_new_obj = expect_new;
127 status = lookup_search (obj, ds, &vl);
131 static lookup_t *checked_lookup_create (void)
133 lookup_t *obj = lookup_create (
134 lookup_class_callback,
138 assert (obj != NULL);
142 static void testcase0 (void)
144 lookup_t *obj = checked_lookup_create ();
146 checked_lookup_add (obj, "/any/", "test", "", "test", "/all/");
147 checked_lookup_search (obj, "host0", "test", "", "test", "0",
148 /* expect new = */ 1);
149 checked_lookup_search (obj, "host0", "test", "", "test", "1",
150 /* expect new = */ 0);
151 checked_lookup_search (obj, "host1", "test", "", "test", "0",
152 /* expect new = */ 1);
153 checked_lookup_search (obj, "host1", "test", "", "test", "1",
154 /* expect new = */ 0);
156 lookup_destroy (obj);
159 static void testcase1 (void)
161 lookup_t *obj = checked_lookup_create ();
163 checked_lookup_add (obj, "/any/", "/all/", "/all/", "test", "/all/");
164 checked_lookup_search (obj, "host0", "plugin0", "", "test", "0",
165 /* expect new = */ 1);
166 checked_lookup_search (obj, "host0", "plugin0", "", "test", "1",
167 /* expect new = */ 0);
168 checked_lookup_search (obj, "host0", "plugin1", "", "test", "0",
169 /* expect new = */ 0);
170 checked_lookup_search (obj, "host0", "plugin1", "", "test", "1",
171 /* expect new = */ 0);
172 checked_lookup_search (obj, "host1", "plugin0", "", "test", "0",
173 /* expect new = */ 1);
174 checked_lookup_search (obj, "host1", "plugin0", "", "test", "1",
175 /* expect new = */ 0);
176 checked_lookup_search (obj, "host1", "plugin1", "", "test", "0",
177 /* expect new = */ 0);
178 checked_lookup_search (obj, "host1", "plugin1", "", "test", "1",
179 /* expect new = */ 0);
181 lookup_destroy (obj);
184 static void testcase2 (void)
186 lookup_t *obj = checked_lookup_create ();
189 checked_lookup_add (obj, "/any/", "plugin0", "", "test", "/all/");
190 checked_lookup_add (obj, "/any/", "/all/", "", "test", "ti0");
192 status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "",
193 /* expect new = */ 0);
194 assert (status == 0);
195 status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "",
196 /* expect new = */ 1);
197 assert (status == 1);
198 status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "ti0",
199 /* expect new = */ 1);
200 assert (status == 1);
201 status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "ti0",
202 /* expect new = */ 0);
203 assert (status == 2);
205 lookup_destroy (obj);
208 int main (int argc, char **argv) /* {{{ */
213 return (EXIT_SUCCESS);