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": "mongo-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",
55 expected_result
= {"MONGODB_URI": "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
= "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://mongodb-exporter",
77 "ingress_whitelist_source_range": "",
79 app_name
= "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://mongodb-exporter",
119 "ingress_whitelist_source_range": "0.0.0.0/0",
121 app_name
= "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://mongodb-exporter",
164 "ingress_whitelist_source_range": "",
165 "tls_secret_name": "",
167 app_name
= "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://mongodb-exporter",
206 "ingress_whitelist_source_range": "",
207 "tls_secret_name": "secret_name",
209 app_name
= "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_spec(self
) -> NoReturn
:
285 """Testing make pod spec."""
286 image_info
= {"upstream-source": "bitnami/mongodb-exporter:latest"}
291 "mongodb_connection_string": "mongodb://mongo",
293 app_name
= "mongodb-exporter"
301 "imageDetails": image_info
,
302 "imagePullPolicy": "Always",
305 "name": "mongo-exporter",
306 "containerPort": port
,
311 "MONGODB_URI": "mongodb://mongo",
316 "path": "/api/health",
319 "initialDelaySeconds": 10,
322 "successThreshold": 1,
323 "failureThreshold": 3,
327 "path": "/api/health",
330 "initialDelaySeconds": 60,
331 "timeoutSeconds": 30,
332 "failureThreshold": 10,
337 "kubernetesResources": {"ingressResources": []},
340 spec
= pod_spec
.make_pod_spec(
341 image_info
, config
, relation_state
, app_name
, port
344 self
.assertDictEqual(expected_result
, spec
)
346 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
347 """Testing make pod spec."""
348 image_info
= {"upstream-source": "bitnami/mongodb-exporter:latest"}
350 "site_url": "https://mongodb-exporter",
351 "tls_secret_name": "mongodb-exporter",
352 "ingress_whitelist_source_range": "0.0.0.0/0",
355 "mongodb_connection_string": "mongodb://mongo",
357 app_name
= "mongodb-exporter"
365 "imageDetails": image_info
,
366 "imagePullPolicy": "Always",
369 "name": "mongo-exporter",
370 "containerPort": port
,
375 "MONGODB_URI": "mongodb://mongo",
380 "path": "/api/health",
383 "initialDelaySeconds": 10,
386 "successThreshold": 1,
387 "failureThreshold": 3,
391 "path": "/api/health",
394 "initialDelaySeconds": 60,
395 "timeoutSeconds": 30,
396 "failureThreshold": 10,
401 "kubernetesResources": {
402 "ingressResources": [
404 "name": "{}-ingress".format(app_name
),
406 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
407 "ingress_whitelist_source_range"
419 "serviceName": app_name
,
430 "secretName": config
.get("tls_secret_name"),
439 spec
= pod_spec
.make_pod_spec(
440 image_info
, config
, relation_state
, app_name
, port
443 self
.assertDictEqual(expected_result
, spec
)
445 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
446 """Testing make pod spec without image_info."""
452 "mongodb_connection_string": "mongodb://mongo",
454 app_name
= "mongodb-exporter"
457 spec
= pod_spec
.make_pod_spec(
458 image_info
, config
, relation_state
, app_name
, port
461 self
.assertIsNone(spec
)
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"}
470 app_name
= "mongodb-exporter"
473 with self
.assertRaises(ValueError):
474 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
477 if __name__
== "__main__":