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",
75 "vim_database": "db_name",
76 "ro_database": "ro_db_name",
77 "openmano_tenant": "mano",
78 "certificates": f
"cert1:{certificate_pem}",
80 self
.harness
.update_config(self
.config
)
82 def test_config_changed_no_relations(
85 """Test ingress resources without HTTP."""
87 self
.harness
.charm
.on
.config_changed
.emit()
90 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
93 relation
in self
.harness
.charm
.unit
.status
.message
94 for relation
in ["mongodb", "kafka"]
99 self
.harness
.update_config({"enable_ng_ro": False})
100 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
103 relation
in self
.harness
.charm
.unit
.status
.message
104 for relation
in ["mysql"]
108 def test_config_changed_non_leader(
111 """Test ingress resources without HTTP."""
112 self
.harness
.set_leader(is_leader
=False)
113 self
.harness
.charm
.on
.config_changed
.emit()
116 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
118 def test_with_relations_ng(
121 "Test with relations (ng-ro)"
123 # Initializing the kafka relation
124 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
125 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
126 self
.harness
.update_relation_data(
127 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
130 # Initializing the mongo relation
131 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
132 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
133 self
.harness
.update_relation_data(
136 {"connection_string": "mongodb://mongo:27017"},
140 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
143 if __name__
== "__main__":
146 # class TestCharm(unittest.TestCase):
147 # """RO Charm unit tests."""
149 # def setUp(self) -> NoReturn:
151 # self.harness = Harness(RoCharm)
152 # self.harness.set_leader(is_leader=True)
153 # self.harness.begin()
155 # def test_on_start_without_relations_ng_ro(self) -> NoReturn:
156 # """Test installation without any relation."""
157 # self.harness.charm.on.start.emit()
160 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
162 # # Verifying status message
163 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
165 # self.harness.charm.unit.status.message.startswith("Waiting for ")
167 # self.assertIn("kafka", self.harness.charm.unit.status.message)
168 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
169 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
171 # def test_on_start_without_relations_no_ng_ro(self) -> NoReturn:
172 # """Test installation without any relation."""
173 # self.harness.update_config({"enable_ng_ro": False})
175 # self.harness.charm.on.start.emit()
178 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
180 # # Verifying status message
181 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
183 # self.harness.charm.unit.status.message.startswith("Waiting for ")
185 # self.assertIn("mysql", self.harness.charm.unit.status.message)
186 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
188 # def test_on_start_with_relations_ng_ro(self) -> NoReturn:
189 # """Test deployment with NG-RO."""
190 # expected_result = {
195 # "imageDetails": self.harness.charm.image.fetch(),
196 # "imagePullPolicy": "Always",
200 # "containerPort": 9090,
205 # "OSMRO_LOG_LEVEL": "INFO",
206 # "OSMRO_MESSAGE_DRIVER": "kafka",
207 # "OSMRO_MESSAGE_HOST": "kafka",
208 # "OSMRO_MESSAGE_PORT": "9090",
209 # "OSMRO_DATABASE_DRIVER": "mongo",
210 # "OSMRO_DATABASE_URI": "mongodb://mongo",
211 # "OSMRO_DATABASE_COMMONKEY": "osm",
215 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
216 # "initialDelaySeconds": 60,
217 # "timeoutSeconds": 5,
219 # "readinessProbe": {
221 # "path": "/openmano/tenants",
224 # "periodSeconds": 10,
225 # "timeoutSeconds": 5,
226 # "successThreshold": 1,
227 # "failureThreshold": 3,
231 # "path": "/openmano/tenants",
234 # "initialDelaySeconds": 600,
235 # "periodSeconds": 10,
236 # "timeoutSeconds": 5,
237 # "successThreshold": 1,
238 # "failureThreshold": 3,
243 # "kubernetesResources": {"ingressResources": []},
246 # self.harness.charm.on.start.emit()
248 # # Initializing the kafka relation
249 # relation_id = self.harness.add_relation("kafka", "kafka")
250 # self.harness.add_relation_unit(relation_id, "kafka/0")
251 # self.harness.update_relation_data(
260 # # Initializing the mongodb relation
261 # relation_id = self.harness.add_relation("mongodb", "mongodb")
262 # self.harness.add_relation_unit(relation_id, "mongodb/0")
263 # self.harness.update_relation_data(
267 # "connection_string": "mongodb://mongo",
272 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
274 # pod_spec, _ = self.harness.get_pod_spec()
276 # self.assertDictEqual(expected_result, pod_spec)
278 # def test_on_start_with_relations_no_ng_ro(self) -> NoReturn:
279 # """Test deployment with old RO."""
280 # self.harness.update_config({"enable_ng_ro": False})
282 # expected_result = {
287 # "imageDetails": self.harness.charm.image.fetch(),
288 # "imagePullPolicy": "Always",
292 # "containerPort": 9090,
297 # "OSMRO_LOG_LEVEL": "INFO",
298 # "RO_DB_HOST": "mysql",
299 # "RO_DB_OVIM_HOST": "mysql",
300 # "RO_DB_PORT": 3306,
301 # "RO_DB_OVIM_PORT": 3306,
302 # "RO_DB_USER": "mano",
303 # "RO_DB_OVIM_USER": "mano",
304 # "RO_DB_PASSWORD": "manopw",
305 # "RO_DB_OVIM_PASSWORD": "manopw",
306 # "RO_DB_ROOT_PASSWORD": "rootmanopw",
307 # "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw",
308 # "RO_DB_NAME": "mano_db",
309 # "RO_DB_OVIM_NAME": "mano_vim_db",
310 # "OPENMANO_TENANT": "osm",
314 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
315 # "initialDelaySeconds": 60,
316 # "timeoutSeconds": 5,
318 # "readinessProbe": {
320 # "path": "/openmano/tenants",
323 # "periodSeconds": 10,
324 # "timeoutSeconds": 5,
325 # "successThreshold": 1,
326 # "failureThreshold": 3,
330 # "path": "/openmano/tenants",
333 # "initialDelaySeconds": 600,
334 # "periodSeconds": 10,
335 # "timeoutSeconds": 5,
336 # "successThreshold": 1,
337 # "failureThreshold": 3,
342 # "kubernetesResources": {"ingressResources": []},
345 # self.harness.charm.on.start.emit()
347 # # Initializing the mysql relation
348 # relation_id = self.harness.add_relation("mysql", "mysql")
349 # self.harness.add_relation_unit(relation_id, "mysql/0")
350 # self.harness.update_relation_data(
357 # "password": "manopw",
358 # "root_password": "rootmanopw",
363 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
365 # pod_spec, _ = self.harness.get_pod_spec()
367 # self.assertDictEqual(expected_result, pod_spec)
369 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
370 # """Test to see if kafka relation is updated."""
371 # self.harness.charm.on.start.emit()
373 # relation_id = self.harness.add_relation("kafka", "kafka")
374 # self.harness.add_relation_unit(relation_id, "kafka/0")
375 # self.harness.update_relation_data(
385 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
387 # # Verifying status message
388 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
390 # self.harness.charm.unit.status.message.startswith("Waiting for ")
392 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
393 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
395 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
396 # """Test to see if mongodb relation is updated."""
397 # self.harness.charm.on.start.emit()
399 # relation_id = self.harness.add_relation("mongodb", "mongodb")
400 # self.harness.add_relation_unit(relation_id, "mongodb/0")
401 # self.harness.update_relation_data(
405 # "connection_string": "mongodb://mongo",
410 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
412 # # Verifying status message
413 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
415 # self.harness.charm.unit.status.message.startswith("Waiting for ")
417 # self.assertIn("kafka", self.harness.charm.unit.status.message)
418 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
420 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
421 # """Test to see if mysql relation is updated."""
422 # self.harness.charm.on.start.emit()
424 # relation_id = self.harness.add_relation("mysql", "mysql")
425 # self.harness.add_relation_unit(relation_id, "mysql/0")
426 # self.harness.update_relation_data(
433 # "password": "manopw",
434 # "root_password": "rootmanopw",
439 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
441 # # Verifying status message
442 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
444 # self.harness.charm.unit.status.message.startswith("Waiting for ")
446 # self.assertIn("kafka", self.harness.charm.unit.status.message)
447 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
448 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
450 # def test_publish_ro_info(self) -> NoReturn:
451 # """Test to see if ro relation is updated."""
452 # expected_result = {
457 # self.harness.charm.on.start.emit()
459 # relation_id = self.harness.add_relation("ro", "lcm")
460 # self.harness.add_relation_unit(relation_id, "lcm/0")
461 # relation_data = self.harness.get_relation_data(relation_id, "ro")
463 # self.assertDictEqual(expected_result, relation_data)
466 if __name__
== "__main__":