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 NbiCharm
29 from ops
.model
import ActiveStatus
, BlockedStatus
30 from ops
.testing
import Harness
33 class TestCharm(unittest
.TestCase
):
34 """Prometheus Charm unit tests."""
36 def setUp(self
) -> NoReturn
:
38 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
39 self
.harness
= Harness(NbiCharm
)
40 self
.harness
.set_leader(is_leader
=True)
44 "auth_backend": "internal",
45 "database_commonkey": "key",
48 "ingress_whitelist_source_range": "",
49 "tls_secret_name": "",
50 "site_url": "https://nbi.192.168.100.100.xip.io",
52 self
.harness
.update_config(self
.config
)
54 def test_config_changed_no_relations(
57 """Test ingress resources without HTTP."""
59 self
.harness
.charm
.on
.config_changed
.emit()
62 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
65 relation
in self
.harness
.charm
.unit
.status
.message
66 for relation
in ["mongodb", "kafka", "prometheus"]
70 def test_config_changed_non_leader(
73 """Test ingress resources without HTTP."""
74 self
.harness
.set_leader(is_leader
=False)
75 self
.harness
.charm
.on
.config_changed
.emit()
78 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
80 def test_with_relations_internal(
83 "Test with relations (internal)"
84 self
.initialize_kafka_relation()
85 self
.initialize_mongo_relation()
86 self
.initialize_prometheus_relation()
88 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
90 def test_with_relations_keystone_missing(
93 "Test with relations (keystone)"
94 self
.harness
.update_config({"auth_backend": "keystone"})
95 self
.initialize_kafka_relation()
96 self
.initialize_mongo_relation()
97 self
.initialize_prometheus_relation()
99 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
100 self
.assertTrue("keystone" in self
.harness
.charm
.unit
.status
.message
)
102 def test_with_relations_keystone(
105 "Test with relations (keystone)"
106 self
.harness
.update_config({"auth_backend": "keystone"})
107 self
.initialize_kafka_relation()
108 self
.initialize_mongo_relation()
109 self
.initialize_prometheus_relation()
110 self
.initialize_keystone_relation()
112 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
114 def initialize_kafka_relation(self
):
115 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
116 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
117 self
.harness
.update_relation_data(
118 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
121 def initialize_mongo_relation(self
):
122 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
123 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
124 self
.harness
.update_relation_data(
127 {"connection_string": "mongodb://mongo:27017"},
130 def initialize_keystone_relation(self
):
131 keystone_relation_id
= self
.harness
.add_relation("keystone", "keystone")
132 self
.harness
.add_relation_unit(keystone_relation_id
, "keystone/0")
133 self
.harness
.update_relation_data(
134 keystone_relation_id
,
139 "user_domain_name": "ud",
140 "project_domain_name": "pd",
144 "keystone_db_password": "something",
145 "region_id": "something",
146 "admin_username": "something",
147 "admin_password": "something",
148 "admin_project_name": "something",
152 def initialize_prometheus_relation(self
):
153 prometheus_relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
154 self
.harness
.add_relation_unit(prometheus_relation_id
, "prometheus/0")
155 self
.harness
.update_relation_data(
156 prometheus_relation_id
,
158 {"hostname": "prometheus", "port": 9090},
162 if __name__
== "__main__":
166 # class TestCharm(unittest.TestCase):
167 # """Prometheus Charm unit tests."""
169 # def setUp(self) -> NoReturn:
171 # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
172 # self.harness = Harness(NbiCharm)
173 # self.harness.set_leader(is_leader=True)
174 # self.harness.begin()
176 # "enable_ng_ro": True,
177 # "database_commonkey": "commonkey",
178 # "log_level": "INFO",
179 # "vim_database": "db_name",
180 # "ro_database": "ro_db_name",
181 # "openmano_tenant": "mano",
184 # def test_config_changed_no_relations(
187 # """Test ingress resources without HTTP."""
189 # self.harness.charm.on.config_changed.emit()
192 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
195 # relation in self.harness.charm.unit.status.message
196 # for relation in ["mongodb", "kafka"]
201 # self.harness.update_config({"enable_ng_ro": False})
202 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
205 # relation in self.harness.charm.unit.status.message
206 # for relation in ["mysql"]
210 # def test_config_changed_non_leader(
213 # """Test ingress resources without HTTP."""
214 # self.harness.set_leader(is_leader=False)
215 # self.harness.charm.on.config_changed.emit()
218 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
220 # def test_with_relations_ng(
223 # "Test with relations (ng-ro)"
225 # # Initializing the kafka relation
226 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
227 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
228 # self.harness.update_relation_data(
229 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
232 # # Initializing the mongo relation
233 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
234 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
235 # self.harness.update_relation_data(
236 # mongodb_relation_id,
238 # {"connection_string": "mongodb://mongo:27017"},
241 # self.harness.charm.on.config_changed.emit()
244 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
247 # if __name__ == "__main__":