2 # Copyright 2020 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
24 from typing
import NoReturn
27 from charm
import GrafanaCharm
28 from ops
.model
import ActiveStatus
, BlockedStatus
29 from ops
.testing
import Harness
32 class TestCharm(unittest
.TestCase
):
33 """Prometheus Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
38 self
.harness
= Harness(GrafanaCharm
)
39 self
.harness
.set_leader(is_leader
=True)
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,
49 self
.harness
.update_config(self
.config
)
51 def test_config_changed(
54 """Test ingress resources without HTTP."""
56 self
.harness
.charm
.on
.config_changed
.emit()
59 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
60 self
.assertTrue("prometheus" in self
.harness
.charm
.unit
.status
.message
)
62 def test_config_changed_non_leader(
65 """Test ingress resources without HTTP."""
66 self
.harness
.set_leader(is_leader
=False)
67 self
.harness
.charm
.on
.config_changed
.emit()
70 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
72 def test_with_prometheus(
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(
81 {"hostname": "prometheus", "port": 9090},
85 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
88 if __name__
== "__main__":
91 # class TestCharm(unittest.TestCase):
92 # """Grafana Charm unit tests."""
94 # def setUp(self) -> NoReturn:
96 # self.harness = Harness(GrafanaCharm)
97 # self.harness.set_leader(is_leader=True)
98 # self.harness.begin()
100 # def test_on_start_without_relations(self) -> NoReturn:
101 # """Test installation without any relation."""
102 # self.harness.charm.on.start.emit()
105 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
107 # # Verifying status message
108 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
110 # self.harness.charm.unit.status.message.startswith("Waiting for ")
112 # self.assertIn("prometheus", self.harness.charm.unit.status.message)
113 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
115 # def test_on_start_with_relations_without_http(self) -> NoReturn:
116 # """Test deployment."""
117 # expected_result = {
122 # "imageDetails": self.harness.charm.image.fetch(),
123 # "imagePullPolicy": "Always",
127 # "containerPort": 3000,
134 # "name": "dashboards",
135 # "mountPath": "/etc/grafana/provisioning/dashboards/",
138 # "path": "dashboard-osm.yml",
147 # " path: /etc/grafana/provisioning/dashboards/\n"
153 # "name": "datasources",
154 # "mountPath": "/etc/grafana/provisioning/datasources/",
157 # "path": "datasource-prometheus.yml",
160 # " - access: proxy\n"
161 # " editable: true\n"
162 # " is_default: true\n"
163 # " name: osm_prometheus\n"
165 # " type: prometheus\n"
167 # " url: http://prometheus:9090\n"
174 # "readinessProbe": {
176 # "path": "/api/health",
179 # "initialDelaySeconds": 10,
180 # "periodSeconds": 10,
181 # "timeoutSeconds": 5,
182 # "successThreshold": 1,
183 # "failureThreshold": 3,
187 # "path": "/api/health",
190 # "initialDelaySeconds": 60,
191 # "timeoutSeconds": 30,
192 # "failureThreshold": 10,
197 # "kubernetesResources": {"ingressResources": []},
200 # self.harness.charm.on.start.emit()
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(
209 # "hostname": "prometheus",
215 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
217 # pod_spec, _ = self.harness.get_pod_spec()
219 # self.assertDictEqual(expected_result, pod_spec)
221 # def test_ingress_resources_with_http(self) -> NoReturn:
222 # """Test ingress resources with HTTP."""
223 # expected_result = {
228 # "imageDetails": self.harness.charm.image.fetch(),
229 # "imagePullPolicy": "Always",
233 # "containerPort": 3000,
240 # "name": "dashboards",
241 # "mountPath": "/etc/grafana/provisioning/dashboards/",
244 # "path": "dashboard-osm.yml",
253 # " path: /etc/grafana/provisioning/dashboards/\n"
259 # "name": "datasources",
260 # "mountPath": "/etc/grafana/provisioning/datasources/",
263 # "path": "datasource-prometheus.yml",
266 # " - access: proxy\n"
267 # " editable: true\n"
268 # " is_default: true\n"
269 # " name: osm_prometheus\n"
271 # " type: prometheus\n"
273 # " url: http://prometheus:9090\n"
280 # "readinessProbe": {
282 # "path": "/api/health",
285 # "initialDelaySeconds": 10,
286 # "periodSeconds": 10,
287 # "timeoutSeconds": 5,
288 # "successThreshold": 1,
289 # "failureThreshold": 3,
293 # "path": "/api/health",
296 # "initialDelaySeconds": 60,
297 # "timeoutSeconds": 30,
298 # "failureThreshold": 10,
303 # "kubernetesResources": {
304 # "ingressResources": [
306 # "name": "grafana-ingress",
308 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
309 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
320 # "serviceName": "grafana",
321 # "servicePort": 3000,
334 # self.harness.charm.on.start.emit()
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(
343 # "hostname": "prometheus",
348 # self.harness.update_config({"site_url": "http://grafana"})
350 # pod_spec, _ = self.harness.get_pod_spec()
352 # self.assertDictEqual(expected_result, pod_spec)
354 # def test_ingress_resources_with_https(self) -> NoReturn:
355 # """Test ingress resources with HTTPS."""
356 # expected_result = {
361 # "imageDetails": self.harness.charm.image.fetch(),
362 # "imagePullPolicy": "Always",
366 # "containerPort": 3000,
373 # "name": "dashboards",
374 # "mountPath": "/etc/grafana/provisioning/dashboards/",
377 # "path": "dashboard-osm.yml",
386 # " path: /etc/grafana/provisioning/dashboards/\n"
392 # "name": "datasources",
393 # "mountPath": "/etc/grafana/provisioning/datasources/",
396 # "path": "datasource-prometheus.yml",
399 # " - access: proxy\n"
400 # " editable: true\n"
401 # " is_default: true\n"
402 # " name: osm_prometheus\n"
404 # " type: prometheus\n"
406 # " url: http://prometheus:9090\n"
413 # "readinessProbe": {
415 # "path": "/api/health",
418 # "initialDelaySeconds": 10,
419 # "periodSeconds": 10,
420 # "timeoutSeconds": 5,
421 # "successThreshold": 1,
422 # "failureThreshold": 3,
426 # "path": "/api/health",
429 # "initialDelaySeconds": 60,
430 # "timeoutSeconds": 30,
431 # "failureThreshold": 10,
436 # "kubernetesResources": {
437 # "ingressResources": [
439 # "name": "grafana-ingress",
441 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
452 # "serviceName": "grafana",
453 # "servicePort": 3000,
460 # "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
467 # self.harness.charm.on.start.emit()
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(
476 # "hostname": "prometheus",
481 # self.harness.update_config(
482 # {"site_url": "https://grafana", "tls_secret_name": "grafana"}
485 # pod_spec, _ = self.harness.get_pod_spec()
487 # self.assertDictEqual(expected_result, pod_spec)
489 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
490 # """Test ingress resources with HTTPS and ingress whitelist."""
491 # expected_result = {
496 # "imageDetails": self.harness.charm.image.fetch(),
497 # "imagePullPolicy": "Always",
501 # "containerPort": 3000,
508 # "name": "dashboards",
509 # "mountPath": "/etc/grafana/provisioning/dashboards/",
512 # "path": "dashboard-osm.yml",
521 # " path: /etc/grafana/provisioning/dashboards/\n"
527 # "name": "datasources",
528 # "mountPath": "/etc/grafana/provisioning/datasources/",
531 # "path": "datasource-prometheus.yml",
534 # " - access: proxy\n"
535 # " editable: true\n"
536 # " is_default: true\n"
537 # " name: osm_prometheus\n"
539 # " type: prometheus\n"
541 # " url: http://prometheus:9090\n"
548 # "readinessProbe": {
550 # "path": "/api/health",
553 # "initialDelaySeconds": 10,
554 # "periodSeconds": 10,
555 # "timeoutSeconds": 5,
556 # "successThreshold": 1,
557 # "failureThreshold": 3,
561 # "path": "/api/health",
564 # "initialDelaySeconds": 60,
565 # "timeoutSeconds": 30,
566 # "failureThreshold": 10,
571 # "kubernetesResources": {
572 # "ingressResources": [
574 # "name": "grafana-ingress",
576 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
577 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
588 # "serviceName": "grafana",
589 # "servicePort": 3000,
596 # "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
603 # self.harness.charm.on.start.emit()
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(
612 # "hostname": "prometheus",
617 # self.harness.update_config(
619 # "site_url": "https://grafana",
620 # "tls_secret_name": "grafana",
621 # "ingress_whitelist_source_range": "0.0.0.0/0",
625 # pod_spec, _ = self.harness.get_pod_spec()
627 # self.assertDictEqual(expected_result, pod_spec)
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()
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(
638 # {"hostname": "prometheus", "port": 9090},
642 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
645 if __name__
== "__main__":