=========
[ric-plt/xapp-frame.git] / pkg / xapp / metrics_test.go
1 /*
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
20 package xapp
21
22 import (
23         "testing"
24 )
25
26 var mCVect map[string]CounterVec
27 var mGVect map[string]GaugeVec
28
29 func TestMetricSetup(t *testing.T) {
30         mCVect = Metric.RegisterCounterVecGroup(
31                 []CounterOpts{
32                         {Name: "counter1", Help: "counter1"},
33                 },
34                 []string{"name", "event"},
35                 "SUBSYSTEM")
36
37         mGVect = Metric.RegisterGaugeVecGroup(
38                 []CounterOpts{
39                         {Name: "counter2", Help: "counter2"},
40                 },
41                 []string{"name", "event"},
42                 "SUBSYSTEM")
43 }
44
45 func TestMetricCounterVector(t *testing.T) {
46         //
47         //
48         c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
49         if _, ok := c_grp1["counter1"]; ok == false {
50                 t.Errorf("c_grp1 counter1 not exists")
51         }
52         c_grp1["counter1"].Inc()
53
54         //
55         //
56         c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
57         if _, ok := c_grp2["counter1"]; ok == false {
58                 t.Errorf("c_grp2 counter1 not exists")
59         }
60         c_grp2["counter1"].Inc()
61 }
62
63 func TestMetricGaugeVector(t *testing.T) {
64         //
65         //
66         g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
67         if _, ok := g_grp1["counter2"]; ok == false {
68                 t.Errorf("g_grp1 counter2 not exists")
69         }
70         g_grp1["counter2"].Inc()
71
72         //
73         //
74         g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
75         if _, ok := g_grp2["counter2"]; ok == false {
76                 t.Errorf("g_grp2 counter2 not exists")
77         }
78         g_grp2["counter2"].Inc()
79 }
80
81 func TestMetricCounterVectorPrefix(t *testing.T) {
82         //
83         //
84         c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
85         if _, ok := c_grp1["event1_counter1"]; ok == false {
86                 t.Errorf("c_grp1 event1_counter1 not exists")
87         }
88         c_grp1["event1_counter1"].Inc()
89
90         //
91         //
92         c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
93         if _, ok := c_grp2["event2_counter1"]; ok == false {
94                 t.Errorf("c_grp2 event2_counter1 not exists")
95         }
96         c_grp2["event2_counter1"].Inc()
97
98         //
99         //
100         c_grp := Metric.CombineCounterGroups(c_grp1, c_grp2)
101
102         //
103         //
104         if _, ok := c_grp["event1_counter1"]; ok == false {
105                 t.Errorf("c_grp event1_counter1 not exists")
106         }
107         c_grp["event1_counter1"].Inc()
108
109         //
110         //
111         if _, ok := c_grp["event2_counter1"]; ok == false {
112                 t.Errorf("c_grp event2_counter1 not exists")
113         }
114         c_grp["event2_counter1"].Inc()
115 }
116
117 func TestMetricGaugeVectorPrefix(t *testing.T) {
118         //
119         //
120         g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
121         if _, ok := g_grp1["event1_counter2"]; ok == false {
122                 t.Errorf("g_grp1 event1_counter2 not exists")
123         }
124         g_grp1["event1_counter2"].Inc()
125
126         //
127         //
128         g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
129         if _, ok := g_grp2["event2_counter2"]; ok == false {
130                 t.Errorf("g_grp2 event2_counter2 not exists")
131         }
132         g_grp2["event2_counter2"].Inc()
133
134         //
135         //
136         g_grp := Metric.CombineGaugeGroups(g_grp1, g_grp2)
137
138         //
139         //
140         if _, ok := g_grp["event1_counter2"]; ok == false {
141                 t.Errorf("g_grp event1_counter2 not exists")
142         }
143         g_grp["event1_counter2"].Inc()
144
145         //
146         //
147         if _, ok := g_grp["event2_counter2"]; ok == false {
148                 t.Errorf("g_grp event2_counter2 not exists")
149         }
150         g_grp["event2_counter2"].Inc()
151 }
152
153 func TestMetricGroupCache(t *testing.T) {
154         //
155         //
156         c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
157         if _, ok := c_grp1["event1_counter1"]; ok == false {
158                 t.Errorf("c_grp1 event1_counter1 not exists")
159         }
160         c_grp1["event1_counter1"].Inc()
161
162         //
163         //
164         c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
165         if _, ok := c_grp2["event2_counter1"]; ok == false {
166                 t.Errorf("c_grp2 event2_counter1 not exists")
167         }
168         c_grp2["event2_counter1"].Inc()
169
170         //
171         //
172         g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
173         if _, ok := g_grp1["event1_counter2"]; ok == false {
174                 t.Errorf("g_grp1 event1_counter2 not exists")
175         }
176         g_grp1["event1_counter2"].Inc()
177
178         //
179         //
180         g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
181         if _, ok := g_grp2["event2_counter2"]; ok == false {
182                 t.Errorf("g_grp2 event2_counter2 not exists")
183         }
184         g_grp2["event2_counter2"].Inc()
185
186         //
187         //
188         cacheid := "CACHEID"
189         entry := Metric.GroupCacheGet(cacheid)
190         if entry == nil {
191                 Metric.GroupCacheAddCounters(cacheid, c_grp1)
192                 Metric.GroupCacheAddCounters(cacheid, c_grp2)
193                 Metric.GroupCacheAddGauges(cacheid, g_grp1)
194                 Metric.GroupCacheAddGauges(cacheid, g_grp2)
195                 entry = Metric.GroupCacheGet(cacheid)
196         }
197
198         if entry == nil {
199                 t.Errorf("Cache failed")
200         }
201
202         if _, ok := entry.Counters["event1_counter1"]; ok == false {
203                 t.Errorf("entry.Counters event1_counter1 not exists")
204         }
205         entry.Counters["event1_counter1"].Inc()
206
207         if _, ok := entry.Counters["event2_counter1"]; ok == false {
208                 t.Errorf("entry.Counters event2_counter1 not exists")
209         }
210         entry.Counters["event2_counter1"].Inc()
211
212         if _, ok := entry.Gauges["event1_counter2"]; ok == false {
213                 t.Errorf("entry.Gauges event1_counter2 not exists")
214         }
215         entry.Gauges["event1_counter2"].Inc()
216
217         if _, ok := entry.Gauges["event2_counter2"]; ok == false {
218                 t.Errorf("entry.Gauges event2_counter2 not exists")
219         }
220         entry.Gauges["event2_counter2"].Inc()
221
222 }