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 pydantic
import ValidationError
24 from typing
import NoReturn
30 class TestPodSpec(unittest
.TestCase
):
31 """Pod spec unit tests."""
33 def test_make_pod_ports(self
) -> NoReturn
:
34 """Testing make pod ports."""
40 "containerPort": port
,
45 pod_ports
= pod_spec
._make
_pod
_ports
(port
)
47 self
.assertListEqual(expected_result
, pod_ports
)
49 def test_make_pod_envconfig_without_keystone(self
) -> NoReturn
:
50 """Teting make pod envconfig without Keystone."""
53 "database_commonkey": "commonkey",
55 "auth_backend": "internal",
58 "message_host": "kafka",
60 "database_uri": "mongo://mongo",
61 "prometheus_host": "prometheus",
62 "prometheus_port": 9082,
66 "ALLOW_ANONYMOUS_LOGIN": "yes",
67 "OSMNBI_SERVER_ENABLE_TEST": config
["enable_test"],
68 "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
69 "OSMNBI_MESSAGE_HOST": relation_state
["message_host"],
70 "OSMNBI_MESSAGE_DRIVER": "kafka",
71 "OSMNBI_MESSAGE_PORT": relation_state
["message_port"],
72 "OSMNBI_DATABASE_DRIVER": "mongo",
73 "OSMNBI_DATABASE_URI": relation_state
["database_uri"],
74 "OSMNBI_DATABASE_COMMONKEY": config
["database_commonkey"],
75 "OSMNBI_STORAGE_DRIVER": "mongo",
76 "OSMNBI_STORAGE_PATH": "/app/storage",
77 "OSMNBI_STORAGE_COLLECTION": "files",
78 "OSMNBI_STORAGE_URI": relation_state
["database_uri"],
79 "OSMNBI_PROMETHEUS_HOST": relation_state
["prometheus_host"],
80 "OSMNBI_PROMETHEUS_PORT": relation_state
["prometheus_port"],
81 "OSMNBI_LOG_LEVEL": config
["log_level"],
82 "OSMNBI_AUTHENTICATION_BACKEND": config
["auth_backend"],
85 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
87 self
.assertDictEqual(expected_result
, pod_envconfig
)
89 def test_make_pod_envconfig_with_keystone(self
) -> NoReturn
:
90 """Teting make pod envconfig with Keystone."""
93 "database_commonkey": "commonkey",
95 "auth_backend": "keystone",
98 "message_host": "kafka",
100 "database_uri": "mongo://mongo",
101 "prometheus_host": "prometheus",
102 "prometheus_port": 9082,
103 "keystone_host": "keystone",
104 "keystone_port": 5000,
105 "keystone_user_domain_name": "user_domain",
106 "keystone_project_domain_name": "project_domain",
107 "keystone_username": "username",
108 "keystone_password": "password",
109 "keystone_service": "service",
113 "ALLOW_ANONYMOUS_LOGIN": "yes",
114 "OSMNBI_SERVER_ENABLE_TEST": config
["enable_test"],
115 "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
116 "OSMNBI_MESSAGE_HOST": relation_state
["message_host"],
117 "OSMNBI_MESSAGE_DRIVER": "kafka",
118 "OSMNBI_MESSAGE_PORT": relation_state
["message_port"],
119 "OSMNBI_DATABASE_DRIVER": "mongo",
120 "OSMNBI_DATABASE_URI": relation_state
["database_uri"],
121 "OSMNBI_DATABASE_COMMONKEY": config
["database_commonkey"],
122 "OSMNBI_STORAGE_DRIVER": "mongo",
123 "OSMNBI_STORAGE_PATH": "/app/storage",
124 "OSMNBI_STORAGE_COLLECTION": "files",
125 "OSMNBI_STORAGE_URI": relation_state
["database_uri"],
126 "OSMNBI_PROMETHEUS_HOST": relation_state
["prometheus_host"],
127 "OSMNBI_PROMETHEUS_PORT": relation_state
["prometheus_port"],
128 "OSMNBI_LOG_LEVEL": config
["log_level"],
129 "OSMNBI_AUTHENTICATION_BACKEND": config
["auth_backend"],
130 "OSMNBI_AUTHENTICATION_AUTH_URL": relation_state
["keystone_host"],
131 "OSMNBI_AUTHENTICATION_AUTH_PORT": relation_state
["keystone_port"],
132 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": relation_state
[
133 "keystone_user_domain_name"
135 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": relation_state
[
136 "keystone_project_domain_name"
138 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": relation_state
[
141 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": relation_state
[
144 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": relation_state
["keystone_service"],
147 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
149 self
.assertDictEqual(expected_result
, pod_envconfig
)
151 def test_make_pod_envconfig_wrong_auth_backend(self
) -> NoReturn
:
152 """Teting make pod envconfig with wrong auth_backend."""
154 "enable_test": False,
155 "database_commonkey": "commonkey",
156 "log_level": "DEBUG",
157 "auth_backend": "kerberos",
160 "message_host": "kafka",
161 "message_port": 9090,
162 "database_uri": "mongo://mongo",
163 "prometheus_host": "prometheus",
164 "prometheus_port": 9082,
165 "keystone_host": "keystone",
166 "keystone_port": 5000,
167 "keystone_user_domain_name": "user_domain",
168 "keystone_project_domain_name": "project_domain",
169 "keystone_username": "username",
170 "keystone_password": "password",
171 "keystone_service": "service",
174 with self
.assertRaises(ValueError) as exc
:
175 pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
178 "auth_backend needs to be either internal or keystone" in str(exc
.exception
)
181 def test_make_pod_ingress_resources_without_site_url(self
) -> NoReturn
:
182 """Testing make pod ingress resources without site_url."""
183 config
= {"site_url": ""}
187 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
188 config
, app_name
, port
191 self
.assertIsNone(pod_ingress_resources
)
193 def test_make_pod_ingress_resources(self
) -> NoReturn
:
194 """Testing make pod ingress resources."""
196 "site_url": "http://nbi",
198 "ingress_whitelist_source_range": "",
205 "name": f
"{app_name}-ingress",
207 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
208 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
219 "serviceName": app_name
,
231 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
232 config
, app_name
, port
235 self
.assertListEqual(expected_result
, pod_ingress_resources
)
237 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
238 """Testing make pod ingress resources with whitelist_source_range."""
240 "site_url": "http://nbi",
242 "ingress_whitelist_source_range": "0.0.0.0/0",
249 "name": f
"{app_name}-ingress",
251 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
252 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
253 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
254 "ingress_whitelist_source_range"
266 "serviceName": app_name
,
278 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
279 config
, app_name
, port
282 self
.assertListEqual(expected_result
, pod_ingress_resources
)
284 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
285 """Testing make pod ingress resources with HTTPs."""
287 "site_url": "https://nbi",
289 "ingress_whitelist_source_range": "",
290 "tls_secret_name": "",
297 "name": f
"{app_name}-ingress",
299 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
310 "serviceName": app_name
,
318 "tls": [{"hosts": [app_name
]}],
323 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
324 config
, app_name
, port
327 self
.assertListEqual(expected_result
, pod_ingress_resources
)
329 def test_make_pod_ingress_resources_with_https_tls_secret_name(self
) -> NoReturn
:
330 """Testing make pod ingress resources with HTTPs and TLS secret name."""
332 "site_url": "https://nbi",
334 "ingress_whitelist_source_range": "",
335 "tls_secret_name": "secret_name",
342 "name": f
"{app_name}-ingress",
344 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
355 "serviceName": app_name
,
364 {"hosts": [app_name
], "secretName": config
["tls_secret_name"]}
370 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
371 config
, app_name
, port
374 self
.assertListEqual(expected_result
, pod_ingress_resources
)
376 def test_make_startup_probe(self
) -> NoReturn
:
377 """Testing make startup probe."""
379 "exec": {"command": ["/usr/bin/pgrep python3"]},
380 "initialDelaySeconds": 60,
384 startup_probe
= pod_spec
._make
_startup
_probe
()
386 self
.assertDictEqual(expected_result
, startup_probe
)
388 def test_make_readiness_probe(self
) -> NoReturn
:
389 """Testing make readiness probe."""
397 "initialDelaySeconds": 45,
401 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
403 self
.assertDictEqual(expected_result
, readiness_probe
)
405 def test_make_liveness_probe(self
) -> NoReturn
:
406 """Testing make liveness probe."""
414 "initialDelaySeconds": 45,
418 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
420 self
.assertDictEqual(expected_result
, liveness_probe
)
422 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
423 """Testing make pod spec without image_info."""
426 "enable_test": False,
427 "database_commonkey": "commonkey",
428 "log_level": "DEBUG",
429 "auth_backend": "internal",
433 "message_host": "kafka",
434 "message_port": 9090,
435 "database_uri": "mongo://mongo",
436 "prometheus_host": "prometheus",
437 "prometheus_port": 9082,
442 spec
= pod_spec
.make_pod_spec(
443 image_info
, config
, relation_state
, app_name
, port
446 self
.assertIsNone(spec
)
448 def test_make_pod_spec_without_config(self
) -> NoReturn
:
449 """Testing make pod spec without config."""
450 image_info
= {"upstream-source": "opensourcemano/nbi:8"}
453 "message_host": "kafka",
454 "message_port": 9090,
455 "database_uri": "mongo://mongo",
456 "prometheus_host": "prometheus",
457 "prometheus_port": 9082,
462 with self
.assertRaises(ValidationError
) as exc
:
463 pod_spec
.make_pod_spec(
464 image_info
, config
, relation_state
, app_name
, port
467 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
468 """Testing make pod spec without relation_state."""
469 image_info
= {"upstream-source": "opensourcemano/nbi:8"}
471 "enable_test": False,
472 "database_commonkey": "commonkey",
473 "log_level": "DEBUG",
474 "auth_backend": "internal",
481 with self
.assertRaises(ValidationError
) as exc
:
482 pod_spec
.make_pod_spec(
483 image_info
, config
, relation_state
, app_name
, port
486 def test_make_pod_spec(self
) -> NoReturn
:
487 """Testing make pod spec."""
488 image_info
= {"upstream-source": "opensourcemano/nbi:8"}
490 "enable_test": False,
491 "database_commonkey": "commonkey",
492 "log_level": "DEBUG",
493 "auth_backend": "internal",
497 "message_host": "kafka",
498 "message_port": 9090,
499 "database_uri": "mongo://mongo",
500 "prometheus_host": "prometheus",
501 "prometheus_port": 9082,
511 "imageDetails": image_info
,
512 "imagePullPolicy": "Always",
516 "containerPort": port
,
521 "ALLOW_ANONYMOUS_LOGIN": "yes",
522 "OSMNBI_SERVER_ENABLE_TEST": config
["enable_test"],
523 "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
524 "OSMNBI_MESSAGE_HOST": relation_state
["message_host"],
525 "OSMNBI_MESSAGE_DRIVER": "kafka",
526 "OSMNBI_MESSAGE_PORT": relation_state
["message_port"],
527 "OSMNBI_DATABASE_DRIVER": "mongo",
528 "OSMNBI_DATABASE_URI": relation_state
["database_uri"],
529 "OSMNBI_DATABASE_COMMONKEY": config
["database_commonkey"],
530 "OSMNBI_STORAGE_DRIVER": "mongo",
531 "OSMNBI_STORAGE_PATH": "/app/storage",
532 "OSMNBI_STORAGE_COLLECTION": "files",
533 "OSMNBI_STORAGE_URI": relation_state
["database_uri"],
534 "OSMNBI_PROMETHEUS_HOST": relation_state
["prometheus_host"],
535 "OSMNBI_PROMETHEUS_PORT": relation_state
["prometheus_port"],
536 "OSMNBI_LOG_LEVEL": config
["log_level"],
537 "OSMNBI_AUTHENTICATION_BACKEND": config
["auth_backend"],
541 "kubernetesResources": {
542 "ingressResources": [],
546 spec
= pod_spec
.make_pod_spec(
547 image_info
, config
, relation_state
, app_name
, port
550 self
.assertDictEqual(expected_result
, spec
)
552 def test_make_pod_spec_with_keystone(self
) -> NoReturn
:
553 """Testing make pod spec with keystone."""
554 image_info
= {"upstream-source": "opensourcemano/nbi:8"}
556 "enable_test": False,
557 "database_commonkey": "commonkey",
558 "log_level": "DEBUG",
559 "auth_backend": "keystone",
563 "message_host": "kafka",
564 "message_port": 9090,
565 "database_uri": "mongo://mongo",
566 "prometheus_host": "prometheus",
567 "prometheus_port": 9082,
568 "keystone_host": "keystone",
569 "keystone_port": 5000,
570 "keystone_user_domain_name": "user_domain",
571 "keystone_project_domain_name": "project_domain",
572 "keystone_username": "username",
573 "keystone_password": "password",
574 "keystone_service": "service",
584 "imageDetails": image_info
,
585 "imagePullPolicy": "Always",
589 "containerPort": port
,
594 "ALLOW_ANONYMOUS_LOGIN": "yes",
595 "OSMNBI_SERVER_ENABLE_TEST": config
["enable_test"],
596 "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
597 "OSMNBI_MESSAGE_HOST": relation_state
["message_host"],
598 "OSMNBI_MESSAGE_DRIVER": "kafka",
599 "OSMNBI_MESSAGE_PORT": relation_state
["message_port"],
600 "OSMNBI_DATABASE_DRIVER": "mongo",
601 "OSMNBI_DATABASE_URI": relation_state
["database_uri"],
602 "OSMNBI_DATABASE_COMMONKEY": config
["database_commonkey"],
603 "OSMNBI_STORAGE_DRIVER": "mongo",
604 "OSMNBI_STORAGE_PATH": "/app/storage",
605 "OSMNBI_STORAGE_COLLECTION": "files",
606 "OSMNBI_STORAGE_URI": relation_state
["database_uri"],
607 "OSMNBI_PROMETHEUS_HOST": relation_state
["prometheus_host"],
608 "OSMNBI_PROMETHEUS_PORT": relation_state
["prometheus_port"],
609 "OSMNBI_LOG_LEVEL": config
["log_level"],
610 "OSMNBI_AUTHENTICATION_BACKEND": config
["auth_backend"],
611 "OSMNBI_AUTHENTICATION_AUTH_URL": relation_state
[
614 "OSMNBI_AUTHENTICATION_AUTH_PORT": relation_state
[
617 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": relation_state
[
618 "keystone_user_domain_name"
620 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": relation_state
[
621 "keystone_project_domain_name"
623 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": relation_state
[
626 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": relation_state
[
629 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": relation_state
[
635 "kubernetesResources": {
636 "ingressResources": [],
640 spec
= pod_spec
.make_pod_spec(
641 image_info
, config
, relation_state
, app_name
, port
644 self
.assertDictEqual(expected_result
, spec
)
647 if __name__
== "__main__":