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 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
(9999)
46 self
.assertListEqual(expected_result
, pod_ports
)
48 def test_make_pod_envconfig_without_vca_apiproxy(self
) -> NoReturn
:
49 """Teting make pod envconfig without vca_apiproxy configuration."""
51 "database_commonkey": "commonkey",
55 "vca_user": "vca_user",
56 "vca_pubkey": "vca_pubkey",
57 "vca_password": "vca_password",
58 "vca_cacert": "vca_cacert",
59 "vca_cloud": "vca_cloud",
60 "vca_k8s_cloud": "vca_k8s_cloud",
63 "message_host": "kafka",
65 "database_uri": "mongodb://mongo",
71 "ALLOW_ANONYMOUS_LOGIN": "yes",
72 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
73 "OSMLCM_RO_HOST": relation_state
["ro_host"],
74 "OSMLCM_RO_PORT": relation_state
["ro_port"],
75 "OSMLCM_RO_TENANT": "osm",
76 "OSMLCM_MESSAGE_DRIVER": "kafka",
77 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
78 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
79 "OSMLCM_DATABASE_DRIVER": "mongo",
80 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
81 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
82 "OSMLCM_STORAGE_DRIVER": "mongo",
83 "OSMLCM_STORAGE_PATH": "/app/storage",
84 "OSMLCM_STORAGE_COLLECTION": "files",
85 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
86 "OSMLCM_VCA_HOST": config
["vca_host"],
87 "OSMLCM_VCA_PORT": config
["vca_port"],
88 "OSMLCM_VCA_USER": config
["vca_user"],
89 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
90 "OSMLCM_VCA_SECRET": config
["vca_password"],
91 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
92 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
93 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
96 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
98 self
.assertDictEqual(expected_result
, pod_envconfig
)
100 def test_make_pod_envconfig_with_vca_apiproxy(self
) -> NoReturn
:
101 """Teting make pod envconfig with vca_apiproxy configuration."""
103 "database_commonkey": "commonkey",
107 "vca_user": "vca_user",
108 "vca_pubkey": "vca_pubkey",
109 "vca_password": "vca_password",
110 "vca_cacert": "vca_cacert",
111 "vca_cloud": "vca_cloud",
112 "vca_k8s_cloud": "vca_k8s_cloud",
113 "vca_apiproxy": "vca_apiproxy",
116 "message_host": "kafka",
117 "message_port": 2181,
118 "database_uri": "mongodb://mongo",
124 "ALLOW_ANONYMOUS_LOGIN": "yes",
125 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
126 "OSMLCM_RO_HOST": relation_state
["ro_host"],
127 "OSMLCM_RO_PORT": relation_state
["ro_port"],
128 "OSMLCM_RO_TENANT": "osm",
129 "OSMLCM_MESSAGE_DRIVER": "kafka",
130 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
131 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
132 "OSMLCM_DATABASE_DRIVER": "mongo",
133 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
134 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
135 "OSMLCM_STORAGE_DRIVER": "mongo",
136 "OSMLCM_STORAGE_PATH": "/app/storage",
137 "OSMLCM_STORAGE_COLLECTION": "files",
138 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
139 "OSMLCM_VCA_HOST": config
["vca_host"],
140 "OSMLCM_VCA_PORT": config
["vca_port"],
141 "OSMLCM_VCA_USER": config
["vca_user"],
142 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
143 "OSMLCM_VCA_SECRET": config
["vca_password"],
144 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
145 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
146 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
147 "OSMLCM_VCA_APIPROXY": config
["vca_apiproxy"],
150 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
152 self
.assertDictEqual(expected_result
, pod_envconfig
)
154 def test_make_startup_probe(self
) -> NoReturn
:
155 """Testing make startup probe."""
157 "exec": {"command": ["/usr/bin/pgrep python3"]},
158 "initialDelaySeconds": 60,
162 startup_probe
= pod_spec
._make
_startup
_probe
()
164 self
.assertDictEqual(expected_result
, startup_probe
)
166 def test_make_readiness_probe(self
) -> NoReturn
:
167 """Testing make readiness probe."""
175 "initialDelaySeconds": 45,
179 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
181 self
.assertDictEqual(expected_result
, readiness_probe
)
183 def test_make_liveness_probe(self
) -> NoReturn
:
184 """Testing make liveness probe."""
192 "initialDelaySeconds": 45,
196 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
198 self
.assertDictEqual(expected_result
, liveness_probe
)
200 def test_make_pod_spec(self
) -> NoReturn
:
201 """Testing make pod spec."""
202 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
204 "database_commonkey": "commonkey",
208 "vca_user": "vca_user",
209 "vca_pubkey": "vca_pubkey",
210 "vca_password": "vca_password",
211 "vca_cacert": "vca_cacert",
212 "vca_cloud": "vca_cloud",
213 "vca_k8s_cloud": "vca_k8s_cloud",
214 "vca_apiproxy": "vca_apiproxy",
217 "message_host": "kafka",
218 "message_port": 2181,
219 "database_uri": "mongodb://mongo",
231 "imageDetails": image_info
,
232 "imagePullPolicy": "Always",
236 "containerPort": port
,
241 "ALLOW_ANONYMOUS_LOGIN": "yes",
242 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
243 "OSMLCM_RO_HOST": relation_state
["ro_host"],
244 "OSMLCM_RO_PORT": relation_state
["ro_port"],
245 "OSMLCM_RO_TENANT": "osm",
246 "OSMLCM_MESSAGE_DRIVER": "kafka",
247 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
248 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
249 "OSMLCM_DATABASE_DRIVER": "mongo",
250 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
251 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
252 "OSMLCM_STORAGE_DRIVER": "mongo",
253 "OSMLCM_STORAGE_PATH": "/app/storage",
254 "OSMLCM_STORAGE_COLLECTION": "files",
255 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
256 "OSMLCM_VCA_HOST": config
["vca_host"],
257 "OSMLCM_VCA_PORT": config
["vca_port"],
258 "OSMLCM_VCA_USER": config
["vca_user"],
259 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
260 "OSMLCM_VCA_SECRET": config
["vca_password"],
261 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
262 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
263 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
264 "OSMLCM_VCA_APIPROXY": config
["vca_apiproxy"],
268 "kubernetesResources": {"ingressResources": []},
271 spec
= pod_spec
.make_pod_spec(
272 image_info
, config
, relation_state
, app_name
, port
275 self
.assertDictEqual(expected_result
, spec
)
277 def test_make_pod_spec_with_vca_apiproxy(self
) -> NoReturn
:
278 """Testing make pod spec with vca_apiproxy."""
279 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
281 "database_commonkey": "commonkey",
285 "vca_user": "vca_user",
286 "vca_pubkey": "vca_pubkey",
287 "vca_password": "vca_password",
288 "vca_cacert": "vca_cacert",
289 "vca_cloud": "vca_cloud",
290 "vca_k8s_cloud": "vca_k8s_cloud",
293 "message_host": "kafka",
294 "message_port": 2181,
295 "database_uri": "mongodb://mongo",
307 "imageDetails": image_info
,
308 "imagePullPolicy": "Always",
312 "containerPort": port
,
317 "ALLOW_ANONYMOUS_LOGIN": "yes",
318 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
319 "OSMLCM_RO_HOST": relation_state
["ro_host"],
320 "OSMLCM_RO_PORT": relation_state
["ro_port"],
321 "OSMLCM_RO_TENANT": "osm",
322 "OSMLCM_MESSAGE_DRIVER": "kafka",
323 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
324 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
325 "OSMLCM_DATABASE_DRIVER": "mongo",
326 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
327 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
328 "OSMLCM_STORAGE_DRIVER": "mongo",
329 "OSMLCM_STORAGE_PATH": "/app/storage",
330 "OSMLCM_STORAGE_COLLECTION": "files",
331 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
332 "OSMLCM_VCA_HOST": config
["vca_host"],
333 "OSMLCM_VCA_PORT": config
["vca_port"],
334 "OSMLCM_VCA_USER": config
["vca_user"],
335 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
336 "OSMLCM_VCA_SECRET": config
["vca_password"],
337 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
338 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
339 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
343 "kubernetesResources": {"ingressResources": []},
346 spec
= pod_spec
.make_pod_spec(
347 image_info
, config
, relation_state
, app_name
, port
350 self
.assertDictEqual(expected_result
, spec
)
352 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
353 """Testing make pod spec without image_info."""
356 "database_commonkey": "commonkey",
360 "vca_user": "vca_user",
361 "vca_pubkey": "vca_pubkey",
362 "vca_password": "vca_password",
363 "vca_cacert": "vca_cacert",
364 "vca_cloud": "vca_cloud",
365 "vca_k8s_cloud": "vca_k8s_cloud",
366 "vca_apiproxy": "vca_apiproxy",
369 "message_host": "kafka",
370 "message_port": 2181,
371 "database_uri": "mongodb://mongo",
378 spec
= pod_spec
.make_pod_spec(
379 image_info
, config
, relation_state
, app_name
, port
382 self
.assertIsNone(spec
)
384 def test_make_pod_spec_without_config(self
) -> NoReturn
:
385 """Testing make pod spec without config."""
386 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
389 "message_host": "kafka",
390 "message_port": 2181,
391 "database_uri": "mongodb://mongo",
398 with self
.assertRaises(ValueError):
399 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
401 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
402 """Testing make pod spec without relation_state."""
403 image_info
= {"upstream-source": "opensourcemano/lcm:8"}
405 "database_commonkey": "commonkey",
409 "vca_user": "vca_user",
410 "vca_pubkey": "vca_pubkey",
411 "vca_password": "vca_password",
412 "vca_cacert": "vca_cacert",
413 "vca_cloud": "vca_cloud",
414 "vca_k8s_cloud": "vca_k8s_cloud",
415 "vca_apiproxy": "vca_apiproxy",
421 with self
.assertRaises(ValueError):
422 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
425 if __name__
== "__main__":