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