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\n"
68 " evaluation_interval: 15s\n"
71 " - static_configs:\n"
75 " - job_name: 'prometheus'\n"
77 " - targets: [{}]\n".format("")
85 "--config.file=/etc/prometheus/prometheus.yml",
86 "--storage.tsdb.path=/prometheus",
87 "--web.console.libraries=/usr/share/prometheus/console_libraries",
88 "--web.console.templates=/usr/share/prometheus/consoles",
89 "--web.route-prefix={}".format("/"),
90 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
98 "initialDelaySeconds": 10,
103 "path": "/-/healthy",
106 "initialDelaySeconds": 30,
112 "kubernetesResources": {"ingressResources": []},
115 self
.harness
.charm
.on
.start
.emit()
118 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
120 pod_spec
, _
= self
.harness
.get_pod_spec()
122 self
.assertDictEqual(expected_result
, pod_spec
)
124 def test_ingress_resources_with_http(self
) -> NoReturn
:
125 """Test ingress resources with HTTP."""
130 "name": "prometheus",
131 "imageDetails": self
.harness
.charm
.image
.fetch(),
132 "imagePullPolicy": "Always",
135 "name": "prometheus",
136 "containerPort": 9090,
144 "mountPath": "/etc/prometheus",
147 "path": "prometheus.yml",
150 " scrape_interval: 15s\n"
151 " evaluation_interval: 15s\n"
154 " - static_configs:\n"
158 " - job_name: 'prometheus'\n"
160 " - targets: [{}]\n".format("")
168 "--config.file=/etc/prometheus/prometheus.yml",
169 "--storage.tsdb.path=/prometheus",
170 "--web.console.libraries=/usr/share/prometheus/console_libraries",
171 "--web.console.templates=/usr/share/prometheus/consoles",
172 "--web.route-prefix={}".format("/"),
173 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
181 "initialDelaySeconds": 10,
182 "timeoutSeconds": 30,
186 "path": "/-/healthy",
189 "initialDelaySeconds": 30,
195 "kubernetesResources": {
196 "ingressResources": [
198 "name": "prometheus-ingress",
200 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
201 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
206 "host": "prometheus",
212 "serviceName": "prometheus",
226 self
.harness
.charm
.on
.start
.emit()
228 self
.harness
.update_config({"site_url": "http://prometheus"})
230 pod_spec
, _
= self
.harness
.get_pod_spec()
232 self
.assertDictEqual(expected_result
, pod_spec
)
234 def test_ingress_resources_with_https(self
) -> NoReturn
:
235 """Test ingress resources with HTTPS."""
240 "name": "prometheus",
241 "imageDetails": self
.harness
.charm
.image
.fetch(),
242 "imagePullPolicy": "Always",
245 "name": "prometheus",
246 "containerPort": 9090,
254 "mountPath": "/etc/prometheus",
257 "path": "prometheus.yml",
260 " scrape_interval: 15s\n"
261 " evaluation_interval: 15s\n"
264 " - static_configs:\n"
268 " - job_name: 'prometheus'\n"
270 " - targets: [{}]\n".format("")
278 "--config.file=/etc/prometheus/prometheus.yml",
279 "--storage.tsdb.path=/prometheus",
280 "--web.console.libraries=/usr/share/prometheus/console_libraries",
281 "--web.console.templates=/usr/share/prometheus/consoles",
282 "--web.route-prefix={}".format("/"),
283 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
291 "initialDelaySeconds": 10,
292 "timeoutSeconds": 30,
296 "path": "/-/healthy",
299 "initialDelaySeconds": 30,
305 "kubernetesResources": {
306 "ingressResources": [
308 "name": "prometheus-ingress",
310 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
315 "host": "prometheus",
321 "serviceName": "prometheus",
330 {"hosts": ["prometheus"], "secretName": "prometheus"}
338 self
.harness
.charm
.on
.start
.emit()
340 self
.harness
.update_config(
341 {"site_url": "https://prometheus", "tls_secret_name": "prometheus"}
344 pod_spec
, _
= self
.harness
.get_pod_spec()
346 self
.assertDictEqual(expected_result
, pod_spec
)
348 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
349 """Test ingress resources with HTTPS and ingress whitelist."""
354 "name": "prometheus",
355 "imageDetails": self
.harness
.charm
.image
.fetch(),
356 "imagePullPolicy": "Always",
359 "name": "prometheus",
360 "containerPort": 9090,
368 "mountPath": "/etc/prometheus",
371 "path": "prometheus.yml",
374 " scrape_interval: 15s\n"
375 " evaluation_interval: 15s\n"
378 " - static_configs:\n"
382 " - job_name: 'prometheus'\n"
384 " - targets: [{}]\n".format("")
392 "--config.file=/etc/prometheus/prometheus.yml",
393 "--storage.tsdb.path=/prometheus",
394 "--web.console.libraries=/usr/share/prometheus/console_libraries",
395 "--web.console.templates=/usr/share/prometheus/consoles",
396 "--web.route-prefix={}".format("/"),
397 "--web.external-url=http://localhost:{}{}".format(9090, "/"),
405 "initialDelaySeconds": 10,
406 "timeoutSeconds": 30,
410 "path": "/-/healthy",
413 "initialDelaySeconds": 30,
419 "kubernetesResources": {
420 "ingressResources": [
422 "name": "prometheus-ingress",
424 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
425 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
430 "host": "prometheus",
436 "serviceName": "prometheus",
445 {"hosts": ["prometheus"], "secretName": "prometheus"}
453 self
.harness
.charm
.on
.start
.emit()
455 self
.harness
.update_config(
457 "site_url": "https://prometheus",
458 "tls_secret_name": "prometheus",
459 "ingress_whitelist_source_range": "0.0.0.0/0",
463 pod_spec
, _
= self
.harness
.get_pod_spec()
465 self
.assertDictEqual(expected_result
, pod_spec
)
467 def test_publish_prometheus_info(self
) -> NoReturn
:
468 """Test to see if prometheus relation is updated."""
470 "host": "prometheus",
474 self
.harness
.charm
.on
.start
.emit()
476 relation_id
= self
.harness
.add_relation("prometheus", "mon")
477 self
.harness
.add_relation_unit(relation_id
, "mon/0")
478 relation_data
= self
.harness
.get_relation_data(relation_id
, "prometheus")
480 self
.assertDictEqual(expected_result
, relation_data
)
483 if __name__
== "__main__":