1ac4204e0cd18343fb1890298f5e4d6239f18c2a
[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 //  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, err := ParseConfiguration()
33         if err != nil {
34                 t.Errorf("failed to parse configuration: %s", err)
35         }
36         assert.Equal(t, 4800, config.Http.Port)
37         assert.Equal(t, 4801, config.Rmr.Port)
38         assert.Equal(t, 65536, config.Rmr.MaxMsgSize)
39         assert.Equal(t, "info", config.Logging.LogLevel)
40
41         assert.Equal(t, 3, config.Rnib.MaxRnibConnectionAttempts)
42         assert.Equal(t, 10, config.Rnib.RnibRetryIntervalMs)
43 }
44
45 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
46         configPath := "../resources/configuration.yaml"
47         configPathTmp := "../resources/configuration.yaml_tmp"
48         err := os.Rename(configPath, configPathTmp)
49         if err != nil {
50                 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
51         }
52         defer func() {
53                 err = os.Rename(configPathTmp, configPath)
54                 if err != nil {
55                         t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
56                 }
57         }()
58
59         _, cErr := ParseConfiguration()
60         assert.Error(t, cErr)
61 }
62
63 func TestRmrConfigNotFoundFailure(t *testing.T) {
64         yamlMap := map[string]interface{}{
65                 "logging":              map[string]interface{}{"logLevel": "info"},
66                 "http":                 map[string]interface{}{"port": 631},
67                 "rnib":                 map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
68         }
69         cleanUp := prepareTempConfigForTest(t, yamlMap)
70         defer cleanUp()
71
72         _, cErr := ParseConfiguration()
73         assert.EqualError(t, cErr, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n")
74 }
75
76 func TestLoggingConfigNotFoundFailure(t *testing.T) {
77         yamlMap := map[string]interface{}{
78                 "rmr":                  map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
79                 "http":                 map[string]interface{}{"port": 631},
80                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
81                 "rnib":                 map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
82         }
83         cleanUp := prepareTempConfigForTest(t, yamlMap)
84         defer cleanUp()
85
86         _, cErr := ParseConfiguration()
87         assert.EqualError(t, cErr, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n")
88 }
89
90 func TestHttpConfigNotFoundFailure(t *testing.T) {
91         yamlMap := map[string]interface{}{
92                 "rmr":                  map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
93                 "logging":              map[string]interface{}{"logLevel": "info"},
94                 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
95                 "rnib":                 map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
96         }
97         cleanUp := prepareTempConfigForTest(t, yamlMap)
98         defer cleanUp()
99
100         _, cErr := ParseConfiguration()
101         assert.EqualError(t, cErr, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n")
102 }
103
104 func TestRnibConfigNotFoundFailure(t *testing.T) {
105         yamlMap := map[string]interface{}{
106                 "rmr":                  map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
107                 "logging":              map[string]interface{}{"logLevel": "info"},
108                 "http":                 map[string]interface{}{"port": 631},
109         }
110         cleanUp := prepareTempConfigForTest(t, yamlMap)
111         defer cleanUp()
112
113         _, cErr := ParseConfiguration()
114         assert.EqualError(t, cErr, "#configuration.fillRnibConfig - failed to fill RNib configuration: The entry 'rnib' not found\n")
115 }
116
117 func TestConfigurationString(t *testing.T) {
118         config, err := ParseConfiguration()
119         if err != nil {
120                 t.Errorf("failed to parse configuration. error: %s", err)
121         }
122         str := config.String()
123         assert.NotEmpty(t, str)
124         assert.Contains(t, str, "logging")
125         assert.Contains(t, str, "http")
126         assert.Contains(t, str, "rmr")
127         assert.Contains(t, str, "rnib")
128 }
129
130 func prepareTempConfigForTest(t *testing.T, yamlMap map[string]interface{}) func() {
131         configPath := "../resources/configuration.yaml"
132         configPathTmp := "../resources/configuration.yaml_tmp"
133         err := os.Rename(configPath, configPathTmp)
134         if err != nil {
135                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
136         }
137         buf, err := yaml.Marshal(yamlMap)
138         if err != nil {
139                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to marshal configuration map\n")
140         }
141         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
142         if err != nil {
143                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
144         }
145
146         return func() {
147                 err = os.Rename(configPathTmp, configPath)
148                 if err != nil {
149                         t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
150                 }
151         }
152 }