blob: ee80b52ab5a4793f0fb124598fbb607872e4f6a7 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolo56879722017-05-17 21:39:54 -070017import unittest
18from mock import patch
19import os
20from xosconfig import Config
21from xosconfig import Config as Config2
22
23basic_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/basic_conf.yaml")
24yaml_not_valid = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/yaml_not_valid.yaml")
25invalid_format = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/invalid_format.yaml")
26sample_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/sample_conf.yaml")
Matteo Scandoloc59372a2018-06-11 15:17:40 -070027override_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/override_conf.yaml")
28extend_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/extend_conf.yaml")
Matteo Scandolo56879722017-05-17 21:39:54 -070029
Matteo Scandolo1879ce72017-05-30 15:45:26 -070030small_schema = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml")
31
Matteo Scandolo56879722017-05-17 21:39:54 -070032services_list = {
33 "xos-ws": [],
34 "xos-db": [],
35}
36
37db_service = [
38 {
39 "ModifyIndex": 6,
40 "CreateIndex": 6,
41 "Node": "0152982c3159",
42 "Address": "172.19.0.2",
43 "ServiceID": "0d53ce210785:frontend_xos_db_1:5432",
44 "ServiceName": "xos-db",
45 "ServiceTags": [],
46 "ServiceAddress": "172.18.0.4",
47 "ServicePort": 5432,
48 "ServiceEnableTagOverride": "false"
49 }
50 ]
51
52class XOSConfigTest(unittest.TestCase):
53 """
54 Testing the XOS Config Module
55 """
56
Scott Baker7dddd512017-10-24 10:13:34 -070057 def setUp(self):
58 # In case some other testcase in nose has left config in an unclean state
59 Config.clear()
60
Matteo Scandolo56879722017-05-17 21:39:54 -070061 def tearDown(self):
62 # NOTE clear the config after each test
63 Config.clear()
64
65 def test_initialize_only_once(self):
66 """
67 [XOS-Config] Raise if initialized twice
68 """
69 with self.assertRaises(Exception) as e:
70 Config.init(sample_conf)
71 Config2.init(sample_conf)
72 self.assertEqual(e.exception.message, "[XOS-Config] Module already initialized")
73
74 def test_config_not_initialized(self):
75 """
76 [XOS-Config] Raise if accessing properties without initialization
77 """
78 with self.assertRaises(Exception) as e:
79 Config.get("database")
80 self.assertEqual(e.exception.message, "[XOS-Config] Module has not been initialized")
81
82 def test_missing_file_exception(self):
83 """
84 [XOS-Config] Raise if file not found
85 """
86 with self.assertRaises(Exception) as e:
87 Config.init("missing_conf")
88 self.assertEqual(e.exception.message, "[XOS-Config] Config file not found at: missing_conf")
89
90 def test_yaml_not_valid(self):
91 """
92 [XOS-Config] Raise if yaml is not valid
93 """
94 with self.assertRaises(Exception) as e:
95 Config.init(yaml_not_valid)
Scott Bakerb96ba432018-02-26 09:53:48 -080096 self.assertTrue(e.exception.message.startswith("[XOS-Config] The config format is wrong:"))
Matteo Scandolo56879722017-05-17 21:39:54 -070097
98 def test_invalid_format(self):
99 """
100 [XOS-Config] Raise if format is not valid (we expect a dictionary)
101 """
102 with self.assertRaises(Exception) as e:
103 Config.init(invalid_format)
104 self.assertEqual(e.exception.message, "[XOS-Config] The config format is wrong: Schema validation failed:\n - Value '['I am', 'a yaml', 'but the', 'format is not', 'correct']' is not a dict. Value path: ''.")
105
106 def test_env_override(self):
107 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700108 [XOS-Config] the XOS_CONFIG_FILE environment variable should override the config_file
Matteo Scandolo56879722017-05-17 21:39:54 -0700109 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700110 os.environ["XOS_CONFIG_FILE"] = "env.yaml"
Matteo Scandolo56879722017-05-17 21:39:54 -0700111 with self.assertRaises(Exception) as e:
112 Config.init("missing_conf")
113 self.assertEqual(e.exception.message, "[XOS-Config] Config file not found at: env.yaml")
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700114 del os.environ["XOS_CONFIG_FILE"]
115
116 def test_schema_override(self):
117 """
118 [XOS-Config] the XOS_CONFIG_SCHEMA environment variable should override the config_schema
119 """
120 os.environ["XOS_CONFIG_SCHEMA"] = "env-schema.yaml"
121 with self.assertRaises(Exception) as e:
122 Config.init(basic_conf)
123 self.assertRegexpMatches(e.exception.message, '\[XOS\-Config\] Config schema not found at: (.+)env-schema\.yaml')
124 # self.assertEqual(e.exception.message, "[XOS-Config] Config schema not found at: env-schema.yaml")
125 del os.environ["XOS_CONFIG_SCHEMA"]
126
127 def test_schema_override_usage(self):
128 """
129 [XOS-Config] the XOS_CONFIG_SCHEMA should be used to validate a config
130 """
131 os.environ["XOS_CONFIG_SCHEMA"] = small_schema
132 with self.assertRaises(Exception) as e:
133 Config.init(basic_conf)
134 self.assertEqual(e.exception.message, "[XOS-Config] The config format is wrong: Schema validation failed:\n - Key 'database' was not defined. Path: ''.")
135 del os.environ["XOS_CONFIG_SCHEMA"]
Matteo Scandolo56879722017-05-17 21:39:54 -0700136
137 def test_get_cli_param(self):
138 """
139 [XOS-Config] Should read CLI -C param
140 """
141 args = ["-A", "Foo", "-c", "Bar", "-C", "config.yaml"]
142 res = Config.get_cli_param(args)
143 self.assertEqual(res, "config.yaml")
144
145 def test_get_default_val_for_missing_param(self):
146 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700147 [XOS-Config] Should get the default value if nothing is specified
Matteo Scandolo56879722017-05-17 21:39:54 -0700148 """
149 Config.init(basic_conf)
Matteo Scandolo8fa0f7a2017-09-25 15:00:07 -0700150 dir = Config.get("xos_dir")
151 self.assertEqual(dir, "/opt/xos")
Matteo Scandolo56879722017-05-17 21:39:54 -0700152
Matteo Scandoloe0fc6852017-06-07 16:01:54 -0700153 def test_get_config_file(self):
154 """
155 [XOS-Config] Should return the config file in use
156 """
157 Config.init(sample_conf)
158 res = Config.get_config_file()
159 self.assertEqual(res, sample_conf)
160
161 def test_get_missing_param(self):
Matteo Scandolo56879722017-05-17 21:39:54 -0700162 """
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700163 [XOS-Config] Should return None reading a missing param
Matteo Scandolo56879722017-05-17 21:39:54 -0700164 """
165 Config.init(sample_conf)
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700166 res = Config.get("foo")
167 self.assertEqual(res, None)
Matteo Scandolo56879722017-05-17 21:39:54 -0700168
169 def test_get_first_level(self):
170 """
171 [XOS-Config] Should return a first level param
172 """
173 Config.init(sample_conf)
174 # NOTE we are using Config2 here to be sure that the configuration is readable from any import,
175 # not only from the one that has been used to initialize it
176 res = Config2.get("database")
177 self.assertEqual(res, {
Matteo Scandolo6bc017c2017-05-25 18:37:42 -0700178 "name": "xos",
Matteo Scandolo56879722017-05-17 21:39:54 -0700179 "username": "test",
180 "password": "safe"
181 })
182
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700183 def test_get_child_level(self):
Matteo Scandolo56879722017-05-17 21:39:54 -0700184 """
185 [XOS-Config] Should return a child level param
186 """
187 Config.init(sample_conf)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700188 res = Config.get("database.name")
189 self.assertEqual(res, "xos")
190
191 def test_config_override(self):
192 """
193 [XOS-Config] If an override is provided for the config, it should return the overridden value
194 """
195 Config.init(sample_conf, 'xos-config-schema.yaml', override_conf)
196 res = Config.get("logging.level")
197 self.assertEqual(res, "info")
198 res = Config.get("database.password")
199 self.assertEqual(res, "overridden_password")
200
201 def test_config_extend(self):
202 """
203 [XOS-Config] If an override is provided for the config, it should return the overridden value (also if not defined in the base one)
204 """
205 Config.init(sample_conf, 'xos-config-schema.yaml', extend_conf)
206 res = Config.get("xos_dir")
207 self.assertEqual(res, "/opt/xos")
208 res = Config.get("database.password")
209 self.assertEqual(res, "safe")
Matteo Scandolo56879722017-05-17 21:39:54 -0700210
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700211if __name__ == '__main__':
Scott Baker7dddd512017-10-24 10:13:34 -0700212 unittest.main()