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
26 from ops
.model
import ActiveStatus
, BlockedStatus
27 from ops
.testing
import Harness
29 from charm
import NbiCharm
32 class TestCharm(unittest
.TestCase
):
33 """Prometheus Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
38 self
.harness
= Harness(NbiCharm
)
39 self
.harness
.set_leader(is_leader
=True)
43 "auth_backend": "internal",
44 "database_commonkey": "key",
47 "ingress_whitelist_source_range": "",
48 "tls_secret_name": "",
49 "site_url": "https://nbi.192.168.100.100.xip.io",
51 self
.harness
.update_config(self
.config
)
53 def test_config_changed_no_relations(
56 """Test ingress resources without HTTP."""
58 self
.harness
.charm
.on
.config_changed
.emit()
61 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
64 relation
in self
.harness
.charm
.unit
.status
.message
65 for relation
in ["mongodb", "kafka", "prometheus"]
69 def test_config_changed_non_leader(
72 """Test ingress resources without HTTP."""
73 self
.harness
.set_leader(is_leader
=False)
74 self
.harness
.charm
.on
.config_changed
.emit()
77 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
79 def test_with_relations_internal(
82 "Test with relations (internal)"
83 self
.initialize_kafka_relation()
84 self
.initialize_mongo_relation()
85 self
.initialize_prometheus_relation()
87 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
89 def test_with_relations_keystone_missing(
92 "Test with relations (keystone)"
93 self
.harness
.update_config({"auth_backend": "keystone"})
94 self
.initialize_kafka_relation()
95 self
.initialize_mongo_relation()
96 self
.initialize_prometheus_relation()
98 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
99 self
.assertTrue("keystone" in self
.harness
.charm
.unit
.status
.message
)
101 def test_with_relations_keystone(
104 "Test with relations (keystone)"
105 self
.harness
.update_config({"auth_backend": "keystone"})
106 self
.initialize_kafka_relation()
107 self
.initialize_mongo_relation()
108 self
.initialize_prometheus_relation()
109 self
.initialize_keystone_relation()
111 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
113 def initialize_kafka_relation(self
):
114 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
115 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
116 self
.harness
.update_relation_data(
117 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
120 def initialize_mongo_relation(self
):
121 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
122 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
123 self
.harness
.update_relation_data(
126 {"connection_string": "mongodb://mongo:27017"},
129 def initialize_keystone_relation(self
):
130 keystone_relation_id
= self
.harness
.add_relation("keystone", "keystone")
131 self
.harness
.add_relation_unit(keystone_relation_id
, "keystone/0")
132 self
.harness
.update_relation_data(
133 keystone_relation_id
,
138 "user_domain_name": "ud",
139 "project_domain_name": "pd",
143 "keystone_db_password": "something",
144 "region_id": "something",
145 "admin_username": "something",
146 "admin_password": "something",
147 "admin_project_name": "something",
151 def initialize_prometheus_relation(self
):
152 prometheus_relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
153 self
.harness
.add_relation_unit(prometheus_relation_id
, "prometheus/0")
154 self
.harness
.update_relation_data(
155 prometheus_relation_id
,
157 {"hostname": "prometheus", "port": 9090},
161 if __name__
== "__main__":
165 # class TestCharm(unittest.TestCase):
166 # """Prometheus Charm unit tests."""
168 # def setUp(self) -> NoReturn:
170 # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
171 # self.harness = Harness(NbiCharm)
172 # self.harness.set_leader(is_leader=True)
173 # self.harness.begin()
175 # "enable_ng_ro": True,
176 # "database_commonkey": "commonkey",
177 # "log_level": "INFO",
178 # "vim_database": "db_name",
179 # "ro_database": "ro_db_name",
180 # "openmano_tenant": "mano",
183 # def test_config_changed_no_relations(
186 # """Test ingress resources without HTTP."""
188 # self.harness.charm.on.config_changed.emit()
191 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
194 # relation in self.harness.charm.unit.status.message
195 # for relation in ["mongodb", "kafka"]
200 # self.harness.update_config({"enable_ng_ro": False})
201 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
204 # relation in self.harness.charm.unit.status.message
205 # for relation in ["mysql"]
209 # def test_config_changed_non_leader(
212 # """Test ingress resources without HTTP."""
213 # self.harness.set_leader(is_leader=False)
214 # self.harness.charm.on.config_changed.emit()
217 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
219 # def test_with_relations_ng(
222 # "Test with relations (ng-ro)"
224 # # Initializing the kafka relation
225 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
226 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
227 # self.harness.update_relation_data(
228 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
231 # # Initializing the mongo relation
232 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
233 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
234 # self.harness.update_relation_data(
235 # mongodb_relation_id,
237 # {"connection_string": "mongodb://mongo:27017"},
240 # self.harness.charm.on.config_changed.emit()
243 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
246 # if __name__ == "__main__":