d65baa27a38fc5ee684bc75c29479258fb9ac20b
[ric-plt/vespamgr.git] / cmd / vespamgr / config_test.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-2019 Nokia.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18  *  platform project (RICP).
19  */
20 package main
21
22 import (
23         "bytes"
24         "encoding/json"
25         "io/ioutil"
26         "testing"
27         "time"
28
29         "github.com/stretchr/testify/assert"
30         "gopkg.in/yaml.v2"
31 )
32
33 var vespaMgr *VespaMgr
34
35 func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
36         vespaMgr = NewVespaMgr()
37
38         assert.Equal(t, "/tmp/data", vesconf.DataDir)
39         assert.False(t, vesconf.Debug)
40         assert.Equal(t, vesconf.Event.MaxMissed, 2)
41         assert.Equal(t, vesconf.Event.RetryInterval, time.Second*5)
42         assert.Equal(t, vesconf.Measurement.Prometheus.KeepAlive, time.Second*30)
43         assert.Equal(t, vesconf.Event.VNFName, defaultVNFName)
44         assert.Equal(t, vesconf.Event.NfNamingCode, defaultNFNamingCode)
45         assert.Equal(t, vesconf.Event.ReportingEntityName, "Vespa")
46         // depending on the credentials with which this test is run,
47         // root or non-root, the code either reads the UUID from the file or
48         // ends up using the default id. Just check the length here,
49         // not the actual value.
50         assert.Len(t, vesconf.Event.ReportingEntityID, len(defaultReportingEntityID))
51 }
52
53 func TestBasicConfigContainsCorrectValues(t *testing.T) {
54         vesconf := vespaMgr.BasicVespaConf()
55         testBaseConf(t, vesconf)
56 }
57
58 func TestYamlGenerationWithoutXAppsConfig(t *testing.T) {
59         buffer := new(bytes.Buffer)
60         vespaMgr.CreateConfig(buffer, []byte{})
61         var vesconf VESAgentConfiguration
62         err := yaml.Unmarshal(buffer.Bytes(), &vesconf)
63         assert.Nil(t, err)
64         testBaseConf(t, vesconf)
65         assert.Empty(t, vesconf.Measurement.Prometheus.Rules.Metrics)
66 }
67
68 func TestYamlGenerationWithXAppsConfig(t *testing.T) {
69         buffer := new(bytes.Buffer)
70         bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
71         assert.Nil(t, err)
72         vespaMgr.CreateConfig(buffer, bytes)
73         var vesconf VESAgentConfiguration
74         err = yaml.Unmarshal(buffer.Bytes(), &vesconf)
75         assert.Nil(t, err)
76         testBaseConf(t, vesconf)
77         assert.Len(t, vesconf.Measurement.Prometheus.Rules.Metrics, 4)
78 }
79
80 // Helper function for the metrics parsing tests
81 func metricsStringToInterfaceArray(metrics string) []interface{} {
82         var metricsArray map[string][]interface{}
83         json.Unmarshal([]byte(metrics), &metricsArray)
84         return metricsArray["metrics"]
85 }
86
87 func TestParseMetricsRules(t *testing.T) {
88         metricsJSON := `{"metrics": [
89                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived", "counterId": "0011" },
90                         { "name": "ricxapp_RMR_ReceiveError", "objectName": "ricxappRMRReceiveErrorCounter", "objectInstance": "ricxappRMRReceiveError", "counterId": "0011" },
91                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted", "counterId": "0011" },
92                         { "name": "ricxapp_RMR_TransmitError", "objectName": "ricxappRMRTransmitErrorCounter", "objectInstance": "ricxappRMRTransmitError", "counterId": "0011" },
93                         { "name": "ricxapp_SDL_Stored", "objectName": "ricxappSDLStoredCounter", "objectInstance": "ricxappSDLStored", "counterId": "0011" },
94                         { "name": "ricxapp_SDL_StoreError", "objectName": "ricxappSDLStoreErrorCounter", "objectInstance": "ricxappSDLStoreError", "counterId": "0011" } ]}`
95         appMetrics := make(AppMetrics)
96         m := metricsStringToInterfaceArray(metricsJSON)
97         appMetrics = vespaMgr.ParseMetricsRules(m, appMetrics, "SEP/XAPP", "X2", "1234", "60")
98         assert.Len(t, appMetrics, 6)
99         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
100         assert.Equal(t, "ricxappRMRTransmitErrorCounter", appMetrics["ricxapp_RMR_TransmitError"].ObjectName)
101         assert.Equal(t, "ricxappSDLStoreError", appMetrics["ricxapp_SDL_StoreError"].ObjectInstance)
102 }
103
104 func TestParseMetricsRulesNoMetrics(t *testing.T) {
105         appMetrics := make(AppMetrics)
106         metricsJSON := `{"metrics": []`
107         m := metricsStringToInterfaceArray(metricsJSON)
108         appMetrics = vespaMgr.ParseMetricsRules(m, appMetrics, "SEP/XAPP", "X2", "1234", "60")
109         assert.Empty(t, appMetrics)
110 }
111
112 func TestParseMetricsRulesAdditionalFields(t *testing.T) {
113         appMetrics := make(AppMetrics)
114         metricsJSON := `{"metrics": [
115                         { "additionalField": "valueIgnored", "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived", "counterId": "0011" }]}`
116         m := metricsStringToInterfaceArray(metricsJSON)
117         appMetrics = vespaMgr.ParseMetricsRules(m, appMetrics, "SEP/XAPP", "X2", "1234", "60")
118         assert.Len(t, appMetrics, 1)
119         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
120         assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
121 }
122
123 func TestParseMetricsRulesMissingFields(t *testing.T) {
124         appMetrics := make(AppMetrics)
125         metricsJSON := `{"metrics": [
126                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived", "counterId": "0011" },
127                         { "name": "ricxapp_RMR_ReceiveError", "objectInstance": "ricxappRMRReceiveError" },
128                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted", "counterId": "0011" }]}`
129         m := metricsStringToInterfaceArray(metricsJSON)
130         appMetrics = vespaMgr.ParseMetricsRules(m, appMetrics, "SEP/XAPP", "X2", "1234", "60")
131         assert.Len(t, appMetrics, 2)
132         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
133         assert.Equal(t, "ricxappRMRTransmittedCounter", appMetrics["ricxapp_RMR_Transmitted"].ObjectName)
134         _, ok := appMetrics["ricxapp_RMR_ReceiveError"]
135         assert.False(t, ok)
136 }
137
138 func TestParseMetricsRulesDuplicateDefinitionIsIgnored(t *testing.T) {
139         appMetrics := make(AppMetrics)
140         metricsJSON := `{"metrics": [
141                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived", "counterId": "0011" },
142                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounterXXX", "objectInstance": "ricxappRMRReceivedXXX", "counterId": "0011" },
143                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted", "counterId": "0011" }]}`
144         m := metricsStringToInterfaceArray(metricsJSON)
145         appMetrics = vespaMgr.ParseMetricsRules(m, appMetrics, "SEP/XAPP", "X2", "1234", "60")
146         assert.Len(t, appMetrics, 2)
147         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
148         assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
149 }
150
151 func TestParseMetricsRulesIncrementalFillOfAppMetrics(t *testing.T) {
152         appMetrics := make(AppMetrics)
153         metricsJSON1 := `{"metrics": [
154                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived", "counterId": "0011" }]}`
155         metricsJSON2 := `{"metrics": [
156                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted", "counterId": "0011" }]}`
157         m1 := metricsStringToInterfaceArray(metricsJSON1)
158         m2 := metricsStringToInterfaceArray(metricsJSON2)
159         appMetrics = vespaMgr.ParseMetricsRules(m1, appMetrics, "SEP/XAPP", "X2", "1234", "60")
160         appMetrics = vespaMgr.ParseMetricsRules(m2, appMetrics, "SEP/XAPP", "X2", "1234", "60")
161         assert.Len(t, appMetrics, 2)
162         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
163         assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
164 }
165
166 func TestParseXAppDescriptor(t *testing.T) {
167         appMetrics := make(AppMetrics)
168         bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
169         assert.Nil(t, err)
170
171         appMetrics = vespaMgr.ParseMetricsFromDescriptor(bytes, appMetrics)
172         assert.Len(t, appMetrics, 4)
173         assert.Equal(t, "App1ExampleCounterOneObject", appMetrics["App1ExampleCounterOne"].ObjectName)
174         assert.Equal(t, "App1ExampleCounterOneObjectInstance", appMetrics["App1ExampleCounterOne"].ObjectInstance)
175         assert.Equal(t, "App1ExampleCounterTwoObject", appMetrics["App1ExampleCounterTwo"].ObjectName)
176         assert.Equal(t, "App1ExampleCounterTwoObjectInstance", appMetrics["App1ExampleCounterTwo"].ObjectInstance)
177         assert.Equal(t, "App2ExampleCounterOneObject", appMetrics["App2ExampleCounterOne"].ObjectName)
178         assert.Equal(t, "App2ExampleCounterOneObjectInstance", appMetrics["App2ExampleCounterOne"].ObjectInstance)
179         assert.Equal(t, "App2ExampleCounterTwoObject", appMetrics["App2ExampleCounterTwo"].ObjectName)
180         assert.Equal(t, "App2ExampleCounterTwoObjectInstance", appMetrics["App2ExampleCounterTwo"].ObjectInstance)
181
182         bytes, err = ioutil.ReadFile("../../test/noConfig_xApp_config_test_output.json")
183         assert.Nil(t, err)
184         appMetrics = vespaMgr.ParseMetricsFromDescriptor(bytes, appMetrics)
185
186         bytes, err = ioutil.ReadFile("../../test/noMeasurements_xApp_config_test_output.json")
187         assert.Nil(t, err)
188         appMetrics = vespaMgr.ParseMetricsFromDescriptor(bytes, appMetrics)
189
190         bytes, err = ioutil.ReadFile("../../test/inValidMeasurements_xApp_config_test_output.json")
191         assert.Nil(t, err)
192         appMetrics = vespaMgr.ParseMetricsFromDescriptor(bytes, appMetrics)
193
194 }
195
196 func TestParseXAppDescriptorWithNoConfig(t *testing.T) {
197         metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse"}},
198                          {{"metadata": "something", "descriptor": "somethingelse"}}]`
199         metricsBytes := []byte(metricsJSON)
200         appMetrics := make(AppMetrics)
201         appMetrics = vespaMgr.ParseMetricsFromDescriptor(metricsBytes, appMetrics)
202         assert.Empty(t, appMetrics)
203 }
204
205 func TestParseXAppDescriptorWithNoMetrics(t *testing.T) {
206         metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse", "config":{}},
207                          {{"metadata": "something", "descriptor": "somethingelse", "config":{}}}]`
208         metricsBytes := []byte(metricsJSON)
209         appMetrics := make(AppMetrics)
210         appMetrics = vespaMgr.ParseMetricsFromDescriptor(metricsBytes, appMetrics)
211         assert.Empty(t, appMetrics)
212 }