blob: 36869b3def963d5387d30160637d10bff07c4789 [file] [log] [blame]
Matteo Scandolo56879722017-05-17 21:39:54 -07001import unittest
2from mock import patch
3import os
4from xosconfig import Config
5from xosconfig import Config as Config2
6
7basic_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/basic_conf.yaml")
8yaml_not_valid = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/yaml_not_valid.yaml")
9invalid_format = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/invalid_format.yaml")
10sample_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/sample_conf.yaml")
11
Matteo Scandolo1879ce72017-05-30 15:45:26 -070012small_schema = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml")
13
Matteo Scandolo56879722017-05-17 21:39:54 -070014services_list = {
15 "xos-ws": [],
16 "xos-db": [],
17}
18
19db_service = [
20 {
21 "ModifyIndex": 6,
22 "CreateIndex": 6,
23 "Node": "0152982c3159",
24 "Address": "172.19.0.2",
25 "ServiceID": "0d53ce210785:frontend_xos_db_1:5432",
26 "ServiceName": "xos-db",
27 "ServiceTags": [],
28 "ServiceAddress": "172.18.0.4",
29 "ServicePort": 5432,
30 "ServiceEnableTagOverride": "false"
31 }
32 ]
33
34class XOSConfigTest(unittest.TestCase):
35 """
36 Testing the XOS Config Module
37 """
38
39 def tearDown(self):
40 # NOTE clear the config after each test
41 Config.clear()
42
43 def test_initialize_only_once(self):
44 """
45 [XOS-Config] Raise if initialized twice
46 """
47 with self.assertRaises(Exception) as e:
48 Config.init(sample_conf)
49 Config2.init(sample_conf)
50 self.assertEqual(e.exception.message, "[XOS-Config] Module already initialized")
51
52 def test_config_not_initialized(self):
53 """
54 [XOS-Config] Raise if accessing properties without initialization
55 """
56 with self.assertRaises(Exception) as e:
57 Config.get("database")
58 self.assertEqual(e.exception.message, "[XOS-Config] Module has not been initialized")
59
60 def test_missing_file_exception(self):
61 """
62 [XOS-Config] Raise if file not found
63 """
64 with self.assertRaises(Exception) as e:
65 Config.init("missing_conf")
66 self.assertEqual(e.exception.message, "[XOS-Config] Config file not found at: missing_conf")
67
68 def test_yaml_not_valid(self):
69 """
70 [XOS-Config] Raise if yaml is not valid
71 """
72 with self.assertRaises(Exception) as e:
73 Config.init(yaml_not_valid)
74 self.assertEqual(e.exception.message, "[XOS-Config] The config format is wrong: Unable to load any data from source yaml file")
75
76 def test_invalid_format(self):
77 """
78 [XOS-Config] Raise if format is not valid (we expect a dictionary)
79 """
80 with self.assertRaises(Exception) as e:
81 Config.init(invalid_format)
82 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: ''.")
83
84 def test_env_override(self):
85 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -070086 [XOS-Config] the XOS_CONFIG_FILE environment variable should override the config_file
Matteo Scandolo56879722017-05-17 21:39:54 -070087 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -070088 os.environ["XOS_CONFIG_FILE"] = "env.yaml"
Matteo Scandolo56879722017-05-17 21:39:54 -070089 with self.assertRaises(Exception) as e:
90 Config.init("missing_conf")
91 self.assertEqual(e.exception.message, "[XOS-Config] Config file not found at: env.yaml")
Matteo Scandolo1879ce72017-05-30 15:45:26 -070092 del os.environ["XOS_CONFIG_FILE"]
93
94 def test_schema_override(self):
95 """
96 [XOS-Config] the XOS_CONFIG_SCHEMA environment variable should override the config_schema
97 """
98 os.environ["XOS_CONFIG_SCHEMA"] = "env-schema.yaml"
99 with self.assertRaises(Exception) as e:
100 Config.init(basic_conf)
101 self.assertRegexpMatches(e.exception.message, '\[XOS\-Config\] Config schema not found at: (.+)env-schema\.yaml')
102 # self.assertEqual(e.exception.message, "[XOS-Config] Config schema not found at: env-schema.yaml")
103 del os.environ["XOS_CONFIG_SCHEMA"]
104
105 def test_schema_override_usage(self):
106 """
107 [XOS-Config] the XOS_CONFIG_SCHEMA should be used to validate a config
108 """
109 os.environ["XOS_CONFIG_SCHEMA"] = small_schema
110 with self.assertRaises(Exception) as e:
111 Config.init(basic_conf)
112 self.assertEqual(e.exception.message, "[XOS-Config] The config format is wrong: Schema validation failed:\n - Key 'database' was not defined. Path: ''.")
113 del os.environ["XOS_CONFIG_SCHEMA"]
Matteo Scandolo56879722017-05-17 21:39:54 -0700114
115 def test_get_cli_param(self):
116 """
117 [XOS-Config] Should read CLI -C param
118 """
119 args = ["-A", "Foo", "-c", "Bar", "-C", "config.yaml"]
120 res = Config.get_cli_param(args)
121 self.assertEqual(res, "config.yaml")
122
123 def test_get_default_val_for_missing_param(self):
124 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700125 [XOS-Config] Should get the default value if nothing is specified
Matteo Scandolo56879722017-05-17 21:39:54 -0700126 """
127 Config.init(basic_conf)
128 log = Config.get("logging")
129 self.assertEqual(log, {
130 "level": "info",
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700131 "channels": ["file", "console"],
132 "logstash_hostport": "cordloghost:5617",
133 "file": "/var/log/xos.log",
Matteo Scandolo56879722017-05-17 21:39:54 -0700134 })
135
Matteo Scandoloe0fc6852017-06-07 16:01:54 -0700136 def test_get_config_file(self):
137 """
138 [XOS-Config] Should return the config file in use
139 """
140 Config.init(sample_conf)
141 res = Config.get_config_file()
142 self.assertEqual(res, sample_conf)
143
144 def test_get_missing_param(self):
Matteo Scandolo56879722017-05-17 21:39:54 -0700145 """
146 [XOS-Config] Should raise reading a missing param
147 """
148 Config.init(sample_conf)
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700149 res = Config.get("foo")
150 self.assertEqual(res, None)
Matteo Scandolo56879722017-05-17 21:39:54 -0700151
152 def test_get_first_level(self):
153 """
154 [XOS-Config] Should return a first level param
155 """
156 Config.init(sample_conf)
157 # NOTE we are using Config2 here to be sure that the configuration is readable from any import,
158 # not only from the one that has been used to initialize it
159 res = Config2.get("database")
160 self.assertEqual(res, {
Matteo Scandolo6bc017c2017-05-25 18:37:42 -0700161 "name": "xos",
Matteo Scandolo56879722017-05-17 21:39:54 -0700162 "username": "test",
163 "password": "safe"
164 })
165
166 def _test_get_child_level(self):
167 """
168 [XOS-Config] Should return a child level param
169 """
170 Config.init(sample_conf)
171 res = Config.get("nested.parameter.for")
172 self.assertEqual(res, "testing")
173
174 def test_get_service_list(self):
175 """
176 [XOS-Config] Should query registrator and return a list of services
177 """
178 with patch("xosconfig.config.requests.get") as mock_get:
179 mock_get.return_value.json.return_value = services_list
180 res = Config.get_service_list()
181 self.assertEqual(res, [
182 "xos-ws",
183 "xos-db",
184 ])
185
186 def test_get_service_info(self):
187 """
188 [XOS-Config] Should query registrator and return service info
189 """
190 with patch("xosconfig.config.requests.get") as mock_get:
191 mock_get.return_value.json.return_value = db_service
192 info = Config.get_service_info("xos-db")
193 self.assertEqual(info, {
194 "name": "xos-db",
195 "url": "172.18.0.4",
196 "port": 5432
197 })
198
199 def test_fail_get_service_info(self):
200 """
201 [XOS-Config] Should query registrator and return an exception if it"s down
202 """
203 with patch("xosconfig.config.requests.get") as mock_get:
204 mock_get.return_value.ok = False
205 with self.assertRaises(Exception) as e:
206 Config.get_service_info("missing-service")
207 self.assertEqual(e.exception.message, "[XOS-Config] Registrator is down")
208
209 def test_missing_get_service_info(self):
210 """
211 [XOS-Config] Should query registrator and return an exception if service is not there
212 """
213 with patch("xosconfig.config.requests.get") as mock_get:
214 mock_get.return_value.json.return_value = []
215 with self.assertRaises(Exception) as e:
216 Config.get_service_info("missing-service")
217 self.assertEqual(e.exception.message, "[XOS-Config] The service missing-service looking for does not exist")
218
219
220 def test_get_service_endpoint(self):
221 """
222 [XOS-Config] Should query registrator and return service endpoint
223 """
224 with patch("xosconfig.config.requests.get") as mock_get:
225 mock_get.return_value.json.return_value = db_service
226 endpoint = Config.get_service_endpoint("xos-db")
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700227 self.assertEqual(endpoint, "http://172.18.0.4:5432")
228
229
230if __name__ == '__main__':
231 unittest.main()