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>
27 #include "tests/macros.h"
29 #include "utils_vl_lookup.h"
31 static _Bool expect_new_obj = 0;
32 static _Bool have_new_obj = 0;
34 static identifier_t last_class_ident;
35 static identifier_t last_obj_ident;
37 static data_source_t dsrc_test = { "value", DS_TYPE_DERIVE, 0.0, NAN };
38 static data_set_t const ds_test = { "test", 1, &dsrc_test };
40 static data_source_t dsrc_unknown = { "value", DS_TYPE_DERIVE, 0.0, NAN };
41 static data_set_t const ds_unknown = { "unknown", 1, &dsrc_unknown };
43 static int lookup_obj_callback (data_set_t const *ds,
44 value_list_t const *vl,
45 void *user_class, void *user_obj)
47 identifier_t *class = user_class;
48 identifier_t *obj = user_obj;
50 OK1(expect_new_obj == have_new_obj,
51 (expect_new_obj ? "New obj is created." : "Updating existing obj."));
53 memcpy (&last_class_ident, class, sizeof (last_class_ident));
54 memcpy (&last_obj_ident, obj, sizeof (last_obj_ident));
56 if (strcmp (obj->plugin_instance, "failure") == 0)
62 static void *lookup_class_callback (data_set_t const *ds,
63 value_list_t const *vl, void *user_class)
65 identifier_t *class = user_class;
70 memcpy (&last_class_ident, class, sizeof (last_class_ident));
72 obj = malloc (sizeof (*obj));
73 strncpy (obj->host, vl->host, sizeof (obj->host));
74 strncpy (obj->plugin, vl->plugin, sizeof (obj->plugin));
75 strncpy (obj->plugin_instance, vl->plugin_instance, sizeof (obj->plugin_instance));
76 strncpy (obj->type, vl->type, sizeof (obj->type));
77 strncpy (obj->type_instance, vl->type_instance, sizeof (obj->type_instance));
81 return ((void *) obj);
84 static void checked_lookup_add (lookup_t *obj, /* {{{ */
86 char const *plugin, char const *plugin_instance,
87 char const *type, char const *type_instance,
88 unsigned int group_by)
93 memset (&ident, 0, sizeof (ident));
94 strncpy (ident.host, host, sizeof (ident.host));
95 strncpy (ident.plugin, plugin, sizeof (ident.plugin));
96 strncpy (ident.plugin_instance, plugin_instance, sizeof (ident.plugin_instance));
97 strncpy (ident.type, type, sizeof (ident.type));
98 strncpy (ident.type_instance, type_instance, sizeof (ident.type_instance));
100 user_class = malloc (sizeof (ident));
101 memmove (user_class, &ident, sizeof (ident));
103 OK(lookup_add (obj, &ident, group_by, user_class) == 0);
104 } /* }}} void test_add */
106 static int checked_lookup_search (lookup_t *obj,
108 char const *plugin, char const *plugin_instance,
109 char const *type, char const *type_instance,
113 value_list_t vl = VALUE_LIST_STATIC;
114 data_set_t const *ds = &ds_unknown;
116 strncpy (vl.host, host, sizeof (vl.host));
117 strncpy (vl.plugin, plugin, sizeof (vl.plugin));
118 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
119 strncpy (vl.type, type, sizeof (vl.type));
120 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
122 if (strcmp (vl.type, "test") == 0)
125 expect_new_obj = expect_new;
128 status = lookup_search (obj, ds, &vl);
132 static lookup_t *checked_lookup_create (void)
134 lookup_t *obj = lookup_create (
135 lookup_class_callback,
143 DEF_TEST(group_by_specific_host)
145 lookup_t *obj = checked_lookup_create ();
147 checked_lookup_add (obj, "/.*/", "test", "", "test", "/.*/", LU_GROUP_BY_HOST);
148 checked_lookup_search (obj, "host0", "test", "", "test", "0",
149 /* expect new = */ 1);
150 checked_lookup_search (obj, "host0", "test", "", "test", "1",
151 /* expect new = */ 0);
152 checked_lookup_search (obj, "host1", "test", "", "test", "0",
153 /* expect new = */ 1);
154 checked_lookup_search (obj, "host1", "test", "", "test", "1",
155 /* expect new = */ 0);
157 lookup_destroy (obj);
161 DEF_TEST(group_by_any_host)
163 lookup_t *obj = checked_lookup_create ();
165 checked_lookup_add (obj, "/.*/", "/.*/", "/.*/", "test", "/.*/", LU_GROUP_BY_HOST);
166 checked_lookup_search (obj, "host0", "plugin0", "", "test", "0",
167 /* expect new = */ 1);
168 checked_lookup_search (obj, "host0", "plugin0", "", "test", "1",
169 /* expect new = */ 0);
170 checked_lookup_search (obj, "host0", "plugin1", "", "test", "0",
171 /* expect new = */ 0);
172 checked_lookup_search (obj, "host0", "plugin1", "", "test", "1",
173 /* expect new = */ 0);
174 checked_lookup_search (obj, "host1", "plugin0", "", "test", "0",
175 /* expect new = */ 1);
176 checked_lookup_search (obj, "host1", "plugin0", "", "test", "1",
177 /* expect new = */ 0);
178 checked_lookup_search (obj, "host1", "plugin1", "", "test", "0",
179 /* expect new = */ 0);
180 checked_lookup_search (obj, "host1", "plugin1", "", "test", "1",
181 /* expect new = */ 0);
183 lookup_destroy (obj);
187 DEF_TEST(multiple_lookups)
189 lookup_t *obj = checked_lookup_create ();
192 checked_lookup_add (obj, "/.*/", "plugin0", "", "test", "/.*/", LU_GROUP_BY_HOST);
193 checked_lookup_add (obj, "/.*/", "/.*/", "", "test", "ti0", LU_GROUP_BY_HOST);
195 status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "",
196 /* expect new = */ 0);
197 assert (status == 0);
198 status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "",
199 /* expect new = */ 1);
200 assert (status == 1);
201 status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "ti0",
202 /* expect new = */ 1);
203 assert (status == 1);
204 status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "ti0",
205 /* expect new = */ 0);
206 assert (status == 2);
208 lookup_destroy (obj);
214 lookup_t *obj = checked_lookup_create ();
216 checked_lookup_add (obj, "/^db[0-9]\\./", "cpu", "/.*/", "cpu", "/.*/",
217 LU_GROUP_BY_TYPE_INSTANCE);
218 checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "user",
219 /* expect new = */ 1);
220 checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "idle",
221 /* expect new = */ 1);
222 checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "user",
223 /* expect new = */ 0);
224 checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "idle",
225 /* expect new = */ 0);
226 checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "user",
227 /* expect new = */ 0);
228 checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "idle",
229 /* expect new = */ 0);
230 checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "user",
231 /* expect new = */ 0);
232 checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "idle",
233 /* expect new = */ 0);
234 checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "system",
235 /* expect new = */ 1);
237 lookup_destroy (obj);
241 int main (int argc, char **argv) /* {{{ */
243 RUN_TEST(group_by_specific_host);
244 RUN_TEST(group_by_any_host);
245 RUN_TEST(multiple_lookups);