blob: a3e57db7d784a9590d90f4b56335040cca6a4e14 [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
12services_list = {
13 "xos-ws": [],
14 "xos-db": [],
15}
16
17db_service = [
18 {
19 "ModifyIndex": 6,
20 "CreateIndex": 6,
21 "Node": "0152982c3159",
22 "Address": "172.19.0.2",
23 "ServiceID": "0d53ce210785:frontend_xos_db_1:5432",
24 "ServiceName": "xos-db",
25 "ServiceTags": [],
26 "ServiceAddress": "172.18.0.4",
27 "ServicePort": 5432,
28 "ServiceEnableTagOverride": "false"
29 }
30 ]
31
32class XOSConfigTest(unittest.TestCase):
33 """
34 Testing the XOS Config Module
35 """
36
37 def tearDown(self):
38 # NOTE clear the config after each test
39 Config.clear()
40
41 def test_initialize_only_once(self):
42 """
43 [XOS-Config] Raise if initialized twice
44 """
45 with self.assertRaises(Exception) as e:
46 Config.init(sample_conf)
47 Config2.init(sample_conf)
48 self.assertEqual(e.exception.message, "[XOS-Config] Module already initialized")
49
50 def test_config_not_initialized(self):
51 """
52 [XOS-Config] Raise if accessing properties without initialization
53 """
54 with self.assertRaises(Exception) as e:
55 Config.get("database")
56 self.assertEqual(e.exception.message, "[XOS-Config] Module has not been initialized")
57
58 def test_missing_file_exception(self):
59 """
60 [XOS-Config] Raise if file not found
61 """
62 with self.assertRaises(Exception) as e:
63 Config.init("missing_conf")
64 self.assertEqual(e.exception.message, "[XOS-Config] Config file not found at: missing_conf")
65
66 def test_yaml_not_valid(self):
67 """
68 [XOS-Config] Raise if yaml is not valid
69 """
70 with self.assertRaises(Exception) as e:
71 Config.init(yaml_not_valid)
72 self.assertEqual(e.exception.message, "[XOS-Config] The config format is wrong: Unable to load any data from source yaml file")
73
74 def test_invalid_format(self):
75 """
76 [XOS-Config] Raise if format is not valid (we expect a dictionary)
77 """
78 with self.assertRaises(Exception) as e:
79 Config.init(invalid_format)
80 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: ''.")
81
82 def test_env_override(self):
83 """
84 [XOS-Config] the XOS-CONFIG environment variable should override the config_file
85 """
86 os.environ["XOS-CONFIG"] = "env.yaml"
87 with self.assertRaises(Exception) as e:
88 Config.init("missing_conf")
89 self.assertEqual(e.exception.message, "[XOS-Config] Config file not found at: env.yaml")
90 del os.environ["XOS-CONFIG"]
91
92 def test_get_cli_param(self):
93 """
94 [XOS-Config] Should read CLI -C param
95 """
96 args = ["-A", "Foo", "-c", "Bar", "-C", "config.yaml"]
97 res = Config.get_cli_param(args)
98 self.assertEqual(res, "config.yaml")
99
100 def test_get_default_val_for_missing_param(self):
101 """
102 [XOS-Config] Should raise reading a missing param
103 """
104 Config.init(basic_conf)
105 log = Config.get("logging")
106 self.assertEqual(log, {
107 "level": "info",
108 "channels": ["file", "console"]
109 })
110
111 def _test_get_missing_param(self):
112 """
113 [XOS-Config] Should raise reading a missing param
114 """
115 Config.init(sample_conf)
116 with self.assertRaises(Exception) as e:
117 Config.get("foo")
118 self.assertEqual(e.exception.message, "[XOS-Config] Config does not have a value (or a default) parameter foo")
119
120 def test_get_first_level(self):
121 """
122 [XOS-Config] Should return a first level param
123 """
124 Config.init(sample_conf)
125 # NOTE we are using Config2 here to be sure that the configuration is readable from any import,
126 # not only from the one that has been used to initialize it
127 res = Config2.get("database")
128 self.assertEqual(res, {
Matteo Scandolo6bc017c2017-05-25 18:37:42 -0700129 "name": "xos",
Matteo Scandolo56879722017-05-17 21:39:54 -0700130 "username": "test",
131 "password": "safe"
132 })
133
134 def _test_get_child_level(self):
135 """
136 [XOS-Config] Should return a child level param
137 """
138 Config.init(sample_conf)
139 res = Config.get("nested.parameter.for")
140 self.assertEqual(res, "testing")
141
142 def test_get_service_list(self):
143 """
144 [XOS-Config] Should query registrator and return a list of services
145 """
146 with patch("xosconfig.config.requests.get") as mock_get:
147 mock_get.return_value.json.return_value = services_list
148 res = Config.get_service_list()
149 self.assertEqual(res, [
150 "xos-ws",
151 "xos-db",
152 ])
153
154 def test_get_service_info(self):
155 """
156 [XOS-Config] Should query registrator and return service info
157 """
158 with patch("xosconfig.config.requests.get") as mock_get:
159 mock_get.return_value.json.return_value = db_service
160 info = Config.get_service_info("xos-db")
161 self.assertEqual(info, {
162 "name": "xos-db",
163 "url": "172.18.0.4",
164 "port": 5432
165 })
166
167 def test_fail_get_service_info(self):
168 """
169 [XOS-Config] Should query registrator and return an exception if it"s down
170 """
171 with patch("xosconfig.config.requests.get") as mock_get:
172 mock_get.return_value.ok = False
173 with self.assertRaises(Exception) as e:
174 Config.get_service_info("missing-service")
175 self.assertEqual(e.exception.message, "[XOS-Config] Registrator is down")
176
177 def test_missing_get_service_info(self):
178 """
179 [XOS-Config] Should query registrator and return an exception if service is not there
180 """
181 with patch("xosconfig.config.requests.get") as mock_get:
182 mock_get.return_value.json.return_value = []
183 with self.assertRaises(Exception) as e:
184 Config.get_service_info("missing-service")
185 self.assertEqual(e.exception.message, "[XOS-Config] The service missing-service looking for does not exist")
186
187
188 def test_get_service_endpoint(self):
189 """
190 [XOS-Config] Should query registrator and return service endpoint
191 """
192 with patch("xosconfig.config.requests.get") as mock_get:
193 mock_get.return_value.json.return_value = db_service
194 endpoint = Config.get_service_endpoint("xos-db")
195 self.assertEqual(endpoint, "http://172.18.0.4:5432")