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
(9999)
47 self
.assertListEqual(expected_result
, pod_ports
)
49 def test_make_pod_envconfig_without_vca_apiproxy(self
) -> NoReturn
:
50 """Teting make pod envconfig without vca_apiproxy configuration."""
52 "database_commonkey": "commonkey",
56 "vca_user": "vca_user",
57 "vca_pubkey": "vca_pubkey",
58 "vca_password": "vca_password",
59 "vca_cacert": "vca_cacert",
60 "vca_cloud": "vca_cloud",
61 "vca_k8s_cloud": "vca_k8s_cloud",
64 "message_host": "kafka",
66 "database_uri": "mongodb://mongo",
72 "ALLOW_ANONYMOUS_LOGIN": "yes",
73 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
74 "OSMLCM_RO_HOST": relation_state
["ro_host"],
75 "OSMLCM_RO_PORT": relation_state
["ro_port"],
76 "OSMLCM_RO_TENANT": "osm",
77 "OSMLCM_MESSAGE_DRIVER": "kafka",
78 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
79 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
80 "OSMLCM_DATABASE_DRIVER": "mongo",
81 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
82 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
83 "OSMLCM_STORAGE_DRIVER": "mongo",
84 "OSMLCM_STORAGE_PATH": "/app/storage",
85 "OSMLCM_STORAGE_COLLECTION": "files",
86 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
87 "OSMLCM_VCA_HOST": config
["vca_host"],
88 "OSMLCM_VCA_PORT": config
["vca_port"],
89 "OSMLCM_VCA_USER": config
["vca_user"],
90 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
91 "OSMLCM_VCA_SECRET": config
["vca_password"],
92 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
93 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
94 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
97 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
99 self
.assertDictEqual(expected_result
, pod_envconfig
)
101 def test_make_pod_envconfig_with_vca_apiproxy(self
) -> NoReturn
:
102 """Teting make pod envconfig with vca_apiproxy configuration."""
104 "database_commonkey": "commonkey",
108 "vca_user": "vca_user",
109 "vca_pubkey": "vca_pubkey",
110 "vca_password": "vca_password",
111 "vca_cacert": "vca_cacert",
112 "vca_cloud": "vca_cloud",
113 "vca_k8s_cloud": "vca_k8s_cloud",
114 "vca_apiproxy": "vca_apiproxy",
117 "message_host": "kafka",
118 "message_port": 2181,
119 "database_uri": "mongodb://mongo",
125 "ALLOW_ANONYMOUS_LOGIN": "yes",
126 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
127 "OSMLCM_RO_HOST": relation_state
["ro_host"],
128 "OSMLCM_RO_PORT": relation_state
["ro_port"],
129 "OSMLCM_RO_TENANT": "osm",
130 "OSMLCM_MESSAGE_DRIVER": "kafka",
131 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
132 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
133 "OSMLCM_DATABASE_DRIVER": "mongo",
134 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
135 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
136 "OSMLCM_STORAGE_DRIVER": "mongo",
137 "OSMLCM_STORAGE_PATH": "/app/storage",
138 "OSMLCM_STORAGE_COLLECTION": "files",
139 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
140 "OSMLCM_VCA_HOST": config
["vca_host"],
141 "OSMLCM_VCA_PORT": config
["vca_port"],
142 "OSMLCM_VCA_USER": config
["vca_user"],
143 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
144 "OSMLCM_VCA_SECRET": config
["vca_password"],
145 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
146 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
147 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
148 "OSMLCM_VCA_APIPROXY": config
["vca_apiproxy"],
151 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
153 self
.assertDictEqual(expected_result
, pod_envconfig
)
155 def test_make_startup_probe(self
) -> NoReturn
:
156 """Testing make startup probe."""
158 "exec": {"command": ["/usr/bin/pgrep python3"]},
159 "initialDelaySeconds": 60,
163 startup_probe
= pod_spec
._make
_startup
_probe
()
165 self
.assertDictEqual(expected_result
, startup_probe
)
167 def test_make_readiness_probe(self
) -> NoReturn
:
168 """Testing make readiness probe."""
176 "initialDelaySeconds": 45,
180 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
182 self
.assertDictEqual(expected_result
, readiness_probe
)
184 def test_make_liveness_probe(self
) -> NoReturn
:
185 """Testing make liveness probe."""
193 "initialDelaySeconds": 45,
197 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
199 self
.assertDictEqual(expected_result
, liveness_probe
)
201 def test_make_pod_spec(self
) -> NoReturn
:
202 """Testing make pod spec."""
203 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
205 "database_commonkey": "commonkey",
209 "vca_user": "vca_user",
210 "vca_pubkey": "vca_pubkey",
211 "vca_password": "vca_password",
212 "vca_cacert": "vca_cacert",
213 "vca_cloud": "vca_cloud",
214 "vca_k8s_cloud": "vca_k8s_cloud",
215 "vca_apiproxy": "vca_apiproxy",
218 "message_host": "kafka",
219 "message_port": 2181,
220 "database_uri": "mongodb://mongo",
232 "imageDetails": image_info
,
233 "imagePullPolicy": "Always",
237 "containerPort": port
,
242 "ALLOW_ANONYMOUS_LOGIN": "yes",
243 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
244 "OSMLCM_RO_HOST": relation_state
["ro_host"],
245 "OSMLCM_RO_PORT": relation_state
["ro_port"],
246 "OSMLCM_RO_TENANT": "osm",
247 "OSMLCM_MESSAGE_DRIVER": "kafka",
248 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
249 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
250 "OSMLCM_DATABASE_DRIVER": "mongo",
251 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
252 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
253 "OSMLCM_STORAGE_DRIVER": "mongo",
254 "OSMLCM_STORAGE_PATH": "/app/storage",
255 "OSMLCM_STORAGE_COLLECTION": "files",
256 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
257 "OSMLCM_VCA_HOST": config
["vca_host"],
258 "OSMLCM_VCA_PORT": config
["vca_port"],
259 "OSMLCM_VCA_USER": config
["vca_user"],
260 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
261 "OSMLCM_VCA_SECRET": config
["vca_password"],
262 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
263 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
264 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
265 "OSMLCM_VCA_APIPROXY": config
["vca_apiproxy"],
269 "kubernetesResources": {"ingressResources": []},
272 spec
= pod_spec
.make_pod_spec(
273 image_info
, config
, relation_state
, app_name
, port
276 self
.assertDictEqual(expected_result
, spec
)
278 def test_make_pod_spec_with_vca_apiproxy(self
) -> NoReturn
:
279 """Testing make pod spec with vca_apiproxy."""
280 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
282 "database_commonkey": "commonkey",
286 "vca_user": "vca_user",
287 "vca_pubkey": "vca_pubkey",
288 "vca_password": "vca_password",
289 "vca_cacert": "vca_cacert",
290 "vca_cloud": "vca_cloud",
291 "vca_k8s_cloud": "vca_k8s_cloud",
294 "message_host": "kafka",
295 "message_port": 2181,
296 "database_uri": "mongodb://mongo",
308 "imageDetails": image_info
,
309 "imagePullPolicy": "Always",
313 "containerPort": port
,
318 "ALLOW_ANONYMOUS_LOGIN": "yes",
319 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
320 "OSMLCM_RO_HOST": relation_state
["ro_host"],
321 "OSMLCM_RO_PORT": relation_state
["ro_port"],
322 "OSMLCM_RO_TENANT": "osm",
323 "OSMLCM_MESSAGE_DRIVER": "kafka",
324 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
325 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
326 "OSMLCM_DATABASE_DRIVER": "mongo",
327 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
328 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
329 "OSMLCM_STORAGE_DRIVER": "mongo",
330 "OSMLCM_STORAGE_PATH": "/app/storage",
331 "OSMLCM_STORAGE_COLLECTION": "files",
332 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
333 "OSMLCM_VCA_HOST": config
["vca_host"],
334 "OSMLCM_VCA_PORT": config
["vca_port"],
335 "OSMLCM_VCA_USER": config
["vca_user"],
336 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
337 "OSMLCM_VCA_SECRET": config
["vca_password"],
338 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
339 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
340 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
344 "kubernetesResources": {"ingressResources": []},
347 spec
= pod_spec
.make_pod_spec(
348 image_info
, config
, relation_state
, app_name
, port
351 self
.assertDictEqual(expected_result
, spec
)
353 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
354 """Testing make pod spec without image_info."""
357 "database_commonkey": "commonkey",
361 "vca_user": "vca_user",
362 "vca_pubkey": "vca_pubkey",
363 "vca_password": "vca_password",
364 "vca_cacert": "vca_cacert",
365 "vca_cloud": "vca_cloud",
366 "vca_k8s_cloud": "vca_k8s_cloud",
367 "vca_apiproxy": "vca_apiproxy",
370 "message_host": "kafka",
371 "message_port": 2181,
372 "database_uri": "mongodb://mongo",
379 spec
= pod_spec
.make_pod_spec(
380 image_info
, config
, relation_state
, app_name
, port
383 self
.assertIsNone(spec
)
385 def test_make_pod_spec_without_config(self
) -> NoReturn
:
386 """Testing make pod spec without config."""
387 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
390 "message_host": "kafka",
391 "message_port": 2181,
392 "database_uri": "mongodb://mongo",
399 with self
.assertRaises(ValidationError
):
400 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
402 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
403 """Testing make pod spec without relation_state."""
404 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
406 "database_commonkey": "commonkey",
410 "vca_user": "vca_user",
411 "vca_pubkey": "vca_pubkey",
412 "vca_password": "vca_password",
413 "vca_cacert": "vca_cacert",
414 "vca_cloud": "vca_cloud",
415 "vca_k8s_cloud": "vca_k8s_cloud",
416 "vca_apiproxy": "vca_apiproxy",
422 with self
.assertRaises(ValidationError
):
423 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
426 if __name__
== "__main__":