Merge branch 'collectd-5.6'
[collectd.git] / src / utils_vl_lookup_test.c
1 /**
2  * collectd - src/tests/test_utils_vl_lookup.c
3  * Copyright (C) 2012       Florian Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "testing.h"
30 #include "utils_vl_lookup.h"
31
32 static _Bool expect_new_obj = 0;
33 static _Bool have_new_obj = 0;
34
35 static identifier_t last_class_ident;
36 static identifier_t last_obj_ident;
37
38 static data_source_t dsrc_test = { "value", DS_TYPE_DERIVE, 0.0, NAN };
39 static data_set_t const ds_test = { "test", 1, &dsrc_test };
40
41 static data_source_t dsrc_unknown = { "value", DS_TYPE_DERIVE, 0.0, NAN };
42 static data_set_t const ds_unknown = { "unknown", 1, &dsrc_unknown };
43
44 static int lookup_obj_callback (data_set_t const *ds,
45     value_list_t const *vl,
46     void *user_class, void *user_obj)
47 {
48   identifier_t *class = user_class;
49   identifier_t *obj = user_obj;
50
51   OK1(expect_new_obj == have_new_obj,
52       (expect_new_obj ? "New obj is created." : "Updating existing obj."));
53
54   memcpy (&last_class_ident, class, sizeof (last_class_ident));
55   memcpy (&last_obj_ident, obj, sizeof (last_obj_ident));
56
57   if (strcmp (obj->plugin_instance, "failure") == 0)
58     return (-1);
59
60   return (0);
61 }
62
63 static void *lookup_class_callback (data_set_t const *ds,
64     value_list_t const *vl, void *user_class)
65 {
66   identifier_t *class = user_class;
67   identifier_t *obj;
68
69   assert (expect_new_obj);
70
71   memcpy (&last_class_ident, class, sizeof (last_class_ident));
72
73   obj = malloc (sizeof (*obj));
74   strncpy (obj->host, vl->host, sizeof (obj->host));
75   strncpy (obj->plugin, vl->plugin, sizeof (obj->plugin));
76   strncpy (obj->plugin_instance, vl->plugin_instance, sizeof (obj->plugin_instance));
77   strncpy (obj->type, vl->type, sizeof (obj->type));
78   strncpy (obj->type_instance, vl->type_instance, sizeof (obj->type_instance));
79
80   have_new_obj = 1;
81
82   return ((void *) obj);
83 }
84
85 static int checked_lookup_add (lookup_t *obj, /* {{{ */
86     char const *host,
87     char const *plugin, char const *plugin_instance,
88     char const *type, char const *type_instance,
89     unsigned int group_by)
90 {
91   identifier_t ident;
92   void *user_class;
93
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));
99
100   user_class = malloc (sizeof (ident));
101   memmove (user_class, &ident, sizeof (ident));
102
103   OK(lookup_add (obj, &ident, group_by, user_class) == 0);
104   return 0;
105 } /* }}} int checked_lookup_add */
106
107 static int checked_lookup_search (lookup_t *obj,
108     char const *host,
109     char const *plugin, char const *plugin_instance,
110     char const *type, char const *type_instance,
111     _Bool expect_new)
112 {
113   int status;
114   value_list_t vl = VALUE_LIST_INIT;
115   data_set_t const *ds = &ds_unknown;
116
117   strncpy (vl.host, host, sizeof (vl.host));
118   strncpy (vl.plugin, plugin, sizeof (vl.plugin));
119   strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
120   strncpy (vl.type, type, sizeof (vl.type));
121   strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
122
123   if (strcmp (vl.type, "test") == 0)
124     ds = &ds_test;
125
126   expect_new_obj = expect_new;
127   have_new_obj = 0;
128
129   status = lookup_search (obj, ds, &vl);
130   return (status);
131 }
132
133 DEF_TEST(group_by_specific_host)
134 {
135   lookup_t *obj;
136   CHECK_NOT_NULL (obj = lookup_create (
137         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
138
139   checked_lookup_add (obj, "/.*/", "test", "", "test", "/.*/", LU_GROUP_BY_HOST);
140   checked_lookup_search (obj, "host0", "test", "", "test", "0",
141       /* expect new = */ 1);
142   checked_lookup_search (obj, "host0", "test", "", "test", "1",
143       /* expect new = */ 0);
144   checked_lookup_search (obj, "host1", "test", "", "test", "0",
145       /* expect new = */ 1);
146   checked_lookup_search (obj, "host1", "test", "", "test", "1",
147       /* expect new = */ 0);
148
149   lookup_destroy (obj);
150   return (0);
151 }
152
153 DEF_TEST(group_by_any_host)
154 {
155   lookup_t *obj;
156   CHECK_NOT_NULL (obj = lookup_create (
157         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
158
159   checked_lookup_add (obj, "/.*/", "/.*/", "/.*/", "test", "/.*/", LU_GROUP_BY_HOST);
160   checked_lookup_search (obj, "host0", "plugin0", "", "test", "0",
161       /* expect new = */ 1);
162   checked_lookup_search (obj, "host0", "plugin0", "", "test", "1",
163       /* expect new = */ 0);
164   checked_lookup_search (obj, "host0", "plugin1", "", "test", "0",
165       /* expect new = */ 0);
166   checked_lookup_search (obj, "host0", "plugin1", "", "test", "1",
167       /* expect new = */ 0);
168   checked_lookup_search (obj, "host1", "plugin0", "", "test", "0",
169       /* expect new = */ 1);
170   checked_lookup_search (obj, "host1", "plugin0", "", "test", "1",
171       /* expect new = */ 0);
172   checked_lookup_search (obj, "host1", "plugin1", "", "test", "0",
173       /* expect new = */ 0);
174   checked_lookup_search (obj, "host1", "plugin1", "", "test", "1",
175       /* expect new = */ 0);
176
177   lookup_destroy (obj);
178   return (0);
179 }
180
181 DEF_TEST(multiple_lookups)
182 {
183   lookup_t *obj;
184   int status;
185
186   CHECK_NOT_NULL (obj = lookup_create (
187         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
188
189   checked_lookup_add (obj, "/.*/", "plugin0", "", "test", "/.*/", LU_GROUP_BY_HOST);
190   checked_lookup_add (obj, "/.*/", "/.*/", "", "test", "ti0", LU_GROUP_BY_HOST);
191
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);
204
205   lookup_destroy (obj);
206   return (0);
207 }
208
209 DEF_TEST(regex)
210 {
211   lookup_t *obj;
212   CHECK_NOT_NULL (obj = lookup_create (
213         lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
214
215   checked_lookup_add (obj, "/^db[0-9]\\./", "cpu", "/.*/", "cpu", "/.*/",
216       LU_GROUP_BY_TYPE_INSTANCE);
217   checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "user",
218       /* expect new = */ 1);
219   checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "idle",
220       /* expect new = */ 1);
221   checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "user",
222       /* expect new = */ 0);
223   checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "idle",
224       /* expect new = */ 0);
225   checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "user",
226       /* expect new = */ 0);
227   checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "idle",
228       /* expect new = */ 0);
229   checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "user",
230       /* expect new = */ 0);
231   checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "idle",
232       /* expect new = */ 0);
233   checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "system",
234       /* expect new = */ 1);
235
236   lookup_destroy (obj);
237   return (0);
238 }
239
240 int main (int argc, char **argv) /* {{{ */
241 {
242   RUN_TEST(group_by_specific_host);
243   RUN_TEST(group_by_any_host);
244   RUN_TEST(multiple_lookups);
245   RUN_TEST(regex);
246
247   END_TEST;
248 } /* }}} int main */