Moving exporter charms to use opslib
[osm/devops.git] / installers / charm / mongodb-exporter / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2021 Canonical Ltd.
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 ##
22
23 import sys
24 from typing import NoReturn
25 import unittest
26
27 from charm import MongodbExporterCharm
28 from ops.model import ActiveStatus, BlockedStatus
29 from ops.testing import Harness
30
31
32 class TestCharm(unittest.TestCase):
33 """Mongodb Exporter Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
38 self.harness = Harness(MongodbExporterCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
41 self.config = {
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",
46 }
47 self.harness.update_config(self.config)
48
49 def test_config_changed_no_relations(
50 self,
51 ) -> NoReturn:
52 """Test ingress resources without HTTP."""
53
54 self.harness.charm.on.config_changed.emit()
55
56 # Assertions
57 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
58 print(self.harness.charm.unit.status.message)
59 self.assertTrue(
60 all(
61 relation in self.harness.charm.unit.status.message
62 for relation in ["mongodb"]
63 )
64 )
65
66 def test_config_changed_non_leader(
67 self,
68 ) -> NoReturn:
69 """Test ingress resources without HTTP."""
70 self.harness.set_leader(is_leader=False)
71 self.harness.charm.on.config_changed.emit()
72
73 # Assertions
74 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
75
76 def test_with_relations(
77 self,
78 ) -> NoReturn:
79 "Test with relations"
80 self.initialize_mongo_relation()
81
82 # Verifying status
83 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
84
85 def test_with_config(
86 self,
87 ) -> NoReturn:
88 "Test with config"
89 self.initialize_mongo_relation()
90
91 # Verifying status
92 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
93
94 def test_mongodb_exception_relation_and_config(
95 self,
96 ) -> NoReturn:
97 self.initialize_mongo_config()
98 self.initialize_mongo_relation()
99
100 # Verifying status
101 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
102
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(
107 mongodb_relation_id,
108 "mongodb/0",
109 {"connection_string": "mongodb://mongo:27017"},
110 )
111
112 def initialize_mongo_config(self):
113 self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"})
114
115
116 if __name__ == "__main__":
117 unittest.main()
118
119
120 # class TestCharm(unittest.TestCase):
121 # """Mongodb Exporter Charm unit tests."""
122 #
123 # def setUp(self) -> NoReturn:
124 # """Test setup"""
125 # self.harness = Harness(MongodbExporterCharm)
126 # self.harness.set_leader(is_leader=True)
127 # self.harness.begin()
128 #
129 # def test_on_start_without_relations(self) -> NoReturn:
130 # """Test installation without any relation."""
131 # self.harness.charm.on.start.emit()
132 #
133 # # Verifying status
134 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
135 #
136 # # Verifying status message
137 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
138 # self.assertTrue(
139 # self.harness.charm.unit.status.message.startswith("Waiting for ")
140 # )
141 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
142 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
143 #
144 # def test_on_start_with_relations_without_http(self) -> NoReturn:
145 # """Test deployment."""
146 # expected_result = {
147 # "version": 3,
148 # "containers": [
149 # {
150 # "name": "mongodb-exporter",
151 # "imageDetails": self.harness.charm.image.fetch(),
152 # "imagePullPolicy": "Always",
153 # "ports": [
154 # {
155 # "name": "mongo-exporter",
156 # "containerPort": 9216,
157 # "protocol": "TCP",
158 # }
159 # ],
160 # "envConfig": {
161 # "MONGODB_URI": "mongodb://mongo",
162 # },
163 # "kubernetes": {
164 # "readinessProbe": {
165 # "httpGet": {
166 # "path": "/api/health",
167 # "port": 9216,
168 # },
169 # "initialDelaySeconds": 10,
170 # "periodSeconds": 10,
171 # "timeoutSeconds": 5,
172 # "successThreshold": 1,
173 # "failureThreshold": 3,
174 # },
175 # "livenessProbe": {
176 # "httpGet": {
177 # "path": "/api/health",
178 # "port": 9216,
179 # },
180 # "initialDelaySeconds": 60,
181 # "timeoutSeconds": 30,
182 # "failureThreshold": 10,
183 # },
184 # },
185 # },
186 # ],
187 # "kubernetesResources": {"ingressResources": []},
188 # }
189 #
190 # self.harness.charm.on.start.emit()
191 #
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(
196 # relation_id,
197 # "mongodb/0",
198 # {
199 # "connection_string": "mongodb://mongo",
200 # },
201 # )
202 #
203 # # Verifying status
204 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
205 #
206 # pod_spec, _ = self.harness.get_pod_spec()
207 #
208 # self.assertDictEqual(expected_result, pod_spec)
209 #
210 # def test_ingress_resources_with_http(self) -> NoReturn:
211 # """Test ingress resources with HTTP."""
212 # expected_result = {
213 # "version": 3,
214 # "containers": [
215 # {
216 # "name": "mongodb-exporter",
217 # "imageDetails": self.harness.charm.image.fetch(),
218 # "imagePullPolicy": "Always",
219 # "ports": [
220 # {
221 # "name": "mongo-exporter",
222 # "containerPort": 9216,
223 # "protocol": "TCP",
224 # }
225 # ],
226 # "envConfig": {
227 # "MONGODB_URI": "mongodb://mongo",
228 # },
229 # "kubernetes": {
230 # "readinessProbe": {
231 # "httpGet": {
232 # "path": "/api/health",
233 # "port": 9216,
234 # },
235 # "initialDelaySeconds": 10,
236 # "periodSeconds": 10,
237 # "timeoutSeconds": 5,
238 # "successThreshold": 1,
239 # "failureThreshold": 3,
240 # },
241 # "livenessProbe": {
242 # "httpGet": {
243 # "path": "/api/health",
244 # "port": 9216,
245 # },
246 # "initialDelaySeconds": 60,
247 # "timeoutSeconds": 30,
248 # "failureThreshold": 10,
249 # },
250 # },
251 # },
252 # ],
253 # "kubernetesResources": {
254 # "ingressResources": [
255 # {
256 # "name": "mongodb-exporter-ingress",
257 # "annotations": {
258 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
259 # },
260 # "spec": {
261 # "rules": [
262 # {
263 # "host": "mongodb-exporter",
264 # "http": {
265 # "paths": [
266 # {
267 # "path": "/",
268 # "backend": {
269 # "serviceName": "mongodb-exporter",
270 # "servicePort": 9216,
271 # },
272 # }
273 # ]
274 # },
275 # }
276 # ]
277 # },
278 # }
279 # ],
280 # },
281 # }
282 #
283 # self.harness.charm.on.start.emit()
284 #
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(
289 # relation_id,
290 # "mongodb/0",
291 # {
292 # "connection_string": "mongodb://mongo",
293 # },
294 # )
295 #
296 # self.harness.update_config({"site_url": "http://mongodb-exporter"})
297 #
298 # pod_spec, _ = self.harness.get_pod_spec()
299 #
300 # self.assertDictEqual(expected_result, pod_spec)
301 #
302 # def test_ingress_resources_with_https(self) -> NoReturn:
303 # """Test ingress resources with HTTPS."""
304 # expected_result = {
305 # "version": 3,
306 # "containers": [
307 # {
308 # "name": "mongodb-exporter",
309 # "imageDetails": self.harness.charm.image.fetch(),
310 # "imagePullPolicy": "Always",
311 # "ports": [
312 # {
313 # "name": "mongo-exporter",
314 # "containerPort": 9216,
315 # "protocol": "TCP",
316 # }
317 # ],
318 # "envConfig": {
319 # "MONGODB_URI": "mongodb://mongo",
320 # },
321 # "kubernetes": {
322 # "readinessProbe": {
323 # "httpGet": {
324 # "path": "/api/health",
325 # "port": 9216,
326 # },
327 # "initialDelaySeconds": 10,
328 # "periodSeconds": 10,
329 # "timeoutSeconds": 5,
330 # "successThreshold": 1,
331 # "failureThreshold": 3,
332 # },
333 # "livenessProbe": {
334 # "httpGet": {
335 # "path": "/api/health",
336 # "port": 9216,
337 # },
338 # "initialDelaySeconds": 60,
339 # "timeoutSeconds": 30,
340 # "failureThreshold": 10,
341 # },
342 # },
343 # },
344 # ],
345 # "kubernetesResources": {
346 # "ingressResources": [
347 # {
348 # "name": "mongodb-exporter-ingress",
349 # "annotations": {},
350 # "spec": {
351 # "rules": [
352 # {
353 # "host": "mongodb-exporter",
354 # "http": {
355 # "paths": [
356 # {
357 # "path": "/",
358 # "backend": {
359 # "serviceName": "mongodb-exporter",
360 # "servicePort": 9216,
361 # },
362 # }
363 # ]
364 # },
365 # }
366 # ],
367 # "tls": [
368 # {
369 # "hosts": ["mongodb-exporter"],
370 # "secretName": "mongodb-exporter",
371 # }
372 # ],
373 # },
374 # }
375 # ],
376 # },
377 # }
378 #
379 # self.harness.charm.on.start.emit()
380 #
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(
385 # relation_id,
386 # "mongodb/0",
387 # {
388 # "connection_string": "mongodb://mongo",
389 # },
390 # )
391 #
392 # self.harness.update_config(
393 # {
394 # "site_url": "https://mongodb-exporter",
395 # "tls_secret_name": "mongodb-exporter",
396 # }
397 # )
398 #
399 # pod_spec, _ = self.harness.get_pod_spec()
400 #
401 # self.assertDictEqual(expected_result, pod_spec)
402 #
403 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
404 # """Test ingress resources with HTTPS and ingress whitelist."""
405 # expected_result = {
406 # "version": 3,
407 # "containers": [
408 # {
409 # "name": "mongodb-exporter",
410 # "imageDetails": self.harness.charm.image.fetch(),
411 # "imagePullPolicy": "Always",
412 # "ports": [
413 # {
414 # "name": "mongo-exporter",
415 # "containerPort": 9216,
416 # "protocol": "TCP",
417 # }
418 # ],
419 # "envConfig": {
420 # "MONGODB_URI": "mongodb://mongo",
421 # },
422 # "kubernetes": {
423 # "readinessProbe": {
424 # "httpGet": {
425 # "path": "/api/health",
426 # "port": 9216,
427 # },
428 # "initialDelaySeconds": 10,
429 # "periodSeconds": 10,
430 # "timeoutSeconds": 5,
431 # "successThreshold": 1,
432 # "failureThreshold": 3,
433 # },
434 # "livenessProbe": {
435 # "httpGet": {
436 # "path": "/api/health",
437 # "port": 9216,
438 # },
439 # "initialDelaySeconds": 60,
440 # "timeoutSeconds": 30,
441 # "failureThreshold": 10,
442 # },
443 # },
444 # },
445 # ],
446 # "kubernetesResources": {
447 # "ingressResources": [
448 # {
449 # "name": "mongodb-exporter-ingress",
450 # "annotations": {
451 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
452 # },
453 # "spec": {
454 # "rules": [
455 # {
456 # "host": "mongodb-exporter",
457 # "http": {
458 # "paths": [
459 # {
460 # "path": "/",
461 # "backend": {
462 # "serviceName": "mongodb-exporter",
463 # "servicePort": 9216,
464 # },
465 # }
466 # ]
467 # },
468 # }
469 # ],
470 # "tls": [
471 # {
472 # "hosts": ["mongodb-exporter"],
473 # "secretName": "mongodb-exporter",
474 # }
475 # ],
476 # },
477 # }
478 # ],
479 # },
480 # }
481 #
482 # self.harness.charm.on.start.emit()
483 #
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(
488 # relation_id,
489 # "mongodb/0",
490 # {
491 # "connection_string": "mongodb://mongo",
492 # },
493 # )
494 #
495 # self.harness.update_config(
496 # {
497 # "site_url": "https://mongodb-exporter",
498 # "tls_secret_name": "mongodb-exporter",
499 # "ingress_whitelist_source_range": "0.0.0.0/0",
500 # }
501 # )
502 #
503 # pod_spec, _ = self.harness.get_pod_spec()
504 #
505 # self.assertDictEqual(expected_result, pod_spec)
506 #
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()
510 #
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(
515 # relation_id,
516 # "mongodb/0",
517 # {
518 # "connection_string": "mongodb://mongo",
519 # },
520 # )
521 #
522 # # Verifying status
523 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
524 #
525 # def test_publish_scrape_info(self) -> NoReturn:
526 # """Test to see if scrape relation is updated."""
527 # expected_result = {
528 # "hostname": "mongodb-exporter",
529 # "port": "9216",
530 # "metrics_path": "/metrics",
531 # "scrape_interval": "30s",
532 # "scrape_timeout": "15s",
533 # }
534 #
535 # self.harness.charm.on.start.emit()
536 #
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"
541 # )
542 #
543 # self.assertDictEqual(expected_result, relation_data)
544 #
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",
549 # "port": "80",
550 # "metrics_path": "/metrics",
551 # "scrape_interval": "30s",
552 # "scrape_timeout": "15s",
553 # }
554 #
555 # self.harness.charm.on.start.emit()
556 #
557 # self.harness.update_config({"site_url": "http://mongodb-exporter-osm"})
558 #
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"
563 # )
564 #
565 # self.assertDictEqual(expected_result, relation_data)
566 #
567 # def test_publish_dashboard_info(self) -> NoReturn:
568 # """Test to see if dashboard relation is updated."""
569 # self.harness.charm.on.start.emit()
570 #
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"
575 # )
576 #
577 # self.assertEqual("osm-mongodb", relation_data["name"])
578 # self.assertTrue("dashboard" in relation_data)
579 # self.assertTrue(len(relation_data["dashboard"]) > 0)
580 #
581 #
582 # if __name__ == "__main__":
583 # unittest.main()