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": "mysqld-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 "mysql_host": "mysql",
55 "mysql_password": "manopw",
56 "mysql_root_password": "rootpw",
60 "DATA_SOURCE_NAME": "root:{mysql_root_password}@({mysql_host}:{mysql_port})/".format(
65 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
67 self
.assertDictEqual(expected_result
, pod_envconfig
)
69 def test_make_pod_ingress_resources_without_site_url(self
) -> NoReturn
:
70 """Testing make pod ingress resources without site_url."""
75 app_name
= "mysqld-exporter"
78 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
79 config
, app_name
, port
82 self
.assertIsNone(pod_ingress_resources
)
84 def test_make_pod_ingress_resources(self
) -> NoReturn
:
85 """Testing make pod ingress resources."""
87 "site_url": "http://mysqld-exporter",
89 "ingress_whitelist_source_range": "",
91 app_name
= "mysqld-exporter"
96 "name": f
"{app_name}-ingress",
98 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
109 "serviceName": app_name
,
121 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
122 config
, app_name
, port
125 self
.assertListEqual(expected_result
, pod_ingress_resources
)
127 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
128 """Testing make pod ingress resources with whitelist_source_range."""
130 "site_url": "http://mysqld-exporter",
131 "cluster_issuer": "",
132 "ingress_whitelist_source_range": "0.0.0.0/0",
134 app_name
= "mysqld-exporter"
139 "name": f
"{app_name}-ingress",
141 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
142 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
143 "ingress_whitelist_source_range"
155 "serviceName": app_name
,
167 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
168 config
, app_name
, port
171 self
.assertListEqual(expected_result
, pod_ingress_resources
)
173 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
174 """Testing make pod ingress resources with HTTPs."""
176 "site_url": "https://mysqld-exporter",
177 "cluster_issuer": "",
178 "ingress_whitelist_source_range": "",
179 "tls_secret_name": "",
181 app_name
= "mysqld-exporter"
186 "name": f
"{app_name}-ingress",
197 "serviceName": app_name
,
205 "tls": [{"hosts": [app_name
]}],
210 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
211 config
, app_name
, port
214 self
.assertListEqual(expected_result
, pod_ingress_resources
)
216 def test_make_pod_ingress_resources_with_https_tls_secret_name(self
) -> NoReturn
:
217 """Testing make pod ingress resources with HTTPs and TLS secret name."""
219 "site_url": "https://mysqld-exporter",
220 "cluster_issuer": "",
221 "ingress_whitelist_source_range": "",
222 "tls_secret_name": "secret_name",
224 app_name
= "mysqld-exporter"
229 "name": f
"{app_name}-ingress",
240 "serviceName": app_name
,
249 {"hosts": [app_name
], "secretName": config
["tls_secret_name"]}
255 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
256 config
, app_name
, port
259 self
.assertListEqual(expected_result
, pod_ingress_resources
)
261 def test_make_readiness_probe(self
) -> NoReturn
:
262 """Testing make readiness probe."""
267 "path": "/api/health",
270 "initialDelaySeconds": 10,
273 "successThreshold": 1,
274 "failureThreshold": 3,
277 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
279 self
.assertDictEqual(expected_result
, readiness_probe
)
281 def test_make_liveness_probe(self
) -> NoReturn
:
282 """Testing make liveness probe."""
287 "path": "/api/health",
290 "initialDelaySeconds": 60,
291 "timeoutSeconds": 30,
292 "failureThreshold": 10,
295 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
297 self
.assertDictEqual(expected_result
, liveness_probe
)
299 def test_make_pod_spec(self
) -> NoReturn
:
300 """Testing make pod spec."""
301 image_info
= {"upstream-source": "bitnami/mysqld-exporter:latest"}
304 "cluster_issuer": "",
307 "mysql_host": "mysql",
308 "mysql_port": "3306",
309 "mysql_user": "mano",
310 "mysql_password": "manopw",
311 "mysql_root_password": "rootpw",
313 app_name
= "mysqld-exporter"
321 "imageDetails": image_info
,
322 "imagePullPolicy": "Always",
326 "containerPort": port
,
331 "DATA_SOURCE_NAME": "root:{mysql_root_password}@({mysql_host}:{mysql_port})/".format(
338 "path": "/api/health",
341 "initialDelaySeconds": 10,
344 "successThreshold": 1,
345 "failureThreshold": 3,
349 "path": "/api/health",
352 "initialDelaySeconds": 60,
353 "timeoutSeconds": 30,
354 "failureThreshold": 10,
359 "kubernetesResources": {"ingressResources": []},
362 spec
= pod_spec
.make_pod_spec(
363 image_info
, config
, relation_state
, app_name
, port
366 self
.assertDictEqual(expected_result
, spec
)
368 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
369 """Testing make pod spec."""
370 image_info
= {"upstream-source": "bitnami/mysqld-exporter:latest"}
372 "site_url": "https://mysqld-exporter",
373 "cluster_issuer": "",
374 "tls_secret_name": "mysqld-exporter",
375 "ingress_whitelist_source_range": "0.0.0.0/0",
378 "mysql_host": "mysql",
379 "mysql_port": "3306",
380 "mysql_user": "mano",
381 "mysql_password": "manopw",
382 "mysql_root_password": "rootpw",
384 app_name
= "mysqld-exporter"
392 "imageDetails": image_info
,
393 "imagePullPolicy": "Always",
397 "containerPort": port
,
402 "DATA_SOURCE_NAME": "root:{mysql_root_password}@({mysql_host}:{mysql_port})/".format(
409 "path": "/api/health",
412 "initialDelaySeconds": 10,
415 "successThreshold": 1,
416 "failureThreshold": 3,
420 "path": "/api/health",
423 "initialDelaySeconds": 60,
424 "timeoutSeconds": 30,
425 "failureThreshold": 10,
430 "kubernetesResources": {
431 "ingressResources": [
433 "name": "{}-ingress".format(app_name
),
435 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
436 "ingress_whitelist_source_range"
448 "serviceName": app_name
,
459 "secretName": config
.get("tls_secret_name"),
468 spec
= pod_spec
.make_pod_spec(
469 image_info
, config
, relation_state
, app_name
, port
472 self
.assertDictEqual(expected_result
, spec
)
474 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
475 """Testing make pod spec without image_info."""
479 "cluster_issuer": "",
482 "mysql_host": "mysql",
484 "mysql_user": "mano",
485 "mysql_password": "manopw",
486 "mysql_root_password": "rootpw",
488 app_name
= "mysqld-exporter"
491 spec
= pod_spec
.make_pod_spec(
492 image_info
, config
, relation_state
, app_name
, port
495 self
.assertIsNone(spec
)
497 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
498 """Testing make pod spec without relation_state."""
499 image_info
= {"upstream-source": "bitnami/mysqld-exporter:latest"}
502 "cluster_issuer": "",
505 app_name
= "mysqld-exporter"
508 with self
.assertRaises(ValueError):
509 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
512 if __name__
== "__main__":