Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 16 | import unittest |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 17 | import os |
| 18 | from xosconfig import Config |
| 19 | from xosconfig import Config as Config2 |
| 20 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 21 | basic_conf = os.path.abspath( |
| 22 | os.path.dirname(os.path.realpath(__file__)) + "/confs/basic_conf.yaml" |
| 23 | ) |
| 24 | yaml_not_valid = os.path.abspath( |
| 25 | os.path.dirname(os.path.realpath(__file__)) + "/confs/yaml_not_valid.yaml" |
| 26 | ) |
| 27 | invalid_format = os.path.abspath( |
| 28 | os.path.dirname(os.path.realpath(__file__)) + "/confs/invalid_format.yaml" |
| 29 | ) |
| 30 | sample_conf = os.path.abspath( |
| 31 | os.path.dirname(os.path.realpath(__file__)) + "/confs/sample_conf.yaml" |
| 32 | ) |
| 33 | override_conf = os.path.abspath( |
| 34 | os.path.dirname(os.path.realpath(__file__)) + "/confs/override_conf.yaml" |
| 35 | ) |
| 36 | extend_conf = os.path.abspath( |
| 37 | os.path.dirname(os.path.realpath(__file__)) + "/confs/extend_conf.yaml" |
| 38 | ) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 39 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 40 | small_schema = os.path.abspath( |
| 41 | os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml" |
| 42 | ) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 43 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 44 | services_list = {"xos-ws": [], "xos-db": []} |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 45 | |
| 46 | db_service = [ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 47 | { |
| 48 | "ModifyIndex": 6, |
| 49 | "CreateIndex": 6, |
| 50 | "Node": "0152982c3159", |
| 51 | "Address": "172.19.0.2", |
| 52 | "ServiceID": "0d53ce210785:frontend_xos_db_1:5432", |
| 53 | "ServiceName": "xos-db", |
| 54 | "ServiceTags": [], |
| 55 | "ServiceAddress": "172.18.0.4", |
| 56 | "ServicePort": 5432, |
| 57 | "ServiceEnableTagOverride": "false", |
| 58 | } |
| 59 | ] |
| 60 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 61 | |
| 62 | class XOSConfigTest(unittest.TestCase): |
| 63 | """ |
| 64 | Testing the XOS Config Module |
| 65 | """ |
| 66 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 67 | def setUp(self): |
| 68 | # In case some other testcase in nose has left config in an unclean state |
| 69 | Config.clear() |
| 70 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 71 | def tearDown(self): |
| 72 | # NOTE clear the config after each test |
| 73 | Config.clear() |
| 74 | |
| 75 | def test_initialize_only_once(self): |
| 76 | """ |
| 77 | [XOS-Config] Raise if initialized twice |
| 78 | """ |
| 79 | with self.assertRaises(Exception) as e: |
| 80 | Config.init(sample_conf) |
| 81 | Config2.init(sample_conf) |
| 82 | self.assertEqual(e.exception.message, "[XOS-Config] Module already initialized") |
| 83 | |
| 84 | def test_config_not_initialized(self): |
| 85 | """ |
| 86 | [XOS-Config] Raise if accessing properties without initialization |
| 87 | """ |
| 88 | with self.assertRaises(Exception) as e: |
| 89 | Config.get("database") |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 90 | self.assertEqual( |
| 91 | e.exception.message, "[XOS-Config] Module has not been initialized" |
| 92 | ) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 93 | |
| 94 | def test_missing_file_exception(self): |
| 95 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 96 | [XOS-Config] Raise if file not found |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 97 | """ |
| 98 | with self.assertRaises(Exception) as e: |
| 99 | Config.init("missing_conf") |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 100 | self.assertEqual( |
| 101 | e.exception.message, "[XOS-Config] Config file not found at: missing_conf" |
| 102 | ) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 103 | |
| 104 | def test_yaml_not_valid(self): |
| 105 | """ |
| 106 | [XOS-Config] Raise if yaml is not valid |
| 107 | """ |
| 108 | with self.assertRaises(Exception) as e: |
| 109 | Config.init(yaml_not_valid) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 110 | self.assertTrue( |
| 111 | e.exception.message.startswith("[XOS-Config] The config format is wrong:") |
| 112 | ) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 113 | |
| 114 | def test_invalid_format(self): |
| 115 | """ |
| 116 | [XOS-Config] Raise if format is not valid (we expect a dictionary) |
| 117 | """ |
| 118 | with self.assertRaises(Exception) as e: |
| 119 | Config.init(invalid_format) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 120 | self.assertEqual( |
| 121 | e.exception.message, |
| 122 | ( |
| 123 | "[XOS-Config] The config format is wrong: Schema validation failed:\n" |
| 124 | " - Value '['I am', 'a yaml', 'but the', 'format is not', 'correct']' is not a dict. Value path: ''." |
| 125 | ), |
| 126 | ) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 127 | |
| 128 | def test_env_override(self): |
| 129 | """ |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 130 | [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] | 131 | """ |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 132 | os.environ["XOS_CONFIG_FILE"] = "env.yaml" |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 133 | with self.assertRaises(Exception) as e: |
| 134 | Config.init("missing_conf") |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 135 | self.assertEqual( |
| 136 | e.exception.message, "[XOS-Config] Config file not found at: env.yaml" |
| 137 | ) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 138 | del os.environ["XOS_CONFIG_FILE"] |
| 139 | |
| 140 | def test_schema_override(self): |
| 141 | """ |
| 142 | [XOS-Config] the XOS_CONFIG_SCHEMA environment variable should override the config_schema |
| 143 | """ |
| 144 | os.environ["XOS_CONFIG_SCHEMA"] = "env-schema.yaml" |
| 145 | with self.assertRaises(Exception) as e: |
| 146 | Config.init(basic_conf) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 147 | self.assertRegexpMatches( |
| 148 | e.exception.message, |
| 149 | r"\[XOS\-Config\] Config schema not found at: (.+)env-schema\.yaml", |
| 150 | ) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 151 | # self.assertEqual(e.exception.message, "[XOS-Config] Config schema not found at: env-schema.yaml") |
| 152 | del os.environ["XOS_CONFIG_SCHEMA"] |
| 153 | |
| 154 | def test_schema_override_usage(self): |
| 155 | """ |
| 156 | [XOS-Config] the XOS_CONFIG_SCHEMA should be used to validate a config |
| 157 | """ |
| 158 | os.environ["XOS_CONFIG_SCHEMA"] = small_schema |
| 159 | with self.assertRaises(Exception) as e: |
| 160 | Config.init(basic_conf) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 161 | self.assertEqual( |
| 162 | e.exception.message, |
| 163 | ( |
| 164 | "[XOS-Config] The config format is wrong: Schema validation failed:\n" |
| 165 | " - Key 'database' was not defined. Path: ''." |
| 166 | ), |
| 167 | ) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 168 | del os.environ["XOS_CONFIG_SCHEMA"] |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 169 | |
| 170 | def test_get_cli_param(self): |
| 171 | """ |
| 172 | [XOS-Config] Should read CLI -C param |
| 173 | """ |
| 174 | args = ["-A", "Foo", "-c", "Bar", "-C", "config.yaml"] |
| 175 | res = Config.get_cli_param(args) |
| 176 | self.assertEqual(res, "config.yaml") |
| 177 | |
| 178 | def test_get_default_val_for_missing_param(self): |
| 179 | """ |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 180 | [XOS-Config] Should get the default value if nothing is specified |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 181 | """ |
| 182 | Config.init(basic_conf) |
Matteo Scandolo | 8fa0f7a | 2017-09-25 15:00:07 -0700 | [diff] [blame] | 183 | dir = Config.get("xos_dir") |
| 184 | self.assertEqual(dir, "/opt/xos") |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 185 | |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 186 | def test_get_config_file(self): |
| 187 | """ |
| 188 | [XOS-Config] Should return the config file in use |
| 189 | """ |
| 190 | Config.init(sample_conf) |
| 191 | res = Config.get_config_file() |
| 192 | self.assertEqual(res, sample_conf) |
| 193 | |
| 194 | def test_get_missing_param(self): |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 195 | """ |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 196 | [XOS-Config] Should return None reading a missing param |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 197 | """ |
| 198 | Config.init(sample_conf) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 199 | res = Config.get("foo") |
| 200 | self.assertEqual(res, None) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 201 | |
| 202 | def test_get_first_level(self): |
| 203 | """ |
| 204 | [XOS-Config] Should return a first level param |
| 205 | """ |
| 206 | Config.init(sample_conf) |
| 207 | # NOTE we are using Config2 here to be sure that the configuration is readable from any import, |
| 208 | # not only from the one that has been used to initialize it |
| 209 | res = Config2.get("database") |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 210 | self.assertEqual(res, {"name": "xos", "username": "test", "password": "safe"}) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 211 | |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 212 | def test_get_child_level(self): |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 213 | """ |
| 214 | [XOS-Config] Should return a child level param |
| 215 | """ |
| 216 | Config.init(sample_conf) |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 217 | res = Config.get("database.name") |
| 218 | self.assertEqual(res, "xos") |
| 219 | |
| 220 | def test_config_override(self): |
| 221 | """ |
| 222 | [XOS-Config] If an override is provided for the config, it should return the overridden value |
| 223 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 224 | Config.init(sample_conf, "xos-config-schema.yaml", override_conf) |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 225 | res = Config.get("logging.level") |
| 226 | self.assertEqual(res, "info") |
| 227 | res = Config.get("database.password") |
| 228 | self.assertEqual(res, "overridden_password") |
| 229 | |
| 230 | def test_config_extend(self): |
| 231 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 232 | [XOS-Config] If an override is provided for the config, it should |
| 233 | return the overridden value (also if not defined in the base one) |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 234 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 235 | |
| 236 | Config.init(sample_conf, "xos-config-schema.yaml", extend_conf) |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 237 | res = Config.get("xos_dir") |
| 238 | self.assertEqual(res, "/opt/xos") |
| 239 | res = Config.get("database.password") |
| 240 | self.assertEqual(res, "safe") |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 241 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 242 | |
| 243 | if __name__ == "__main__": |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 244 | unittest.main() |