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 = 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_force_true(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 # TEST-1 (--force true)
210 await self.helm_conn.upgrade(
211 self.cluster_uuid,
212 kdu_instance,
213 kdu_model,
214 atomic=True,
215 db_dict=db_dict,
216 force=True,
217 )
218 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
219 self.helm_conn.fs.reverse_sync.assert_has_calls(
220 [
221 asynctest.call(from_path=self.cluster_id),
222 asynctest.call(from_path=self.cluster_id),
223 ]
224 )
225 self.helm_conn._store_status.assert_called_with(
226 cluster_id=self.cluster_id,
227 kdu_instance=kdu_instance,
228 namespace=self.namespace,
229 db_dict=db_dict,
230 operation="upgrade",
231 )
232 command = (
233 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm upgrade --namespace testk8s "
234 "--atomic --output yaml --timeout 300 --force --reuse-values stable-openldap-0005399828 stable/openldap "
235 "--version 1.2.3"
236 )
237 self.helm_conn._local_async_exec.assert_called_with(
238 command=command, env=self.env, raise_exception_on_error=False
239 )
240 # TEST-2 (--force false)
241 await self.helm_conn.upgrade(
242 self.cluster_uuid,
243 kdu_instance,
244 kdu_model,
245 atomic=True,
246 db_dict=db_dict,
247 )
248 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
249 self.helm_conn.fs.reverse_sync.assert_has_calls(
250 [
251 asynctest.call(from_path=self.cluster_id),
252 asynctest.call(from_path=self.cluster_id),
253 ]
254 )
255 self.helm_conn._store_status.assert_called_with(
256 cluster_id=self.cluster_id,
257 kdu_instance=kdu_instance,
258 namespace=self.namespace,
259 db_dict=db_dict,
260 operation="upgrade",
261 )
262 command = (
263 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm upgrade --namespace testk8s "
264 "--atomic --output yaml --timeout 300 --reuse-values stable-openldap-0005399828 stable/openldap "
265 "--version 1.2.3"
266 )
267 self.helm_conn._local_async_exec.assert_called_with(
268 command=command, env=self.env, raise_exception_on_error=False
269 )
270
271 @asynctest.fail_on(active_handles=True)
272 async def test_upgrade_namespace(self):
273 kdu_model = "stable/openldap:1.2.3"
274 kdu_instance = "stable-openldap-0005399828"
275 db_dict = {}
276 instance_info = {
277 "chart": "openldap-1.2.2",
278 "name": kdu_instance,
279 "namespace": self.namespace,
280 "revision": 1,
281 "status": "DEPLOYED",
282 }
283 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
284 self.helm_conn._store_status = asynctest.CoroutineMock()
285 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
286 return_value=instance_info
287 )
288
289 await self.helm_conn.upgrade(
290 self.cluster_uuid,
291 kdu_instance,
292 kdu_model,
293 atomic=True,
294 db_dict=db_dict,
295 namespace="default",
296 )
297 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
298 self.helm_conn.fs.reverse_sync.assert_has_calls(
299 [
300 asynctest.call(from_path=self.cluster_id),
301 asynctest.call(from_path=self.cluster_id),
302 ]
303 )
304 self.helm_conn._store_status.assert_called_with(
305 cluster_id=self.cluster_id,
306 kdu_instance=kdu_instance,
307 namespace="default",
308 db_dict=db_dict,
309 operation="upgrade",
310 )
311 command = (
312 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm upgrade --namespace default "
313 "--atomic --output yaml --timeout 300 --reuse-values stable-openldap-0005399828 stable/openldap "
314 "--version 1.2.3"
315 )
316 self.helm_conn._local_async_exec.assert_called_with(
317 command=command, env=self.env, raise_exception_on_error=False
318 )
319
320 @asynctest.fail_on(active_handles=True)
321 async def test_scale(self):
322 kdu_model = "stable/openldap:1.2.3"
323 kdu_instance = "stable-openldap-0005399828"
324 db_dict = {}
325 instance_info = {
326 "chart": "openldap-1.2.3",
327 "name": kdu_instance,
328 "namespace": self.namespace,
329 "revision": 1,
330 "status": "DEPLOYED",
331 }
332 repo_list = [
333 {
334 "name": "stable",
335 "url": "https://kubernetes-charts.storage.googleapis.com/",
336 }
337 ]
338 kdu_values = """
339 # Default values for openldap.
340 # This is a YAML-formatted file.
341 # Declare variables to be passed into your templates.
342
343 replicaCount: 1
344 dummy-app:
345 replicas: 2
346 """
347
348 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=repo_list)
349 self.helm_conn.values_kdu = asynctest.CoroutineMock(return_value=kdu_values)
350 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
351 self.helm_conn._store_status = asynctest.CoroutineMock()
352 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
353 return_value=instance_info
354 )
355
356 # TEST-1
357 await self.helm_conn.scale(
358 kdu_instance,
359 2,
360 "",
361 kdu_model=kdu_model,
362 cluster_uuid=self.cluster_uuid,
363 atomic=True,
364 db_dict=db_dict,
365 )
366 command = (
367 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
368 "/usr/bin/helm upgrade --namespace testk8s --atomic --output yaml --set replicaCount=2 "
369 "--timeout 1800 --reuse-values stable-openldap-0005399828 stable/openldap "
370 "--version 1.2.3"
371 )
372 self.helm_conn._local_async_exec.assert_called_once_with(
373 command=command, env=self.env, raise_exception_on_error=False
374 )
375
376 # TEST-2
377 await self.helm_conn.scale(
378 kdu_instance,
379 3,
380 "dummy-app",
381 kdu_model=kdu_model,
382 cluster_uuid=self.cluster_uuid,
383 atomic=True,
384 db_dict=db_dict,
385 )
386 command = (
387 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
388 "/usr/bin/helm upgrade --namespace testk8s --atomic --output yaml --set dummy-app.replicas=3 "
389 "--timeout 1800 --reuse-values stable-openldap-0005399828 stable/openldap "
390 "--version 1.2.3"
391 )
392 self.helm_conn._local_async_exec.assert_called_with(
393 command=command, env=self.env, raise_exception_on_error=False
394 )
395 self.helm_conn.fs.reverse_sync.assert_called_with(from_path=self.cluster_id)
396 self.helm_conn._store_status.assert_called_with(
397 cluster_id=self.cluster_id,
398 kdu_instance=kdu_instance,
399 namespace=self.namespace,
400 db_dict=db_dict,
401 operation="scale",
402 )
403
404 @asynctest.fail_on(active_handles=True)
405 async def test_rollback(self):
406 kdu_instance = "stable-openldap-0005399828"
407 db_dict = {}
408 instance_info = {
409 "chart": "openldap-1.2.3",
410 "name": kdu_instance,
411 "namespace": self.namespace,
412 "revision": 2,
413 "status": "DEPLOYED",
414 }
415 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
416 self.helm_conn._store_status = asynctest.CoroutineMock()
417 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
418 return_value=instance_info
419 )
420
421 await self.helm_conn.rollback(
422 self.cluster_uuid, kdu_instance=kdu_instance, revision=1, db_dict=db_dict
423 )
424 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
425 self.helm_conn.fs.reverse_sync.assert_called_once_with(
426 from_path=self.cluster_id
427 )
428 self.helm_conn._store_status.assert_called_with(
429 cluster_id=self.cluster_id,
430 kdu_instance=kdu_instance,
431 namespace=self.namespace,
432 db_dict=db_dict,
433 operation="rollback",
434 )
435 command = (
436 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
437 "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
438 )
439 self.helm_conn._local_async_exec.assert_called_once_with(
440 command=command, env=self.env, raise_exception_on_error=False
441 )
442
443 @asynctest.fail_on(active_handles=True)
444 async def test_uninstall(self):
445 kdu_instance = "stable-openldap-0005399828"
446 instance_info = {
447 "chart": "openldap-1.2.2",
448 "name": kdu_instance,
449 "namespace": self.namespace,
450 "revision": 3,
451 "status": "DEPLOYED",
452 }
453 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
454 self.helm_conn._store_status = asynctest.CoroutineMock()
455 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
456 return_value=instance_info
457 )
458
459 await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
460 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
461 self.helm_conn.fs.reverse_sync.assert_called_once_with(
462 from_path=self.cluster_id
463 )
464 command = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm delete --purge {}".format(
465 kdu_instance
466 )
467 self.helm_conn._local_async_exec.assert_called_once_with(
468 command=command, env=self.env, raise_exception_on_error=True
469 )
470
471 @asynctest.fail_on(active_handles=True)
472 async def test_get_services(self):
473 kdu_instance = "test_services_1"
474 service = {"name": "testservice", "type": "LoadBalancer"}
475 self.helm_conn._local_async_exec_pipe = asynctest.CoroutineMock(
476 return_value=("", 0)
477 )
478 self.helm_conn._parse_services = Mock(return_value=["testservice"])
479 self.helm_conn._get_service = asynctest.CoroutineMock(return_value=service)
480
481 services = await self.helm_conn.get_services(
482 self.cluster_uuid, kdu_instance, self.namespace
483 )
484 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
485 self.helm_conn.fs.reverse_sync.assert_called_once_with(
486 from_path=self.cluster_id
487 )
488 self.helm_conn._parse_services.assert_called_once()
489 command1 = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm get manifest {} ".format(
490 kdu_instance
491 )
492 command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
493 self.helm_conn._local_async_exec_pipe.assert_called_once_with(
494 command1, command2, env=self.env, raise_exception_on_error=True
495 )
496 self.assertEqual(
497 services, [service], "Invalid service returned from get_service"
498 )
499
500 @asynctest.fail_on(active_handles=True)
501 async def test_get_service(self):
502 service_name = "service1"
503
504 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
505 await self.helm_conn.get_service(
506 self.cluster_uuid, service_name, self.namespace
507 )
508
509 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
510 self.helm_conn.fs.reverse_sync.assert_called_once_with(
511 from_path=self.cluster_id
512 )
513 command = (
514 "/usr/bin/kubectl --kubeconfig=./tmp/helm_cluster_id/.kube/config "
515 "--namespace=testk8s get service service1 -o=yaml"
516 )
517 self.helm_conn._local_async_exec.assert_called_once_with(
518 command=command, env=self.env, raise_exception_on_error=True
519 )
520
521 @asynctest.fail_on(active_handles=True)
522 async def test_inspect_kdu(self):
523 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
524
525 kdu_model = "stable/openldap:1.2.4"
526 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
527 await self.helm_conn.inspect_kdu(kdu_model, repo_url)
528
529 command = (
530 "/usr/bin/helm inspect openldap --repo "
531 "https://kubernetes-charts.storage.googleapis.com/ "
532 "--version 1.2.4"
533 )
534 self.helm_conn._local_async_exec.assert_called_with(command=command)
535
536 @asynctest.fail_on(active_handles=True)
537 async def test_help_kdu(self):
538 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
539
540 kdu_model = "stable/openldap:1.2.4"
541 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
542 await self.helm_conn.help_kdu(kdu_model, repo_url)
543
544 command = (
545 "/usr/bin/helm inspect readme openldap --repo "
546 "https://kubernetes-charts.storage.googleapis.com/ "
547 "--version 1.2.4"
548 )
549 self.helm_conn._local_async_exec.assert_called_with(command=command)
550
551 @asynctest.fail_on(active_handles=True)
552 async def test_values_kdu(self):
553 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
554
555 kdu_model = "stable/openldap:1.2.4"
556 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
557 await self.helm_conn.values_kdu(kdu_model, repo_url)
558
559 command = (
560 "/usr/bin/helm inspect values openldap --repo "
561 "https://kubernetes-charts.storage.googleapis.com/ "
562 "--version 1.2.4"
563 )
564 self.helm_conn._local_async_exec.assert_called_with(command=command)
565
566 @asynctest.fail_on(active_handles=True)
567 async def test_get_values_kdu(self):
568 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
569
570 kdu_instance = "stable-openldap-0005399828"
571 await self.helm_conn.get_values_kdu(
572 kdu_instance, self.namespace, self.env["KUBECONFIG"]
573 )
574
575 command = (
576 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm get values "
577 "stable-openldap-0005399828 --output yaml"
578 )
579 self.helm_conn._local_async_exec.assert_called_with(command=command)
580
581 @asynctest.fail_on(active_handles=True)
582 async def test_instances_list(self):
583 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
584
585 await self.helm_conn.instances_list(self.cluster_uuid)
586 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
587 self.helm_conn.fs.reverse_sync.assert_called_once_with(
588 from_path=self.cluster_id
589 )
590 command = "/usr/bin/helm list --output yaml"
591 self.helm_conn._local_async_exec.assert_called_once_with(
592 command=command, env=self.env, raise_exception_on_error=True
593 )
594
595 @asynctest.fail_on(active_handles=True)
596 async def test_status_kdu(self):
597 kdu_instance = "stable-openldap-0005399828"
598 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
599
600 await self.helm_conn._status_kdu(
601 self.cluster_id, kdu_instance, self.namespace, yaml_format=True
602 )
603 command = (
604 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm status {} --output yaml"
605 ).format(kdu_instance)
606 self.helm_conn._local_async_exec.assert_called_once_with(
607 command=command,
608 env=self.env,
609 raise_exception_on_error=True,
610 show_error_log=False,
611 )
612
613 @asynctest.fail_on(active_handles=True)
614 async def test_store_status(self):
615 kdu_instance = "stable-openldap-0005399828"
616 db_dict = {}
617 status = {
618 "info": {
619 "description": "Install complete",
620 "status": {
621 "code": "1",
622 "notes": "The openldap helm chart has been installed",
623 },
624 }
625 }
626 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=status)
627 self.helm_conn.write_app_status_to_db = asynctest.CoroutineMock(
628 return_value=status
629 )
630
631 await self.helm_conn._store_status(
632 cluster_id=self.cluster_id,
633 kdu_instance=kdu_instance,
634 namespace=self.namespace,
635 db_dict=db_dict,
636 operation="install",
637 )
638 self.helm_conn._status_kdu.assert_called_once_with(
639 cluster_id=self.cluster_id,
640 kdu_instance=kdu_instance,
641 namespace=self.namespace,
642 yaml_format=False,
643 )
644 self.helm_conn.write_app_status_to_db.assert_called_once_with(
645 db_dict=db_dict,
646 status="Install complete",
647 detailed_status=str(status),
648 operation="install",
649 )
650
651 @asynctest.fail_on(active_handles=True)
652 async def test_reset_uninstall_false(self):
653 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
654
655 await self.helm_conn.reset(self.cluster_uuid, force=False, uninstall_sw=False)
656 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
657 self.helm_conn.fs.file_delete.assert_called_once_with(
658 self.cluster_id, ignore_non_exist=True
659 )
660 self.helm_conn._uninstall_sw.assert_not_called()
661
662 @asynctest.fail_on(active_handles=True)
663 async def test_reset_uninstall(self):
664 kdu_instance = "stable-openldap-0021099429"
665 instances = [
666 {
667 "app_version": "2.4.48",
668 "chart": "openldap-1.2.3",
669 "name": kdu_instance,
670 "namespace": self.namespace,
671 "revision": "1",
672 "status": "deployed",
673 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
674 }
675 ]
676 self.helm_conn._get_namespace = Mock(return_value=self.namespace)
677 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
678 self.helm_conn.instances_list = asynctest.CoroutineMock(return_value=instances)
679 self.helm_conn.uninstall = asynctest.CoroutineMock()
680
681 await self.helm_conn.reset(self.cluster_uuid, force=True, uninstall_sw=True)
682 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
683 self.helm_conn.fs.file_delete.assert_called_once_with(
684 self.cluster_id, ignore_non_exist=True
685 )
686 self.helm_conn._get_namespace.assert_called_once_with(
687 cluster_uuid=self.cluster_uuid
688 )
689 self.helm_conn.instances_list.assert_called_once_with(
690 cluster_uuid=self.cluster_uuid
691 )
692 self.helm_conn.uninstall.assert_called_once_with(
693 cluster_uuid=self.cluster_uuid, kdu_instance=kdu_instance
694 )
695 self.helm_conn._uninstall_sw.assert_called_once_with(
696 cluster_id=self.cluster_id, namespace=self.namespace
697 )
698
699 @asynctest.fail_on(active_handles=True)
700 async def test_uninstall_sw_namespace(self):
701 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
702
703 await self.helm_conn._uninstall_sw(self.cluster_id, self.namespace)
704 calls = self.helm_conn._local_async_exec.call_args_list
705 self.assertEqual(
706 len(calls), 3, "To uninstall should have executed three commands"
707 )
708 call0_kargs = calls[0][1]
709 command_0 = "/usr/bin/helm --kubeconfig={} --home={} reset".format(
710 self.kube_config, self.helm_home
711 )
712 self.assertEqual(
713 call0_kargs,
714 {"command": command_0, "raise_exception_on_error": True, "env": self.env},
715 "Invalid args for first call to local_exec",
716 )
717 call1_kargs = calls[1][1]
718 command_1 = (
719 "/usr/bin/kubectl --kubeconfig={} delete "
720 "clusterrolebinding.rbac.authorization.k8s.io/osm-tiller-cluster-rule".format(
721 self.kube_config
722 )
723 )
724 self.assertEqual(
725 call1_kargs,
726 {"command": command_1, "raise_exception_on_error": False, "env": self.env},
727 "Invalid args for second call to local_exec",
728 )
729 call2_kargs = calls[2][1]
730 command_2 = (
731 "/usr/bin/kubectl --kubeconfig={} --namespace {} delete "
732 "serviceaccount/{}".format(
733 self.kube_config, self.namespace, self.service_account
734 )
735 )
736 self.assertEqual(
737 call2_kargs,
738 {"command": command_2, "raise_exception_on_error": False, "env": self.env},
739 "Invalid args for third call to local_exec",
740 )