695ed9ab7998051d987c46fe1737e7097928bb0b
[ric-plt/e2mgr.git] / E2Manager / 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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package configuration
22
23 import (
24         "github.com/stretchr/testify/assert"
25         "gopkg.in/yaml.v2"
26         "io/ioutil"
27         "os"
28         "testing"
29 )
30
31 func TestParseConfigurationSuccess(t *testing.T) {
32         config := ParseConfiguration()
33         assert.Equal(t, 3800, config.Http.Port)
34         assert.Equal(t, 3801, config.Rmr.Port)
35         assert.Equal(t, 65536, config.Rmr.MaxMsgSize)
36         assert.Equal(t, "info", config.Logging.LogLevel)
37         assert.Equal(t, 100, config.NotificationResponseBuffer)
38         assert.Equal(t, 5, config.BigRedButtonTimeoutSec)
39         assert.Equal(t, 4500, config.KeepAliveResponseTimeoutMs)
40         assert.Equal(t, 1500, config.KeepAliveDelayMs)
41         assert.Equal(t, 15000, config.E2TInstanceDeletionTimeoutMs)
42         assert.NotNil(t, config.GlobalRicId)
43         assert.NotEmpty(t, config.GlobalRicId.PlmnId)
44         assert.NotEmpty(t, config.GlobalRicId.RicNearRtId)
45 /*      assert.NotEmpty(t, config.Kubernetes.KubeNamespace)
46         assert.NotEmpty(t, config.Kubernetes.ConfigPath)*/
47 }
48
49 func TestStringer(t *testing.T) {
50         config := ParseConfiguration().String()
51         assert.NotEmpty(t, config)
52 }
53
54 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
55         configPath := "../resources/configuration.yaml"
56         configPathTmp := "../resources/configuration.yaml_tmp"
57         err := os.Rename(configPath, configPathTmp)
58         if err != nil {
59                 t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
60         }
61         defer func() {
62                 err = os.Rename(configPathTmp, configPath)
63                 if err != nil {
64                         t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
65                 }
66         }()
67         assert.Panics(t, func() { ParseConfiguration() })
68 }
69
70 func TestRmrConfigNotFoundFailure(t *testing.T) {
71         configPath := "../resources/configuration.yaml"
72         configPathTmp := "../resources/configuration.yaml_tmp"
73         err := os.Rename(configPath, configPathTmp)
74         if err != nil {
75                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
76         }
77         defer func() {
78                 err = os.Rename(configPathTmp, configPath)
79                 if err != nil {
80                         t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
81                 }
82         }()
83         yamlMap := map[string]interface{}{
84                 "logging": map[string]interface{}{"logLevel": "info"},
85                 "http":    map[string]interface{}{"port": 3800},
86                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
87                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
88         }
89         buf, err := yaml.Marshal(yamlMap)
90         if err != nil {
91                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
92         }
93         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
94         if err != nil {
95                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
96         }
97         assert.PanicsWithValue(t, "#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n", func() { ParseConfiguration() })
98 }
99
100 func TestLoggingConfigNotFoundFailure(t *testing.T) {
101         configPath := "../resources/configuration.yaml"
102         configPathTmp := "../resources/configuration.yaml_tmp"
103         err := os.Rename(configPath, configPathTmp)
104         if err != nil {
105                 t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
106         }
107         defer func() {
108                 err = os.Rename(configPathTmp, configPath)
109                 if err != nil {
110                         t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
111                 }
112         }()
113         yamlMap := map[string]interface{}{
114                 "rmr":  map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
115                 "http": map[string]interface{}{"port": 3800},
116                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
117                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
118         }
119         buf, err := yaml.Marshal(yamlMap)
120         if err != nil {
121                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
122         }
123         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
124         if err != nil {
125                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
126         }
127         assert.PanicsWithValue(t, "#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n",
128                 func() { ParseConfiguration() })
129 }
130
131 func TestHttpConfigNotFoundFailure(t *testing.T) {
132         configPath := "../resources/configuration.yaml"
133         configPathTmp := "../resources/configuration.yaml_tmp"
134         err := os.Rename(configPath, configPathTmp)
135         if err != nil {
136                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
137         }
138         defer func() {
139                 err = os.Rename(configPathTmp, configPath)
140                 if err != nil {
141                         t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
142                 }
143         }()
144         yamlMap := map[string]interface{}{
145                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
146                 "logging": map[string]interface{}{"logLevel": "info"},
147                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
148                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
149         }
150         buf, err := yaml.Marshal(yamlMap)
151         if err != nil {
152                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
153         }
154         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
155         if err != nil {
156                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
157         }
158         assert.PanicsWithValue(t, "#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n",
159                 func() { ParseConfiguration() })
160 }
161
162 func TestRoutingManagerConfigNotFoundFailure(t *testing.T) {
163         configPath := "../resources/configuration.yaml"
164         configPathTmp := "../resources/configuration.yaml_tmp"
165         err := os.Rename(configPath, configPathTmp)
166         if err != nil {
167                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
168         }
169         defer func() {
170                 err = os.Rename(configPathTmp, configPath)
171                 if err != nil {
172                         t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
173                 }
174         }()
175         yamlMap := map[string]interface{}{
176                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
177                 "logging": map[string]interface{}{"logLevel": "info"},
178                 "http": map[string]interface{}{"port": 3800},
179                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
180         }
181         buf, err := yaml.Marshal(yamlMap)
182         if err != nil {
183                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to marshal configuration map\n")
184         }
185         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
186         if err != nil {
187                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
188         }
189         assert.PanicsWithValue(t, "#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n",
190                 func() { ParseConfiguration() })
191 }
192
193 func TestGlobalRicIdConfigNotFoundFailure(t *testing.T) {
194         configPath := "../resources/configuration.yaml"
195         configPathTmp := "../resources/configuration.yaml_tmp"
196         err := os.Rename(configPath, configPathTmp)
197         if err != nil {
198                 t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
199         }
200         defer func() {
201                 err = os.Rename(configPathTmp, configPath)
202                 if err != nil {
203                         t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
204                 }
205         }()
206         yamlMap := map[string]interface{}{
207                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
208                 "logging": map[string]interface{}{"logLevel": "info"},
209                 "http": map[string]interface{}{"port": 3800},
210                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
211                 //"kubernetes":    map[string]interface{}{"kubeNamespace": "test", "ConfigPath": "test"},
212         }
213         buf, err := yaml.Marshal(yamlMap)
214         if err != nil {
215                 t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to marshal configuration map\n")
216         }
217         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
218         if err != nil {
219                 t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
220         }
221         assert.PanicsWithValue(t, "#configuration.populateGlobalRicIdConfig - failed to populate Global RicId configuration: The entry 'globalRicId' not found\n",
222                 func() { ParseConfiguration() })
223 }
224
225 /*func TestKubernetesConfigNotFoundFailure(t *testing.T) {
226         configPath := "../resources/configuration.yaml"
227         configPathTmp := "../resources/configuration.yaml_tmp"
228         err := os.Rename(configPath, configPathTmp)
229         if err != nil {
230                 t.Errorf("#TestKubernetesConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
231         }
232         defer func() {
233                 err = os.Rename(configPathTmp, configPath)
234                 if err != nil {
235                         t.Errorf("#TestKubernetesConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
236                 }
237         }()
238         yamlMap := map[string]interface{}{
239                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
240                 "logging": map[string]interface{}{"logLevel": "info"},
241                 "http": map[string]interface{}{"port": 3800},
242                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
243                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
244         }
245         buf, err := yaml.Marshal(yamlMap)
246         if err != nil {
247                 t.Errorf("#TestKubernetesConfigNotFoundFailure - failed to marshal configuration map\n")
248         }
249         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
250         if err != nil {
251                 t.Errorf("#TestKubernetesConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
252         }
253         assert.PanicsWithValue(t, "#configuration.populateKubernetesConfig - failed to populate Kubernetes configuration: The entry 'kubernetes' not found\n",
254                 func() { ParseConfiguration() })
255 }*/