2 # Copyright 2021 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 from typing
import NoReturn
29 class TestPodSpec(unittest
.TestCase
):
30 """Pod spec unit tests."""
32 def test_make_pod_ports(self
) -> NoReturn
:
33 """Testing make pod ports."""
38 "name": "prometheus-mongodb-exporter",
39 "containerPort": port
,
44 pod_ports
= pod_spec
._make
_pod
_ports
(port
)
46 self
.assertListEqual(expected_result
, pod_ports
)
48 def test_make_pod_envconfig(self
) -> NoReturn
:
49 """Teting make pod envconfig."""
52 "mongodb_connection_string": "mongodb://mongo",
57 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
59 self
.assertDictEqual(expected_result
, pod_envconfig
)
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"
67 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
68 config
, app_name
, port
71 self
.assertIsNone(pod_ingress_resources
)
73 def test_make_pod_ingress_resources(self
) -> NoReturn
:
74 """Testing make pod ingress resources."""
76 "site_url": "http://prometheus-mongodb-exporter",
77 "ingress_whitelist_source_range": "",
79 app_name
= "prometheus-mongodb-exporter"
84 "name": f
"{app_name}-ingress",
86 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
97 "serviceName": app_name
,
109 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
110 config
, app_name
, port
113 self
.assertListEqual(expected_result
, pod_ingress_resources
)
115 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
116 """Testing make pod ingress resources with whitelist_source_range."""
118 "site_url": "http://prometheus-mongodb-exporter",
119 "ingress_whitelist_source_range": "0.0.0.0/0",
121 app_name
= "prometheus-mongodb-exporter"
126 "name": f
"{app_name}-ingress",
128 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
129 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
130 "ingress_whitelist_source_range"
142 "serviceName": app_name
,
154 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
155 config
, app_name
, port
158 self
.assertListEqual(expected_result
, pod_ingress_resources
)
160 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
161 """Testing make pod ingress resources with HTTPs."""
163 "site_url": "https://prometheus-mongodb-exporter",
164 "ingress_whitelist_source_range": "",
165 "tls_secret_name": "",
167 app_name
= "prometheus-mongodb-exporter"
172 "name": f
"{app_name}-ingress",
183 "serviceName": app_name
,
191 "tls": [{"hosts": [app_name
]}],
196 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
197 config
, app_name
, port
200 self
.assertListEqual(expected_result
, pod_ingress_resources
)
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."""
205 "site_url": "https://prometheus-mongodb-exporter",
206 "ingress_whitelist_source_range": "",
207 "tls_secret_name": "secret_name",
209 app_name
= "prometheus-mongodb-exporter"
214 "name": f
"{app_name}-ingress",
225 "serviceName": app_name
,
234 {"hosts": [app_name
], "secretName": config
["tls_secret_name"]}
240 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
241 config
, app_name
, port
244 self
.assertListEqual(expected_result
, pod_ingress_resources
)
246 def test_make_readiness_probe(self
) -> NoReturn
:
247 """Testing make readiness probe."""
252 "path": "/api/health",
255 "initialDelaySeconds": 10,
258 "successThreshold": 1,
259 "failureThreshold": 3,
262 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
264 self
.assertDictEqual(expected_result
, readiness_probe
)
266 def test_make_liveness_probe(self
) -> NoReturn
:
267 """Testing make liveness probe."""
272 "path": "/api/health",
275 "initialDelaySeconds": 60,
276 "timeoutSeconds": 30,
277 "failureThreshold": 10,
280 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
282 self
.assertDictEqual(expected_result
, liveness_probe
)
284 def test_make_pod_command(self
) -> NoReturn
:
285 """Testing make pod command."""
287 "mongodb_connection_string": "mongodb://mongo",
291 "mongodb_exporter_linux_amd64/mongodb_exporter",
292 "--mongodbdsn={}".format(relation
.get("mongodb_connection_string")),
295 pod_envconfig
= pod_spec
._make
_pod
_command
(relation
)
297 self
.assertListEqual(expected_result
, pod_envconfig
)
299 def test_make_pod_spec(self
) -> NoReturn
:
300 """Testing make pod spec."""
301 image_info
= {"upstream-source": "bitnami/mongodb-exporter:latest"}
306 "mongodb_connection_string": "mongodb://mongo",
308 app_name
= "prometheus-mongodb-exporter"
316 "imageDetails": image_info
,
317 "imagePullPolicy": "Always",
321 "containerPort": port
,
327 "mongodb_exporter_linux_amd64/mongodb_exporter",
328 "--mongodbdsn=mongodb://mongo",
333 "path": "/api/health",
336 "initialDelaySeconds": 10,
339 "successThreshold": 1,
340 "failureThreshold": 3,
344 "path": "/api/health",
347 "initialDelaySeconds": 60,
348 "timeoutSeconds": 30,
349 "failureThreshold": 10,
354 "kubernetesResources": {"ingressResources": []},
357 spec
= pod_spec
.make_pod_spec(
358 image_info
, config
, relation_state
, app_name
, port
361 self
.assertDictEqual(expected_result
, spec
)
363 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
364 """Testing make pod spec."""
365 image_info
= {"upstream-source": "bitnami/mongodb-exporter:latest"}
367 "site_url": "https://prometheus-mongodb-exporter",
368 "tls_secret_name": "prometheus-mongodb-exporter",
369 "ingress_whitelist_source_range": "0.0.0.0/0",
372 "mongodb_connection_string": "mongodb://mongo",
374 app_name
= "prometheus-mongodb-exporter"
382 "imageDetails": image_info
,
383 "imagePullPolicy": "Always",
387 "containerPort": port
,
393 "mongodb_exporter_linux_amd64/mongodb_exporter",
394 "--mongodbdsn=mongodb://mongo",
399 "path": "/api/health",
402 "initialDelaySeconds": 10,
405 "successThreshold": 1,
406 "failureThreshold": 3,
410 "path": "/api/health",
413 "initialDelaySeconds": 60,
414 "timeoutSeconds": 30,
415 "failureThreshold": 10,
420 "kubernetesResources": {
421 "ingressResources": [
423 "name": "{}-ingress".format(app_name
),
425 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
426 "ingress_whitelist_source_range"
438 "serviceName": app_name
,
449 "secretName": config
.get("tls_secret_name"),
458 spec
= pod_spec
.make_pod_spec(
459 image_info
, config
, relation_state
, app_name
, port
462 self
.assertDictEqual(expected_result
, spec
)
464 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
465 """Testing make pod spec without image_info."""
471 "mongodb_connection_string": "mongodb://mongo",
473 app_name
= "prometheus-mongodb-exporter"
476 spec
= pod_spec
.make_pod_spec(
477 image_info
, config
, relation_state
, app_name
, port
480 self
.assertIsNone(spec
)
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"}
489 app_name
= "prometheus-mongodb-exporter"
492 with self
.assertRaises(ValueError):
493 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
496 if __name__
== "__main__":