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