1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  CMetrics
4  *  ========
5  *  Copyright 2021 Eduardo Silva <eduardo@calyptia.com>
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 
20 #include <cmetrics/cmetrics.h>
21 #include <cmetrics/cmt_counter.h>
22 #include <cmetrics/cmt_encode_msgpack.h>
23 #include <cmetrics/cmt_decode_msgpack.h>
24 #include <cmetrics/cmt_encode_text.h>
25 
26 #include "cmt_tests.h"
27 
28 static struct cmt *generate_encoder_test_data()
29 {
30     double val;
31     uint64_t ts;
32     struct cmt *cmt;
33     struct cmt_counter *c1;
34     struct cmt_counter *c2;
35     struct cmt_counter *c3;
36 
37     ts = 0;
38     cmt = cmt_create();
39 
40     c1 = cmt_counter_create(cmt, "kubernetes", "", "load", "Network load",
41                             2, (char *[]) {"hostname", "app"});
42     cmt_counter_set(c1, ts, 10, 0, NULL);
43 
44     c2 = cmt_counter_create(cmt, "kubernetes", "", "cpu", "CPU load",
45                             2, (char *[]) {"hostname", "app"});
46     cmt_counter_set(c2, ts, 10, 0, NULL);
47 
48     return cmt;
49 }
50 
51 
52 void test_issue_54()
53 {
54     const char  expected_text[] = "1970-01-01T00:00:00.000000000Z kubernetes_load{tag1=\"tag1\",tag2=\"tag2\"} = 10\n" \
55                                   "1970-01-01T00:00:00.000000000Z kubernetes_cpu{tag1=\"tag1\",tag2=\"tag2\"} = 10\n";
56     cmt_sds_t   text_result;
57     size_t      mp1_size;
58     char       *mp1_buf;
59     size_t      offset;
60     int         result;
61     struct cmt *cmt2;
62     struct cmt *cmt1;
63 
64     cmt_initialize();
65 
66     /* Generate context with data */
67     cmt1 = generate_encoder_test_data();
68     TEST_CHECK(NULL != cmt1);
69 
70     /* append static labels */
71     cmt_label_add(cmt1, "tag1", "tag1");
72     cmt_label_add(cmt1, "tag2", "tag2");
73 
74     /* CMT1 -> Msgpack */
75     result = cmt_encode_msgpack_create(cmt1, &mp1_buf, &mp1_size);
76     TEST_CHECK(0 == result);
77 
78     /* Msgpack -> CMT2 */
79     offset = 0;
80     result = cmt_decode_msgpack_create(&cmt2, mp1_buf, mp1_size, &offset);
81     TEST_CHECK(0 == result);
82 
83     text_result = cmt_encode_text_create(cmt2);
84 
85     TEST_CHECK(NULL != text_result);
86     TEST_CHECK(0 == strcmp(text_result, expected_text));
87 
88     cmt_encode_text_destroy(text_result);
89     cmt_decode_msgpack_destroy(cmt2);
90     cmt_encode_msgpack_destroy(mp1_buf);
91     cmt_destroy(cmt1);
92 }
93 
94 TEST_LIST = {
95     {"issue_54", test_issue_54},
96     { 0 }
97 };
98