Adding Prometheus Kafka Exporter Charm
[osm/devops.git] / installers / charm / prometheus-kafka-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 = 9308
35
36 expected_result = [
37 {
38 "name": "prometheus-kafka-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
53 expected_result = {}
54
55 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
56
57 self.assertDictEqual(expected_result, pod_envconfig)
58
59 def test_make_pod_ingress_resources_without_site_url(self) -> NoReturn:
60 """Testing make pod ingress resources without site_url."""
61 config = {"site_url": ""}
62 app_name = "prometheus-kafka-exporter"
63 port = 9308
64
65 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
66 config, app_name, port
67 )
68
69 self.assertIsNone(pod_ingress_resources)
70
71 def test_make_pod_ingress_resources(self) -> NoReturn:
72 """Testing make pod ingress resources."""
73 config = {
74 "site_url": "http://prometheus-kafka-exporter",
75 "ingress_whitelist_source_range": "",
76 }
77 app_name = "prometheus-kafka-exporter"
78 port = 9308
79
80 expected_result = [
81 {
82 "name": f"{app_name}-ingress",
83 "annotations": {
84 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
85 },
86 "spec": {
87 "rules": [
88 {
89 "host": app_name,
90 "http": {
91 "paths": [
92 {
93 "path": "/",
94 "backend": {
95 "serviceName": app_name,
96 "servicePort": port,
97 },
98 }
99 ]
100 },
101 }
102 ]
103 },
104 }
105 ]
106
107 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
108 config, app_name, port
109 )
110
111 self.assertListEqual(expected_result, pod_ingress_resources)
112
113 def test_make_pod_ingress_resources_with_whitelist_source_range(self) -> NoReturn:
114 """Testing make pod ingress resources with whitelist_source_range."""
115 config = {
116 "site_url": "http://prometheus-kafka-exporter",
117 "ingress_whitelist_source_range": "0.0.0.0/0",
118 }
119 app_name = "prometheus-kafka-exporter"
120 port = 9308
121
122 expected_result = [
123 {
124 "name": f"{app_name}-ingress",
125 "annotations": {
126 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
127 "nginx.ingress.kubernetes.io/whitelist-source-range": config[
128 "ingress_whitelist_source_range"
129 ],
130 },
131 "spec": {
132 "rules": [
133 {
134 "host": app_name,
135 "http": {
136 "paths": [
137 {
138 "path": "/",
139 "backend": {
140 "serviceName": app_name,
141 "servicePort": port,
142 },
143 }
144 ]
145 },
146 }
147 ]
148 },
149 }
150 ]
151
152 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
153 config, app_name, port
154 )
155
156 self.assertListEqual(expected_result, pod_ingress_resources)
157
158 def test_make_pod_ingress_resources_with_https(self) -> NoReturn:
159 """Testing make pod ingress resources with HTTPs."""
160 config = {
161 "site_url": "https://prometheus-kafka-exporter",
162 "max_file_size": 0,
163 "ingress_whitelist_source_range": "",
164 "tls_secret_name": "",
165 }
166 app_name = "prometheus-kafka-exporter"
167 port = 9308
168
169 expected_result = [
170 {
171 "name": f"{app_name}-ingress",
172 "annotations": {},
173 "spec": {
174 "rules": [
175 {
176 "host": app_name,
177 "http": {
178 "paths": [
179 {
180 "path": "/",
181 "backend": {
182 "serviceName": app_name,
183 "servicePort": port,
184 },
185 }
186 ]
187 },
188 }
189 ],
190 "tls": [{"hosts": [app_name]}],
191 },
192 }
193 ]
194
195 pod_ingress_resources = pod_spec._make_pod_ingress_resources(
196 config, app_name, port
197 )
198
199 self.assertListEqual(expected_result, pod_ingress_resources)
200
201 def test_make_pod_ingress_resources_with_https_tls_secret_name(self) -> NoReturn:
202 """Testing make pod ingress resources with HTTPs and TLS secret name."""
203 config = {
204 "site_url": "https://prometheus-kafka-exporter",
205 "max_file_size": 0,
206 "ingress_whitelist_source_range": "",
207 "tls_secret_name": "secret_name",
208 }
209 app_name = "prometheus-kafka-exporter"
210 port = 9308
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 = 9308
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 = 9308
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 "kakfa_host": "kafka",
288 "kafka_port": "9090",
289 }
290
291 expected_result = [
292 "kafka-exporter",
293 "--kafka.server={}:{}".format(
294 relation.get("kafka_host"), relation.get("kafka_port")
295 ),
296 ]
297
298 pod_envconfig = pod_spec._make_pod_command(relation)
299
300 self.assertListEqual(expected_result, pod_envconfig)
301
302 def test_make_pod_spec(self) -> NoReturn:
303 """Testing make pod spec."""
304 image_info = {"upstream-source": "bitnami/kafka-exporter:latest"}
305 config = {
306 "site_url": "",
307 }
308 relation_state = {
309 "kafka_host": "kafka",
310 "kafka_port": "9090",
311 }
312 app_name = "prometheus-kafka-exporter"
313 port = 9308
314
315 expected_result = {
316 "version": 3,
317 "containers": [
318 {
319 "name": app_name,
320 "imageDetails": image_info,
321 "imagePullPolicy": "Always",
322 "ports": [
323 {
324 "name": app_name,
325 "containerPort": port,
326 "protocol": "TCP",
327 }
328 ],
329 "envConfig": {},
330 "command": ["kafka-exporter", "--kafka.server=kafka:9090"],
331 "kubernetes": {
332 "readinessProbe": {
333 "httpGet": {
334 "path": "/api/health",
335 "port": port,
336 },
337 "initialDelaySeconds": 10,
338 "periodSeconds": 10,
339 "timeoutSeconds": 5,
340 "successThreshold": 1,
341 "failureThreshold": 3,
342 },
343 "livenessProbe": {
344 "httpGet": {
345 "path": "/api/health",
346 "port": port,
347 },
348 "initialDelaySeconds": 60,
349 "timeoutSeconds": 30,
350 "failureThreshold": 10,
351 },
352 },
353 }
354 ],
355 "kubernetesResources": {"ingressResources": []},
356 }
357
358 spec = pod_spec.make_pod_spec(
359 image_info, config, relation_state, app_name, port
360 )
361
362 self.assertDictEqual(expected_result, spec)
363
364 def test_make_pod_spec_with_ingress(self) -> NoReturn:
365 """Testing make pod spec."""
366 image_info = {"upstream-source": "bitnami/kafka-exporter:latest"}
367 config = {
368 "site_url": "https://prometheus-kafka-exporter",
369 "tls_secret_name": "prometheus-kafka-exporter",
370 "max_file_size": 0,
371 "ingress_whitelist_source_range": "0.0.0.0/0",
372 }
373 relation_state = {
374 "kafka_host": "kafka",
375 "kafka_port": "9090",
376 }
377 app_name = "prometheus-kafka-exporter"
378 port = 9308
379
380 expected_result = {
381 "version": 3,
382 "containers": [
383 {
384 "name": app_name,
385 "imageDetails": image_info,
386 "imagePullPolicy": "Always",
387 "ports": [
388 {
389 "name": app_name,
390 "containerPort": port,
391 "protocol": "TCP",
392 }
393 ],
394 "envConfig": {},
395 "command": ["kafka-exporter", "--kafka.server=kafka:9090"],
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 "kafka_host": "kafka",
472 "kafka_port": "9090",
473 }
474 app_name = "prometheus-kafka-exporter"
475 port = 9308
476
477 spec = pod_spec.make_pod_spec(
478 image_info, config, relation_state, app_name, port
479 )
480
481 self.assertIsNone(spec)
482
483 def test_make_pod_spec_without_relation_state(self) -> NoReturn:
484 """Testing make pod spec without relation_state."""
485 image_info = {"upstream-source": "bitnami/kafka-exporter:latest"}
486 config = {
487 "site_url": "",
488 }
489 relation_state = {}
490 app_name = "prometheus-kafka-exporter"
491 port = 9308
492
493 with self.assertRaises(ValueError):
494 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
495
496
497 if __name__ == "__main__":
498 unittest.main()