blob: c89ed47d8a04fbb5c5368afd1ed675251fde380e [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, {
129 "username": "test",
130 "password": "safe"
131 })
132
133 def _test_get_child_level(self):
134 """
135 [XOS-Config] Should return a child level param
136 """
137 Config.init(sample_conf)
138 res = Config.get("nested.parameter.for")
139 self.assertEqual(res, "testing")
140
141 def test_get_service_list(self):
142 """
143 [XOS-Config] Should query registrator and return a list of services
144 """
145 with patch("xosconfig.config.requests.get") as mock_get:
146 mock_get.return_value.json.return_value = services_list
147 res = Config.get_service_list()
148 self.assertEqual(res, [
149 "xos-ws",
150 "xos-db",
151 ])
152
153 def test_get_service_info(self):
154 """
155 [XOS-Config] Should query registrator and return service info
156 """
157 with patch("xosconfig.config.requests.get") as mock_get:
158 mock_get.return_value.json.return_value = db_service
159 info = Config.get_service_info("xos-db")
160 self.assertEqual(info, {
161 "name": "xos-db",
162 "url": "172.18.0.4",
163 "port": 5432
164 })
165
166 def test_fail_get_service_info(self):
167 """
168 [XOS-Config] Should query registrator and return an exception if it"s down
169 """
170 with patch("xosconfig.config.requests.get") as mock_get:
171 mock_get.return_value.ok = False
172 with self.assertRaises(Exception) as e:
173 Config.get_service_info("missing-service")
174 self.assertEqual(e.exception.message, "[XOS-Config] Registrator is down")
175
176 def test_missing_get_service_info(self):
177 """
178 [XOS-Config] Should query registrator and return an exception if service is not there
179 """
180 with patch("xosconfig.config.requests.get") as mock_get:
181 mock_get.return_value.json.return_value = []
182 with self.assertRaises(Exception) as e:
183 Config.get_service_info("missing-service")
184 self.assertEqual(e.exception.message, "[XOS-Config] The service missing-service looking for does not exist")
185
186
187 def test_get_service_endpoint(self):
188 """
189 [XOS-Config] Should query registrator and return service endpoint
190 """
191 with patch("xosconfig.config.requests.get") as mock_get:
192 mock_get.return_value.json.return_value = db_service
193 endpoint = Config.get_service_endpoint("xos-db")
194 self.assertEqual(endpoint, "http://172.18.0.4:5432")