7cf586ce1d9b51a245396f353c82f3311b9bf12b
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": "mongodb://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": "mongodb://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": "mongodb://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/backend-protocol": "HTTPS",
209 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
220 "serviceName": app_name
,
232 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
233 config
, app_name
, port
236 self
.assertListEqual(expected_result
, pod_ingress_resources
)
238 def test_make_pod_ingress_resources_with_whitelist_source_range(self
) -> NoReturn
:
239 """Testing make pod ingress resources with whitelist_source_range."""
241 "site_url": "http://nbi",
243 "ingress_whitelist_source_range": "0.0.0.0/0",
250 "name": f
"{app_name}-ingress",
252 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
253 "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
254 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
255 "nginx.ingress.kubernetes.io/whitelist-source-range": config
[
256 "ingress_whitelist_source_range"
268 "serviceName": app_name
,
280 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
281 config
, app_name
, port
284 self
.assertListEqual(expected_result
, pod_ingress_resources
)
286 def test_make_pod_ingress_resources_with_https(self
) -> NoReturn
:
287 """Testing make pod ingress resources with HTTPs."""
289 "site_url": "https://nbi",
291 "ingress_whitelist_source_range": "",
292 "tls_secret_name": "",
299 "name": f
"{app_name}-ingress",
301 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
302 "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
313 "serviceName": app_name
,
321 "tls": [{"hosts": [app_name
]}],
326 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
327 config
, app_name
, port
330 self
.assertListEqual(expected_result
, pod_ingress_resources
)
332 def test_make_pod_ingress_resources_with_https_tls_secret_name(self
) -> NoReturn
:
333 """Testing make pod ingress resources with HTTPs and TLS secret name."""
335 "site_url": "https://nbi",
337 "ingress_whitelist_source_range": "",
338 "tls_secret_name": "secret_name",
345 "name": f
"{app_name}-ingress",
347 "nginx.ingress.kubernetes.io/proxy-body-size": f
"{config['max_file_size']}",
348 "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
359 "serviceName": app_name
,
368 {"hosts": [app_name
], "secretName": config
["tls_secret_name"]}
374 pod_ingress_resources
= pod_spec
._make
_pod
_ingress
_resources
(
375 config
, app_name
, port
378 self
.assertListEqual(expected_result
, pod_ingress_resources
)
380 def test_make_startup_probe(self
) -> NoReturn
:
381 """Testing make startup probe."""
383 "exec": {"command": ["/usr/bin/pgrep python3"]},
384 "initialDelaySeconds": 60,
388 startup_probe
= pod_spec
._make
_startup
_probe
()
390 self
.assertDictEqual(expected_result
, startup_probe
)
392 def test_make_readiness_probe(self
) -> NoReturn
:
393 """Testing make readiness probe."""
401 "initialDelaySeconds": 45,
405 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
407 self
.assertDictEqual(expected_result
, readiness_probe
)
409 def test_make_liveness_probe(self
) -> NoReturn
:
410 """Testing make liveness probe."""
418 "initialDelaySeconds": 45,
422 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
424 self
.assertDictEqual(expected_result
, liveness_probe
)
426 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
427 """Testing make pod spec without image_info."""
430 "enable_test": False,
431 "database_commonkey": "commonkey",
432 "log_level": "DEBUG",
433 "auth_backend": "internal",
437 "message_host": "kafka",
438 "message_port": 9090,
439 "database_uri": "mongodb://mongo",
440 "prometheus_host": "prometheus",
441 "prometheus_port": 9082,
446 spec
= pod_spec
.make_pod_spec(
447 image_info
, config
, relation_state
, app_name
, port
450 self
.assertIsNone(spec
)
452 def test_make_pod_spec_without_config(self
) -> NoReturn
:
453 """Testing make pod spec without config."""
454 image_info
= {"upstream-source": "opensourcemano/nbi:8"}
457 "message_host": "kafka",
458 "message_port": 9090,
459 "database_uri": "mongodb://mongo",
460 "prometheus_host": "prometheus",
461 "prometheus_port": 9082,
466 with self
.assertRaises(ValidationError
):
467 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
469 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
470 """Testing make pod spec without relation_state."""
471 image_info
= {"upstream-source": "opensourcemano/nbi:8"}
473 "enable_test": False,
474 "database_commonkey": "commonkey",
475 "log_level": "DEBUG",
476 "auth_backend": "internal",
483 with self
.assertRaises(ValidationError
):
484 pod_spec
.make_pod_spec(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": "mongodb://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": "mongodb://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__":