Sync from Azure to LF
[ric-plt/resource-status-manager.git] / RSM / configuration / configuration_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 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
18 package configuration
19
20 import (
21         "github.com/stretchr/testify/assert"
22         "gopkg.in/yaml.v2"
23         "io/ioutil"
24         "os"
25         "testing"
26 )
27
28 func TestParseConfigurationSuccess(t *testing.T) {
29         config, err := ParseConfiguration()
30         if err != nil {
31                 t.Errorf("failed to parse configuration: %s", err)
32         }
33         assert.Equal(t, 4800, config.Http.Port)
34         assert.Equal(t, 4801, config.Rmr.Port)
35         assert.Equal(t, 65536, config.Rmr.MaxMsgSize)
36         assert.Equal(t, "info", config.Logging.LogLevel)
37
38         assert.Equal(t, 3, config.Rnib.MaxRnibConnectionAttempts)
39         assert.Equal(t, 10, config.Rnib.RnibRetryIntervalMs)
40
41         assert.Equal(t, true, config.ResourceStatusParams.EnableResourceStatus)
42         assert.Equal(t, true, config.ResourceStatusParams.PrbPeriodic)
43         assert.Equal(t, true, config.ResourceStatusParams.TnlLoadIndPeriodic)
44         assert.Equal(t, true, config.ResourceStatusParams.HwLoadIndPeriodic)
45         assert.Equal(t, true, config.ResourceStatusParams.AbsStatusPeriodic)
46         assert.Equal(t, true, config.ResourceStatusParams.RsrpMeasurementPeriodic)
47         assert.Equal(t, true, config.ResourceStatusParams.CsiPeriodic)
48
49         /*assert.Equal(t, 1, config.ResourceStatusParams.PeriodicityMs)
50         assert.Equal(t, 120, config.ResourceStatusParams.PeriodicityRsrpMeasurementMs)
51         assert.Equal(t, 5, config.ResourceStatusParams.PeriodicityCsiMs)*/
52
53 }
54
55 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
56         configPath := "../resources/configuration.yaml"
57         configPathTmp := "../resources/configuration.yaml_tmp"
58         err := os.Rename(configPath, configPathTmp)
59         if err != nil {
60                 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
61         }
62         defer func() {
63                 err = os.Rename(configPathTmp, configPath)
64                 if err != nil {
65                         t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
66                 }
67         }()
68
69         _, cErr := ParseConfiguration()
70         assert.Error(t, cErr)
71 }
72
73 func TestRmrConfigNotFoundFailure(t *testing.T) {
74         yamlMap := map[string]interface{}{
75                 "logging":         map[string]interface{}{"logLevel": "info"},
76                 "http":            map[string]interface{}{"port": 631},
77                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
78                 "rnib":            map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
79         }
80         cleanUp := prepareTempConfigForTest(t, yamlMap)
81         defer cleanUp()
82
83         _, cErr := ParseConfiguration()
84         assert.EqualError(t, cErr, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n")
85 }
86
87 func TestLoggingConfigNotFoundFailure(t *testing.T) {
88         yamlMap := map[string]interface{}{
89                 "rmr":             map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
90                 "http":            map[string]interface{}{"port": 631},
91                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
92                 "rnib":            map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
93         }
94         cleanUp := prepareTempConfigForTest(t, yamlMap)
95         defer cleanUp()
96
97         _, cErr := ParseConfiguration()
98         assert.EqualError(t, cErr, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n")
99 }
100
101 func TestHttpConfigNotFoundFailure(t *testing.T) {
102         yamlMap := map[string]interface{}{
103                 "rmr":             map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
104                 "logging":         map[string]interface{}{"logLevel": "info"},
105                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
106                 "rnib":            map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
107         }
108         cleanUp := prepareTempConfigForTest(t, yamlMap)
109         defer cleanUp()
110
111         _, cErr := ParseConfiguration()
112         assert.EqualError(t, cErr, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n")
113 }
114
115 func TestRnibConfigNotFoundFailure(t *testing.T) {
116         yamlMap := map[string]interface{}{
117                 "rmr":             map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
118                 "logging":         map[string]interface{}{"logLevel": "info"},
119                 "http":            map[string]interface{}{"port": 631},
120                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
121         }
122         cleanUp := prepareTempConfigForTest(t, yamlMap)
123         defer cleanUp()
124
125         _, cErr := ParseConfiguration()
126         assert.EqualError(t, cErr, "#configuration.fillRnibConfig - failed to fill RNib configuration: The entry 'rnib' not found\n")
127 }
128
129 func TestResourceStatusParamsConfigNotFoundFailure(t *testing.T) {
130
131         yamlMap := map[string]interface{}{
132                 "rmr":     map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
133                 "logging": map[string]interface{}{"logLevel": "info"},
134                 "http":    map[string]interface{}{"port": 631},
135                 "rnib":    map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
136         }
137         cleanUp := prepareTempConfigForTest(t, yamlMap)
138         defer cleanUp()
139
140         _, cErr := ParseConfiguration()
141         assert.EqualError(t, cErr, "#configuration.fillResourceStatusParamsConfig - failed to fill resourceStatusParams configuration: The entry 'resourceStatusParams' not found\n")
142 }
143
144 func TestCharacteristicsConfigInvalidPeriodicityMs(t *testing.T) {
145
146         yamlMap := map[string]interface{}{
147                 "rmr":     map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
148                 "logging": map[string]interface{}{"logLevel": "info"},
149                 "http":    map[string]interface{}{"port": 631},
150                 "rnib":    map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
151                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 50, "periodicityRsrpMeasurementMs": 480, "periodicityCsiMs": 20},
152         }
153         cleanUp := prepareTempConfigForTest(t, yamlMap)
154         defer cleanUp()
155
156         _, cErr := ParseConfiguration()
157         assert.Error(t, cErr)
158 }
159
160 func TestResourceStatusParamsConfigInvalidPeriodicityRsrpMeasurementMs(t *testing.T) {
161
162         yamlMap := map[string]interface{}{
163                 "rmr":     map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
164                 "logging": map[string]interface{}{"logLevel": "info"},
165                 "http":    map[string]interface{}{"port": 631},
166                 "rnib":    map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
167                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 1000, "periodicityRsrpMeasurementMs": 50, "periodicityCsiMs": 20},
168         }
169         cleanUp := prepareTempConfigForTest(t, yamlMap)
170         defer cleanUp()
171
172         _, cErr := ParseConfiguration()
173         assert.Error(t, cErr)
174 }
175
176 func TestResourceStatusParamsConfigInvalidPeriodicityCsiMs(t *testing.T) {
177
178         yamlMap := map[string]interface{}{
179                 "rmr":     map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
180                 "logging": map[string]interface{}{"logLevel": "info"},
181                 "http":    map[string]interface{}{"port": 631},
182                 "rnib":    map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
183                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 1000, "periodicityRsrpMeasurementMs": 480, "periodicityCsiMs": 50},
184         }
185         cleanUp := prepareTempConfigForTest(t, yamlMap)
186         defer cleanUp()
187
188         _, cErr := ParseConfiguration()
189         assert.Error(t, cErr)
190 }
191
192 func TestConfigurationString(t *testing.T) {
193         config, err := ParseConfiguration()
194         if err != nil {
195                 t.Errorf("failed to parse configuration. error: %s", err)
196         }
197         str := config.String()
198         assert.NotEmpty(t, str)
199         assert.Contains(t, str, "logging")
200         assert.Contains(t, str, "http")
201         assert.Contains(t, str, "rmr")
202         assert.Contains(t, str, "rnib")
203         assert.Contains(t, str, "resourceStatusParams")
204 }
205
206 func prepareTempConfigForTest(t *testing.T, yamlMap map[string]interface{}) func() {
207         configPath := "../resources/configuration.yaml"
208         configPathTmp := "../resources/configuration.yaml_tmp"
209         err := os.Rename(configPath, configPathTmp)
210         if err != nil {
211                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
212         }
213         buf, err := yaml.Marshal(yamlMap)
214         if err != nil {
215                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to marshal configuration map\n")
216         }
217         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
218         if err != nil {
219                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
220         }
221
222         return func() {
223                 err = os.Rename(configPathTmp, configPath)
224                 if err != nil {
225                         t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
226                 }
227         }
228 }