1 /**
2  * collectd - src/utils_format_stackdriver_test.c
3  * ISC license
4  *
5  * Copyright (C) 2017  Florian Forster
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * Authors:
20  *   Florian Forster <octo at collectd.org>
21  **/
22 
23 #include "collectd.h"
24 
25 #include "testing.h"
26 #include "utils/format_stackdriver/format_stackdriver.h"
27 
DEF_TEST(sd_format_metric_descriptor)28 DEF_TEST(sd_format_metric_descriptor) {
29   value_list_t vl = {
30       .host = "example.com",
31       .plugin = "unit-test",
32       .type = "example",
33   };
34   char got[1024];
35 
36   data_set_t ds_single = {
37       .type = "example",
38       .ds_num = 1,
39       .ds =
40           &(data_source_t){
41               .name = "value",
42               .type = DS_TYPE_GAUGE,
43               .min = NAN,
44               .max = NAN,
45           },
46   };
47   EXPECT_EQ_INT(
48       0, sd_format_metric_descriptor(got, sizeof(got), &ds_single, &vl, 0));
49   char const *want_single =
50       "{\"type\":\"custom.googleapis.com/collectd/unit_test/"
51       "example\",\"metricKind\":\"GAUGE\",\"valueType\":\"DOUBLE\",\"labels\":["
52       "{\"key\":\"host\",\"valueType\":\"STRING\"},{\"key\":\"plugin_"
53       "instance\",\"valueType\":\"STRING\"},{\"key\":\"type_instance\","
54       "\"valueType\":\"STRING\"}]}";
55   EXPECT_EQ_STR(want_single, got);
56 
57   data_set_t ds_double = {
58       .type = "example",
59       .ds_num = 2,
60       .ds =
61           (data_source_t[]){
62               {.name = "one", .type = DS_TYPE_DERIVE, .min = 0, .max = NAN},
63               {.name = "two", .type = DS_TYPE_DERIVE, .min = 0, .max = NAN},
64           },
65   };
66   EXPECT_EQ_INT(
67       0, sd_format_metric_descriptor(got, sizeof(got), &ds_double, &vl, 0));
68   char const *want_double =
69       "{\"type\":\"custom.googleapis.com/collectd/unit_test/"
70       "example_one\",\"metricKind\":\"CUMULATIVE\",\"valueType\":\"INT64\","
71       "\"labels\":[{\"key\":\"host\",\"valueType\":\"STRING\"},{\"key\":"
72       "\"plugin_instance\",\"valueType\":\"STRING\"},{\"key\":\"type_"
73       "instance\",\"valueType\":\"STRING\"}]}";
74   EXPECT_EQ_STR(want_double, got);
75   return 0;
76 }
77 
main(int argc,char ** argv)78 int main(int argc, char **argv) {
79   RUN_TEST(sd_format_metric_descriptor);
80 
81   END_TEST;
82 }
83