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 MongodbExporterCharm
32 class TestCharm(unittest
.TestCase
):
33 """Mongodb Exporter Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(MongodbExporterCharm
)
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": "mongodb-exporter",
63 "imageDetails": self
.harness
.charm
.image
.fetch(),
64 "imagePullPolicy": "Always",
67 "name": "mongo-exporter",
68 "containerPort": 9216,
73 "MONGODB_URI": "mongodb://mongo",
78 "path": "/api/health",
81 "initialDelaySeconds": 10,
84 "successThreshold": 1,
85 "failureThreshold": 3,
89 "path": "/api/health",
92 "initialDelaySeconds": 60,
94 "failureThreshold": 10,
99 "kubernetesResources": {"ingressResources": []},
102 self
.harness
.charm
.on
.start
.emit()
104 # Initializing the mongodb relation
105 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
106 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
107 self
.harness
.update_relation_data(
111 "connection_string": "mongodb://mongo",
116 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
118 pod_spec
, _
= self
.harness
.get_pod_spec()
120 self
.assertDictEqual(expected_result
, pod_spec
)
122 def test_ingress_resources_with_http(self
) -> NoReturn
:
123 """Test ingress resources with HTTP."""
128 "name": "mongodb-exporter",
129 "imageDetails": self
.harness
.charm
.image
.fetch(),
130 "imagePullPolicy": "Always",
133 "name": "mongo-exporter",
134 "containerPort": 9216,
139 "MONGODB_URI": "mongodb://mongo",
144 "path": "/api/health",
147 "initialDelaySeconds": 10,
150 "successThreshold": 1,
151 "failureThreshold": 3,
155 "path": "/api/health",
158 "initialDelaySeconds": 60,
159 "timeoutSeconds": 30,
160 "failureThreshold": 10,
165 "kubernetesResources": {
166 "ingressResources": [
168 "name": "mongodb-exporter-ingress",
170 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
175 "host": "mongodb-exporter",
181 "serviceName": "mongodb-exporter",
195 self
.harness
.charm
.on
.start
.emit()
197 # Initializing the mongodb relation
198 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
199 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
200 self
.harness
.update_relation_data(
204 "connection_string": "mongodb://mongo",
208 self
.harness
.update_config({"site_url": "http://mongodb-exporter"})
210 pod_spec
, _
= self
.harness
.get_pod_spec()
212 self
.assertDictEqual(expected_result
, pod_spec
)
214 def test_ingress_resources_with_https(self
) -> NoReturn
:
215 """Test ingress resources with HTTPS."""
220 "name": "mongodb-exporter",
221 "imageDetails": self
.harness
.charm
.image
.fetch(),
222 "imagePullPolicy": "Always",
225 "name": "mongo-exporter",
226 "containerPort": 9216,
231 "MONGODB_URI": "mongodb://mongo",
236 "path": "/api/health",
239 "initialDelaySeconds": 10,
242 "successThreshold": 1,
243 "failureThreshold": 3,
247 "path": "/api/health",
250 "initialDelaySeconds": 60,
251 "timeoutSeconds": 30,
252 "failureThreshold": 10,
257 "kubernetesResources": {
258 "ingressResources": [
260 "name": "mongodb-exporter-ingress",
265 "host": "mongodb-exporter",
271 "serviceName": "mongodb-exporter",
281 "hosts": ["mongodb-exporter"],
282 "secretName": "mongodb-exporter",
291 self
.harness
.charm
.on
.start
.emit()
293 # Initializing the mongodb relation
294 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
295 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
296 self
.harness
.update_relation_data(
300 "connection_string": "mongodb://mongo",
304 self
.harness
.update_config(
306 "site_url": "https://mongodb-exporter",
307 "tls_secret_name": "mongodb-exporter",
311 pod_spec
, _
= self
.harness
.get_pod_spec()
313 self
.assertDictEqual(expected_result
, pod_spec
)
315 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
316 """Test ingress resources with HTTPS and ingress whitelist."""
321 "name": "mongodb-exporter",
322 "imageDetails": self
.harness
.charm
.image
.fetch(),
323 "imagePullPolicy": "Always",
326 "name": "mongo-exporter",
327 "containerPort": 9216,
332 "MONGODB_URI": "mongodb://mongo",
337 "path": "/api/health",
340 "initialDelaySeconds": 10,
343 "successThreshold": 1,
344 "failureThreshold": 3,
348 "path": "/api/health",
351 "initialDelaySeconds": 60,
352 "timeoutSeconds": 30,
353 "failureThreshold": 10,
358 "kubernetesResources": {
359 "ingressResources": [
361 "name": "mongodb-exporter-ingress",
363 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
368 "host": "mongodb-exporter",
374 "serviceName": "mongodb-exporter",
384 "hosts": ["mongodb-exporter"],
385 "secretName": "mongodb-exporter",
394 self
.harness
.charm
.on
.start
.emit()
396 # Initializing the mongodb relation
397 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
398 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
399 self
.harness
.update_relation_data(
403 "connection_string": "mongodb://mongo",
407 self
.harness
.update_config(
409 "site_url": "https://mongodb-exporter",
410 "tls_secret_name": "mongodb-exporter",
411 "ingress_whitelist_source_range": "0.0.0.0/0",
415 pod_spec
, _
= self
.harness
.get_pod_spec()
417 self
.assertDictEqual(expected_result
, pod_spec
)
419 def test_on_mongodb_unit_relation_changed(self
) -> NoReturn
:
420 """Test to see if mongodb relation is updated."""
421 self
.harness
.charm
.on
.start
.emit()
423 # Initializing the mongodb relation
424 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
425 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
426 self
.harness
.update_relation_data(
430 "connection_string": "mongodb://mongo",
435 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
437 def test_publish_scrape_info(self
) -> NoReturn
:
438 """Test to see if scrape relation is updated."""
440 "hostname": "mongodb-exporter",
442 "metrics_path": "/metrics",
443 "scrape_interval": "30s",
444 "scrape_timeout": "15s",
447 self
.harness
.charm
.on
.start
.emit()
449 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
450 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
451 relation_data
= self
.harness
.get_relation_data(
452 relation_id
, "mongodb-exporter/0"
455 self
.assertDictEqual(expected_result
, relation_data
)
457 def test_publish_scrape_info_with_site_url(self
) -> NoReturn
:
458 """Test to see if target relation is updated."""
460 "hostname": "mongodb-exporter-osm",
462 "metrics_path": "/metrics",
463 "scrape_interval": "30s",
464 "scrape_timeout": "15s",
467 self
.harness
.charm
.on
.start
.emit()
469 self
.harness
.update_config({"site_url": "http://mongodb-exporter-osm"})
471 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
472 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
473 relation_data
= self
.harness
.get_relation_data(
474 relation_id
, "mongodb-exporter/0"
477 self
.assertDictEqual(expected_result
, relation_data
)
479 def test_publish_dashboard_info(self
) -> NoReturn
:
480 """Test to see if dashboard relation is updated."""
481 self
.harness
.charm
.on
.start
.emit()
483 relation_id
= self
.harness
.add_relation("grafana-dashboard", "grafana")
484 self
.harness
.add_relation_unit(relation_id
, "grafana/0")
485 relation_data
= self
.harness
.get_relation_data(
486 relation_id
, "mongodb-exporter/0"
489 self
.assertEqual("osm-mongodb", relation_data
["name"])
490 self
.assertTrue("dashboard" in relation_data
)
491 self
.assertTrue(len(relation_data
["dashboard"]) > 0)
494 if __name__
== "__main__":