Add secret-management in Charmed OSM
[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({"mysql_uri": "mysql://root:manopw@mysql:3306/"})
114
115 def initialize_mysql_relation(self):
116 relation_id = self.harness.add_relation("db", "mysql")
117 self.harness.add_relation_unit(relation_id, "mysql/0")
118 self.harness.update_relation_data(
119 relation_id,
120 "mysql/0",
121 {
122 "host": "mysql",
123 "port": 3306,
124 "user": "mano",
125 "password": "manopw",
126 "root_password": "rootmanopw",
127 },
128 )
129
130
131 if __name__ == "__main__":
132 unittest.main()
133
134
135 # class TestCharm(unittest.TestCase):
136 # """Prometheus Charm unit tests."""
137
138 # def setUp(self) -> NoReturn:
139 # """Test setup"""
140 # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
141 # self.harness = Harness(KeystoneCharm)
142 # self.harness.set_leader(is_leader=True)
143 # self.harness.begin()
144 # self.config = {
145 # "enable_ng_ro": True,
146 # "database_commonkey": "commonkey",
147 # "log_level": "INFO",
148 # "vim_database": "db_name",
149 # "ro_database": "ro_db_name",
150 # "openmano_tenant": "mano",
151 # }
152
153 # def test_config_changed_no_relations(
154 # self,
155 # ) -> NoReturn:
156 # """Test ingress resources without HTTP."""
157
158 # self.harness.charm.on.config_changed.emit()
159
160 # # Assertions
161 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
162 # self.assertTrue(
163 # all(
164 # relation in self.harness.charm.unit.status.message
165 # for relation in ["mongodb", "kafka"]
166 # )
167 # )
168
169 # # Disable ng-ro
170 # self.harness.update_config({"enable_ng_ro": False})
171 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
172 # self.assertTrue(
173 # all(
174 # relation in self.harness.charm.unit.status.message
175 # for relation in ["mysql"]
176 # )
177 # )
178
179 # def test_config_changed_non_leader(
180 # self,
181 # ) -> NoReturn:
182 # """Test ingress resources without HTTP."""
183 # self.harness.set_leader(is_leader=False)
184 # self.harness.charm.on.config_changed.emit()
185
186 # # Assertions
187 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
188
189 # def test_with_relations_ng(
190 # self,
191 # ) -> NoReturn:
192 # "Test with relations (ng-ro)"
193
194 # # Initializing the kafka relation
195 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
196 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
197 # self.harness.update_relation_data(
198 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
199 # )
200
201 # # Initializing the mongo relation
202 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
203 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
204 # self.harness.update_relation_data(
205 # mongodb_relation_id,
206 # "mongodb/0",
207 # {"connection_string": "mongodb://mongo:27017"},
208 # )
209
210 # self.harness.charm.on.config_changed.emit()
211
212 # # Verifying status
213 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
214
215
216 # if __name__ == "__main__":
217 # unittest.main()