Merge branch 'collectd-5.6'
[collectd.git] / src / utils_format_graphite_test.c
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_format_graphite.h"
31 #include "common.h" /* for STATIC_ARRAY_SIZE */
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 {
52   struct {
53     char *plugin_instance;
54     char *type_instance;
55     char *prefix;
56     char *suffix;
57     unsigned int flags;
58     char *want_name;
59   } cases[] = {
60     {
61       .want_name = "example@com.test.single",
62     },
63     /* plugin and type instances */
64     {
65       .plugin_instance = "foo",
66       .type_instance = "bar",
67       .want_name = "example@com.test-foo.single-bar",
68     },
69     {
70       .plugin_instance = NULL,
71       .type_instance = "bar",
72       .want_name = "example@com.test.single-bar",
73     },
74     {
75       .plugin_instance = "foo",
76       .type_instance = NULL,
77       .want_name = "example@com.test-foo.single",
78     },
79     /* special chars */
80     {
81       .plugin_instance = "foo (test)",
82       .type_instance = "test: \"hello\"",
83       .want_name = "example@com.test-foo@@test@.single-test@@@hello@",
84     },
85     /* flag GRAPHITE_SEPARATE_INSTANCES */
86     {
87       .plugin_instance = "foo",
88       .type_instance = "bar",
89       .flags = GRAPHITE_SEPARATE_INSTANCES,
90       .want_name = "example@com.test.foo.single.bar",
91     },
92     /* flag GRAPHITE_ALWAYS_APPEND_DS */
93     {
94       .plugin_instance = "foo",
95       .type_instance = "bar",
96       .flags = GRAPHITE_ALWAYS_APPEND_DS,
97       .want_name = "example@com.test-foo.single-bar.value",
98     },
99     /* flag GRAPHITE_PRESERVE_SEPARATOR */
100     {
101       .plugin_instance = "f.o.o",
102       .type_instance = "b.a.r",
103       .flags = 0,
104       .want_name = "example@com.test-f@o@o.single-b@a@r",
105     },
106     {
107       .plugin_instance = "f.o.o",
108       .type_instance = "b.a.r",
109       .flags = GRAPHITE_PRESERVE_SEPARATOR,
110       .want_name = "example.com.test-f.o.o.single-b.a.r",
111     },
112     /* prefix and suffix */
113     {
114       .prefix = "foo.",
115       .suffix = ".bar",
116       .want_name = "foo.example@com.bar.test.single",
117     },
118     {
119       .prefix = NULL,
120       .suffix = ".bar",
121       .want_name = "example@com.bar.test.single",
122     },
123     {
124       .prefix = "foo.",
125       .suffix = NULL,
126       .want_name = "foo.example@com.test.single",
127     },
128   };
129
130   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
131     value_list_t vl = {
132       .values = &(value_t){.gauge = 42},
133       .values_len = 1,
134       .time = TIME_T_TO_CDTIME_T_STATIC(1480063672),
135       .interval = TIME_T_TO_CDTIME_T_STATIC(10),
136       .host = "example.com",
137       .plugin = "test",
138       .type = "single",
139     };
140
141     char want[1024];
142     snprintf(want, sizeof(want), "%s 42 1480063672\r\n", cases[i].want_name);
143
144     if (cases[i].plugin_instance != NULL)
145       sstrncpy (vl.plugin_instance, cases[i].plugin_instance, sizeof (vl.plugin_instance));
146     if (cases[i].type_instance != NULL)
147       sstrncpy (vl.type_instance, cases[i].type_instance, sizeof (vl.type_instance));
148
149     char got[1024];
150     EXPECT_EQ_INT(0, format_graphite(got, sizeof(got), &ds_single, &vl, cases[i].prefix, cases[i].suffix, '@', cases[i].flags));
151     EXPECT_EQ_STR(want, got);
152   }
153
154   return 0;
155 }
156
157 int main (void)
158 {
159   RUN_TEST(metric_name);
160
161   END_TEST;
162 }