blob: 16844722daa9f858a4b109585d8d5da543b260fa [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo56879722017-05-17 21:39:54 -070017import unittest
18from mock import patch
19import os
20from xosconfig import Config
21from xosconfig import Config as Config2
22
23basic_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/basic_conf.yaml")
24yaml_not_valid = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/yaml_not_valid.yaml")
25invalid_format = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/invalid_format.yaml")
26sample_conf = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/confs/sample_conf.yaml")
27
Matteo Scandolo1879ce72017-05-30 15:45:26 -070028small_schema = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/schemas/small_schema.yaml")
29
Matteo Scandolo56879722017-05-17 21:39:54 -070030services_list = {
31 "xos-ws": [],
32 "xos-db": [],
33}
34
35db_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
50class XOSConfigTest(unittest.TestCase):
51 """
52 Testing the XOS Config Module
53 """
54
Scott Baker7dddd512017-10-24 10:13:34 -070055 def setUp(self):
56 # In case some other testcase in nose has left config in an unclean state
57 Config.clear()
58
Matteo Scandolo56879722017-05-17 21:39:54 -070059 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 Bakerb96ba432018-02-26 09:53:48 -080094 self.assertTrue(e.exception.message.startswith("[XOS-Config] The config format is wrong:"))
Matteo Scandolo56879722017-05-17 21:39:54 -070095
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 Scandolo1879ce72017-05-30 15:45:26 -0700106 [XOS-Config] the XOS_CONFIG_FILE environment variable should override the config_file
Matteo Scandolo56879722017-05-17 21:39:54 -0700107 """
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700108 os.environ["XOS_CONFIG_FILE"] = "env.yaml"
Matteo Scandolo56879722017-05-17 21:39:54 -0700109 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 Scandolo1879ce72017-05-30 15:45:26 -0700112 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 Scandolo56879722017-05-17 21:39:54 -0700134
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 Scandolo1879ce72017-05-30 15:45:26 -0700145 [XOS-Config] Should get the default value if nothing is specified
Matteo Scandolo56879722017-05-17 21:39:54 -0700146 """
147 Config.init(basic_conf)
Matteo Scandolo8fa0f7a2017-09-25 15:00:07 -0700148 dir = Config.get("xos_dir")
149 self.assertEqual(dir, "/opt/xos")
Matteo Scandolo56879722017-05-17 21:39:54 -0700150
Matteo Scandoloe0fc6852017-06-07 16:01:54 -0700151 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 Scandolo56879722017-05-17 21:39:54 -0700160 """
161 [XOS-Config] Should raise reading a missing param
162 """
163 Config.init(sample_conf)
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700164 res = Config.get("foo")
165 self.assertEqual(res, None)
Matteo Scandolo56879722017-05-17 21:39:54 -0700166
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 Scandolo6bc017c2017-05-25 18:37:42 -0700176 "name": "xos",
Matteo Scandolo56879722017-05-17 21:39:54 -0700177 "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 Scandolo1879ce72017-05-30 15:45:26 -0700189if __name__ == '__main__':
Scott Baker7dddd512017-10-24 10:13:34 -0700190 unittest.main()