blob: f18e7682add2703d408647178af4fe8f87464f89 [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",
sousaedu996a5602021-05-03 00:22:43 +020074 "mongodb_uri": "",
David Garcia49379ce2021-02-24 13:48:22 +010075 "log_level": "INFO",
76 "vim_database": "db_name",
77 "ro_database": "ro_db_name",
78 "openmano_tenant": "mano",
David Garcia5d1ec6e2021-03-25 15:04:52 +010079 "certificates": f"cert1:{certificate_pem}",
sousaeduccfacbb2020-11-04 21:44:01 +000080 }
David Garcia49379ce2021-02-24 13:48:22 +010081 self.harness.update_config(self.config)
sousaeduccfacbb2020-11-04 21:44:01 +000082
David Garcia49379ce2021-02-24 13:48:22 +010083 def test_config_changed_no_relations(
84 self,
85 ) -> NoReturn:
86 """Test ingress resources without HTTP."""
87
88 self.harness.charm.on.config_changed.emit()
89
90 # Assertions
91 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
92 self.assertTrue(
93 all(
94 relation in self.harness.charm.unit.status.message
95 for relation in ["mongodb", "kafka"]
96 )
97 )
98
99 # Disable ng-ro
100 self.harness.update_config({"enable_ng_ro": False})
101 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
102 self.assertTrue(
103 all(
104 relation in self.harness.charm.unit.status.message
105 for relation in ["mysql"]
106 )
107 )
108
109 def test_config_changed_non_leader(
110 self,
111 ) -> NoReturn:
112 """Test ingress resources without HTTP."""
113 self.harness.set_leader(is_leader=False)
114 self.harness.charm.on.config_changed.emit()
115
116 # Assertions
117 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
118
sousaedu996a5602021-05-03 00:22:43 +0200119 def test_with_relations_and_mongodb_config_ng(
120 self,
121 ) -> NoReturn:
122 "Test with relations (ng-ro)"
123
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(
David Garcia4a0db7c2022-02-21 11:48:11 +0100128 kafka_relation_id, "kafka", {"host": "kafka", "port": 9092}
sousaedu996a5602021-05-03 00:22:43 +0200129 )
130
131 # Initializing the mongodb config
132 self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"})
133
134 # Verifying status
135 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
136
David Garcia49379ce2021-02-24 13:48:22 +0100137 def test_with_relations_ng(
138 self,
139 ) -> NoReturn:
140 "Test with relations (ng-ro)"
sousaeduccfacbb2020-11-04 21:44:01 +0000141
142 # Initializing the kafka relation
David Garcia49379ce2021-02-24 13:48:22 +0100143 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
144 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
sousaeduccfacbb2020-11-04 21:44:01 +0000145 self.harness.update_relation_data(
David Garcia4a0db7c2022-02-21 11:48:11 +0100146 kafka_relation_id, "kafka", {"host": "kafka", "port": 9092}
sousaeduccfacbb2020-11-04 21:44:01 +0000147 )
148
David Garcia49379ce2021-02-24 13:48:22 +0100149 # 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")
sousaeduccfacbb2020-11-04 21:44:01 +0000152 self.harness.update_relation_data(
David Garcia49379ce2021-02-24 13:48:22 +0100153 mongodb_relation_id,
sousaeduccfacbb2020-11-04 21:44:01 +0000154 "mongodb/0",
David Garcia49379ce2021-02-24 13:48:22 +0100155 {"connection_string": "mongodb://mongo:27017"},
sousaeduccfacbb2020-11-04 21:44:01 +0000156 )
157
158 # Verifying status
159 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
160
sousaedu996a5602021-05-03 00:22:43 +0200161 def test_ng_exception_mongodb_relation_and_config(
162 self,
163 ) -> NoReturn:
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(
169 mongodb_relation_id,
170 "mongodb/0",
171 {"connection_string": "mongodb://mongo:27017"},
172 )
173
174 # Initializing the mongodb config
175 self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"})
176
177 # Verifying status
178 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
179
sousaeduccfacbb2020-11-04 21:44:01 +0000180
David Garcia49379ce2021-02-24 13:48:22 +0100181if __name__ == "__main__":
182 unittest.main()
sousaeduccfacbb2020-11-04 21:44:01 +0000183
David Garcia49379ce2021-02-24 13:48:22 +0100184# class TestCharm(unittest.TestCase):
185# """RO Charm unit tests."""
sousaeduccfacbb2020-11-04 21:44:01 +0000186
David Garcia49379ce2021-02-24 13:48:22 +0100187# def setUp(self) -> NoReturn:
188# """Test setup"""
189# self.harness = Harness(RoCharm)
190# self.harness.set_leader(is_leader=True)
191# self.harness.begin()
sousaeduccfacbb2020-11-04 21:44:01 +0000192
David Garcia49379ce2021-02-24 13:48:22 +0100193# def test_on_start_without_relations_ng_ro(self) -> NoReturn:
194# """Test installation without any relation."""
195# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000196
David Garcia49379ce2021-02-24 13:48:22 +0100197# # Verifying status
198# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000199
David Garcia49379ce2021-02-24 13:48:22 +0100200# # Verifying status message
201# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
202# self.assertTrue(
203# self.harness.charm.unit.status.message.startswith("Waiting for ")
204# )
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"))
sousaeduccfacbb2020-11-04 21:44:01 +0000208
David Garcia49379ce2021-02-24 13:48:22 +0100209# 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})
sousaeduccfacbb2020-11-04 21:44:01 +0000212
David Garcia49379ce2021-02-24 13:48:22 +0100213# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000214
David Garcia49379ce2021-02-24 13:48:22 +0100215# # Verifying status
216# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000217
David Garcia49379ce2021-02-24 13:48:22 +0100218# # Verifying status message
219# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
220# self.assertTrue(
221# self.harness.charm.unit.status.message.startswith("Waiting for ")
222# )
223# self.assertIn("mysql", self.harness.charm.unit.status.message)
224# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
sousaeduccfacbb2020-11-04 21:44:01 +0000225
David Garcia49379ce2021-02-24 13:48:22 +0100226# def test_on_start_with_relations_ng_ro(self) -> NoReturn:
227# """Test deployment with NG-RO."""
228# expected_result = {
229# "version": 3,
230# "containers": [
231# {
232# "name": "ro",
233# "imageDetails": self.harness.charm.image.fetch(),
234# "imagePullPolicy": "Always",
235# "ports": [
236# {
237# "name": "ro",
238# "containerPort": 9090,
239# "protocol": "TCP",
240# }
241# ],
242# "envConfig": {
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",
250# },
251# "kubernetes": {
252# "startupProbe": {
253# "exec": {"command": ["/usr/bin/pgrep", "python3"]},
254# "initialDelaySeconds": 60,
255# "timeoutSeconds": 5,
256# },
257# "readinessProbe": {
258# "httpGet": {
259# "path": "/openmano/tenants",
260# "port": 9090,
261# },
262# "periodSeconds": 10,
263# "timeoutSeconds": 5,
264# "successThreshold": 1,
265# "failureThreshold": 3,
266# },
267# "livenessProbe": {
268# "httpGet": {
269# "path": "/openmano/tenants",
270# "port": 9090,
271# },
272# "initialDelaySeconds": 600,
273# "periodSeconds": 10,
274# "timeoutSeconds": 5,
275# "successThreshold": 1,
276# "failureThreshold": 3,
277# },
278# },
279# }
280# ],
281# "kubernetesResources": {"ingressResources": []},
282# }
sousaeduccfacbb2020-11-04 21:44:01 +0000283
David Garcia49379ce2021-02-24 13:48:22 +0100284# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000285
David Garcia49379ce2021-02-24 13:48:22 +0100286# # 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(
290# relation_id,
291# "kafka/0",
292# {
293# "host": "kafka",
294# "port": "9090",
295# },
296# )
sousaeduccfacbb2020-11-04 21:44:01 +0000297
David Garcia49379ce2021-02-24 13:48:22 +0100298# # 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(
302# relation_id,
303# "mongodb/0",
304# {
305# "connection_string": "mongodb://mongo",
306# },
307# )
sousaeduccfacbb2020-11-04 21:44:01 +0000308
David Garcia49379ce2021-02-24 13:48:22 +0100309# # Verifying status
310# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000311
David Garcia49379ce2021-02-24 13:48:22 +0100312# pod_spec, _ = self.harness.get_pod_spec()
sousaeduccfacbb2020-11-04 21:44:01 +0000313
David Garcia49379ce2021-02-24 13:48:22 +0100314# self.assertDictEqual(expected_result, pod_spec)
sousaeduccfacbb2020-11-04 21:44:01 +0000315
David Garcia49379ce2021-02-24 13:48:22 +0100316# 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})
sousaeduccfacbb2020-11-04 21:44:01 +0000319
David Garcia49379ce2021-02-24 13:48:22 +0100320# expected_result = {
321# "version": 3,
322# "containers": [
323# {
324# "name": "ro",
325# "imageDetails": self.harness.charm.image.fetch(),
326# "imagePullPolicy": "Always",
327# "ports": [
328# {
329# "name": "ro",
330# "containerPort": 9090,
331# "protocol": "TCP",
332# }
333# ],
334# "envConfig": {
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",
349# },
350# "kubernetes": {
351# "startupProbe": {
352# "exec": {"command": ["/usr/bin/pgrep", "python3"]},
353# "initialDelaySeconds": 60,
354# "timeoutSeconds": 5,
355# },
356# "readinessProbe": {
357# "httpGet": {
358# "path": "/openmano/tenants",
359# "port": 9090,
360# },
361# "periodSeconds": 10,
362# "timeoutSeconds": 5,
363# "successThreshold": 1,
364# "failureThreshold": 3,
365# },
366# "livenessProbe": {
367# "httpGet": {
368# "path": "/openmano/tenants",
369# "port": 9090,
370# },
371# "initialDelaySeconds": 600,
372# "periodSeconds": 10,
373# "timeoutSeconds": 5,
374# "successThreshold": 1,
375# "failureThreshold": 3,
376# },
377# },
378# }
379# ],
380# "kubernetesResources": {"ingressResources": []},
381# }
sousaeduccfacbb2020-11-04 21:44:01 +0000382
David Garcia49379ce2021-02-24 13:48:22 +0100383# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000384
David Garcia49379ce2021-02-24 13:48:22 +0100385# # 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(
389# relation_id,
390# "mysql/0",
391# {
392# "host": "mysql",
393# "port": 3306,
394# "user": "mano",
395# "password": "manopw",
396# "root_password": "rootmanopw",
397# },
398# )
sousaeduccfacbb2020-11-04 21:44:01 +0000399
David Garcia49379ce2021-02-24 13:48:22 +0100400# # Verifying status
401# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000402
David Garcia49379ce2021-02-24 13:48:22 +0100403# pod_spec, _ = self.harness.get_pod_spec()
sousaeduccfacbb2020-11-04 21:44:01 +0000404
David Garcia49379ce2021-02-24 13:48:22 +0100405# self.assertDictEqual(expected_result, pod_spec)
406
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()
410
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(
414# relation_id,
415# "kafka/0",
416# {
417# "host": "kafka",
418# "port": 9090,
419# },
420# )
421
422# # Verifying status
423# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
424
425# # Verifying status message
426# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
427# self.assertTrue(
428# self.harness.charm.unit.status.message.startswith("Waiting for ")
429# )
430# self.assertIn("mongodb", self.harness.charm.unit.status.message)
431# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
432
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()
436
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(
440# relation_id,
441# "mongodb/0",
442# {
443# "connection_string": "mongodb://mongo",
444# },
445# )
446
447# # Verifying status
448# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
449
450# # Verifying status message
451# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
452# self.assertTrue(
453# self.harness.charm.unit.status.message.startswith("Waiting for ")
454# )
455# self.assertIn("kafka", self.harness.charm.unit.status.message)
456# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
457
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()
461
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(
465# relation_id,
466# "mysql/0",
467# {
468# "host": "mysql",
469# "port": 3306,
470# "user": "mano",
471# "password": "manopw",
472# "root_password": "rootmanopw",
473# },
474# )
475
476# # Verifying status
477# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
478
479# # Verifying status message
480# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
481# self.assertTrue(
482# self.harness.charm.unit.status.message.startswith("Waiting for ")
483# )
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"))
487
488# def test_publish_ro_info(self) -> NoReturn:
489# """Test to see if ro relation is updated."""
490# expected_result = {
491# "host": "ro",
492# "port": "9090",
493# }
494
495# self.harness.charm.on.start.emit()
496
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")
500
501# self.assertDictEqual(expected_result, relation_data)
sousaeduccfacbb2020-11-04 21:44:01 +0000502
503
504if __name__ == "__main__":
505 unittest.main()