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
24 from typing
import NoReturn
27 from charm
import MongodbExporterCharm
28 from ops
.model
import ActiveStatus
, BlockedStatus
29 from ops
.testing
import Harness
32 class TestCharm(unittest
.TestCase
):
33 """Mongodb Exporter Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
38 self
.harness
= Harness(MongodbExporterCharm
)
39 self
.harness
.set_leader(is_leader
=True)
42 "ingress_whitelist_source_range": "",
43 "tls_secret_name": "",
44 "site_url": "https://mongodb-exporter.192.168.100.100.nip.io",
45 "cluster_issuer": "vault-issuer",
47 self
.harness
.update_config(self
.config
)
49 def test_config_changed_no_relations(
52 """Test ingress resources without HTTP."""
54 self
.harness
.charm
.on
.config_changed
.emit()
57 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
58 print(self
.harness
.charm
.unit
.status
.message
)
61 relation
in self
.harness
.charm
.unit
.status
.message
62 for relation
in ["mongodb"]
66 def test_config_changed_non_leader(
69 """Test ingress resources without HTTP."""
70 self
.harness
.set_leader(is_leader
=False)
71 self
.harness
.charm
.on
.config_changed
.emit()
74 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
76 def test_with_relations(
80 self
.initialize_mongo_relation()
83 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
89 self
.initialize_mongo_relation()
92 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
94 def test_mongodb_exception_relation_and_config(
97 self
.initialize_mongo_config()
98 self
.initialize_mongo_relation()
101 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
103 def initialize_mongo_relation(self
):
104 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
105 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
106 self
.harness
.update_relation_data(
109 {"connection_string": "mongodb://mongo:27017"},
112 def initialize_mongo_config(self
):
113 self
.harness
.update_config({"mongodb_uri": "mongodb://mongo:27017"})
116 if __name__
== "__main__":
120 # class TestCharm(unittest.TestCase):
121 # """Mongodb Exporter Charm unit tests."""
123 # def setUp(self) -> NoReturn:
125 # self.harness = Harness(MongodbExporterCharm)
126 # self.harness.set_leader(is_leader=True)
127 # self.harness.begin()
129 # def test_on_start_without_relations(self) -> NoReturn:
130 # """Test installation without any relation."""
131 # self.harness.charm.on.start.emit()
134 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
136 # # Verifying status message
137 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
139 # self.harness.charm.unit.status.message.startswith("Waiting for ")
141 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
142 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
144 # def test_on_start_with_relations_without_http(self) -> NoReturn:
145 # """Test deployment."""
146 # expected_result = {
150 # "name": "mongodb-exporter",
151 # "imageDetails": self.harness.charm.image.fetch(),
152 # "imagePullPolicy": "Always",
155 # "name": "mongo-exporter",
156 # "containerPort": 9216,
161 # "MONGODB_URI": "mongodb://mongo",
164 # "readinessProbe": {
166 # "path": "/api/health",
169 # "initialDelaySeconds": 10,
170 # "periodSeconds": 10,
171 # "timeoutSeconds": 5,
172 # "successThreshold": 1,
173 # "failureThreshold": 3,
177 # "path": "/api/health",
180 # "initialDelaySeconds": 60,
181 # "timeoutSeconds": 30,
182 # "failureThreshold": 10,
187 # "kubernetesResources": {"ingressResources": []},
190 # self.harness.charm.on.start.emit()
192 # # Initializing the mongodb relation
193 # relation_id = self.harness.add_relation("mongodb", "mongodb")
194 # self.harness.add_relation_unit(relation_id, "mongodb/0")
195 # self.harness.update_relation_data(
199 # "connection_string": "mongodb://mongo",
204 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
206 # pod_spec, _ = self.harness.get_pod_spec()
208 # self.assertDictEqual(expected_result, pod_spec)
210 # def test_ingress_resources_with_http(self) -> NoReturn:
211 # """Test ingress resources with HTTP."""
212 # expected_result = {
216 # "name": "mongodb-exporter",
217 # "imageDetails": self.harness.charm.image.fetch(),
218 # "imagePullPolicy": "Always",
221 # "name": "mongo-exporter",
222 # "containerPort": 9216,
227 # "MONGODB_URI": "mongodb://mongo",
230 # "readinessProbe": {
232 # "path": "/api/health",
235 # "initialDelaySeconds": 10,
236 # "periodSeconds": 10,
237 # "timeoutSeconds": 5,
238 # "successThreshold": 1,
239 # "failureThreshold": 3,
243 # "path": "/api/health",
246 # "initialDelaySeconds": 60,
247 # "timeoutSeconds": 30,
248 # "failureThreshold": 10,
253 # "kubernetesResources": {
254 # "ingressResources": [
256 # "name": "mongodb-exporter-ingress",
258 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
263 # "host": "mongodb-exporter",
269 # "serviceName": "mongodb-exporter",
270 # "servicePort": 9216,
283 # self.harness.charm.on.start.emit()
285 # # Initializing the mongodb relation
286 # relation_id = self.harness.add_relation("mongodb", "mongodb")
287 # self.harness.add_relation_unit(relation_id, "mongodb/0")
288 # self.harness.update_relation_data(
292 # "connection_string": "mongodb://mongo",
296 # self.harness.update_config({"site_url": "http://mongodb-exporter"})
298 # pod_spec, _ = self.harness.get_pod_spec()
300 # self.assertDictEqual(expected_result, pod_spec)
302 # def test_ingress_resources_with_https(self) -> NoReturn:
303 # """Test ingress resources with HTTPS."""
304 # expected_result = {
308 # "name": "mongodb-exporter",
309 # "imageDetails": self.harness.charm.image.fetch(),
310 # "imagePullPolicy": "Always",
313 # "name": "mongo-exporter",
314 # "containerPort": 9216,
319 # "MONGODB_URI": "mongodb://mongo",
322 # "readinessProbe": {
324 # "path": "/api/health",
327 # "initialDelaySeconds": 10,
328 # "periodSeconds": 10,
329 # "timeoutSeconds": 5,
330 # "successThreshold": 1,
331 # "failureThreshold": 3,
335 # "path": "/api/health",
338 # "initialDelaySeconds": 60,
339 # "timeoutSeconds": 30,
340 # "failureThreshold": 10,
345 # "kubernetesResources": {
346 # "ingressResources": [
348 # "name": "mongodb-exporter-ingress",
353 # "host": "mongodb-exporter",
359 # "serviceName": "mongodb-exporter",
360 # "servicePort": 9216,
369 # "hosts": ["mongodb-exporter"],
370 # "secretName": "mongodb-exporter",
379 # self.harness.charm.on.start.emit()
381 # # Initializing the mongodb relation
382 # relation_id = self.harness.add_relation("mongodb", "mongodb")
383 # self.harness.add_relation_unit(relation_id, "mongodb/0")
384 # self.harness.update_relation_data(
388 # "connection_string": "mongodb://mongo",
392 # self.harness.update_config(
394 # "site_url": "https://mongodb-exporter",
395 # "tls_secret_name": "mongodb-exporter",
399 # pod_spec, _ = self.harness.get_pod_spec()
401 # self.assertDictEqual(expected_result, pod_spec)
403 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
404 # """Test ingress resources with HTTPS and ingress whitelist."""
405 # expected_result = {
409 # "name": "mongodb-exporter",
410 # "imageDetails": self.harness.charm.image.fetch(),
411 # "imagePullPolicy": "Always",
414 # "name": "mongo-exporter",
415 # "containerPort": 9216,
420 # "MONGODB_URI": "mongodb://mongo",
423 # "readinessProbe": {
425 # "path": "/api/health",
428 # "initialDelaySeconds": 10,
429 # "periodSeconds": 10,
430 # "timeoutSeconds": 5,
431 # "successThreshold": 1,
432 # "failureThreshold": 3,
436 # "path": "/api/health",
439 # "initialDelaySeconds": 60,
440 # "timeoutSeconds": 30,
441 # "failureThreshold": 10,
446 # "kubernetesResources": {
447 # "ingressResources": [
449 # "name": "mongodb-exporter-ingress",
451 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
456 # "host": "mongodb-exporter",
462 # "serviceName": "mongodb-exporter",
463 # "servicePort": 9216,
472 # "hosts": ["mongodb-exporter"],
473 # "secretName": "mongodb-exporter",
482 # self.harness.charm.on.start.emit()
484 # # Initializing the mongodb relation
485 # relation_id = self.harness.add_relation("mongodb", "mongodb")
486 # self.harness.add_relation_unit(relation_id, "mongodb/0")
487 # self.harness.update_relation_data(
491 # "connection_string": "mongodb://mongo",
495 # self.harness.update_config(
497 # "site_url": "https://mongodb-exporter",
498 # "tls_secret_name": "mongodb-exporter",
499 # "ingress_whitelist_source_range": "0.0.0.0/0",
503 # pod_spec, _ = self.harness.get_pod_spec()
505 # self.assertDictEqual(expected_result, pod_spec)
507 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
508 # """Test to see if mongodb relation is updated."""
509 # self.harness.charm.on.start.emit()
511 # # Initializing the mongodb relation
512 # relation_id = self.harness.add_relation("mongodb", "mongodb")
513 # self.harness.add_relation_unit(relation_id, "mongodb/0")
514 # self.harness.update_relation_data(
518 # "connection_string": "mongodb://mongo",
523 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
525 # def test_publish_scrape_info(self) -> NoReturn:
526 # """Test to see if scrape relation is updated."""
527 # expected_result = {
528 # "hostname": "mongodb-exporter",
530 # "metrics_path": "/metrics",
531 # "scrape_interval": "30s",
532 # "scrape_timeout": "15s",
535 # self.harness.charm.on.start.emit()
537 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
538 # self.harness.add_relation_unit(relation_id, "prometheus/0")
539 # relation_data = self.harness.get_relation_data(
540 # relation_id, "mongodb-exporter/0"
543 # self.assertDictEqual(expected_result, relation_data)
545 # def test_publish_scrape_info_with_site_url(self) -> NoReturn:
546 # """Test to see if target relation is updated."""
547 # expected_result = {
548 # "hostname": "mongodb-exporter-osm",
550 # "metrics_path": "/metrics",
551 # "scrape_interval": "30s",
552 # "scrape_timeout": "15s",
555 # self.harness.charm.on.start.emit()
557 # self.harness.update_config({"site_url": "http://mongodb-exporter-osm"})
559 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
560 # self.harness.add_relation_unit(relation_id, "prometheus/0")
561 # relation_data = self.harness.get_relation_data(
562 # relation_id, "mongodb-exporter/0"
565 # self.assertDictEqual(expected_result, relation_data)
567 # def test_publish_dashboard_info(self) -> NoReturn:
568 # """Test to see if dashboard relation is updated."""
569 # self.harness.charm.on.start.emit()
571 # relation_id = self.harness.add_relation("grafana-dashboard", "grafana")
572 # self.harness.add_relation_unit(relation_id, "grafana/0")
573 # relation_data = self.harness.get_relation_data(
574 # relation_id, "mongodb-exporter/0"
577 # self.assertEqual("osm-mongodb", relation_data["name"])
578 # self.assertTrue("dashboard" in relation_data)
579 # self.assertTrue(len(relation_data["dashboard"]) > 0)
582 # if __name__ == "__main__":