blob: e600743c8fa745c40c0e8445a0782d85d598907f [file] [log] [blame]
Juha Hyttinen90f6dd42020-05-08 12:17:05 +03001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17==================================================================================
18*/
19
20package xapp
21
22import (
23 "testing"
24)
25
26var mCVect map[string]CounterVec
27var mGVect map[string]GaugeVec
Mohamed Abukared119192020-11-19 14:38:22 +020028var mGGroup map[string]Gauge
Juha Hyttinen90f6dd42020-05-08 12:17:05 +030029
30func TestMetricSetup(t *testing.T) {
31 mCVect = Metric.RegisterCounterVecGroup(
32 []CounterOpts{
33 {Name: "counter1", Help: "counter1"},
34 },
35 []string{"name", "event"},
36 "SUBSYSTEM")
37
38 mGVect = Metric.RegisterGaugeVecGroup(
39 []CounterOpts{
40 {Name: "counter2", Help: "counter2"},
41 },
42 []string{"name", "event"},
43 "SUBSYSTEM")
Mohamed Abukared119192020-11-19 14:38:22 +020044
45 mGGroup = Metric.RegisterGaugeGroup(
46 []CounterOpts{
47 {Name: "counter3", Help: "counter3"},
48 },
49 "SUBSYSTEM2")
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +020050}
51
52func TestMetricCounter(t *testing.T) {
53 var TestCounterOpts = []CounterOpts{
54 {Name: "Blaah1", Help: "Blaah1"},
55 {Name: "Blaah2", Help: "Blaah2"},
56 {Name: "Blaah3", Help: "Blaah3"},
57 {Name: "Blaah4", Help: "Blaah4"},
58 }
59
60 ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
61
62 if len(ret1) == 0 {
63 t.Errorf("ret1 counter group is empty")
64 }
65
66 ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
67
68 if len(ret2) == 0 {
69 t.Errorf("ret2 counter group is empty")
70 }
71
72 if len(ret1) != len(ret2) {
73 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
74 }
Juha Hyttinen90f6dd42020-05-08 12:17:05 +030075}
76
77func TestMetricCounterVector(t *testing.T) {
78 //
79 //
80 c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
81 if _, ok := c_grp1["counter1"]; ok == false {
82 t.Errorf("c_grp1 counter1 not exists")
83 }
84 c_grp1["counter1"].Inc()
85
86 //
87 //
88 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
89 if _, ok := c_grp2["counter1"]; ok == false {
90 t.Errorf("c_grp2 counter1 not exists")
91 }
92 c_grp2["counter1"].Inc()
93}
94
95func TestMetricGaugeVector(t *testing.T) {
96 //
97 //
98 g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
99 if _, ok := g_grp1["counter2"]; ok == false {
100 t.Errorf("g_grp1 counter2 not exists")
101 }
102 g_grp1["counter2"].Inc()
103
104 //
105 //
106 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
107 if _, ok := g_grp2["counter2"]; ok == false {
108 t.Errorf("g_grp2 counter2 not exists")
109 }
110 g_grp2["counter2"].Inc()
111}
112
113func TestMetricCounterVectorPrefix(t *testing.T) {
114 //
115 //
116 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
117 if _, ok := c_grp1["event1_counter1"]; ok == false {
118 t.Errorf("c_grp1 event1_counter1 not exists")
119 }
120 c_grp1["event1_counter1"].Inc()
121
122 //
123 //
124 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
125 if _, ok := c_grp2["event2_counter1"]; ok == false {
126 t.Errorf("c_grp2 event2_counter1 not exists")
127 }
128 c_grp2["event2_counter1"].Inc()
129
130 //
131 //
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200132 m_grp := NewMetricGroupsCache()
133 m_grp.CombineCounterGroups(c_grp1, c_grp2)
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300134
135 //
136 //
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200137 if m_grp.CIs("event1_counter1") == false {
138 t.Errorf("m_grp event1_counter1 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300139 }
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200140 m_grp.CInc("event1_counter1")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300141
142 //
143 //
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200144 if m_grp.CIs("event2_counter1") == false {
145 t.Errorf("m_grp event2_counter1 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300146 }
Mohamed Abukared119192020-11-19 14:38:22 +0200147
148 m_grp.CAdd("event2_counter1", 1)
149 m_grp.CGet("event2_counter1")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300150}
151
152func TestMetricGaugeVectorPrefix(t *testing.T) {
153 //
154 //
155 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
156 if _, ok := g_grp1["event1_counter2"]; ok == false {
157 t.Errorf("g_grp1 event1_counter2 not exists")
158 }
159 g_grp1["event1_counter2"].Inc()
160
161 //
162 //
163 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
164 if _, ok := g_grp2["event2_counter2"]; ok == false {
165 t.Errorf("g_grp2 event2_counter2 not exists")
166 }
167 g_grp2["event2_counter2"].Inc()
168
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200169 m_grp := NewMetricGroupsCache()
170 m_grp.CombineGaugeGroups(g_grp1, g_grp2)
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300171
172 //
173 //
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200174 if m_grp.GIs("event1_counter2") == false {
175 t.Errorf("m_grp event1_counter2 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300176 }
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200177 m_grp.GInc("event1_counter2")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300178
179 //
180 //
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200181 if m_grp.GIs("event2_counter2") == false {
182 t.Errorf("m_grp event2_counter2 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300183 }
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200184 m_grp.GInc("event2_counter2")
Mohamed Abukared119192020-11-19 14:38:22 +0200185
186 m_grp.GGet("event2_counter2")
187 m_grp.GDec("event2_counter2")
188 m_grp.GSet("event2_counter2", 1)
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300189}
190
191func TestMetricGroupCache(t *testing.T) {
192 //
193 //
194 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
195 if _, ok := c_grp1["event1_counter1"]; ok == false {
196 t.Errorf("c_grp1 event1_counter1 not exists")
197 }
198 c_grp1["event1_counter1"].Inc()
199
200 //
201 //
202 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
203 if _, ok := c_grp2["event2_counter1"]; ok == false {
204 t.Errorf("c_grp2 event2_counter1 not exists")
205 }
206 c_grp2["event2_counter1"].Inc()
207
208 //
209 //
210 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
211 if _, ok := g_grp1["event1_counter2"]; ok == false {
212 t.Errorf("g_grp1 event1_counter2 not exists")
213 }
214 g_grp1["event1_counter2"].Inc()
215
216 //
217 //
218 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
219 if _, ok := g_grp2["event2_counter2"]; ok == false {
220 t.Errorf("g_grp2 event2_counter2 not exists")
221 }
222 g_grp2["event2_counter2"].Inc()
223
224 //
225 //
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200226 m_grp := NewMetricGroupsCache()
227 m_grp.CombineCounterGroups(c_grp1)
228 m_grp.CombineCounterGroups(c_grp2)
229 m_grp.CombineGaugeGroups(g_grp1)
230 m_grp.CombineGaugeGroups(g_grp2)
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300231
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200232 if m_grp == nil {
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300233 t.Errorf("Cache failed")
234 }
235
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200236 if m_grp.CIs("event1_counter1") == false {
237 t.Errorf("m_grp.Counters event1_counter1 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300238 }
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200239 m_grp.CInc("event1_counter1")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300240
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200241 if m_grp.CIs("event2_counter1") == false {
242 t.Errorf("m_grp.Counters event2_counter1 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300243 }
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200244 m_grp.CInc("event2_counter1")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300245
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200246 if m_grp.GIs("event1_counter2") == false {
247 t.Errorf("m_grp.Gauges event1_counter2 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300248 }
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200249 m_grp.GInc("event1_counter2")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300250
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200251 if m_grp.GIs("event2_counter2") == false {
252 t.Errorf("m_grp.Gauges event2_counter2 not exists")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300253 }
Juha Hyttinen5bfa60a2020-10-30 10:31:39 +0200254 m_grp.GInc("event2_counter2")
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300255
Mohamed Abukared119192020-11-19 14:38:22 +0200256 m_grp.CAdd("event2_counter1", 1)
257 m_grp.CGet("event2_counter1")
258 m_grp.GGet("event2_counter2")
259 m_grp.GDec("event2_counter2")
260 m_grp.GSet("event2_counter2", 1)
Juha Hyttinen90f6dd42020-05-08 12:17:05 +0300261}
Mohamed Abukared119192020-11-19 14:38:22 +0200262