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."""
71 config
= {"site_url": ""}
72 app_name
= "mysqld-exporter"
75 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
76 config
, app_name
, port
79 self
.assertIsNone(pod_ingress_resources
)
81 def test_make_pod_ingress_resources(self
) -> NoReturn
:
82 """Testing make pod ingress resources."""
84 "site_url": "http://mysqld-exporter",
85 "ingress_whitelist_source_range": "",
87 app_name
= "mysqld-exporter"
92 "name": f
"{app_name}-ingress",
94 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
105 "serviceName": app_name
,
117 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
118 config
, app_name
, port
121 self
.assertListEqual(expected_result
, pod_ingress_resources
)
123 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
124 """Testing make pod ingress resources with whitelist_source_range."""
126 "site_url": "http://mysqld-exporter",
127 "ingress_whitelist_source_range": "0.0.0.0/0",
129 app_name
= "mysqld-exporter"
134 "name": f
"{app_name}-ingress",
136 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
137 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
138 "ingress_whitelist_source_range"
150 "serviceName": app_name
,
162 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
163 config
, app_name
, port
166 self
.assertListEqual(expected_result
, pod_ingress_resources
)
168 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
169 """Testing make pod ingress resources with HTTPs."""
171 "site_url": "https://mysqld-exporter",
172 "ingress_whitelist_source_range": "",
173 "tls_secret_name": "",
175 app_name
= "mysqld-exporter"
180 "name": f
"{app_name}-ingress",
191 "serviceName": app_name
,
199 "tls": [{"hosts": [app_name
]}],
204 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
205 config
, app_name
, port
208 self
.assertListEqual(expected_result
, pod_ingress_resources
)
210 def test_make_pod_ingress_resources_with_https_tls_secret_name(self
) -> NoReturn
:
211 """Testing make pod ingress resources with HTTPs and TLS secret name."""
213 "site_url": "https://mysqld-exporter",
214 "ingress_whitelist_source_range": "",
215 "tls_secret_name": "secret_name",
217 app_name
= "mysqld-exporter"
222 "name": f
"{app_name}-ingress",
233 "serviceName": app_name
,
242 {"hosts": [app_name
], "secretName": config
["tls_secret_name"]}
248 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
249 config
, app_name
, port
252 self
.assertListEqual(expected_result
, pod_ingress_resources
)
254 def test_make_readiness_probe(self
) -> NoReturn
:
255 """Testing make readiness probe."""
260 "path": "/api/health",
263 "initialDelaySeconds": 10,
266 "successThreshold": 1,
267 "failureThreshold": 3,
270 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
272 self
.assertDictEqual(expected_result
, readiness_probe
)
274 def test_make_liveness_probe(self
) -> NoReturn
:
275 """Testing make liveness probe."""
280 "path": "/api/health",
283 "initialDelaySeconds": 60,
284 "timeoutSeconds": 30,
285 "failureThreshold": 10,
288 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
290 self
.assertDictEqual(expected_result
, liveness_probe
)
292 def test_make_pod_spec(self
) -> NoReturn
:
293 """Testing make pod spec."""
294 image_info
= {"upstream-source": "bitnami/mysqld-exporter:latest"}
299 "mysql_host": "mysql",
300 "mysql_port": "3306",
301 "mysql_user": "mano",
302 "mysql_password": "manopw",
303 "mysql_root_password": "rootpw",
305 app_name
= "mysqld-exporter"
313 "imageDetails": image_info
,
314 "imagePullPolicy": "Always",
318 "containerPort": port
,
323 "DATA_SOURCE_NAME": "root:{mysql_root_password}@({mysql_host}:{mysql_port})/".format(
330 "path": "/api/health",
333 "initialDelaySeconds": 10,
336 "successThreshold": 1,
337 "failureThreshold": 3,
341 "path": "/api/health",
344 "initialDelaySeconds": 60,
345 "timeoutSeconds": 30,
346 "failureThreshold": 10,
351 "kubernetesResources": {"ingressResources": []},
354 spec
= pod_spec
.make_pod_spec(
355 image_info
, config
, relation_state
, app_name
, port
358 self
.assertDictEqual(expected_result
, spec
)
360 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
361 """Testing make pod spec."""
362 image_info
= {"upstream-source": "bitnami/mysqld-exporter:latest"}
364 "site_url": "https://mysqld-exporter",
365 "tls_secret_name": "mysqld-exporter",
366 "ingress_whitelist_source_range": "0.0.0.0/0",
369 "mysql_host": "mysql",
370 "mysql_port": "3306",
371 "mysql_user": "mano",
372 "mysql_password": "manopw",
373 "mysql_root_password": "rootpw",
375 app_name
= "mysqld-exporter"
383 "imageDetails": image_info
,
384 "imagePullPolicy": "Always",
388 "containerPort": port
,
393 "DATA_SOURCE_NAME": "root:{mysql_root_password}@({mysql_host}:{mysql_port})/".format(
400 "path": "/api/health",
403 "initialDelaySeconds": 10,
406 "successThreshold": 1,
407 "failureThreshold": 3,
411 "path": "/api/health",
414 "initialDelaySeconds": 60,
415 "timeoutSeconds": 30,
416 "failureThreshold": 10,
421 "kubernetesResources": {
422 "ingressResources": [
424 "name": "{}-ingress".format(app_name
),
426 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
427 "ingress_whitelist_source_range"
439 "serviceName": app_name
,
450 "secretName": config
.get("tls_secret_name"),
459 spec
= pod_spec
.make_pod_spec(
460 image_info
, config
, relation_state
, app_name
, port
463 self
.assertDictEqual(expected_result
, spec
)
465 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
466 """Testing make pod spec without image_info."""
472 "mysql_host": "mysql",
474 "mysql_user": "mano",
475 "mysql_password": "manopw",
476 "mysql_root_password": "rootpw",
478 app_name
= "mysqld-exporter"
481 spec
= pod_spec
.make_pod_spec(
482 image_info
, config
, relation_state
, app_name
, port
485 self
.assertIsNone(spec
)
487 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
488 """Testing make pod spec without relation_state."""
489 image_info
= {"upstream-source": "bitnami/mysqld-exporter:latest"}
494 app_name
= "mysqld-exporter"
497 with self
.assertRaises(ValueError):
498 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
501 if __name__
== "__main__":