2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 "github.com/stretchr/testify/assert"
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.ReportingEntityName, "Vespa")
39 // depending on the credentials with which this test is run,
40 // root or non-root, the code either reads the UUID from the file or
41 // ends up using the default id. Just check the length here,
42 // not the actual value.
43 assert.Len(t, vesconf.Event.ReportingEntityID, len(defaultReportingEntityID))
46 func TestBasicConfigContainsCorrectValues(t *testing.T) {
47 vesconf := basicVespaConf()
48 testBaseConf(t, vesconf)
51 func TestBasicConfigContainsCorrectVNFName(t *testing.T) {
52 os.Setenv("VESMGR_VNFNAME", "VNF-111")
53 vesconf := basicVespaConf()
54 assert.Equal(t, vesconf.Event.VNFName, "VNF-111")
55 os.Unsetenv("VESMGR_VNFNAME")
58 func TestCollectorConfiguration(t *testing.T) {
59 os.Unsetenv("VESMGR_VNFNAME")
60 os.Setenv("VESMGR_PRICOLLECTOR_USER", "user123")
61 os.Setenv("VESMGR_PRICOLLECTOR_PASSWORD", "pass123")
62 os.Setenv("VESMGR_PRICOLLECTOR_PASSPHRASE", "phrase123")
63 os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "1.2.3.4")
64 os.Setenv("VESMGR_PRICOLLECTOR_PORT", "1234")
65 os.Setenv("VESMGR_PRICOLLECTOR_SERVERROOT", "vescollector")
66 os.Setenv("VESMGR_PRICOLLECTOR_TOPIC", "sometopic")
67 os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "true")
69 vesconf := basicVespaConf()
70 getCollectorConfiguration(&vesconf)
72 assert.Equal(t, "user123", vesconf.PrimaryCollector.User)
73 assert.Equal(t, "pass123", vesconf.PrimaryCollector.Password)
74 assert.Equal(t, "phrase123", vesconf.PrimaryCollector.PassPhrase)
75 assert.Equal(t, "1.2.3.4", vesconf.PrimaryCollector.FQDN)
76 assert.Equal(t, 1234, vesconf.PrimaryCollector.Port)
77 assert.Equal(t, "vescollector", vesconf.PrimaryCollector.ServerRoot)
78 assert.Equal(t, "sometopic", vesconf.PrimaryCollector.Topic)
79 assert.True(t, vesconf.PrimaryCollector.Secure)
82 func TestCollectorConfigurationWhenEnvironmentVariablesAreNotDefined(t *testing.T) {
83 os.Unsetenv("VESMGR_VNFNAME")
84 os.Unsetenv("VESMGR_PRICOLLECTOR_USER")
85 os.Unsetenv("VESMGR_PRICOLLECTOR_PASSWORD")
86 os.Unsetenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
87 os.Unsetenv("VESMGR_PRICOLLECTOR_ADDR")
88 os.Unsetenv("VESMGR_PRICOLLECTOR_PORT")
89 os.Unsetenv("VESMGR_PRICOLLECTOR_SERVERROOT")
90 os.Unsetenv("VESMGR_PRICOLLECTOR_TOPIC")
91 os.Unsetenv("VESMGR_PRICOLLECTOR_SECURE")
93 vesconf := basicVespaConf()
94 getCollectorConfiguration(&vesconf)
96 assert.Equal(t, "", vesconf.PrimaryCollector.User)
97 assert.Equal(t, "", vesconf.PrimaryCollector.Password)
98 assert.Equal(t, "", vesconf.PrimaryCollector.PassPhrase)
99 assert.Equal(t, "", vesconf.PrimaryCollector.FQDN)
100 assert.Equal(t, 8443, vesconf.PrimaryCollector.Port)
101 assert.Equal(t, "", vesconf.PrimaryCollector.ServerRoot)
102 assert.Equal(t, "", vesconf.PrimaryCollector.Topic)
103 assert.False(t, vesconf.PrimaryCollector.Secure)
106 func TestCollectorConfigurationWhenPrimaryCollectorPortIsNotInteger(t *testing.T) {
107 os.Setenv("VESMGR_PRICOLLECTOR_PORT", "abcd")
108 vesconf := basicVespaConf()
109 getCollectorConfiguration(&vesconf)
110 assert.Equal(t, 0, vesconf.PrimaryCollector.Port)
113 func TestCollectorConfigurationWhenPrimaryCollectorSecureIsNotTrueOrFalse(t *testing.T) {
114 os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "foo")
115 vesconf := basicVespaConf()
116 getCollectorConfiguration(&vesconf)
117 assert.False(t, vesconf.PrimaryCollector.Secure)
120 func TestYamlGenerationWithoutXAppsConfig(t *testing.T) {
121 buffer := new(bytes.Buffer)
122 createVespaConfig(buffer, []byte{})
123 var vesconf VESAgentConfiguration
124 err := yaml.Unmarshal(buffer.Bytes(), &vesconf)
126 testBaseConf(t, vesconf)
127 assert.Empty(t, vesconf.Measurement.Prometheus.Rules.Metrics)
130 func TestYamlGenerationWithXAppsConfig(t *testing.T) {
131 buffer := new(bytes.Buffer)
132 bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
134 createVespaConfig(buffer, bytes)
135 var vesconf VESAgentConfiguration
136 err = yaml.Unmarshal(buffer.Bytes(), &vesconf)
138 testBaseConf(t, vesconf)
139 assert.Len(t, vesconf.Measurement.Prometheus.Rules.Metrics, 4)
142 // Helper function for the metrics parsing tests
143 func metricsStringToInterfaceArray(metrics string) []interface{} {
144 var metricsArray map[string][]interface{}
145 json.Unmarshal([]byte(metrics), &metricsArray)
146 return metricsArray["metrics"]
149 func TestParseMetricsRules(t *testing.T) {
150 metricsJSON := `{"metrics": [
151 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
152 { "name": "ricxapp_RMR_ReceiveError", "objectName": "ricxappRMRReceiveErrorCounter", "objectInstance": "ricxappRMRReceiveError" },
153 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" },
154 { "name": "ricxapp_RMR_TransmitError", "objectName": "ricxappRMRTransmitErrorCounter", "objectInstance": "ricxappRMRTransmitError" },
155 { "name": "ricxapp_SDL_Stored", "objectName": "ricxappSDLStoredCounter", "objectInstance": "ricxappSDLStored" },
156 { "name": "ricxapp_SDL_StoreError", "objectName": "ricxappSDLStoreErrorCounter", "objectInstance": "ricxappSDLStoreError" } ]}`
157 appMetrics := make(AppMetrics)
158 m := metricsStringToInterfaceArray(metricsJSON)
159 appMetrics = parseMetricsRules(m, appMetrics)
160 assert.Len(t, appMetrics, 6)
161 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
162 assert.Equal(t, "ricxappRMRTransmitErrorCounter", appMetrics["ricxapp_RMR_TransmitError"].ObjectName)
163 assert.Equal(t, "ricxappSDLStoreError", appMetrics["ricxapp_SDL_StoreError"].ObjectInstance)
166 func TestParseMetricsRulesNoMetrics(t *testing.T) {
167 appMetrics := make(AppMetrics)
168 metricsJSON := `{"metrics": []`
169 m := metricsStringToInterfaceArray(metricsJSON)
170 appMetrics = parseMetricsRules(m, appMetrics)
171 assert.Empty(t, appMetrics)
174 func TestParseMetricsRulesAdditionalFields(t *testing.T) {
175 appMetrics := make(AppMetrics)
176 metricsJSON := `{"metrics": [
177 { "additionalField": "valueIgnored", "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
178 m := metricsStringToInterfaceArray(metricsJSON)
179 appMetrics = parseMetricsRules(m, appMetrics)
180 assert.Len(t, appMetrics, 1)
181 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
182 assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
185 func TestParseMetricsRulesMissingFields(t *testing.T) {
186 appMetrics := make(AppMetrics)
187 metricsJSON := `{"metrics": [
188 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
189 { "name": "ricxapp_RMR_ReceiveError", "objectInstance": "ricxappRMRReceiveError" },
190 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
191 m := metricsStringToInterfaceArray(metricsJSON)
192 appMetrics = parseMetricsRules(m, appMetrics)
193 assert.Len(t, appMetrics, 2)
194 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
195 assert.Equal(t, "ricxappRMRTransmittedCounter", appMetrics["ricxapp_RMR_Transmitted"].ObjectName)
196 _, ok := appMetrics["ricxapp_RMR_ReceiveError"]
200 func TestParseMetricsRulesDuplicateDefinitionIsIgnored(t *testing.T) {
201 appMetrics := make(AppMetrics)
202 metricsJSON := `{"metrics": [
203 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
204 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounterXXX", "objectInstance": "ricxappRMRReceivedXXX" },
205 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
206 m := metricsStringToInterfaceArray(metricsJSON)
207 appMetrics = parseMetricsRules(m, appMetrics)
208 assert.Len(t, appMetrics, 2)
209 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
210 assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
213 func TestParseMetricsRulesIncrementalFillOfAppMetrics(t *testing.T) {
214 appMetrics := make(AppMetrics)
215 metricsJSON1 := `{"metrics": [
216 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
217 metricsJSON2 := `{"metrics": [
218 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
219 m1 := metricsStringToInterfaceArray(metricsJSON1)
220 m2 := metricsStringToInterfaceArray(metricsJSON2)
221 appMetrics = parseMetricsRules(m1, appMetrics)
222 appMetrics = parseMetricsRules(m2, appMetrics)
223 assert.Len(t, appMetrics, 2)
224 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
225 assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
228 func TestParseXAppDescriptor(t *testing.T) {
229 appMetrics := make(AppMetrics)
230 bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
233 appMetrics = parseMetricsFromXAppDescriptor(bytes, appMetrics)
234 assert.Len(t, appMetrics, 4)
235 assert.Equal(t, "App1ExampleCounterOneObject", appMetrics["App1ExampleCounterOne"].ObjectName)
236 assert.Equal(t, "App1ExampleCounterOneObjectInstance", appMetrics["App1ExampleCounterOne"].ObjectInstance)
237 assert.Equal(t, "App1ExampleCounterTwoObject", appMetrics["App1ExampleCounterTwo"].ObjectName)
238 assert.Equal(t, "App1ExampleCounterTwoObjectInstance", appMetrics["App1ExampleCounterTwo"].ObjectInstance)
239 assert.Equal(t, "App2ExampleCounterOneObject", appMetrics["App2ExampleCounterOne"].ObjectName)
240 assert.Equal(t, "App2ExampleCounterOneObjectInstance", appMetrics["App2ExampleCounterOne"].ObjectInstance)
241 assert.Equal(t, "App2ExampleCounterTwoObject", appMetrics["App2ExampleCounterTwo"].ObjectName)
242 assert.Equal(t, "App2ExampleCounterTwoObjectInstance", appMetrics["App2ExampleCounterTwo"].ObjectInstance)
245 func TestParseXAppDescriptorWithNoConfig(t *testing.T) {
246 metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse"}},
247 {{"metadata": "something", "descriptor": "somethingelse"}}]`
248 metricsBytes := []byte(metricsJSON)
249 appMetrics := make(AppMetrics)
250 appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
251 assert.Empty(t, appMetrics)
254 func TestParseXAppDescriptorWithNoMetrics(t *testing.T) {
255 metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse", "config":{}},
256 {{"metadata": "something", "descriptor": "somethingelse", "config":{}}}]`
257 metricsBytes := []byte(metricsJSON)
258 appMetrics := make(AppMetrics)
259 appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
260 assert.Empty(t, appMetrics)