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 unittest
.mock
import patch
28 from charm
import GrafanaCharm
29 from ops
.model
import ActiveStatus
, BlockedStatus
30 from ops
.testing
import Harness
33 class TestCharm(unittest
.TestCase
):
34 """Prometheus Charm unit tests."""
36 def setUp(self
) -> NoReturn
:
38 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
39 self
.harness
= Harness(GrafanaCharm
)
40 self
.harness
.set_leader(is_leader
=True)
44 "ingress_whitelist_source_range": "",
45 "tls_secret_name": "",
46 "site_url": "https://grafana.192.168.100.100.nip.io",
47 "cluster_issuer": "vault-issuer",
48 "osm_dashboards": True,
50 self
.harness
.update_config(self
.config
)
52 def test_config_changed(
55 """Test ingress resources without HTTP."""
57 self
.harness
.charm
.on
.config_changed
.emit()
60 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
61 self
.assertTrue("prometheus" in self
.harness
.charm
.unit
.status
.message
)
63 def test_config_changed_non_leader(
66 """Test ingress resources without HTTP."""
67 self
.harness
.set_leader(is_leader
=False)
68 self
.harness
.charm
.on
.config_changed
.emit()
71 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
73 @patch("opslib.osm.interfaces.grafana.GrafanaCluster.set_initial_password")
74 def test_with_db_relation_and_prometheus(self
, _
) -> NoReturn
:
75 self
.initialize_prometheus_relation()
76 self
.initialize_mysql_relation()
77 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
79 @patch("opslib.osm.interfaces.grafana.GrafanaCluster.set_initial_password")
80 def test_with_db_config_and_prometheus(self
, _
) -> NoReturn
:
81 self
.initialize_prometheus_relation()
82 self
.initialize_mysql_config()
83 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
85 def test_with_prometheus(
88 """Test to see if prometheus relation is updated."""
89 self
.initialize_prometheus_relation()
91 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
93 def test_with_db_config(self
) -> NoReturn
:
94 "Test with mysql config"
95 self
.initialize_mysql_config()
97 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
99 @patch("opslib.osm.interfaces.grafana.GrafanaCluster.set_initial_password")
100 def test_with_db_relations(self
, _
) -> NoReturn
:
101 "Test with relations"
102 self
.initialize_mysql_relation()
104 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
106 def test_exception_db_relation_and_config(
109 "Test with relations and config. Must throw exception"
110 self
.initialize_mysql_config()
111 self
.initialize_mysql_relation()
113 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
115 def initialize_prometheus_relation(self
):
116 relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
117 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
118 self
.harness
.update_relation_data(
121 {"hostname": "prometheus", "port": 9090},
124 def initialize_mysql_config(self
):
125 self
.harness
.update_config(
126 {"mysql_uri": "mysql://grafana:$grafanapw$@host:3606/db"}
129 def initialize_mysql_relation(self
):
130 relation_id
= self
.harness
.add_relation("db", "mysql")
131 self
.harness
.add_relation_unit(relation_id
, "mysql/0")
132 self
.harness
.update_relation_data(
139 "password": "manopw",
140 "root_password": "rootmanopw",
145 if __name__
== "__main__":
148 # class TestCharm(unittest.TestCase):
149 # """Grafana Charm unit tests."""
151 # def setUp(self) -> NoReturn:
153 # self.harness = Harness(GrafanaCharm)
154 # self.harness.set_leader(is_leader=True)
155 # self.harness.begin()
157 # def test_on_start_without_relations(self) -> NoReturn:
158 # """Test installation without any relation."""
159 # self.harness.charm.on.start.emit()
162 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
164 # # Verifying status message
165 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
167 # self.harness.charm.unit.status.message.startswith("Waiting for ")
169 # self.assertIn("prometheus", self.harness.charm.unit.status.message)
170 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
172 # def test_on_start_with_relations_without_http(self) -> NoReturn:
173 # """Test deployment."""
174 # expected_result = {
179 # "imageDetails": self.harness.charm.image.fetch(),
180 # "imagePullPolicy": "Always",
184 # "containerPort": 3000,
191 # "name": "dashboards",
192 # "mountPath": "/etc/grafana/provisioning/dashboards/",
195 # "path": "dashboard-osm.yml",
204 # " path: /etc/grafana/provisioning/dashboards/\n"
210 # "name": "datasources",
211 # "mountPath": "/etc/grafana/provisioning/datasources/",
214 # "path": "datasource-prometheus.yml",
217 # " - access: proxy\n"
218 # " editable: true\n"
219 # " is_default: true\n"
220 # " name: osm_prometheus\n"
222 # " type: prometheus\n"
224 # " url: http://prometheus:9090\n"
231 # "readinessProbe": {
233 # "path": "/api/health",
236 # "initialDelaySeconds": 10,
237 # "periodSeconds": 10,
238 # "timeoutSeconds": 5,
239 # "successThreshold": 1,
240 # "failureThreshold": 3,
244 # "path": "/api/health",
247 # "initialDelaySeconds": 60,
248 # "timeoutSeconds": 30,
249 # "failureThreshold": 10,
254 # "kubernetesResources": {"ingressResources": []},
257 # self.harness.charm.on.start.emit()
259 # # Initializing the prometheus relation
260 # relation_id = self.harness.add_relation("prometheus", "prometheus")
261 # self.harness.add_relation_unit(relation_id, "prometheus/0")
262 # self.harness.update_relation_data(
266 # "hostname": "prometheus",
272 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
274 # pod_spec, _ = self.harness.get_pod_spec()
276 # self.assertDictEqual(expected_result, pod_spec)
278 # def test_ingress_resources_with_http(self) -> NoReturn:
279 # """Test ingress resources with HTTP."""
280 # expected_result = {
285 # "imageDetails": self.harness.charm.image.fetch(),
286 # "imagePullPolicy": "Always",
290 # "containerPort": 3000,
297 # "name": "dashboards",
298 # "mountPath": "/etc/grafana/provisioning/dashboards/",
301 # "path": "dashboard-osm.yml",
310 # " path: /etc/grafana/provisioning/dashboards/\n"
316 # "name": "datasources",
317 # "mountPath": "/etc/grafana/provisioning/datasources/",
320 # "path": "datasource-prometheus.yml",
323 # " - access: proxy\n"
324 # " editable: true\n"
325 # " is_default: true\n"
326 # " name: osm_prometheus\n"
328 # " type: prometheus\n"
330 # " url: http://prometheus:9090\n"
337 # "readinessProbe": {
339 # "path": "/api/health",
342 # "initialDelaySeconds": 10,
343 # "periodSeconds": 10,
344 # "timeoutSeconds": 5,
345 # "successThreshold": 1,
346 # "failureThreshold": 3,
350 # "path": "/api/health",
353 # "initialDelaySeconds": 60,
354 # "timeoutSeconds": 30,
355 # "failureThreshold": 10,
360 # "kubernetesResources": {
361 # "ingressResources": [
363 # "name": "grafana-ingress",
365 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
366 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
377 # "serviceName": "grafana",
378 # "servicePort": 3000,
391 # self.harness.charm.on.start.emit()
393 # # Initializing the prometheus relation
394 # relation_id = self.harness.add_relation("prometheus", "prometheus")
395 # self.harness.add_relation_unit(relation_id, "prometheus/0")
396 # self.harness.update_relation_data(
400 # "hostname": "prometheus",
405 # self.harness.update_config({"site_url": "http://grafana"})
407 # pod_spec, _ = self.harness.get_pod_spec()
409 # self.assertDictEqual(expected_result, pod_spec)
411 # def test_ingress_resources_with_https(self) -> NoReturn:
412 # """Test ingress resources with HTTPS."""
413 # expected_result = {
418 # "imageDetails": self.harness.charm.image.fetch(),
419 # "imagePullPolicy": "Always",
423 # "containerPort": 3000,
430 # "name": "dashboards",
431 # "mountPath": "/etc/grafana/provisioning/dashboards/",
434 # "path": "dashboard-osm.yml",
443 # " path: /etc/grafana/provisioning/dashboards/\n"
449 # "name": "datasources",
450 # "mountPath": "/etc/grafana/provisioning/datasources/",
453 # "path": "datasource-prometheus.yml",
456 # " - access: proxy\n"
457 # " editable: true\n"
458 # " is_default: true\n"
459 # " name: osm_prometheus\n"
461 # " type: prometheus\n"
463 # " url: http://prometheus:9090\n"
470 # "readinessProbe": {
472 # "path": "/api/health",
475 # "initialDelaySeconds": 10,
476 # "periodSeconds": 10,
477 # "timeoutSeconds": 5,
478 # "successThreshold": 1,
479 # "failureThreshold": 3,
483 # "path": "/api/health",
486 # "initialDelaySeconds": 60,
487 # "timeoutSeconds": 30,
488 # "failureThreshold": 10,
493 # "kubernetesResources": {
494 # "ingressResources": [
496 # "name": "grafana-ingress",
498 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
509 # "serviceName": "grafana",
510 # "servicePort": 3000,
517 # "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
524 # self.harness.charm.on.start.emit()
526 # # Initializing the prometheus relation
527 # relation_id = self.harness.add_relation("prometheus", "prometheus")
528 # self.harness.add_relation_unit(relation_id, "prometheus/0")
529 # self.harness.update_relation_data(
533 # "hostname": "prometheus",
538 # self.harness.update_config(
539 # {"site_url": "https://grafana", "tls_secret_name": "grafana"}
542 # pod_spec, _ = self.harness.get_pod_spec()
544 # self.assertDictEqual(expected_result, pod_spec)
546 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
547 # """Test ingress resources with HTTPS and ingress whitelist."""
548 # expected_result = {
553 # "imageDetails": self.harness.charm.image.fetch(),
554 # "imagePullPolicy": "Always",
558 # "containerPort": 3000,
565 # "name": "dashboards",
566 # "mountPath": "/etc/grafana/provisioning/dashboards/",
569 # "path": "dashboard-osm.yml",
578 # " path: /etc/grafana/provisioning/dashboards/\n"
584 # "name": "datasources",
585 # "mountPath": "/etc/grafana/provisioning/datasources/",
588 # "path": "datasource-prometheus.yml",
591 # " - access: proxy\n"
592 # " editable: true\n"
593 # " is_default: true\n"
594 # " name: osm_prometheus\n"
596 # " type: prometheus\n"
598 # " url: http://prometheus:9090\n"
605 # "readinessProbe": {
607 # "path": "/api/health",
610 # "initialDelaySeconds": 10,
611 # "periodSeconds": 10,
612 # "timeoutSeconds": 5,
613 # "successThreshold": 1,
614 # "failureThreshold": 3,
618 # "path": "/api/health",
621 # "initialDelaySeconds": 60,
622 # "timeoutSeconds": 30,
623 # "failureThreshold": 10,
628 # "kubernetesResources": {
629 # "ingressResources": [
631 # "name": "grafana-ingress",
633 # "nginx.ingress.kubernetes.io/proxy-body-size": "0",
634 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
645 # "serviceName": "grafana",
646 # "servicePort": 3000,
653 # "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
660 # self.harness.charm.on.start.emit()
662 # # Initializing the prometheus relation
663 # relation_id = self.harness.add_relation("prometheus", "prometheus")
664 # self.harness.add_relation_unit(relation_id, "prometheus/0")
665 # self.harness.update_relation_data(
669 # "hostname": "prometheus",
674 # self.harness.update_config(
676 # "site_url": "https://grafana",
677 # "tls_secret_name": "grafana",
678 # "ingress_whitelist_source_range": "0.0.0.0/0",
682 # pod_spec, _ = self.harness.get_pod_spec()
684 # self.assertDictEqual(expected_result, pod_spec)
686 # def test_on_prometheus_unit_relation_changed(self) -> NoReturn:
687 # """Test to see if prometheus relation is updated."""
688 # self.harness.charm.on.start.emit()
690 # relation_id = self.harness.add_relation("prometheus", "prometheus")
691 # self.harness.add_relation_unit(relation_id, "prometheus/0")
692 # self.harness.update_relation_data(
695 # {"hostname": "prometheus", "port": 9090},
699 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
702 if __name__
== "__main__":