blob: 5eb86af73d1637854c03a8fbe3bbdca4b1d5fc95 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
Matteo Scandolo56879722017-05-17 21:39:54 -070016import unittest
Matteo Scandolo56879722017-05-17 21:39:54 -070017import os
18from xosconfig import Config
19from xosconfig import Config as Config2
20
Zack Williams045b63d2019-01-22 16:30:57 -070021basic_conf = os.path.abspath(
22 os.path.dirname(os.path.realpath(__file__)) + "/confs/basic_conf.yaml"
23)
24yaml_not_valid = os.path.abspath(
25 os.path.dirname(os.path.realpath(__file__)) + "/confs/yaml_not_valid.yaml"
26)
27invalid_format = os.path.abspath(
28 os.path.dirname(os.path.realpath(__file__)) + "/confs/invalid_format.yaml"
29)
30sample_conf = os.path.abspath(
31 os.path.dirname(os.path.realpath(__file__)) + "/confs/sample_conf.yaml"
32)
33override_conf = os.path.abspath(
34 os.path.dirname(os.path.realpath(__file__)) + "/confs/override_conf.yaml"
35)
36extend_conf = os.path.abspath(
37 os.path.dirname(os.path.realpath(__file__)) + "/confs/extend_conf.yaml"
38)
Matteo Scandolo56879722017-05-17 21:39:54 -070039
Zack Williams045b63d2019-01-22 16:30:57 -070040small_schema = os.path.abspath(
41 os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml"
42)
Matteo Scandolo1879ce72017-05-30 15:45:26 -070043
Zack Williams045b63d2019-01-22 16:30:57 -070044services_list = {"xos-ws": [], "xos-db": []}
Matteo Scandolo56879722017-05-17 21:39:54 -070045
46db_service = [
Zack Williams045b63d2019-01-22 16:30:57 -070047 {
48 "ModifyIndex": 6,
49 "CreateIndex": 6,
50 "Node": "0152982c3159",
51 "Address": "172.19.0.2",
52 "ServiceID": "0d53ce210785:frontend_xos_db_1:5432",
53 "ServiceName": "xos-db",
54 "ServiceTags": [],
55 "ServiceAddress": "172.18.0.4",
56 "ServicePort": 5432,
57 "ServiceEnableTagOverride": "false",
58 }
59]
60
Matteo Scandolo56879722017-05-17 21:39:54 -070061
62class XOSConfigTest(unittest.TestCase):
63 """
64 Testing the XOS Config Module
65 """
66
Scott Baker7dddd512017-10-24 10:13:34 -070067 def setUp(self):
68 # In case some other testcase in nose has left config in an unclean state
69 Config.clear()
70
Matteo Scandolo56879722017-05-17 21:39:54 -070071 def tearDown(self):
72 # NOTE clear the config after each test
73 Config.clear()
74
75 def test_initialize_only_once(self):
76 """
77 [XOS-Config] Raise if initialized twice
78 """
79 with self.assertRaises(Exception) as e:
80 Config.init(sample_conf)
81 Config2.init(sample_conf)
82 self.assertEqual(e.exception.message, "[XOS-Config] Module already initialized")
83
84 def test_config_not_initialized(self):
85 """
86 [XOS-Config] Raise if accessing properties without initialization
87 """
88 with self.assertRaises(Exception) as e:
89 Config.get("database")
Zack Williams045b63d2019-01-22 16:30:57 -070090 self.assertEqual(
91 e.exception.message, "[XOS-Config] Module has not been initialized"
92 )
Matteo Scandolo56879722017-05-17 21:39:54 -070093
94 def test_missing_file_exception(self):
95 """
Zack Williams045b63d2019-01-22 16:30:57 -070096 [XOS-Config] Raise if file not found
Matteo Scandolo56879722017-05-17 21:39:54 -070097 """
98 with self.assertRaises(Exception) as e:
99 Config.init("missing_conf")
Zack Williams045b63d2019-01-22 16:30:57 -0700100 self.assertEqual(
101 e.exception.message, "[XOS-Config] Config file not found at: missing_conf"
102 )
Matteo Scandolo56879722017-05-17 21:39:54 -0700103
104 def test_yaml_not_valid(self):
105 """
106 [XOS-Config] Raise if yaml is not valid
107 """
108 with self.assertRaises(Exception) as e:
109 Config.init(yaml_not_valid)
Zack Williams045b63d2019-01-22 16:30:57 -0700110 self.assertTrue(
111 e.exception.message.startswith("[XOS-Config] The config format is wrong:")
112 )
Matteo Scandolo56879722017-05-17 21:39:54 -0700113
114 def test_invalid_format(self):
115 """
116 [XOS-Config] Raise if format is not valid (we expect a dictionary)
117 """
118 with self.assertRaises(Exception) as e:
119 Config.init(invalid_format)
Zack Williams045b63d2019-01-22 16:30:57 -0700120 self.assertEqual(
121 e.exception.message,
122 (
123 "[XOS-Config] The config format is wrong: Schema validation failed:\n"
124 " - Value '['I am', 'a yaml', 'but the', 'format is not', 'correct']' is not a dict. Value path: ''."
125 ),
126 )
Matteo Scandolo56879722017-05-17 21:39:54 -0700127
128 def test_env_override(self):
129 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700130 [XOS-Config] the XOS_CONFIG_FILE environment variable should override the config_file
Matteo Scandolo56879722017-05-17 21:39:54 -0700131 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700132 os.environ["XOS_CONFIG_FILE"] = "env.yaml"
Matteo Scandolo56879722017-05-17 21:39:54 -0700133 with self.assertRaises(Exception) as e:
134 Config.init("missing_conf")
Zack Williams045b63d2019-01-22 16:30:57 -0700135 self.assertEqual(
136 e.exception.message, "[XOS-Config] Config file not found at: env.yaml"
137 )
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700138 del os.environ["XOS_CONFIG_FILE"]
139
140 def test_schema_override(self):
141 """
142 [XOS-Config] the XOS_CONFIG_SCHEMA environment variable should override the config_schema
143 """
144 os.environ["XOS_CONFIG_SCHEMA"] = "env-schema.yaml"
145 with self.assertRaises(Exception) as e:
146 Config.init(basic_conf)
Zack Williams045b63d2019-01-22 16:30:57 -0700147 self.assertRegexpMatches(
148 e.exception.message,
149 r"\[XOS\-Config\] Config schema not found at: (.+)env-schema\.yaml",
150 )
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700151 # self.assertEqual(e.exception.message, "[XOS-Config] Config schema not found at: env-schema.yaml")
152 del os.environ["XOS_CONFIG_SCHEMA"]
153
154 def test_schema_override_usage(self):
155 """
156 [XOS-Config] the XOS_CONFIG_SCHEMA should be used to validate a config
157 """
158 os.environ["XOS_CONFIG_SCHEMA"] = small_schema
159 with self.assertRaises(Exception) as e:
160 Config.init(basic_conf)
Zack Williams045b63d2019-01-22 16:30:57 -0700161 self.assertEqual(
162 e.exception.message,
163 (
164 "[XOS-Config] The config format is wrong: Schema validation failed:\n"
165 " - Key 'database' was not defined. Path: ''."
166 ),
167 )
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700168 del os.environ["XOS_CONFIG_SCHEMA"]
Matteo Scandolo56879722017-05-17 21:39:54 -0700169
170 def test_get_cli_param(self):
171 """
172 [XOS-Config] Should read CLI -C param
173 """
174 args = ["-A", "Foo", "-c", "Bar", "-C", "config.yaml"]
175 res = Config.get_cli_param(args)
176 self.assertEqual(res, "config.yaml")
177
178 def test_get_default_val_for_missing_param(self):
179 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700180 [XOS-Config] Should get the default value if nothing is specified
Matteo Scandolo56879722017-05-17 21:39:54 -0700181 """
182 Config.init(basic_conf)
Matteo Scandolo8fa0f7a2017-09-25 15:00:07 -0700183 dir = Config.get("xos_dir")
184 self.assertEqual(dir, "/opt/xos")
Matteo Scandolo56879722017-05-17 21:39:54 -0700185
Matteo Scandoloe0fc6852017-06-07 16:01:54 -0700186 def test_get_config_file(self):
187 """
188 [XOS-Config] Should return the config file in use
189 """
190 Config.init(sample_conf)
191 res = Config.get_config_file()
192 self.assertEqual(res, sample_conf)
193
194 def test_get_missing_param(self):
Matteo Scandolo56879722017-05-17 21:39:54 -0700195 """
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700196 [XOS-Config] Should return None reading a missing param
Matteo Scandolo56879722017-05-17 21:39:54 -0700197 """
198 Config.init(sample_conf)
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700199 res = Config.get("foo")
200 self.assertEqual(res, None)
Matteo Scandolo56879722017-05-17 21:39:54 -0700201
202 def test_get_first_level(self):
203 """
204 [XOS-Config] Should return a first level param
205 """
206 Config.init(sample_conf)
207 # NOTE we are using Config2 here to be sure that the configuration is readable from any import,
208 # not only from the one that has been used to initialize it
209 res = Config2.get("database")
Zack Williams045b63d2019-01-22 16:30:57 -0700210 self.assertEqual(res, {"name": "xos", "username": "test", "password": "safe"})
Matteo Scandolo56879722017-05-17 21:39:54 -0700211
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700212 def test_get_child_level(self):
Matteo Scandolo56879722017-05-17 21:39:54 -0700213 """
214 [XOS-Config] Should return a child level param
215 """
216 Config.init(sample_conf)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700217 res = Config.get("database.name")
218 self.assertEqual(res, "xos")
219
220 def test_config_override(self):
221 """
222 [XOS-Config] If an override is provided for the config, it should return the overridden value
223 """
Zack Williams045b63d2019-01-22 16:30:57 -0700224 Config.init(sample_conf, "xos-config-schema.yaml", override_conf)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700225 res = Config.get("logging.level")
226 self.assertEqual(res, "info")
227 res = Config.get("database.password")
228 self.assertEqual(res, "overridden_password")
229
230 def test_config_extend(self):
231 """
Zack Williams045b63d2019-01-22 16:30:57 -0700232 [XOS-Config] If an override is provided for the config, it should
233 return the overridden value (also if not defined in the base one)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700234 """
Zack Williams045b63d2019-01-22 16:30:57 -0700235
236 Config.init(sample_conf, "xos-config-schema.yaml", extend_conf)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700237 res = Config.get("xos_dir")
238 self.assertEqual(res, "/opt/xos")
239 res = Config.get("database.password")
240 self.assertEqual(res, "safe")
Matteo Scandolo56879722017-05-17 21:39:54 -0700241
Zack Williams045b63d2019-01-22 16:30:57 -0700242
243if __name__ == "__main__":
Scott Baker7dddd512017-10-24 10:13:34 -0700244 unittest.main()