blob: 6fcd6a65217926300be3f0bc7e4063b5601214a9 [file] [log] [blame]
sousaedu1dd4c0d2020-11-04 17:43:47 +00001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
sousaedu1dd4c0d2020-11-04 17:43:47 +00003#
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
sousaedu1dd4c0d2020-11-04 17:43:47 +000024from typing import NoReturn
25import unittest
David Garcia49379ce2021-02-24 13:48:22 +010026from ops.model import ActiveStatus, BlockedStatus
sousaedu1dd4c0d2020-11-04 17:43:47 +000027from ops.testing import Harness
28
29from charm import MonCharm
30
31
32class TestCharm(unittest.TestCase):
David Garcia49379ce2021-02-24 13:48:22 +010033 """Prometheus Charm unit tests."""
sousaedu1dd4c0d2020-11-04 17:43:47 +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()
sousaedu1dd4c0d2020-11-04 17:43:47 +000038 self.harness = Harness(MonCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010041 self.config = {
42 "vca_host": "192.168.0.13",
43 "vca_user": "admin",
44 "vca_password": "admin",
45 "vca_cacert": "cacert",
46 "database_commonkey": "commonkey",
47 "log_level": "INFO",
48 "openstack_default_granularity": 10,
49 "global_request_timeout": 10,
50 "collector_interval": 30,
51 "evaluator_interval": 30,
sousaedu1dd4c0d2020-11-04 17:43:47 +000052 }
David Garcia49379ce2021-02-24 13:48:22 +010053 self.harness.update_config(self.config)
sousaedu1dd4c0d2020-11-04 17:43:47 +000054
David Garcia49379ce2021-02-24 13:48:22 +010055 def test_config_changed_no_relations(
56 self,
57 ) -> NoReturn:
58 """Test ingress resources without HTTP."""
sousaedu1dd4c0d2020-11-04 17:43:47 +000059
David Garcia49379ce2021-02-24 13:48:22 +010060 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +000061
David Garcia49379ce2021-02-24 13:48:22 +010062 # Assertions
63 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
64 self.assertTrue(
65 all(
66 relation in self.harness.charm.unit.status.message
67 for relation in ["mongodb", "kafka", "prometheus"]
68 )
69 )
sousaedu1dd4c0d2020-11-04 17:43:47 +000070
David Garcia49379ce2021-02-24 13:48:22 +010071 def test_config_changed_non_leader(
72 self,
73 ) -> NoReturn:
74 """Test ingress resources without HTTP."""
75 self.harness.set_leader(is_leader=False)
76 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +000077
David Garcia49379ce2021-02-24 13:48:22 +010078 # Assertions
79 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
80
81 def test_with_relations(
82 self,
83 ) -> NoReturn:
84 "Test with relations (internal)"
85 self.initialize_kafka_relation()
86 self.initialize_mongo_relation()
87 self.initialize_prometheus_relation()
88 # Verifying status
89 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
90
91 def initialize_kafka_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +000092 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
93 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
94 self.harness.update_relation_data(
95 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
96 )
97
David Garcia49379ce2021-02-24 13:48:22 +010098 def initialize_mongo_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +000099 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
100 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
101 self.harness.update_relation_data(
102 mongodb_relation_id,
103 "mongodb/0",
104 {"connection_string": "mongodb://mongo:27017"},
105 )
106
David Garcia49379ce2021-02-24 13:48:22 +0100107 def initialize_prometheus_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000108 prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
109 self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
110 self.harness.update_relation_data(
111 prometheus_relation_id,
David Garcia49379ce2021-02-24 13:48:22 +0100112 "prometheus",
sousaedu1dd4c0d2020-11-04 17:43:47 +0000113 {"hostname": "prometheus", "port": 9090},
114 )
115
sousaedu1dd4c0d2020-11-04 17:43:47 +0000116
117if __name__ == "__main__":
118 unittest.main()
David Garcia49379ce2021-02-24 13:48:22 +0100119
120
121# class TestCharm(unittest.TestCase):
122# """MON Charm unit tests."""
123
124# def setUp(self) -> NoReturn:
125# """Test setup"""
126# self.harness = Harness(MonCharm)
127# self.harness.set_leader(is_leader=True)
128# self.harness.begin()
129
130# def test_on_start_without_relations(self) -> NoReturn:
131# """Test installation without any relation."""
132# self.harness.charm.on.start.emit()
133
134# # Verifying status
135# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
136
137# # Verifying status message
138# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
139# self.assertTrue(
140# self.harness.charm.unit.status.message.startswith("Waiting for ")
141# )
142# self.assertIn("kafka", self.harness.charm.unit.status.message)
143# self.assertIn("mongodb", self.harness.charm.unit.status.message)
144# self.assertIn("prometheus", self.harness.charm.unit.status.message)
145# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
146
147# def test_on_start_with_relations(self) -> NoReturn:
148# """Test deployment without keystone."""
149# expected_result = {
150# "version": 3,
151# "containers": [
152# {
153# "name": "mon",
154# "imageDetails": self.harness.charm.image.fetch(),
155# "imagePullPolicy": "Always",
156# "ports": [
157# {
158# "name": "mon",
159# "containerPort": 8000,
160# "protocol": "TCP",
161# }
162# ],
163# "envConfig": {
164# "ALLOW_ANONYMOUS_LOGIN": "yes",
165# "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": 300,
166# "OSMMON_GLOBAL_REQUEST_TIMEOUT": 10,
167# "OSMMON_GLOBAL_LOGLEVEL": "INFO",
168# "OSMMON_COLLECTOR_INTERVAL": 30,
169# "OSMMON_EVALUATOR_INTERVAL": 30,
170# "OSMMON_MESSAGE_DRIVER": "kafka",
171# "OSMMON_MESSAGE_HOST": "kafka",
172# "OSMMON_MESSAGE_PORT": 9092,
173# "OSMMON_DATABASE_DRIVER": "mongo",
174# "OSMMON_DATABASE_URI": "mongodb://mongo:27017",
175# "OSMMON_DATABASE_COMMONKEY": "osm",
176# "OSMMON_PROMETHEUS_URL": "http://prometheus:9090",
177# "OSMMON_VCA_HOST": "admin",
178# "OSMMON_VCA_USER": "admin",
179# "OSMMON_VCA_SECRET": "secret",
180# "OSMMON_VCA_CACERT": "",
181# },
182# }
183# ],
184# "kubernetesResources": {"ingressResources": []},
185# }
186
187# self.harness.charm.on.start.emit()
188
189# # Check if kafka datastore is initialized
190# self.assertIsNone(self.harness.charm.state.message_host)
191# self.assertIsNone(self.harness.charm.state.message_port)
192
193# # Check if mongodb datastore is initialized
194# self.assertIsNone(self.harness.charm.state.database_uri)
195
196# # Check if prometheus datastore is initialized
197# self.assertIsNone(self.harness.charm.state.prometheus_host)
198# self.assertIsNone(self.harness.charm.state.prometheus_port)
199
200# # Initializing the kafka relation
201# kafka_relation_id = self.harness.add_relation("kafka", "kafka")
202# self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
203# self.harness.update_relation_data(
204# kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
205# )
206
207# # Initializing the mongo relation
208# mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
209# self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
210# self.harness.update_relation_data(
211# mongodb_relation_id,
212# "mongodb/0",
213# {"connection_string": "mongodb://mongo:27017"},
214# )
215
216# # Initializing the prometheus relation
217# prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
218# self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
219# self.harness.update_relation_data(
220# prometheus_relation_id,
221# "prometheus",
222# {"hostname": "prometheus", "port": 9090},
223# )
224
225# # Checking if kafka data is stored
226# self.assertEqual(self.harness.charm.state.message_host, "kafka")
227# self.assertEqual(self.harness.charm.state.message_port, 9092)
228
229# # Checking if mongodb data is stored
230# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
231
232# # Checking if prometheus data is stored
233# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
234# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
235
236# # Verifying status
237# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
238
239# pod_spec, _ = self.harness.get_pod_spec()
240
241# self.assertDictEqual(expected_result, pod_spec)
242
243# def test_on_kafka_unit_relation_changed(self) -> NoReturn:
244# """Test to see if kafka relation is updated."""
245# self.harness.charm.on.start.emit()
246
247# self.assertIsNone(self.harness.charm.state.message_host)
248# self.assertIsNone(self.harness.charm.state.message_port)
249
250# relation_id = self.harness.add_relation("kafka", "kafka")
251# self.harness.add_relation_unit(relation_id, "kafka/0")
252# self.harness.update_relation_data(
253# relation_id, "kafka/0", {"host": "kafka", "port": 9092}
254# )
255
256# self.assertEqual(self.harness.charm.state.message_host, "kafka")
257# self.assertEqual(self.harness.charm.state.message_port, 9092)
258
259# # Verifying status
260# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
261
262# # Verifying status message
263# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
264# self.assertTrue(
265# self.harness.charm.unit.status.message.startswith("Waiting for ")
266# )
267# self.assertNotIn("kafka", self.harness.charm.unit.status.message)
268# self.assertIn("mongodb", self.harness.charm.unit.status.message)
269# self.assertIn("prometheus", self.harness.charm.unit.status.message)
270# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
271
272# def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
273# """Test to see if mongodb relation is updated."""
274# self.harness.charm.on.start.emit()
275
276# self.assertIsNone(self.harness.charm.state.database_uri)
277
278# relation_id = self.harness.add_relation("mongodb", "mongodb")
279# self.harness.add_relation_unit(relation_id, "mongodb/0")
280# self.harness.update_relation_data(
281# relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
282# )
283
284# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
285
286# # Verifying status
287# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
288
289# # Verifying status message
290# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
291# self.assertTrue(
292# self.harness.charm.unit.status.message.startswith("Waiting for ")
293# )
294# self.assertIn("kafka", self.harness.charm.unit.status.message)
295# self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
296# self.assertIn("prometheus", self.harness.charm.unit.status.message)
297# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
298
299# def test_on_prometheus_unit_relation_changed(self) -> NoReturn:
300# """Test to see if prometheus relation is updated."""
301# self.harness.charm.on.start.emit()
302
303# self.assertIsNone(self.harness.charm.state.prometheus_host)
304# self.assertIsNone(self.harness.charm.state.prometheus_port)
305
306# relation_id = self.harness.add_relation("prometheus", "prometheus")
307# self.harness.add_relation_unit(relation_id, "prometheus/0")
308# self.harness.update_relation_data(
309# relation_id, "prometheus", {"hostname": "prometheus", "port": 9090}
310# )
311
312# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
313# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
314
315# # Verifying status
316# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
317
318# # Verifying status message
319# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
320# self.assertTrue(
321# self.harness.charm.unit.status.message.startswith("Waiting for ")
322# )
323# self.assertIn("kafka", self.harness.charm.unit.status.message)
324# self.assertIn("mongodb", self.harness.charm.unit.status.message)
325# self.assertNotIn("prometheus", self.harness.charm.unit.status.message)
326# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
327
328
329# if __name__ == "__main__":
330# unittest.main()