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."""
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 "prometheus_host": "prometheus",
53 "prometheus_port": "9090",
58 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
60 self
.assertDictEqual(expected_result
, pod_envconfig
)
62 def test_make_pod_ingress_resources_without_site_url(self
) -> NoReturn
:
63 """Testing make pod ingress resources without site_url."""
64 config
= {"site_url": ""}
68 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
69 config
, app_name
, port
72 self
.assertIsNone(pod_ingress_resources
)
74 def test_make_pod_ingress_resources(self
) -> NoReturn
:
75 """Testing make pod ingress resources."""
77 "site_url": "http://grafana",
79 "ingress_whitelist_source_range": "",
86 "name": f
"{app_name}-ingress",
88 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
89 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
100 "serviceName": app_name
,
112 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
113 config
, app_name
, port
116 self
.assertListEqual(expected_result
, pod_ingress_resources
)
118 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
119 """Testing make pod ingress resources with whitelist_source_range."""
121 "site_url": "http://grafana",
123 "ingress_whitelist_source_range": "0.0.0.0/0",
130 "name": f
"{app_name}-ingress",
132 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
133 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
134 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
135 "ingress_whitelist_source_range"
147 "serviceName": app_name
,
159 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
160 config
, app_name
, port
163 self
.assertListEqual(expected_result
, pod_ingress_resources
)
165 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
166 """Testing make pod ingress resources with HTTPs."""
168 "site_url": "https://grafana",
170 "ingress_whitelist_source_range": "",
171 "tls_secret_name": "",
178 "name": f
"{app_name}-ingress",
180 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
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://grafana",
215 "ingress_whitelist_source_range": "",
216 "tls_secret_name": "secret_name",
223 "name": f
"{app_name}-ingress",
225 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
236 "serviceName": app_name
,
245 {"hosts": [app_name
], "secretName": config
["tls_secret_name"]}
251 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
252 config
, app_name
, port
255 self
.assertListEqual(expected_result
, pod_ingress_resources
)
257 def test_make_pod_files(self
) -> NoReturn
:
258 """Testing make pod files."""
260 "prometheus_host": "prometheus",
261 "prometheus_port": "9090",
266 "name": "dashboards",
267 "mountPath": "/etc/grafana/provisioning/dashboards/",
270 "path": "dashboard-osm.yml",
279 " path: /etc/grafana/provisioning/dashboards/\n"
285 "name": "datasources",
286 "mountPath": "/etc/grafana/provisioning/datasources/",
289 "path": "datasource-prometheus.yml",
294 " is_default: true\n"
295 " name: osm_prometheus\n"
297 " type: prometheus\n"
299 " url: http://{}:{}\n".format(
300 relation_state
.get("prometheus_host"),
301 relation_state
.get("prometheus_port"),
309 pod_envconfig
= pod_spec
._make
_pod
_files
(relation_state
)
311 self
.assertListEqual(expected_result
, pod_envconfig
)
313 def test_make_readiness_probe(self
) -> NoReturn
:
314 """Testing make readiness probe."""
319 "path": "/api/health",
322 "initialDelaySeconds": 10,
325 "successThreshold": 1,
326 "failureThreshold": 3,
329 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
331 self
.assertDictEqual(expected_result
, readiness_probe
)
333 def test_make_liveness_probe(self
) -> NoReturn
:
334 """Testing make liveness probe."""
339 "path": "/api/health",
342 "initialDelaySeconds": 60,
343 "timeoutSeconds": 30,
344 "failureThreshold": 10,
347 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
349 self
.assertDictEqual(expected_result
, liveness_probe
)
351 def test_make_pod_spec(self
) -> NoReturn
:
352 """Testing make pod spec."""
353 image_info
= {"upstream-source": "ubuntu/grafana:latest"}
358 "prometheus_host": "prometheus",
359 "prometheus_port": "9090",
369 "imageDetails": image_info
,
370 "imagePullPolicy": "Always",
374 "containerPort": port
,
381 "name": "dashboards",
382 "mountPath": "/etc/grafana/provisioning/dashboards/",
385 "path": "dashboard-osm.yml",
394 " path: /etc/grafana/provisioning/dashboards/\n"
400 "name": "datasources",
401 "mountPath": "/etc/grafana/provisioning/datasources/",
404 "path": "datasource-prometheus.yml",
409 " is_default: true\n"
410 " name: osm_prometheus\n"
412 " type: prometheus\n"
414 " url: http://{}:{}\n".format(
415 relation_state
.get("prometheus_host"),
416 relation_state
.get("prometheus_port"),
426 "path": "/api/health",
429 "initialDelaySeconds": 10,
432 "successThreshold": 1,
433 "failureThreshold": 3,
437 "path": "/api/health",
440 "initialDelaySeconds": 60,
441 "timeoutSeconds": 30,
442 "failureThreshold": 10,
447 "kubernetesResources": {"ingressResources": []},
450 spec
= pod_spec
.make_pod_spec(
451 image_info
, config
, relation_state
, app_name
, port
454 self
.assertDictEqual(expected_result
, spec
)
456 def test_make_pod_spec_with_ingress(self
) -> NoReturn
:
457 """Testing make pod spec."""
458 image_info
= {"upstream-source": "ubuntu/grafana:latest"}
460 "site_url": "https://grafana",
461 "tls_secret_name": "grafana",
463 "ingress_whitelist_source_range": "0.0.0.0/0",
466 "prometheus_host": "prometheus",
467 "prometheus_port": "9090",
477 "imageDetails": image_info
,
478 "imagePullPolicy": "Always",
482 "containerPort": port
,
489 "name": "dashboards",
490 "mountPath": "/etc/grafana/provisioning/dashboards/",
493 "path": "dashboard-osm.yml",
502 " path: /etc/grafana/provisioning/dashboards/\n"
508 "name": "datasources",
509 "mountPath": "/etc/grafana/provisioning/datasources/",
512 "path": "datasource-prometheus.yml",
517 " is_default: true\n"
518 " name: osm_prometheus\n"
520 " type: prometheus\n"
522 " url: http://{}:{}\n".format(
523 relation_state
.get("prometheus_host"),
524 relation_state
.get("prometheus_port"),
534 "path": "/api/health",
537 "initialDelaySeconds": 10,
540 "successThreshold": 1,
541 "failureThreshold": 3,
545 "path": "/api/health",
548 "initialDelaySeconds": 60,
549 "timeoutSeconds": 30,
550 "failureThreshold": 10,
555 "kubernetesResources": {
556 "ingressResources": [
558 "name": "{}-ingress".format(app_name
),
560 "nginx.ingress.kubernetes.io/proxy-body-size": str(
561 config
.get("max_file_size")
563 "nginx.ingress.kubernetes.io/whitelist-source-range": config
.get(
564 "ingress_whitelist_source_range"
576 "serviceName": app_name
,
587 "secretName": config
.get("tls_secret_name"),
596 spec
= pod_spec
.make_pod_spec(
597 image_info
, config
, relation_state
, app_name
, port
600 self
.assertDictEqual(expected_result
, spec
)
602 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
603 """Testing make pod spec without image_info."""
609 "prometheus_host": "prometheus",
610 "prometheus_port": "9090",
615 spec
= pod_spec
.make_pod_spec(
616 image_info
, config
, relation_state
, app_name
, port
619 self
.assertIsNone(spec
)
621 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
622 """Testing make pod spec without relation_state."""
623 image_info
= {"upstream-source": "ubuntu/grafana:latest"}
631 with self
.assertRaises(ValueError):
632 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
635 if __name__
== "__main__":