2 # Copyright 2020 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."""
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 """Testing 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
= "prometheus"
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://prometheus",
76 "ingress_whitelist_source_range": "",
78 app_name
= "prometheus"
83 "name": f
"{app_name}-ingress",
85 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
86 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
97 "serviceName": app_name
,
109 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
110 config
, app_name
, port
113 self
.assertListEqual(expected_result
, pod_ingress_resources
)
115 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
116 """Testing make pod ingress resources with whitelist_source_range."""
118 "site_url": "http://prometheus",
120 "ingress_whitelist_source_range": "0.0.0.0/0",
122 app_name
= "prometheus"
127 "name": f
"{app_name}-ingress",
129 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
130 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
131 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
132 "ingress_whitelist_source_range"
144 "serviceName": app_name
,
156 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
157 config
, app_name
, port
160 self
.assertListEqual(expected_result
, pod_ingress_resources
)
162 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
163 """Testing make pod ingress resources with HTTPs."""
165 "site_url": "https://prometheus",
167 "ingress_whitelist_source_range": "",
168 "tls_secret_name": "",
170 app_name
= "prometheus"
175 "name": f
"{app_name}-ingress",
177 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
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://prometheus",
212 "ingress_whitelist_source_range": "",
213 "tls_secret_name": "secret_name",
215 app_name
= "prometheus"
220 "name": f
"{app_name}-ingress",
222 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
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_pod_files(self
) -> NoReturn
:
255 """Testing make pod files."""
258 "default_target": "",
265 "mountPath": "/etc/prometheus",
268 "path": "prometheus.yml",
271 " scrape_interval: 15s"
272 " evaluation_interval: 15s"
279 " - job_name: 'prometheus'"
281 " - targets: [{}]".format(config
["default_target"])
288 pod_envconfig
= pod_spec
._make
_pod
_files
(config
)
290 self
.assertListEqual(expected_result
, pod_envconfig
)
292 def test_make_readiness_probe(self
) -> NoReturn
:
293 """Testing make readiness probe."""
301 "initialDelaySeconds": 10,
302 "timeoutSeconds": 30,
305 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
307 self
.assertDictEqual(expected_result
, readiness_probe
)
309 def test_make_liveness_probe(self
) -> NoReturn
:
310 """Testing make liveness probe."""
315 "path": "/-/healthy",
318 "initialDelaySeconds": 30,
322 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
324 self
.assertDictEqual(expected_result
, liveness_probe
)
326 def test_make_pod_command(self
) -> NoReturn
:
327 """Testing make pod command."""
331 "default_target": "",
339 "--config.file=/etc/prometheus/prometheus.yml",
340 "--storage.tsdb.path=/prometheus",
341 "--web.console.libraries=/usr/share/prometheus/console_libraries",
342 "--web.console.templates=/usr/share/prometheus/consoles",
343 "--web.route-prefix={}".format(config
.get("web_subpath")),
344 "--web.external-url=http://localhost:{}{}".format(
345 port
, config
.get("web_subpath")
349 pod_envconfig
= pod_spec
._make
_pod
_command
(config
, port
)
351 self
.assertListEqual(expected_result
, pod_envconfig
)
353 def test_make_pod_spec(self
) -> NoReturn
:
354 """Testing make pod spec."""
355 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
358 "default_target": "",
362 app_name
= "prometheus"
370 "imageDetails": image_info
,
371 "imagePullPolicy": "Always",
375 "containerPort": port
,
383 "mountPath": "/etc/prometheus",
386 "path": "prometheus.yml",
389 " scrape_interval: 15s"
390 " evaluation_interval: 15s"
397 " - job_name: 'prometheus'"
399 " - targets: [{}]".format(
400 config
.get("default_target")
411 "--config.file=/etc/prometheus/prometheus.yml",
412 "--storage.tsdb.path=/prometheus",
413 "--web.console.libraries=/usr/share/prometheus/console_libraries",
414 "--web.console.templates=/usr/share/prometheus/consoles",
415 "--web.route-prefix={}".format(config
.get("web_subpath")),
416 "--web.external-url=http://localhost:{}{}".format(
417 port
, config
.get("web_subpath")
426 "initialDelaySeconds": 10,
427 "timeoutSeconds": 30,
431 "path": "/-/healthy",
434 "initialDelaySeconds": 30,
440 "kubernetesResources": {"ingressResources": []},
443 spec
= pod_spec
.make_pod_spec(
444 image_info
, config
, relation_state
, app_name
, port
447 self
.assertDictEqual(expected_result
, spec
)
449 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
450 """Testing make pod spec."""
451 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
454 "default_target": "",
455 "site_url": "https://prometheus",
456 "tls_secret_name": "prometheus",
458 "ingress_whitelist_source_range": "0.0.0.0/0",
461 app_name
= "prometheus"
469 "imageDetails": image_info
,
470 "imagePullPolicy": "Always",
474 "containerPort": port
,
482 "mountPath": "/etc/prometheus",
485 "path": "prometheus.yml",
488 " scrape_interval: 15s"
489 " evaluation_interval: 15s"
496 " - job_name: 'prometheus'"
498 " - targets: [{}]".format(
499 config
.get("default_target")
510 "--config.file=/etc/prometheus/prometheus.yml",
511 "--storage.tsdb.path=/prometheus",
512 "--web.console.libraries=/usr/share/prometheus/console_libraries",
513 "--web.console.templates=/usr/share/prometheus/consoles",
514 "--web.route-prefix={}".format(config
.get("web_subpath")),
515 "--web.external-url=http://localhost:{}{}".format(
516 port
, config
.get("web_subpath")
525 "initialDelaySeconds": 10,
526 "timeoutSeconds": 30,
530 "path": "/-/healthy",
533 "initialDelaySeconds": 30,
539 "kubernetesResources": {
540 "ingressResources": [
542 "name": "{}-ingress".format(app_name
),
544 "nginx.ingress.kubernetes.io/proxy-body-size": str(
545 config
.get("max_file_size")
547 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
548 "ingress_whitelist_source_range"
560 "serviceName": app_name
,
571 "secretName": config
.get("tls_secret_name"),
580 spec
= pod_spec
.make_pod_spec(
581 image_info
, config
, relation_state
, app_name
, port
584 self
.assertDictEqual(expected_result
, spec
)
586 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
587 """Testing make pod spec without image_info."""
591 "default_target": "",
595 app_name
= "prometheus"
598 spec
= pod_spec
.make_pod_spec(
599 image_info
, config
, relation_state
, app_name
, port
602 self
.assertIsNone(spec
)
604 def test_make_pod_spec_without_config(self
) -> NoReturn
:
605 """Testing make pod spec without config."""
606 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
609 app_name
= "prometheus"
612 with self
.assertRaises(ValueError):
613 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
616 if __name__
== "__main__":