Bug 2104 fixed
[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 = 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 = (
83 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo update {}"
84 ).format(repo_name)
85 repo_add_command = (
86 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo add {} {}"
87 ).format(repo_name, repo_url)
88 calls = self.helm_conn._local_async_exec.call_args_list
89 call0_kargs = calls[0][1]
90 self.assertEqual(
91 call0_kargs.get("command"),
92 repo_add_command,
93 "Invalid repo add command: {}".format(call0_kargs.get("command")),
94 )
95 self.assertEqual(
96 call0_kargs.get("env"),
97 self.env,
98 "Invalid env for add command: {}".format(call0_kargs.get("env")),
99 )
100 call1_kargs = calls[1][1]
101 self.assertEqual(
102 call1_kargs.get("command"),
103 repo_update_command,
104 "Invalid repo update command: {}".format(call1_kargs.get("command")),
105 )
106 self.assertEqual(
107 call1_kargs.get("env"),
108 self.env,
109 "Invalid env for update command: {}".format(call1_kargs.get("env")),
110 )
111
112 @asynctest.fail_on(active_handles=True)
113 async def test_repo_list(self):
114 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
115
116 await self.helm_conn.repo_list(self.cluster_uuid)
117
118 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
119 self.helm_conn.fs.reverse_sync.assert_called_once_with(
120 from_path=self.cluster_id
121 )
122 command = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo list --output yaml"
123 self.helm_conn._local_async_exec.assert_called_with(
124 command=command, env=self.env, raise_exception_on_error=False
125 )
126
127 @asynctest.fail_on(active_handles=True)
128 async def test_repo_remove(self):
129 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
130 repo_name = "bitnami"
131 await self.helm_conn.repo_remove(self.cluster_uuid, repo_name)
132
133 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
134 self.helm_conn.fs.reverse_sync.assert_called_once_with(
135 from_path=self.cluster_id
136 )
137 command = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo remove {}".format(
138 repo_name
139 )
140 self.helm_conn._local_async_exec.assert_called_once_with(
141 command=command, env=self.env, raise_exception_on_error=True
142 )
143
144 @asynctest.fail_on(active_handles=True)
145 async def test_install(self):
146 kdu_model = "stable/openldap:1.2.2"
147 kdu_instance = "stable-openldap-0005399828"
148 db_dict = {}
149 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
150 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=None)
151 self.helm_conn._store_status = asynctest.CoroutineMock()
152 self.helm_conn.generate_kdu_instance_name = Mock(return_value=kdu_instance)
153
154 await self.helm_conn.install(
155 self.cluster_uuid,
156 kdu_model,
157 kdu_instance,
158 atomic=True,
159 namespace=self.namespace,
160 db_dict=db_dict,
161 )
162
163 self.helm_conn.fs.sync.assert_has_calls(
164 [
165 asynctest.call(from_path=self.cluster_id),
166 asynctest.call(from_path=self.cluster_id),
167 ]
168 )
169 self.helm_conn.fs.reverse_sync.assert_has_calls(
170 [
171 asynctest.call(from_path=self.cluster_id),
172 asynctest.call(from_path=self.cluster_id),
173 ]
174 )
175 self.helm_conn._store_status.assert_called_with(
176 cluster_id=self.cluster_id,
177 kdu_instance=kdu_instance,
178 namespace=self.namespace,
179 db_dict=db_dict,
180 operation="install",
181 )
182 command = (
183 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm install "
184 "--atomic --output yaml --timeout 300 "
185 "--name=stable-openldap-0005399828 --namespace testk8s stable/openldap "
186 "--version 1.2.2"
187 )
188 self.helm_conn._local_async_exec.assert_called_with(
189 command=command, env=self.env, raise_exception_on_error=False
190 )
191
192 @asynctest.fail_on(active_handles=True)
193 async def test_upgrade(self):
194 kdu_model = "stable/openldap:1.2.3"
195 kdu_instance = "stable-openldap-0005399828"
196 db_dict = {}
197 instance_info = {
198 "chart": "openldap-1.2.2",
199 "name": kdu_instance,
200 "namespace": self.namespace,
201 "revision": 1,
202 "status": "DEPLOYED",
203 }
204 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
205 self.helm_conn._store_status = asynctest.CoroutineMock()
206 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
207 return_value=instance_info
208 )
209
210 await self.helm_conn.upgrade(
211 self.cluster_uuid, kdu_instance, kdu_model, atomic=True, db_dict=db_dict
212 )
213 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
214 self.helm_conn.fs.reverse_sync.assert_has_calls(
215 [
216 asynctest.call(from_path=self.cluster_id),
217 asynctest.call(from_path=self.cluster_id),
218 ]
219 )
220 self.helm_conn._store_status.assert_called_with(
221 cluster_id=self.cluster_id,
222 kdu_instance=kdu_instance,
223 namespace=self.namespace,
224 db_dict=db_dict,
225 operation="upgrade",
226 )
227 command = (
228 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm upgrade "
229 "--atomic --output yaml --timeout 300 --reuse-values stable-openldap-0005399828 stable/openldap "
230 "--version 1.2.3"
231 )
232 self.helm_conn._local_async_exec.assert_called_with(
233 command=command, env=self.env, raise_exception_on_error=False
234 )
235
236 @asynctest.fail_on(active_handles=True)
237 async def test_scale(self):
238 kdu_model = "stable/openldap:1.2.3"
239 kdu_instance = "stable-openldap-0005399828"
240 db_dict = {}
241 instance_info = {
242 "chart": "openldap-1.2.3",
243 "name": kdu_instance,
244 "namespace": self.namespace,
245 "revision": 1,
246 "status": "DEPLOYED",
247 }
248 repo_list = [
249 {
250 "name": "stable",
251 "url": "https://kubernetes-charts.storage.googleapis.com/",
252 }
253 ]
254 kdu_values = """
255 # Default values for openldap.
256 # This is a YAML-formatted file.
257 # Declare variables to be passed into your templates.
258
259 replicaCount: 1
260 dummy-app:
261 replicas: 2
262 """
263
264 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=repo_list)
265 self.helm_conn.values_kdu = asynctest.CoroutineMock(return_value=kdu_values)
266 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
267 self.helm_conn._store_status = asynctest.CoroutineMock()
268 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
269 return_value=instance_info
270 )
271
272 # TEST-1
273 await self.helm_conn.scale(
274 kdu_instance,
275 2,
276 "",
277 kdu_model=kdu_model,
278 cluster_uuid=self.cluster_uuid,
279 atomic=True,
280 db_dict=db_dict,
281 )
282 command = (
283 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
284 "/usr/bin/helm upgrade --atomic --output yaml --set replicaCount=2 "
285 "--timeout 1800 --reuse-values stable-openldap-0005399828 stable/openldap "
286 "--version 1.2.3"
287 )
288 self.helm_conn._local_async_exec.assert_called_once_with(
289 command=command, env=self.env, raise_exception_on_error=False
290 )
291
292 # TEST-2
293 await self.helm_conn.scale(
294 kdu_instance,
295 3,
296 "dummy-app",
297 kdu_model=kdu_model,
298 cluster_uuid=self.cluster_uuid,
299 atomic=True,
300 db_dict=db_dict,
301 )
302 command = (
303 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
304 "/usr/bin/helm upgrade --atomic --output yaml --set dummy-app.replicas=3 "
305 "--timeout 1800 --reuse-values stable-openldap-0005399828 stable/openldap "
306 "--version 1.2.3"
307 )
308 self.helm_conn._local_async_exec.assert_called_with(
309 command=command, env=self.env, raise_exception_on_error=False
310 )
311 self.helm_conn.fs.reverse_sync.assert_called_with(from_path=self.cluster_id)
312 self.helm_conn._store_status.assert_called_with(
313 cluster_id=self.cluster_id,
314 kdu_instance=kdu_instance,
315 namespace=self.namespace,
316 db_dict=db_dict,
317 operation="scale",
318 )
319
320 @asynctest.fail_on(active_handles=True)
321 async def test_rollback(self):
322 kdu_instance = "stable-openldap-0005399828"
323 db_dict = {}
324 instance_info = {
325 "chart": "openldap-1.2.3",
326 "name": kdu_instance,
327 "namespace": self.namespace,
328 "revision": 2,
329 "status": "DEPLOYED",
330 }
331 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
332 self.helm_conn._store_status = asynctest.CoroutineMock()
333 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
334 return_value=instance_info
335 )
336
337 await self.helm_conn.rollback(
338 self.cluster_uuid, kdu_instance=kdu_instance, revision=1, db_dict=db_dict
339 )
340 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
341 self.helm_conn.fs.reverse_sync.assert_called_once_with(
342 from_path=self.cluster_id
343 )
344 self.helm_conn._store_status.assert_called_with(
345 cluster_id=self.cluster_id,
346 kdu_instance=kdu_instance,
347 namespace=self.namespace,
348 db_dict=db_dict,
349 operation="rollback",
350 )
351 command = (
352 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
353 "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
354 )
355 self.helm_conn._local_async_exec.assert_called_once_with(
356 command=command, env=self.env, raise_exception_on_error=False
357 )
358
359 @asynctest.fail_on(active_handles=True)
360 async def test_uninstall(self):
361 kdu_instance = "stable-openldap-0005399828"
362 instance_info = {
363 "chart": "openldap-1.2.2",
364 "name": kdu_instance,
365 "namespace": self.namespace,
366 "revision": 3,
367 "status": "DEPLOYED",
368 }
369 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
370 self.helm_conn._store_status = asynctest.CoroutineMock()
371 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
372 return_value=instance_info
373 )
374
375 await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
376 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
377 self.helm_conn.fs.reverse_sync.assert_called_once_with(
378 from_path=self.cluster_id
379 )
380 command = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm delete --purge {}".format(
381 kdu_instance
382 )
383 self.helm_conn._local_async_exec.assert_called_once_with(
384 command=command, env=self.env, raise_exception_on_error=True
385 )
386
387 @asynctest.fail_on(active_handles=True)
388 async def test_get_services(self):
389 kdu_instance = "test_services_1"
390 service = {"name": "testservice", "type": "LoadBalancer"}
391 self.helm_conn._local_async_exec_pipe = asynctest.CoroutineMock(
392 return_value=("", 0)
393 )
394 self.helm_conn._parse_services = Mock(return_value=["testservice"])
395 self.helm_conn._get_service = asynctest.CoroutineMock(return_value=service)
396
397 services = await self.helm_conn.get_services(
398 self.cluster_uuid, kdu_instance, self.namespace
399 )
400 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
401 self.helm_conn.fs.reverse_sync.assert_called_once_with(
402 from_path=self.cluster_id
403 )
404 self.helm_conn._parse_services.assert_called_once()
405 command1 = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm get manifest {} ".format(
406 kdu_instance
407 )
408 command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
409 self.helm_conn._local_async_exec_pipe.assert_called_once_with(
410 command1, command2, env=self.env, raise_exception_on_error=True
411 )
412 self.assertEqual(
413 services, [service], "Invalid service returned from get_service"
414 )
415
416 @asynctest.fail_on(active_handles=True)
417 async def test_get_service(self):
418 service_name = "service1"
419
420 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
421 await self.helm_conn.get_service(
422 self.cluster_uuid, service_name, self.namespace
423 )
424
425 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
426 self.helm_conn.fs.reverse_sync.assert_called_once_with(
427 from_path=self.cluster_id
428 )
429 command = (
430 "/usr/bin/kubectl --kubeconfig=./tmp/helm_cluster_id/.kube/config "
431 "--namespace=testk8s get service service1 -o=yaml"
432 )
433 self.helm_conn._local_async_exec.assert_called_once_with(
434 command=command, env=self.env, raise_exception_on_error=True
435 )
436
437 @asynctest.fail_on(active_handles=True)
438 async def test_inspect_kdu(self):
439 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
440
441 kdu_model = "stable/openldap:1.2.4"
442 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
443 await self.helm_conn.inspect_kdu(kdu_model, repo_url)
444
445 command = (
446 "/usr/bin/helm inspect openldap --repo "
447 "https://kubernetes-charts.storage.googleapis.com/ "
448 "--version 1.2.4"
449 )
450 self.helm_conn._local_async_exec.assert_called_with(command=command)
451
452 @asynctest.fail_on(active_handles=True)
453 async def test_help_kdu(self):
454 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
455
456 kdu_model = "stable/openldap:1.2.4"
457 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
458 await self.helm_conn.help_kdu(kdu_model, repo_url)
459
460 command = (
461 "/usr/bin/helm inspect readme openldap --repo "
462 "https://kubernetes-charts.storage.googleapis.com/ "
463 "--version 1.2.4"
464 )
465 self.helm_conn._local_async_exec.assert_called_with(command=command)
466
467 @asynctest.fail_on(active_handles=True)
468 async def test_values_kdu(self):
469 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
470
471 kdu_model = "stable/openldap:1.2.4"
472 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
473 await self.helm_conn.values_kdu(kdu_model, repo_url)
474
475 command = (
476 "/usr/bin/helm inspect values openldap --repo "
477 "https://kubernetes-charts.storage.googleapis.com/ "
478 "--version 1.2.4"
479 )
480 self.helm_conn._local_async_exec.assert_called_with(command=command)
481
482 @asynctest.fail_on(active_handles=True)
483 async def test_get_values_kdu(self):
484 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
485
486 kdu_instance = "stable-openldap-0005399828"
487 await self.helm_conn.get_values_kdu(
488 kdu_instance, self.namespace, self.env["KUBECONFIG"]
489 )
490
491 command = (
492 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm get values "
493 "stable-openldap-0005399828 --output yaml"
494 )
495 self.helm_conn._local_async_exec.assert_called_with(command=command)
496
497 @asynctest.fail_on(active_handles=True)
498 async def test_instances_list(self):
499 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
500
501 await self.helm_conn.instances_list(self.cluster_uuid)
502 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
503 self.helm_conn.fs.reverse_sync.assert_called_once_with(
504 from_path=self.cluster_id
505 )
506 command = "/usr/bin/helm list --output yaml"
507 self.helm_conn._local_async_exec.assert_called_once_with(
508 command=command, env=self.env, raise_exception_on_error=True
509 )
510
511 @asynctest.fail_on(active_handles=True)
512 async def test_status_kdu(self):
513 kdu_instance = "stable-openldap-0005399828"
514 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
515
516 await self.helm_conn._status_kdu(
517 self.cluster_id, kdu_instance, self.namespace, yaml_format=True
518 )
519 command = (
520 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm status {} --output yaml"
521 ).format(kdu_instance)
522 self.helm_conn._local_async_exec.assert_called_once_with(
523 command=command,
524 env=self.env,
525 raise_exception_on_error=True,
526 show_error_log=False,
527 )
528
529 @asynctest.fail_on(active_handles=True)
530 async def test_store_status(self):
531 kdu_instance = "stable-openldap-0005399828"
532 db_dict = {}
533 status = {
534 "info": {
535 "description": "Install complete",
536 "status": {
537 "code": "1",
538 "notes": "The openldap helm chart has been installed",
539 },
540 }
541 }
542 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=status)
543 self.helm_conn.write_app_status_to_db = asynctest.CoroutineMock(
544 return_value=status
545 )
546
547 await self.helm_conn._store_status(
548 cluster_id=self.cluster_id,
549 kdu_instance=kdu_instance,
550 namespace=self.namespace,
551 db_dict=db_dict,
552 operation="install",
553 )
554 self.helm_conn._status_kdu.assert_called_once_with(
555 cluster_id=self.cluster_id,
556 kdu_instance=kdu_instance,
557 namespace=self.namespace,
558 yaml_format=False,
559 )
560 self.helm_conn.write_app_status_to_db.assert_called_once_with(
561 db_dict=db_dict,
562 status="Install complete",
563 detailed_status=str(status),
564 operation="install",
565 )
566
567 @asynctest.fail_on(active_handles=True)
568 async def test_reset_uninstall_false(self):
569 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
570
571 await self.helm_conn.reset(self.cluster_uuid, force=False, uninstall_sw=False)
572 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
573 self.helm_conn.fs.file_delete.assert_called_once_with(
574 self.cluster_id, ignore_non_exist=True
575 )
576 self.helm_conn._uninstall_sw.assert_not_called()
577
578 @asynctest.fail_on(active_handles=True)
579 async def test_reset_uninstall(self):
580 kdu_instance = "stable-openldap-0021099429"
581 instances = [
582 {
583 "app_version": "2.4.48",
584 "chart": "openldap-1.2.3",
585 "name": kdu_instance,
586 "namespace": self.namespace,
587 "revision": "1",
588 "status": "deployed",
589 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
590 }
591 ]
592 self.helm_conn._get_namespace = Mock(return_value=self.namespace)
593 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
594 self.helm_conn.instances_list = asynctest.CoroutineMock(return_value=instances)
595 self.helm_conn.uninstall = asynctest.CoroutineMock()
596
597 await self.helm_conn.reset(self.cluster_uuid, force=True, uninstall_sw=True)
598 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
599 self.helm_conn.fs.file_delete.assert_called_once_with(
600 self.cluster_id, ignore_non_exist=True
601 )
602 self.helm_conn._get_namespace.assert_called_once_with(
603 cluster_uuid=self.cluster_uuid
604 )
605 self.helm_conn.instances_list.assert_called_once_with(
606 cluster_uuid=self.cluster_uuid
607 )
608 self.helm_conn.uninstall.assert_called_once_with(
609 cluster_uuid=self.cluster_uuid, kdu_instance=kdu_instance
610 )
611 self.helm_conn._uninstall_sw.assert_called_once_with(
612 cluster_id=self.cluster_id, namespace=self.namespace
613 )
614
615 @asynctest.fail_on(active_handles=True)
616 async def test_uninstall_sw_namespace(self):
617 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
618
619 await self.helm_conn._uninstall_sw(self.cluster_id, self.namespace)
620 calls = self.helm_conn._local_async_exec.call_args_list
621 self.assertEqual(
622 len(calls), 3, "To uninstall should have executed three commands"
623 )
624 call0_kargs = calls[0][1]
625 command_0 = "/usr/bin/helm --kubeconfig={} --home={} reset".format(
626 self.kube_config, self.helm_home
627 )
628 self.assertEqual(
629 call0_kargs,
630 {"command": command_0, "raise_exception_on_error": True, "env": self.env},
631 "Invalid args for first call to local_exec",
632 )
633 call1_kargs = calls[1][1]
634 command_1 = (
635 "/usr/bin/kubectl --kubeconfig={} delete "
636 "clusterrolebinding.rbac.authorization.k8s.io/osm-tiller-cluster-rule".format(
637 self.kube_config
638 )
639 )
640 self.assertEqual(
641 call1_kargs,
642 {"command": command_1, "raise_exception_on_error": False, "env": self.env},
643 "Invalid args for second call to local_exec",
644 )
645 call2_kargs = calls[2][1]
646 command_2 = (
647 "/usr/bin/kubectl --kubeconfig={} --namespace {} delete "
648 "serviceaccount/{}".format(
649 self.kube_config, self.namespace, self.service_account
650 )
651 )
652 self.assertEqual(
653 call2_kargs,
654 {"command": command_2, "raise_exception_on_error": False, "env": self.env},
655 "Invalid args for third call to local_exec",
656 )