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