blob: 858ff7cb90832cb396c5d736a2f496fadf8cfef1 [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
sousaedu1dd4c0d2020-11-04 17:43:47 +000026
27from charm import MonCharm
David Garciac753dc52021-03-17 15:28:47 +010028from ops.model import ActiveStatus, BlockedStatus
29from ops.testing import Harness
sousaedu1dd4c0d2020-11-04 17:43:47 +000030
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",
David Garciac753dc52021-03-17 15:28:47 +010044 "vca_secret": "admin",
David Garcia49379ce2021-02-24 13:48:22 +010045 "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,
calvinosanc1a43a22f2021-03-08 15:20:07 +010052 "keystone_enabled": True,
sousaedu1dd4c0d2020-11-04 17:43:47 +000053 }
David Garcia49379ce2021-02-24 13:48:22 +010054 self.harness.update_config(self.config)
sousaedu1dd4c0d2020-11-04 17:43:47 +000055
David Garcia49379ce2021-02-24 13:48:22 +010056 def test_config_changed_no_relations(
57 self,
58 ) -> NoReturn:
59 """Test ingress resources without HTTP."""
sousaedu1dd4c0d2020-11-04 17:43:47 +000060
David Garcia49379ce2021-02-24 13:48:22 +010061 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +000062
David Garcia49379ce2021-02-24 13:48:22 +010063 # Assertions
64 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
65 self.assertTrue(
66 all(
67 relation in self.harness.charm.unit.status.message
calvinosanc1a43a22f2021-03-08 15:20:07 +010068 for relation in ["mongodb", "kafka", "prometheus", "keystone"]
David Garcia49379ce2021-02-24 13:48:22 +010069 )
70 )
sousaedu1dd4c0d2020-11-04 17:43:47 +000071
David Garcia49379ce2021-02-24 13:48:22 +010072 def test_config_changed_non_leader(
73 self,
74 ) -> NoReturn:
75 """Test ingress resources without HTTP."""
76 self.harness.set_leader(is_leader=False)
77 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +000078
David Garcia49379ce2021-02-24 13:48:22 +010079 # Assertions
80 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
81
82 def test_with_relations(
83 self,
84 ) -> NoReturn:
85 "Test with relations (internal)"
86 self.initialize_kafka_relation()
87 self.initialize_mongo_relation()
88 self.initialize_prometheus_relation()
calvinosanc1a43a22f2021-03-08 15:20:07 +010089 self.initialize_keystone_relation()
David Garcia49379ce2021-02-24 13:48:22 +010090 # Verifying status
91 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
92
93 def initialize_kafka_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +000094 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
95 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
96 self.harness.update_relation_data(
97 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
98 )
99
David Garcia49379ce2021-02-24 13:48:22 +0100100 def initialize_mongo_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000101 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
102 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
103 self.harness.update_relation_data(
104 mongodb_relation_id,
105 "mongodb/0",
106 {"connection_string": "mongodb://mongo:27017"},
107 )
108
David Garcia49379ce2021-02-24 13:48:22 +0100109 def initialize_prometheus_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000110 prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
111 self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
112 self.harness.update_relation_data(
113 prometheus_relation_id,
David Garcia49379ce2021-02-24 13:48:22 +0100114 "prometheus",
sousaedu1dd4c0d2020-11-04 17:43:47 +0000115 {"hostname": "prometheus", "port": 9090},
116 )
117
calvinosanc1a43a22f2021-03-08 15:20:07 +0100118 def initialize_keystone_relation(self):
119 keystone_relation_id = self.harness.add_relation("keystone", "keystone")
120 self.harness.add_relation_unit(keystone_relation_id, "keystone/0")
121 self.harness.update_relation_data(
122 keystone_relation_id,
123 "keystone",
124 {
125 "host": "host",
126 "port": 5000,
127 "user_domain_name": "ud",
128 "project_domain_name": "pd",
129 "username": "u",
130 "password": "p",
131 "service": "s",
132 "keystone_db_password": "something",
133 "region_id": "something",
134 "admin_username": "something",
135 "admin_password": "something",
136 "admin_project_name": "something",
137 },
138 )
139
sousaedu1dd4c0d2020-11-04 17:43:47 +0000140
141if __name__ == "__main__":
142 unittest.main()
David Garcia49379ce2021-02-24 13:48:22 +0100143
144
145# class TestCharm(unittest.TestCase):
146# """MON Charm unit tests."""
147
148# def setUp(self) -> NoReturn:
149# """Test setup"""
150# self.harness = Harness(MonCharm)
151# self.harness.set_leader(is_leader=True)
152# self.harness.begin()
153
154# def test_on_start_without_relations(self) -> NoReturn:
155# """Test installation without any relation."""
156# self.harness.charm.on.start.emit()
157
158# # Verifying status
159# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
160
161# # Verifying status message
162# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
163# self.assertTrue(
164# self.harness.charm.unit.status.message.startswith("Waiting for ")
165# )
166# self.assertIn("kafka", self.harness.charm.unit.status.message)
167# self.assertIn("mongodb", self.harness.charm.unit.status.message)
168# self.assertIn("prometheus", self.harness.charm.unit.status.message)
169# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
170
171# def test_on_start_with_relations(self) -> NoReturn:
172# """Test deployment without keystone."""
173# expected_result = {
174# "version": 3,
175# "containers": [
176# {
177# "name": "mon",
178# "imageDetails": self.harness.charm.image.fetch(),
179# "imagePullPolicy": "Always",
180# "ports": [
181# {
182# "name": "mon",
183# "containerPort": 8000,
184# "protocol": "TCP",
185# }
186# ],
187# "envConfig": {
188# "ALLOW_ANONYMOUS_LOGIN": "yes",
189# "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": 300,
190# "OSMMON_GLOBAL_REQUEST_TIMEOUT": 10,
191# "OSMMON_GLOBAL_LOGLEVEL": "INFO",
192# "OSMMON_COLLECTOR_INTERVAL": 30,
193# "OSMMON_EVALUATOR_INTERVAL": 30,
194# "OSMMON_MESSAGE_DRIVER": "kafka",
195# "OSMMON_MESSAGE_HOST": "kafka",
196# "OSMMON_MESSAGE_PORT": 9092,
197# "OSMMON_DATABASE_DRIVER": "mongo",
198# "OSMMON_DATABASE_URI": "mongodb://mongo:27017",
199# "OSMMON_DATABASE_COMMONKEY": "osm",
200# "OSMMON_PROMETHEUS_URL": "http://prometheus:9090",
201# "OSMMON_VCA_HOST": "admin",
202# "OSMMON_VCA_USER": "admin",
203# "OSMMON_VCA_SECRET": "secret",
204# "OSMMON_VCA_CACERT": "",
205# },
206# }
207# ],
208# "kubernetesResources": {"ingressResources": []},
209# }
210
211# self.harness.charm.on.start.emit()
212
213# # Check if kafka datastore is initialized
214# self.assertIsNone(self.harness.charm.state.message_host)
215# self.assertIsNone(self.harness.charm.state.message_port)
216
217# # Check if mongodb datastore is initialized
218# self.assertIsNone(self.harness.charm.state.database_uri)
219
220# # Check if prometheus datastore is initialized
221# self.assertIsNone(self.harness.charm.state.prometheus_host)
222# self.assertIsNone(self.harness.charm.state.prometheus_port)
223
224# # Initializing the kafka relation
225# kafka_relation_id = self.harness.add_relation("kafka", "kafka")
226# self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
227# self.harness.update_relation_data(
228# kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
229# )
230
231# # Initializing the mongo relation
232# mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
233# self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
234# self.harness.update_relation_data(
235# mongodb_relation_id,
236# "mongodb/0",
237# {"connection_string": "mongodb://mongo:27017"},
238# )
239
240# # Initializing the prometheus relation
241# prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
242# self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
243# self.harness.update_relation_data(
244# prometheus_relation_id,
245# "prometheus",
246# {"hostname": "prometheus", "port": 9090},
247# )
248
249# # Checking if kafka data is stored
250# self.assertEqual(self.harness.charm.state.message_host, "kafka")
251# self.assertEqual(self.harness.charm.state.message_port, 9092)
252
253# # Checking if mongodb data is stored
254# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
255
256# # Checking if prometheus data is stored
257# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
258# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
259
260# # Verifying status
261# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
262
263# pod_spec, _ = self.harness.get_pod_spec()
264
265# self.assertDictEqual(expected_result, pod_spec)
266
267# def test_on_kafka_unit_relation_changed(self) -> NoReturn:
268# """Test to see if kafka relation is updated."""
269# self.harness.charm.on.start.emit()
270
271# self.assertIsNone(self.harness.charm.state.message_host)
272# self.assertIsNone(self.harness.charm.state.message_port)
273
274# relation_id = self.harness.add_relation("kafka", "kafka")
275# self.harness.add_relation_unit(relation_id, "kafka/0")
276# self.harness.update_relation_data(
277# relation_id, "kafka/0", {"host": "kafka", "port": 9092}
278# )
279
280# self.assertEqual(self.harness.charm.state.message_host, "kafka")
281# self.assertEqual(self.harness.charm.state.message_port, 9092)
282
283# # Verifying status
284# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
285
286# # Verifying status message
287# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
288# self.assertTrue(
289# self.harness.charm.unit.status.message.startswith("Waiting for ")
290# )
291# self.assertNotIn("kafka", self.harness.charm.unit.status.message)
292# self.assertIn("mongodb", self.harness.charm.unit.status.message)
293# self.assertIn("prometheus", self.harness.charm.unit.status.message)
294# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
295
296# def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
297# """Test to see if mongodb relation is updated."""
298# self.harness.charm.on.start.emit()
299
300# self.assertIsNone(self.harness.charm.state.database_uri)
301
302# relation_id = self.harness.add_relation("mongodb", "mongodb")
303# self.harness.add_relation_unit(relation_id, "mongodb/0")
304# self.harness.update_relation_data(
305# relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
306# )
307
308# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
309
310# # Verifying status
311# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
312
313# # Verifying status message
314# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
315# self.assertTrue(
316# self.harness.charm.unit.status.message.startswith("Waiting for ")
317# )
318# self.assertIn("kafka", self.harness.charm.unit.status.message)
319# self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
320# self.assertIn("prometheus", self.harness.charm.unit.status.message)
321# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
322
323# def test_on_prometheus_unit_relation_changed(self) -> NoReturn:
324# """Test to see if prometheus relation is updated."""
325# self.harness.charm.on.start.emit()
326
327# self.assertIsNone(self.harness.charm.state.prometheus_host)
328# self.assertIsNone(self.harness.charm.state.prometheus_port)
329
330# relation_id = self.harness.add_relation("prometheus", "prometheus")
331# self.harness.add_relation_unit(relation_id, "prometheus/0")
332# self.harness.update_relation_data(
333# relation_id, "prometheus", {"hostname": "prometheus", "port": 9090}
334# )
335
336# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
337# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
338
339# # Verifying status
340# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
341
342# # Verifying status message
343# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
344# self.assertTrue(
345# self.harness.charm.unit.status.message.startswith("Waiting for ")
346# )
347# self.assertIn("kafka", self.harness.charm.unit.status.message)
348# self.assertIn("mongodb", self.harness.charm.unit.status.message)
349# self.assertNotIn("prometheus", self.harness.charm.unit.status.message)
350# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
351
352
353# if __name__ == "__main__":
354# unittest.main()