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