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 print(expected_result
, pod_envconfig
)
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": "",
337 "--config.file=/etc/prometheus/prometheus.yml",
338 "--storage.tsdb.path=/prometheus",
339 "--web.console.libraries=/usr/share/prometheus/console_libraries",
340 "--web.console.templates=/usr/share/prometheus/consoles",
341 "--web.route-prefix={}".format(config
.get("web_subpath")),
342 "--web.external-url=http://localhost:{}{}".format(
343 port
, config
.get("web_subpath")
347 pod_envconfig
= pod_spec
._make
_pod
_command
(config
, port
)
349 self
.assertListEqual(expected_result
, pod_envconfig
)
351 def test_make_pod_command_with_web_admin_api_enabled(self
) -> NoReturn
:
352 """Testing make pod command."""
356 "default_target": "",
358 "enable_web_admin_api": True,
363 "--config.file=/etc/prometheus/prometheus.yml",
364 "--storage.tsdb.path=/prometheus",
365 "--web.console.libraries=/usr/share/prometheus/console_libraries",
366 "--web.console.templates=/usr/share/prometheus/consoles",
367 "--web.route-prefix={}".format(config
.get("web_subpath")),
368 "--web.external-url=http://localhost:{}{}".format(
369 port
, config
.get("web_subpath")
371 "--web.enable-admin-api",
374 pod_envconfig
= pod_spec
._make
_pod
_command
(config
, port
)
376 self
.assertListEqual(expected_result
, pod_envconfig
)
378 def test_make_pod_spec(self
) -> NoReturn
:
379 """Testing make pod spec."""
380 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
383 "default_target": "",
385 "enable_web_admin_api": False,
388 app_name
= "prometheus"
396 "imageDetails": image_info
,
397 "imagePullPolicy": "Always",
401 "containerPort": port
,
409 "mountPath": "/etc/prometheus",
412 "path": "prometheus.yml",
415 " scrape_interval: 15s\n"
416 " evaluation_interval: 15s\n"
419 " - static_configs:\n"
423 " - job_name: 'prometheus'\n"
425 " - targets: [{}]\n".format(
426 config
.get("default_target")
435 "--config.file=/etc/prometheus/prometheus.yml",
436 "--storage.tsdb.path=/prometheus",
437 "--web.console.libraries=/usr/share/prometheus/console_libraries",
438 "--web.console.templates=/usr/share/prometheus/consoles",
439 "--web.route-prefix={}".format(config
.get("web_subpath")),
440 "--web.external-url=http://localhost:{}{}".format(
441 port
, config
.get("web_subpath")
450 "initialDelaySeconds": 10,
451 "timeoutSeconds": 30,
455 "path": "/-/healthy",
458 "initialDelaySeconds": 30,
464 "kubernetesResources": {"ingressResources": []},
467 spec
= pod_spec
.make_pod_spec(
468 image_info
, config
, relation_state
, app_name
, port
471 self
.assertDictEqual(expected_result
, spec
)
473 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
474 """Testing make pod spec."""
475 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
478 "default_target": "",
479 "site_url": "https://prometheus",
480 "tls_secret_name": "prometheus",
482 "ingress_whitelist_source_range": "0.0.0.0/0",
483 "enable_web_admin_api": False,
486 app_name
= "prometheus"
494 "imageDetails": image_info
,
495 "imagePullPolicy": "Always",
499 "containerPort": port
,
507 "mountPath": "/etc/prometheus",
510 "path": "prometheus.yml",
513 " scrape_interval: 15s\n"
514 " evaluation_interval: 15s\n"
517 " - static_configs:\n"
521 " - job_name: 'prometheus'\n"
523 " - targets: [{}]\n".format(
524 config
.get("default_target")
533 "--config.file=/etc/prometheus/prometheus.yml",
534 "--storage.tsdb.path=/prometheus",
535 "--web.console.libraries=/usr/share/prometheus/console_libraries",
536 "--web.console.templates=/usr/share/prometheus/consoles",
537 "--web.route-prefix={}".format(config
.get("web_subpath")),
538 "--web.external-url=http://localhost:{}{}".format(
539 port
, config
.get("web_subpath")
548 "initialDelaySeconds": 10,
549 "timeoutSeconds": 30,
553 "path": "/-/healthy",
556 "initialDelaySeconds": 30,
562 "kubernetesResources": {
563 "ingressResources": [
565 "name": "{}-ingress".format(app_name
),
567 "nginx.ingress.kubernetes.io/proxy-body-size": str(
568 config
.get("max_file_size")
570 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
571 "ingress_whitelist_source_range"
583 "serviceName": app_name
,
594 "secretName": config
.get("tls_secret_name"),
603 spec
= pod_spec
.make_pod_spec(
604 image_info
, config
, relation_state
, app_name
, port
607 self
.assertDictEqual(expected_result
, spec
)
609 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
610 """Testing make pod spec without image_info."""
614 "default_target": "",
616 "enable_web_admin_api": False,
619 app_name
= "prometheus"
622 spec
= pod_spec
.make_pod_spec(
623 image_info
, config
, relation_state
, app_name
, port
626 self
.assertIsNone(spec
)
628 def test_make_pod_spec_without_config(self
) -> NoReturn
:
629 """Testing make pod spec without config."""
630 image_info
= {"upstream-source": "ubuntu/prometheus:latest"}
633 app_name
= "prometheus"
636 with self
.assertRaises(ValueError):
637 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
640 if __name__
== "__main__":