Adding manual external DB URI config
[osm/devops.git] / installers / charm / keystone / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2021 Canonical Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 ##
22
23 import sys
24 from typing import NoReturn
25 import unittest
26
27
28 from charm import KeystoneCharm
29 from ops.model import ActiveStatus, BlockedStatus
30 from ops.testing import Harness
31
32
33 class TestCharm(unittest.TestCase):
34 """Keystone Charm unit tests."""
35
36 def setUp(self) -> NoReturn:
37 """Test setup"""
38 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
39 self.harness = Harness(KeystoneCharm)
40 self.harness.set_leader(is_leader=True)
41 self.harness.begin()
42 self.config = {
43 "region_id": "str",
44 "keystone_db_password": "str",
45 "mysql_host": "",
46 "mysql_port": 3306,
47 "mysql_root_password": "manopw",
48 "admin_username": "str",
49 "admin_password": "str",
50 "admin_project": "str",
51 "service_username": "str",
52 "service_password": "str",
53 "service_project": "str",
54 "user_domain_name": "str",
55 "project_domain_name": "str",
56 "token_expiration": 10,
57 "max_file_size": 1,
58 "site_url": "http://keystone.com",
59 "ldap_enabled": False,
60 }
61 self.harness.update_config(self.config)
62
63 def test_config_changed_no_relations(
64 self,
65 ) -> NoReturn:
66 """Test ingress resources without HTTP."""
67
68 self.harness.charm.on.config_changed.emit()
69
70 # Assertions
71 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
72 self.assertTrue(
73 all(
74 relation in self.harness.charm.unit.status.message
75 for relation in ["mysql"]
76 )
77 )
78
79 def test_config_changed_non_leader(
80 self,
81 ) -> NoReturn:
82 """Test ingress resources without HTTP."""
83 self.harness.set_leader(is_leader=False)
84 self.harness.charm.on.config_changed.emit()
85
86 # Assertions
87 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
88
89 def test_with_config(self) -> NoReturn:
90 "Test with mysql config"
91 self.initialize_mysql_config()
92 # Verifying status
93 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
94
95 def test_with_relations(
96 self,
97 ) -> NoReturn:
98 "Test with relations"
99 self.initialize_mysql_relation()
100 # Verifying status
101 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
102
103 def test_exception_mysql_relation_and_config(
104 self,
105 ) -> NoReturn:
106 "Test with relations and config. Must throw exception"
107 self.initialize_mysql_config()
108 self.initialize_mysql_relation()
109 # Verifying status
110 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
111
112 def initialize_mysql_config(self):
113 self.harness.update_config(
114 {
115 "mysql_host": "mysql",
116 "mysql_port": 3306,
117 "mysql_root_password": "manopw",
118 }
119 )
120
121 def initialize_mysql_relation(self):
122 relation_id = self.harness.add_relation("db", "mysql")
123 self.harness.add_relation_unit(relation_id, "mysql/0")
124 self.harness.update_relation_data(
125 relation_id,
126 "mysql/0",
127 {
128 "host": "mysql",
129 "port": 3306,
130 "user": "mano",
131 "password": "manopw",
132 "root_password": "rootmanopw",
133 },
134 )
135
136
137 if __name__ == "__main__":
138 unittest.main()
139
140
141 # class TestCharm(unittest.TestCase):
142 # """Prometheus Charm unit tests."""
143
144 # def setUp(self) -> NoReturn:
145 # """Test setup"""
146 # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
147 # self.harness = Harness(KeystoneCharm)
148 # self.harness.set_leader(is_leader=True)
149 # self.harness.begin()
150 # self.config = {
151 # "enable_ng_ro": True,
152 # "database_commonkey": "commonkey",
153 # "log_level": "INFO",
154 # "vim_database": "db_name",
155 # "ro_database": "ro_db_name",
156 # "openmano_tenant": "mano",
157 # }
158
159 # def test_config_changed_no_relations(
160 # self,
161 # ) -> NoReturn:
162 # """Test ingress resources without HTTP."""
163
164 # self.harness.charm.on.config_changed.emit()
165
166 # # Assertions
167 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
168 # self.assertTrue(
169 # all(
170 # relation in self.harness.charm.unit.status.message
171 # for relation in ["mongodb", "kafka"]
172 # )
173 # )
174
175 # # Disable ng-ro
176 # self.harness.update_config({"enable_ng_ro": False})
177 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
178 # self.assertTrue(
179 # all(
180 # relation in self.harness.charm.unit.status.message
181 # for relation in ["mysql"]
182 # )
183 # )
184
185 # def test_config_changed_non_leader(
186 # self,
187 # ) -> NoReturn:
188 # """Test ingress resources without HTTP."""
189 # self.harness.set_leader(is_leader=False)
190 # self.harness.charm.on.config_changed.emit()
191
192 # # Assertions
193 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
194
195 # def test_with_relations_ng(
196 # self,
197 # ) -> NoReturn:
198 # "Test with relations (ng-ro)"
199
200 # # Initializing the kafka relation
201 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
202 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
203 # self.harness.update_relation_data(
204 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
205 # )
206
207 # # Initializing the mongo relation
208 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
209 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
210 # self.harness.update_relation_data(
211 # mongodb_relation_id,
212 # "mongodb/0",
213 # {"connection_string": "mongodb://mongo:27017"},
214 # )
215
216 # self.harness.charm.on.config_changed.emit()
217
218 # # Verifying status
219 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
220
221
222 # if __name__ == "__main__":
223 # unittest.main()