2 # Copyright 2020 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
27 from charm
import RoCharm
28 from ops
.model
import ActiveStatus
, BlockedStatus
29 from ops
.testing
import Harness
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(RoCharm
)
39 self
.harness
.set_leader(is_leader
=True)
43 "database_commonkey": "commonkey",
45 "vim_database": "db_name",
46 "ro_database": "ro_db_name",
47 "openmano_tenant": "mano",
49 self
.harness
.update_config(self
.config
)
51 def test_config_changed_no_relations(
54 """Test ingress resources without HTTP."""
56 self
.harness
.charm
.on
.config_changed
.emit()
59 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
62 relation
in self
.harness
.charm
.unit
.status
.message
63 for relation
in ["mongodb", "kafka"]
68 self
.harness
.update_config({"enable_ng_ro": False})
69 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
72 relation
in self
.harness
.charm
.unit
.status
.message
73 for relation
in ["mysql"]
77 def test_config_changed_non_leader(
80 """Test ingress resources without HTTP."""
81 self
.harness
.set_leader(is_leader
=False)
82 self
.harness
.charm
.on
.config_changed
.emit()
85 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
87 def test_with_relations_ng(
90 "Test with relations (ng-ro)"
92 # Initializing the kafka relation
93 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
94 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
95 self
.harness
.update_relation_data(
96 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
99 # Initializing the mongo relation
100 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
101 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
102 self
.harness
.update_relation_data(
105 {"connection_string": "mongodb://mongo:27017"},
109 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
112 if __name__
== "__main__":
115 # class TestCharm(unittest.TestCase):
116 # """RO Charm unit tests."""
118 # def setUp(self) -> NoReturn:
120 # self.harness = Harness(RoCharm)
121 # self.harness.set_leader(is_leader=True)
122 # self.harness.begin()
124 # def test_on_start_without_relations_ng_ro(self) -> NoReturn:
125 # """Test installation without any relation."""
126 # self.harness.charm.on.start.emit()
129 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
131 # # Verifying status message
132 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
134 # self.harness.charm.unit.status.message.startswith("Waiting for ")
136 # self.assertIn("kafka", self.harness.charm.unit.status.message)
137 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
138 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
140 # def test_on_start_without_relations_no_ng_ro(self) -> NoReturn:
141 # """Test installation without any relation."""
142 # self.harness.update_config({"enable_ng_ro": False})
144 # self.harness.charm.on.start.emit()
147 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
149 # # Verifying status message
150 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
152 # self.harness.charm.unit.status.message.startswith("Waiting for ")
154 # self.assertIn("mysql", self.harness.charm.unit.status.message)
155 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
157 # def test_on_start_with_relations_ng_ro(self) -> NoReturn:
158 # """Test deployment with NG-RO."""
159 # expected_result = {
164 # "imageDetails": self.harness.charm.image.fetch(),
165 # "imagePullPolicy": "Always",
169 # "containerPort": 9090,
174 # "OSMRO_LOG_LEVEL": "INFO",
175 # "OSMRO_MESSAGE_DRIVER": "kafka",
176 # "OSMRO_MESSAGE_HOST": "kafka",
177 # "OSMRO_MESSAGE_PORT": "9090",
178 # "OSMRO_DATABASE_DRIVER": "mongo",
179 # "OSMRO_DATABASE_URI": "mongodb://mongo",
180 # "OSMRO_DATABASE_COMMONKEY": "osm",
184 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
185 # "initialDelaySeconds": 60,
186 # "timeoutSeconds": 5,
188 # "readinessProbe": {
190 # "path": "/openmano/tenants",
193 # "periodSeconds": 10,
194 # "timeoutSeconds": 5,
195 # "successThreshold": 1,
196 # "failureThreshold": 3,
200 # "path": "/openmano/tenants",
203 # "initialDelaySeconds": 600,
204 # "periodSeconds": 10,
205 # "timeoutSeconds": 5,
206 # "successThreshold": 1,
207 # "failureThreshold": 3,
212 # "kubernetesResources": {"ingressResources": []},
215 # self.harness.charm.on.start.emit()
217 # # Initializing the kafka relation
218 # relation_id = self.harness.add_relation("kafka", "kafka")
219 # self.harness.add_relation_unit(relation_id, "kafka/0")
220 # self.harness.update_relation_data(
229 # # Initializing the mongodb relation
230 # relation_id = self.harness.add_relation("mongodb", "mongodb")
231 # self.harness.add_relation_unit(relation_id, "mongodb/0")
232 # self.harness.update_relation_data(
236 # "connection_string": "mongodb://mongo",
241 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
243 # pod_spec, _ = self.harness.get_pod_spec()
245 # self.assertDictEqual(expected_result, pod_spec)
247 # def test_on_start_with_relations_no_ng_ro(self) -> NoReturn:
248 # """Test deployment with old RO."""
249 # self.harness.update_config({"enable_ng_ro": False})
251 # expected_result = {
256 # "imageDetails": self.harness.charm.image.fetch(),
257 # "imagePullPolicy": "Always",
261 # "containerPort": 9090,
266 # "OSMRO_LOG_LEVEL": "INFO",
267 # "RO_DB_HOST": "mysql",
268 # "RO_DB_OVIM_HOST": "mysql",
269 # "RO_DB_PORT": 3306,
270 # "RO_DB_OVIM_PORT": 3306,
271 # "RO_DB_USER": "mano",
272 # "RO_DB_OVIM_USER": "mano",
273 # "RO_DB_PASSWORD": "manopw",
274 # "RO_DB_OVIM_PASSWORD": "manopw",
275 # "RO_DB_ROOT_PASSWORD": "rootmanopw",
276 # "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw",
277 # "RO_DB_NAME": "mano_db",
278 # "RO_DB_OVIM_NAME": "mano_vim_db",
279 # "OPENMANO_TENANT": "osm",
283 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
284 # "initialDelaySeconds": 60,
285 # "timeoutSeconds": 5,
287 # "readinessProbe": {
289 # "path": "/openmano/tenants",
292 # "periodSeconds": 10,
293 # "timeoutSeconds": 5,
294 # "successThreshold": 1,
295 # "failureThreshold": 3,
299 # "path": "/openmano/tenants",
302 # "initialDelaySeconds": 600,
303 # "periodSeconds": 10,
304 # "timeoutSeconds": 5,
305 # "successThreshold": 1,
306 # "failureThreshold": 3,
311 # "kubernetesResources": {"ingressResources": []},
314 # self.harness.charm.on.start.emit()
316 # # Initializing the mysql relation
317 # relation_id = self.harness.add_relation("mysql", "mysql")
318 # self.harness.add_relation_unit(relation_id, "mysql/0")
319 # self.harness.update_relation_data(
326 # "password": "manopw",
327 # "root_password": "rootmanopw",
332 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
334 # pod_spec, _ = self.harness.get_pod_spec()
336 # self.assertDictEqual(expected_result, pod_spec)
338 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
339 # """Test to see if kafka relation is updated."""
340 # self.harness.charm.on.start.emit()
342 # relation_id = self.harness.add_relation("kafka", "kafka")
343 # self.harness.add_relation_unit(relation_id, "kafka/0")
344 # self.harness.update_relation_data(
354 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
356 # # Verifying status message
357 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
359 # self.harness.charm.unit.status.message.startswith("Waiting for ")
361 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
362 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
364 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
365 # """Test to see if mongodb relation is updated."""
366 # self.harness.charm.on.start.emit()
368 # relation_id = self.harness.add_relation("mongodb", "mongodb")
369 # self.harness.add_relation_unit(relation_id, "mongodb/0")
370 # self.harness.update_relation_data(
374 # "connection_string": "mongodb://mongo",
379 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
381 # # Verifying status message
382 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
384 # self.harness.charm.unit.status.message.startswith("Waiting for ")
386 # self.assertIn("kafka", self.harness.charm.unit.status.message)
387 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
389 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
390 # """Test to see if mysql relation is updated."""
391 # self.harness.charm.on.start.emit()
393 # relation_id = self.harness.add_relation("mysql", "mysql")
394 # self.harness.add_relation_unit(relation_id, "mysql/0")
395 # self.harness.update_relation_data(
402 # "password": "manopw",
403 # "root_password": "rootmanopw",
408 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
410 # # Verifying status message
411 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
413 # self.harness.charm.unit.status.message.startswith("Waiting for ")
415 # self.assertIn("kafka", self.harness.charm.unit.status.message)
416 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
417 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
419 # def test_publish_ro_info(self) -> NoReturn:
420 # """Test to see if ro relation is updated."""
421 # expected_result = {
426 # self.harness.charm.on.start.emit()
428 # relation_id = self.harness.add_relation("ro", "lcm")
429 # self.harness.add_relation_unit(relation_id, "lcm/0")
430 # relation_data = self.harness.get_relation_data(relation_id, "ro")
432 # self.assertDictEqual(expected_result, relation_data)
435 if __name__
== "__main__":