Adding Prometheus Mongodb Exporter Charm
[osm/devops.git] / installers / charm / prometheus-mongodb-exporter / tests / test_pod_spec.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 import pod_spec
27
28
29 class TestPodSpec(unittest.TestCase):
30 """Pod spec unit tests."""
31
32 def test_make_pod_ports(self) -> NoReturn:
33 """Testing make pod ports."""
34 port = 9216
35
36 expected_result = [
37 {
38 "name": "prometheus-mongodb-exporter",
39 "containerPort": port,
40 "protocol": "TCP",
41 }
42 ]
43
44 pod_ports = pod_spec._make_pod_ports(port)
45
46 self.assertListEqual(expected_result, pod_ports)
47
48 def test_make_pod_envconfig(self) -> NoReturn:
49 """Teting make pod envconfig."""
50 config = {}
51 relation_state = {
52 "mongodb_connection_string": "mongodb://mongo",
53 }
54
55 expected_result = {}
56
57 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
58
59 self.assertDictEqual(expected_result, pod_envconfig)
60
61 def test_make_pod_ingress_resources_without_site_url(self) -> NoReturn:
62 """Testing make pod ingress resources without site_url."""
63 config = {"site_url": ""}
64 app_name = "prometheus-mongodb-exporter"
65 port = 9216
66
67 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
68 config, app_name, port
69 )
70
71 self.assertIsNone(pod_ingress_resources)
72
73 def test_make_pod_ingress_resources(self) -> NoReturn:
74 """Testing make pod ingress resources."""
75 config = {
76 "site_url": "http://prometheus-mongodb-exporter",
77 "ingress_whitelist_source_range": "",
78 }
79 app_name = "prometheus-mongodb-exporter"
80 port = 9216
81
82 expected_result = [
83 {
84 "name": f"{app_name}-ingress",
85 "annotations": {
86 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
87 },
88 "spec": {
89 "rules": [
90 {
91 "host": app_name,
92 "http": {
93 "paths": [
94 {
95 "path": "/",
96 "backend": {
97 "serviceName": app_name,
98 "servicePort": port,
99 },
100 }
101 ]
102 },
103 }
104 ]
105 },
106 }
107 ]
108
109 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
110 config, app_name, port
111 )
112
113 self.assertListEqual(expected_result, pod_ingress_resources)
114
115 def test_make_pod_ingress_resources_with_whitelist_source_range(self) -> NoReturn:
116 """Testing make pod ingress resources with whitelist_source_range."""
117 config = {
118 "site_url": "http://prometheus-mongodb-exporter",
119 "ingress_whitelist_source_range": "0.0.0.0/0",
120 }
121 app_name = "prometheus-mongodb-exporter"
122 port = 9216
123
124 expected_result = [
125 {
126 "name": f"{app_name}-ingress",
127 "annotations": {
128 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
129 "nginx.ingress.kubernetes.io/whitelist-source-range": config[
130 "ingress_whitelist_source_range"
131 ],
132 },
133 "spec": {
134 "rules": [
135 {
136 "host": app_name,
137 "http": {
138 "paths": [
139 {
140 "path": "/",
141 "backend": {
142 "serviceName": app_name,
143 "servicePort": port,
144 },
145 }
146 ]
147 },
148 }
149 ]
150 },
151 }
152 ]
153
154 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
155 config, app_name, port
156 )
157
158 self.assertListEqual(expected_result, pod_ingress_resources)
159
160 def test_make_pod_ingress_resources_with_https(self) -> NoReturn:
161 """Testing make pod ingress resources with HTTPs."""
162 config = {
163 "site_url": "https://prometheus-mongodb-exporter",
164 "ingress_whitelist_source_range": "",
165 "tls_secret_name": "",
166 }
167 app_name = "prometheus-mongodb-exporter"
168 port = 9216
169
170 expected_result = [
171 {
172 "name": f"{app_name}-ingress",
173 "annotations": {},
174 "spec": {
175 "rules": [
176 {
177 "host": app_name,
178 "http": {
179 "paths": [
180 {
181 "path": "/",
182 "backend": {
183 "serviceName": app_name,
184 "servicePort": port,
185 },
186 }
187 ]
188 },
189 }
190 ],
191 "tls": [{"hosts": [app_name]}],
192 },
193 }
194 ]
195
196 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
197 config, app_name, port
198 )
199
200 self.assertListEqual(expected_result, pod_ingress_resources)
201
202 def test_make_pod_ingress_resources_with_https_tls_secret_name(self) -> NoReturn:
203 """Testing make pod ingress resources with HTTPs and TLS secret name."""
204 config = {
205 "site_url": "https://prometheus-mongodb-exporter",
206 "ingress_whitelist_source_range": "",
207 "tls_secret_name": "secret_name",
208 }
209 app_name = "prometheus-mongodb-exporter"
210 port = 9216
211
212 expected_result = [
213 {
214 "name": f"{app_name}-ingress",
215 "annotations": {},
216 "spec": {
217 "rules": [
218 {
219 "host": app_name,
220 "http": {
221 "paths": [
222 {
223 "path": "/",
224 "backend": {
225 "serviceName": app_name,
226 "servicePort": port,
227 },
228 }
229 ]
230 },
231 }
232 ],
233 "tls": [
234 {"hosts": [app_name], "secretName": config["tls_secret_name"]}
235 ],
236 },
237 }
238 ]
239
240 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
241 config, app_name, port
242 )
243
244 self.assertListEqual(expected_result, pod_ingress_resources)
245
246 def test_make_readiness_probe(self) -> NoReturn:
247 """Testing make readiness probe."""
248 port = 9216
249
250 expected_result = {
251 "httpGet": {
252 "path": "/api/health",
253 "port": port,
254 },
255 "initialDelaySeconds": 10,
256 "periodSeconds": 10,
257 "timeoutSeconds": 5,
258 "successThreshold": 1,
259 "failureThreshold": 3,
260 }
261
262 readiness_probe = pod_spec._make_readiness_probe(port)
263
264 self.assertDictEqual(expected_result, readiness_probe)
265
266 def test_make_liveness_probe(self) -> NoReturn:
267 """Testing make liveness probe."""
268 port = 9216
269
270 expected_result = {
271 "httpGet": {
272 "path": "/api/health",
273 "port": port,
274 },
275 "initialDelaySeconds": 60,
276 "timeoutSeconds": 30,
277 "failureThreshold": 10,
278 }
279
280 liveness_probe = pod_spec._make_liveness_probe(port)
281
282 self.assertDictEqual(expected_result, liveness_probe)
283
284 def test_make_pod_command(self) -> NoReturn:
285 """Testing make pod command."""
286 relation = {
287 "mongodb_connection_string": "mongodb://mongo",
288 }
289
290 expected_result = [
291 "mongodb_exporter_linux_amd64/mongodb_exporter",
292 "--mongodbdsn={}".format(relation.get("mongodb_connection_string")),
293 ]
294
295 pod_envconfig = pod_spec._make_pod_command(relation)
296
297 self.assertListEqual(expected_result, pod_envconfig)
298
299 def test_make_pod_spec(self) -> NoReturn:
300 """Testing make pod spec."""
301 image_info = {"upstream-source": "bitnami/mongodb-exporter:latest"}
302 config = {
303 "site_url": "",
304 }
305 relation_state = {
306 "mongodb_connection_string": "mongodb://mongo",
307 }
308 app_name = "prometheus-mongodb-exporter"
309 port = 9216
310
311 expected_result = {
312 "version": 3,
313 "containers": [
314 {
315 "name": app_name,
316 "imageDetails": image_info,
317 "imagePullPolicy": "Always",
318 "ports": [
319 {
320 "name": app_name,
321 "containerPort": port,
322 "protocol": "TCP",
323 }
324 ],
325 "envConfig": {},
326 "command": [
327 "mongodb_exporter_linux_amd64/mongodb_exporter",
328 "--mongodbdsn=mongodb://mongo",
329 ],
330 "kubernetes": {
331 "readinessProbe": {
332 "httpGet": {
333 "path": "/api/health",
334 "port": port,
335 },
336 "initialDelaySeconds": 10,
337 "periodSeconds": 10,
338 "timeoutSeconds": 5,
339 "successThreshold": 1,
340 "failureThreshold": 3,
341 },
342 "livenessProbe": {
343 "httpGet": {
344 "path": "/api/health",
345 "port": port,
346 },
347 "initialDelaySeconds": 60,
348 "timeoutSeconds": 30,
349 "failureThreshold": 10,
350 },
351 },
352 }
353 ],
354 "kubernetesResources": {"ingressResources": []},
355 }
356
357 spec = pod_spec.make_pod_spec(
358 image_info, config, relation_state, app_name, port
359 )
360
361 self.assertDictEqual(expected_result, spec)
362
363 def test_make_pod_spec_with_ingress(self) -> NoReturn:
364 """Testing make pod spec."""
365 image_info = {"upstream-source": "bitnami/mongodb-exporter:latest"}
366 config = {
367 "site_url": "https://prometheus-mongodb-exporter",
368 "tls_secret_name": "prometheus-mongodb-exporter",
369 "ingress_whitelist_source_range": "0.0.0.0/0",
370 }
371 relation_state = {
372 "mongodb_connection_string": "mongodb://mongo",
373 }
374 app_name = "prometheus-mongodb-exporter"
375 port = 9216
376
377 expected_result = {
378 "version": 3,
379 "containers": [
380 {
381 "name": app_name,
382 "imageDetails": image_info,
383 "imagePullPolicy": "Always",
384 "ports": [
385 {
386 "name": app_name,
387 "containerPort": port,
388 "protocol": "TCP",
389 }
390 ],
391 "envConfig": {},
392 "command": [
393 "mongodb_exporter_linux_amd64/mongodb_exporter",
394 "--mongodbdsn=mongodb://mongo",
395 ],
396 "kubernetes": {
397 "readinessProbe": {
398 "httpGet": {
399 "path": "/api/health",
400 "port": port,
401 },
402 "initialDelaySeconds": 10,
403 "periodSeconds": 10,
404 "timeoutSeconds": 5,
405 "successThreshold": 1,
406 "failureThreshold": 3,
407 },
408 "livenessProbe": {
409 "httpGet": {
410 "path": "/api/health",
411 "port": port,
412 },
413 "initialDelaySeconds": 60,
414 "timeoutSeconds": 30,
415 "failureThreshold": 10,
416 },
417 },
418 }
419 ],
420 "kubernetesResources": {
421 "ingressResources": [
422 {
423 "name": "{}-ingress".format(app_name),
424 "annotations": {
425 "nginx.ingress.kubernetes.io/whitelist-source-range": config.get(
426 "ingress_whitelist_source_range"
427 ),
428 },
429 "spec": {
430 "rules": [
431 {
432 "host": app_name,
433 "http": {
434 "paths": [
435 {
436 "path": "/",
437 "backend": {
438 "serviceName": app_name,
439 "servicePort": port,
440 },
441 }
442 ]
443 },
444 }
445 ],
446 "tls": [
447 {
448 "hosts": [app_name],
449 "secretName": config.get("tls_secret_name"),
450 }
451 ],
452 },
453 }
454 ],
455 },
456 }
457
458 spec = pod_spec.make_pod_spec(
459 image_info, config, relation_state, app_name, port
460 )
461
462 self.assertDictEqual(expected_result, spec)
463
464 def test_make_pod_spec_without_image_info(self) -> NoReturn:
465 """Testing make pod spec without image_info."""
466 image_info = None
467 config = {
468 "site_url": "",
469 }
470 relation_state = {
471 "mongodb_connection_string": "mongodb://mongo",
472 }
473 app_name = "prometheus-mongodb-exporter"
474 port = 9216
475
476 spec = pod_spec.make_pod_spec(
477 image_info, config, relation_state, app_name, port
478 )
479
480 self.assertIsNone(spec)
481
482 def test_make_pod_spec_without_relation_state(self) -> NoReturn:
483 """Testing make pod spec without relation_state."""
484 image_info = {"upstream-source": "bitnami/mongodb-exporter:latest"}
485 config = {
486 "site_url": "",
487 }
488 relation_state = {}
489 app_name = "prometheus-mongodb-exporter"
490 port = 9216
491
492 with self.assertRaises(ValueError):
493 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
494
495
496 if __name__ == "__main__":
497 unittest.main()