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 | |
| 136 | def _test_get_missing_param(self): |
| 137 | """ |
| 138 | [XOS-Config] Should raise reading a missing param |
| 139 | """ |
| 140 | Config.init(sample_conf) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 141 | res = Config.get("foo") |
| 142 | self.assertEqual(res, None) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 143 | |
| 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 Scandolo | 6bc017c | 2017-05-25 18:37:42 -0700 | [diff] [blame] | 153 | "name": "xos", |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 154 | "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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 219 | self.assertEqual(endpoint, "http://172.18.0.4:5432") |
| 220 | |
| 221 | |
| 222 | if __name__ == '__main__': |
| 223 | unittest.main() |