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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # For those usages not covered by the Apache License, Version 2.0 please
15 # contact: alfonso.tiernosepulveda@telefonica.com
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
26 __author__
= "Isabel Lloret <illoret@indra.es>"
29 class TestK8sHelmConn(asynctest
.TestCase
):
30 logging
.basicConfig(level
=logging
.DEBUG
)
31 logger
= logging
.getLogger(__name__
)
32 logger
.setLevel(logging
.DEBUG
)
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(
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"
51 "HELM_HOME": "{}/.helm".format(cluster_dir
),
52 "KUBECONFIG": "{}/.kube/config".format(cluster_dir
),
54 self
.helm_conn
= K8sHelmConnector(self
.fs
, self
.db
, log
=self
.logger
)
55 self
.logger
.debug("Set up executed")
57 @asynctest.fail_on(active_handles
=True)
58 async def test_init_env(self
):
62 @asynctest.fail_on(active_handles
=True)
63 async def test_repo_add(self
):
65 repo_url
= "https://charts.bitnami.com/bitnami"
66 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
68 await self
.helm_conn
.repo_add(self
.cluster_uuid
, repo_name
, repo_url
)
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
75 self
.helm_conn
._local
_async
_exec
.call_count
,
77 "local_async_exec expected 2 calls, called {}".format(
78 self
.helm_conn
._local
_async
_exec
.call_count
82 repo_update_command
= "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo update"
84 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo add {} {}"
85 ).format(repo_name
, repo_url
)
86 calls
= self
.helm_conn
._local
_async
_exec
.call_args_list
87 call0_kargs
= calls
[0][1]
89 call0_kargs
.get("command"),
91 "Invalid repo update command: {}".format(call0_kargs
.get("command")),
94 call0_kargs
.get("env"),
96 "Invalid env for update command: {}".format(call0_kargs
.get("env")),
98 call1_kargs
= calls
[1][1]
100 call1_kargs
.get("command"),
102 "Invalid repo add command: {}".format(call1_kargs
.get("command")),
105 call1_kargs
.get("env"),
107 "Invalid env for add command: {}".format(call1_kargs
.get("env")),
110 @asynctest.fail_on(active_handles
=True)
111 async def test_repo_list(self
):
112 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
114 await self
.helm_conn
.repo_list(self
.cluster_uuid
)
116 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
117 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
118 from_path
=self
.cluster_id
120 command
= "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo list --output yaml"
121 self
.helm_conn
._local
_async
_exec
.assert_called_with(
122 command
=command
, env
=self
.env
, raise_exception_on_error
=False
125 @asynctest.fail_on(active_handles
=True)
126 async def test_repo_remove(self
):
127 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
128 repo_name
= "bitnami"
129 await self
.helm_conn
.repo_remove(self
.cluster_uuid
, repo_name
)
131 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
132 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
133 from_path
=self
.cluster_id
135 command
= "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo remove {}".format(
138 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
139 command
=command
, env
=self
.env
, raise_exception_on_error
=True
142 @asynctest.fail_on(active_handles
=True)
143 async def test_install(self
):
144 kdu_model
= "stable/openldap:1.2.2"
145 kdu_instance
= "stable-openldap-0005399828"
147 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
148 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=None)
149 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
150 self
.helm_conn
.generate_kdu_instance_name
= Mock(return_value
=kdu_instance
)
152 await self
.helm_conn
.install(
157 namespace
=self
.namespace
,
161 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
162 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
163 from_path
=self
.cluster_id
165 self
.helm_conn
._store
_status
.assert_called_with(
166 cluster_id
=self
.cluster_id
,
167 kdu_instance
=kdu_instance
,
168 namespace
=self
.namespace
,
175 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm install "
176 "--atomic --output yaml --timeout 300 "
177 "--name=stable-openldap-0005399828 --namespace testk8s stable/openldap "
180 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
181 command
=command
, env
=self
.env
, raise_exception_on_error
=False
184 @asynctest.fail_on(active_handles
=True)
185 async def test_upgrade(self
):
186 kdu_model
= "stable/openldap:1.2.3"
187 kdu_instance
= "stable-openldap-0005399828"
190 "chart": "openldap-1.2.2",
191 "name": kdu_instance
,
192 "namespace": self
.namespace
,
194 "status": "DEPLOYED",
196 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
197 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
198 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
199 return_value
=instance_info
202 await self
.helm_conn
.upgrade(
203 self
.cluster_uuid
, kdu_instance
, kdu_model
, atomic
=True, db_dict
=db_dict
205 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
206 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
207 from_path
=self
.cluster_id
209 self
.helm_conn
._store
_status
.assert_called_with(
210 cluster_id
=self
.cluster_id
,
211 kdu_instance
=kdu_instance
,
212 namespace
=self
.namespace
,
219 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm upgrade "
220 "--atomic --output yaml --timeout 300 stable-openldap-0005399828 stable/openldap --version 1.2.3"
222 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
223 command
=command
, env
=self
.env
, raise_exception_on_error
=False
226 @asynctest.fail_on(active_handles
=True)
227 async def test_scale(self
):
228 kdu_model
= "stable/openldap:1.2.3"
229 kdu_instance
= "stable-openldap-0005399828"
232 "chart": "openldap-1.2.3",
233 "name": kdu_instance
,
234 "namespace": self
.namespace
,
236 "status": "DEPLOYED",
241 "url": "https://kubernetes-charts.storage.googleapis.com/",
245 # Default values for openldap.
246 # This is a YAML-formatted file.
247 # Declare variables to be passed into your templates.
254 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
255 self
.helm_conn
.values_kdu
= asynctest
.CoroutineMock(return_value
=kdu_values
)
256 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
257 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
258 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
259 return_value
=instance_info
263 await self
.helm_conn
.scale(
268 cluster_uuid
=self
.cluster_uuid
,
273 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
274 "/usr/bin/helm upgrade --atomic --output yaml --set replicaCount=2 "
275 "--timeout 1800s stable-openldap-0005399828 stable/openldap "
278 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
279 command
=command
, env
=self
.env
, raise_exception_on_error
=False
283 await self
.helm_conn
.scale(
288 cluster_uuid
=self
.cluster_uuid
,
293 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
294 "/usr/bin/helm upgrade --atomic --output yaml --set dummy-app.replicas=3 "
295 "--timeout 1800s stable-openldap-0005399828 stable/openldap "
298 self
.helm_conn
._local
_async
_exec
.assert_called_with(
299 command
=command
, env
=self
.env
, raise_exception_on_error
=False
301 self
.helm_conn
.fs
.reverse_sync
.assert_called_with(from_path
=self
.cluster_id
)
302 self
.helm_conn
._store
_status
.assert_called_with(
303 cluster_id
=self
.cluster_id
,
304 kdu_instance
=kdu_instance
,
305 namespace
=self
.namespace
,
312 @asynctest.fail_on(active_handles
=True)
313 async def test_rollback(self
):
314 kdu_instance
= "stable-openldap-0005399828"
317 "chart": "openldap-1.2.3",
318 "name": kdu_instance
,
319 "namespace": self
.namespace
,
321 "status": "DEPLOYED",
323 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
324 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
325 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
326 return_value
=instance_info
329 await self
.helm_conn
.rollback(
330 self
.cluster_uuid
, kdu_instance
=kdu_instance
, revision
=1, db_dict
=db_dict
332 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
333 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
334 from_path
=self
.cluster_id
336 self
.helm_conn
._store
_status
.assert_called_with(
337 cluster_id
=self
.cluster_id
,
338 kdu_instance
=kdu_instance
,
339 namespace
=self
.namespace
,
341 operation
="rollback",
346 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
347 "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
349 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
350 command
=command
, env
=self
.env
, raise_exception_on_error
=False
353 @asynctest.fail_on(active_handles
=True)
354 async def test_uninstall(self
):
355 kdu_instance
= "stable-openldap-0005399828"
357 "chart": "openldap-1.2.2",
358 "name": kdu_instance
,
359 "namespace": self
.namespace
,
361 "status": "DEPLOYED",
363 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
364 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
365 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
366 return_value
=instance_info
369 await self
.helm_conn
.uninstall(self
.cluster_uuid
, kdu_instance
)
370 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
371 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
372 from_path
=self
.cluster_id
374 command
= "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm delete --purge {}".format(
377 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
378 command
=command
, env
=self
.env
, raise_exception_on_error
=True
381 @asynctest.fail_on(active_handles
=True)
382 async def test_get_services(self
):
383 kdu_instance
= "test_services_1"
384 service
= {"name": "testservice", "type": "LoadBalancer"}
385 self
.helm_conn
._local
_async
_exec
_pipe
= asynctest
.CoroutineMock(
388 self
.helm_conn
._parse
_services
= Mock(return_value
=["testservice"])
389 self
.helm_conn
._get
_service
= asynctest
.CoroutineMock(return_value
=service
)
391 services
= await self
.helm_conn
.get_services(
392 self
.cluster_uuid
, kdu_instance
, self
.namespace
394 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
395 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
396 from_path
=self
.cluster_id
398 self
.helm_conn
._parse
_services
.assert_called_once()
399 command1
= "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm get manifest {} ".format(
402 command2
= "/usr/bin/kubectl get --namespace={} -f -".format(self
.namespace
)
403 self
.helm_conn
._local
_async
_exec
_pipe
.assert_called_once_with(
404 command1
, command2
, env
=self
.env
, raise_exception_on_error
=True
407 services
, [service
], "Invalid service returned from get_service"
410 @asynctest.fail_on(active_handles
=True)
411 async def test_get_service(self
):
412 service_name
= "service1"
414 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
415 await self
.helm_conn
.get_service(
416 self
.cluster_uuid
, service_name
, self
.namespace
419 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
420 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
421 from_path
=self
.cluster_id
424 "/usr/bin/kubectl --kubeconfig=./tmp/helm_cluster_id/.kube/config "
425 "--namespace=testk8s get service service1 -o=yaml"
427 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
428 command
=command
, env
=self
.env
, raise_exception_on_error
=True
431 @asynctest.fail_on(active_handles
=True)
432 async def test_inspect_kdu(self
):
433 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
435 kdu_model
= "stable/openldap:1.2.4"
436 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
437 await self
.helm_conn
.inspect_kdu(kdu_model
, repo_url
)
440 "/usr/bin/helm inspect openldap --repo "
441 "https://kubernetes-charts.storage.googleapis.com/ "
444 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
446 @asynctest.fail_on(active_handles
=True)
447 async def test_help_kdu(self
):
448 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
450 kdu_model
= "stable/openldap:1.2.4"
451 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
452 await self
.helm_conn
.help_kdu(kdu_model
, repo_url
)
455 "/usr/bin/helm inspect readme openldap --repo "
456 "https://kubernetes-charts.storage.googleapis.com/ "
459 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
461 @asynctest.fail_on(active_handles
=True)
462 async def test_values_kdu(self
):
463 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
465 kdu_model
= "stable/openldap:1.2.4"
466 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
467 await self
.helm_conn
.values_kdu(kdu_model
, repo_url
)
470 "/usr/bin/helm inspect values openldap --repo "
471 "https://kubernetes-charts.storage.googleapis.com/ "
474 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
476 @asynctest.fail_on(active_handles
=True)
477 async def test_get_values_kdu(self
):
478 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
480 kdu_instance
= "stable-openldap-0005399828"
481 await self
.helm_conn
.get_values_kdu(
482 kdu_instance
, self
.namespace
, self
.env
["KUBECONFIG"]
486 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm get values "
487 "stable-openldap-0005399828 --output yaml"
489 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
491 @asynctest.fail_on(active_handles
=True)
492 async def test_instances_list(self
):
493 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
495 await self
.helm_conn
.instances_list(self
.cluster_uuid
)
496 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
497 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
498 from_path
=self
.cluster_id
500 command
= "/usr/bin/helm list --output yaml"
501 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
502 command
=command
, env
=self
.env
, raise_exception_on_error
=True
505 @asynctest.fail_on(active_handles
=True)
506 async def test_status_kdu(self
):
507 kdu_instance
= "stable-openldap-0005399828"
508 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
510 await self
.helm_conn
._status
_kdu
(
511 self
.cluster_id
, kdu_instance
, self
.namespace
, return_text
=True
514 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm status {} --output yaml"
515 ).format(kdu_instance
)
516 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
519 raise_exception_on_error
=True,
520 show_error_log
=False,
523 @asynctest.fail_on(active_handles
=True)
524 async def test_store_status(self
):
525 kdu_instance
= "stable-openldap-0005399828"
529 "description": "Install complete",
532 "notes": "The openldap helm chart has been installed",
536 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=status
)
537 self
.helm_conn
.write_app_status_to_db
= asynctest
.CoroutineMock(
541 await self
.helm_conn
._store
_status
(
542 cluster_id
=self
.cluster_id
,
543 kdu_instance
=kdu_instance
,
544 namespace
=self
.namespace
,
550 self
.helm_conn
._status
_kdu
.assert_called_once_with(
551 cluster_id
=self
.cluster_id
,
552 kdu_instance
=kdu_instance
,
553 namespace
=self
.namespace
,
556 self
.helm_conn
.write_app_status_to_db
.assert_called_once_with(
558 status
="Install complete",
559 detailed_status
=str(status
),
563 @asynctest.fail_on(active_handles
=True)
564 async def test_reset_uninstall_false(self
):
565 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
567 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=False, uninstall_sw
=False)
568 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
569 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
570 self
.cluster_id
, ignore_non_exist
=True
572 self
.helm_conn
._uninstall
_sw
.assert_not_called()
574 @asynctest.fail_on(active_handles
=True)
575 async def test_reset_uninstall(self
):
576 kdu_instance
= "stable-openldap-0021099429"
579 "app_version": "2.4.48",
580 "chart": "openldap-1.2.3",
581 "name": kdu_instance
,
582 "namespace": self
.namespace
,
584 "status": "deployed",
585 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
588 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
589 self
.helm_conn
.instances_list
= asynctest
.CoroutineMock(return_value
=instances
)
590 self
.helm_conn
.uninstall
= asynctest
.CoroutineMock()
592 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=True, uninstall_sw
=True)
593 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
594 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
595 self
.cluster_id
, ignore_non_exist
=True
597 self
.helm_conn
.instances_list
.assert_called_once_with(
598 cluster_uuid
=self
.cluster_uuid
600 self
.helm_conn
.uninstall
.assert_called_once_with(
601 cluster_uuid
=self
.cluster_uuid
, kdu_instance
=kdu_instance
603 self
.helm_conn
._uninstall
_sw
.assert_called_once_with(
604 self
.cluster_id
, self
.namespace
607 @asynctest.fail_on(active_handles
=True)
608 async def test_uninstall_sw_namespace(self
):
609 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
611 await self
.helm_conn
._uninstall
_sw
(self
.cluster_id
, self
.namespace
)
612 calls
= self
.helm_conn
._local
_async
_exec
.call_args_list
614 len(calls
), 3, "To uninstall should have executed three commands"
616 call0_kargs
= calls
[0][1]
617 command_0
= "/usr/bin/helm --kubeconfig={} --home={} reset".format(
618 self
.kube_config
, self
.helm_home
622 {"command": command_0
, "raise_exception_on_error": True, "env": self
.env
},
623 "Invalid args for first call to local_exec",
625 call1_kargs
= calls
[1][1]
627 "/usr/bin/kubectl --kubeconfig={} delete "
628 "clusterrolebinding.rbac.authorization.k8s.io/osm-tiller-cluster-rule".format(
634 {"command": command_1
, "raise_exception_on_error": False, "env": self
.env
},
635 "Invalid args for second call to local_exec",
637 call2_kargs
= calls
[2][1]
639 "/usr/bin/kubectl --kubeconfig={} --namespace kube-system delete "
640 "serviceaccount/{}".format(self
.kube_config
, self
.service_account
)
644 {"command": command_2
, "raise_exception_on_error": False, "env": self
.env
},
645 "Invalid args for third call to local_exec",