2d317bd36eaf70298b371a57728f3f02c0a7c549
[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 "mongodb_uri": "",
75 "log_level": "INFO",
76 "vim_database": "db_name",
77 "ro_database": "ro_db_name",
78 "openmano_tenant": "mano",
79 "certificates": f"cert1:{certificate_pem}",
80 }
81 self.harness.update_config(self.config)
82
83 def test_config_changed_no_relations(
84 self,
85 ) -> NoReturn:
86 """Test ingress resources without HTTP."""
87
88 self.harness.charm.on.config_changed.emit()
89
90 # Assertions
91 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
92 self.assertTrue(
93 all(
94 relation in self.harness.charm.unit.status.message
95 for relation in ["mongodb", "kafka"]
96 )
97 )
98
99 # Disable ng-ro
100 self.harness.update_config({"enable_ng_ro": False})
101 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
102 self.assertTrue(
103 all(
104 relation in self.harness.charm.unit.status.message
105 for relation in ["mysql"]
106 )
107 )
108
109 def test_config_changed_non_leader(
110 self,
111 ) -> NoReturn:
112 """Test ingress resources without HTTP."""
113 self.harness.set_leader(is_leader=False)
114 self.harness.charm.on.config_changed.emit()
115
116 # Assertions
117 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
118
119 def test_with_relations_and_mongodb_config_ng(
120 self,
121 ) -> NoReturn:
122 "Test with relations (ng-ro)"
123
124 # Initializing the kafka relation
125 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
126 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
127 self.harness.update_relation_data(
128 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
129 )
130
131 # Initializing the mongodb config
132 self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"})
133
134 # Verifying status
135 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
136
137 def test_with_relations_ng(
138 self,
139 ) -> NoReturn:
140 "Test with relations (ng-ro)"
141
142 # Initializing the kafka relation
143 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
144 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
145 self.harness.update_relation_data(
146 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
147 )
148
149 # Initializing the mongo relation
150 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
151 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
152 self.harness.update_relation_data(
153 mongodb_relation_id,
154 "mongodb/0",
155 {"connection_string": "mongodb://mongo:27017"},
156 )
157
158 # Verifying status
159 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
160
161 def test_ng_exception_mongodb_relation_and_config(
162 self,
163 ) -> NoReturn:
164 "Test NG-RO mongodb relation and config. Must fail"
165 # Initializing the mongo relation
166 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
167 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
168 self.harness.update_relation_data(
169 mongodb_relation_id,
170 "mongodb/0",
171 {"connection_string": "mongodb://mongo:27017"},
172 )
173
174 # Initializing the mongodb config
175 self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"})
176
177 # Verifying status
178 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
179
180
181 if __name__ == "__main__":
182 unittest.main()
183
184 # class TestCharm(unittest.TestCase):
185 # """RO Charm unit tests."""
186
187 # def setUp(self) -> NoReturn:
188 # """Test setup"""
189 # self.harness = Harness(RoCharm)
190 # self.harness.set_leader(is_leader=True)
191 # self.harness.begin()
192
193 # def test_on_start_without_relations_ng_ro(self) -> NoReturn:
194 # """Test installation without any relation."""
195 # self.harness.charm.on.start.emit()
196
197 # # Verifying status
198 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
199
200 # # Verifying status message
201 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
202 # self.assertTrue(
203 # self.harness.charm.unit.status.message.startswith("Waiting for ")
204 # )
205 # self.assertIn("kafka", self.harness.charm.unit.status.message)
206 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
207 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
208
209 # def test_on_start_without_relations_no_ng_ro(self) -> NoReturn:
210 # """Test installation without any relation."""
211 # self.harness.update_config({"enable_ng_ro": False})
212
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("mysql", self.harness.charm.unit.status.message)
224 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
225
226 # def test_on_start_with_relations_ng_ro(self) -> NoReturn:
227 # """Test deployment with NG-RO."""
228 # expected_result = {
229 # "version": 3,
230 # "containers": [
231 # {
232 # "name": "ro",
233 # "imageDetails": self.harness.charm.image.fetch(),
234 # "imagePullPolicy": "Always",
235 # "ports": [
236 # {
237 # "name": "ro",
238 # "containerPort": 9090,
239 # "protocol": "TCP",
240 # }
241 # ],
242 # "envConfig": {
243 # "OSMRO_LOG_LEVEL": "INFO",
244 # "OSMRO_MESSAGE_DRIVER": "kafka",
245 # "OSMRO_MESSAGE_HOST": "kafka",
246 # "OSMRO_MESSAGE_PORT": "9090",
247 # "OSMRO_DATABASE_DRIVER": "mongo",
248 # "OSMRO_DATABASE_URI": "mongodb://mongo",
249 # "OSMRO_DATABASE_COMMONKEY": "osm",
250 # },
251 # "kubernetes": {
252 # "startupProbe": {
253 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
254 # "initialDelaySeconds": 60,
255 # "timeoutSeconds": 5,
256 # },
257 # "readinessProbe": {
258 # "httpGet": {
259 # "path": "/openmano/tenants",
260 # "port": 9090,
261 # },
262 # "periodSeconds": 10,
263 # "timeoutSeconds": 5,
264 # "successThreshold": 1,
265 # "failureThreshold": 3,
266 # },
267 # "livenessProbe": {
268 # "httpGet": {
269 # "path": "/openmano/tenants",
270 # "port": 9090,
271 # },
272 # "initialDelaySeconds": 600,
273 # "periodSeconds": 10,
274 # "timeoutSeconds": 5,
275 # "successThreshold": 1,
276 # "failureThreshold": 3,
277 # },
278 # },
279 # }
280 # ],
281 # "kubernetesResources": {"ingressResources": []},
282 # }
283
284 # self.harness.charm.on.start.emit()
285
286 # # Initializing the kafka relation
287 # relation_id = self.harness.add_relation("kafka", "kafka")
288 # self.harness.add_relation_unit(relation_id, "kafka/0")
289 # self.harness.update_relation_data(
290 # relation_id,
291 # "kafka/0",
292 # {
293 # "host": "kafka",
294 # "port": "9090",
295 # },
296 # )
297
298 # # Initializing the mongodb relation
299 # relation_id = self.harness.add_relation("mongodb", "mongodb")
300 # self.harness.add_relation_unit(relation_id, "mongodb/0")
301 # self.harness.update_relation_data(
302 # relation_id,
303 # "mongodb/0",
304 # {
305 # "connection_string": "mongodb://mongo",
306 # },
307 # )
308
309 # # Verifying status
310 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
311
312 # pod_spec, _ = self.harness.get_pod_spec()
313
314 # self.assertDictEqual(expected_result, pod_spec)
315
316 # def test_on_start_with_relations_no_ng_ro(self) -> NoReturn:
317 # """Test deployment with old RO."""
318 # self.harness.update_config({"enable_ng_ro": False})
319
320 # expected_result = {
321 # "version": 3,
322 # "containers": [
323 # {
324 # "name": "ro",
325 # "imageDetails": self.harness.charm.image.fetch(),
326 # "imagePullPolicy": "Always",
327 # "ports": [
328 # {
329 # "name": "ro",
330 # "containerPort": 9090,
331 # "protocol": "TCP",
332 # }
333 # ],
334 # "envConfig": {
335 # "OSMRO_LOG_LEVEL": "INFO",
336 # "RO_DB_HOST": "mysql",
337 # "RO_DB_OVIM_HOST": "mysql",
338 # "RO_DB_PORT": 3306,
339 # "RO_DB_OVIM_PORT": 3306,
340 # "RO_DB_USER": "mano",
341 # "RO_DB_OVIM_USER": "mano",
342 # "RO_DB_PASSWORD": "manopw",
343 # "RO_DB_OVIM_PASSWORD": "manopw",
344 # "RO_DB_ROOT_PASSWORD": "rootmanopw",
345 # "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw",
346 # "RO_DB_NAME": "mano_db",
347 # "RO_DB_OVIM_NAME": "mano_vim_db",
348 # "OPENMANO_TENANT": "osm",
349 # },
350 # "kubernetes": {
351 # "startupProbe": {
352 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
353 # "initialDelaySeconds": 60,
354 # "timeoutSeconds": 5,
355 # },
356 # "readinessProbe": {
357 # "httpGet": {
358 # "path": "/openmano/tenants",
359 # "port": 9090,
360 # },
361 # "periodSeconds": 10,
362 # "timeoutSeconds": 5,
363 # "successThreshold": 1,
364 # "failureThreshold": 3,
365 # },
366 # "livenessProbe": {
367 # "httpGet": {
368 # "path": "/openmano/tenants",
369 # "port": 9090,
370 # },
371 # "initialDelaySeconds": 600,
372 # "periodSeconds": 10,
373 # "timeoutSeconds": 5,
374 # "successThreshold": 1,
375 # "failureThreshold": 3,
376 # },
377 # },
378 # }
379 # ],
380 # "kubernetesResources": {"ingressResources": []},
381 # }
382
383 # self.harness.charm.on.start.emit()
384
385 # # Initializing the mysql relation
386 # relation_id = self.harness.add_relation("mysql", "mysql")
387 # self.harness.add_relation_unit(relation_id, "mysql/0")
388 # self.harness.update_relation_data(
389 # relation_id,
390 # "mysql/0",
391 # {
392 # "host": "mysql",
393 # "port": 3306,
394 # "user": "mano",
395 # "password": "manopw",
396 # "root_password": "rootmanopw",
397 # },
398 # )
399
400 # # Verifying status
401 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
402
403 # pod_spec, _ = self.harness.get_pod_spec()
404
405 # self.assertDictEqual(expected_result, pod_spec)
406
407 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
408 # """Test to see if kafka relation is updated."""
409 # self.harness.charm.on.start.emit()
410
411 # relation_id = self.harness.add_relation("kafka", "kafka")
412 # self.harness.add_relation_unit(relation_id, "kafka/0")
413 # self.harness.update_relation_data(
414 # relation_id,
415 # "kafka/0",
416 # {
417 # "host": "kafka",
418 # "port": 9090,
419 # },
420 # )
421
422 # # Verifying status
423 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
424
425 # # Verifying status message
426 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
427 # self.assertTrue(
428 # self.harness.charm.unit.status.message.startswith("Waiting for ")
429 # )
430 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
431 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
432
433 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
434 # """Test to see if mongodb relation is updated."""
435 # self.harness.charm.on.start.emit()
436
437 # relation_id = self.harness.add_relation("mongodb", "mongodb")
438 # self.harness.add_relation_unit(relation_id, "mongodb/0")
439 # self.harness.update_relation_data(
440 # relation_id,
441 # "mongodb/0",
442 # {
443 # "connection_string": "mongodb://mongo",
444 # },
445 # )
446
447 # # Verifying status
448 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
449
450 # # Verifying status message
451 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
452 # self.assertTrue(
453 # self.harness.charm.unit.status.message.startswith("Waiting for ")
454 # )
455 # self.assertIn("kafka", self.harness.charm.unit.status.message)
456 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
457
458 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
459 # """Test to see if mysql relation is updated."""
460 # self.harness.charm.on.start.emit()
461
462 # relation_id = self.harness.add_relation("mysql", "mysql")
463 # self.harness.add_relation_unit(relation_id, "mysql/0")
464 # self.harness.update_relation_data(
465 # relation_id,
466 # "mysql/0",
467 # {
468 # "host": "mysql",
469 # "port": 3306,
470 # "user": "mano",
471 # "password": "manopw",
472 # "root_password": "rootmanopw",
473 # },
474 # )
475
476 # # Verifying status
477 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
478
479 # # Verifying status message
480 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
481 # self.assertTrue(
482 # self.harness.charm.unit.status.message.startswith("Waiting for ")
483 # )
484 # self.assertIn("kafka", self.harness.charm.unit.status.message)
485 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
486 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
487
488 # def test_publish_ro_info(self) -> NoReturn:
489 # """Test to see if ro relation is updated."""
490 # expected_result = {
491 # "host": "ro",
492 # "port": "9090",
493 # }
494
495 # self.harness.charm.on.start.emit()
496
497 # relation_id = self.harness.add_relation("ro", "lcm")
498 # self.harness.add_relation_unit(relation_id, "lcm/0")
499 # relation_data = self.harness.get_relation_data(relation_id, "ro")
500
501 # self.assertDictEqual(expected_result, relation_data)
502
503
504 if __name__ == "__main__":
505 unittest.main()