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
23 from typing
import NoReturn
26 from ops
.model
import BlockedStatus
27 from ops
.testing
import Harness
29 from charm
import PrometheusCharm
32 class TestCharm(unittest
.TestCase
):
33 """Prometheus Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(PrometheusCharm
)
38 self
.harness
.set_leader(is_leader
=True)
41 def test_ingress_resources_without_http(self
) -> NoReturn
:
42 """Test ingress resources without HTTP."""
48 "imageDetails": self
.harness
.charm
.image
.fetch(),
49 "imagePullPolicy": "Always",
53 "containerPort": 9090,
61 "mountPath": "/etc/prometheus",
64 "path": "prometheus.yml",
67 " scrape_interval: 15s"
68 " evaluation_interval: 15s"
75 " - job_name: 'prometheus'"
77 " - targets: [{}]".format("")
87 "--config.file=/etc/prometheus/prometheus.yml",
88 "--storage.tsdb.path=/prometheus",
89 "--web.console.libraries=/usr/share/prometheus/console_libraries",
90 "--web.console.templates=/usr/share/prometheus/consoles",
91 "--web.route-prefix={}".format("/"),
92 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
100 "initialDelaySeconds": 10,
101 "timeoutSeconds": 30,
105 "path": "/-/healthy",
108 "initialDelaySeconds": 30,
114 "kubernetesResources": {"ingressResources": []},
117 self
.harness
.charm
.on
.start
.emit()
120 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
122 pod_spec
, _
= self
.harness
.get_pod_spec()
124 self
.assertDictEqual(expected_result
, pod_spec
)
126 def test_ingress_resources_with_http(self
) -> NoReturn
:
127 """Test ingress resources with HTTP."""
132 "name": "prometheus",
133 "imageDetails": self
.harness
.charm
.image
.fetch(),
134 "imagePullPolicy": "Always",
137 "name": "prometheus",
138 "containerPort": 9090,
146 "mountPath": "/etc/prometheus",
149 "path": "prometheus.yml",
152 " scrape_interval: 15s"
153 " evaluation_interval: 15s"
160 " - job_name: 'prometheus'"
162 " - targets: [{}]".format("")
172 "--config.file=/etc/prometheus/prometheus.yml",
173 "--storage.tsdb.path=/prometheus",
174 "--web.console.libraries=/usr/share/prometheus/console_libraries",
175 "--web.console.templates=/usr/share/prometheus/consoles",
176 "--web.route-prefix={}".format("/"),
177 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
185 "initialDelaySeconds": 10,
186 "timeoutSeconds": 30,
190 "path": "/-/healthy",
193 "initialDelaySeconds": 30,
199 "kubernetesResources": {
200 "ingressResources": [
202 "name": "prometheus-ingress",
204 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
205 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
210 "host": "prometheus",
216 "serviceName": "prometheus",
230 self
.harness
.charm
.on
.start
.emit()
232 self
.harness
.update_config({"site_url": "http://prometheus"})
234 pod_spec
, _
= self
.harness
.get_pod_spec()
236 self
.assertDictEqual(expected_result
, pod_spec
)
238 def test_ingress_resources_with_https(self
) -> NoReturn
:
239 """Test ingress resources with HTTPS."""
244 "name": "prometheus",
245 "imageDetails": self
.harness
.charm
.image
.fetch(),
246 "imagePullPolicy": "Always",
249 "name": "prometheus",
250 "containerPort": 9090,
258 "mountPath": "/etc/prometheus",
261 "path": "prometheus.yml",
264 " scrape_interval: 15s"
265 " evaluation_interval: 15s"
272 " - job_name: 'prometheus'"
274 " - targets: [{}]".format("")
284 "--config.file=/etc/prometheus/prometheus.yml",
285 "--storage.tsdb.path=/prometheus",
286 "--web.console.libraries=/usr/share/prometheus/console_libraries",
287 "--web.console.templates=/usr/share/prometheus/consoles",
288 "--web.route-prefix={}".format("/"),
289 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
297 "initialDelaySeconds": 10,
298 "timeoutSeconds": 30,
302 "path": "/-/healthy",
305 "initialDelaySeconds": 30,
311 "kubernetesResources": {
312 "ingressResources": [
314 "name": "prometheus-ingress",
316 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
321 "host": "prometheus",
327 "serviceName": "prometheus",
336 {"hosts": ["prometheus"], "secretName": "prometheus"}
344 self
.harness
.charm
.on
.start
.emit()
346 self
.harness
.update_config(
347 {"site_url": "https://prometheus", "tls_secret_name": "prometheus"}
350 pod_spec
, _
= self
.harness
.get_pod_spec()
352 self
.assertDictEqual(expected_result
, pod_spec
)
354 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
355 """Test ingress resources with HTTPS and ingress whitelist."""
360 "name": "prometheus",
361 "imageDetails": self
.harness
.charm
.image
.fetch(),
362 "imagePullPolicy": "Always",
365 "name": "prometheus",
366 "containerPort": 9090,
374 "mountPath": "/etc/prometheus",
377 "path": "prometheus.yml",
380 " scrape_interval: 15s"
381 " evaluation_interval: 15s"
388 " - job_name: 'prometheus'"
390 " - targets: [{}]".format("")
400 "--config.file=/etc/prometheus/prometheus.yml",
401 "--storage.tsdb.path=/prometheus",
402 "--web.console.libraries=/usr/share/prometheus/console_libraries",
403 "--web.console.templates=/usr/share/prometheus/consoles",
404 "--web.route-prefix={}".format("/"),
405 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
413 "initialDelaySeconds": 10,
414 "timeoutSeconds": 30,
418 "path": "/-/healthy",
421 "initialDelaySeconds": 30,
427 "kubernetesResources": {
428 "ingressResources": [
430 "name": "prometheus-ingress",
432 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
433 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
438 "host": "prometheus",
444 "serviceName": "prometheus",
453 {"hosts": ["prometheus"], "secretName": "prometheus"}
461 self
.harness
.charm
.on
.start
.emit()
463 self
.harness
.update_config(
465 "site_url": "https://prometheus",
466 "tls_secret_name": "prometheus",
467 "ingress_whitelist_source_range": "0.0.0.0/0",
471 pod_spec
, _
= self
.harness
.get_pod_spec()
473 self
.assertDictEqual(expected_result
, pod_spec
)
475 def test_publish_prometheus_info(self
) -> NoReturn
:
476 """Test to see if prometheus relation is updated."""
478 "host": "prometheus",
482 self
.harness
.charm
.on
.start
.emit()
484 relation_id
= self
.harness
.add_relation("prometheus", "mon")
485 self
.harness
.add_relation_unit(relation_id
, "mon/0")
486 relation_data
= self
.harness
.get_relation_data(relation_id
, "prometheus")
488 self
.assertDictEqual(expected_result
, relation_data
)
491 if __name__
== "__main__":