Debian Codegraph @

config_tempest/tests/test_tempest_conf.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Red Hat, Inc.
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17
18 from unittest import mock
19
20 from config_tempest import tempest_conf
21 from config_tempest.tests.base import BaseConfigTempestTest
22
23
24 class TestTempestConf(BaseConfigTempestTest):
25 def setUp(self):
26 super(TestTempestConf, self).setUp()
27 self.conf = tempest_conf.TempestConf()
28
29 def test_set_value(self):
30 conf = self._get_conf("v2.0", "v3")
31 resp = conf.set("section", "key", "value")
32 self.assertTrue(resp)
33 self.assertEqual(conf.get("section", "key"), "value")
34 self.assertEqual(conf.get_defaulted("section", "key"), "value")
35
36 def test_set_value_overwrite(self):
37 conf = self._get_conf("v2.0", "v3")
38 # set value without priority (default: priority=False)
39 resp = conf.set("section", "key", "value")
40 self.assertTrue(resp)
41 self.assertEqual(conf.get("section", "key"), "value")
42 # value should be overwritten, because it wasn't set with priority
43 resp = conf.set("section", "key", "new_value")
44 self.assertTrue(resp)
45 self.assertEqual(conf.get("section", "key"), "new_value")
46
47 def test_set_value_overwrite_priority(self):
48 conf = self._get_conf("v2.0", "v3")
49 resp = conf.set("sectionPriority", "key", "value", priority=True)
50 resp = conf.set("sectionPriority", "key", "new_value")
51 self.assertFalse(resp)
52 self.assertEqual(conf.get("sectionPriority", "key"), "value")
53
54 def test_set_value_overwrite_by_priority(self):
55 conf = self._get_conf("v2.0", "v3")
56 resp = conf.set("section", "key", "value")
57 self.assertTrue(resp)
58 self.assertEqual(conf.get("section", "key"), "value")
59 resp = conf.set("section", "key", "new_value", priority=True)
60 self.assertTrue(resp)
61 self.assertEqual(conf.get("section", "key"), "new_value")
62
63 def test_set_value_overwrite_priority_by_priority(self):
64 conf = self._get_conf("v2.0", "v3")
65 resp = conf.set("sectionPriority", "key", "value", priority=True)
66 self.assertTrue(resp)
67 self.assertEqual(conf.get("sectionPriority", "key"), "value")
68 resp = conf.set("sectionPriority", "key", "new_value", priority=True)
69 self.assertTrue(resp)
70 self.assertEqual(conf.get("sectionPriority", "key"), "new_value")
71
72 def test_get_bool_value(self):
73 self.assertTrue(self.conf.get_bool_value("True"))
74 self.assertFalse(self.conf.get_bool_value("False"))
75 self.assertRaises(ValueError, self.conf.get_bool_value, "no")
76
77 def test_remove_values(self):
78 api_exts = "router_availability_zone,rbac-policies,pagination,sorting,"
79 api_exts += "standard-attr-description,router,binding,metering,"
80 api_exts += "allowed-address-pairs,project-id,dvr,l3-flavors,tag-ext"
81 remove_exts = ["router", "project-id", "dvr"]
82 remove = {
83 "identity.username": ["demo"],
84 "identity.project_name": ["project"],
85 "compute.image_ssh_user": ["rhel", "cirros"],
86 "network-feature-enabled.api_extensions": remove_exts
87 }
88 conf = self._get_conf("v2.0", "v3")
89 conf.set("compute", "image_ssh_user", "cirros")
90 conf.set("network-feature-enabled", "api_extensions", api_exts)
91 conf.remove_values(remove)
92 self.assertFalse(conf.has_option("identity", "username"))
93 self.assertTrue(conf.has_option("identity", "project_name"))
94 self.assertFalse(conf.has_option("compute", "image_ssh_user"))
95 conf_exts = conf.get("network-feature-enabled", "api_extensions")
96 conf_exts = conf_exts.split(',')
97 for ext in api_exts.split(','):
98 if ext in remove_exts:
99 self.assertNotIn(ext, conf_exts)
100 else:
101 self.assertIn(ext, conf_exts)
102
103 def test_remove_values_having_hyphen(self):
104 api_exts = "dvr,l3-flavors,rbac-policies,project-id"
105 remove_exts = ["dvr", "project-id"]
106 remove = {
107 "network-feature-enabled.api_extensions": remove_exts
108 }
109 conf = self._get_conf("v2.0", "v3")
110 conf.set("network-feature-enabled", "api_extensions", api_exts)
111 conf.remove_values(remove)
112 conf_exts = conf.get("network-feature-enabled", "api_extensions")
113 conf_exts = conf_exts.split(',')
114 for ext in api_exts.split(','):
115 if ext in remove_exts:
116 self.assertNotIn(ext, conf_exts)
117 else:
118 self.assertIn(ext, conf_exts)
119
120 @mock.patch('config_tempest.tempest_conf.C.LOG')
121 def test_remove_not_defined_values(self, mock_logging):
122 self.conf.remove_values({"notExistSection.key": []})
123 # check if LOG.error was called
124 self.assertTrue(mock_logging.error.called)
125 self.conf.remove_values({"section.notExistKey": []})
126 # check if LOG.error was called
127 self.assertTrue(mock_logging.error.called)
128
129 def test_append_values(self):
130 api_exts = "dvr,l3-flavors,rbac-policies"
131 add_exts = ["dvr", "project-id"]
132 add = {
133 "compute-feature-enabled.api_extensions": add_exts
134 }
135 self.conf = self._get_conf("v2.0", "v3")
136 self.conf.set("compute-feature-enabled", "api_extensions", api_exts)
137 self.conf.append_values(add)
138 conf_exts = self.conf.get("compute-feature-enabled", "api_extensions")
139 conf_exts = conf_exts.split(',')
140 self.assertEqual(len(conf_exts), 4)
141 self.assertIn("project-id", conf_exts)
142
143 def test_append_values_with_overrides(self):
144 # Test if --add option can override an option which was
145 # passed to python-tempestconf as an override, it shouldn't
146 api_exts = "dvr,l3-flavors,rbac-policies"
147 add_exts = ["dvr", "project-id"]
148 add = {
149 "compute-feature-enabled.api_extensions": add_exts
150 }
151 self.conf = self._get_conf("v2.0", "v3")
152 # let's simulate a situation when the following apis were set
153 # via overrides => they are set with the priority
154 self.conf.set("compute-feature-enabled", "api_extensions",
155 api_exts, priority=True)
156 self.conf.append_values(add)
157 conf_exts = self.conf.get("compute-feature-enabled", "api_extensions")
158 conf_exts = conf_exts.split(',')
159 # if there are still 3 extensions, no new was added
160 self.assertEqual(len(conf_exts), 3)
161 # option added via --add shouldn't be there
162 self.assertNotIn("project-id", conf_exts)
163 self.assertIn("dvr", conf_exts)
164 self.assertIn("l3-flavors", conf_exts)
165 self.assertIn("rbac-policies", conf_exts)