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({"mysql_uri": "mysql://root:manopw@mysql:3306/"})
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(
125 "password": "manopw",
126 "root_password": "rootmanopw",
131 if __name__
== "__main__":
135 # class TestCharm(unittest.TestCase):
136 # """Prometheus Charm unit tests."""
138 # def setUp(self) -> NoReturn:
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()
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",
153 # def test_config_changed_no_relations(
156 # """Test ingress resources without HTTP."""
158 # self.harness.charm.on.config_changed.emit()
161 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
164 # relation in self.harness.charm.unit.status.message
165 # for relation in ["mongodb", "kafka"]
170 # self.harness.update_config({"enable_ng_ro": False})
171 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
174 # relation in self.harness.charm.unit.status.message
175 # for relation in ["mysql"]
179 # def test_config_changed_non_leader(
182 # """Test ingress resources without HTTP."""
183 # self.harness.set_leader(is_leader=False)
184 # self.harness.charm.on.config_changed.emit()
187 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
189 # def test_with_relations_ng(
192 # "Test with relations (ng-ro)"
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}
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,
207 # {"connection_string": "mongodb://mongo:27017"},
210 # self.harness.charm.on.config_changed.emit()
213 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
216 # if __name__ == "__main__":