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