blob: e04010f9f1fcba17af2332db992495212e0d67fc [file] [log] [blame]
Katri Turunen4b74f012019-08-15 10:49:36 +03001/*
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.
Roni Riska6ffba082019-11-27 10:59:54 +020016 *
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
Katri Turunen4b74f012019-08-15 10:49:36 +030019 */
Katri Turunen412df962019-09-16 08:48:18 +030020package main
Katri Turunen4b74f012019-08-15 10:49:36 +030021
Katri Turunen412df962019-09-16 08:48:18 +030022import (
Katri Turunen4b74f012019-08-15 10:49:36 +030023 "bytes"
Katri Turunen412df962019-09-16 08:48:18 +030024 "encoding/json"
Katri Turunen412df962019-09-16 08:48:18 +030025 "io/ioutil"
Katri Turunen66b78132019-09-02 10:28:52 +030026 "os"
Katri Turunen412df962019-09-16 08:48:18 +030027 "testing"
28 "time"
Roni Riskafc77ebb2019-09-26 08:20:44 +030029
30 "github.com/stretchr/testify/assert"
31 "gopkg.in/yaml.v2"
Katri Turunen66b78132019-09-02 10:28:52 +030032)
Katri Turunen4b74f012019-08-15 10:49:36 +030033
Katri Turunen66b78132019-09-02 10:28:52 +030034func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
Katri Turunen4b74f012019-08-15 10:49:36 +030035 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)
Katri Turunen3e79fbe2019-09-26 12:46:07 +030040 assert.Equal(t, vesconf.Event.VNFName, defaultVNFName)
Roni Riska364295f2019-09-30 09:39:12 +030041 assert.Equal(t, vesconf.Event.NfNamingCode, defaultNFNamingCode)
Katri Turunen3e79fbe2019-09-26 12:46:07 +030042 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))
Katri Turunen66b78132019-09-02 10:28:52 +030048}
Katri Turunen4b74f012019-08-15 10:49:36 +030049
Katri Turunen66b78132019-09-02 10:28:52 +030050func TestBasicConfigContainsCorrectValues(t *testing.T) {
Katri Turunen4b74f012019-08-15 10:49:36 +030051 vesconf := basicVespaConf()
52 testBaseConf(t, vesconf)
Katri Turunen66b78132019-09-02 10:28:52 +030053}
Katri Turunen4b74f012019-08-15 10:49:36 +030054
Katri Turunen3e79fbe2019-09-26 12:46:07 +030055func TestBasicConfigContainsCorrectVNFName(t *testing.T) {
56 os.Setenv("VESMGR_VNFNAME", "VNF-111")
Roni Riska364295f2019-09-30 09:39:12 +030057 os.Setenv("VESMGR_NFNAMINGCODE", "code55")
Katri Turunen3e79fbe2019-09-26 12:46:07 +030058 vesconf := basicVespaConf()
59 assert.Equal(t, vesconf.Event.VNFName, "VNF-111")
Roni Riska364295f2019-09-30 09:39:12 +030060 assert.Equal(t, vesconf.Event.NfNamingCode, "code55")
Katri Turunen3e79fbe2019-09-26 12:46:07 +030061 os.Unsetenv("VESMGR_VNFNAME")
Roni Riska364295f2019-09-30 09:39:12 +030062 os.Unsetenv("VESMGR_NFNAMINGCODE")
Katri Turunen3e79fbe2019-09-26 12:46:07 +030063}
64
Katri Turunen66b78132019-09-02 10:28:52 +030065func TestCollectorConfiguration(t *testing.T) {
Katri Turunen3e79fbe2019-09-26 12:46:07 +030066 os.Unsetenv("VESMGR_VNFNAME")
Roni Riska364295f2019-09-30 09:39:12 +030067 os.Unsetenv("VESMGR_NFNAMINGCODE")
Katri Turunen66b78132019-09-02 10:28:52 +030068 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)
Katri Turunen412df962019-09-16 08:48:18 +030087 assert.True(t, vesconf.PrimaryCollector.Secure)
Katri Turunen66b78132019-09-02 10:28:52 +030088}
89
90func TestCollectorConfigurationWhenEnvironmentVariablesAreNotDefined(t *testing.T) {
Katri Turunen3e79fbe2019-09-26 12:46:07 +030091 os.Unsetenv("VESMGR_VNFNAME")
Roni Riska364295f2019-09-30 09:39:12 +030092 os.Unsetenv("VESMGR_NFNAMINGCODE")
Katri Turunen66b78132019-09-02 10:28:52 +030093 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)
Katri Turunen412df962019-09-16 08:48:18 +0300112 assert.False(t, vesconf.PrimaryCollector.Secure)
Katri Turunen66b78132019-09-02 10:28:52 +0300113}
114
115func 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
122func TestCollectorConfigurationWhenPrimaryCollectorSecureIsNotTrueOrFalse(t *testing.T) {
123 os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "foo")
124 vesconf := basicVespaConf()
125 getCollectorConfiguration(&vesconf)
Katri Turunen412df962019-09-16 08:48:18 +0300126 assert.False(t, vesconf.PrimaryCollector.Secure)
Katri Turunen66b78132019-09-02 10:28:52 +0300127}
128
Katri Turunen412df962019-09-16 08:48:18 +0300129func TestYamlGenerationWithoutXAppsConfig(t *testing.T) {
Katri Turunen4b74f012019-08-15 10:49:36 +0300130 buffer := new(bytes.Buffer)
Katri Turunen412df962019-09-16 08:48:18 +0300131 createVespaConfig(buffer, []byte{})
Katri Turunen4b74f012019-08-15 10:49:36 +0300132 var vesconf VESAgentConfiguration
133 err := yaml.Unmarshal(buffer.Bytes(), &vesconf)
134 assert.Nil(t, err)
135 testBaseConf(t, vesconf)
Katri Turunen412df962019-09-16 08:48:18 +0300136 assert.Empty(t, vesconf.Measurement.Prometheus.Rules.Metrics)
137}
138
139func 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
152func metricsStringToInterfaceArray(metrics string) []interface{} {
153 var metricsArray map[string][]interface{}
154 json.Unmarshal([]byte(metrics), &metricsArray)
155 return metricsArray["metrics"]
156}
157
158func TestParseMetricsRules(t *testing.T) {
Roni Riskafc77ebb2019-09-26 08:20:44 +0300159 metricsJSON := `{"metrics": [
Katri Turunen412df962019-09-16 08:48:18 +0300160 { "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)
Roni Riskafc77ebb2019-09-26 08:20:44 +0300167 m := metricsStringToInterfaceArray(metricsJSON)
Katri Turunen412df962019-09-16 08:48:18 +0300168 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
175func TestParseMetricsRulesNoMetrics(t *testing.T) {
176 appMetrics := make(AppMetrics)
Roni Riskafc77ebb2019-09-26 08:20:44 +0300177 metricsJSON := `{"metrics": []`
178 m := metricsStringToInterfaceArray(metricsJSON)
Katri Turunen412df962019-09-16 08:48:18 +0300179 appMetrics = parseMetricsRules(m, appMetrics)
180 assert.Empty(t, appMetrics)
181}
182
183func TestParseMetricsRulesAdditionalFields(t *testing.T) {
184 appMetrics := make(AppMetrics)
Roni Riskafc77ebb2019-09-26 08:20:44 +0300185 metricsJSON := `{"metrics": [
Katri Turunen412df962019-09-16 08:48:18 +0300186 { "additionalField": "valueIgnored", "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
Roni Riskafc77ebb2019-09-26 08:20:44 +0300187 m := metricsStringToInterfaceArray(metricsJSON)
Katri Turunen412df962019-09-16 08:48:18 +0300188 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
194func TestParseMetricsRulesMissingFields(t *testing.T) {
195 appMetrics := make(AppMetrics)
Roni Riskafc77ebb2019-09-26 08:20:44 +0300196 metricsJSON := `{"metrics": [
Katri Turunen412df962019-09-16 08:48:18 +0300197 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
198 { "name": "ricxapp_RMR_ReceiveError", "objectInstance": "ricxappRMRReceiveError" },
199 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
Roni Riskafc77ebb2019-09-26 08:20:44 +0300200 m := metricsStringToInterfaceArray(metricsJSON)
Katri Turunen412df962019-09-16 08:48:18 +0300201 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
209func TestParseMetricsRulesDuplicateDefinitionIsIgnored(t *testing.T) {
210 appMetrics := make(AppMetrics)
Roni Riskafc77ebb2019-09-26 08:20:44 +0300211 metricsJSON := `{"metrics": [
Katri Turunen412df962019-09-16 08:48:18 +0300212 { "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" }]}`
Roni Riskafc77ebb2019-09-26 08:20:44 +0300215 m := metricsStringToInterfaceArray(metricsJSON)
Katri Turunen412df962019-09-16 08:48:18 +0300216 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
222func TestParseMetricsRulesIncrementalFillOfAppMetrics(t *testing.T) {
223 appMetrics := make(AppMetrics)
Roni Riskafc77ebb2019-09-26 08:20:44 +0300224 metricsJSON1 := `{"metrics": [
Katri Turunen412df962019-09-16 08:48:18 +0300225 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
Roni Riskafc77ebb2019-09-26 08:20:44 +0300226 metricsJSON2 := `{"metrics": [
Katri Turunen412df962019-09-16 08:48:18 +0300227 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
Roni Riskafc77ebb2019-09-26 08:20:44 +0300228 m1 := metricsStringToInterfaceArray(metricsJSON1)
229 m2 := metricsStringToInterfaceArray(metricsJSON2)
Katri Turunen412df962019-09-16 08:48:18 +0300230 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
237func 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
254func TestParseXAppDescriptorWithNoConfig(t *testing.T) {
Roni Riskafc77ebb2019-09-26 08:20:44 +0300255 metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse"}},
Katri Turunen412df962019-09-16 08:48:18 +0300256 {{"metadata": "something", "descriptor": "somethingelse"}}]`
Roni Riskafc77ebb2019-09-26 08:20:44 +0300257 metricsBytes := []byte(metricsJSON)
Katri Turunen412df962019-09-16 08:48:18 +0300258 appMetrics := make(AppMetrics)
259 appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
260 assert.Empty(t, appMetrics)
261}
262
263func TestParseXAppDescriptorWithNoMetrics(t *testing.T) {
Roni Riskafc77ebb2019-09-26 08:20:44 +0300264 metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse", "config":{}},
Katri Turunen412df962019-09-16 08:48:18 +0300265 {{"metadata": "something", "descriptor": "somethingelse", "config":{}}}]`
Roni Riskafc77ebb2019-09-26 08:20:44 +0300266 metricsBytes := []byte(metricsJSON)
Katri Turunen412df962019-09-16 08:48:18 +0300267 appMetrics := make(AppMetrics)
268 appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
269 assert.Empty(t, appMetrics)
Katri Turunen66b78132019-09-02 10:28:52 +0300270}