2b4ea0fef3cd081a202b1ea19e1d63e039e6ff6f
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",
51 "cluster_issuer": "vault-issuer",
53 self
.harness
.update_config(self
.config
)
55 def test_config_changed_no_relations(
58 """Test ingress resources without HTTP."""
60 self
.harness
.charm
.on
.config_changed
.emit()
63 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
66 relation
in self
.harness
.charm
.unit
.status
.message
67 for relation
in ["mongodb", "kafka", "prometheus"]
71 def test_config_changed_non_leader(
74 """Test ingress resources without HTTP."""
75 self
.harness
.set_leader(is_leader
=False)
76 self
.harness
.charm
.on
.config_changed
.emit()
79 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
81 def test_with_relations_internal(
84 "Test with relations (internal)"
85 self
.initialize_kafka_relation()
86 self
.initialize_mongo_relation()
87 self
.initialize_prometheus_relation()
89 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
91 def test_with_relations_keystone_missing(
94 "Test with relations (keystone)"
95 self
.harness
.update_config({"auth_backend": "keystone"})
96 self
.initialize_kafka_relation()
97 self
.initialize_mongo_relation()
98 self
.initialize_prometheus_relation()
100 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
101 self
.assertTrue("keystone" in self
.harness
.charm
.unit
.status
.message
)
103 def test_with_relations_keystone(
106 "Test with relations (keystone)"
107 self
.harness
.update_config({"auth_backend": "keystone"})
108 self
.initialize_kafka_relation()
109 self
.initialize_mongo_relation()
110 self
.initialize_prometheus_relation()
111 self
.initialize_keystone_relation()
113 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
115 def initialize_kafka_relation(self
):
116 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
117 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
118 self
.harness
.update_relation_data(
119 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
122 def initialize_mongo_relation(self
):
123 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
124 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
125 self
.harness
.update_relation_data(
128 {"connection_string": "mongodb://mongo:27017"},
131 def initialize_keystone_relation(self
):
132 keystone_relation_id
= self
.harness
.add_relation("keystone", "keystone")
133 self
.harness
.add_relation_unit(keystone_relation_id
, "keystone/0")
134 self
.harness
.update_relation_data(
135 keystone_relation_id
,
140 "user_domain_name": "ud",
141 "project_domain_name": "pd",
145 "keystone_db_password": "something",
146 "region_id": "something",
147 "admin_username": "something",
148 "admin_password": "something",
149 "admin_project_name": "something",
153 def initialize_prometheus_relation(self
):
154 prometheus_relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
155 self
.harness
.add_relation_unit(prometheus_relation_id
, "prometheus/0")
156 self
.harness
.update_relation_data(
157 prometheus_relation_id
,
159 {"hostname": "prometheus", "port": 9090},
163 if __name__
== "__main__":
167 # class TestCharm(unittest.TestCase):
168 # """Prometheus Charm unit tests."""
170 # def setUp(self) -> NoReturn:
172 # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
173 # self.harness = Harness(NbiCharm)
174 # self.harness.set_leader(is_leader=True)
175 # self.harness.begin()
177 # "enable_ng_ro": True,
178 # "database_commonkey": "commonkey",
179 # "log_level": "INFO",
180 # "vim_database": "db_name",
181 # "ro_database": "ro_db_name",
182 # "openmano_tenant": "mano",
185 # def test_config_changed_no_relations(
188 # """Test ingress resources without HTTP."""
190 # self.harness.charm.on.config_changed.emit()
193 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
196 # relation in self.harness.charm.unit.status.message
197 # for relation in ["mongodb", "kafka"]
202 # self.harness.update_config({"enable_ng_ro": False})
203 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
206 # relation in self.harness.charm.unit.status.message
207 # for relation in ["mysql"]
211 # def test_config_changed_non_leader(
214 # """Test ingress resources without HTTP."""
215 # self.harness.set_leader(is_leader=False)
216 # self.harness.charm.on.config_changed.emit()
219 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
221 # def test_with_relations_ng(
224 # "Test with relations (ng-ro)"
226 # # Initializing the kafka relation
227 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
228 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
229 # self.harness.update_relation_data(
230 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
233 # # Initializing the mongo relation
234 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
235 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
236 # self.harness.update_relation_data(
237 # mongodb_relation_id,
239 # {"connection_string": "mongodb://mongo:27017"},
242 # self.harness.charm.on.config_changed.emit()
245 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
248 # if __name__ == "__main__":