blob: b98b817841755a940041c72f5315a2b285b6ef09 [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
136 def _test_get_missing_param(self):
137 """
138 [XOS-Config] Should raise reading a missing param
139 """
140 Config.init(sample_conf)
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700141 res = Config.get("foo")
142 self.assertEqual(res, None)
Matteo Scandolo56879722017-05-17 21:39:54 -0700143
144 def test_get_first_level(self):
145 """
146 [XOS-Config] Should return a first level param
147 """
148 Config.init(sample_conf)
149 # NOTE we are using Config2 here to be sure that the configuration is readable from any import,
150 # not only from the one that has been used to initialize it
151 res = Config2.get("database")
152 self.assertEqual(res, {
Matteo Scandolo6bc017c2017-05-25 18:37:42 -0700153 "name": "xos",
Matteo Scandolo56879722017-05-17 21:39:54 -0700154 "username": "test",
155 "password": "safe"
156 })
157
158 def _test_get_child_level(self):
159 """
160 [XOS-Config] Should return a child level param
161 """
162 Config.init(sample_conf)
163 res = Config.get("nested.parameter.for")
164 self.assertEqual(res, "testing")
165
166 def test_get_service_list(self):
167 """
168 [XOS-Config] Should query registrator and return a list of services
169 """
170 with patch("xosconfig.config.requests.get") as mock_get:
171 mock_get.return_value.json.return_value = services_list
172 res = Config.get_service_list()
173 self.assertEqual(res, [
174 "xos-ws",
175 "xos-db",
176 ])
177
178 def test_get_service_info(self):
179 """
180 [XOS-Config] Should query registrator and return service info
181 """
182 with patch("xosconfig.config.requests.get") as mock_get:
183 mock_get.return_value.json.return_value = db_service
184 info = Config.get_service_info("xos-db")
185 self.assertEqual(info, {
186 "name": "xos-db",
187 "url": "172.18.0.4",
188 "port": 5432
189 })
190
191 def test_fail_get_service_info(self):
192 """
193 [XOS-Config] Should query registrator and return an exception if it"s down
194 """
195 with patch("xosconfig.config.requests.get") as mock_get:
196 mock_get.return_value.ok = False
197 with self.assertRaises(Exception) as e:
198 Config.get_service_info("missing-service")
199 self.assertEqual(e.exception.message, "[XOS-Config] Registrator is down")
200
201 def test_missing_get_service_info(self):
202 """
203 [XOS-Config] Should query registrator and return an exception if service is not there
204 """
205 with patch("xosconfig.config.requests.get") as mock_get:
206 mock_get.return_value.json.return_value = []
207 with self.assertRaises(Exception) as e:
208 Config.get_service_info("missing-service")
209 self.assertEqual(e.exception.message, "[XOS-Config] The service missing-service looking for does not exist")
210
211
212 def test_get_service_endpoint(self):
213 """
214 [XOS-Config] Should query registrator and return service endpoint
215 """
216 with patch("xosconfig.config.requests.get") as mock_get:
217 mock_get.return_value.json.return_value = db_service
218 endpoint = Config.get_service_endpoint("xos-db")
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700219 self.assertEqual(endpoint, "http://172.18.0.4:5432")
220
221
222if __name__ == '__main__':
223 unittest.main()