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
sample_data()28 static struct cmt *sample_data()
29 {
30 double val;
31 uint64_t ts;
32 struct cmt *cmt;
33 struct cmt_counter *c1;
34 struct cmt_counter *c2;
35
36 cmt = cmt_create();
37
38 c1 = cmt_counter_create(cmt, "kubernetes", "network", "load", "Network load",
39 2, (char *[]) {"hostname", "app"});
40
41 ts = 0;
42
43 cmt_counter_get_val(c1, 0, NULL, &val);
44 cmt_counter_inc(c1, ts, 0, NULL);
45 cmt_counter_add(c1, ts, 2, 0, NULL);
46 cmt_counter_get_val(c1, 0, NULL, &val);
47
48 cmt_counter_inc(c1, ts, 2, (char *[]) {"localhost", "cmetrics"});
49 cmt_counter_get_val(c1, 2, (char *[]) {"localhost", "cmetrics"}, &val);
50 cmt_counter_add(c1, ts, 10.55, 2, (char *[]) {"localhost", "test"});
51 cmt_counter_get_val(c1, 2, (char *[]) {"localhost", "test"}, &val);
52 cmt_counter_set(c1, ts, 12.15, 2, (char *[]) {"localhost", "test"});
53 cmt_counter_set(c1, ts, 1, 2, (char *[]) {"localhost", "test"});
54
55
56 c2 = cmt_counter_create(cmt, "kubernetes", "network", "cpu", "CPU load",
57 2, (char *[]) {"hostname", "app"});
58
59 ts = 0;
60
61 cmt_counter_get_val(c2, 0, NULL, &val);
62 cmt_counter_inc(c2, ts, 0, NULL);
63 cmt_counter_add(c2, ts, 2, 0, NULL);
64 cmt_counter_get_val(c2, 0, NULL, &val);
65
66 cmt_counter_inc(c2, ts, 2, (char *[]) {"localhost", "cmetrics"});
67 cmt_counter_get_val(c2, 2, (char *[]) {"localhost", "cmetrics"}, &val);
68 cmt_counter_add(c2, ts, 10.55, 2, (char *[]) {"localhost", "test"});
69 cmt_counter_get_val(c2, 2, (char *[]) {"localhost", "test"}, &val);
70 cmt_counter_set(c2, ts, 12.15, 2, (char *[]) {"localhost", "test"});
71 cmt_counter_set(c2, ts, 1, 2, (char *[]) {"localhost", "test"});
72
73 return cmt;
74 }
75
test_basic()76 void test_basic()
77 {
78 int ret;
79 int error;
80 size_t off = 0;
81 cmt_sds_t text1;
82 cmt_sds_t text2;
83 char *mp_buf;
84 size_t mp_size;
85 struct cmt *cmt1;
86 struct cmt *cmt2;
87
88 cmt1 = sample_data();
89 TEST_CHECK(cmt1 != NULL);
90
91 /* encode to text */
92 text1 = cmt_encode_text_create(cmt1);
93 TEST_CHECK(text1 != NULL);
94
95 /* encode to msgpack */
96 ret = cmt_encode_msgpack_create(cmt1, &mp_buf, &mp_size);
97 TEST_CHECK(ret == 0);
98
99 /* decode msgpack into cmt2 */
100 ret = cmt_decode_msgpack_create(&cmt2, mp_buf, mp_size, &off);
101 TEST_CHECK(ret == 0);
102
103 /* encode cmt2 to text */
104 text2 = cmt_encode_text_create(cmt2);
105 TEST_CHECK(text2 != NULL);
106
107 /* compate both texts */
108 error = 0;
109 if ((cmt_sds_len(text1) != cmt_sds_len(text2)) ||
110 strcmp(text1, text2) != 0) {
111
112 printf("\n");
113 printf("====== EXPECTED OUTPUT =====\n%s", text1);
114 printf("\n\n");
115 printf("====== RECEIVED OUTPUT =====\n%s\n", text2);
116 error = 1;
117 }
118 TEST_CHECK(error == 0);
119
120 cmt_encode_msgpack_destroy(mp_buf);
121 cmt_encode_text_destroy(text1);
122 cmt_encode_text_destroy(text2);
123 cmt_destroy(cmt1);
124 cmt_destroy(cmt2);
125 }
126
127 TEST_LIST = {
128 {"basic", test_basic},
129 { 0 }
130 };
131