1 # #!/usr/bin/env python3
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
14 # # under the License.
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_hostname": "prometheus",
53 # "prometheus_port": "9090",
56 # expected_result = {}
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": ""}
65 # app_name = "grafana"
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": "",
81 # app_name = "grafana"
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,
101 # "servicePort": port,
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",
122 # "max_file_size": 0,
123 # "ingress_whitelist_source_range": "0.0.0.0/0",
125 # app_name = "grafana"
128 # expected_result = [
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,
148 # "servicePort": port,
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",
169 # "max_file_size": 0,
170 # "ingress_whitelist_source_range": "",
171 # "tls_secret_name": "",
173 # app_name = "grafana"
176 # expected_result = [
178 # "name": f"{app_name}-ingress",
180 # "nginx.ingress.kubernetes.io/proxy-body-size": f"{config['max_file_size']}",
191 # "serviceName": app_name,
192 # "servicePort": port,
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",
214 # "max_file_size": 0,
215 # "ingress_whitelist_source_range": "",
216 # "tls_secret_name": "secret_name",
218 # app_name = "grafana"
221 # expected_result = [
223 # "name": f"{app_name}-ingress",
225 # "nginx.ingress.kubernetes.io/proxy-body-size": f"{config['max_file_size']}",
236 # "serviceName": app_name,
237 # "servicePort": port,
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."""
259 # config = {"osm_dashboards": False}
261 # "prometheus_hostname": "prometheus",
262 # "prometheus_port": "9090",
265 # expected_result = [
267 # "name": "dashboards",
268 # "mountPath": "/etc/grafana/provisioning/dashboards/",
271 # "path": "dashboard-osm.yml",
280 # " path: /etc/grafana/provisioning/dashboards/\n"
286 # "name": "datasources",
287 # "mountPath": "/etc/grafana/provisioning/datasources/",
290 # "path": "datasource-prometheus.yml",
293 # " - access: proxy\n"
294 # " editable: true\n"
295 # " is_default: true\n"
296 # " name: osm_prometheus\n"
298 # " type: prometheus\n"
300 # " url: http://{}:{}\n".format(
301 # relation_state.get("prometheus_hostname"),
302 # relation_state.get("prometheus_port"),
310 # pod_envconfig = pod_spec._make_pod_files(config, relation_state)
311 # self.assertListEqual(expected_result, pod_envconfig)
313 # def test_make_readiness_probe(self) -> NoReturn:
314 # """Testing make readiness probe."""
317 # expected_result = {
319 # "path": "/api/health",
322 # "initialDelaySeconds": 10,
323 # "periodSeconds": 10,
324 # "timeoutSeconds": 5,
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."""
337 # expected_result = {
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_hostname": "prometheus",
359 # "prometheus_port": "9090",
361 # app_name = "grafana"
364 # expected_result = {
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",
407 # " - access: proxy\n"
408 # " editable: true\n"
409 # " is_default: true\n"
410 # " name: osm_prometheus\n"
412 # " type: prometheus\n"
414 # " url: http://{}:{}\n".format(
415 # relation_state.get("prometheus_hostname"),
416 # relation_state.get("prometheus_port"),
424 # "readinessProbe": {
426 # "path": "/api/health",
429 # "initialDelaySeconds": 10,
430 # "periodSeconds": 10,
431 # "timeoutSeconds": 5,
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",
462 # "max_file_size": 0,
463 # "ingress_whitelist_source_range": "0.0.0.0/0",
466 # "prometheus_hostname": "prometheus",
467 # "prometheus_port": "9090",
469 # app_name = "grafana"
472 # expected_result = {
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",
515 # " - access: proxy\n"
516 # " editable: true\n"
517 # " is_default: true\n"
518 # " name: osm_prometheus\n"
520 # " type: prometheus\n"
522 # " url: http://{}:{}\n".format(
523 # relation_state.get("prometheus_hostname"),
524 # relation_state.get("prometheus_port"),
532 # "readinessProbe": {
534 # "path": "/api/health",
537 # "initialDelaySeconds": 10,
538 # "periodSeconds": 10,
539 # "timeoutSeconds": 5,
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,
577 # "servicePort": port,
586 # "hosts": [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_hostname": "prometheus",
610 # "prometheus_port": "9090",
612 # app_name = "grafana"
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"}
627 # relation_state = {}
628 # app_name = "grafana"
631 # with self.assertRaises(ValueError):
632 # pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
635 # if __name__ == "__main__":