3e312f487be979e32cd31771de38d40f04c506d5
[osm/devops.git] / installers / charm / 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": "mongo-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 = {"MONGODB_URI": "mongodb://mongo"}
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 = "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://mongodb-exporter",
77 "ingress_whitelist_source_range": "",
78 }
79 app_name = "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://mongodb-exporter",
119 "ingress_whitelist_source_range": "0.0.0.0/0",
120 }
121 app_name = "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://mongodb-exporter",
164 "ingress_whitelist_source_range": "",
165 "tls_secret_name": "",
166 }
167 app_name = "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://mongodb-exporter",
206 "ingress_whitelist_source_range": "",
207 "tls_secret_name": "secret_name",
208 }
209 app_name = "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_spec(self) -> NoReturn:
285 """Testing make pod spec."""
286 image_info = {"upstream-source": "bitnami/mongodb-exporter:latest"}
287 config = {
288 "site_url": "",
289 }
290 relation_state = {
291 "mongodb_connection_string": "mongodb://mongo",
292 }
293 app_name = "mongodb-exporter"
294 port = 9216
295
296 expected_result = {
297 "version": 3,
298 "containers": [
299 {
300 "name": app_name,
301 "imageDetails": image_info,
302 "imagePullPolicy": "Always",
303 "ports": [
304 {
305 "name": "mongo-exporter",
306 "containerPort": port,
307 "protocol": "TCP",
308 }
309 ],
310 "envConfig": {
311 "MONGODB_URI": "mongodb://mongo",
312 },
313 "kubernetes": {
314 "readinessProbe": {
315 "httpGet": {
316 "path": "/api/health",
317 "port": port,
318 },
319 "initialDelaySeconds": 10,
320 "periodSeconds": 10,
321 "timeoutSeconds": 5,
322 "successThreshold": 1,
323 "failureThreshold": 3,
324 },
325 "livenessProbe": {
326 "httpGet": {
327 "path": "/api/health",
328 "port": port,
329 },
330 "initialDelaySeconds": 60,
331 "timeoutSeconds": 30,
332 "failureThreshold": 10,
333 },
334 },
335 }
336 ],
337 "kubernetesResources": {"ingressResources": []},
338 }
339
340 spec = pod_spec.make_pod_spec(
341 image_info, config, relation_state, app_name, port
342 )
343
344 self.assertDictEqual(expected_result, spec)
345
346 def test_make_pod_spec_with_ingress(self) -> NoReturn:
347 """Testing make pod spec."""
348 image_info = {"upstream-source": "bitnami/mongodb-exporter:latest"}
349 config = {
350 "site_url": "https://mongodb-exporter",
351 "tls_secret_name": "mongodb-exporter",
352 "ingress_whitelist_source_range": "0.0.0.0/0",
353 }
354 relation_state = {
355 "mongodb_connection_string": "mongodb://mongo",
356 }
357 app_name = "mongodb-exporter"
358 port = 9216
359
360 expected_result = {
361 "version": 3,
362 "containers": [
363 {
364 "name": app_name,
365 "imageDetails": image_info,
366 "imagePullPolicy": "Always",
367 "ports": [
368 {
369 "name": "mongo-exporter",
370 "containerPort": port,
371 "protocol": "TCP",
372 }
373 ],
374 "envConfig": {
375 "MONGODB_URI": "mongodb://mongo",
376 },
377 "kubernetes": {
378 "readinessProbe": {
379 "httpGet": {
380 "path": "/api/health",
381 "port": port,
382 },
383 "initialDelaySeconds": 10,
384 "periodSeconds": 10,
385 "timeoutSeconds": 5,
386 "successThreshold": 1,
387 "failureThreshold": 3,
388 },
389 "livenessProbe": {
390 "httpGet": {
391 "path": "/api/health",
392 "port": port,
393 },
394 "initialDelaySeconds": 60,
395 "timeoutSeconds": 30,
396 "failureThreshold": 10,
397 },
398 },
399 }
400 ],
401 "kubernetesResources": {
402 "ingressResources": [
403 {
404 "name": "{}-ingress".format(app_name),
405 "annotations": {
406 "nginx.ingress.kubernetes.io/whitelist-source-range": config.get(
407 "ingress_whitelist_source_range"
408 ),
409 },
410 "spec": {
411 "rules": [
412 {
413 "host": app_name,
414 "http": {
415 "paths": [
416 {
417 "path": "/",
418 "backend": {
419 "serviceName": app_name,
420 "servicePort": port,
421 },
422 }
423 ]
424 },
425 }
426 ],
427 "tls": [
428 {
429 "hosts": [app_name],
430 "secretName": config.get("tls_secret_name"),
431 }
432 ],
433 },
434 }
435 ],
436 },
437 }
438
439 spec = pod_spec.make_pod_spec(
440 image_info, config, relation_state, app_name, port
441 )
442
443 self.assertDictEqual(expected_result, spec)
444
445 def test_make_pod_spec_without_image_info(self) -> NoReturn:
446 """Testing make pod spec without image_info."""
447 image_info = None
448 config = {
449 "site_url": "",
450 }
451 relation_state = {
452 "mongodb_connection_string": "mongodb://mongo",
453 }
454 app_name = "mongodb-exporter"
455 port = 9216
456
457 spec = pod_spec.make_pod_spec(
458 image_info, config, relation_state, app_name, port
459 )
460
461 self.assertIsNone(spec)
462
463 def test_make_pod_spec_without_relation_state(self) -> NoReturn:
464 """Testing make pod spec without relation_state."""
465 image_info = {"upstream-source": "bitnami/mongodb-exporter:latest"}
466 config = {
467 "site_url": "",
468 }
469 relation_state = {}
470 app_name = "mongodb-exporter"
471 port = 9216
472
473 with self.assertRaises(ValueError):
474 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
475
476
477 if __name__ == "__main__":
478 unittest.main()