2 # Copyright 2021 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
23 from typing
import NoReturn
26 from ops
.model
import BlockedStatus
27 from ops
.testing
import Harness
29 from charm
import GrafanaCharm
32 class TestCharm(unittest
.TestCase
):
33 """Grafana Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(GrafanaCharm
)
38 self
.harness
.set_leader(is_leader
=True)
41 def test_on_start_without_relations(self
) -> NoReturn
:
42 """Test installation without any relation."""
43 self
.harness
.charm
.on
.start
.emit()
46 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
48 # Verifying status message
49 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
51 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
53 self
.assertIn("prometheus", self
.harness
.charm
.unit
.status
.message
)
54 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
56 def test_on_start_with_relations_without_http(self
) -> NoReturn
:
57 """Test deployment."""
63 "imageDetails": self
.harness
.charm
.image
.fetch(),
64 "imagePullPolicy": "Always",
68 "containerPort": 3000,
76 "mountPath": "/etc/grafana/provisioning/dashboards/",
79 "path": "dashboard-osm.yml",
88 " path: /etc/grafana/provisioning/dashboards/\n"
94 "name": "datasources",
95 "mountPath": "/etc/grafana/provisioning/datasources/",
98 "path": "datasource-prometheus.yml",
103 " is_default: true\n"
104 " name: osm_prometheus\n"
106 " type: prometheus\n"
108 " url: http://prometheus:9090\n"
117 "path": "/api/health",
120 "initialDelaySeconds": 10,
123 "successThreshold": 1,
124 "failureThreshold": 3,
128 "path": "/api/health",
131 "initialDelaySeconds": 60,
132 "timeoutSeconds": 30,
133 "failureThreshold": 10,
138 "kubernetesResources": {"ingressResources": []},
141 self
.harness
.charm
.on
.start
.emit()
143 # Initializing the prometheus relation
144 relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
145 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
146 self
.harness
.update_relation_data(
150 "host": "prometheus",
156 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
158 pod_spec
, _
= self
.harness
.get_pod_spec()
160 self
.assertDictEqual(expected_result
, pod_spec
)
162 def test_ingress_resources_with_http(self
) -> NoReturn
:
163 """Test ingress resources with HTTP."""
169 "imageDetails": self
.harness
.charm
.image
.fetch(),
170 "imagePullPolicy": "Always",
174 "containerPort": 3000,
181 "name": "dashboards",
182 "mountPath": "/etc/grafana/provisioning/dashboards/",
185 "path": "dashboard-osm.yml",
194 " path: /etc/grafana/provisioning/dashboards/\n"
200 "name": "datasources",
201 "mountPath": "/etc/grafana/provisioning/datasources/",
204 "path": "datasource-prometheus.yml",
209 " is_default: true\n"
210 " name: osm_prometheus\n"
212 " type: prometheus\n"
214 " url: http://prometheus:9090\n"
223 "path": "/api/health",
226 "initialDelaySeconds": 10,
229 "successThreshold": 1,
230 "failureThreshold": 3,
234 "path": "/api/health",
237 "initialDelaySeconds": 60,
238 "timeoutSeconds": 30,
239 "failureThreshold": 10,
244 "kubernetesResources": {
245 "ingressResources": [
247 "name": "grafana-ingress",
249 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
250 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
261 "serviceName": "grafana",
275 self
.harness
.charm
.on
.start
.emit()
277 # Initializing the prometheus relation
278 relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
279 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
280 self
.harness
.update_relation_data(
284 "host": "prometheus",
289 self
.harness
.update_config({"site_url": "http://grafana"})
291 pod_spec
, _
= self
.harness
.get_pod_spec()
293 self
.assertDictEqual(expected_result
, pod_spec
)
295 def test_ingress_resources_with_https(self
) -> NoReturn
:
296 """Test ingress resources with HTTPS."""
302 "imageDetails": self
.harness
.charm
.image
.fetch(),
303 "imagePullPolicy": "Always",
307 "containerPort": 3000,
314 "name": "dashboards",
315 "mountPath": "/etc/grafana/provisioning/dashboards/",
318 "path": "dashboard-osm.yml",
327 " path: /etc/grafana/provisioning/dashboards/\n"
333 "name": "datasources",
334 "mountPath": "/etc/grafana/provisioning/datasources/",
337 "path": "datasource-prometheus.yml",
342 " is_default: true\n"
343 " name: osm_prometheus\n"
345 " type: prometheus\n"
347 " url: http://prometheus:9090\n"
356 "path": "/api/health",
359 "initialDelaySeconds": 10,
362 "successThreshold": 1,
363 "failureThreshold": 3,
367 "path": "/api/health",
370 "initialDelaySeconds": 60,
371 "timeoutSeconds": 30,
372 "failureThreshold": 10,
377 "kubernetesResources": {
378 "ingressResources": [
380 "name": "grafana-ingress",
382 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
393 "serviceName": "grafana",
401 "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
408 self
.harness
.charm
.on
.start
.emit()
410 # Initializing the prometheus relation
411 relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
412 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
413 self
.harness
.update_relation_data(
417 "host": "prometheus",
422 self
.harness
.update_config(
423 {"site_url": "https://grafana", "tls_secret_name": "grafana"}
426 pod_spec
, _
= self
.harness
.get_pod_spec()
428 self
.assertDictEqual(expected_result
, pod_spec
)
430 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
431 """Test ingress resources with HTTPS and ingress whitelist."""
437 "imageDetails": self
.harness
.charm
.image
.fetch(),
438 "imagePullPolicy": "Always",
442 "containerPort": 3000,
449 "name": "dashboards",
450 "mountPath": "/etc/grafana/provisioning/dashboards/",
453 "path": "dashboard-osm.yml",
462 " path: /etc/grafana/provisioning/dashboards/\n"
468 "name": "datasources",
469 "mountPath": "/etc/grafana/provisioning/datasources/",
472 "path": "datasource-prometheus.yml",
477 " is_default: true\n"
478 " name: osm_prometheus\n"
480 " type: prometheus\n"
482 " url: http://prometheus:9090\n"
491 "path": "/api/health",
494 "initialDelaySeconds": 10,
497 "successThreshold": 1,
498 "failureThreshold": 3,
502 "path": "/api/health",
505 "initialDelaySeconds": 60,
506 "timeoutSeconds": 30,
507 "failureThreshold": 10,
512 "kubernetesResources": {
513 "ingressResources": [
515 "name": "grafana-ingress",
517 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
518 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
529 "serviceName": "grafana",
537 "tls": [{"hosts": ["grafana"], "secretName": "grafana"}],
544 self
.harness
.charm
.on
.start
.emit()
546 # Initializing the prometheus relation
547 relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
548 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
549 self
.harness
.update_relation_data(
553 "host": "prometheus",
558 self
.harness
.update_config(
560 "site_url": "https://grafana",
561 "tls_secret_name": "grafana",
562 "ingress_whitelist_source_range": "0.0.0.0/0",
566 pod_spec
, _
= self
.harness
.get_pod_spec()
568 self
.assertDictEqual(expected_result
, pod_spec
)
570 def test_on_prometheus_unit_relation_changed(self
) -> NoReturn
:
571 """Test to see if prometheus relation is updated."""
572 self
.harness
.charm
.on
.start
.emit()
574 relation_id
= self
.harness
.add_relation("prometheus", "prometheus")
575 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
576 self
.harness
.update_relation_data(
580 "host": "prometheus",
586 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
589 if __name__
== "__main__":