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