Removing pydantic from LCM charm
[osm/devops.git] / installers / charm / lcm / tests / test_pod_spec.py
1 #!/usr/bin/env python3
2 # Copyright 2020 Canonical Ltd.
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 ##
22
23 from typing import NoReturn
24 import unittest
25
26 import pod_spec
27
28
29 class TestPodSpec(unittest.TestCase):
30 """Pod spec unit tests."""
31
32 def test_make_pod_ports(self) -> NoReturn:
33 """Testing make pod ports."""
34 port = 9999
35
36 expected_result = [
37 {
38 "name": "lcm",
39 "containerPort": port,
40 "protocol": "TCP",
41 }
42 ]
43
44 pod_ports = pod_spec._make_pod_ports(9999)
45
46 self.assertListEqual(expected_result, pod_ports)
47
48 def test_make_pod_envconfig_without_vca_apiproxy(self) -> NoReturn:
49 """Teting make pod envconfig without vca_apiproxy configuration."""
50 config = {
51 "database_commonkey": "commonkey",
52 "log_level": "INFO",
53 "vca_host": "vca",
54 "vca_port": 1212,
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",
61 }
62 relation_state = {
63 "message_host": "kafka",
64 "message_port": 2181,
65 "database_uri": "mongodb://mongo",
66 "ro_host": "ro",
67 "ro_port": 9090,
68 }
69
70 expected_result = {
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"],
94 }
95
96 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
97
98 self.assertDictEqual(expected_result, pod_envconfig)
99
100 def test_make_pod_envconfig_with_vca_apiproxy(self) -> NoReturn:
101 """Teting make pod envconfig with vca_apiproxy configuration."""
102 config = {
103 "database_commonkey": "commonkey",
104 "log_level": "INFO",
105 "vca_host": "vca",
106 "vca_port": 1212,
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",
114 }
115 relation_state = {
116 "message_host": "kafka",
117 "message_port": 2181,
118 "database_uri": "mongodb://mongo",
119 "ro_host": "ro",
120 "ro_port": 9090,
121 }
122
123 expected_result = {
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"],
148 }
149
150 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
151
152 self.assertDictEqual(expected_result, pod_envconfig)
153
154 def test_make_startup_probe(self) -> NoReturn:
155 """Testing make startup probe."""
156 expected_result = {
157 "exec": {"command": ["/usr/bin/pgrep python3"]},
158 "initialDelaySeconds": 60,
159 "timeoutSeconds": 5,
160 }
161
162 startup_probe = pod_spec._make_startup_probe()
163
164 self.assertDictEqual(expected_result, startup_probe)
165
166 def test_make_readiness_probe(self) -> NoReturn:
167 """Testing make readiness probe."""
168 port = 9999
169
170 expected_result = {
171 "httpGet": {
172 "path": "/osm/",
173 "port": port,
174 },
175 "initialDelaySeconds": 45,
176 "timeoutSeconds": 5,
177 }
178
179 readiness_probe = pod_spec._make_readiness_probe(port)
180
181 self.assertDictEqual(expected_result, readiness_probe)
182
183 def test_make_liveness_probe(self) -> NoReturn:
184 """Testing make liveness probe."""
185 port = 9999
186
187 expected_result = {
188 "httpGet": {
189 "path": "/osm/",
190 "port": port,
191 },
192 "initialDelaySeconds": 45,
193 "timeoutSeconds": 5,
194 }
195
196 liveness_probe = pod_spec._make_liveness_probe(port)
197
198 self.assertDictEqual(expected_result, liveness_probe)
199
200 def test_make_pod_spec(self) -> NoReturn:
201 """Testing make pod spec."""
202 image_info = {"upstream-source": "opensourcemano/lcm:8"}
203 config = {
204 "database_commonkey": "commonkey",
205 "log_level": "INFO",
206 "vca_host": "vca",
207 "vca_port": 1212,
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",
215 }
216 relation_state = {
217 "message_host": "kafka",
218 "message_port": 2181,
219 "database_uri": "mongodb://mongo",
220 "ro_host": "ro",
221 "ro_port": 9090,
222 }
223 app_name = "lcm"
224 port = 9999
225
226 expected_result = {
227 "version": 3,
228 "containers": [
229 {
230 "name": app_name,
231 "imageDetails": image_info,
232 "imagePullPolicy": "Always",
233 "ports": [
234 {
235 "name": app_name,
236 "containerPort": port,
237 "protocol": "TCP",
238 }
239 ],
240 "envConfig": {
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"],
265 },
266 }
267 ],
268 "kubernetesResources": {"ingressResources": []},
269 }
270
271 spec = pod_spec.make_pod_spec(
272 image_info, config, relation_state, app_name, port
273 )
274
275 self.assertDictEqual(expected_result, spec)
276
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"}
280 config = {
281 "database_commonkey": "commonkey",
282 "log_level": "INFO",
283 "vca_host": "vca",
284 "vca_port": 1212,
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",
291 }
292 relation_state = {
293 "message_host": "kafka",
294 "message_port": 2181,
295 "database_uri": "mongodb://mongo",
296 "ro_host": "ro",
297 "ro_port": 9090,
298 }
299 app_name = "lcm"
300 port = 9999
301
302 expected_result = {
303 "version": 3,
304 "containers": [
305 {
306 "name": app_name,
307 "imageDetails": image_info,
308 "imagePullPolicy": "Always",
309 "ports": [
310 {
311 "name": app_name,
312 "containerPort": port,
313 "protocol": "TCP",
314 }
315 ],
316 "envConfig": {
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"],
340 },
341 }
342 ],
343 "kubernetesResources": {"ingressResources": []},
344 }
345
346 spec = pod_spec.make_pod_spec(
347 image_info, config, relation_state, app_name, port
348 )
349
350 self.assertDictEqual(expected_result, spec)
351
352 def test_make_pod_spec_without_image_info(self) -> NoReturn:
353 """Testing make pod spec without image_info."""
354 image_info = None
355 config = {
356 "database_commonkey": "commonkey",
357 "log_level": "INFO",
358 "vca_host": "vca",
359 "vca_port": 1212,
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",
367 }
368 relation_state = {
369 "message_host": "kafka",
370 "message_port": 2181,
371 "database_uri": "mongodb://mongo",
372 "ro_host": "ro",
373 "ro_port": 9090,
374 }
375 app_name = "lcm"
376 port = 9999
377
378 spec = pod_spec.make_pod_spec(
379 image_info, config, relation_state, app_name, port
380 )
381
382 self.assertIsNone(spec)
383
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"}
387 config = {}
388 relation_state = {
389 "message_host": "kafka",
390 "message_port": 2181,
391 "database_uri": "mongodb://mongo",
392 "ro_host": "ro",
393 "ro_port": 9090,
394 }
395 app_name = "lcm"
396 port = 9999
397
398 with self.assertRaises(ValueError):
399 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
400
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"}
404 config = {
405 "database_commonkey": "commonkey",
406 "log_level": "INFO",
407 "vca_host": "vca",
408 "vca_port": 1212,
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",
416 }
417 relation_state = {}
418 app_name = "lcm"
419 port = 9999
420
421 with self.assertRaises(ValueError):
422 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
423
424
425 if __name__ == "__main__":
426 unittest.main()