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."""
67 app_name
= "mongodb-exporter"
70 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
71 config
, app_name
, port
74 self
.assertIsNone(pod_ingress_resources
)
76 def test_make_pod_ingress_resources(self
) -> NoReturn
:
77 """Testing make pod ingress resources."""
79 "site_url": "http://mongodb-exporter",
81 "ingress_whitelist_source_range": "",
83 app_name
= "mongodb-exporter"
88 "name": f
"{app_name}-ingress",
90 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
101 "serviceName": app_name
,
113 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
114 config
, app_name
, port
117 self
.assertListEqual(expected_result
, pod_ingress_resources
)
119 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
120 """Testing make pod ingress resources with whitelist_source_range."""
122 "site_url": "http://mongodb-exporter",
123 "cluster_issuer": "",
124 "ingress_whitelist_source_range": "0.0.0.0/0",
126 app_name
= "mongodb-exporter"
131 "name": f
"{app_name}-ingress",
133 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
134 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
135 "ingress_whitelist_source_range"
147 "serviceName": app_name
,
159 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
160 config
, app_name
, port
163 self
.assertListEqual(expected_result
, pod_ingress_resources
)
165 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
166 """Testing make pod ingress resources with HTTPs."""
168 "site_url": "https://mongodb-exporter",
169 "cluster_issuer": "",
170 "ingress_whitelist_source_range": "",
171 "tls_secret_name": "",
173 app_name
= "mongodb-exporter"
178 "name": f
"{app_name}-ingress",
189 "serviceName": app_name
,
197 "tls": [{"hosts": [app_name
]}],
202 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
203 config
, app_name
, port
206 self
.assertListEqual(expected_result
, pod_ingress_resources
)
208 def test_make_pod_ingress_resources_with_https_tls_secret_name(self
) -> NoReturn
:
209 """Testing make pod ingress resources with HTTPs and TLS secret name."""
211 "site_url": "https://mongodb-exporter",
212 "cluster_issuer": "",
213 "ingress_whitelist_source_range": "",
214 "tls_secret_name": "secret_name",
216 app_name
= "mongodb-exporter"
221 "name": f
"{app_name}-ingress",
232 "serviceName": app_name
,
241 {"hosts": [app_name
], "secretName": config
["tls_secret_name"]}
247 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
248 config
, app_name
, port
251 self
.assertListEqual(expected_result
, pod_ingress_resources
)
253 def test_make_readiness_probe(self
) -> NoReturn
:
254 """Testing make readiness probe."""
259 "path": "/api/health",
262 "initialDelaySeconds": 10,
265 "successThreshold": 1,
266 "failureThreshold": 3,
269 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
271 self
.assertDictEqual(expected_result
, readiness_probe
)
273 def test_make_liveness_probe(self
) -> NoReturn
:
274 """Testing make liveness probe."""
279 "path": "/api/health",
282 "initialDelaySeconds": 60,
283 "timeoutSeconds": 30,
284 "failureThreshold": 10,
287 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
289 self
.assertDictEqual(expected_result
, liveness_probe
)
291 def test_make_pod_spec(self
) -> NoReturn
:
292 """Testing make pod spec."""
293 image_info
= {"upstream-source": "bitnami/mongodb-exporter:latest"}
296 "cluster_issuer": "",
299 "mongodb_connection_string": "mongodb://mongo",
301 app_name
= "mongodb-exporter"
309 "imageDetails": image_info
,
310 "imagePullPolicy": "Always",
313 "name": "mongo-exporter",
314 "containerPort": port
,
319 "MONGODB_URI": "mongodb://mongo",
324 "path": "/api/health",
327 "initialDelaySeconds": 10,
330 "successThreshold": 1,
331 "failureThreshold": 3,
335 "path": "/api/health",
338 "initialDelaySeconds": 60,
339 "timeoutSeconds": 30,
340 "failureThreshold": 10,
345 "kubernetesResources": {"ingressResources": []},
348 spec
= pod_spec
.make_pod_spec(
349 image_info
, config
, relation_state
, app_name
, port
352 self
.assertDictEqual(expected_result
, spec
)
354 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
355 """Testing make pod spec."""
356 image_info
= {"upstream-source": "bitnami/mongodb-exporter:latest"}
358 "site_url": "https://mongodb-exporter",
359 "cluster_issuer": "",
360 "tls_secret_name": "mongodb-exporter",
361 "ingress_whitelist_source_range": "0.0.0.0/0",
364 "mongodb_connection_string": "mongodb://mongo",
366 app_name
= "mongodb-exporter"
374 "imageDetails": image_info
,
375 "imagePullPolicy": "Always",
378 "name": "mongo-exporter",
379 "containerPort": port
,
384 "MONGODB_URI": "mongodb://mongo",
389 "path": "/api/health",
392 "initialDelaySeconds": 10,
395 "successThreshold": 1,
396 "failureThreshold": 3,
400 "path": "/api/health",
403 "initialDelaySeconds": 60,
404 "timeoutSeconds": 30,
405 "failureThreshold": 10,
410 "kubernetesResources": {
411 "ingressResources": [
413 "name": "{}-ingress".format(app_name
),
415 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
416 "ingress_whitelist_source_range"
428 "serviceName": app_name
,
439 "secretName": config
.get("tls_secret_name"),
448 spec
= pod_spec
.make_pod_spec(
449 image_info
, config
, relation_state
, app_name
, port
452 self
.assertDictEqual(expected_result
, spec
)
454 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
455 """Testing make pod spec without image_info."""
459 "cluster_issuer": "",
462 "mongodb_connection_string": "mongodb://mongo",
464 app_name
= "mongodb-exporter"
467 spec
= pod_spec
.make_pod_spec(
468 image_info
, config
, relation_state
, app_name
, port
471 self
.assertIsNone(spec
)
473 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
474 """Testing make pod spec without relation_state."""
475 image_info
= {"upstream-source": "bitnami/mongodb-exporter:latest"}
478 "cluster_issuer": "",
481 app_name
= "mongodb-exporter"
484 with self
.assertRaises(ValueError):
485 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
488 if __name__
== "__main__":