Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | from mock import patch |
| 3 | import os |
| 4 | from xosconfig import Config |
| 5 | from xosconfig import Config as Config2 |
| 6 | |
| 7 | basic_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/basic_conf.yaml") |
| 8 | yaml_not_valid = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/yaml_not_valid.yaml") |
| 9 | invalid_format = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/invalid_format.yaml") |
| 10 | sample_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/sample_conf.yaml") |
| 11 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 12 | small_schema = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml") |
| 13 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 14 | services_list = { |
| 15 | "xos-ws": [], |
| 16 | "xos-db": [], |
| 17 | } |
| 18 | |
| 19 | db_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 | |
| 34 | class 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 86 | [XOS-Config] the XOS_CONFIG_FILE environment variable should override the config_file |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 87 | """ |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 88 | os.environ["XOS_CONFIG_FILE"] = "env.yaml" |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 89 | 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 92 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 114 | |
| 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 125 | [XOS-Config] Should get the default value if nothing is specified |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 126 | """ |
| 127 | Config.init(basic_conf) |
| 128 | log = Config.get("logging") |
| 129 | self.assertEqual(log, { |
| 130 | "level": "info", |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 131 | "channels": ["file", "console"], |
| 132 | "logstash_hostport": "cordloghost:5617", |
| 133 | "file": "/var/log/xos.log", |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 134 | }) |
| 135 | |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 136 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 145 | """ |
| 146 | [XOS-Config] Should raise reading a missing param |
| 147 | """ |
| 148 | Config.init(sample_conf) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 149 | res = Config.get("foo") |
| 150 | self.assertEqual(res, None) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 151 | |
| 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 Scandolo | 6bc017c | 2017-05-25 18:37:42 -0700 | [diff] [blame] | 161 | "name": "xos", |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 162 | "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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 227 | self.assertEqual(endpoint, "http://172.18.0.4:5432") |
| 228 | |
| 229 | |
| 230 | if __name__ == '__main__': |
| 231 | unittest.main() |