Major improvement in OSM charms
[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 from ops.model import ActiveStatus, BlockedStatus
27 from ops.testing import Harness
28
29 from charm import KeystoneCharm
30
31
32 class TestCharm(unittest.TestCase):
33 """Keystone Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
38 self.harness = Harness(KeystoneCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
41 self.config = {
42 "region_id": "str",
43 "keystone_db_password": "str",
44 "admin_username": "str",
45 "admin_password": "str",
46 "admin_project": "str",
47 "service_username": "str",
48 "service_password": "str",
49 "service_project": "str",
50 "user_domain_name": "str",
51 "project_domain_name": "str",
52 "token_expiration": 10,
53 "max_file_size": 1,
54 "site_url": "http://keystone.com",
55 "ldap_enabled": False,
56 }
57 self.harness.update_config(self.config)
58
59 def test_config_changed_no_relations(
60 self,
61 ) -> NoReturn:
62 """Test ingress resources without HTTP."""
63
64 self.harness.charm.on.config_changed.emit()
65
66 # Assertions
67 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
68 self.assertTrue(
69 all(
70 relation in self.harness.charm.unit.status.message
71 for relation in ["mysql"]
72 )
73 )
74
75 def test_config_changed_non_leader(
76 self,
77 ) -> NoReturn:
78 """Test ingress resources without HTTP."""
79 self.harness.set_leader(is_leader=False)
80 self.harness.charm.on.config_changed.emit()
81
82 # Assertions
83 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
84
85 def test_with_relations(
86 self,
87 ) -> NoReturn:
88 "Test with relations"
89 self.initialize_mysql_relation()
90 # Verifying status
91 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
92
93 def initialize_mysql_relation(self):
94 relation_id = self.harness.add_relation("db", "mysql")
95 self.harness.add_relation_unit(relation_id, "mysql/0")
96 self.harness.update_relation_data(
97 relation_id,
98 "mysql/0",
99 {
100 "host": "mysql",
101 "port": 3306,
102 "user": "mano",
103 "password": "manopw",
104 "root_password": "rootmanopw",
105 },
106 )
107
108
109 if __name__ == "__main__":
110 unittest.main()
111
112
113 # class TestCharm(unittest.TestCase):
114 # """Prometheus Charm unit tests."""
115
116 # def setUp(self) -> NoReturn:
117 # """Test setup"""
118 # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
119 # self.harness = Harness(KeystoneCharm)
120 # self.harness.set_leader(is_leader=True)
121 # self.harness.begin()
122 # self.config = {
123 # "enable_ng_ro": True,
124 # "database_commonkey": "commonkey",
125 # "log_level": "INFO",
126 # "vim_database": "db_name",
127 # "ro_database": "ro_db_name",
128 # "openmano_tenant": "mano",
129 # }
130
131 # def test_config_changed_no_relations(
132 # self,
133 # ) -> NoReturn:
134 # """Test ingress resources without HTTP."""
135
136 # self.harness.charm.on.config_changed.emit()
137
138 # # Assertions
139 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
140 # self.assertTrue(
141 # all(
142 # relation in self.harness.charm.unit.status.message
143 # for relation in ["mongodb", "kafka"]
144 # )
145 # )
146
147 # # Disable ng-ro
148 # self.harness.update_config({"enable_ng_ro": False})
149 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
150 # self.assertTrue(
151 # all(
152 # relation in self.harness.charm.unit.status.message
153 # for relation in ["mysql"]
154 # )
155 # )
156
157 # def test_config_changed_non_leader(
158 # self,
159 # ) -> NoReturn:
160 # """Test ingress resources without HTTP."""
161 # self.harness.set_leader(is_leader=False)
162 # self.harness.charm.on.config_changed.emit()
163
164 # # Assertions
165 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
166
167 # def test_with_relations_ng(
168 # self,
169 # ) -> NoReturn:
170 # "Test with relations (ng-ro)"
171
172 # # Initializing the kafka relation
173 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
174 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
175 # self.harness.update_relation_data(
176 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
177 # )
178
179 # # Initializing the mongo relation
180 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
181 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
182 # self.harness.update_relation_data(
183 # mongodb_relation_id,
184 # "mongodb/0",
185 # {"connection_string": "mongodb://mongo:27017"},
186 # )
187
188 # self.harness.charm.on.config_changed.emit()
189
190 # # Verifying status
191 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
192
193
194 # if __name__ == "__main__":
195 # unittest.main()