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.
22 "github.com/stretchr/testify/assert"
30 func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
31 assert.Equal(t, "/tmp/data", vesconf.DataDir)
32 assert.False(t, vesconf.Debug)
33 assert.Equal(t, vesconf.Event.MaxMissed, 2)
34 assert.Equal(t, vesconf.Event.RetryInterval, time.Second*5)
35 assert.Equal(t, vesconf.Measurement.Prometheus.KeepAlive, time.Second*30)
38 func TestBasicConfigContainsCorrectValues(t *testing.T) {
39 vesconf := basicVespaConf()
40 testBaseConf(t, vesconf)
43 func TestCollectorConfiguration(t *testing.T) {
44 os.Setenv("VESMGR_PRICOLLECTOR_USER", "user123")
45 os.Setenv("VESMGR_PRICOLLECTOR_PASSWORD", "pass123")
46 os.Setenv("VESMGR_PRICOLLECTOR_PASSPHRASE", "phrase123")
47 os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "1.2.3.4")
48 os.Setenv("VESMGR_PRICOLLECTOR_PORT", "1234")
49 os.Setenv("VESMGR_PRICOLLECTOR_SERVERROOT", "vescollector")
50 os.Setenv("VESMGR_PRICOLLECTOR_TOPIC", "sometopic")
51 os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "true")
53 vesconf := basicVespaConf()
54 getCollectorConfiguration(&vesconf)
56 assert.Equal(t, "user123", vesconf.PrimaryCollector.User)
57 assert.Equal(t, "pass123", vesconf.PrimaryCollector.Password)
58 assert.Equal(t, "phrase123", vesconf.PrimaryCollector.PassPhrase)
59 assert.Equal(t, "1.2.3.4", vesconf.PrimaryCollector.FQDN)
60 assert.Equal(t, 1234, vesconf.PrimaryCollector.Port)
61 assert.Equal(t, "vescollector", vesconf.PrimaryCollector.ServerRoot)
62 assert.Equal(t, "sometopic", vesconf.PrimaryCollector.Topic)
63 assert.True(t, vesconf.PrimaryCollector.Secure)
66 func TestCollectorConfigurationWhenEnvironmentVariablesAreNotDefined(t *testing.T) {
67 os.Unsetenv("VESMGR_PRICOLLECTOR_USER")
68 os.Unsetenv("VESMGR_PRICOLLECTOR_PASSWORD")
69 os.Unsetenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
70 os.Unsetenv("VESMGR_PRICOLLECTOR_ADDR")
71 os.Unsetenv("VESMGR_PRICOLLECTOR_PORT")
72 os.Unsetenv("VESMGR_PRICOLLECTOR_SERVERROOT")
73 os.Unsetenv("VESMGR_PRICOLLECTOR_TOPIC")
74 os.Unsetenv("VESMGR_PRICOLLECTOR_SECURE")
76 vesconf := basicVespaConf()
77 getCollectorConfiguration(&vesconf)
79 assert.Equal(t, "", vesconf.PrimaryCollector.User)
80 assert.Equal(t, "", vesconf.PrimaryCollector.Password)
81 assert.Equal(t, "", vesconf.PrimaryCollector.PassPhrase)
82 assert.Equal(t, "", vesconf.PrimaryCollector.FQDN)
83 assert.Equal(t, 8443, vesconf.PrimaryCollector.Port)
84 assert.Equal(t, "", vesconf.PrimaryCollector.ServerRoot)
85 assert.Equal(t, "", vesconf.PrimaryCollector.Topic)
86 assert.False(t, vesconf.PrimaryCollector.Secure)
89 func TestCollectorConfigurationWhenPrimaryCollectorPortIsNotInteger(t *testing.T) {
90 os.Setenv("VESMGR_PRICOLLECTOR_PORT", "abcd")
91 vesconf := basicVespaConf()
92 getCollectorConfiguration(&vesconf)
93 assert.Equal(t, 0, vesconf.PrimaryCollector.Port)
96 func TestCollectorConfigurationWhenPrimaryCollectorSecureIsNotTrueOrFalse(t *testing.T) {
97 os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "foo")
98 vesconf := basicVespaConf()
99 getCollectorConfiguration(&vesconf)
100 assert.False(t, vesconf.PrimaryCollector.Secure)
103 func TestYamlGenerationWithoutXAppsConfig(t *testing.T) {
104 buffer := new(bytes.Buffer)
105 createVespaConfig(buffer, []byte{})
106 var vesconf VESAgentConfiguration
107 err := yaml.Unmarshal(buffer.Bytes(), &vesconf)
109 testBaseConf(t, vesconf)
110 assert.Empty(t, vesconf.Measurement.Prometheus.Rules.Metrics)
113 func TestYamlGenerationWithXAppsConfig(t *testing.T) {
114 buffer := new(bytes.Buffer)
115 bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
117 createVespaConfig(buffer, bytes)
118 var vesconf VESAgentConfiguration
119 err = yaml.Unmarshal(buffer.Bytes(), &vesconf)
121 testBaseConf(t, vesconf)
122 assert.Len(t, vesconf.Measurement.Prometheus.Rules.Metrics, 4)
125 // Helper function for the metrics parsing tests
126 func metricsStringToInterfaceArray(metrics string) []interface{} {
127 var metricsArray map[string][]interface{}
128 json.Unmarshal([]byte(metrics), &metricsArray)
129 return metricsArray["metrics"]
132 func TestParseMetricsRules(t *testing.T) {
133 metricsJson := `{"metrics": [
134 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
135 { "name": "ricxapp_RMR_ReceiveError", "objectName": "ricxappRMRReceiveErrorCounter", "objectInstance": "ricxappRMRReceiveError" },
136 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" },
137 { "name": "ricxapp_RMR_TransmitError", "objectName": "ricxappRMRTransmitErrorCounter", "objectInstance": "ricxappRMRTransmitError" },
138 { "name": "ricxapp_SDL_Stored", "objectName": "ricxappSDLStoredCounter", "objectInstance": "ricxappSDLStored" },
139 { "name": "ricxapp_SDL_StoreError", "objectName": "ricxappSDLStoreErrorCounter", "objectInstance": "ricxappSDLStoreError" } ]}`
140 appMetrics := make(AppMetrics)
141 var m []interface{} = metricsStringToInterfaceArray(metricsJson)
142 appMetrics = parseMetricsRules(m, appMetrics)
143 assert.Len(t, appMetrics, 6)
144 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
145 assert.Equal(t, "ricxappRMRTransmitErrorCounter", appMetrics["ricxapp_RMR_TransmitError"].ObjectName)
146 assert.Equal(t, "ricxappSDLStoreError", appMetrics["ricxapp_SDL_StoreError"].ObjectInstance)
149 func TestParseMetricsRulesNoMetrics(t *testing.T) {
150 appMetrics := make(AppMetrics)
151 metricsJson := `{"metrics": []`
152 var m []interface{} = metricsStringToInterfaceArray(metricsJson)
153 appMetrics = parseMetricsRules(m, appMetrics)
154 assert.Empty(t, appMetrics)
157 func TestParseMetricsRulesAdditionalFields(t *testing.T) {
158 appMetrics := make(AppMetrics)
159 metricsJson := `{"metrics": [
160 { "additionalField": "valueIgnored", "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
161 var m []interface{} = metricsStringToInterfaceArray(metricsJson)
162 appMetrics = parseMetricsRules(m, appMetrics)
163 assert.Len(t, appMetrics, 1)
164 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
165 assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
168 func TestParseMetricsRulesMissingFields(t *testing.T) {
169 appMetrics := make(AppMetrics)
170 metricsJson := `{"metrics": [
171 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
172 { "name": "ricxapp_RMR_ReceiveError", "objectInstance": "ricxappRMRReceiveError" },
173 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
174 var m []interface{} = metricsStringToInterfaceArray(metricsJson)
175 appMetrics = parseMetricsRules(m, appMetrics)
176 assert.Len(t, appMetrics, 2)
177 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
178 assert.Equal(t, "ricxappRMRTransmittedCounter", appMetrics["ricxapp_RMR_Transmitted"].ObjectName)
179 _, ok := appMetrics["ricxapp_RMR_ReceiveError"]
183 func TestParseMetricsRulesDuplicateDefinitionIsIgnored(t *testing.T) {
184 appMetrics := make(AppMetrics)
185 metricsJson := `{"metrics": [
186 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
187 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounterXXX", "objectInstance": "ricxappRMRReceivedXXX" },
188 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
189 var m []interface{} = metricsStringToInterfaceArray(metricsJson)
190 appMetrics = parseMetricsRules(m, appMetrics)
191 assert.Len(t, appMetrics, 2)
192 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
193 assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
196 func TestParseMetricsRulesIncrementalFillOfAppMetrics(t *testing.T) {
197 appMetrics := make(AppMetrics)
198 metricsJson1 := `{"metrics": [
199 { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
200 metricsJson2 := `{"metrics": [
201 { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
202 var m1 []interface{} = metricsStringToInterfaceArray(metricsJson1)
203 var m2 []interface{} = metricsStringToInterfaceArray(metricsJson2)
204 appMetrics = parseMetricsRules(m1, appMetrics)
205 appMetrics = parseMetricsRules(m2, appMetrics)
206 assert.Len(t, appMetrics, 2)
207 assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
208 assert.Equal(t, "ricxappRMRReceived", appMetrics["ricxapp_RMR_Received"].ObjectInstance)
211 func TestParseXAppDescriptor(t *testing.T) {
212 appMetrics := make(AppMetrics)
213 bytes, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
216 appMetrics = parseMetricsFromXAppDescriptor(bytes, appMetrics)
217 assert.Len(t, appMetrics, 4)
218 assert.Equal(t, "App1ExampleCounterOneObject", appMetrics["App1ExampleCounterOne"].ObjectName)
219 assert.Equal(t, "App1ExampleCounterOneObjectInstance", appMetrics["App1ExampleCounterOne"].ObjectInstance)
220 assert.Equal(t, "App1ExampleCounterTwoObject", appMetrics["App1ExampleCounterTwo"].ObjectName)
221 assert.Equal(t, "App1ExampleCounterTwoObjectInstance", appMetrics["App1ExampleCounterTwo"].ObjectInstance)
222 assert.Equal(t, "App2ExampleCounterOneObject", appMetrics["App2ExampleCounterOne"].ObjectName)
223 assert.Equal(t, "App2ExampleCounterOneObjectInstance", appMetrics["App2ExampleCounterOne"].ObjectInstance)
224 assert.Equal(t, "App2ExampleCounterTwoObject", appMetrics["App2ExampleCounterTwo"].ObjectName)
225 assert.Equal(t, "App2ExampleCounterTwoObjectInstance", appMetrics["App2ExampleCounterTwo"].ObjectInstance)
228 func TestParseXAppDescriptorWithNoConfig(t *testing.T) {
229 metricsJson := `[{{"metadata": "something", "descriptor": "somethingelse"}},
230 {{"metadata": "something", "descriptor": "somethingelse"}}]`
231 metricsBytes := []byte(metricsJson)
232 appMetrics := make(AppMetrics)
233 appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
234 assert.Empty(t, appMetrics)
237 func TestParseXAppDescriptorWithNoMetrics(t *testing.T) {
238 metricsJson := `[{{"metadata": "something", "descriptor": "somethingelse", "config":{}},
239 {{"metadata": "something", "descriptor": "somethingelse", "config":{}}}]`
240 metricsBytes := []byte(metricsJson)
241 appMetrics := make(AppMetrics)
242 appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
243 assert.Empty(t, appMetrics)