Moving exporter charms to use opslib
[osm/devops.git] / installers / charm / mysqld-exporter / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2021 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 sys
24 from typing import NoReturn
25 import unittest
26
27 from charm import MysqlExporterCharm
28 from ops.model import ActiveStatus, BlockedStatus
29 from ops.testing import Harness
30
31
32 class TestCharm(unittest.TestCase):
33 """Mysql Exporter Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
38 self.harness = Harness(MysqlExporterCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
41 self.config = {
42 "ingress_whitelist_source_range": "",
43 "tls_secret_name": "",
44 "site_url": "https://mysql-exporter.192.168.100.100.nip.io",
45 "cluster_issuer": "vault-issuer",
46 }
47 self.harness.update_config(self.config)
48
49 def test_config_changed_no_relations(
50 self,
51 ) -> NoReturn:
52 """Test ingress resources without HTTP."""
53
54 self.harness.charm.on.config_changed.emit()
55
56 # Assertions
57 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
58 print(self.harness.charm.unit.status.message)
59 self.assertTrue(
60 all(
61 relation in self.harness.charm.unit.status.message
62 for relation in ["mysql"]
63 )
64 )
65
66 def test_config_changed_non_leader(
67 self,
68 ) -> NoReturn:
69 """Test ingress resources without HTTP."""
70 self.harness.set_leader(is_leader=False)
71 self.harness.charm.on.config_changed.emit()
72
73 # Assertions
74 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
75
76 def test_with_relations(
77 self,
78 ) -> NoReturn:
79 "Test with relations"
80 self.initialize_mysql_relation()
81
82 # Verifying status
83 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
84
85 def test_with_config(
86 self,
87 ) -> NoReturn:
88 "Test with config"
89 self.initialize_mysql_relation()
90
91 # Verifying status
92 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
93
94 def test_mysql_exception_relation_and_config(
95 self,
96 ) -> NoReturn:
97 self.initialize_mysql_config()
98 self.initialize_mysql_relation()
99
100 # Verifying status
101 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
102
103 def initialize_mysql_relation(self):
104 mongodb_relation_id = self.harness.add_relation("mysql", "mysql")
105 self.harness.add_relation_unit(mongodb_relation_id, "mysql/0")
106 self.harness.update_relation_data(
107 mongodb_relation_id,
108 "mysql/0",
109 {
110 "user": "user",
111 "password": "pass",
112 "host": "host",
113 "port": "1234",
114 "database": "pol",
115 "root_password": "root_password",
116 },
117 )
118
119 def initialize_mysql_config(self):
120 self.harness.update_config({"mysql_uri": "mysql://user:pass@mysql-host:3306"})
121
122
123 if __name__ == "__main__":
124 unittest.main()
125
126
127 # class TestCharm(unittest.TestCase):
128 # """Mysql Exporter Charm unit tests."""
129 #
130 # def setUp(self) -> NoReturn:
131 # """Test setup"""
132 # self.harness = Harness(MysqldExporterCharm)
133 # self.harness.set_leader(is_leader=True)
134 # self.harness.begin()
135 #
136 # def test_on_start_without_relations(self) -> NoReturn:
137 # """Test installation without any relation."""
138 # self.harness.charm.on.start.emit()
139 #
140 # # Verifying status
141 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
142 #
143 # # Verifying status message
144 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
145 # self.assertTrue(
146 # self.harness.charm.unit.status.message.startswith("Waiting for ")
147 # )
148 # self.assertIn("mysql", self.harness.charm.unit.status.message)
149 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
150 #
151 # def test_on_start_with_relations_without_http(self) -> NoReturn:
152 # """Test deployment."""
153 # expected_result = {
154 # "version": 3,
155 # "containers": [
156 # {
157 # "name": "mysqld-exporter",
158 # "imageDetails": self.harness.charm.image.fetch(),
159 # "imagePullPolicy": "Always",
160 # "ports": [
161 # {
162 # "name": "mysqld-exporter",
163 # "containerPort": 9104,
164 # "protocol": "TCP",
165 # }
166 # ],
167 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
168 # "kubernetes": {
169 # "readinessProbe": {
170 # "httpGet": {
171 # "path": "/api/health",
172 # "port": 9104,
173 # },
174 # "initialDelaySeconds": 10,
175 # "periodSeconds": 10,
176 # "timeoutSeconds": 5,
177 # "successThreshold": 1,
178 # "failureThreshold": 3,
179 # },
180 # "livenessProbe": {
181 # "httpGet": {
182 # "path": "/api/health",
183 # "port": 9104,
184 # },
185 # "initialDelaySeconds": 60,
186 # "timeoutSeconds": 30,
187 # "failureThreshold": 10,
188 # },
189 # },
190 # },
191 # ],
192 # "kubernetesResources": {"ingressResources": []},
193 # }
194 #
195 # self.harness.charm.on.start.emit()
196 #
197 # # Initializing the mysql relation
198 # relation_id = self.harness.add_relation("mysql", "mysql")
199 # self.harness.add_relation_unit(relation_id, "mysql/0")
200 # self.harness.update_relation_data(
201 # relation_id,
202 # "mysql/0",
203 # {
204 # "host": "mysql",
205 # "port": "3306",
206 # "user": "mano",
207 # "password": "manopw",
208 # "root_password": "rootpw",
209 # },
210 # )
211 #
212 # # Verifying status
213 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
214 #
215 # pod_spec, _ = self.harness.get_pod_spec()
216 #
217 # self.assertDictEqual(expected_result, pod_spec)
218 #
219 # def test_ingress_resources_with_http(self) -> NoReturn:
220 # """Test ingress resources with HTTP."""
221 # expected_result = {
222 # "version": 3,
223 # "containers": [
224 # {
225 # "name": "mysqld-exporter",
226 # "imageDetails": self.harness.charm.image.fetch(),
227 # "imagePullPolicy": "Always",
228 # "ports": [
229 # {
230 # "name": "mysqld-exporter",
231 # "containerPort": 9104,
232 # "protocol": "TCP",
233 # }
234 # ],
235 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
236 # "kubernetes": {
237 # "readinessProbe": {
238 # "httpGet": {
239 # "path": "/api/health",
240 # "port": 9104,
241 # },
242 # "initialDelaySeconds": 10,
243 # "periodSeconds": 10,
244 # "timeoutSeconds": 5,
245 # "successThreshold": 1,
246 # "failureThreshold": 3,
247 # },
248 # "livenessProbe": {
249 # "httpGet": {
250 # "path": "/api/health",
251 # "port": 9104,
252 # },
253 # "initialDelaySeconds": 60,
254 # "timeoutSeconds": 30,
255 # "failureThreshold": 10,
256 # },
257 # },
258 # },
259 # ],
260 # "kubernetesResources": {
261 # "ingressResources": [
262 # {
263 # "name": "mysqld-exporter-ingress",
264 # "annotations": {
265 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
266 # },
267 # "spec": {
268 # "rules": [
269 # {
270 # "host": "mysqld-exporter",
271 # "http": {
272 # "paths": [
273 # {
274 # "path": "/",
275 # "backend": {
276 # "serviceName": "mysqld-exporter",
277 # "servicePort": 9104,
278 # },
279 # }
280 # ]
281 # },
282 # }
283 # ]
284 # },
285 # }
286 # ],
287 # },
288 # }
289 #
290 # self.harness.charm.on.start.emit()
291 #
292 # # Initializing the mysql relation
293 # relation_id = self.harness.add_relation("mysql", "mysql")
294 # self.harness.add_relation_unit(relation_id, "mysql/0")
295 # self.harness.update_relation_data(
296 # relation_id,
297 # "mysql/0",
298 # {
299 # "host": "mysql",
300 # "port": "3306",
301 # "user": "mano",
302 # "password": "manopw",
303 # "root_password": "rootpw",
304 # },
305 # )
306 #
307 # self.harness.update_config({"site_url": "http://mysqld-exporter"})
308 #
309 # pod_spec, _ = self.harness.get_pod_spec()
310 #
311 # self.assertDictEqual(expected_result, pod_spec)
312 #
313 # def test_ingress_resources_with_https(self) -> NoReturn:
314 # """Test ingress resources with HTTPS."""
315 # expected_result = {
316 # "version": 3,
317 # "containers": [
318 # {
319 # "name": "mysqld-exporter",
320 # "imageDetails": self.harness.charm.image.fetch(),
321 # "imagePullPolicy": "Always",
322 # "ports": [
323 # {
324 # "name": "mysqld-exporter",
325 # "containerPort": 9104,
326 # "protocol": "TCP",
327 # }
328 # ],
329 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
330 # "kubernetes": {
331 # "readinessProbe": {
332 # "httpGet": {
333 # "path": "/api/health",
334 # "port": 9104,
335 # },
336 # "initialDelaySeconds": 10,
337 # "periodSeconds": 10,
338 # "timeoutSeconds": 5,
339 # "successThreshold": 1,
340 # "failureThreshold": 3,
341 # },
342 # "livenessProbe": {
343 # "httpGet": {
344 # "path": "/api/health",
345 # "port": 9104,
346 # },
347 # "initialDelaySeconds": 60,
348 # "timeoutSeconds": 30,
349 # "failureThreshold": 10,
350 # },
351 # },
352 # },
353 # ],
354 # "kubernetesResources": {
355 # "ingressResources": [
356 # {
357 # "name": "mysqld-exporter-ingress",
358 # "annotations": {},
359 # "spec": {
360 # "rules": [
361 # {
362 # "host": "mysqld-exporter",
363 # "http": {
364 # "paths": [
365 # {
366 # "path": "/",
367 # "backend": {
368 # "serviceName": "mysqld-exporter",
369 # "servicePort": 9104,
370 # },
371 # }
372 # ]
373 # },
374 # }
375 # ],
376 # "tls": [
377 # {
378 # "hosts": ["mysqld-exporter"],
379 # "secretName": "mysqld-exporter",
380 # }
381 # ],
382 # },
383 # }
384 # ],
385 # },
386 # }
387 #
388 # self.harness.charm.on.start.emit()
389 #
390 # # Initializing the mysql relation
391 # relation_id = self.harness.add_relation("mysql", "mysql")
392 # self.harness.add_relation_unit(relation_id, "mysql/0")
393 # self.harness.update_relation_data(
394 # relation_id,
395 # "mysql/0",
396 # {
397 # "host": "mysql",
398 # "port": "3306",
399 # "user": "mano",
400 # "password": "manopw",
401 # "root_password": "rootpw",
402 # },
403 # )
404 #
405 # self.harness.update_config(
406 # {
407 # "site_url": "https://mysqld-exporter",
408 # "tls_secret_name": "mysqld-exporter",
409 # }
410 # )
411 #
412 # pod_spec, _ = self.harness.get_pod_spec()
413 #
414 # self.assertDictEqual(expected_result, pod_spec)
415 #
416 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
417 # """Test ingress resources with HTTPS and ingress whitelist."""
418 # expected_result = {
419 # "version": 3,
420 # "containers": [
421 # {
422 # "name": "mysqld-exporter",
423 # "imageDetails": self.harness.charm.image.fetch(),
424 # "imagePullPolicy": "Always",
425 # "ports": [
426 # {
427 # "name": "mysqld-exporter",
428 # "containerPort": 9104,
429 # "protocol": "TCP",
430 # }
431 # ],
432 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
433 # "kubernetes": {
434 # "readinessProbe": {
435 # "httpGet": {
436 # "path": "/api/health",
437 # "port": 9104,
438 # },
439 # "initialDelaySeconds": 10,
440 # "periodSeconds": 10,
441 # "timeoutSeconds": 5,
442 # "successThreshold": 1,
443 # "failureThreshold": 3,
444 # },
445 # "livenessProbe": {
446 # "httpGet": {
447 # "path": "/api/health",
448 # "port": 9104,
449 # },
450 # "initialDelaySeconds": 60,
451 # "timeoutSeconds": 30,
452 # "failureThreshold": 10,
453 # },
454 # },
455 # },
456 # ],
457 # "kubernetesResources": {
458 # "ingressResources": [
459 # {
460 # "name": "mysqld-exporter-ingress",
461 # "annotations": {
462 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
463 # },
464 # "spec": {
465 # "rules": [
466 # {
467 # "host": "mysqld-exporter",
468 # "http": {
469 # "paths": [
470 # {
471 # "path": "/",
472 # "backend": {
473 # "serviceName": "mysqld-exporter",
474 # "servicePort": 9104,
475 # },
476 # }
477 # ]
478 # },
479 # }
480 # ],
481 # "tls": [
482 # {
483 # "hosts": ["mysqld-exporter"],
484 # "secretName": "mysqld-exporter",
485 # }
486 # ],
487 # },
488 # }
489 # ],
490 # },
491 # }
492 #
493 # self.harness.charm.on.start.emit()
494 #
495 # # Initializing the mysql relation
496 # relation_id = self.harness.add_relation("mysql", "mysql")
497 # self.harness.add_relation_unit(relation_id, "mysql/0")
498 # self.harness.update_relation_data(
499 # relation_id,
500 # "mysql/0",
501 # {
502 # "host": "mysql",
503 # "port": "3306",
504 # "user": "mano",
505 # "password": "manopw",
506 # "root_password": "rootpw",
507 # },
508 # )
509 #
510 # self.harness.update_config(
511 # {
512 # "site_url": "https://mysqld-exporter",
513 # "tls_secret_name": "mysqld-exporter",
514 # "ingress_whitelist_source_range": "0.0.0.0/0",
515 # }
516 # )
517 #
518 # pod_spec, _ = self.harness.get_pod_spec()
519 #
520 # self.assertDictEqual(expected_result, pod_spec)
521 #
522 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
523 # """Test to see if mysql relation is updated."""
524 # self.harness.charm.on.start.emit()
525 #
526 # relation_id = self.harness.add_relation("mysql", "mysql")
527 # self.harness.add_relation_unit(relation_id, "mysql/0")
528 # self.harness.update_relation_data(
529 # relation_id,
530 # "mysql/0",
531 # {
532 # "host": "mysql",
533 # "port": "3306",
534 # "user": "mano",
535 # "password": "manopw",
536 # "root_password": "rootpw",
537 # },
538 # )
539 #
540 # # Verifying status
541 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
542 #
543 # def test_publish_target_info(self) -> NoReturn:
544 # """Test to see if target relation is updated."""
545 # expected_result = {
546 # "hostname": "mysqld-exporter",
547 # "port": "9104",
548 # "metrics_path": "/metrics",
549 # "scrape_interval": "30s",
550 # "scrape_timeout": "15s",
551 # }
552 #
553 # self.harness.charm.on.start.emit()
554 #
555 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
556 # self.harness.add_relation_unit(relation_id, "prometheus/0")
557 # relation_data = self.harness.get_relation_data(relation_id, "mysqld-exporter/0")
558 #
559 # self.assertDictEqual(expected_result, relation_data)
560 #
561 # def test_publish_scrape_info_with_site_url(self) -> NoReturn:
562 # """Test to see if target relation is updated."""
563 # expected_result = {
564 # "hostname": "mysqld-exporter-osm",
565 # "port": "80",
566 # "metrics_path": "/metrics",
567 # "scrape_interval": "30s",
568 # "scrape_timeout": "15s",
569 # }
570 #
571 # self.harness.charm.on.start.emit()
572 #
573 # self.harness.update_config({"site_url": "http://mysqld-exporter-osm"})
574 #
575 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
576 # self.harness.add_relation_unit(relation_id, "prometheus/0")
577 # relation_data = self.harness.get_relation_data(relation_id, "mysqld-exporter/0")
578 #
579 # self.assertDictEqual(expected_result, relation_data)
580 #
581 # def test_publish_dashboard_info(self) -> NoReturn:
582 # """Test to see if dashboard relation is updated."""
583 # self.harness.charm.on.start.emit()
584 #
585 # relation_id = self.harness.add_relation("grafana-dashboard", "grafana")
586 # self.harness.add_relation_unit(relation_id, "grafana/0")
587 # relation_data = self.harness.get_relation_data(relation_id, "mysqld-exporter/0")
588 #
589 # self.assertTrue("dashboard" in relation_data)
590 # self.assertTrue(len(relation_data["dashboard"]) > 0)
591 # self.assertEqual(relation_data["name"], "osm-mysql")
592 #
593 #
594 # if __name__ == "__main__":
595 # unittest.main()