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": "kafka-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."""
55 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
57 self
.assertDictEqual(expected_result
, pod_envconfig
)
59 def test_make_pod_ingress_resources_without_site_url(self
) -> NoReturn
:
60 """Testing make pod ingress resources without site_url."""
65 app_name
= "kafka-exporter"
68 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
69 config
, app_name
, port
72 self
.assertIsNone(pod_ingress_resources
)
74 def test_make_pod_ingress_resources(self
) -> NoReturn
:
75 """Testing make pod ingress resources."""
78 "site_url": "http://kafka-exporter",
79 "ingress_whitelist_source_range": "",
81 app_name
= "kafka-exporter"
86 "name": f
"{app_name}-ingress",
88 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
99 "serviceName": app_name
,
111 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
112 config
, app_name
, port
115 self
.assertListEqual(expected_result
, pod_ingress_resources
)
117 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
118 """Testing make pod ingress resources with whitelist_source_range."""
120 "site_url": "http://kafka-exporter",
121 "cluster_issuer": "",
122 "ingress_whitelist_source_range": "0.0.0.0/0",
124 app_name
= "kafka-exporter"
129 "name": f
"{app_name}-ingress",
131 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
132 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
133 "ingress_whitelist_source_range"
145 "serviceName": app_name
,
157 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
158 config
, app_name
, port
161 self
.assertListEqual(expected_result
, pod_ingress_resources
)
163 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
164 """Testing make pod ingress resources with HTTPs."""
166 "site_url": "https://kafka-exporter",
168 "cluster_issuer": "",
169 "ingress_whitelist_source_range": "",
170 "tls_secret_name": "",
172 app_name
= "kafka-exporter"
177 "name": f
"{app_name}-ingress",
188 "serviceName": app_name
,
196 "tls": [{"hosts": [app_name
]}],
201 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
202 config
, app_name
, port
205 self
.assertListEqual(expected_result
, pod_ingress_resources
)
207 def test_make_pod_ingress_resources_with_https_tls_secret_name(self
) -> NoReturn
:
208 """Testing make pod ingress resources with HTTPs and TLS secret name."""
210 "site_url": "https://kafka-exporter",
212 "cluster_issuer": "",
213 "ingress_whitelist_source_range": "",
214 "tls_secret_name": "secret_name",
216 app_name
= "kafka-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_command(self
) -> NoReturn
:
292 """Testing make pod command."""
294 "kakfa_host": "kafka",
295 "kafka_port": "9090",
300 "--kafka.server={}:{}".format(
301 relation
.get("kafka_host"), relation
.get("kafka_port")
305 pod_envconfig
= pod_spec
._make
_pod
_command
(relation
)
307 self
.assertListEqual(expected_result
, pod_envconfig
)
309 def test_make_pod_spec(self
) -> NoReturn
:
310 """Testing make pod spec."""
311 image_info
= {"upstream-source": "bitnami/kafka-exporter:latest"}
314 "cluster_issuer": "",
317 "kafka_host": "kafka",
318 "kafka_port": "9090",
320 app_name
= "kafka-exporter"
328 "imageDetails": image_info
,
329 "imagePullPolicy": "Always",
333 "containerPort": port
,
338 "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
342 "path": "/api/health",
345 "initialDelaySeconds": 10,
348 "successThreshold": 1,
349 "failureThreshold": 3,
353 "path": "/api/health",
356 "initialDelaySeconds": 60,
357 "timeoutSeconds": 30,
358 "failureThreshold": 10,
363 "kubernetesResources": {"ingressResources": []},
366 spec
= pod_spec
.make_pod_spec(
367 image_info
, config
, relation_state
, app_name
, port
370 self
.assertDictEqual(expected_result
, spec
)
372 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
373 """Testing make pod spec."""
374 image_info
= {"upstream-source": "bitnami/kafka-exporter:latest"}
376 "site_url": "https://kafka-exporter",
377 "cluster_issuer": "",
378 "tls_secret_name": "kafka-exporter",
380 "ingress_whitelist_source_range": "0.0.0.0/0",
383 "kafka_host": "kafka",
384 "kafka_port": "9090",
386 app_name
= "kafka-exporter"
394 "imageDetails": image_info
,
395 "imagePullPolicy": "Always",
399 "containerPort": port
,
404 "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
408 "path": "/api/health",
411 "initialDelaySeconds": 10,
414 "successThreshold": 1,
415 "failureThreshold": 3,
419 "path": "/api/health",
422 "initialDelaySeconds": 60,
423 "timeoutSeconds": 30,
424 "failureThreshold": 10,
429 "kubernetesResources": {
430 "ingressResources": [
432 "name": "{}-ingress".format(app_name
),
434 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
435 "ingress_whitelist_source_range"
447 "serviceName": app_name
,
458 "secretName": config
.get("tls_secret_name"),
467 spec
= pod_spec
.make_pod_spec(
468 image_info
, config
, relation_state
, app_name
, port
471 self
.assertDictEqual(expected_result
, spec
)
473 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
474 """Testing make pod spec without image_info."""
478 "cluster_issuer": "",
481 "kafka_host": "kafka",
482 "kafka_port": "9090",
484 app_name
= "kafka-exporter"
487 spec
= pod_spec
.make_pod_spec(
488 image_info
, config
, relation_state
, app_name
, port
491 self
.assertIsNone(spec
)
493 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
494 """Testing make pod spec without relation_state."""
495 image_info
= {"upstream-source": "bitnami/kafka-exporter:latest"}
498 "cluster_issuer": "",
501 app_name
= "kafka-exporter"
504 with self
.assertRaises(ValueError):
505 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
508 if __name__
== "__main__":