2 # Copyright 2021 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
24 from typing
import NoReturn
28 from charm
import KeystoneCharm
29 from ops
.model
import ActiveStatus
, BlockedStatus
30 from ops
.testing
import Harness
33 class TestCharm(unittest
.TestCase
):
34 """Keystone Charm unit tests."""
36 def setUp(self
) -> NoReturn
:
38 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
39 self
.harness
= Harness(KeystoneCharm
)
40 self
.harness
.set_leader(is_leader
=True)
44 "keystone_db_password": "str",
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,
58 "site_url": "http://keystone.com",
59 "ldap_enabled": False,
61 self
.harness
.update_config(self
.config
)
63 def test_config_changed_no_relations(
66 """Test ingress resources without HTTP."""
68 self
.harness
.charm
.on
.config_changed
.emit()
71 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
74 relation
in self
.harness
.charm
.unit
.status
.message
75 for relation
in ["mysql"]
79 def test_config_changed_non_leader(
82 """Test ingress resources without HTTP."""
83 self
.harness
.set_leader(is_leader
=False)
84 self
.harness
.charm
.on
.config_changed
.emit()
87 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
89 def test_with_config(self
) -> NoReturn
:
90 "Test with mysql config"
91 self
.initialize_mysql_config()
93 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
95 def test_with_relations(
99 self
.initialize_mysql_relation()
101 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
103 def test_exception_mysql_relation_and_config(
106 "Test with relations and config. Must throw exception"
107 self
.initialize_mysql_config()
108 self
.initialize_mysql_relation()
110 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
112 def initialize_mysql_config(self
):
113 self
.harness
.update_config(
115 "mysql_host": "mysql",
117 "mysql_root_password": "manopw",
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(
131 "password": "manopw",
132 "root_password": "rootmanopw",
137 if __name__
== "__main__":
141 # class TestCharm(unittest.TestCase):
142 # """Prometheus Charm unit tests."""
144 # def setUp(self) -> NoReturn:
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()
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",
159 # def test_config_changed_no_relations(
162 # """Test ingress resources without HTTP."""
164 # self.harness.charm.on.config_changed.emit()
167 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
170 # relation in self.harness.charm.unit.status.message
171 # for relation in ["mongodb", "kafka"]
176 # self.harness.update_config({"enable_ng_ro": False})
177 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
180 # relation in self.harness.charm.unit.status.message
181 # for relation in ["mysql"]
185 # def test_config_changed_non_leader(
188 # """Test ingress resources without HTTP."""
189 # self.harness.set_leader(is_leader=False)
190 # self.harness.charm.on.config_changed.emit()
193 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
195 # def test_with_relations_ng(
198 # "Test with relations (ng-ro)"
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}
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,
213 # {"connection_string": "mongodb://mongo:27017"},
216 # self.harness.charm.on.config_changed.emit()
219 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
222 # if __name__ == "__main__":