blob: edec0b54d474deaa7bb8a8346381d72ba6d499a8 [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
Zack Williams9a42f872019-02-15 17:56:04 -070015from __future__ import absolute_import
Matteo Scandolod2044a42017-08-07 16:08:28 -070016
Matteo Scandolo56879722017-05-17 21:39:54 -070017import os
Zack Williams9a42f872019-02-15 17:56:04 -070018import unittest
19
Matteo Scandolo56879722017-05-17 21:39:54 -070020from xosconfig import Config
21from xosconfig import Config as Config2
22
Zack Williams045b63d2019-01-22 16:30:57 -070023basic_conf = os.path.abspath(
24 os.path.dirname(os.path.realpath(__file__)) + "/confs/basic_conf.yaml"
25)
26yaml_not_valid = os.path.abspath(
27 os.path.dirname(os.path.realpath(__file__)) + "/confs/yaml_not_valid.yaml"
28)
29invalid_format = os.path.abspath(
30 os.path.dirname(os.path.realpath(__file__)) + "/confs/invalid_format.yaml"
31)
32sample_conf = os.path.abspath(
33 os.path.dirname(os.path.realpath(__file__)) + "/confs/sample_conf.yaml"
34)
35override_conf = os.path.abspath(
36 os.path.dirname(os.path.realpath(__file__)) + "/confs/override_conf.yaml"
37)
38extend_conf = os.path.abspath(
39 os.path.dirname(os.path.realpath(__file__)) + "/confs/extend_conf.yaml"
40)
Matteo Scandolo56879722017-05-17 21:39:54 -070041
Zack Williams045b63d2019-01-22 16:30:57 -070042small_schema = os.path.abspath(
43 os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml"
44)
Matteo Scandolo1879ce72017-05-30 15:45:26 -070045
Zack Williams045b63d2019-01-22 16:30:57 -070046services_list = {"xos-ws": [], "xos-db": []}
Matteo Scandolo56879722017-05-17 21:39:54 -070047
48db_service = [
Zack Williams045b63d2019-01-22 16:30:57 -070049 {
50 "ModifyIndex": 6,
51 "CreateIndex": 6,
52 "Node": "0152982c3159",
53 "Address": "172.19.0.2",
54 "ServiceID": "0d53ce210785:frontend_xos_db_1:5432",
55 "ServiceName": "xos-db",
56 "ServiceTags": [],
57 "ServiceAddress": "172.18.0.4",
58 "ServicePort": 5432,
59 "ServiceEnableTagOverride": "false",
60 }
61]
62
Matteo Scandolo56879722017-05-17 21:39:54 -070063
64class XOSConfigTest(unittest.TestCase):
65 """
66 Testing the XOS Config Module
67 """
68
Scott Baker7dddd512017-10-24 10:13:34 -070069 def setUp(self):
70 # In case some other testcase in nose has left config in an unclean state
71 Config.clear()
72
Matteo Scandolo56879722017-05-17 21:39:54 -070073 def tearDown(self):
74 # NOTE clear the config after each test
75 Config.clear()
76
77 def test_initialize_only_once(self):
78 """
79 [XOS-Config] Raise if initialized twice
80 """
81 with self.assertRaises(Exception) as e:
82 Config.init(sample_conf)
83 Config2.init(sample_conf)
Zack Williams9a42f872019-02-15 17:56:04 -070084 self.assertEqual(str(e.exception), "[XOS-Config] Module already initialized")
Matteo Scandolo56879722017-05-17 21:39:54 -070085
86 def test_config_not_initialized(self):
87 """
88 [XOS-Config] Raise if accessing properties without initialization
89 """
90 with self.assertRaises(Exception) as e:
91 Config.get("database")
Zack Williams045b63d2019-01-22 16:30:57 -070092 self.assertEqual(
Zack Williams9a42f872019-02-15 17:56:04 -070093 str(e.exception), "[XOS-Config] Module has not been initialized"
Zack Williams045b63d2019-01-22 16:30:57 -070094 )
Matteo Scandolo56879722017-05-17 21:39:54 -070095
96 def test_missing_file_exception(self):
97 """
Zack Williams045b63d2019-01-22 16:30:57 -070098 [XOS-Config] Raise if file not found
Matteo Scandolo56879722017-05-17 21:39:54 -070099 """
100 with self.assertRaises(Exception) as e:
101 Config.init("missing_conf")
Zack Williams045b63d2019-01-22 16:30:57 -0700102 self.assertEqual(
Zack Williams9a42f872019-02-15 17:56:04 -0700103 str(e.exception), "[XOS-Config] Config file not found at: missing_conf"
Zack Williams045b63d2019-01-22 16:30:57 -0700104 )
Matteo Scandolo56879722017-05-17 21:39:54 -0700105
106 def test_yaml_not_valid(self):
107 """
108 [XOS-Config] Raise if yaml is not valid
109 """
110 with self.assertRaises(Exception) as e:
111 Config.init(yaml_not_valid)
Zack Williams045b63d2019-01-22 16:30:57 -0700112 self.assertTrue(
Zack Williams9a42f872019-02-15 17:56:04 -0700113 str(e.exception).startswith("[XOS-Config] The config format is wrong:")
Zack Williams045b63d2019-01-22 16:30:57 -0700114 )
Matteo Scandolo56879722017-05-17 21:39:54 -0700115
116 def test_invalid_format(self):
117 """
118 [XOS-Config] Raise if format is not valid (we expect a dictionary)
119 """
120 with self.assertRaises(Exception) as e:
121 Config.init(invalid_format)
Zack Williams045b63d2019-01-22 16:30:57 -0700122 self.assertEqual(
Zack Williams9a42f872019-02-15 17:56:04 -0700123 str(e.exception),
Zack Williams045b63d2019-01-22 16:30:57 -0700124 (
125 "[XOS-Config] The config format is wrong: Schema validation failed:\n"
126 " - Value '['I am', 'a yaml', 'but the', 'format is not', 'correct']' is not a dict. Value path: ''."
127 ),
128 )
Matteo Scandolo56879722017-05-17 21:39:54 -0700129
130 def test_env_override(self):
131 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700132 [XOS-Config] the XOS_CONFIG_FILE environment variable should override the config_file
Matteo Scandolo56879722017-05-17 21:39:54 -0700133 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700134 os.environ["XOS_CONFIG_FILE"] = "env.yaml"
Matteo Scandolo56879722017-05-17 21:39:54 -0700135 with self.assertRaises(Exception) as e:
136 Config.init("missing_conf")
Zack Williams045b63d2019-01-22 16:30:57 -0700137 self.assertEqual(
Zack Williams9a42f872019-02-15 17:56:04 -0700138 str(e.exception), "[XOS-Config] Config file not found at: env.yaml"
Zack Williams045b63d2019-01-22 16:30:57 -0700139 )
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700140 del os.environ["XOS_CONFIG_FILE"]
141
142 def test_schema_override(self):
143 """
144 [XOS-Config] the XOS_CONFIG_SCHEMA environment variable should override the config_schema
145 """
146 os.environ["XOS_CONFIG_SCHEMA"] = "env-schema.yaml"
147 with self.assertRaises(Exception) as e:
148 Config.init(basic_conf)
Zack Williams045b63d2019-01-22 16:30:57 -0700149 self.assertRegexpMatches(
Zack Williams9a42f872019-02-15 17:56:04 -0700150 str(e.exception),
Zack Williams045b63d2019-01-22 16:30:57 -0700151 r"\[XOS\-Config\] Config schema not found at: (.+)env-schema\.yaml",
152 )
Zack Williams9a42f872019-02-15 17:56:04 -0700153 # self.assertEqual(str(e.exception), "[XOS-Config] Config schema not found at: env-schema.yaml")
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700154 del os.environ["XOS_CONFIG_SCHEMA"]
155
156 def test_schema_override_usage(self):
157 """
158 [XOS-Config] the XOS_CONFIG_SCHEMA should be used to validate a config
159 """
160 os.environ["XOS_CONFIG_SCHEMA"] = small_schema
161 with self.assertRaises(Exception) as e:
162 Config.init(basic_conf)
Zack Williams045b63d2019-01-22 16:30:57 -0700163 self.assertEqual(
Zack Williams9a42f872019-02-15 17:56:04 -0700164 str(e.exception),
Zack Williams045b63d2019-01-22 16:30:57 -0700165 (
166 "[XOS-Config] The config format is wrong: Schema validation failed:\n"
167 " - Key 'database' was not defined. Path: ''."
168 ),
169 )
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700170 del os.environ["XOS_CONFIG_SCHEMA"]
Matteo Scandolo56879722017-05-17 21:39:54 -0700171
172 def test_get_cli_param(self):
173 """
174 [XOS-Config] Should read CLI -C param
175 """
176 args = ["-A", "Foo", "-c", "Bar", "-C", "config.yaml"]
177 res = Config.get_cli_param(args)
178 self.assertEqual(res, "config.yaml")
179
180 def test_get_default_val_for_missing_param(self):
181 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700182 [XOS-Config] Should get the default value if nothing is specified
Matteo Scandolo56879722017-05-17 21:39:54 -0700183 """
184 Config.init(basic_conf)
Matteo Scandolo8fa0f7a2017-09-25 15:00:07 -0700185 dir = Config.get("xos_dir")
186 self.assertEqual(dir, "/opt/xos")
Matteo Scandolo56879722017-05-17 21:39:54 -0700187
Matteo Scandoloe0fc6852017-06-07 16:01:54 -0700188 def test_get_config_file(self):
189 """
190 [XOS-Config] Should return the config file in use
191 """
192 Config.init(sample_conf)
193 res = Config.get_config_file()
194 self.assertEqual(res, sample_conf)
195
196 def test_get_missing_param(self):
Matteo Scandolo56879722017-05-17 21:39:54 -0700197 """
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700198 [XOS-Config] Should return None reading a missing param
Matteo Scandolo56879722017-05-17 21:39:54 -0700199 """
200 Config.init(sample_conf)
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700201 res = Config.get("foo")
202 self.assertEqual(res, None)
Matteo Scandolo56879722017-05-17 21:39:54 -0700203
204 def test_get_first_level(self):
205 """
206 [XOS-Config] Should return a first level param
207 """
208 Config.init(sample_conf)
209 # NOTE we are using Config2 here to be sure that the configuration is readable from any import,
210 # not only from the one that has been used to initialize it
211 res = Config2.get("database")
Zack Williams045b63d2019-01-22 16:30:57 -0700212 self.assertEqual(res, {"name": "xos", "username": "test", "password": "safe"})
Matteo Scandolo56879722017-05-17 21:39:54 -0700213
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700214 def test_get_child_level(self):
Matteo Scandolo56879722017-05-17 21:39:54 -0700215 """
216 [XOS-Config] Should return a child level param
217 """
218 Config.init(sample_conf)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700219 res = Config.get("database.name")
220 self.assertEqual(res, "xos")
221
222 def test_config_override(self):
223 """
224 [XOS-Config] If an override is provided for the config, it should return the overridden value
225 """
Zack Williams045b63d2019-01-22 16:30:57 -0700226 Config.init(sample_conf, "xos-config-schema.yaml", override_conf)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700227 res = Config.get("logging.level")
228 self.assertEqual(res, "info")
229 res = Config.get("database.password")
230 self.assertEqual(res, "overridden_password")
231
232 def test_config_extend(self):
233 """
Zack Williams045b63d2019-01-22 16:30:57 -0700234 [XOS-Config] If an override is provided for the config, it should
235 return the overridden value (also if not defined in the base one)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700236 """
Zack Williams045b63d2019-01-22 16:30:57 -0700237
238 Config.init(sample_conf, "xos-config-schema.yaml", extend_conf)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700239 res = Config.get("xos_dir")
240 self.assertEqual(res, "/opt/xos")
241 res = Config.get("database.password")
242 self.assertEqual(res, "safe")
Matteo Scandolo56879722017-05-17 21:39:54 -0700243
Zack Williams045b63d2019-01-22 16:30:57 -0700244
245if __name__ == "__main__":
Scott Baker7dddd512017-10-24 10:13:34 -0700246 unittest.main()