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
26 from ops
.model
import ActiveStatus
, BlockedStatus
27 from ops
.testing
import Harness
29 from charm
import GrafanaCharm
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.xip.io",
46 "osm_dashboards": True,
48 self
.harness
.update_config(self
.config
)
50 def test_config_changed(
53 """Test ingress resources without HTTP."""
55 self
.harness
.charm
.on
.config_changed
.emit()
58 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
59 self
.assertTrue("prometheus" in self
.harness
.charm
.unit
.status
.message
)
61 def test_config_changed_non_leader(
64 """Test ingress resources without HTTP."""
65 self
.harness
.set_leader(is_leader
=False)
66 self
.harness
.charm
.on
.config_changed
.emit()
69 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
71 def test_with_prometheus(
74 """Test to see if prometheus relation is updated."""
75 relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
76 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
77 self
.harness
.update_relation_data(
80 {"hostname": "prometheus", "port": 9090},
84 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
87 if __name__
== "__main__":
90 # class TestCharm(unittest.TestCase):
91 # """Grafana Charm unit tests."""
93 # def setUp(self) -> NoReturn:
95 # self.harness = Harness(GrafanaCharm)
96 # self.harness.set_leader(is_leader=True)
97 # self.harness.begin()
99 # def test_on_start_without_relations(self) -> NoReturn:
100 # """Test installation without any relation."""
101 # self.harness.charm.on.start.emit()
104 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
106 # # Verifying status message
107 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
109 # self.harness.charm.unit.status.message.startswith("Waiting for ")
111 # self.assertIn("prometheus", self.harness.charm.unit.status.message)
112 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
114 # def test_on_start_with_relations_without_http(self) -> NoReturn:
115 # """Test deployment."""
116 # expected_result = {
121 # "imageDetails": self.harness.charm.image.fetch(),
122 # "imagePullPolicy": "Always",
126 # "containerPort": 3000,
133 # "name": "dashboards",
134 # "mountPath": "/etc/grafana/provisioning/dashboards/",
137 # "path": "dashboard-osm.yml",
146 # " path: /etc/grafana/provisioning/dashboards/\n"
152 # "name": "datasources",
153 # "mountPath": "/etc/grafana/provisioning/datasources/",
156 # "path": "datasource-prometheus.yml",
159 # " - access: proxy\n"
160 # " editable: true\n"
161 # " is_default: true\n"
162 # " name: osm_prometheus\n"
164 # " type: prometheus\n"
166 # " url: http://prometheus:9090\n"
173 # "readinessProbe": {
175 # "path": "/api/health",
178 # "initialDelaySeconds": 10,
179 # "periodSeconds": 10,
180 # "timeoutSeconds": 5,
181 # "successThreshold": 1,
182 # "failureThreshold": 3,
186 # "path": "/api/health",
189 # "initialDelaySeconds": 60,
190 # "timeoutSeconds": 30,
191 # "failureThreshold": 10,
196 # "kubernetesResources": {"ingressResources": []},
199 # self.harness.charm.on.start.emit()
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(
208 # "hostname": "prometheus",
214 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
216 # pod_spec, _ = self.harness.get_pod_spec()
218 # self.assertDictEqual(expected_result, pod_spec)
220 # def test_ingress_resources_with_http(self) -> NoReturn:
221 # """Test ingress resources with HTTP."""
222 # expected_result = {
227 # "imageDetails": self.harness.charm.image.fetch(),
228 # "imagePullPolicy": "Always",
232 # "containerPort": 3000,
239 # "name": "dashboards",
240 # "mountPath": "/etc/grafana/provisioning/dashboards/",
243 # "path": "dashboard-osm.yml",
252 # " path: /etc/grafana/provisioning/dashboards/\n"
258 # "name": "datasources",
259 # "mountPath": "/etc/grafana/provisioning/datasources/",
262 # "path": "datasource-prometheus.yml",
265 # " - access: proxy\n"
266 # " editable: true\n"
267 # " is_default: true\n"
268 # " name: osm_prometheus\n"
270 # " type: prometheus\n"
272 # " url: http://prometheus:9090\n"
279 # "readinessProbe": {
281 # "path": "/api/health",
284 # "initialDelaySeconds": 10,
285 # "periodSeconds": 10,
286 # "timeoutSeconds": 5,
287 # "successThreshold": 1,
288 # "failureThreshold": 3,
292 # "path": "/api/health",
295 # "initialDelaySeconds": 60,
296 # "timeoutSeconds": 30,
297 # "failureThreshold": 10,
302 # "kubernetesResources": {
303 # "ingressResources": [
305 # "name": "grafana-ingress",
307 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
308 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
319 # "serviceName": "grafana",
320 # "servicePort": 3000,
333 # self.harness.charm.on.start.emit()
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(
342 # "hostname": "prometheus",
347 # self.harness.update_config({"site_url": "http://grafana"})
349 # pod_spec, _ = self.harness.get_pod_spec()
351 # self.assertDictEqual(expected_result, pod_spec)
353 # def test_ingress_resources_with_https(self) -> NoReturn:
354 # """Test ingress resources with HTTPS."""
355 # expected_result = {
360 # "imageDetails": self.harness.charm.image.fetch(),
361 # "imagePullPolicy": "Always",
365 # "containerPort": 3000,
372 # "name": "dashboards",
373 # "mountPath": "/etc/grafana/provisioning/dashboards/",
376 # "path": "dashboard-osm.yml",
385 # " path: /etc/grafana/provisioning/dashboards/\n"
391 # "name": "datasources",
392 # "mountPath": "/etc/grafana/provisioning/datasources/",
395 # "path": "datasource-prometheus.yml",
398 # " - access: proxy\n"
399 # " editable: true\n"
400 # " is_default: true\n"
401 # " name: osm_prometheus\n"
403 # " type: prometheus\n"
405 # " url: http://prometheus:9090\n"
412 # "readinessProbe": {
414 # "path": "/api/health",
417 # "initialDelaySeconds": 10,
418 # "periodSeconds": 10,
419 # "timeoutSeconds": 5,
420 # "successThreshold": 1,
421 # "failureThreshold": 3,
425 # "path": "/api/health",
428 # "initialDelaySeconds": 60,
429 # "timeoutSeconds": 30,
430 # "failureThreshold": 10,
435 # "kubernetesResources": {
436 # "ingressResources": [
438 # "name": "grafana-ingress",
440 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
451 # "serviceName": "grafana",
452 # "servicePort": 3000,
459 # "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
466 # self.harness.charm.on.start.emit()
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(
475 # "hostname": "prometheus",
480 # self.harness.update_config(
481 # {"site_url": "https://grafana", "tls_secret_name": "grafana"}
484 # pod_spec, _ = self.harness.get_pod_spec()
486 # self.assertDictEqual(expected_result, pod_spec)
488 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
489 # """Test ingress resources with HTTPS and ingress whitelist."""
490 # expected_result = {
495 # "imageDetails": self.harness.charm.image.fetch(),
496 # "imagePullPolicy": "Always",
500 # "containerPort": 3000,
507 # "name": "dashboards",
508 # "mountPath": "/etc/grafana/provisioning/dashboards/",
511 # "path": "dashboard-osm.yml",
520 # " path: /etc/grafana/provisioning/dashboards/\n"
526 # "name": "datasources",
527 # "mountPath": "/etc/grafana/provisioning/datasources/",
530 # "path": "datasource-prometheus.yml",
533 # " - access: proxy\n"
534 # " editable: true\n"
535 # " is_default: true\n"
536 # " name: osm_prometheus\n"
538 # " type: prometheus\n"
540 # " url: http://prometheus:9090\n"
547 # "readinessProbe": {
549 # "path": "/api/health",
552 # "initialDelaySeconds": 10,
553 # "periodSeconds": 10,
554 # "timeoutSeconds": 5,
555 # "successThreshold": 1,
556 # "failureThreshold": 3,
560 # "path": "/api/health",
563 # "initialDelaySeconds": 60,
564 # "timeoutSeconds": 30,
565 # "failureThreshold": 10,
570 # "kubernetesResources": {
571 # "ingressResources": [
573 # "name": "grafana-ingress",
575 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
576 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
587 # "serviceName": "grafana",
588 # "servicePort": 3000,
595 # "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
602 # self.harness.charm.on.start.emit()
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(
611 # "hostname": "prometheus",
616 # self.harness.update_config(
618 # "site_url": "https://grafana",
619 # "tls_secret_name": "grafana",
620 # "ingress_whitelist_source_range": "0.0.0.0/0",
624 # pod_spec, _ = self.harness.get_pod_spec()
626 # self.assertDictEqual(expected_result, pod_spec)
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()
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(
637 # {"hostname": "prometheus", "port": 9090},
641 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
644 if __name__
== "__main__":