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 def encode(content
: str):
33 return base64
.b64encode(content
.encode("ascii")).decode("utf-8")
36 certificate_pem
= encode(
38 -----BEGIN CERTIFICATE-----
39 MIIDazCCAlOgAwIBAgIUf1b0s3UKtrxHXH2rge7UaQyfJAMwDQYJKoZIhvcNAQEL
40 BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
41 GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMTAzMjIxNzEyMjdaFw0zMTAz
42 MjAxNzEyMjdaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
43 HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
44 AQUAA4IBDwAwggEKAoIBAQCgCfCBgYAN6ON0yHDXuW407rFtJVRf0u46Jrp0Dk7J
45 kkSZ1e7Kq14r7yFHazEBWv78oOdwBocvWrd8leLuf3bYGcHR65hRy6A/fbYm5Aje
46 cKpwlFwaqfR4BLelwJl79jZ2rJX738cCBVrIk1nAVdOxGrXV4MTWUaKR2c+uKKvc
47 OKRT+5VqCeP4N5FWeATZ/KqGu8uV9E9WhFgwIZyStemLyLaDbn5PmAQ6S9oeR5jJ
48 o2gEEp/lDKvsqOWs76KFumSKa9hQs5Dw2lj0mb1UoyYK1gYc4ubzVChJadv44AU8
49 MYtIjlFn1X1P+RjaKZNUIAGXkoLwYn6SizF6y6LiuFS9AgMBAAGjUzBRMB0GA1Ud
50 DgQWBBRl+/23CB+FXczeAZRQyYcfOdy9YDAfBgNVHSMEGDAWgBRl+/23CB+FXcze
51 AZRQyYcfOdy9YDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAd
52 dkeDym6lRN8kWFtfu3IyiLF8G8sn91qNbH3Yr4TuTBhgcjYyW6PgisSbrNgA9ysE
53 GoaF7ohb8GeVfCsQdK23+NpAlj/+DZ3OnGcxwXj1RUAz4yr9kanV1yuEtr1q2xJI
54 UaECWr8HZlwGBAKNTGx2EXT2/2aFzgULpDcxzTKD+MRpKpMUrWhf9ULvVrclvHWe
55 POLYhobUFuBHuo6rt5Rcq16j67zCX9EVTlAE3o2OECIWByK22sXdeOidYMpTkl4q
56 8FrOqjNsx5d+SBPJBv/pqtBm4bA47Vx1P8tbWOQ4bXS0UmXgwpeBOU/O/ot30+KS
58 -----END CERTIFICATE-----
63 class TestCharm(unittest
.TestCase
):
64 """Prometheus Charm unit tests."""
66 def setUp(self
) -> NoReturn
:
68 self
.harness
= Harness(RoCharm
)
69 self
.harness
.set_leader(is_leader
=True)
73 "database_commonkey": "commonkey",
76 "vim_database": "db_name",
77 "ro_database": "ro_db_name",
78 "openmano_tenant": "mano",
79 "certificates": f
"cert1:{certificate_pem}",
81 self
.harness
.update_config(self
.config
)
83 def test_config_changed_no_relations(
86 """Test ingress resources without HTTP."""
88 self
.harness
.charm
.on
.config_changed
.emit()
91 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
94 relation
in self
.harness
.charm
.unit
.status
.message
95 for relation
in ["mongodb", "kafka"]
100 self
.harness
.update_config({"enable_ng_ro": False})
101 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
104 relation
in self
.harness
.charm
.unit
.status
.message
105 for relation
in ["mysql"]
109 def test_config_changed_non_leader(
112 """Test ingress resources without HTTP."""
113 self
.harness
.set_leader(is_leader
=False)
114 self
.harness
.charm
.on
.config_changed
.emit()
117 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
119 def test_with_relations_and_mongodb_config_ng(
122 "Test with relations (ng-ro)"
124 # Initializing the kafka relation
125 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
126 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
127 self
.harness
.update_relation_data(
128 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
131 # Initializing the mongodb config
132 self
.harness
.update_config({"mongodb_uri": "mongodb://mongo:27017"})
135 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
137 def test_with_relations_ng(
140 "Test with relations (ng-ro)"
142 # Initializing the kafka relation
143 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
144 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
145 self
.harness
.update_relation_data(
146 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
149 # Initializing the mongo relation
150 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
151 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
152 self
.harness
.update_relation_data(
155 {"connection_string": "mongodb://mongo:27017"},
159 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
161 def test_ng_exception_mongodb_relation_and_config(
164 "Test NG-RO mongodb relation and config. Must fail"
165 # Initializing the mongo relation
166 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
167 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
168 self
.harness
.update_relation_data(
171 {"connection_string": "mongodb://mongo:27017"},
174 # Initializing the mongodb config
175 self
.harness
.update_config({"mongodb_uri": "mongodb://mongo:27017"})
178 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
181 if __name__
== "__main__":
184 # class TestCharm(unittest.TestCase):
185 # """RO Charm unit tests."""
187 # def setUp(self) -> NoReturn:
189 # self.harness = Harness(RoCharm)
190 # self.harness.set_leader(is_leader=True)
191 # self.harness.begin()
193 # def test_on_start_without_relations_ng_ro(self) -> NoReturn:
194 # """Test installation without any relation."""
195 # self.harness.charm.on.start.emit()
198 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
200 # # Verifying status message
201 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
203 # self.harness.charm.unit.status.message.startswith("Waiting for ")
205 # self.assertIn("kafka", self.harness.charm.unit.status.message)
206 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
207 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
209 # def test_on_start_without_relations_no_ng_ro(self) -> NoReturn:
210 # """Test installation without any relation."""
211 # self.harness.update_config({"enable_ng_ro": False})
213 # self.harness.charm.on.start.emit()
216 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
218 # # Verifying status message
219 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
221 # self.harness.charm.unit.status.message.startswith("Waiting for ")
223 # self.assertIn("mysql", self.harness.charm.unit.status.message)
224 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
226 # def test_on_start_with_relations_ng_ro(self) -> NoReturn:
227 # """Test deployment with NG-RO."""
228 # expected_result = {
233 # "imageDetails": self.harness.charm.image.fetch(),
234 # "imagePullPolicy": "Always",
238 # "containerPort": 9090,
243 # "OSMRO_LOG_LEVEL": "INFO",
244 # "OSMRO_MESSAGE_DRIVER": "kafka",
245 # "OSMRO_MESSAGE_HOST": "kafka",
246 # "OSMRO_MESSAGE_PORT": "9090",
247 # "OSMRO_DATABASE_DRIVER": "mongo",
248 # "OSMRO_DATABASE_URI": "mongodb://mongo",
249 # "OSMRO_DATABASE_COMMONKEY": "osm",
253 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
254 # "initialDelaySeconds": 60,
255 # "timeoutSeconds": 5,
257 # "readinessProbe": {
259 # "path": "/openmano/tenants",
262 # "periodSeconds": 10,
263 # "timeoutSeconds": 5,
264 # "successThreshold": 1,
265 # "failureThreshold": 3,
269 # "path": "/openmano/tenants",
272 # "initialDelaySeconds": 600,
273 # "periodSeconds": 10,
274 # "timeoutSeconds": 5,
275 # "successThreshold": 1,
276 # "failureThreshold": 3,
281 # "kubernetesResources": {"ingressResources": []},
284 # self.harness.charm.on.start.emit()
286 # # Initializing the kafka relation
287 # relation_id = self.harness.add_relation("kafka", "kafka")
288 # self.harness.add_relation_unit(relation_id, "kafka/0")
289 # self.harness.update_relation_data(
298 # # Initializing the mongodb relation
299 # relation_id = self.harness.add_relation("mongodb", "mongodb")
300 # self.harness.add_relation_unit(relation_id, "mongodb/0")
301 # self.harness.update_relation_data(
305 # "connection_string": "mongodb://mongo",
310 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
312 # pod_spec, _ = self.harness.get_pod_spec()
314 # self.assertDictEqual(expected_result, pod_spec)
316 # def test_on_start_with_relations_no_ng_ro(self) -> NoReturn:
317 # """Test deployment with old RO."""
318 # self.harness.update_config({"enable_ng_ro": False})
320 # expected_result = {
325 # "imageDetails": self.harness.charm.image.fetch(),
326 # "imagePullPolicy": "Always",
330 # "containerPort": 9090,
335 # "OSMRO_LOG_LEVEL": "INFO",
336 # "RO_DB_HOST": "mysql",
337 # "RO_DB_OVIM_HOST": "mysql",
338 # "RO_DB_PORT": 3306,
339 # "RO_DB_OVIM_PORT": 3306,
340 # "RO_DB_USER": "mano",
341 # "RO_DB_OVIM_USER": "mano",
342 # "RO_DB_PASSWORD": "manopw",
343 # "RO_DB_OVIM_PASSWORD": "manopw",
344 # "RO_DB_ROOT_PASSWORD": "rootmanopw",
345 # "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw",
346 # "RO_DB_NAME": "mano_db",
347 # "RO_DB_OVIM_NAME": "mano_vim_db",
348 # "OPENMANO_TENANT": "osm",
352 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
353 # "initialDelaySeconds": 60,
354 # "timeoutSeconds": 5,
356 # "readinessProbe": {
358 # "path": "/openmano/tenants",
361 # "periodSeconds": 10,
362 # "timeoutSeconds": 5,
363 # "successThreshold": 1,
364 # "failureThreshold": 3,
368 # "path": "/openmano/tenants",
371 # "initialDelaySeconds": 600,
372 # "periodSeconds": 10,
373 # "timeoutSeconds": 5,
374 # "successThreshold": 1,
375 # "failureThreshold": 3,
380 # "kubernetesResources": {"ingressResources": []},
383 # self.harness.charm.on.start.emit()
385 # # Initializing the mysql relation
386 # relation_id = self.harness.add_relation("mysql", "mysql")
387 # self.harness.add_relation_unit(relation_id, "mysql/0")
388 # self.harness.update_relation_data(
395 # "password": "manopw",
396 # "root_password": "rootmanopw",
401 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
403 # pod_spec, _ = self.harness.get_pod_spec()
405 # self.assertDictEqual(expected_result, pod_spec)
407 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
408 # """Test to see if kafka relation is updated."""
409 # self.harness.charm.on.start.emit()
411 # relation_id = self.harness.add_relation("kafka", "kafka")
412 # self.harness.add_relation_unit(relation_id, "kafka/0")
413 # self.harness.update_relation_data(
423 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
425 # # Verifying status message
426 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
428 # self.harness.charm.unit.status.message.startswith("Waiting for ")
430 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
431 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
433 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
434 # """Test to see if mongodb relation is updated."""
435 # self.harness.charm.on.start.emit()
437 # relation_id = self.harness.add_relation("mongodb", "mongodb")
438 # self.harness.add_relation_unit(relation_id, "mongodb/0")
439 # self.harness.update_relation_data(
443 # "connection_string": "mongodb://mongo",
448 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
450 # # Verifying status message
451 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
453 # self.harness.charm.unit.status.message.startswith("Waiting for ")
455 # self.assertIn("kafka", self.harness.charm.unit.status.message)
456 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
458 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
459 # """Test to see if mysql relation is updated."""
460 # self.harness.charm.on.start.emit()
462 # relation_id = self.harness.add_relation("mysql", "mysql")
463 # self.harness.add_relation_unit(relation_id, "mysql/0")
464 # self.harness.update_relation_data(
471 # "password": "manopw",
472 # "root_password": "rootmanopw",
477 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
479 # # Verifying status message
480 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
482 # self.harness.charm.unit.status.message.startswith("Waiting for ")
484 # self.assertIn("kafka", self.harness.charm.unit.status.message)
485 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
486 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
488 # def test_publish_ro_info(self) -> NoReturn:
489 # """Test to see if ro relation is updated."""
490 # expected_result = {
495 # self.harness.charm.on.start.emit()
497 # relation_id = self.harness.add_relation("ro", "lcm")
498 # self.harness.add_relation_unit(relation_id, "lcm/0")
499 # relation_data = self.harness.get_relation_data(relation_id, "ro")
501 # self.assertDictEqual(expected_result, relation_data)
504 if __name__
== "__main__":