blob: dcf74ed5e17bf2893861bc8e33b24a9fd6642fd3 [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",
79 "log_level": "INFO",
80 "openstack_default_granularity": 10,
81 "global_request_timeout": 10,
82 "collector_interval": 30,
83 "evaluator_interval": 30,
calvinosanc1a43a22f2021-03-08 15:20:07 +010084 "keystone_enabled": True,
David Garcia5d1ec6e2021-03-25 15:04:52 +010085 "certificates": f"cert1:{certificate_pem}",
sousaedu1dd4c0d2020-11-04 17:43:47 +000086 }
David Garcia49379ce2021-02-24 13:48:22 +010087 self.harness.update_config(self.config)
sousaedu1dd4c0d2020-11-04 17:43:47 +000088
David Garcia49379ce2021-02-24 13:48:22 +010089 def test_config_changed_no_relations(
90 self,
91 ) -> NoReturn:
92 """Test ingress resources without HTTP."""
sousaedu1dd4c0d2020-11-04 17:43:47 +000093
David Garcia49379ce2021-02-24 13:48:22 +010094 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +000095
David Garcia49379ce2021-02-24 13:48:22 +010096 # Assertions
97 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
98 self.assertTrue(
99 all(
100 relation in self.harness.charm.unit.status.message
calvinosanc1a43a22f2021-03-08 15:20:07 +0100101 for relation in ["mongodb", "kafka", "prometheus", "keystone"]
David Garcia49379ce2021-02-24 13:48:22 +0100102 )
103 )
sousaedu1dd4c0d2020-11-04 17:43:47 +0000104
David Garcia49379ce2021-02-24 13:48:22 +0100105 def test_config_changed_non_leader(
106 self,
107 ) -> NoReturn:
108 """Test ingress resources without HTTP."""
109 self.harness.set_leader(is_leader=False)
110 self.harness.charm.on.config_changed.emit()
sousaedu1dd4c0d2020-11-04 17:43:47 +0000111
David Garcia49379ce2021-02-24 13:48:22 +0100112 # Assertions
113 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
114
115 def test_with_relations(
116 self,
117 ) -> NoReturn:
118 "Test with relations (internal)"
119 self.initialize_kafka_relation()
120 self.initialize_mongo_relation()
121 self.initialize_prometheus_relation()
calvinosanc1a43a22f2021-03-08 15:20:07 +0100122 self.initialize_keystone_relation()
David Garcia49379ce2021-02-24 13:48:22 +0100123 # Verifying status
124 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
125
126 def initialize_kafka_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000127 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
128 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
129 self.harness.update_relation_data(
130 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
131 )
132
David Garcia49379ce2021-02-24 13:48:22 +0100133 def initialize_mongo_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000134 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
135 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
136 self.harness.update_relation_data(
137 mongodb_relation_id,
138 "mongodb/0",
139 {"connection_string": "mongodb://mongo:27017"},
140 )
141
David Garcia49379ce2021-02-24 13:48:22 +0100142 def initialize_prometheus_relation(self):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000143 prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
144 self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
145 self.harness.update_relation_data(
146 prometheus_relation_id,
David Garcia49379ce2021-02-24 13:48:22 +0100147 "prometheus",
sousaedu1dd4c0d2020-11-04 17:43:47 +0000148 {"hostname": "prometheus", "port": 9090},
149 )
150
calvinosanc1a43a22f2021-03-08 15:20:07 +0100151 def initialize_keystone_relation(self):
152 keystone_relation_id = self.harness.add_relation("keystone", "keystone")
153 self.harness.add_relation_unit(keystone_relation_id, "keystone/0")
154 self.harness.update_relation_data(
155 keystone_relation_id,
156 "keystone",
157 {
158 "host": "host",
159 "port": 5000,
160 "user_domain_name": "ud",
161 "project_domain_name": "pd",
162 "username": "u",
163 "password": "p",
164 "service": "s",
165 "keystone_db_password": "something",
166 "region_id": "something",
167 "admin_username": "something",
168 "admin_password": "something",
169 "admin_project_name": "something",
170 },
171 )
172
sousaedu1dd4c0d2020-11-04 17:43:47 +0000173
174if __name__ == "__main__":
175 unittest.main()
David Garcia49379ce2021-02-24 13:48:22 +0100176
177
178# class TestCharm(unittest.TestCase):
179# """MON Charm unit tests."""
180
181# def setUp(self) -> NoReturn:
182# """Test setup"""
183# self.harness = Harness(MonCharm)
184# self.harness.set_leader(is_leader=True)
185# self.harness.begin()
186
187# def test_on_start_without_relations(self) -> NoReturn:
188# """Test installation without any relation."""
189# self.harness.charm.on.start.emit()
190
191# # Verifying status
192# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
193
194# # Verifying status message
195# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
196# self.assertTrue(
197# self.harness.charm.unit.status.message.startswith("Waiting for ")
198# )
199# self.assertIn("kafka", self.harness.charm.unit.status.message)
200# self.assertIn("mongodb", self.harness.charm.unit.status.message)
201# self.assertIn("prometheus", self.harness.charm.unit.status.message)
202# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
203
204# def test_on_start_with_relations(self) -> NoReturn:
205# """Test deployment without keystone."""
206# expected_result = {
207# "version": 3,
208# "containers": [
209# {
210# "name": "mon",
211# "imageDetails": self.harness.charm.image.fetch(),
212# "imagePullPolicy": "Always",
213# "ports": [
214# {
215# "name": "mon",
216# "containerPort": 8000,
217# "protocol": "TCP",
218# }
219# ],
220# "envConfig": {
221# "ALLOW_ANONYMOUS_LOGIN": "yes",
222# "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": 300,
223# "OSMMON_GLOBAL_REQUEST_TIMEOUT": 10,
224# "OSMMON_GLOBAL_LOGLEVEL": "INFO",
225# "OSMMON_COLLECTOR_INTERVAL": 30,
226# "OSMMON_EVALUATOR_INTERVAL": 30,
227# "OSMMON_MESSAGE_DRIVER": "kafka",
228# "OSMMON_MESSAGE_HOST": "kafka",
229# "OSMMON_MESSAGE_PORT": 9092,
230# "OSMMON_DATABASE_DRIVER": "mongo",
231# "OSMMON_DATABASE_URI": "mongodb://mongo:27017",
232# "OSMMON_DATABASE_COMMONKEY": "osm",
233# "OSMMON_PROMETHEUS_URL": "http://prometheus:9090",
234# "OSMMON_VCA_HOST": "admin",
235# "OSMMON_VCA_USER": "admin",
236# "OSMMON_VCA_SECRET": "secret",
237# "OSMMON_VCA_CACERT": "",
238# },
239# }
240# ],
241# "kubernetesResources": {"ingressResources": []},
242# }
243
244# self.harness.charm.on.start.emit()
245
246# # Check if kafka datastore is initialized
247# self.assertIsNone(self.harness.charm.state.message_host)
248# self.assertIsNone(self.harness.charm.state.message_port)
249
250# # Check if mongodb datastore is initialized
251# self.assertIsNone(self.harness.charm.state.database_uri)
252
253# # Check if prometheus datastore is initialized
254# self.assertIsNone(self.harness.charm.state.prometheus_host)
255# self.assertIsNone(self.harness.charm.state.prometheus_port)
256
257# # Initializing the kafka relation
258# kafka_relation_id = self.harness.add_relation("kafka", "kafka")
259# self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
260# self.harness.update_relation_data(
261# kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
262# )
263
264# # Initializing the mongo relation
265# mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
266# self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
267# self.harness.update_relation_data(
268# mongodb_relation_id,
269# "mongodb/0",
270# {"connection_string": "mongodb://mongo:27017"},
271# )
272
273# # Initializing the prometheus relation
274# prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
275# self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
276# self.harness.update_relation_data(
277# prometheus_relation_id,
278# "prometheus",
279# {"hostname": "prometheus", "port": 9090},
280# )
281
282# # Checking if kafka data is stored
283# self.assertEqual(self.harness.charm.state.message_host, "kafka")
284# self.assertEqual(self.harness.charm.state.message_port, 9092)
285
286# # Checking if mongodb data is stored
287# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
288
289# # Checking if prometheus data is stored
290# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
291# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
292
293# # Verifying status
294# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
295
296# pod_spec, _ = self.harness.get_pod_spec()
297
298# self.assertDictEqual(expected_result, pod_spec)
299
300# def test_on_kafka_unit_relation_changed(self) -> NoReturn:
301# """Test to see if kafka relation is updated."""
302# self.harness.charm.on.start.emit()
303
304# self.assertIsNone(self.harness.charm.state.message_host)
305# self.assertIsNone(self.harness.charm.state.message_port)
306
307# relation_id = self.harness.add_relation("kafka", "kafka")
308# self.harness.add_relation_unit(relation_id, "kafka/0")
309# self.harness.update_relation_data(
310# relation_id, "kafka/0", {"host": "kafka", "port": 9092}
311# )
312
313# self.assertEqual(self.harness.charm.state.message_host, "kafka")
314# self.assertEqual(self.harness.charm.state.message_port, 9092)
315
316# # Verifying status
317# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
318
319# # Verifying status message
320# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
321# self.assertTrue(
322# self.harness.charm.unit.status.message.startswith("Waiting for ")
323# )
324# self.assertNotIn("kafka", self.harness.charm.unit.status.message)
325# self.assertIn("mongodb", self.harness.charm.unit.status.message)
326# self.assertIn("prometheus", self.harness.charm.unit.status.message)
327# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
328
329# def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
330# """Test to see if mongodb relation is updated."""
331# self.harness.charm.on.start.emit()
332
333# self.assertIsNone(self.harness.charm.state.database_uri)
334
335# relation_id = self.harness.add_relation("mongodb", "mongodb")
336# self.harness.add_relation_unit(relation_id, "mongodb/0")
337# self.harness.update_relation_data(
338# relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
339# )
340
341# self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
342
343# # Verifying status
344# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
345
346# # Verifying status message
347# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
348# self.assertTrue(
349# self.harness.charm.unit.status.message.startswith("Waiting for ")
350# )
351# self.assertIn("kafka", self.harness.charm.unit.status.message)
352# self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
353# self.assertIn("prometheus", self.harness.charm.unit.status.message)
354# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
355
356# def test_on_prometheus_unit_relation_changed(self) -> NoReturn:
357# """Test to see if prometheus relation is updated."""
358# self.harness.charm.on.start.emit()
359
360# self.assertIsNone(self.harness.charm.state.prometheus_host)
361# self.assertIsNone(self.harness.charm.state.prometheus_port)
362
363# relation_id = self.harness.add_relation("prometheus", "prometheus")
364# self.harness.add_relation_unit(relation_id, "prometheus/0")
365# self.harness.update_relation_data(
366# relation_id, "prometheus", {"hostname": "prometheus", "port": 9090}
367# )
368
369# self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
370# self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
371
372# # Verifying status
373# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
374
375# # Verifying status message
376# self.assertGreater(len(self.harness.charm.unit.status.message), 0)
377# self.assertTrue(
378# self.harness.charm.unit.status.message.startswith("Waiting for ")
379# )
380# self.assertIn("kafka", self.harness.charm.unit.status.message)
381# self.assertIn("mongodb", self.harness.charm.unit.status.message)
382# self.assertNotIn("prometheus", self.harness.charm.unit.status.message)
383# self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
384
385
386# if __name__ == "__main__":
387# unittest.main()