Certificate addition support to mon and ro charms
[osm/devops.git] / installers / charm / ro / tests / test_charm.py
1 #!/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
23 import base64
24 from typing import NoReturn
25 import unittest
26
27 from charm import RoCharm
28 from ops.model import ActiveStatus, BlockedStatus
29 from ops.testing import Harness
30
31
32 def encode(content: str):
33 return base64.b64encode(content.encode("ascii")).decode("utf-8")
34
35
36 certificate_pem = encode(
37 """
38 -----BEGIN CERTIFICATE-----
39 MIIDazCCAlOgAwIBAgIUf1b0s3UKtrxHXH2rge7UaQyfJAMwDQYJKoZIhvcNAQEL
40 BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
41 GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMTAzMjIxNzEyMjdaFw0zMTAz
42 MjAxNzEyMjdaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
43 HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
44 AQUAA4IBDwAwggEKAoIBAQCgCfCBgYAN6ON0yHDXuW407rFtJVRf0u46Jrp0Dk7J
45 kkSZ1e7Kq14r7yFHazEBWv78oOdwBocvWrd8leLuf3bYGcHR65hRy6A/fbYm5Aje
46 cKpwlFwaqfR4BLelwJl79jZ2rJX738cCBVrIk1nAVdOxGrXV4MTWUaKR2c+uKKvc
47 OKRT+5VqCeP4N5FWeATZ/KqGu8uV9E9WhFgwIZyStemLyLaDbn5PmAQ6S9oeR5jJ
48 o2gEEp/lDKvsqOWs76KFumSKa9hQs5Dw2lj0mb1UoyYK1gYc4ubzVChJadv44AU8
49 MYtIjlFn1X1P+RjaKZNUIAGXkoLwYn6SizF6y6LiuFS9AgMBAAGjUzBRMB0GA1Ud
50 DgQWBBRl+/23CB+FXczeAZRQyYcfOdy9YDAfBgNVHSMEGDAWgBRl+/23CB+FXcze
51 AZRQyYcfOdy9YDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAd
52 dkeDym6lRN8kWFtfu3IyiLF8G8sn91qNbH3Yr4TuTBhgcjYyW6PgisSbrNgA9ysE
53 GoaF7ohb8GeVfCsQdK23+NpAlj/+DZ3OnGcxwXj1RUAz4yr9kanV1yuEtr1q2xJI
54 UaECWr8HZlwGBAKNTGx2EXT2/2aFzgULpDcxzTKD+MRpKpMUrWhf9ULvVrclvHWe
55 POLYhobUFuBHuo6rt5Rcq16j67zCX9EVTlAE3o2OECIWByK22sXdeOidYMpTkl4q
56 8FrOqjNsx5d+SBPJBv/pqtBm4bA47Vx1P8tbWOQ4bXS0UmXgwpeBOU/O/ot30+KS
57 JnKEy+dYyvVBKg77sRHw
58 -----END CERTIFICATE-----
59 """
60 )
61
62
63 class TestCharm(unittest.TestCase):
64 """Prometheus Charm unit tests."""
65
66 def setUp(self) -> NoReturn:
67 """Test setup"""
68 self.harness = Harness(RoCharm)
69 self.harness.set_leader(is_leader=True)
70 self.harness.begin()
71 self.config = {
72 "enable_ng_ro": True,
73 "database_commonkey": "commonkey",
74 "log_level": "INFO",
75 "vim_database": "db_name",
76 "ro_database": "ro_db_name",
77 "openmano_tenant": "mano",
78 "certificates": f"cert1:{certificate_pem}",
79 }
80 self.harness.update_config(self.config)
81
82 def test_config_changed_no_relations(
83 self,
84 ) -> NoReturn:
85 """Test ingress resources without HTTP."""
86
87 self.harness.charm.on.config_changed.emit()
88
89 # Assertions
90 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
91 self.assertTrue(
92 all(
93 relation in self.harness.charm.unit.status.message
94 for relation in ["mongodb", "kafka"]
95 )
96 )
97
98 # Disable ng-ro
99 self.harness.update_config({"enable_ng_ro": False})
100 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
101 self.assertTrue(
102 all(
103 relation in self.harness.charm.unit.status.message
104 for relation in ["mysql"]
105 )
106 )
107
108 def test_config_changed_non_leader(
109 self,
110 ) -> NoReturn:
111 """Test ingress resources without HTTP."""
112 self.harness.set_leader(is_leader=False)
113 self.harness.charm.on.config_changed.emit()
114
115 # Assertions
116 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
117
118 def test_with_relations_ng(
119 self,
120 ) -> NoReturn:
121 "Test with relations (ng-ro)"
122
123 # Initializing the kafka relation
124 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
125 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
126 self.harness.update_relation_data(
127 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
128 )
129
130 # Initializing the mongo relation
131 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
132 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
133 self.harness.update_relation_data(
134 mongodb_relation_id,
135 "mongodb/0",
136 {"connection_string": "mongodb://mongo:27017"},
137 )
138
139 # Verifying status
140 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
141
142
143 if __name__ == "__main__":
144 unittest.main()
145
146 # class TestCharm(unittest.TestCase):
147 # """RO Charm unit tests."""
148
149 # def setUp(self) -> NoReturn:
150 # """Test setup"""
151 # self.harness = Harness(RoCharm)
152 # self.harness.set_leader(is_leader=True)
153 # self.harness.begin()
154
155 # def test_on_start_without_relations_ng_ro(self) -> NoReturn:
156 # """Test installation without any relation."""
157 # self.harness.charm.on.start.emit()
158
159 # # Verifying status
160 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
161
162 # # Verifying status message
163 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
164 # self.assertTrue(
165 # self.harness.charm.unit.status.message.startswith("Waiting for ")
166 # )
167 # self.assertIn("kafka", self.harness.charm.unit.status.message)
168 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
169 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
170
171 # def test_on_start_without_relations_no_ng_ro(self) -> NoReturn:
172 # """Test installation without any relation."""
173 # self.harness.update_config({"enable_ng_ro": False})
174
175 # self.harness.charm.on.start.emit()
176
177 # # Verifying status
178 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
179
180 # # Verifying status message
181 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
182 # self.assertTrue(
183 # self.harness.charm.unit.status.message.startswith("Waiting for ")
184 # )
185 # self.assertIn("mysql", self.harness.charm.unit.status.message)
186 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
187
188 # def test_on_start_with_relations_ng_ro(self) -> NoReturn:
189 # """Test deployment with NG-RO."""
190 # expected_result = {
191 # "version": 3,
192 # "containers": [
193 # {
194 # "name": "ro",
195 # "imageDetails": self.harness.charm.image.fetch(),
196 # "imagePullPolicy": "Always",
197 # "ports": [
198 # {
199 # "name": "ro",
200 # "containerPort": 9090,
201 # "protocol": "TCP",
202 # }
203 # ],
204 # "envConfig": {
205 # "OSMRO_LOG_LEVEL": "INFO",
206 # "OSMRO_MESSAGE_DRIVER": "kafka",
207 # "OSMRO_MESSAGE_HOST": "kafka",
208 # "OSMRO_MESSAGE_PORT": "9090",
209 # "OSMRO_DATABASE_DRIVER": "mongo",
210 # "OSMRO_DATABASE_URI": "mongodb://mongo",
211 # "OSMRO_DATABASE_COMMONKEY": "osm",
212 # },
213 # "kubernetes": {
214 # "startupProbe": {
215 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
216 # "initialDelaySeconds": 60,
217 # "timeoutSeconds": 5,
218 # },
219 # "readinessProbe": {
220 # "httpGet": {
221 # "path": "/openmano/tenants",
222 # "port": 9090,
223 # },
224 # "periodSeconds": 10,
225 # "timeoutSeconds": 5,
226 # "successThreshold": 1,
227 # "failureThreshold": 3,
228 # },
229 # "livenessProbe": {
230 # "httpGet": {
231 # "path": "/openmano/tenants",
232 # "port": 9090,
233 # },
234 # "initialDelaySeconds": 600,
235 # "periodSeconds": 10,
236 # "timeoutSeconds": 5,
237 # "successThreshold": 1,
238 # "failureThreshold": 3,
239 # },
240 # },
241 # }
242 # ],
243 # "kubernetesResources": {"ingressResources": []},
244 # }
245
246 # self.harness.charm.on.start.emit()
247
248 # # Initializing the kafka relation
249 # relation_id = self.harness.add_relation("kafka", "kafka")
250 # self.harness.add_relation_unit(relation_id, "kafka/0")
251 # self.harness.update_relation_data(
252 # relation_id,
253 # "kafka/0",
254 # {
255 # "host": "kafka",
256 # "port": "9090",
257 # },
258 # )
259
260 # # Initializing the mongodb relation
261 # relation_id = self.harness.add_relation("mongodb", "mongodb")
262 # self.harness.add_relation_unit(relation_id, "mongodb/0")
263 # self.harness.update_relation_data(
264 # relation_id,
265 # "mongodb/0",
266 # {
267 # "connection_string": "mongodb://mongo",
268 # },
269 # )
270
271 # # Verifying status
272 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
273
274 # pod_spec, _ = self.harness.get_pod_spec()
275
276 # self.assertDictEqual(expected_result, pod_spec)
277
278 # def test_on_start_with_relations_no_ng_ro(self) -> NoReturn:
279 # """Test deployment with old RO."""
280 # self.harness.update_config({"enable_ng_ro": False})
281
282 # expected_result = {
283 # "version": 3,
284 # "containers": [
285 # {
286 # "name": "ro",
287 # "imageDetails": self.harness.charm.image.fetch(),
288 # "imagePullPolicy": "Always",
289 # "ports": [
290 # {
291 # "name": "ro",
292 # "containerPort": 9090,
293 # "protocol": "TCP",
294 # }
295 # ],
296 # "envConfig": {
297 # "OSMRO_LOG_LEVEL": "INFO",
298 # "RO_DB_HOST": "mysql",
299 # "RO_DB_OVIM_HOST": "mysql",
300 # "RO_DB_PORT": 3306,
301 # "RO_DB_OVIM_PORT": 3306,
302 # "RO_DB_USER": "mano",
303 # "RO_DB_OVIM_USER": "mano",
304 # "RO_DB_PASSWORD": "manopw",
305 # "RO_DB_OVIM_PASSWORD": "manopw",
306 # "RO_DB_ROOT_PASSWORD": "rootmanopw",
307 # "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw",
308 # "RO_DB_NAME": "mano_db",
309 # "RO_DB_OVIM_NAME": "mano_vim_db",
310 # "OPENMANO_TENANT": "osm",
311 # },
312 # "kubernetes": {
313 # "startupProbe": {
314 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
315 # "initialDelaySeconds": 60,
316 # "timeoutSeconds": 5,
317 # },
318 # "readinessProbe": {
319 # "httpGet": {
320 # "path": "/openmano/tenants",
321 # "port": 9090,
322 # },
323 # "periodSeconds": 10,
324 # "timeoutSeconds": 5,
325 # "successThreshold": 1,
326 # "failureThreshold": 3,
327 # },
328 # "livenessProbe": {
329 # "httpGet": {
330 # "path": "/openmano/tenants",
331 # "port": 9090,
332 # },
333 # "initialDelaySeconds": 600,
334 # "periodSeconds": 10,
335 # "timeoutSeconds": 5,
336 # "successThreshold": 1,
337 # "failureThreshold": 3,
338 # },
339 # },
340 # }
341 # ],
342 # "kubernetesResources": {"ingressResources": []},
343 # }
344
345 # self.harness.charm.on.start.emit()
346
347 # # Initializing the mysql relation
348 # relation_id = self.harness.add_relation("mysql", "mysql")
349 # self.harness.add_relation_unit(relation_id, "mysql/0")
350 # self.harness.update_relation_data(
351 # relation_id,
352 # "mysql/0",
353 # {
354 # "host": "mysql",
355 # "port": 3306,
356 # "user": "mano",
357 # "password": "manopw",
358 # "root_password": "rootmanopw",
359 # },
360 # )
361
362 # # Verifying status
363 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
364
365 # pod_spec, _ = self.harness.get_pod_spec()
366
367 # self.assertDictEqual(expected_result, pod_spec)
368
369 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
370 # """Test to see if kafka relation is updated."""
371 # self.harness.charm.on.start.emit()
372
373 # relation_id = self.harness.add_relation("kafka", "kafka")
374 # self.harness.add_relation_unit(relation_id, "kafka/0")
375 # self.harness.update_relation_data(
376 # relation_id,
377 # "kafka/0",
378 # {
379 # "host": "kafka",
380 # "port": 9090,
381 # },
382 # )
383
384 # # Verifying status
385 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
386
387 # # Verifying status message
388 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
389 # self.assertTrue(
390 # self.harness.charm.unit.status.message.startswith("Waiting for ")
391 # )
392 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
393 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
394
395 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
396 # """Test to see if mongodb relation is updated."""
397 # self.harness.charm.on.start.emit()
398
399 # relation_id = self.harness.add_relation("mongodb", "mongodb")
400 # self.harness.add_relation_unit(relation_id, "mongodb/0")
401 # self.harness.update_relation_data(
402 # relation_id,
403 # "mongodb/0",
404 # {
405 # "connection_string": "mongodb://mongo",
406 # },
407 # )
408
409 # # Verifying status
410 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
411
412 # # Verifying status message
413 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
414 # self.assertTrue(
415 # self.harness.charm.unit.status.message.startswith("Waiting for ")
416 # )
417 # self.assertIn("kafka", self.harness.charm.unit.status.message)
418 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
419
420 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
421 # """Test to see if mysql relation is updated."""
422 # self.harness.charm.on.start.emit()
423
424 # relation_id = self.harness.add_relation("mysql", "mysql")
425 # self.harness.add_relation_unit(relation_id, "mysql/0")
426 # self.harness.update_relation_data(
427 # relation_id,
428 # "mysql/0",
429 # {
430 # "host": "mysql",
431 # "port": 3306,
432 # "user": "mano",
433 # "password": "manopw",
434 # "root_password": "rootmanopw",
435 # },
436 # )
437
438 # # Verifying status
439 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
440
441 # # Verifying status message
442 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
443 # self.assertTrue(
444 # self.harness.charm.unit.status.message.startswith("Waiting for ")
445 # )
446 # self.assertIn("kafka", self.harness.charm.unit.status.message)
447 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
448 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
449
450 # def test_publish_ro_info(self) -> NoReturn:
451 # """Test to see if ro relation is updated."""
452 # expected_result = {
453 # "host": "ro",
454 # "port": "9090",
455 # }
456
457 # self.harness.charm.on.start.emit()
458
459 # relation_id = self.harness.add_relation("ro", "lcm")
460 # self.harness.add_relation_unit(relation_id, "lcm/0")
461 # relation_data = self.harness.get_relation_data(relation_id, "ro")
462
463 # self.assertDictEqual(expected_result, relation_data)
464
465
466 if __name__ == "__main__":
467 unittest.main()