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") |
| 27 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 28 | small_schema = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml") |
| 29 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 30 | services_list = { |
| 31 | "xos-ws": [], |
| 32 | "xos-db": [], |
| 33 | } |
| 34 | |
| 35 | db_service = [ |
| 36 | { |
| 37 | "ModifyIndex": 6, |
| 38 | "CreateIndex": 6, |
| 39 | "Node": "0152982c3159", |
| 40 | "Address": "172.19.0.2", |
| 41 | "ServiceID": "0d53ce210785:frontend_xos_db_1:5432", |
| 42 | "ServiceName": "xos-db", |
| 43 | "ServiceTags": [], |
| 44 | "ServiceAddress": "172.18.0.4", |
| 45 | "ServicePort": 5432, |
| 46 | "ServiceEnableTagOverride": "false" |
| 47 | } |
| 48 | ] |
| 49 | |
| 50 | class XOSConfigTest(unittest.TestCase): |
| 51 | """ |
| 52 | Testing the XOS Config Module |
| 53 | """ |
| 54 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 55 | def setUp(self): |
| 56 | # In case some other testcase in nose has left config in an unclean state |
| 57 | Config.clear() |
| 58 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 59 | def tearDown(self): |
| 60 | # NOTE clear the config after each test |
| 61 | Config.clear() |
| 62 | |
| 63 | def test_initialize_only_once(self): |
| 64 | """ |
| 65 | [XOS-Config] Raise if initialized twice |
| 66 | """ |
| 67 | with self.assertRaises(Exception) as e: |
| 68 | Config.init(sample_conf) |
| 69 | Config2.init(sample_conf) |
| 70 | self.assertEqual(e.exception.message, "[XOS-Config] Module already initialized") |
| 71 | |
| 72 | def test_config_not_initialized(self): |
| 73 | """ |
| 74 | [XOS-Config] Raise if accessing properties without initialization |
| 75 | """ |
| 76 | with self.assertRaises(Exception) as e: |
| 77 | Config.get("database") |
| 78 | self.assertEqual(e.exception.message, "[XOS-Config] Module has not been initialized") |
| 79 | |
| 80 | def test_missing_file_exception(self): |
| 81 | """ |
| 82 | [XOS-Config] Raise if file not found |
| 83 | """ |
| 84 | with self.assertRaises(Exception) as e: |
| 85 | Config.init("missing_conf") |
| 86 | self.assertEqual(e.exception.message, "[XOS-Config] Config file not found at: missing_conf") |
| 87 | |
| 88 | def test_yaml_not_valid(self): |
| 89 | """ |
| 90 | [XOS-Config] Raise if yaml is not valid |
| 91 | """ |
| 92 | with self.assertRaises(Exception) as e: |
| 93 | Config.init(yaml_not_valid) |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame^] | 94 | 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] | 95 | |
| 96 | def test_invalid_format(self): |
| 97 | """ |
| 98 | [XOS-Config] Raise if format is not valid (we expect a dictionary) |
| 99 | """ |
| 100 | with self.assertRaises(Exception) as e: |
| 101 | Config.init(invalid_format) |
| 102 | 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: ''.") |
| 103 | |
| 104 | def test_env_override(self): |
| 105 | """ |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 106 | [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] | 107 | """ |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 108 | os.environ["XOS_CONFIG_FILE"] = "env.yaml" |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 109 | with self.assertRaises(Exception) as e: |
| 110 | Config.init("missing_conf") |
| 111 | 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] | 112 | del os.environ["XOS_CONFIG_FILE"] |
| 113 | |
| 114 | def test_schema_override(self): |
| 115 | """ |
| 116 | [XOS-Config] the XOS_CONFIG_SCHEMA environment variable should override the config_schema |
| 117 | """ |
| 118 | os.environ["XOS_CONFIG_SCHEMA"] = "env-schema.yaml" |
| 119 | with self.assertRaises(Exception) as e: |
| 120 | Config.init(basic_conf) |
| 121 | self.assertRegexpMatches(e.exception.message, '\[XOS\-Config\] Config schema not found at: (.+)env-schema\.yaml') |
| 122 | # self.assertEqual(e.exception.message, "[XOS-Config] Config schema not found at: env-schema.yaml") |
| 123 | del os.environ["XOS_CONFIG_SCHEMA"] |
| 124 | |
| 125 | def test_schema_override_usage(self): |
| 126 | """ |
| 127 | [XOS-Config] the XOS_CONFIG_SCHEMA should be used to validate a config |
| 128 | """ |
| 129 | os.environ["XOS_CONFIG_SCHEMA"] = small_schema |
| 130 | with self.assertRaises(Exception) as e: |
| 131 | Config.init(basic_conf) |
| 132 | self.assertEqual(e.exception.message, "[XOS-Config] The config format is wrong: Schema validation failed:\n - Key 'database' was not defined. Path: ''.") |
| 133 | del os.environ["XOS_CONFIG_SCHEMA"] |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 134 | |
| 135 | def test_get_cli_param(self): |
| 136 | """ |
| 137 | [XOS-Config] Should read CLI -C param |
| 138 | """ |
| 139 | args = ["-A", "Foo", "-c", "Bar", "-C", "config.yaml"] |
| 140 | res = Config.get_cli_param(args) |
| 141 | self.assertEqual(res, "config.yaml") |
| 142 | |
| 143 | def test_get_default_val_for_missing_param(self): |
| 144 | """ |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 145 | [XOS-Config] Should get the default value if nothing is specified |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 146 | """ |
| 147 | Config.init(basic_conf) |
Matteo Scandolo | 8fa0f7a | 2017-09-25 15:00:07 -0700 | [diff] [blame] | 148 | dir = Config.get("xos_dir") |
| 149 | self.assertEqual(dir, "/opt/xos") |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 150 | |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 151 | def test_get_config_file(self): |
| 152 | """ |
| 153 | [XOS-Config] Should return the config file in use |
| 154 | """ |
| 155 | Config.init(sample_conf) |
| 156 | res = Config.get_config_file() |
| 157 | self.assertEqual(res, sample_conf) |
| 158 | |
| 159 | def test_get_missing_param(self): |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 160 | """ |
| 161 | [XOS-Config] Should raise reading a missing param |
| 162 | """ |
| 163 | Config.init(sample_conf) |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 164 | res = Config.get("foo") |
| 165 | self.assertEqual(res, None) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 166 | |
| 167 | def test_get_first_level(self): |
| 168 | """ |
| 169 | [XOS-Config] Should return a first level param |
| 170 | """ |
| 171 | Config.init(sample_conf) |
| 172 | # NOTE we are using Config2 here to be sure that the configuration is readable from any import, |
| 173 | # not only from the one that has been used to initialize it |
| 174 | res = Config2.get("database") |
| 175 | self.assertEqual(res, { |
Matteo Scandolo | 6bc017c | 2017-05-25 18:37:42 -0700 | [diff] [blame] | 176 | "name": "xos", |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 177 | "username": "test", |
| 178 | "password": "safe" |
| 179 | }) |
| 180 | |
| 181 | def _test_get_child_level(self): |
| 182 | """ |
| 183 | [XOS-Config] Should return a child level param |
| 184 | """ |
| 185 | Config.init(sample_conf) |
| 186 | res = Config.get("nested.parameter.for") |
| 187 | self.assertEqual(res, "testing") |
| 188 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 189 | if __name__ == '__main__': |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 190 | unittest.main() |