blob: 4610b2594d67c8b2e296857285489a736d566a91 [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 Garcia49379ce2021-02-24 13:48:22 +010023import sys
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
32class TestCharm(unittest.TestCase):
David Garcia49379ce2021-02-24 13:48:22 +010033 """Prometheus Charm unit tests."""
sousaeduccfacbb2020-11-04 21:44:01 +000034
35 def setUp(self) -> NoReturn:
36 """Test setup"""
David Garcia49379ce2021-02-24 13:48:22 +010037 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
sousaeduccfacbb2020-11-04 21:44:01 +000038 self.harness = Harness(RoCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010041 self.config = {
42 "enable_ng_ro": True,
43 "database_commonkey": "commonkey",
44 "log_level": "INFO",
45 "vim_database": "db_name",
46 "ro_database": "ro_db_name",
47 "openmano_tenant": "mano",
sousaeduccfacbb2020-11-04 21:44:01 +000048 }
David Garcia49379ce2021-02-24 13:48:22 +010049 self.harness.update_config(self.config)
sousaeduccfacbb2020-11-04 21:44:01 +000050
David Garcia49379ce2021-02-24 13:48:22 +010051 def test_config_changed_no_relations(
52 self,
53 ) -> NoReturn:
54 """Test ingress resources without HTTP."""
55
56 self.harness.charm.on.config_changed.emit()
57
58 # Assertions
59 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
60 self.assertTrue(
61 all(
62 relation in self.harness.charm.unit.status.message
63 for relation in ["mongodb", "kafka"]
64 )
65 )
66
67 # Disable ng-ro
68 self.harness.update_config({"enable_ng_ro": False})
69 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
70 self.assertTrue(
71 all(
72 relation in self.harness.charm.unit.status.message
73 for relation in ["mysql"]
74 )
75 )
76
77 def test_config_changed_non_leader(
78 self,
79 ) -> NoReturn:
80 """Test ingress resources without HTTP."""
81 self.harness.set_leader(is_leader=False)
82 self.harness.charm.on.config_changed.emit()
83
84 # Assertions
85 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
86
87 def test_with_relations_ng(
88 self,
89 ) -> NoReturn:
90 "Test with relations (ng-ro)"
sousaeduccfacbb2020-11-04 21:44:01 +000091
92 # Initializing the kafka relation
David Garcia49379ce2021-02-24 13:48:22 +010093 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
94 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
sousaeduccfacbb2020-11-04 21:44:01 +000095 self.harness.update_relation_data(
David Garcia49379ce2021-02-24 13:48:22 +010096 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
sousaeduccfacbb2020-11-04 21:44:01 +000097 )
98
David Garcia49379ce2021-02-24 13:48:22 +010099 # 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")
sousaeduccfacbb2020-11-04 21:44:01 +0000102 self.harness.update_relation_data(
David Garcia49379ce2021-02-24 13:48:22 +0100103 mongodb_relation_id,
sousaeduccfacbb2020-11-04 21:44:01 +0000104 "mongodb/0",
David Garcia49379ce2021-02-24 13:48:22 +0100105 {"connection_string": "mongodb://mongo:27017"},
sousaeduccfacbb2020-11-04 21:44:01 +0000106 )
107
108 # Verifying status
109 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
110
sousaeduccfacbb2020-11-04 21:44:01 +0000111
David Garcia49379ce2021-02-24 13:48:22 +0100112if __name__ == "__main__":
113 unittest.main()
sousaeduccfacbb2020-11-04 21:44:01 +0000114
David Garcia49379ce2021-02-24 13:48:22 +0100115# class TestCharm(unittest.TestCase):
116# """RO Charm unit tests."""
sousaeduccfacbb2020-11-04 21:44:01 +0000117
David Garcia49379ce2021-02-24 13:48:22 +0100118# def setUp(self) -> NoReturn:
119# """Test setup"""
120# self.harness = Harness(RoCharm)
121# self.harness.set_leader(is_leader=True)
122# self.harness.begin()
sousaeduccfacbb2020-11-04 21:44:01 +0000123
David Garcia49379ce2021-02-24 13:48:22 +0100124# def test_on_start_without_relations_ng_ro(self) -> NoReturn:
125# """Test installation without any relation."""
126# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000127
David Garcia49379ce2021-02-24 13:48:22 +0100128# # Verifying status
129# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000130
David Garcia49379ce2021-02-24 13:48:22 +0100131# # Verifying status message
132# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
133# self.assertTrue(
134# self.harness.charm.unit.status.message.startswith("Waiting for ")
135# )
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"))
sousaeduccfacbb2020-11-04 21:44:01 +0000139
David Garcia49379ce2021-02-24 13:48:22 +0100140# 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})
sousaeduccfacbb2020-11-04 21:44:01 +0000143
David Garcia49379ce2021-02-24 13:48:22 +0100144# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000145
David Garcia49379ce2021-02-24 13:48:22 +0100146# # Verifying status
147# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000148
David Garcia49379ce2021-02-24 13:48:22 +0100149# # Verifying status message
150# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
151# self.assertTrue(
152# self.harness.charm.unit.status.message.startswith("Waiting for ")
153# )
154# self.assertIn("mysql", self.harness.charm.unit.status.message)
155# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
sousaeduccfacbb2020-11-04 21:44:01 +0000156
David Garcia49379ce2021-02-24 13:48:22 +0100157# def test_on_start_with_relations_ng_ro(self) -> NoReturn:
158# """Test deployment with NG-RO."""
159# expected_result = {
160# "version": 3,
161# "containers": [
162# {
163# "name": "ro",
164# "imageDetails": self.harness.charm.image.fetch(),
165# "imagePullPolicy": "Always",
166# "ports": [
167# {
168# "name": "ro",
169# "containerPort": 9090,
170# "protocol": "TCP",
171# }
172# ],
173# "envConfig": {
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",
181# },
182# "kubernetes": {
183# "startupProbe": {
184# "exec": {"command": ["/usr/bin/pgrep", "python3"]},
185# "initialDelaySeconds": 60,
186# "timeoutSeconds": 5,
187# },
188# "readinessProbe": {
189# "httpGet": {
190# "path": "/openmano/tenants",
191# "port": 9090,
192# },
193# "periodSeconds": 10,
194# "timeoutSeconds": 5,
195# "successThreshold": 1,
196# "failureThreshold": 3,
197# },
198# "livenessProbe": {
199# "httpGet": {
200# "path": "/openmano/tenants",
201# "port": 9090,
202# },
203# "initialDelaySeconds": 600,
204# "periodSeconds": 10,
205# "timeoutSeconds": 5,
206# "successThreshold": 1,
207# "failureThreshold": 3,
208# },
209# },
210# }
211# ],
212# "kubernetesResources": {"ingressResources": []},
213# }
sousaeduccfacbb2020-11-04 21:44:01 +0000214
David Garcia49379ce2021-02-24 13:48:22 +0100215# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000216
David Garcia49379ce2021-02-24 13:48:22 +0100217# # 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(
221# relation_id,
222# "kafka/0",
223# {
224# "host": "kafka",
225# "port": "9090",
226# },
227# )
sousaeduccfacbb2020-11-04 21:44:01 +0000228
David Garcia49379ce2021-02-24 13:48:22 +0100229# # 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(
233# relation_id,
234# "mongodb/0",
235# {
236# "connection_string": "mongodb://mongo",
237# },
238# )
sousaeduccfacbb2020-11-04 21:44:01 +0000239
David Garcia49379ce2021-02-24 13:48:22 +0100240# # Verifying status
241# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000242
David Garcia49379ce2021-02-24 13:48:22 +0100243# pod_spec, _ = self.harness.get_pod_spec()
sousaeduccfacbb2020-11-04 21:44:01 +0000244
David Garcia49379ce2021-02-24 13:48:22 +0100245# self.assertDictEqual(expected_result, pod_spec)
sousaeduccfacbb2020-11-04 21:44:01 +0000246
David Garcia49379ce2021-02-24 13:48:22 +0100247# 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})
sousaeduccfacbb2020-11-04 21:44:01 +0000250
David Garcia49379ce2021-02-24 13:48:22 +0100251# expected_result = {
252# "version": 3,
253# "containers": [
254# {
255# "name": "ro",
256# "imageDetails": self.harness.charm.image.fetch(),
257# "imagePullPolicy": "Always",
258# "ports": [
259# {
260# "name": "ro",
261# "containerPort": 9090,
262# "protocol": "TCP",
263# }
264# ],
265# "envConfig": {
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",
280# },
281# "kubernetes": {
282# "startupProbe": {
283# "exec": {"command": ["/usr/bin/pgrep", "python3"]},
284# "initialDelaySeconds": 60,
285# "timeoutSeconds": 5,
286# },
287# "readinessProbe": {
288# "httpGet": {
289# "path": "/openmano/tenants",
290# "port": 9090,
291# },
292# "periodSeconds": 10,
293# "timeoutSeconds": 5,
294# "successThreshold": 1,
295# "failureThreshold": 3,
296# },
297# "livenessProbe": {
298# "httpGet": {
299# "path": "/openmano/tenants",
300# "port": 9090,
301# },
302# "initialDelaySeconds": 600,
303# "periodSeconds": 10,
304# "timeoutSeconds": 5,
305# "successThreshold": 1,
306# "failureThreshold": 3,
307# },
308# },
309# }
310# ],
311# "kubernetesResources": {"ingressResources": []},
312# }
sousaeduccfacbb2020-11-04 21:44:01 +0000313
David Garcia49379ce2021-02-24 13:48:22 +0100314# self.harness.charm.on.start.emit()
sousaeduccfacbb2020-11-04 21:44:01 +0000315
David Garcia49379ce2021-02-24 13:48:22 +0100316# # 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(
320# relation_id,
321# "mysql/0",
322# {
323# "host": "mysql",
324# "port": 3306,
325# "user": "mano",
326# "password": "manopw",
327# "root_password": "rootmanopw",
328# },
329# )
sousaeduccfacbb2020-11-04 21:44:01 +0000330
David Garcia49379ce2021-02-24 13:48:22 +0100331# # Verifying status
332# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
sousaeduccfacbb2020-11-04 21:44:01 +0000333
David Garcia49379ce2021-02-24 13:48:22 +0100334# pod_spec, _ = self.harness.get_pod_spec()
sousaeduccfacbb2020-11-04 21:44:01 +0000335
David Garcia49379ce2021-02-24 13:48:22 +0100336# self.assertDictEqual(expected_result, pod_spec)
337
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()
341
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(
345# relation_id,
346# "kafka/0",
347# {
348# "host": "kafka",
349# "port": 9090,
350# },
351# )
352
353# # Verifying status
354# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
355
356# # Verifying status message
357# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
358# self.assertTrue(
359# self.harness.charm.unit.status.message.startswith("Waiting for ")
360# )
361# self.assertIn("mongodb", self.harness.charm.unit.status.message)
362# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
363
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()
367
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(
371# relation_id,
372# "mongodb/0",
373# {
374# "connection_string": "mongodb://mongo",
375# },
376# )
377
378# # Verifying status
379# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
380
381# # Verifying status message
382# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
383# self.assertTrue(
384# self.harness.charm.unit.status.message.startswith("Waiting for ")
385# )
386# self.assertIn("kafka", self.harness.charm.unit.status.message)
387# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
388
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()
392
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(
396# relation_id,
397# "mysql/0",
398# {
399# "host": "mysql",
400# "port": 3306,
401# "user": "mano",
402# "password": "manopw",
403# "root_password": "rootmanopw",
404# },
405# )
406
407# # Verifying status
408# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
409
410# # Verifying status message
411# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
412# self.assertTrue(
413# self.harness.charm.unit.status.message.startswith("Waiting for ")
414# )
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"))
418
419# def test_publish_ro_info(self) -> NoReturn:
420# """Test to see if ro relation is updated."""
421# expected_result = {
422# "host": "ro",
423# "port": "9090",
424# }
425
426# self.harness.charm.on.start.emit()
427
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")
431
432# self.assertDictEqual(expected_result, relation_data)
sousaeduccfacbb2020-11-04 21:44:01 +0000433
434
435if __name__ == "__main__":
436 unittest.main()