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."""
61 config
= {"site_url": ""}
62 app_name
= "kafka-exporter"
65 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
66 config
, app_name
, port
69 self
.assertIsNone(pod_ingress_resources
)
71 def test_make_pod_ingress_resources(self
) -> NoReturn
:
72 """Testing make pod ingress resources."""
74 "site_url": "http://kafka-exporter",
75 "ingress_whitelist_source_range": "",
77 app_name
= "kafka-exporter"
82 "name": f
"{app_name}-ingress",
84 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
95 "serviceName": app_name
,
107 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
108 config
, app_name
, port
111 self
.assertListEqual(expected_result
, pod_ingress_resources
)
113 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
114 """Testing make pod ingress resources with whitelist_source_range."""
116 "site_url": "http://kafka-exporter",
117 "ingress_whitelist_source_range": "0.0.0.0/0",
119 app_name
= "kafka-exporter"
124 "name": f
"{app_name}-ingress",
126 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
127 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
128 "ingress_whitelist_source_range"
140 "serviceName": app_name
,
152 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
153 config
, app_name
, port
156 self
.assertListEqual(expected_result
, pod_ingress_resources
)
158 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
159 """Testing make pod ingress resources with HTTPs."""
161 "site_url": "https://kafka-exporter",
163 "ingress_whitelist_source_range": "",
164 "tls_secret_name": "",
166 app_name
= "kafka-exporter"
171 "name": f
"{app_name}-ingress",
182 "serviceName": app_name
,
190 "tls": [{"hosts": [app_name
]}],
195 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
196 config
, app_name
, port
199 self
.assertListEqual(expected_result
, pod_ingress_resources
)
201 def test_make_pod_ingress_resources_with_https_tls_secret_name(self
) -> NoReturn
:
202 """Testing make pod ingress resources with HTTPs and TLS secret name."""
204 "site_url": "https://kafka-exporter",
206 "ingress_whitelist_source_range": "",
207 "tls_secret_name": "secret_name",
209 app_name
= "kafka-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 "kakfa_host": "kafka",
288 "kafka_port": "9090",
293 "--kafka.server={}:{}".format(
294 relation
.get("kafka_host"), relation
.get("kafka_port")
298 pod_envconfig
= pod_spec
._make
_pod
_command
(relation
)
300 self
.assertListEqual(expected_result
, pod_envconfig
)
302 def test_make_pod_spec(self
) -> NoReturn
:
303 """Testing make pod spec."""
304 image_info
= {"upstream-source": "bitnami/kafka-exporter:latest"}
309 "kafka_host": "kafka",
310 "kafka_port": "9090",
312 app_name
= "kafka-exporter"
320 "imageDetails": image_info
,
321 "imagePullPolicy": "Always",
325 "containerPort": port
,
330 "command": ["kafka-exporter", "--kafka.server=kafka:9090"],
334 "path": "/api/health",
337 "initialDelaySeconds": 10,
340 "successThreshold": 1,
341 "failureThreshold": 3,
345 "path": "/api/health",
348 "initialDelaySeconds": 60,
349 "timeoutSeconds": 30,
350 "failureThreshold": 10,
355 "kubernetesResources": {"ingressResources": []},
358 spec
= pod_spec
.make_pod_spec(
359 image_info
, config
, relation_state
, app_name
, port
362 self
.assertDictEqual(expected_result
, spec
)
364 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
365 """Testing make pod spec."""
366 image_info
= {"upstream-source": "bitnami/kafka-exporter:latest"}
368 "site_url": "https://kafka-exporter",
369 "tls_secret_name": "kafka-exporter",
371 "ingress_whitelist_source_range": "0.0.0.0/0",
374 "kafka_host": "kafka",
375 "kafka_port": "9090",
377 app_name
= "kafka-exporter"
385 "imageDetails": image_info
,
386 "imagePullPolicy": "Always",
390 "containerPort": port
,
395 "command": ["kafka-exporter", "--kafka.server=kafka:9090"],
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 "kafka_host": "kafka",
472 "kafka_port": "9090",
474 app_name
= "kafka-exporter"
477 spec
= pod_spec
.make_pod_spec(
478 image_info
, config
, relation_state
, app_name
, port
481 self
.assertIsNone(spec
)
483 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
484 """Testing make pod spec without relation_state."""
485 image_info
= {"upstream-source": "bitnami/kafka-exporter:latest"}
490 app_name
= "kafka-exporter"
493 with self
.assertRaises(ValueError):
494 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
497 if __name__
== "__main__":