Add AlertManager bind address
[ric-plt/vespamgr.git] / cmd / vesmgr / 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         "os"
27         "testing"
28         "time"
29
30         "github.com/stretchr/testify/assert"
31         "gopkg.in/yaml.v2"
32 )
33
34 func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
35         assert.Equal(t, "/tmp/data", vesconf.DataDir)
36         assert.False(t, vesconf.Debug)
37         assert.Equal(t, vesconf.Event.MaxMissed, 2)
38         assert.Equal(t, vesconf.Event.RetryInterval, time.Second*5)
39         assert.Equal(t, vesconf.Measurement.Prometheus.KeepAlive, time.Second*30)
40         assert.Equal(t, vesconf.Event.VNFName, defaultVNFName)
41         assert.Equal(t, vesconf.Event.NfNamingCode, defaultNFNamingCode)
42         assert.Equal(t, vesconf.Event.ReportingEntityName, "Vespa")
43         // depending on the credentials with which this test is run,
44         // root or non-root, the code either reads the UUID from the file or
45         // ends up using the default id. Just check the length here,
46         // not the actual value.
47         assert.Len(t, vesconf.Event.ReportingEntityID, len(defaultReportingEntityID))
48 }
49
50 func TestBasicConfigContainsCorrectValues(t *testing.T) {
51         vesconf := basicVespaConf()
52         testBaseConf(t, vesconf)
53 }
54
55 func TestBasicConfigContainsCorrectVNFName(t *testing.T) {
56         os.Setenv("VESMGR_VNFNAME", "VNF-111")
57         os.Setenv("VESMGR_NFNAMINGCODE", "code55")
58         vesconf := basicVespaConf()
59         assert.Equal(t, vesconf.Event.VNFName, "VNF-111")
60         assert.Equal(t, vesconf.Event.NfNamingCode, "code55")
61         os.Unsetenv("VESMGR_VNFNAME")
62         os.Unsetenv("VESMGR_NFNAMINGCODE")
63 }
64
65 func TestCollectorConfiguration(t *testing.T) {
66         os.Unsetenv("VESMGR_VNFNAME")
67         os.Unsetenv("VESMGR_NFNAMINGCODE")
68         os.Setenv("VESMGR_PRICOLLECTOR_USER", "user123")
69         os.Setenv("VESMGR_PRICOLLECTOR_PASSWORD", "pass123")
70         os.Setenv("VESMGR_PRICOLLECTOR_PASSPHRASE", "phrase123")
71         os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "1.2.3.4")
72         os.Setenv("VESMGR_PRICOLLECTOR_PORT", "1234")
73         os.Setenv("VESMGR_PRICOLLECTOR_SERVERROOT", "vescollector")
74         os.Setenv("VESMGR_PRICOLLECTOR_TOPIC", "sometopic")
75         os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "true")
76
77         vesconf := basicVespaConf()
78         getCollectorConfiguration(&vesconf)
79
80         assert.Equal(t, "user123", vesconf.PrimaryCollector.User)
81         assert.Equal(t, "pass123", vesconf.PrimaryCollector.Password)
82         assert.Equal(t, "phrase123", vesconf.PrimaryCollector.PassPhrase)
83         assert.Equal(t, "1.2.3.4", vesconf.PrimaryCollector.FQDN)
84         assert.Equal(t, 1234, vesconf.PrimaryCollector.Port)
85         assert.Equal(t, "vescollector", vesconf.PrimaryCollector.ServerRoot)
86         assert.Equal(t, "sometopic", vesconf.PrimaryCollector.Topic)
87         assert.True(t, vesconf.PrimaryCollector.Secure)
88 }
89
90 func TestCollectorConfigurationWhenEnvironmentVariablesAreNotDefined(t *testing.T) {
91         os.Unsetenv("VESMGR_VNFNAME")
92         os.Unsetenv("VESMGR_NFNAMINGCODE")
93         os.Unsetenv("VESMGR_PRICOLLECTOR_USER")
94         os.Unsetenv("VESMGR_PRICOLLECTOR_PASSWORD")
95         os.Unsetenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
96         os.Unsetenv("VESMGR_PRICOLLECTOR_ADDR")
97         os.Unsetenv("VESMGR_PRICOLLECTOR_PORT")
98         os.Unsetenv("VESMGR_PRICOLLECTOR_SERVERROOT")
99         os.Unsetenv("VESMGR_PRICOLLECTOR_TOPIC")
100         os.Unsetenv("VESMGR_PRICOLLECTOR_SECURE")
101
102         vesconf := basicVespaConf()
103         getCollectorConfiguration(&vesconf)
104
105         assert.Equal(t, "", vesconf.PrimaryCollector.User)
106         assert.Equal(t, "", vesconf.PrimaryCollector.Password)
107         assert.Equal(t, "", vesconf.PrimaryCollector.PassPhrase)
108         assert.Equal(t, "", vesconf.PrimaryCollector.FQDN)
109         assert.Equal(t, 8443, vesconf.PrimaryCollector.Port)
110         assert.Equal(t, "", vesconf.PrimaryCollector.ServerRoot)
111         assert.Equal(t, "", vesconf.PrimaryCollector.Topic)
112         assert.False(t, vesconf.PrimaryCollector.Secure)
113 }
114
115 func TestCollectorConfigurationWhenPrimaryCollectorPortIsNotInteger(t *testing.T) {
116         os.Setenv("VESMGR_PRICOLLECTOR_PORT", "abcd")
117         vesconf := basicVespaConf()
118         getCollectorConfiguration(&vesconf)
119         assert.Equal(t, 0, vesconf.PrimaryCollector.Port)
120 }
121
122 func TestCollectorConfigurationWhenPrimaryCollectorSecureIsNotTrueOrFalse(t *testing.T) {
123         os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "foo")
124         vesconf := basicVespaConf()
125         getCollectorConfiguration(&vesconf)
126         assert.False(t, vesconf.PrimaryCollector.Secure)
127 }
128
129 func TestYamlGenerationWithoutXAppsConfig(t *testing.T) {
130         buffer := new(bytes.Buffer)
131         createVespaConfig(buffer, []byte{})
132         var vesconf VESAgentConfiguration
133         err := yaml.Unmarshal(buffer.Bytes(), &vesconf)
134         assert.Nil(t, err)
135         testBaseConf(t, vesconf)
136         assert.Empty(t, vesconf.Measurement.Prometheus.Rules.Metrics)
137 }
138
139 func TestYamlGenerationWithXAppsConfig(t *testing.T) {
140         buffer := new(bytes.Buffer)
141         bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
142         assert.Nil(t, err)
143         createVespaConfig(buffer, bytes)
144         var vesconf VESAgentConfiguration
145         err = yaml.Unmarshal(buffer.Bytes(), &vesconf)
146         assert.Nil(t, err)
147         testBaseConf(t, vesconf)
148         assert.Len(t, vesconf.Measurement.Prometheus.Rules.Metrics, 4)
149 }
150
151 // Helper function for the metrics parsing tests
152 func metricsStringToInterfaceArray(metrics string) []interface{} {
153         var metricsArray map[string][]interface{}
154         json.Unmarshal([]byte(metrics), &metricsArray)
155         return metricsArray["metrics"]
156 }
157
158 func TestParseMetricsRules(t *testing.T) {
159         metricsJSON := `{"metrics": [
160                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
161                         { "name": "ricxapp_RMR_ReceiveError", "objectName": "ricxappRMRReceiveErrorCounter", "objectInstance": "ricxappRMRReceiveError" },
162                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" },
163                         { "name": "ricxapp_RMR_TransmitError", "objectName": "ricxappRMRTransmitErrorCounter", "objectInstance": "ricxappRMRTransmitError" },
164                         { "name": "ricxapp_SDL_Stored", "objectName": "ricxappSDLStoredCounter", "objectInstance": "ricxappSDLStored" },
165                         { "name": "ricxapp_SDL_StoreError", "objectName": "ricxappSDLStoreErrorCounter", "objectInstance": "ricxappSDLStoreError" } ]}`
166         appMetrics := make(AppMetrics)
167         m := metricsStringToInterfaceArray(metricsJSON)
168         appMetrics = parseMetricsRules(m, appMetrics)
169         assert.Len(t, appMetrics, 6)
170         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
171         assert.Equal(t, "ricxappRMRTransmitErrorCounter", appMetrics["ricxapp_RMR_TransmitError"].ObjectName)
172         assert.Equal(t, "ricxappSDLStoreError", appMetrics["ricxapp_SDL_StoreError"].ObjectInstance)
173 }
174
175 func TestParseMetricsRulesNoMetrics(t *testing.T) {
176         appMetrics := make(AppMetrics)
177         metricsJSON := `{"metrics": []`
178         m := metricsStringToInterfaceArray(metricsJSON)
179         appMetrics = parseMetricsRules(m, appMetrics)
180         assert.Empty(t, appMetrics)
181 }
182
183 func TestParseMetricsRulesAdditionalFields(t *testing.T) {
184         appMetrics := make(AppMetrics)
185         metricsJSON := `{"metrics": [
186                         { "additionalField": "valueIgnored", "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
187         m := metricsStringToInterfaceArray(metricsJSON)
188         appMetrics = parseMetricsRules(m, appMetrics)
189         assert.Len(t, appMetrics, 1)
190         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
191         assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
192 }
193
194 func TestParseMetricsRulesMissingFields(t *testing.T) {
195         appMetrics := make(AppMetrics)
196         metricsJSON := `{"metrics": [
197                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
198                         { "name": "ricxapp_RMR_ReceiveError", "objectInstance": "ricxappRMRReceiveError" },
199                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
200         m := metricsStringToInterfaceArray(metricsJSON)
201         appMetrics = parseMetricsRules(m, appMetrics)
202         assert.Len(t, appMetrics, 2)
203         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
204         assert.Equal(t, "ricxappRMRTransmittedCounter", appMetrics["ricxapp_RMR_Transmitted"].ObjectName)
205         _, ok := appMetrics["ricxapp_RMR_ReceiveError"]
206         assert.False(t, ok)
207 }
208
209 func TestParseMetricsRulesDuplicateDefinitionIsIgnored(t *testing.T) {
210         appMetrics := make(AppMetrics)
211         metricsJSON := `{"metrics": [
212                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
213                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounterXXX", "objectInstance": "ricxappRMRReceivedXXX" },
214                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
215         m := metricsStringToInterfaceArray(metricsJSON)
216         appMetrics = parseMetricsRules(m, appMetrics)
217         assert.Len(t, appMetrics, 2)
218         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
219         assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
220 }
221
222 func TestParseMetricsRulesIncrementalFillOfAppMetrics(t *testing.T) {
223         appMetrics := make(AppMetrics)
224         metricsJSON1 := `{"metrics": [
225                         { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
226         metricsJSON2 := `{"metrics": [
227                         { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
228         m1 := metricsStringToInterfaceArray(metricsJSON1)
229         m2 := metricsStringToInterfaceArray(metricsJSON2)
230         appMetrics = parseMetricsRules(m1, appMetrics)
231         appMetrics = parseMetricsRules(m2, appMetrics)
232         assert.Len(t, appMetrics, 2)
233         assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
234         assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
235 }
236
237 func TestParseXAppDescriptor(t *testing.T) {
238         appMetrics := make(AppMetrics)
239         bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
240         assert.Nil(t, err)
241
242         appMetrics = parseMetricsFromXAppDescriptor(bytes, appMetrics)
243         assert.Len(t, appMetrics, 4)
244         assert.Equal(t, "App1ExampleCounterOneObject", appMetrics["App1ExampleCounterOne"].ObjectName)
245         assert.Equal(t, "App1ExampleCounterOneObjectInstance", appMetrics["App1ExampleCounterOne"].ObjectInstance)
246         assert.Equal(t, "App1ExampleCounterTwoObject", appMetrics["App1ExampleCounterTwo"].ObjectName)
247         assert.Equal(t, "App1ExampleCounterTwoObjectInstance", appMetrics["App1ExampleCounterTwo"].ObjectInstance)
248         assert.Equal(t, "App2ExampleCounterOneObject", appMetrics["App2ExampleCounterOne"].ObjectName)
249         assert.Equal(t, "App2ExampleCounterOneObjectInstance", appMetrics["App2ExampleCounterOne"].ObjectInstance)
250         assert.Equal(t, "App2ExampleCounterTwoObject", appMetrics["App2ExampleCounterTwo"].ObjectName)
251         assert.Equal(t, "App2ExampleCounterTwoObjectInstance", appMetrics["App2ExampleCounterTwo"].ObjectInstance)
252 }
253
254 func TestParseXAppDescriptorWithNoConfig(t *testing.T) {
255         metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse"}},
256                          {{"metadata": "something", "descriptor": "somethingelse"}}]`
257         metricsBytes := []byte(metricsJSON)
258         appMetrics := make(AppMetrics)
259         appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
260         assert.Empty(t, appMetrics)
261 }
262
263 func TestParseXAppDescriptorWithNoMetrics(t *testing.T) {
264         metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse", "config":{}},
265                          {{"metadata": "something", "descriptor": "somethingelse", "config":{}}}]`
266         metricsBytes := []byte(metricsJSON)
267         appMetrics := make(AppMetrics)
268         appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
269         assert.Empty(t, appMetrics)
270 }