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\n"
272 " evaluation_interval: 15s\n"
275 " - static_configs:\n"
279 " - job_name: 'prometheus'\n"
281 " - targets: [{}]\n".format(config
["default_target"])
288 pod_envconfig
= pod_spec
._make
_pod
_files
(config
)
289 self
.assertListEqual(expected_result
, pod_envconfig
)
291 def test_make_readiness_probe(self
) -> NoReturn
:
292 """Testing make readiness probe."""
300 "initialDelaySeconds": 10,
301 "timeoutSeconds": 30,
304 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
306 self
.assertDictEqual(expected_result
, readiness_probe
)
308 def test_make_liveness_probe(self
) -> NoReturn
:
309 """Testing make liveness probe."""
314 "path": "/-/healthy",
317 "initialDelaySeconds": 30,
321 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
323 self
.assertDictEqual(expected_result
, liveness_probe
)
325 def test_make_pod_command(self
) -> NoReturn
:
326 """Testing make pod command."""
330 "default_target": "",
336 "--config.file=/etc/prometheus/prometheus.yml",
337 "--storage.tsdb.path=/prometheus",
338 "--web.console.libraries=/usr/share/prometheus/console_libraries",
339 "--web.console.templates=/usr/share/prometheus/consoles",
340 "--web.route-prefix={}".format(config
.get("web_subpath")),
341 "--web.external-url=http://localhost:{}{}".format(
342 port
, config
.get("web_subpath")
346 pod_envconfig
= pod_spec
._make
_pod
_command
(config
, port
)
348 self
.assertListEqual(expected_result
, pod_envconfig
)
350 def test_make_pod_command_with_web_admin_api_enabled(self
) -> NoReturn
:
351 """Testing make pod command."""
355 "default_target": "",
357 "enable_web_admin_api": True,
362 "--config.file=/etc/prometheus/prometheus.yml",
363 "--storage.tsdb.path=/prometheus",
364 "--web.console.libraries=/usr/share/prometheus/console_libraries",
365 "--web.console.templates=/usr/share/prometheus/consoles",
366 "--web.route-prefix={}".format(config
.get("web_subpath")),
367 "--web.external-url=http://localhost:{}{}".format(
368 port
, config
.get("web_subpath")
370 "--web.enable-admin-api",
373 pod_envconfig
= pod_spec
._make
_pod
_command
(config
, port
)
375 self
.assertListEqual(expected_result
, pod_envconfig
)
377 def test_make_pod_spec(self
) -> NoReturn
:
378 """Testing make pod spec."""
379 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
382 "default_target": "",
384 "enable_web_admin_api": False,
387 app_name
= "prometheus"
395 "imageDetails": image_info
,
396 "imagePullPolicy": "Always",
400 "containerPort": port
,
408 "mountPath": "/etc/prometheus",
411 "path": "prometheus.yml",
414 " scrape_interval: 15s\n"
415 " evaluation_interval: 15s\n"
418 " - static_configs:\n"
422 " - job_name: 'prometheus'\n"
424 " - targets: [{}]\n".format(
425 config
.get("default_target")
434 "--config.file=/etc/prometheus/prometheus.yml",
435 "--storage.tsdb.path=/prometheus",
436 "--web.console.libraries=/usr/share/prometheus/console_libraries",
437 "--web.console.templates=/usr/share/prometheus/consoles",
438 "--web.route-prefix={}".format(config
.get("web_subpath")),
439 "--web.external-url=http://localhost:{}{}".format(
440 port
, config
.get("web_subpath")
449 "initialDelaySeconds": 10,
450 "timeoutSeconds": 30,
454 "path": "/-/healthy",
457 "initialDelaySeconds": 30,
463 "kubernetesResources": {"ingressResources": []},
466 spec
= pod_spec
.make_pod_spec(
467 image_info
, config
, relation_state
, app_name
, port
470 self
.assertDictEqual(expected_result
, spec
)
472 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
473 """Testing make pod spec."""
474 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
477 "default_target": "",
478 "site_url": "https://prometheus",
479 "tls_secret_name": "prometheus",
481 "ingress_whitelist_source_range": "0.0.0.0/0",
482 "enable_web_admin_api": False,
485 app_name
= "prometheus"
493 "imageDetails": image_info
,
494 "imagePullPolicy": "Always",
498 "containerPort": port
,
506 "mountPath": "/etc/prometheus",
509 "path": "prometheus.yml",
512 " scrape_interval: 15s\n"
513 " evaluation_interval: 15s\n"
516 " - static_configs:\n"
520 " - job_name: 'prometheus'\n"
522 " - targets: [{}]\n".format(
523 config
.get("default_target")
532 "--config.file=/etc/prometheus/prometheus.yml",
533 "--storage.tsdb.path=/prometheus",
534 "--web.console.libraries=/usr/share/prometheus/console_libraries",
535 "--web.console.templates=/usr/share/prometheus/consoles",
536 "--web.route-prefix={}".format(config
.get("web_subpath")),
537 "--web.external-url=http://localhost:{}{}".format(
538 port
, config
.get("web_subpath")
547 "initialDelaySeconds": 10,
548 "timeoutSeconds": 30,
552 "path": "/-/healthy",
555 "initialDelaySeconds": 30,
561 "kubernetesResources": {
562 "ingressResources": [
564 "name": "{}-ingress".format(app_name
),
566 "nginx.ingress.kubernetes.io/proxy-body-size": str(
567 config
.get("max_file_size")
569 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
570 "ingress_whitelist_source_range"
582 "serviceName": app_name
,
593 "secretName": config
.get("tls_secret_name"),
602 spec
= pod_spec
.make_pod_spec(
603 image_info
, config
, relation_state
, app_name
, port
606 self
.assertDictEqual(expected_result
, spec
)
608 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
609 """Testing make pod spec without image_info."""
613 "default_target": "",
615 "enable_web_admin_api": False,
618 app_name
= "prometheus"
621 spec
= pod_spec
.make_pod_spec(
622 image_info
, config
, relation_state
, app_name
, port
625 self
.assertIsNone(spec
)
627 def test_make_pod_spec_without_config(self
) -> NoReturn
:
628 """Testing make pod spec without config."""
629 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
632 app_name
= "prometheus"
635 with self
.assertRaises(ValueError):
636 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
639 if __name__
== "__main__":