blob: a7cef207563c0c2594b9ef423a3c62de223c2cba [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 Garcia5d1ec6e2021-03-25 15:04:52 +010023import base64
David Garcia49379ce2021-02-24 13:48:22 +010024import sys
sousaedu1dd4c0d2020-11-04 17:43:47 +000025from typing import NoReturn
26import unittest
sousaedu1dd4c0d2020-11-04 17:43:47 +000027
28from charm import MonCharm
David Garciac753dc52021-03-17 15:28:47 +010029from ops.model import ActiveStatus, BlockedStatus
30from ops.testing import Harness
sousaedu1dd4c0d2020-11-04 17:43:47 +000031
32
David Garcia5d1ec6e2021-03-25 15:04:52 +010033def encode(content: str):
34 return base64.b64encode(content.encode("ascii")).decode("utf-8")
35
36
37certificate_pem = encode(
38 """
39-----BEGIN CERTIFICATE-----
40MIIDazCCAlOgAwIBAgIUf1b0s3UKtrxHXH2rge7UaQyfJAMwDQYJKoZIhvcNAQEL
41BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
42GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMTAzMjIxNzEyMjdaFw0zMTAz
43MjAxNzEyMjdaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
44HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
45AQUAA4IBDwAwggEKAoIBAQCgCfCBgYAN6ON0yHDXuW407rFtJVRf0u46Jrp0Dk7J
46kkSZ1e7Kq14r7yFHazEBWv78oOdwBocvWrd8leLuf3bYGcHR65hRy6A/fbYm5Aje
47cKpwlFwaqfR4BLelwJl79jZ2rJX738cCBVrIk1nAVdOxGrXV4MTWUaKR2c+uKKvc
48OKRT+5VqCeP4N5FWeATZ/KqGu8uV9E9WhFgwIZyStemLyLaDbn5PmAQ6S9oeR5jJ
49o2gEEp/lDKvsqOWs76KFumSKa9hQs5Dw2lj0mb1UoyYK1gYc4ubzVChJadv44AU8
50MYtIjlFn1X1P+RjaKZNUIAGXkoLwYn6SizF6y6LiuFS9AgMBAAGjUzBRMB0GA1Ud
51DgQWBBRl+/23CB+FXczeAZRQyYcfOdy9YDAfBgNVHSMEGDAWgBRl+/23CB+FXcze
52AZRQyYcfOdy9YDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAd
53dkeDym6lRN8kWFtfu3IyiLF8G8sn91qNbH3Yr4TuTBhgcjYyW6PgisSbrNgA9ysE
54GoaF7ohb8GeVfCsQdK23+NpAlj/+DZ3OnGcxwXj1RUAz4yr9kanV1yuEtr1q2xJI
55UaECWr8HZlwGBAKNTGx2EXT2/2aFzgULpDcxzTKD+MRpKpMUrWhf9ULvVrclvHWe
56POLYhobUFuBHuo6rt5Rcq16j67zCX9EVTlAE3o2OECIWByK22sXdeOidYMpTkl4q
578FrOqjNsx5d+SBPJBv/pqtBm4bA47Vx1P8tbWOQ4bXS0UmXgwpeBOU/O/ot30+KS
58JnKEy+dYyvVBKg77sRHw
59-----END CERTIFICATE-----
60"""
61)
62
63
sousaedu1dd4c0d2020-11-04 17:43:47 +000064class TestCharm(unittest.TestCase):
David Garcia49379ce2021-02-24 13:48:22 +010065 """Prometheus Charm unit tests."""
sousaedu1dd4c0d2020-11-04 17:43:47 +000066
67 def setUp(self) -> NoReturn:
68 """Test setup"""
David Garcia49379ce2021-02-24 13:48:22 +010069 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
sousaedu1dd4c0d2020-11-04 17:43:47 +000070 self.harness = Harness(MonCharm)
71 self.harness.set_leader(is_leader=True)
72 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010073 self.config = {
74 "vca_host": "192.168.0.13",
75 "vca_user": "admin",
David Garciac753dc52021-03-17 15:28:47 +010076 "vca_secret": "admin",
David Garcia49379ce2021-02-24 13:48:22 +010077 "vca_cacert": "cacert",
78 "database_commonkey": "commonkey",
sousaedu996a5602021-05-03 00:22:43 +020079 "mongodb_uri": "",
David Garcia49379ce2021-02-24 13:48:22 +010080 "log_level": "INFO",
81 "openstack_default_granularity": 10,
82 "global_request_timeout": 10,
83 "collector_interval": 30,
84 "evaluator_interval": 30,
calvinosanc1a43a22f2021-03-08 15:20:07 +010085 "keystone_enabled": True,
David Garcia5d1ec6e2021-03-25 15:04:52 +010086 "certificates": f"cert1:{certificate_pem}",
sousaedu1dd4c0d2020-11-04 17:43:47 +000087 }
David Garcia49379ce2021-02-24 13:48:22 +010088 self.harness.update_config(self.config)
sousaedu1dd4c0d2020-11-04 17:43:47 +000089
David Garcia49379ce2021-02-24 13:48:22 +010090 def test_config_changed_no_relations(
91 self,
92 ) -> NoReturn:
93 """Test ingress resources without HTTP."""
sousaedu1dd4c0d2020-11-04 17:43:47 +000094
David Garcia49379ce2021-02-24 13:48:22 +010095 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +000096
David Garcia49379ce2021-02-24 13:48:22 +010097 # Assertions
98 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
99 self.assertTrue(
100 all(
101 relation in self.harness.charm.unit.status.message
calvinosanc1a43a22f2021-03-08 15:20:07 +0100102 for relation in ["mongodb", "kafka", "prometheus", "keystone"]
David Garcia49379ce2021-02-24 13:48:22 +0100103 )
104 )
sousaedu1dd4c0d2020-11-04 17:43:47 +0000105
David Garcia49379ce2021-02-24 13:48:22 +0100106 def test_config_changed_non_leader(
107 self,
108 ) -> NoReturn:
109 """Test ingress resources without HTTP."""
110 self.harness.set_leader(is_leader=False)
111 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +0000112
David Garcia49379ce2021-02-24 13:48:22 +0100113 # Assertions
114 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
115
sousaedu996a5602021-05-03 00:22:43 +0200116 def test_with_relations_and_mongodb_config(
117 self,
118 ) -> NoReturn:
119 "Test with relations (internal)"
120 self.initialize_kafka_relation()
121 self.initialize_mongo_config()
122 self.initialize_prometheus_relation()
123 self.initialize_keystone_relation()
124 # Verifying status
125 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
126
David Garcia49379ce2021-02-24 13:48:22 +0100127 def test_with_relations(
128 self,
129 ) -> NoReturn:
130 "Test with relations (internal)"
131 self.initialize_kafka_relation()
132 self.initialize_mongo_relation()
133 self.initialize_prometheus_relation()
calvinosanc1a43a22f2021-03-08 15:20:07 +0100134 self.initialize_keystone_relation()
David Garcia49379ce2021-02-24 13:48:22 +0100135 # Verifying status
136 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
137
sousaedu996a5602021-05-03 00:22:43 +0200138 def test_exception_mongodb_relation_and_config(
139 self,
140 ) -> NoReturn:
141 "Test with relations and config for mongodb. Must fail"
142 self.initialize_mongo_relation()
143 self.initialize_mongo_config()
144 # Verifying status
145 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
146
David Garcia49379ce2021-02-24 13:48:22 +0100147 def initialize_kafka_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000148 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
149 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
150 self.harness.update_relation_data(
151 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
152 )
153
sousaedu996a5602021-05-03 00:22:43 +0200154 def initialize_mongo_config(self):
155 self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"})
156
David Garcia49379ce2021-02-24 13:48:22 +0100157 def initialize_mongo_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000158 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
159 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
160 self.harness.update_relation_data(
161 mongodb_relation_id,
162 "mongodb/0",
163 {"connection_string": "mongodb://mongo:27017"},
164 )
165
David Garcia49379ce2021-02-24 13:48:22 +0100166 def initialize_prometheus_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000167 prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
168 self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
169 self.harness.update_relation_data(
170 prometheus_relation_id,
David Garcia49379ce2021-02-24 13:48:22 +0100171 "prometheus",
sousaedu1dd4c0d2020-11-04 17:43:47 +0000172 {"hostname": "prometheus", "port": 9090},
173 )
174
calvinosanc1a43a22f2021-03-08 15:20:07 +0100175 def initialize_keystone_relation(self):
176 keystone_relation_id = self.harness.add_relation("keystone", "keystone")
177 self.harness.add_relation_unit(keystone_relation_id, "keystone/0")
178 self.harness.update_relation_data(
179 keystone_relation_id,
180 "keystone",
181 {
182 "host": "host",
183 "port": 5000,
184 "user_domain_name": "ud",
185 "project_domain_name": "pd",
186 "username": "u",
187 "password": "p",
188 "service": "s",
189 "keystone_db_password": "something",
190 "region_id": "something",
191 "admin_username": "something",
192 "admin_password": "something",
193 "admin_project_name": "something",
194 },
195 )
196
sousaedu1dd4c0d2020-11-04 17:43:47 +0000197
198if __name__ == "__main__":
199 unittest.main()
David Garcia49379ce2021-02-24 13:48:22 +0100200
201
202# class TestCharm(unittest.TestCase):
203# """MON Charm unit tests."""
204
205# def setUp(self) -> NoReturn:
206# """Test setup"""
207# self.harness = Harness(MonCharm)
208# self.harness.set_leader(is_leader=True)
209# self.harness.begin()
210
211# def test_on_start_without_relations(self) -> NoReturn:
212# """Test installation without any relation."""
213# self.harness.charm.on.start.emit()
214
215# # Verifying status
216# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
217
218# # 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("kafka", self.harness.charm.unit.status.message)
224# self.assertIn("mongodb", self.harness.charm.unit.status.message)
225# self.assertIn("prometheus", self.harness.charm.unit.status.message)
226# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
227
228# def test_on_start_with_relations(self) -> NoReturn:
229# """Test deployment without keystone."""
230# expected_result = {
231# "version": 3,
232# "containers": [
233# {
234# "name": "mon",
235# "imageDetails": self.harness.charm.image.fetch(),
236# "imagePullPolicy": "Always",
237# "ports": [
238# {
239# "name": "mon",
240# "containerPort": 8000,
241# "protocol": "TCP",
242# }
243# ],
244# "envConfig": {
245# "ALLOW_ANONYMOUS_LOGIN": "yes",
246# "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": 300,
247# "OSMMON_GLOBAL_REQUEST_TIMEOUT": 10,
248# "OSMMON_GLOBAL_LOGLEVEL": "INFO",
249# "OSMMON_COLLECTOR_INTERVAL": 30,
250# "OSMMON_EVALUATOR_INTERVAL": 30,
251# "OSMMON_MESSAGE_DRIVER": "kafka",
252# "OSMMON_MESSAGE_HOST": "kafka",
253# "OSMMON_MESSAGE_PORT": 9092,
254# "OSMMON_DATABASE_DRIVER": "mongo",
255# "OSMMON_DATABASE_URI": "mongodb://mongo:27017",
256# "OSMMON_DATABASE_COMMONKEY": "osm",
257# "OSMMON_PROMETHEUS_URL": "http://prometheus:9090",
258# "OSMMON_VCA_HOST": "admin",
259# "OSMMON_VCA_USER": "admin",
260# "OSMMON_VCA_SECRET": "secret",
261# "OSMMON_VCA_CACERT": "",
262# },
263# }
264# ],
265# "kubernetesResources": {"ingressResources": []},
266# }
267
268# self.harness.charm.on.start.emit()
269
270# # Check if kafka datastore is initialized
271# self.assertIsNone(self.harness.charm.state.message_host)
272# self.assertIsNone(self.harness.charm.state.message_port)
273
274# # Check if mongodb datastore is initialized
275# self.assertIsNone(self.harness.charm.state.database_uri)
276
277# # Check if prometheus datastore is initialized
278# self.assertIsNone(self.harness.charm.state.prometheus_host)
279# self.assertIsNone(self.harness.charm.state.prometheus_port)
280
281# # Initializing the kafka relation
282# kafka_relation_id = self.harness.add_relation("kafka", "kafka")
283# self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
284# self.harness.update_relation_data(
285# kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
286# )
287
288# # Initializing the mongo relation
289# mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
290# self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
291# self.harness.update_relation_data(
292# mongodb_relation_id,
293# "mongodb/0",
294# {"connection_string": "mongodb://mongo:27017"},
295# )
296
297# # Initializing the prometheus relation
298# prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
299# self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
300# self.harness.update_relation_data(
301# prometheus_relation_id,
302# "prometheus",
303# {"hostname": "prometheus", "port": 9090},
304# )
305
306# # Checking if kafka data is stored
307# self.assertEqual(self.harness.charm.state.message_host, "kafka")
308# self.assertEqual(self.harness.charm.state.message_port, 9092)
309
310# # Checking if mongodb data is stored
311# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
312
313# # Checking if prometheus data is stored
314# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
315# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
316
317# # Verifying status
318# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
319
320# pod_spec, _ = self.harness.get_pod_spec()
321
322# self.assertDictEqual(expected_result, pod_spec)
323
324# def test_on_kafka_unit_relation_changed(self) -> NoReturn:
325# """Test to see if kafka relation is updated."""
326# self.harness.charm.on.start.emit()
327
328# self.assertIsNone(self.harness.charm.state.message_host)
329# self.assertIsNone(self.harness.charm.state.message_port)
330
331# relation_id = self.harness.add_relation("kafka", "kafka")
332# self.harness.add_relation_unit(relation_id, "kafka/0")
333# self.harness.update_relation_data(
334# relation_id, "kafka/0", {"host": "kafka", "port": 9092}
335# )
336
337# self.assertEqual(self.harness.charm.state.message_host, "kafka")
338# self.assertEqual(self.harness.charm.state.message_port, 9092)
339
340# # Verifying status
341# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
342
343# # Verifying status message
344# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
345# self.assertTrue(
346# self.harness.charm.unit.status.message.startswith("Waiting for ")
347# )
348# self.assertNotIn("kafka", self.harness.charm.unit.status.message)
349# self.assertIn("mongodb", self.harness.charm.unit.status.message)
350# self.assertIn("prometheus", self.harness.charm.unit.status.message)
351# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
352
353# def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
354# """Test to see if mongodb relation is updated."""
355# self.harness.charm.on.start.emit()
356
357# self.assertIsNone(self.harness.charm.state.database_uri)
358
359# relation_id = self.harness.add_relation("mongodb", "mongodb")
360# self.harness.add_relation_unit(relation_id, "mongodb/0")
361# self.harness.update_relation_data(
362# relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
363# )
364
365# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
366
367# # Verifying status
368# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
369
370# # Verifying status message
371# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
372# self.assertTrue(
373# self.harness.charm.unit.status.message.startswith("Waiting for ")
374# )
375# self.assertIn("kafka", self.harness.charm.unit.status.message)
376# self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
377# self.assertIn("prometheus", self.harness.charm.unit.status.message)
378# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
379
380# def test_on_prometheus_unit_relation_changed(self) -> NoReturn:
381# """Test to see if prometheus relation is updated."""
382# self.harness.charm.on.start.emit()
383
384# self.assertIsNone(self.harness.charm.state.prometheus_host)
385# self.assertIsNone(self.harness.charm.state.prometheus_port)
386
387# relation_id = self.harness.add_relation("prometheus", "prometheus")
388# self.harness.add_relation_unit(relation_id, "prometheus/0")
389# self.harness.update_relation_data(
390# relation_id, "prometheus", {"hostname": "prometheus", "port": 9090}
391# )
392
393# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
394# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
395
396# # Verifying status
397# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
398
399# # Verifying status message
400# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
401# self.assertTrue(
402# self.harness.charm.unit.status.message.startswith("Waiting for ")
403# )
404# self.assertIn("kafka", self.harness.charm.unit.status.message)
405# self.assertIn("mongodb", self.harness.charm.unit.status.message)
406# self.assertNotIn("prometheus", self.harness.charm.unit.status.message)
407# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
408
409
410# if __name__ == "__main__":
411# unittest.main()