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)
40 self
.harness
.update_config(self
.config
)
42 def test_on_start_without_relations(self
) -> NoReturn
:
43 """Test installation without any relation."""
44 self
.harness
.charm
.on
.start
.emit()
47 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
49 # Verifying status message
50 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
52 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
54 self
.assertIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
55 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
57 def test_on_start_with_relations_without_http(self
) -> NoReturn
:
58 """Test deployment."""
63 "name": "mongodb-exporter",
64 "imageDetails": self
.harness
.charm
.image
.fetch(),
65 "imagePullPolicy": "Always",
68 "name": "mongo-exporter",
69 "containerPort": 9216,
74 "MONGODB_URI": "mongodb://mongo",
79 "path": "/api/health",
82 "initialDelaySeconds": 10,
85 "successThreshold": 1,
86 "failureThreshold": 3,
90 "path": "/api/health",
93 "initialDelaySeconds": 60,
95 "failureThreshold": 10,
100 "kubernetesResources": {"ingressResources": []},
103 self
.harness
.charm
.on
.start
.emit()
105 # Initializing the mongodb relation
106 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
107 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
108 self
.harness
.update_relation_data(
112 "connection_string": "mongodb://mongo",
117 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
119 pod_spec
, _
= self
.harness
.get_pod_spec()
121 self
.assertDictEqual(expected_result
, pod_spec
)
123 def test_ingress_resources_with_http(self
) -> NoReturn
:
124 """Test ingress resources with HTTP."""
129 "name": "mongodb-exporter",
130 "imageDetails": self
.harness
.charm
.image
.fetch(),
131 "imagePullPolicy": "Always",
134 "name": "mongo-exporter",
135 "containerPort": 9216,
140 "MONGODB_URI": "mongodb://mongo",
145 "path": "/api/health",
148 "initialDelaySeconds": 10,
151 "successThreshold": 1,
152 "failureThreshold": 3,
156 "path": "/api/health",
159 "initialDelaySeconds": 60,
160 "timeoutSeconds": 30,
161 "failureThreshold": 10,
166 "kubernetesResources": {
167 "ingressResources": [
169 "name": "mongodb-exporter-ingress",
171 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
176 "host": "mongodb-exporter",
182 "serviceName": "mongodb-exporter",
196 self
.harness
.charm
.on
.start
.emit()
198 # Initializing the mongodb relation
199 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
200 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
201 self
.harness
.update_relation_data(
205 "connection_string": "mongodb://mongo",
209 self
.harness
.update_config({"site_url": "http://mongodb-exporter"})
211 pod_spec
, _
= self
.harness
.get_pod_spec()
213 self
.assertDictEqual(expected_result
, pod_spec
)
215 def test_ingress_resources_with_https(self
) -> NoReturn
:
216 """Test ingress resources with HTTPS."""
221 "name": "mongodb-exporter",
222 "imageDetails": self
.harness
.charm
.image
.fetch(),
223 "imagePullPolicy": "Always",
226 "name": "mongo-exporter",
227 "containerPort": 9216,
232 "MONGODB_URI": "mongodb://mongo",
237 "path": "/api/health",
240 "initialDelaySeconds": 10,
243 "successThreshold": 1,
244 "failureThreshold": 3,
248 "path": "/api/health",
251 "initialDelaySeconds": 60,
252 "timeoutSeconds": 30,
253 "failureThreshold": 10,
258 "kubernetesResources": {
259 "ingressResources": [
261 "name": "mongodb-exporter-ingress",
266 "host": "mongodb-exporter",
272 "serviceName": "mongodb-exporter",
282 "hosts": ["mongodb-exporter"],
283 "secretName": "mongodb-exporter",
292 self
.harness
.charm
.on
.start
.emit()
294 # Initializing the mongodb relation
295 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
296 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
297 self
.harness
.update_relation_data(
301 "connection_string": "mongodb://mongo",
305 self
.harness
.update_config(
307 "site_url": "https://mongodb-exporter",
308 "tls_secret_name": "mongodb-exporter",
312 pod_spec
, _
= self
.harness
.get_pod_spec()
314 self
.assertDictEqual(expected_result
, pod_spec
)
316 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
317 """Test ingress resources with HTTPS and ingress whitelist."""
322 "name": "mongodb-exporter",
323 "imageDetails": self
.harness
.charm
.image
.fetch(),
324 "imagePullPolicy": "Always",
327 "name": "mongo-exporter",
328 "containerPort": 9216,
333 "MONGODB_URI": "mongodb://mongo",
338 "path": "/api/health",
341 "initialDelaySeconds": 10,
344 "successThreshold": 1,
345 "failureThreshold": 3,
349 "path": "/api/health",
352 "initialDelaySeconds": 60,
353 "timeoutSeconds": 30,
354 "failureThreshold": 10,
359 "kubernetesResources": {
360 "ingressResources": [
362 "name": "mongodb-exporter-ingress",
364 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
369 "host": "mongodb-exporter",
375 "serviceName": "mongodb-exporter",
385 "hosts": ["mongodb-exporter"],
386 "secretName": "mongodb-exporter",
395 self
.harness
.charm
.on
.start
.emit()
397 # Initializing the mongodb relation
398 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
399 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
400 self
.harness
.update_relation_data(
404 "connection_string": "mongodb://mongo",
408 self
.harness
.update_config(
410 "site_url": "https://mongodb-exporter",
411 "tls_secret_name": "mongodb-exporter",
412 "ingress_whitelist_source_range": "0.0.0.0/0",
416 pod_spec
, _
= self
.harness
.get_pod_spec()
418 self
.assertDictEqual(expected_result
, pod_spec
)
420 def test_on_mongodb_unit_relation_changed(self
) -> NoReturn
:
421 """Test to see if mongodb relation is updated."""
422 self
.harness
.charm
.on
.start
.emit()
424 # Initializing the mongodb relation
425 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
426 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
427 self
.harness
.update_relation_data(
431 "connection_string": "mongodb://mongo",
436 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
438 def test_publish_scrape_info(self
) -> NoReturn
:
439 """Test to see if scrape relation is updated."""
441 "hostname": "mongodb-exporter",
443 "metrics_path": "/metrics",
444 "scrape_interval": "30s",
445 "scrape_timeout": "15s",
448 self
.harness
.charm
.on
.start
.emit()
450 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
451 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
452 relation_data
= self
.harness
.get_relation_data(
453 relation_id
, "mongodb-exporter/0"
456 self
.assertDictEqual(expected_result
, relation_data
)
458 def test_publish_scrape_info_with_site_url(self
) -> NoReturn
:
459 """Test to see if target relation is updated."""
461 "hostname": "mongodb-exporter-osm",
463 "metrics_path": "/metrics",
464 "scrape_interval": "30s",
465 "scrape_timeout": "15s",
468 self
.harness
.charm
.on
.start
.emit()
470 self
.harness
.update_config({"site_url": "http://mongodb-exporter-osm"})
472 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
473 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
474 relation_data
= self
.harness
.get_relation_data(
475 relation_id
, "mongodb-exporter/0"
478 self
.assertDictEqual(expected_result
, relation_data
)
480 def test_publish_dashboard_info(self
) -> NoReturn
:
481 """Test to see if dashboard relation is updated."""
482 self
.harness
.charm
.on
.start
.emit()
484 relation_id
= self
.harness
.add_relation("grafana-dashboard", "grafana")
485 self
.harness
.add_relation_unit(relation_id
, "grafana/0")
486 relation_data
= self
.harness
.get_relation_data(
487 relation_id
, "mongodb-exporter/0"
490 self
.assertEqual("osm-mongodb", relation_data
["name"])
491 self
.assertTrue("dashboard" in relation_data
)
492 self
.assertTrue(len(relation_data
["dashboard"]) > 0)
495 if __name__
== "__main__":