blob: 9cced4558f8193370cd04690b9805e6315349c79 [file] [log] [blame]
sousaeduccfacbb2020-11-04 21:44:01 +00001#!/usr/bin/env python3
2# Copyright 2020 Canonical Ltd.
3#
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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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
14# under the License.
15#
16# For those usages not covered by the Apache License, Version 2.0 please
17# contact: legal@canonical.com
18#
19# To get in touch with the maintainers, please contact:
20# osm-charmers@lists.launchpad.net
21##
22
David Garcia5d1ec6e2021-03-25 15:04:52 +010023import base64
sousaeduccfacbb2020-11-04 21:44:01 +000024from typing import NoReturn
25import unittest
sousaeduccfacbb2020-11-04 21:44:01 +000026
27from charm import RoCharm
David Garciac753dc52021-03-17 15:28:47 +010028from ops.model import ActiveStatus, BlockedStatus
29from ops.testing import Harness
sousaeduccfacbb2020-11-04 21:44:01 +000030
31
David Garcia5d1ec6e2021-03-25 15:04:52 +010032def encode(content: str):
33 return base64.b64encode(content.encode("ascii")).decode("utf-8")
34
35
36certificate_pem = encode(
37 """
38-----BEGIN CERTIFICATE-----
39MIIDazCCAlOgAwIBAgIUf1b0s3UKtrxHXH2rge7UaQyfJAMwDQYJKoZIhvcNAQEL
40BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
41GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMTAzMjIxNzEyMjdaFw0zMTAz
42MjAxNzEyMjdaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
43HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
44AQUAA4IBDwAwggEKAoIBAQCgCfCBgYAN6ON0yHDXuW407rFtJVRf0u46Jrp0Dk7J
45kkSZ1e7Kq14r7yFHazEBWv78oOdwBocvWrd8leLuf3bYGcHR65hRy6A/fbYm5Aje
46cKpwlFwaqfR4BLelwJl79jZ2rJX738cCBVrIk1nAVdOxGrXV4MTWUaKR2c+uKKvc
47OKRT+5VqCeP4N5FWeATZ/KqGu8uV9E9WhFgwIZyStemLyLaDbn5PmAQ6S9oeR5jJ
48o2gEEp/lDKvsqOWs76KFumSKa9hQs5Dw2lj0mb1UoyYK1gYc4ubzVChJadv44AU8
49MYtIjlFn1X1P+RjaKZNUIAGXkoLwYn6SizF6y6LiuFS9AgMBAAGjUzBRMB0GA1Ud
50DgQWBBRl+/23CB+FXczeAZRQyYcfOdy9YDAfBgNVHSMEGDAWgBRl+/23CB+FXcze
51AZRQyYcfOdy9YDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAd
52dkeDym6lRN8kWFtfu3IyiLF8G8sn91qNbH3Yr4TuTBhgcjYyW6PgisSbrNgA9ysE
53GoaF7ohb8GeVfCsQdK23+NpAlj/+DZ3OnGcxwXj1RUAz4yr9kanV1yuEtr1q2xJI
54UaECWr8HZlwGBAKNTGx2EXT2/2aFzgULpDcxzTKD+MRpKpMUrWhf9ULvVrclvHWe
55POLYhobUFuBHuo6rt5Rcq16j67zCX9EVTlAE3o2OECIWByK22sXdeOidYMpTkl4q
568FrOqjNsx5d+SBPJBv/pqtBm4bA47Vx1P8tbWOQ4bXS0UmXgwpeBOU/O/ot30+KS
57JnKEy+dYyvVBKg77sRHw
58-----END CERTIFICATE-----
59"""
60)
61
62
sousaeduccfacbb2020-11-04 21:44:01 +000063class TestCharm(unittest.TestCase):
David Garcia49379ce2021-02-24 13:48:22 +010064 """Prometheus Charm unit tests."""
sousaeduccfacbb2020-11-04 21:44:01 +000065
66 def setUp(self) -> NoReturn:
67 """Test setup"""
68 self.harness = Harness(RoCharm)
69 self.harness.set_leader(is_leader=True)
70 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010071 self.config = {
72 "enable_ng_ro": True,
73 "database_commonkey": "commonkey",
74 "log_level": "INFO",
75 "vim_database": "db_name",
76 "ro_database": "ro_db_name",
77 "openmano_tenant": "mano",
David Garcia5d1ec6e2021-03-25 15:04:52 +010078 "certificates": f"cert1:{certificate_pem}",
sousaeduccfacbb2020-11-04 21:44:01 +000079 }
David Garcia49379ce2021-02-24 13:48:22 +010080 self.harness.update_config(self.config)
sousaeduccfacbb2020-11-04 21:44:01 +000081
David Garcia49379ce2021-02-24 13:48:22 +010082 def test_config_changed_no_relations(
83 self,
84 ) -> NoReturn:
85 """Test ingress resources without HTTP."""
86
87 self.harness.charm.on.config_changed.emit()
88
89 # Assertions
90 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
91 self.assertTrue(
92 all(
93 relation in self.harness.charm.unit.status.message
94 for relation in ["mongodb", "kafka"]
95 )
96 )
97
98 # Disable ng-ro
99 self.harness.update_config({"enable_ng_ro": False})
100 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
101 self.assertTrue(
102 all(
103 relation in self.harness.charm.unit.status.message
104 for relation in ["mysql"]
105 )
106 )
107
108 def test_config_changed_non_leader(
109 self,
110 ) -> NoReturn:
111 """Test ingress resources without HTTP."""
112 self.harness.set_leader(is_leader=False)
113 self.harness.charm.on.config_changed.emit()
114
115 # Assertions
116 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
117
118 def test_with_relations_ng(
119 self,
120 ) -> NoReturn:
121 "Test with relations (ng-ro)"
sousaeduccfacbb2020-11-04 21:44:01 +0000122
123 # Initializing the kafka relation
David Garcia49379ce2021-02-24 13:48:22 +0100124 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
125 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
sousaeduccfacbb2020-11-04 21:44:01 +0000126 self.harness.update_relation_data(
David Garcia49379ce2021-02-24 13:48:22 +0100127 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
sousaeduccfacbb2020-11-04 21:44:01 +0000128 )
129
David Garcia49379ce2021-02-24 13:48:22 +0100130 # 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")
sousaeduccfacbb2020-11-04 21:44:01 +0000133 self.harness.update_relation_data(
David Garcia49379ce2021-02-24 13:48:22 +0100134 mongodb_relation_id,
sousaeduccfacbb2020-11-04 21:44:01 +0000135 "mongodb/0",
David Garcia49379ce2021-02-24 13:48:22 +0100136 {"connection_string": "mongodb://mongo:27017"},
sousaeduccfacbb2020-11-04 21:44:01 +0000137 )
138
139 # Verifying status
140 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
141
sousaeduccfacbb2020-11-04 21:44:01 +0000142
David Garcia49379ce2021-02-24 13:48:22 +0100143if __name__ == "__main__":
144 unittest.main()
sousaeduccfacbb2020-11-04 21:44:01 +0000145
David Garcia49379ce2021-02-24 13:48:22 +0100146# class TestCharm(unittest.TestCase):
147# """RO Charm unit tests."""
sousaeduccfacbb2020-11-04 21:44:01 +0000148
David Garcia49379ce2021-02-24 13:48:22 +0100149# def setUp(self) -> NoReturn:
150# """Test setup"""
151# self.harness = Harness(RoCharm)
152# self.harness.set_leader(is_leader=True)
153# self.harness.begin()
sousaeduccfacbb2020-11-04 21:44:01 +0000154
David Garcia49379ce2021-02-24 13:48:22 +0100155# def test_on_start_without_relations_ng_ro(self) -> NoReturn:
156# """Test installation without any relation."""
157# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000158
David Garcia49379ce2021-02-24 13:48:22 +0100159# # Verifying status
160# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000161
David Garcia49379ce2021-02-24 13:48:22 +0100162# # Verifying status message
163# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
164# self.assertTrue(
165# self.harness.charm.unit.status.message.startswith("Waiting for ")
166# )
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"))
sousaeduccfacbb2020-11-04 21:44:01 +0000170
David Garcia49379ce2021-02-24 13:48:22 +0100171# 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})
sousaeduccfacbb2020-11-04 21:44:01 +0000174
David Garcia49379ce2021-02-24 13:48:22 +0100175# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000176
David Garcia49379ce2021-02-24 13:48:22 +0100177# # Verifying status
178# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000179
David Garcia49379ce2021-02-24 13:48:22 +0100180# # Verifying status message
181# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
182# self.assertTrue(
183# self.harness.charm.unit.status.message.startswith("Waiting for ")
184# )
185# self.assertIn("mysql", self.harness.charm.unit.status.message)
186# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
sousaeduccfacbb2020-11-04 21:44:01 +0000187
David Garcia49379ce2021-02-24 13:48:22 +0100188# def test_on_start_with_relations_ng_ro(self) -> NoReturn:
189# """Test deployment with NG-RO."""
190# expected_result = {
191# "version": 3,
192# "containers": [
193# {
194# "name": "ro",
195# "imageDetails": self.harness.charm.image.fetch(),
196# "imagePullPolicy": "Always",
197# "ports": [
198# {
199# "name": "ro",
200# "containerPort": 9090,
201# "protocol": "TCP",
202# }
203# ],
204# "envConfig": {
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",
212# },
213# "kubernetes": {
214# "startupProbe": {
215# "exec": {"command": ["/usr/bin/pgrep", "python3"]},
216# "initialDelaySeconds": 60,
217# "timeoutSeconds": 5,
218# },
219# "readinessProbe": {
220# "httpGet": {
221# "path": "/openmano/tenants",
222# "port": 9090,
223# },
224# "periodSeconds": 10,
225# "timeoutSeconds": 5,
226# "successThreshold": 1,
227# "failureThreshold": 3,
228# },
229# "livenessProbe": {
230# "httpGet": {
231# "path": "/openmano/tenants",
232# "port": 9090,
233# },
234# "initialDelaySeconds": 600,
235# "periodSeconds": 10,
236# "timeoutSeconds": 5,
237# "successThreshold": 1,
238# "failureThreshold": 3,
239# },
240# },
241# }
242# ],
243# "kubernetesResources": {"ingressResources": []},
244# }
sousaeduccfacbb2020-11-04 21:44:01 +0000245
David Garcia49379ce2021-02-24 13:48:22 +0100246# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000247
David Garcia49379ce2021-02-24 13:48:22 +0100248# # 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(
252# relation_id,
253# "kafka/0",
254# {
255# "host": "kafka",
256# "port": "9090",
257# },
258# )
sousaeduccfacbb2020-11-04 21:44:01 +0000259
David Garcia49379ce2021-02-24 13:48:22 +0100260# # 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(
264# relation_id,
265# "mongodb/0",
266# {
267# "connection_string": "mongodb://mongo",
268# },
269# )
sousaeduccfacbb2020-11-04 21:44:01 +0000270
David Garcia49379ce2021-02-24 13:48:22 +0100271# # Verifying status
272# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000273
David Garcia49379ce2021-02-24 13:48:22 +0100274# pod_spec, _ = self.harness.get_pod_spec()
sousaeduccfacbb2020-11-04 21:44:01 +0000275
David Garcia49379ce2021-02-24 13:48:22 +0100276# self.assertDictEqual(expected_result, pod_spec)
sousaeduccfacbb2020-11-04 21:44:01 +0000277
David Garcia49379ce2021-02-24 13:48:22 +0100278# 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})
sousaeduccfacbb2020-11-04 21:44:01 +0000281
David Garcia49379ce2021-02-24 13:48:22 +0100282# expected_result = {
283# "version": 3,
284# "containers": [
285# {
286# "name": "ro",
287# "imageDetails": self.harness.charm.image.fetch(),
288# "imagePullPolicy": "Always",
289# "ports": [
290# {
291# "name": "ro",
292# "containerPort": 9090,
293# "protocol": "TCP",
294# }
295# ],
296# "envConfig": {
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",
311# },
312# "kubernetes": {
313# "startupProbe": {
314# "exec": {"command": ["/usr/bin/pgrep", "python3"]},
315# "initialDelaySeconds": 60,
316# "timeoutSeconds": 5,
317# },
318# "readinessProbe": {
319# "httpGet": {
320# "path": "/openmano/tenants",
321# "port": 9090,
322# },
323# "periodSeconds": 10,
324# "timeoutSeconds": 5,
325# "successThreshold": 1,
326# "failureThreshold": 3,
327# },
328# "livenessProbe": {
329# "httpGet": {
330# "path": "/openmano/tenants",
331# "port": 9090,
332# },
333# "initialDelaySeconds": 600,
334# "periodSeconds": 10,
335# "timeoutSeconds": 5,
336# "successThreshold": 1,
337# "failureThreshold": 3,
338# },
339# },
340# }
341# ],
342# "kubernetesResources": {"ingressResources": []},
343# }
sousaeduccfacbb2020-11-04 21:44:01 +0000344
David Garcia49379ce2021-02-24 13:48:22 +0100345# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000346
David Garcia49379ce2021-02-24 13:48:22 +0100347# # 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(
351# relation_id,
352# "mysql/0",
353# {
354# "host": "mysql",
355# "port": 3306,
356# "user": "mano",
357# "password": "manopw",
358# "root_password": "rootmanopw",
359# },
360# )
sousaeduccfacbb2020-11-04 21:44:01 +0000361
David Garcia49379ce2021-02-24 13:48:22 +0100362# # Verifying status
363# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000364
David Garcia49379ce2021-02-24 13:48:22 +0100365# pod_spec, _ = self.harness.get_pod_spec()
sousaeduccfacbb2020-11-04 21:44:01 +0000366
David Garcia49379ce2021-02-24 13:48:22 +0100367# self.assertDictEqual(expected_result, pod_spec)
368
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()
372
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(
376# relation_id,
377# "kafka/0",
378# {
379# "host": "kafka",
380# "port": 9090,
381# },
382# )
383
384# # Verifying status
385# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
386
387# # Verifying status message
388# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
389# self.assertTrue(
390# self.harness.charm.unit.status.message.startswith("Waiting for ")
391# )
392# self.assertIn("mongodb", self.harness.charm.unit.status.message)
393# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
394
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()
398
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(
402# relation_id,
403# "mongodb/0",
404# {
405# "connection_string": "mongodb://mongo",
406# },
407# )
408
409# # Verifying status
410# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
411
412# # Verifying status message
413# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
414# self.assertTrue(
415# self.harness.charm.unit.status.message.startswith("Waiting for ")
416# )
417# self.assertIn("kafka", self.harness.charm.unit.status.message)
418# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
419
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()
423
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(
427# relation_id,
428# "mysql/0",
429# {
430# "host": "mysql",
431# "port": 3306,
432# "user": "mano",
433# "password": "manopw",
434# "root_password": "rootmanopw",
435# },
436# )
437
438# # Verifying status
439# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
440
441# # Verifying status message
442# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
443# self.assertTrue(
444# self.harness.charm.unit.status.message.startswith("Waiting for ")
445# )
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"))
449
450# def test_publish_ro_info(self) -> NoReturn:
451# """Test to see if ro relation is updated."""
452# expected_result = {
453# "host": "ro",
454# "port": "9090",
455# }
456
457# self.harness.charm.on.start.emit()
458
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")
462
463# self.assertDictEqual(expected_result, relation_data)
sousaeduccfacbb2020-11-04 21:44:01 +0000464
465
466if __name__ == "__main__":
467 unittest.main()