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 PrometheusMongodbExporterCharm
32 class TestCharm(unittest
.TestCase
):
33 """Prometheus Mongodb Exporter Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(PrometheusMongodbExporterCharm
)
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("mongodb", 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."""
62 "name": "prometheus-mongodb-exporter",
63 "imageDetails": self
.harness
.charm
.image
.fetch(),
64 "imagePullPolicy": "Always",
67 "name": "prometheus-mongodb-exporter",
68 "containerPort": 9216,
74 "mongodb_exporter_linux_amd64/mongodb_exporter",
75 "--mongodbdsn=mongodb://mongo",
80 "path": "/api/health",
83 "initialDelaySeconds": 10,
86 "successThreshold": 1,
87 "failureThreshold": 3,
91 "path": "/api/health",
94 "initialDelaySeconds": 60,
96 "failureThreshold": 10,
101 "kubernetesResources": {"ingressResources": []},
104 self
.harness
.charm
.on
.start
.emit()
106 # Initializing the mongodb relation
107 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
108 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
109 self
.harness
.update_relation_data(
113 "connection_string": "mongodb://mongo",
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-mongodb-exporter",
131 "imageDetails": self
.harness
.charm
.image
.fetch(),
132 "imagePullPolicy": "Always",
135 "name": "prometheus-mongodb-exporter",
136 "containerPort": 9216,
142 "mongodb_exporter_linux_amd64/mongodb_exporter",
143 "--mongodbdsn=mongodb://mongo",
148 "path": "/api/health",
151 "initialDelaySeconds": 10,
154 "successThreshold": 1,
155 "failureThreshold": 3,
159 "path": "/api/health",
162 "initialDelaySeconds": 60,
163 "timeoutSeconds": 30,
164 "failureThreshold": 10,
169 "kubernetesResources": {
170 "ingressResources": [
172 "name": "prometheus-mongodb-exporter-ingress",
174 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
179 "host": "prometheus-mongodb-exporter",
185 "serviceName": "prometheus-mongodb-exporter",
199 self
.harness
.charm
.on
.start
.emit()
201 # Initializing the mongodb relation
202 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
203 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
204 self
.harness
.update_relation_data(
208 "connection_string": "mongodb://mongo",
212 self
.harness
.update_config({"site_url": "http://prometheus-mongodb-exporter"})
214 pod_spec
, _
= self
.harness
.get_pod_spec()
216 self
.assertDictEqual(expected_result
, pod_spec
)
218 def test_ingress_resources_with_https(self
) -> NoReturn
:
219 """Test ingress resources with HTTPS."""
224 "name": "prometheus-mongodb-exporter",
225 "imageDetails": self
.harness
.charm
.image
.fetch(),
226 "imagePullPolicy": "Always",
229 "name": "prometheus-mongodb-exporter",
230 "containerPort": 9216,
236 "mongodb_exporter_linux_amd64/mongodb_exporter",
237 "--mongodbdsn=mongodb://mongo",
242 "path": "/api/health",
245 "initialDelaySeconds": 10,
248 "successThreshold": 1,
249 "failureThreshold": 3,
253 "path": "/api/health",
256 "initialDelaySeconds": 60,
257 "timeoutSeconds": 30,
258 "failureThreshold": 10,
263 "kubernetesResources": {
264 "ingressResources": [
266 "name": "prometheus-mongodb-exporter-ingress",
271 "host": "prometheus-mongodb-exporter",
277 "serviceName": "prometheus-mongodb-exporter",
287 "hosts": ["prometheus-mongodb-exporter"],
288 "secretName": "prometheus-mongodb-exporter",
297 self
.harness
.charm
.on
.start
.emit()
299 # Initializing the mongodb relation
300 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
301 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
302 self
.harness
.update_relation_data(
306 "connection_string": "mongodb://mongo",
310 self
.harness
.update_config(
312 "site_url": "https://prometheus-mongodb-exporter",
313 "tls_secret_name": "prometheus-mongodb-exporter",
317 pod_spec
, _
= self
.harness
.get_pod_spec()
319 self
.assertDictEqual(expected_result
, pod_spec
)
321 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
322 """Test ingress resources with HTTPS and ingress whitelist."""
327 "name": "prometheus-mongodb-exporter",
328 "imageDetails": self
.harness
.charm
.image
.fetch(),
329 "imagePullPolicy": "Always",
332 "name": "prometheus-mongodb-exporter",
333 "containerPort": 9216,
339 "mongodb_exporter_linux_amd64/mongodb_exporter",
340 "--mongodbdsn=mongodb://mongo",
345 "path": "/api/health",
348 "initialDelaySeconds": 10,
351 "successThreshold": 1,
352 "failureThreshold": 3,
356 "path": "/api/health",
359 "initialDelaySeconds": 60,
360 "timeoutSeconds": 30,
361 "failureThreshold": 10,
366 "kubernetesResources": {
367 "ingressResources": [
369 "name": "prometheus-mongodb-exporter-ingress",
371 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
376 "host": "prometheus-mongodb-exporter",
382 "serviceName": "prometheus-mongodb-exporter",
392 "hosts": ["prometheus-mongodb-exporter"],
393 "secretName": "prometheus-mongodb-exporter",
402 self
.harness
.charm
.on
.start
.emit()
404 # Initializing the mongodb relation
405 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
406 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
407 self
.harness
.update_relation_data(
411 "connection_string": "mongodb://mongo",
415 self
.harness
.update_config(
417 "site_url": "https://prometheus-mongodb-exporter",
418 "tls_secret_name": "prometheus-mongodb-exporter",
419 "ingress_whitelist_source_range": "0.0.0.0/0",
423 pod_spec
, _
= self
.harness
.get_pod_spec()
425 self
.assertDictEqual(expected_result
, pod_spec
)
427 def test_on_mongodb_unit_relation_changed(self
) -> NoReturn
:
428 """Test to see if mongodb relation is updated."""
429 self
.harness
.charm
.on
.start
.emit()
431 # Initializing the mongodb relation
432 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
433 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
434 self
.harness
.update_relation_data(
438 "connection_string": "mongodb://mongo",
443 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
445 def test_publish_target_info(self
) -> NoReturn
:
446 """Test to see if target relation is updated."""
448 "hostname": "prometheus-mongodb-exporter",
452 self
.harness
.charm
.on
.start
.emit()
454 relation_id
= self
.harness
.add_relation("prometheus-target", "prometheus")
455 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
456 relation_data
= self
.harness
.get_relation_data(
457 relation_id
, "prometheus-mongodb-exporter/0"
460 self
.assertDictEqual(expected_result
, relation_data
)
462 def test_publish_dashboard_info(self
) -> NoReturn
:
463 """Test to see if dashboard relation is updated."""
464 self
.harness
.charm
.on
.start
.emit()
466 relation_id
= self
.harness
.add_relation("grafana-dashboard", "grafana")
467 self
.harness
.add_relation_unit(relation_id
, "grafana/0")
468 relation_data
= self
.harness
.get_relation_data(
469 relation_id
, "prometheus-mongodb-exporter/0"
472 self
.assertTrue("dashboard" in relation_data
)
473 self
.assertTrue(len(relation_data
["dashboard"]) > 0)
476 if __name__
== "__main__":