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
, patch
22 from osm_common
.dbmemory
import DbMemory
23 from osm_common
.fslocal
import FsLocal
24 from n2vc
.k8s_helm3_conn
import K8sHelm3Connector
, K8sException
26 __author__
= "Isabel Lloret <illoret@indra.es>"
29 class TestK8sHelm3Conn(asynctest
.TestCase
):
30 logging
.basicConfig(level
=logging
.DEBUG
)
31 logger
= logging
.getLogger(__name__
)
32 logger
.setLevel(logging
.DEBUG
)
34 @patch("n2vc.k8s_helm_base_conn.EnvironConfig")
35 async def setUp(self
, mock_env
):
36 mock_env
.return_value
= {"stablerepourl": "https://charts.helm.sh/stable"}
37 self
.db
= Mock(DbMemory())
38 self
.fs
= asynctest
.Mock(FsLocal())
39 self
.fs
.path
= "./tmp/"
40 self
.namespace
= "testk8s"
41 self
.cluster_id
= "helm3_cluster_id"
42 self
.cluster_uuid
= "{}:{}".format(self
.namespace
, self
.cluster_id
)
43 # pass fake kubectl and helm commands to make sure it does not call actual commands
44 K8sHelm3Connector
._check
_file
_exists
= asynctest
.Mock(return_value
=True)
45 cluster_dir
= self
.fs
.path
+ self
.cluster_id
47 "HELM_CACHE_HOME": "{}/.cache/helm".format(cluster_dir
),
48 "HELM_CONFIG_HOME": "{}/.config/helm".format(cluster_dir
),
49 "HELM_DATA_HOME": "{}/.local/share/helm".format(cluster_dir
),
50 "KUBECONFIG": "{}/.kube/config".format(cluster_dir
),
52 self
.helm_conn
= K8sHelm3Connector(self
.fs
, self
.db
, log
=self
.logger
)
53 self
.logger
.debug("Set up executed")
55 @asynctest.fail_on(active_handles
=True)
56 async def test_init_env(self
):
57 k8s_creds
= "false_credentials_string"
58 self
.helm_conn
._get
_namespaces
= asynctest
.CoroutineMock(return_value
=[])
59 self
.helm_conn
._create
_namespace
= asynctest
.CoroutineMock()
60 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=[])
61 self
.helm_conn
.repo_add
= asynctest
.CoroutineMock()
63 k8scluster_uuid
, installed
= await self
.helm_conn
.init_env(
64 k8s_creds
, namespace
=self
.namespace
, reuse_cluster_uuid
=self
.cluster_id
69 "{}:{}".format(self
.namespace
, self
.cluster_id
),
70 "Check cluster_uuid format: <namespace>.<cluster_id>",
72 self
.helm_conn
._get
_namespaces
.assert_called_once_with(self
.cluster_id
)
73 self
.helm_conn
._create
_namespace
.assert_called_once_with(
74 self
.cluster_id
, self
.namespace
76 self
.helm_conn
.repo_list
.assert_called_once_with(k8scluster_uuid
)
77 self
.helm_conn
.repo_add
.assert_called_once_with(
78 k8scluster_uuid
, "stable", "https://charts.helm.sh/stable"
80 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
81 from_path
=self
.cluster_id
83 self
.logger
.debug(f
"cluster_uuid: {k8scluster_uuid}")
85 @asynctest.fail_on(active_handles
=True)
86 async def test_repo_add(self
):
88 repo_url
= "https://charts.bitnami.com/bitnami"
89 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=(0, ""))
91 await self
.helm_conn
.repo_add(self
.cluster_uuid
, repo_name
, repo_url
)
93 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
94 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
95 from_path
=self
.cluster_id
98 self
.helm_conn
._local
_async
_exec
.call_count
,
100 "local_async_exec expected 2 calls, called {}".format(
101 self
.helm_conn
._local
_async
_exec
.call_count
105 repo_update_command
= (
106 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo update {}"
109 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo add {} {}"
110 ).format(repo_name
, repo_url
)
111 calls
= self
.helm_conn
._local
_async
_exec
.call_args_list
112 call0_kargs
= calls
[0][1]
114 call0_kargs
.get("command"),
116 "Invalid repo add command: {}".format(call0_kargs
.get("command")),
119 call0_kargs
.get("env"),
121 "Invalid env for add command: {}".format(call0_kargs
.get("env")),
123 call1_kargs
= calls
[1][1]
125 call1_kargs
.get("command"),
127 "Invalid repo update command: {}".format(call1_kargs
.get("command")),
130 call1_kargs
.get("env"),
132 "Invalid env for update command: {}".format(call1_kargs
.get("env")),
135 @asynctest.fail_on(active_handles
=True)
136 async def test_repo_list(self
):
138 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
140 await self
.helm_conn
.repo_list(self
.cluster_uuid
)
142 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
143 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
144 from_path
=self
.cluster_id
146 command
= "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo list --output yaml"
147 self
.helm_conn
._local
_async
_exec
.assert_called_with(
148 command
=command
, env
=self
.env
, raise_exception_on_error
=False
151 @asynctest.fail_on(active_handles
=True)
152 async def test_repo_remove(self
):
154 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
155 repo_name
= "bitnami"
156 await self
.helm_conn
.repo_remove(self
.cluster_uuid
, repo_name
)
158 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
159 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
160 from_path
=self
.cluster_id
162 command
= "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo remove {}".format(
165 self
.helm_conn
._local
_async
_exec
.assert_called_with(
166 command
=command
, env
=self
.env
, raise_exception_on_error
=True
169 @asynctest.fail_on(active_handles
=True)
170 async def test_install(self
):
171 kdu_model
= "stable/openldap:1.2.2"
172 kdu_instance
= "stable-openldap-0005399828"
174 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
175 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=None)
176 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
177 self
.kdu_instance
= "stable-openldap-0005399828"
178 self
.helm_conn
.generate_kdu_instance_name
= Mock(return_value
=self
.kdu_instance
)
179 self
.helm_conn
._get
_namespaces
= asynctest
.CoroutineMock(return_value
=[])
180 self
.helm_conn
._namespace
_exists
= asynctest
.CoroutineMock(
181 side_effect
=self
.helm_conn
._namespace
_exists
183 self
.helm_conn
._create
_namespace
= asynctest
.CoroutineMock()
185 await self
.helm_conn
.install(
190 namespace
=self
.namespace
,
194 self
.helm_conn
._namespace
_exists
.assert_called_once()
195 self
.helm_conn
._get
_namespaces
.assert_called_once()
196 self
.helm_conn
._create
_namespace
.assert_called_once_with(
197 self
.cluster_id
, self
.namespace
199 self
.helm_conn
.fs
.sync
.assert_has_calls(
201 asynctest
.call(from_path
=self
.cluster_id
),
202 asynctest
.call(from_path
=self
.cluster_id
),
205 self
.helm_conn
.fs
.reverse_sync
.assert_has_calls(
207 asynctest
.call(from_path
=self
.cluster_id
),
208 asynctest
.call(from_path
=self
.cluster_id
),
211 self
.helm_conn
._store
_status
.assert_called_with(
212 cluster_id
=self
.cluster_id
,
213 kdu_instance
=kdu_instance
,
214 namespace
=self
.namespace
,
219 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
220 "install stable-openldap-0005399828 --atomic --output yaml "
221 "--timeout 300s --namespace testk8s stable/openldap --version 1.2.2"
223 self
.helm_conn
._local
_async
_exec
.assert_called_with(
224 command
=command
, env
=self
.env
, raise_exception_on_error
=False
227 # Exception test if namespace could not being created for some reason
228 self
.helm_conn
._namespace
_exists
.return_value
= False
229 self
.helm_conn
._create
_namespace
.side_effect
= Exception()
231 with self
.assertRaises(K8sException
):
232 await self
.helm_conn
.install(
237 namespace
=self
.namespace
,
241 @asynctest.fail_on(active_handles
=True)
242 async def test_namespace_exists(self
):
243 self
.helm_conn
._get
_namespaces
= asynctest
.CoroutineMock()
245 self
.helm_conn
._get
_namespaces
.return_value
= ["testk8s", "kube-system"]
246 result
= await self
.helm_conn
._namespace
_exists
(self
.cluster_id
, self
.namespace
)
247 self
.helm_conn
._get
_namespaces
.assert_called_once()
248 self
.assertEqual(result
, True)
250 self
.helm_conn
._get
_namespaces
.reset_mock()
251 result
= await self
.helm_conn
._namespace
_exists
(
252 self
.cluster_id
, "none-exists-namespace"
254 self
.helm_conn
._get
_namespaces
.assert_called_once()
255 self
.assertEqual(result
, False)
257 @asynctest.fail_on(active_handles
=True)
258 async def test_upgrade(self
):
259 kdu_model
= "stable/openldap:1.2.3"
260 kdu_instance
= "stable-openldap-0005399828"
263 "chart": "openldap-1.2.2",
264 "name": kdu_instance
,
265 "namespace": self
.namespace
,
267 "status": "DEPLOYED",
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
275 await self
.helm_conn
.upgrade(
276 self
.cluster_uuid
, kdu_instance
, kdu_model
, atomic
=True, db_dict
=db_dict
278 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
279 self
.helm_conn
.fs
.reverse_sync
.assert_has_calls(
281 asynctest
.call(from_path
=self
.cluster_id
),
282 asynctest
.call(from_path
=self
.cluster_id
),
285 self
.helm_conn
._store
_status
.assert_called_with(
286 cluster_id
=self
.cluster_id
,
287 kdu_instance
=kdu_instance
,
288 namespace
=self
.namespace
,
293 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
294 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
295 "--namespace testk8s --atomic --output yaml --timeout 300s "
298 self
.helm_conn
._local
_async
_exec
.assert_called_with(
299 command
=command
, env
=self
.env
, raise_exception_on_error
=False
302 @asynctest.fail_on(active_handles
=True)
303 async def test_rollback(self
):
304 kdu_instance
= "stable-openldap-0005399828"
307 "chart": "openldap-1.2.3",
308 "name": kdu_instance
,
309 "namespace": self
.namespace
,
311 "status": "DEPLOYED",
313 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
314 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
315 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
316 return_value
=instance_info
319 await self
.helm_conn
.rollback(
320 self
.cluster_uuid
, kdu_instance
=kdu_instance
, revision
=1, db_dict
=db_dict
322 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
323 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
324 from_path
=self
.cluster_id
326 self
.helm_conn
._store
_status
.assert_called_with(
327 cluster_id
=self
.cluster_id
,
328 kdu_instance
=kdu_instance
,
329 namespace
=self
.namespace
,
331 operation
="rollback",
334 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
335 "rollback stable-openldap-0005399828 1 --namespace=testk8s --wait"
337 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
338 command
=command
, env
=self
.env
, raise_exception_on_error
=False
341 @asynctest.fail_on(active_handles
=True)
342 async def test_uninstall(self
):
343 kdu_instance
= "stable-openldap-0005399828"
345 "chart": "openldap-1.2.2",
346 "name": kdu_instance
,
347 "namespace": self
.namespace
,
349 "status": "DEPLOYED",
351 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
352 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
353 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
354 return_value
=instance_info
357 await self
.helm_conn
.uninstall(self
.cluster_uuid
, kdu_instance
)
358 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
359 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
360 from_path
=self
.cluster_id
363 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 uninstall {} --namespace={}"
364 ).format(kdu_instance
, self
.namespace
)
365 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
366 command
=command
, env
=self
.env
, raise_exception_on_error
=True
369 @asynctest.fail_on(active_handles
=True)
370 async def test_get_services(self
):
371 kdu_instance
= "test_services_1"
372 service
= {"name": "testservice", "type": "LoadBalancer"}
373 self
.helm_conn
._local
_async
_exec
_pipe
= asynctest
.CoroutineMock(
376 self
.helm_conn
._parse
_services
= Mock(return_value
=["testservice"])
377 self
.helm_conn
._get
_service
= asynctest
.CoroutineMock(return_value
=service
)
379 services
= await self
.helm_conn
.get_services(
380 self
.cluster_uuid
, kdu_instance
, self
.namespace
382 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
383 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
384 from_path
=self
.cluster_id
386 self
.helm_conn
._parse
_services
.assert_called_once()
388 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 get manifest {} --namespace=testk8s"
389 ).format(kdu_instance
)
390 command2
= "/usr/bin/kubectl get --namespace={} -f -".format(self
.namespace
)
391 self
.helm_conn
._local
_async
_exec
_pipe
.assert_called_once_with(
392 command1
, command2
, env
=self
.env
, raise_exception_on_error
=True
395 services
, [service
], "Invalid service returned from get_service"
398 @asynctest.fail_on(active_handles
=True)
399 async def test_get_service(self
):
400 service_name
= "service1"
402 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
403 await self
.helm_conn
.get_service(
404 self
.cluster_uuid
, service_name
, self
.namespace
407 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
408 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
409 from_path
=self
.cluster_id
412 "/usr/bin/kubectl --kubeconfig=./tmp/helm3_cluster_id/.kube/config "
413 "--namespace=testk8s get service service1 -o=yaml"
415 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
416 command
=command
, env
=self
.env
, raise_exception_on_error
=True
419 @asynctest.fail_on(active_handles
=True)
420 async def test_inspect_kdu(self
):
421 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
423 kdu_model
= "stable/openldap:1.2.4"
424 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
425 await self
.helm_conn
.inspect_kdu(kdu_model
, repo_url
)
428 "/usr/bin/helm3 show all openldap --repo "
429 "https://kubernetes-charts.storage.googleapis.com/ "
432 self
.helm_conn
._local
_async
_exec
.assert_called_with(
433 command
=command
, encode_utf8
=True
436 @asynctest.fail_on(active_handles
=True)
437 async def test_help_kdu(self
):
438 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
440 kdu_model
= "stable/openldap:1.2.4"
441 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
442 await self
.helm_conn
.help_kdu(kdu_model
, repo_url
)
445 "/usr/bin/helm3 show readme openldap --repo "
446 "https://kubernetes-charts.storage.googleapis.com/ "
449 self
.helm_conn
._local
_async
_exec
.assert_called_with(
450 command
=command
, encode_utf8
=True
453 @asynctest.fail_on(active_handles
=True)
454 async def test_values_kdu(self
):
455 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
457 kdu_model
= "stable/openldap:1.2.4"
458 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
459 await self
.helm_conn
.values_kdu(kdu_model
, repo_url
)
462 "/usr/bin/helm3 show values openldap --repo "
463 "https://kubernetes-charts.storage.googleapis.com/ "
466 self
.helm_conn
._local
_async
_exec
.assert_called_with(
467 command
=command
, encode_utf8
=True
470 @asynctest.fail_on(active_handles
=True)
471 async def test_instances_list(self
):
472 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
474 await self
.helm_conn
.instances_list(self
.cluster_uuid
)
475 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
476 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
477 from_path
=self
.cluster_id
479 command
= "/usr/bin/helm3 list --all-namespaces --output yaml"
480 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
481 command
=command
, env
=self
.env
, raise_exception_on_error
=True
484 @asynctest.fail_on(active_handles
=True)
485 async def test_status_kdu(self
):
486 kdu_instance
= "stable-openldap-0005399828"
487 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
489 await self
.helm_conn
._status
_kdu
(
490 self
.cluster_id
, kdu_instance
, self
.namespace
, yaml_format
=True
493 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 status {} --namespace={} --output yaml"
494 ).format(kdu_instance
, self
.namespace
)
495 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
498 raise_exception_on_error
=True,
499 show_error_log
=False,
502 @asynctest.fail_on(active_handles
=True)
503 async def test_store_status(self
):
504 kdu_instance
= "stable-openldap-0005399828"
508 "description": "Install complete",
511 "notes": "The openldap helm chart has been installed",
515 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=status
)
516 self
.helm_conn
.write_app_status_to_db
= asynctest
.CoroutineMock(
520 await self
.helm_conn
._store
_status
(
521 cluster_id
=self
.cluster_id
,
522 kdu_instance
=kdu_instance
,
523 namespace
=self
.namespace
,
527 self
.helm_conn
._status
_kdu
.assert_called_once_with(
528 cluster_id
=self
.cluster_id
,
529 kdu_instance
=kdu_instance
,
530 namespace
=self
.namespace
,
533 self
.helm_conn
.write_app_status_to_db
.assert_called_once_with(
535 status
="Install complete",
536 detailed_status
=str(status
),
540 @asynctest.fail_on(active_handles
=True)
541 async def test_reset_uninstall_false(self
):
542 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
544 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=False, uninstall_sw
=False)
545 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
546 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
547 self
.cluster_id
, ignore_non_exist
=True
549 self
.helm_conn
._uninstall
_sw
.assert_not_called()
551 @asynctest.fail_on(active_handles
=True)
552 async def test_reset_uninstall(self
):
553 kdu_instance
= "stable-openldap-0021099429"
556 "app_version": "2.4.48",
557 "chart": "openldap-1.2.3",
558 "name": kdu_instance
,
559 "namespace": self
.namespace
,
561 "status": "deployed",
562 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
565 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
566 self
.helm_conn
.instances_list
= asynctest
.CoroutineMock(return_value
=instances
)
567 self
.helm_conn
.uninstall
= asynctest
.CoroutineMock()
569 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=True, uninstall_sw
=True)
570 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
571 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
572 self
.cluster_id
, ignore_non_exist
=True
574 self
.helm_conn
.instances_list
.assert_called_once_with(
575 cluster_uuid
=self
.cluster_uuid
577 self
.helm_conn
.uninstall
.assert_called_once_with(
578 cluster_uuid
=self
.cluster_uuid
, kdu_instance
=kdu_instance
580 self
.helm_conn
._uninstall
_sw
.assert_called_once_with(
581 self
.cluster_id
, self
.namespace
584 @asynctest.fail_on(active_handles
=True)
585 async def test_sync_repos_add(self
):
589 "url": "https://kubernetes-charts.storage.googleapis.com/",
592 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
594 def get_one_result(*args
, **kwargs
):
595 if args
[0] == "k8sclusters":
598 "helm_chart_repos": ["4b5550a9-990d-4d95-8a48-1f4614d6ac9c"]
601 elif args
[0] == "k8srepos":
603 "_id": "4b5550a9-990d-4d95-8a48-1f4614d6ac9c",
604 "type": "helm-chart",
606 "url": "https://charts.bitnami.com/bitnami",
609 self
.helm_conn
.db
.get_one
= asynctest
.Mock()
610 self
.helm_conn
.db
.get_one
.side_effect
= get_one_result
612 self
.helm_conn
.repo_add
= asynctest
.CoroutineMock()
613 self
.helm_conn
.repo_remove
= asynctest
.CoroutineMock()
615 deleted_repo_list
, added_repo_dict
= await self
.helm_conn
.synchronize_repos(
618 self
.helm_conn
.repo_remove
.assert_not_called()
619 self
.helm_conn
.repo_add
.assert_called_once_with(
620 self
.cluster_uuid
, "bitnami", "https://charts.bitnami.com/bitnami"
622 self
.assertEqual(deleted_repo_list
, [], "Deleted repo list should be empty")
625 {"4b5550a9-990d-4d95-8a48-1f4614d6ac9c": "bitnami"},
626 "Repos added should include only one bitnami",
629 @asynctest.fail_on(active_handles
=True)
630 async def test_sync_repos_delete(self
):
634 "url": "https://kubernetes-charts.storage.googleapis.com/",
636 {"name": "bitnami", "url": "https://charts.bitnami.com/bitnami"},
638 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
640 def get_one_result(*args
, **kwargs
):
641 if args
[0] == "k8sclusters":
642 return {"_admin": {"helm_chart_repos": []}}
644 self
.helm_conn
.db
.get_one
= asynctest
.Mock()
645 self
.helm_conn
.db
.get_one
.side_effect
= get_one_result
647 self
.helm_conn
.repo_add
= asynctest
.CoroutineMock()
648 self
.helm_conn
.repo_remove
= asynctest
.CoroutineMock()
650 deleted_repo_list
, added_repo_dict
= await self
.helm_conn
.synchronize_repos(
653 self
.helm_conn
.repo_add
.assert_not_called()
654 self
.helm_conn
.repo_remove
.assert_called_once_with(self
.cluster_uuid
, "bitnami")
656 deleted_repo_list
, ["bitnami"], "Deleted repo list should be bitnami"
658 self
.assertEqual(added_repo_dict
, {}, "No repos should be added")