Pin black version in tox.ini to 23.12.1
[osm/N2VC.git] / n2vc / tests / unit / test_k8s_helm_conn.py
1 ##
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13 #
14 # For those usages not covered by the Apache License, Version 2.0 please
15 # contact: alfonso.tiernosepulveda@telefonica.com
16 ##
17
18 import asynctest
19 import logging
20
21 from asynctest.mock import Mock
22 from osm_common.dbmemory import DbMemory
23 from osm_common.fslocal import FsLocal
24 from n2vc.k8s_helm_conn import K8sHelmConnector
25
26 __author__ = "Isabel Lloret <illoret@indra.es>"
27
28
29 class TestK8sHelmConn(asynctest.TestCase):
30 logging.basicConfig(level=logging.DEBUG)
31 logger = logging.getLogger(__name__)
32 logger.setLevel(logging.DEBUG)
33
34 async def setUp(self):
35 self.db = Mock(DbMemory())
36 self.fs = asynctest.Mock(FsLocal())
37 self.fs.path = "./tmp/"
38 self.namespace = "testk8s"
39 self.service_account = "osm"
40 self.cluster_id = "helm_cluster_id"
41 self.cluster_uuid = "{}:{}".format(self.namespace, self.cluster_id)
42 # pass fake kubectl and helm commands to make sure it does not call actual commands
43 K8sHelmConnector._check_file_exists = asynctest.Mock(return_value=True)
44 K8sHelmConnector._local_async_exec = asynctest.CoroutineMock(
45 return_value=("", 0)
46 )
47 cluster_dir = self.fs.path + self.cluster_id
48 self.kube_config = self.fs.path + self.cluster_id + "/.kube/config"
49 self.helm_home = self.fs.path + self.cluster_id + "/.helm"
50 self.env = {
51 "HELM_HOME": "{}/.helm".format(cluster_dir),
52 "KUBECONFIG": "{}/.kube/config".format(cluster_dir),
53 }
54 self.helm_conn = K8sHelmConnector(self.fs, self.db, log=self.logger)
55 self.logger.debug("Set up executed")
56
57 @asynctest.fail_on(active_handles=True)
58 async def test_init_env(self):
59 # TODO
60 pass
61
62 @asynctest.fail_on(active_handles=True)
63 async def test_repo_add(self):
64 repo_name = "bitnami"
65 repo_url = "https://charts.bitnami.com/bitnami"
66 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
67
68 await self.helm_conn.repo_add(self.cluster_uuid, repo_name, repo_url)
69
70 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
71 self.helm_conn.fs.reverse_sync.assert_called_once_with(
72 from_path=self.cluster_id
73 )
74 self.assertEqual(
75 self.helm_conn._local_async_exec.call_count,
76 2,
77 "local_async_exec expected 2 calls, called {}".format(
78 self.helm_conn._local_async_exec.call_count
79 ),
80 )
81
82 repo_update_command = "/usr/bin/helm repo update"
83 repo_add_command = "/usr/bin/helm repo add {} {}".format(repo_name, repo_url)
84 calls = self.helm_conn._local_async_exec.call_args_list
85 call0_kargs = calls[0][1]
86 self.assertEqual(
87 call0_kargs.get("command"),
88 repo_update_command,
89 "Invalid repo update command: {}".format(call0_kargs.get("command")),
90 )
91 self.assertEqual(
92 call0_kargs.get("env"),
93 self.env,
94 "Invalid env for update command: {}".format(call0_kargs.get("env")),
95 )
96 call1_kargs = calls[1][1]
97 self.assertEqual(
98 call1_kargs.get("command"),
99 repo_add_command,
100 "Invalid repo add command: {}".format(call1_kargs.get("command")),
101 )
102 self.assertEqual(
103 call1_kargs.get("env"),
104 self.env,
105 "Invalid env for add command: {}".format(call1_kargs.get("env")),
106 )
107
108 @asynctest.fail_on(active_handles=True)
109 async def test_repo_list(self):
110 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
111
112 await self.helm_conn.repo_list(self.cluster_uuid)
113
114 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
115 self.helm_conn.fs.reverse_sync.assert_called_once_with(
116 from_path=self.cluster_id
117 )
118 command = "/usr/bin/helm repo list --output yaml"
119 self.helm_conn._local_async_exec.assert_called_with(
120 command=command, env=self.env, raise_exception_on_error=False
121 )
122
123 @asynctest.fail_on(active_handles=True)
124 async def test_repo_remove(self):
125 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
126 repo_name = "bitnami"
127 await self.helm_conn.repo_remove(self.cluster_uuid, repo_name)
128
129 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
130 self.helm_conn.fs.reverse_sync.assert_called_once_with(
131 from_path=self.cluster_id
132 )
133 command = "/usr/bin/helm repo remove {}".format(repo_name)
134 self.helm_conn._local_async_exec.assert_called_once_with(
135 command=command, env=self.env, raise_exception_on_error=True
136 )
137
138 @asynctest.fail_on(active_handles=True)
139 async def test_install(self):
140 kdu_model = "stable/openldap:1.2.2"
141 kdu_instance = "stable-openldap-0005399828"
142 db_dict = {}
143 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
144 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=None)
145 self.helm_conn._store_status = asynctest.CoroutineMock()
146 self.helm_conn.generate_kdu_instance_name = Mock(return_value=kdu_instance)
147
148 await self.helm_conn.install(
149 self.cluster_uuid,
150 kdu_model,
151 kdu_instance,
152 atomic=True,
153 namespace=self.namespace,
154 db_dict=db_dict,
155 )
156
157 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
158 self.helm_conn.fs.reverse_sync.assert_called_once_with(
159 from_path=self.cluster_id
160 )
161 self.helm_conn._store_status.assert_called_with(
162 cluster_id=self.cluster_id,
163 kdu_instance=kdu_instance,
164 namespace=self.namespace,
165 db_dict=db_dict,
166 operation="install",
167 run_once=True,
168 check_every=0,
169 )
170 command = (
171 "/usr/bin/helm install --atomic --output yaml --timeout 300 "
172 "--name=stable-openldap-0005399828 --namespace testk8s stable/openldap "
173 "--version 1.2.2"
174 )
175 self.helm_conn._local_async_exec.assert_called_once_with(
176 command=command, env=self.env, raise_exception_on_error=False
177 )
178
179 @asynctest.fail_on(active_handles=True)
180 async def test_upgrade(self):
181 kdu_model = "stable/openldap:1.2.3"
182 kdu_instance = "stable-openldap-0005399828"
183 db_dict = {}
184 instance_info = {
185 "chart": "openldap-1.2.2",
186 "name": kdu_instance,
187 "namespace": self.namespace,
188 "revision": 1,
189 "status": "DEPLOYED",
190 }
191 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
192 self.helm_conn._store_status = asynctest.CoroutineMock()
193 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
194 return_value=instance_info
195 )
196
197 await self.helm_conn.upgrade(
198 self.cluster_uuid, kdu_instance, kdu_model, atomic=True, db_dict=db_dict
199 )
200 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
201 self.helm_conn.fs.reverse_sync.assert_called_once_with(
202 from_path=self.cluster_id
203 )
204 self.helm_conn._store_status.assert_called_with(
205 cluster_id=self.cluster_id,
206 kdu_instance=kdu_instance,
207 namespace=self.namespace,
208 db_dict=db_dict,
209 operation="upgrade",
210 run_once=True,
211 check_every=0,
212 )
213 command = (
214 "/usr/bin/helm upgrade --atomic --output yaml --timeout 300 "
215 "stable-openldap-0005399828 stable/openldap --version 1.2.3"
216 )
217 self.helm_conn._local_async_exec.assert_called_once_with(
218 command=command, env=self.env, raise_exception_on_error=False
219 )
220
221 @asynctest.fail_on(active_handles=True)
222 async def test_rollback(self):
223 kdu_instance = "stable-openldap-0005399828"
224 db_dict = {}
225 instance_info = {
226 "chart": "openldap-1.2.3",
227 "name": kdu_instance,
228 "namespace": self.namespace,
229 "revision": 2,
230 "status": "DEPLOYED",
231 }
232 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
233 self.helm_conn._store_status = asynctest.CoroutineMock()
234 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
235 return_value=instance_info
236 )
237
238 await self.helm_conn.rollback(
239 self.cluster_uuid, kdu_instance=kdu_instance, revision=1, db_dict=db_dict
240 )
241 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
242 self.helm_conn.fs.reverse_sync.assert_called_once_with(
243 from_path=self.cluster_id
244 )
245 self.helm_conn._store_status.assert_called_with(
246 cluster_id=self.cluster_id,
247 kdu_instance=kdu_instance,
248 namespace=self.namespace,
249 db_dict=db_dict,
250 operation="rollback",
251 run_once=True,
252 check_every=0,
253 )
254 command = "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
255 self.helm_conn._local_async_exec.assert_called_once_with(
256 command=command, env=self.env, raise_exception_on_error=False
257 )
258
259 @asynctest.fail_on(active_handles=True)
260 async def test_uninstall(self):
261 kdu_instance = "stable-openldap-0005399828"
262 instance_info = {
263 "chart": "openldap-1.2.2",
264 "name": kdu_instance,
265 "namespace": self.namespace,
266 "revision": 3,
267 "status": "DEPLOYED",
268 }
269 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
270 self.helm_conn._store_status = asynctest.CoroutineMock()
271 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
272 return_value=instance_info
273 )
274
275 await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
276 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
277 self.helm_conn.fs.reverse_sync.assert_called_once_with(
278 from_path=self.cluster_id
279 )
280 command = "/usr/bin/helm delete --purge {}".format(kdu_instance)
281 self.helm_conn._local_async_exec.assert_called_once_with(
282 command=command, env=self.env, raise_exception_on_error=True
283 )
284
285 @asynctest.fail_on(active_handles=True)
286 async def test_get_services(self):
287 kdu_instance = "test_services_1"
288 service = {"name": "testservice", "type": "LoadBalancer"}
289 self.helm_conn._local_async_exec_pipe = asynctest.CoroutineMock(
290 return_value=("", 0)
291 )
292 self.helm_conn._parse_services = Mock(return_value=["testservice"])
293 self.helm_conn._get_service = asynctest.CoroutineMock(return_value=service)
294
295 services = await self.helm_conn.get_services(
296 self.cluster_uuid, kdu_instance, self.namespace
297 )
298 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
299 self.helm_conn.fs.reverse_sync.assert_called_once_with(
300 from_path=self.cluster_id
301 )
302 self.helm_conn._parse_services.assert_called_once()
303 command1 = "/usr/bin/helm get manifest {} ".format(kdu_instance)
304 command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
305 self.helm_conn._local_async_exec_pipe.assert_called_once_with(
306 command1, command2, env=self.env, raise_exception_on_error=True
307 )
308 self.assertEqual(
309 services, [service], "Invalid service returned from get_service"
310 )
311
312 @asynctest.fail_on(active_handles=True)
313 async def test_get_service(self):
314 service_name = "service1"
315
316 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
317 await self.helm_conn.get_service(
318 self.cluster_uuid, service_name, self.namespace
319 )
320
321 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
322 self.helm_conn.fs.reverse_sync.assert_called_once_with(
323 from_path=self.cluster_id
324 )
325 command = (
326 "/usr/bin/kubectl --kubeconfig=./tmp/helm_cluster_id/.kube/config "
327 "--namespace=testk8s get service service1 -o=yaml"
328 )
329 self.helm_conn._local_async_exec.assert_called_once_with(
330 command=command, env=self.env, raise_exception_on_error=True
331 )
332
333 @asynctest.fail_on(active_handles=True)
334 async def test_inspect_kdu(self):
335 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
336
337 kdu_model = "stable/openldap:1.2.4"
338 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
339 await self.helm_conn.inspect_kdu(kdu_model, repo_url)
340
341 command = (
342 "/usr/bin/helm inspect openldap --repo "
343 "https://kubernetes-charts.storage.googleapis.com/ "
344 "--version 1.2.4"
345 )
346 self.helm_conn._local_async_exec.assert_called_with(
347 command=command, encode_utf8=True
348 )
349
350 @asynctest.fail_on(active_handles=True)
351 async def test_help_kdu(self):
352 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
353
354 kdu_model = "stable/openldap:1.2.4"
355 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
356 await self.helm_conn.help_kdu(kdu_model, repo_url)
357
358 command = (
359 "/usr/bin/helm inspect readme openldap --repo "
360 "https://kubernetes-charts.storage.googleapis.com/ "
361 "--version 1.2.4"
362 )
363 self.helm_conn._local_async_exec.assert_called_with(
364 command=command, encode_utf8=True
365 )
366
367 @asynctest.fail_on(active_handles=True)
368 async def test_values_kdu(self):
369 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
370
371 kdu_model = "stable/openldap:1.2.4"
372 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
373 await self.helm_conn.values_kdu(kdu_model, repo_url)
374
375 command = (
376 "/usr/bin/helm inspect values openldap --repo "
377 "https://kubernetes-charts.storage.googleapis.com/ "
378 "--version 1.2.4"
379 )
380 self.helm_conn._local_async_exec.assert_called_with(
381 command=command, encode_utf8=True
382 )
383
384 @asynctest.fail_on(active_handles=True)
385 async def test_instances_list(self):
386 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
387
388 await self.helm_conn.instances_list(self.cluster_uuid)
389 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
390 self.helm_conn.fs.reverse_sync.assert_called_once_with(
391 from_path=self.cluster_id
392 )
393 command = "/usr/bin/helm list --output yaml"
394 self.helm_conn._local_async_exec.assert_called_once_with(
395 command=command, env=self.env, raise_exception_on_error=True
396 )
397
398 @asynctest.fail_on(active_handles=True)
399 async def test_status_kdu(self):
400 kdu_instance = "stable-openldap-0005399828"
401 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
402
403 await self.helm_conn._status_kdu(
404 self.cluster_id, kdu_instance, self.namespace, return_text=True
405 )
406 command = "/usr/bin/helm status {} --output yaml".format(kdu_instance)
407 self.helm_conn._local_async_exec.assert_called_once_with(
408 command=command,
409 env=self.env,
410 raise_exception_on_error=True,
411 show_error_log=False,
412 )
413
414 @asynctest.fail_on(active_handles=True)
415 async def test_store_status(self):
416 kdu_instance = "stable-openldap-0005399828"
417 db_dict = {}
418 status = {
419 "info": {
420 "description": "Install complete",
421 "status": {
422 "code": "1",
423 "notes": "The openldap helm chart has been installed",
424 },
425 }
426 }
427 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=status)
428 self.helm_conn.write_app_status_to_db = asynctest.CoroutineMock(
429 return_value=status
430 )
431
432 await self.helm_conn._store_status(
433 cluster_id=self.cluster_id,
434 kdu_instance=kdu_instance,
435 namespace=self.namespace,
436 db_dict=db_dict,
437 operation="install",
438 run_once=True,
439 check_every=0,
440 )
441 self.helm_conn._status_kdu.assert_called_once_with(
442 cluster_id=self.cluster_id,
443 kdu_instance=kdu_instance,
444 namespace=self.namespace,
445 return_text=False,
446 )
447 self.helm_conn.write_app_status_to_db.assert_called_once_with(
448 db_dict=db_dict,
449 status="Install complete",
450 detailed_status=str(status),
451 operation="install",
452 )
453
454 @asynctest.fail_on(active_handles=True)
455 async def test_reset_uninstall_false(self):
456 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
457
458 await self.helm_conn.reset(self.cluster_uuid, force=False, uninstall_sw=False)
459 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
460 self.helm_conn.fs.file_delete.assert_called_once_with(
461 self.cluster_id, ignore_non_exist=True
462 )
463 self.helm_conn._uninstall_sw.assert_not_called()
464
465 @asynctest.fail_on(active_handles=True)
466 async def test_reset_uninstall(self):
467 kdu_instance = "stable-openldap-0021099429"
468 instances = [
469 {
470 "app_version": "2.4.48",
471 "chart": "openldap-1.2.3",
472 "name": kdu_instance,
473 "namespace": self.namespace,
474 "revision": "1",
475 "status": "deployed",
476 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
477 }
478 ]
479 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
480 self.helm_conn.instances_list = asynctest.CoroutineMock(return_value=instances)
481 self.helm_conn.uninstall = asynctest.CoroutineMock()
482
483 await self.helm_conn.reset(self.cluster_uuid, force=True, uninstall_sw=True)
484 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
485 self.helm_conn.fs.file_delete.assert_called_once_with(
486 self.cluster_id, ignore_non_exist=True
487 )
488 self.helm_conn.instances_list.assert_called_once_with(
489 cluster_uuid=self.cluster_uuid
490 )
491 self.helm_conn.uninstall.assert_called_once_with(
492 cluster_uuid=self.cluster_uuid, kdu_instance=kdu_instance
493 )
494 self.helm_conn._uninstall_sw.assert_called_once_with(
495 self.cluster_id, self.namespace
496 )
497
498 @asynctest.fail_on(active_handles=True)
499 async def test_uninstall_sw_namespace(self):
500 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
501
502 await self.helm_conn._uninstall_sw(self.cluster_id, self.namespace)
503 calls = self.helm_conn._local_async_exec.call_args_list
504 self.assertEqual(
505 len(calls), 3, "To uninstall should have executed three commands"
506 )
507 call0_kargs = calls[0][1]
508 command_0 = "/usr/bin/helm --kubeconfig={} --home={} reset".format(
509 self.kube_config, self.helm_home
510 )
511 self.assertEqual(
512 call0_kargs,
513 {"command": command_0, "raise_exception_on_error": True, "env": self.env},
514 "Invalid args for first call to local_exec",
515 )
516 call1_kargs = calls[1][1]
517 command_1 = (
518 "/usr/bin/kubectl --kubeconfig={} delete "
519 "clusterrolebinding.rbac.authorization.k8s.io/osm-tiller-cluster-rule".format(
520 self.kube_config
521 )
522 )
523 self.assertEqual(
524 call1_kargs,
525 {"command": command_1, "raise_exception_on_error": False, "env": self.env},
526 "Invalid args for second call to local_exec",
527 )
528 call2_kargs = calls[2][1]
529 command_2 = (
530 "/usr/bin/kubectl --kubeconfig={} --namespace kube-system delete "
531 "serviceaccount/{}".format(self.kube_config, self.service_account)
532 )
533 self.assertEqual(
534 call2_kargs,
535 {"command": command_2, "raise_exception_on_error": False, "env": self.env},
536 "Invalid args for third call to local_exec",
537 )