Debian Codegraph @

src/utils/format_graphite/format_graphite_test.c

raw
1 /**
2 * collectd - src/utils_format_graphite_test.c
3 * Copyright (C) 2016 Florian octo 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 octo Forster <octo at collectd.org>
25 */
26
27 #include "collectd.h"
28
29 #include "testing.h"
30 #include "utils/common/common.h" /* for STATIC_ARRAY_SIZE */
31 #include "utils/format_graphite/format_graphite.h"
32
33 static data_set_t ds_single = {
34 .type = "single",
35 .ds_num = 1,
36 .ds = &(data_source_t){"value", DS_TYPE_GAUGE, NAN, NAN},
37 };
38
39 /*
40 static data_set_t ds_double = {
41 .type = "double",
42 .ds_num = 2,
43 .ds =
44 (data_source_t[]){
45 {"one", DS_TYPE_DERIVE, 0, NAN}, {"two", DS_TYPE_DERIVE, 0, NAN},
46 },
47 };
48 */
49
50 DEF_TEST(metric_name) {
51 struct {
52 const char *plugin_instance;
53 const char *type_instance;
54 const char *prefix;
55 const char *suffix;
56 unsigned int flags;
57 const char *want_name;
58 } cases[] = {
59 {
60 .want_name = "[email protected]",
61 },
62 /* plugin and type instances */
63 {
64 .plugin_instance = "foo",
65 .type_instance = "bar",
66 .want_name = "[email protected]",
67 },
68 {
69 .plugin_instance = NULL,
70 .type_instance = "bar",
71 .want_name = "[email protected]",
72 },
73 {
74 .plugin_instance = "foo",
75 .type_instance = NULL,
76 .want_name = "[email protected]",
77 },
78 /* special chars */
79 {
80 .plugin_instance = "foo (test)",
81 .type_instance = "test: \"hello\"",
82 .want_name = "[email protected]@@[email protected]@@@hello@",
83 },
84 /* test escaping comma */
85 {
86 .plugin_instance = "foo (test)",
87 .type_instance = "test,123,",
88 .want_name = "[email protected]@@[email protected]@123@",
89 },
90 /* flag GRAPHITE_SEPARATE_INSTANCES */
91 {
92 .plugin_instance = "foo",
93 .type_instance = "bar",
94 .flags = GRAPHITE_SEPARATE_INSTANCES,
95 .want_name = "[email protected]",
96 },
97 /* flag GRAPHITE_ALWAYS_APPEND_DS */
98 {
99 .plugin_instance = "foo",
100 .type_instance = "bar",
101 .flags = GRAPHITE_ALWAYS_APPEND_DS,
102 .want_name = "[email protected]",
103 },
104 /* flag GRAPHITE_PRESERVE_SEPARATOR */
105 {
106 .plugin_instance = "f.o.o",
107 .type_instance = "b.a.r",
108 .flags = 0,
109 .want_name = "[email protected]@[email protected]@a@r",
110 },
111 {
112 .plugin_instance = "f.o.o",
113 .type_instance = "b.a.r",
114 .flags = GRAPHITE_PRESERVE_SEPARATOR,
115 .want_name = "example.com.test-f.o.o.single-b.a.r",
116 },
117 /* prefix and suffix */
118 {
119 .prefix = "foo.",
120 .suffix = ".bar",
121 .want_name = "[email protected]",
122 },
123 {
124 .prefix = NULL,
125 .suffix = ".bar",
126 .want_name = "[email protected]",
127 },
128 {
129 .prefix = "foo.",
130 .suffix = NULL,
131 .want_name = "[email protected]",
132 },
133 /* flag GRAPHITE_USE_TAGS */
134 {.flags = GRAPHITE_USE_TAGS,
135 .want_name = "test.single;host=example.com;plugin=test;type=single"},
136 {.plugin_instance = "f.o.o",
137 .type_instance = "b.a.r",
138 .flags = GRAPHITE_USE_TAGS,
139 .want_name = "test.single;host=example.com;plugin=test;plugin_instance="
140 "f.o.o;type=single;type_instance=b.a.r"},
141 {.flags = GRAPHITE_USE_TAGS ^ GRAPHITE_ALWAYS_APPEND_DS,
142 .want_name = "test.single.value;host=example.com;plugin=test;type="
143 "single;ds_name=value"},
144 {.plugin_instance = "foo",
145 .type_instance = "foo",
146 .flags = GRAPHITE_USE_TAGS ^ GRAPHITE_DROP_DUPE_FIELDS,
147 .want_name = "test.single;host=example.com;plugin=test;plugin_instance="
148 "foo;type=single"},
149 };
150
151 for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
152 value_list_t vl = {
153 .values = &(value_t){.gauge = 42},
154 .values_len = 1,
155 .time = TIME_T_TO_CDTIME_T_STATIC(1480063672),
156 .interval = TIME_T_TO_CDTIME_T_STATIC(10),
157 .host = "example.com",
158 .plugin = "test",
159 .type = "single",
160 };
161
162 char want[1024];
163 ssnprintf(want, sizeof(want), "%s 42 1480063672\r\n", cases[i].want_name);
164
165 if (cases[i].plugin_instance != NULL)
166 sstrncpy(vl.plugin_instance, cases[i].plugin_instance,
167 sizeof(vl.plugin_instance));
168 if (cases[i].type_instance != NULL)
169 sstrncpy(vl.type_instance, cases[i].type_instance,
170 sizeof(vl.type_instance));
171
172 char got[1024];
173 EXPECT_EQ_INT(0, format_graphite(got, sizeof(got), &ds_single, &vl,
174 cases[i].prefix, cases[i].suffix, '@',
175 cases[i].flags));
176 EXPECT_EQ_STR(want, got);
177 }
178
179 return 0;
180 }
181
182 DEF_TEST(null_termination) {
183 value_list_t vl = {
184 .values = &(value_t){.gauge = 1337},
185 .values_len = 1,
186 .time = TIME_T_TO_CDTIME_T_STATIC(1480063672),
187 .interval = TIME_T_TO_CDTIME_T_STATIC(10),
188 .host = "example.com",
189 .plugin = "test",
190 .type = "single",
191 };
192 char const *want = "example_com.test.single 1337 1480063672\r\n";
193
194 char buffer[128];
195 for (size_t i = 0; i < sizeof(buffer); i++)
196 buffer[i] = (char)i;
197
198 EXPECT_EQ_INT(0, format_graphite(buffer, sizeof(buffer), &ds_single, &vl,
199 NULL, NULL, '_', 0));
200 EXPECT_EQ_STR(want, buffer);
201 EXPECT_EQ_INT(0, buffer[strlen(want)]);
202 for (size_t i = strlen(want) + 1; i < sizeof(buffer); i++)
203 EXPECT_EQ_INT((int)i, (int)buffer[i]);
204
205 return 0;
206 }
207
208 int main(void) {
209 RUN_TEST(metric_name);
210 RUN_TEST(null_termination);
211
212 END_TEST;
213 }