Adding scraping relation to Kafka Exporter
[osm/devops.git] / installers / charm / prometheus-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 from typing import NoReturn
24 import unittest
25
26 from ops.model import BlockedStatus
27 from ops.testing import Harness
28
29 from charm import PrometheusMongodbExporterCharm
30
31
32 class TestCharm(unittest.TestCase):
33 """Prometheus Mongodb Exporter Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.harness = Harness(PrometheusMongodbExporterCharm)
38 self.harness.set_leader(is_leader=True)
39 self.harness.begin()
40
41 def test_on_start_without_relations(self) -> NoReturn:
42 """Test installation without any relation."""
43 self.harness.charm.on.start.emit()
44
45 # Verifying status
46 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
47
48 # Verifying status message
49 self.assertGreater(len(self.harness.charm.unit.status.message), 0)
50 self.assertTrue(
51 self.harness.charm.unit.status.message.startswith("Waiting for ")
52 )
53 self.assertIn("mongodb", self.harness.charm.unit.status.message)
54 self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
55
56 def test_on_start_with_relations_without_http(self) -> NoReturn:
57 """Test deployment."""
58 expected_result = {
59 "version": 3,
60 "containers": [
61 {
62 "name": "prometheus-mongodb-exporter",
63 "imageDetails": self.harness.charm.image.fetch(),
64 "imagePullPolicy": "Always",
65 "ports": [
66 {
67 "name": "prometheus-mongodb-exporter",
68 "containerPort": 9216,
69 "protocol": "TCP",
70 }
71 ],
72 "envConfig": {},
73 "command": [
74 "mongodb_exporter_linux_amd64/mongodb_exporter",
75 "--mongodbdsn=mongodb://mongo",
76 ],
77 "kubernetes": {
78 "readinessProbe": {
79 "httpGet": {
80 "path": "/api/health",
81 "port": 9216,
82 },
83 "initialDelaySeconds": 10,
84 "periodSeconds": 10,
85 "timeoutSeconds": 5,
86 "successThreshold": 1,
87 "failureThreshold": 3,
88 },
89 "livenessProbe": {
90 "httpGet": {
91 "path": "/api/health",
92 "port": 9216,
93 },
94 "initialDelaySeconds": 60,
95 "timeoutSeconds": 30,
96 "failureThreshold": 10,
97 },
98 },
99 },
100 ],
101 "kubernetesResources": {"ingressResources": []},
102 }
103
104 self.harness.charm.on.start.emit()
105
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(
110 relation_id,
111 "mongodb/0",
112 {
113 "connection_string": "mongodb://mongo",
114 },
115 )
116
117 # Verifying status
118 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
119
120 pod_spec, _ = self.harness.get_pod_spec()
121
122 self.assertDictEqual(expected_result, pod_spec)
123
124 def test_ingress_resources_with_http(self) -> NoReturn:
125 """Test ingress resources with HTTP."""
126 expected_result = {
127 "version": 3,
128 "containers": [
129 {
130 "name": "prometheus-mongodb-exporter",
131 "imageDetails": self.harness.charm.image.fetch(),
132 "imagePullPolicy": "Always",
133 "ports": [
134 {
135 "name": "prometheus-mongodb-exporter",
136 "containerPort": 9216,
137 "protocol": "TCP",
138 }
139 ],
140 "envConfig": {},
141 "command": [
142 "mongodb_exporter_linux_amd64/mongodb_exporter",
143 "--mongodbdsn=mongodb://mongo",
144 ],
145 "kubernetes": {
146 "readinessProbe": {
147 "httpGet": {
148 "path": "/api/health",
149 "port": 9216,
150 },
151 "initialDelaySeconds": 10,
152 "periodSeconds": 10,
153 "timeoutSeconds": 5,
154 "successThreshold": 1,
155 "failureThreshold": 3,
156 },
157 "livenessProbe": {
158 "httpGet": {
159 "path": "/api/health",
160 "port": 9216,
161 },
162 "initialDelaySeconds": 60,
163 "timeoutSeconds": 30,
164 "failureThreshold": 10,
165 },
166 },
167 },
168 ],
169 "kubernetesResources": {
170 "ingressResources": [
171 {
172 "name": "prometheus-mongodb-exporter-ingress",
173 "annotations": {
174 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
175 },
176 "spec": {
177 "rules": [
178 {
179 "host": "prometheus-mongodb-exporter",
180 "http": {
181 "paths": [
182 {
183 "path": "/",
184 "backend": {
185 "serviceName": "prometheus-mongodb-exporter",
186 "servicePort": 9216,
187 },
188 }
189 ]
190 },
191 }
192 ]
193 },
194 }
195 ],
196 },
197 }
198
199 self.harness.charm.on.start.emit()
200
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(
205 relation_id,
206 "mongodb/0",
207 {
208 "connection_string": "mongodb://mongo",
209 },
210 )
211
212 self.harness.update_config({"site_url": "http://prometheus-mongodb-exporter"})
213
214 pod_spec, _ = self.harness.get_pod_spec()
215
216 self.assertDictEqual(expected_result, pod_spec)
217
218 def test_ingress_resources_with_https(self) -> NoReturn:
219 """Test ingress resources with HTTPS."""
220 expected_result = {
221 "version": 3,
222 "containers": [
223 {
224 "name": "prometheus-mongodb-exporter",
225 "imageDetails": self.harness.charm.image.fetch(),
226 "imagePullPolicy": "Always",
227 "ports": [
228 {
229 "name": "prometheus-mongodb-exporter",
230 "containerPort": 9216,
231 "protocol": "TCP",
232 }
233 ],
234 "envConfig": {},
235 "command": [
236 "mongodb_exporter_linux_amd64/mongodb_exporter",
237 "--mongodbdsn=mongodb://mongo",
238 ],
239 "kubernetes": {
240 "readinessProbe": {
241 "httpGet": {
242 "path": "/api/health",
243 "port": 9216,
244 },
245 "initialDelaySeconds": 10,
246 "periodSeconds": 10,
247 "timeoutSeconds": 5,
248 "successThreshold": 1,
249 "failureThreshold": 3,
250 },
251 "livenessProbe": {
252 "httpGet": {
253 "path": "/api/health",
254 "port": 9216,
255 },
256 "initialDelaySeconds": 60,
257 "timeoutSeconds": 30,
258 "failureThreshold": 10,
259 },
260 },
261 },
262 ],
263 "kubernetesResources": {
264 "ingressResources": [
265 {
266 "name": "prometheus-mongodb-exporter-ingress",
267 "annotations": {},
268 "spec": {
269 "rules": [
270 {
271 "host": "prometheus-mongodb-exporter",
272 "http": {
273 "paths": [
274 {
275 "path": "/",
276 "backend": {
277 "serviceName": "prometheus-mongodb-exporter",
278 "servicePort": 9216,
279 },
280 }
281 ]
282 },
283 }
284 ],
285 "tls": [
286 {
287 "hosts": ["prometheus-mongodb-exporter"],
288 "secretName": "prometheus-mongodb-exporter",
289 }
290 ],
291 },
292 }
293 ],
294 },
295 }
296
297 self.harness.charm.on.start.emit()
298
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(
303 relation_id,
304 "mongodb/0",
305 {
306 "connection_string": "mongodb://mongo",
307 },
308 )
309
310 self.harness.update_config(
311 {
312 "site_url": "https://prometheus-mongodb-exporter",
313 "tls_secret_name": "prometheus-mongodb-exporter",
314 }
315 )
316
317 pod_spec, _ = self.harness.get_pod_spec()
318
319 self.assertDictEqual(expected_result, pod_spec)
320
321 def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
322 """Test ingress resources with HTTPS and ingress whitelist."""
323 expected_result = {
324 "version": 3,
325 "containers": [
326 {
327 "name": "prometheus-mongodb-exporter",
328 "imageDetails": self.harness.charm.image.fetch(),
329 "imagePullPolicy": "Always",
330 "ports": [
331 {
332 "name": "prometheus-mongodb-exporter",
333 "containerPort": 9216,
334 "protocol": "TCP",
335 }
336 ],
337 "envConfig": {},
338 "command": [
339 "mongodb_exporter_linux_amd64/mongodb_exporter",
340 "--mongodbdsn=mongodb://mongo",
341 ],
342 "kubernetes": {
343 "readinessProbe": {
344 "httpGet": {
345 "path": "/api/health",
346 "port": 9216,
347 },
348 "initialDelaySeconds": 10,
349 "periodSeconds": 10,
350 "timeoutSeconds": 5,
351 "successThreshold": 1,
352 "failureThreshold": 3,
353 },
354 "livenessProbe": {
355 "httpGet": {
356 "path": "/api/health",
357 "port": 9216,
358 },
359 "initialDelaySeconds": 60,
360 "timeoutSeconds": 30,
361 "failureThreshold": 10,
362 },
363 },
364 },
365 ],
366 "kubernetesResources": {
367 "ingressResources": [
368 {
369 "name": "prometheus-mongodb-exporter-ingress",
370 "annotations": {
371 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
372 },
373 "spec": {
374 "rules": [
375 {
376 "host": "prometheus-mongodb-exporter",
377 "http": {
378 "paths": [
379 {
380 "path": "/",
381 "backend": {
382 "serviceName": "prometheus-mongodb-exporter",
383 "servicePort": 9216,
384 },
385 }
386 ]
387 },
388 }
389 ],
390 "tls": [
391 {
392 "hosts": ["prometheus-mongodb-exporter"],
393 "secretName": "prometheus-mongodb-exporter",
394 }
395 ],
396 },
397 }
398 ],
399 },
400 }
401
402 self.harness.charm.on.start.emit()
403
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(
408 relation_id,
409 "mongodb/0",
410 {
411 "connection_string": "mongodb://mongo",
412 },
413 )
414
415 self.harness.update_config(
416 {
417 "site_url": "https://prometheus-mongodb-exporter",
418 "tls_secret_name": "prometheus-mongodb-exporter",
419 "ingress_whitelist_source_range": "0.0.0.0/0",
420 }
421 )
422
423 pod_spec, _ = self.harness.get_pod_spec()
424
425 self.assertDictEqual(expected_result, pod_spec)
426
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()
430
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(
435 relation_id,
436 "mongodb/0",
437 {
438 "connection_string": "mongodb://mongo",
439 },
440 )
441
442 # Verifying status
443 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
444
445 def test_publish_target_info(self) -> NoReturn:
446 """Test to see if target relation is updated."""
447 expected_result = {
448 "hostname": "prometheus-mongodb-exporter",
449 "port": "9216",
450 }
451
452 self.harness.charm.on.start.emit()
453
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"
458 )
459
460 self.assertDictEqual(expected_result, relation_data)
461
462 def test_publish_dashboard_info(self) -> NoReturn:
463 """Test to see if dashboard relation is updated."""
464 self.harness.charm.on.start.emit()
465
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"
470 )
471
472 self.assertTrue("dashboard" in relation_data)
473 self.assertTrue(len(relation_data["dashboard"]) > 0)
474
475
476 if __name__ == "__main__":
477 unittest.main()