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
= 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
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
= "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo update"
107 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo add {} {}"
108 ).format(repo_name
, repo_url
)
109 calls
= self
.helm_conn
._local
_async
_exec
.call_args_list
110 call0_kargs
= calls
[0][1]
112 call0_kargs
.get("command"),
114 "Invalid repo update command: {}".format(call0_kargs
.get("command")),
117 call0_kargs
.get("env"),
119 "Invalid env for update command: {}".format(call0_kargs
.get("env")),
121 call1_kargs
= calls
[1][1]
123 call1_kargs
.get("command"),
125 "Invalid repo add command: {}".format(call1_kargs
.get("command")),
128 call1_kargs
.get("env"),
130 "Invalid env for add command: {}".format(call1_kargs
.get("env")),
133 @asynctest.fail_on(active_handles
=True)
134 async def test_repo_list(self
):
136 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
138 await self
.helm_conn
.repo_list(self
.cluster_uuid
)
140 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
141 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
142 from_path
=self
.cluster_id
144 command
= "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo list --output yaml"
145 self
.helm_conn
._local
_async
_exec
.assert_called_with(
146 command
=command
, env
=self
.env
, raise_exception_on_error
=False
149 @asynctest.fail_on(active_handles
=True)
150 async def test_repo_remove(self
):
152 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
153 repo_name
= "bitnami"
154 await self
.helm_conn
.repo_remove(self
.cluster_uuid
, repo_name
)
156 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
157 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
158 from_path
=self
.cluster_id
160 command
= "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo remove {}".format(
163 self
.helm_conn
._local
_async
_exec
.assert_called_with(
164 command
=command
, env
=self
.env
, raise_exception_on_error
=True
167 @asynctest.fail_on(active_handles
=True)
168 async def test_install(self
):
169 kdu_model
= "stable/openldap:1.2.2"
170 kdu_instance
= "stable-openldap-0005399828"
172 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
173 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=None)
174 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
175 self
.kdu_instance
= "stable-openldap-0005399828"
176 self
.helm_conn
.generate_kdu_instance_name
= Mock(return_value
=self
.kdu_instance
)
177 self
.helm_conn
._get
_namespaces
= asynctest
.CoroutineMock(return_value
=[])
178 self
.helm_conn
._namespace
_exists
= asynctest
.CoroutineMock(
179 side_effect
=self
.helm_conn
._namespace
_exists
181 self
.helm_conn
._create
_namespace
= asynctest
.CoroutineMock()
183 await self
.helm_conn
.install(
188 namespace
=self
.namespace
,
192 self
.helm_conn
._namespace
_exists
.assert_called_once()
193 self
.helm_conn
._get
_namespaces
.assert_called_once()
194 self
.helm_conn
._create
_namespace
.assert_called_once_with(
195 self
.cluster_id
, self
.namespace
197 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
198 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
199 from_path
=self
.cluster_id
201 self
.helm_conn
._store
_status
.assert_called_with(
202 cluster_id
=self
.cluster_id
,
203 kdu_instance
=kdu_instance
,
204 namespace
=self
.namespace
,
211 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
212 "install stable-openldap-0005399828 --atomic --output yaml "
213 "--timeout 300s --namespace testk8s stable/openldap --version 1.2.2"
215 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
216 command
=command
, env
=self
.env
, raise_exception_on_error
=False
219 # Exception test if namespace could not being created for some reason
220 self
.helm_conn
._namespace
_exists
.return_value
= False
221 self
.helm_conn
._create
_namespace
.side_effect
= Exception()
223 with self
.assertRaises(K8sException
):
224 await self
.helm_conn
.install(
229 namespace
=self
.namespace
,
233 @asynctest.fail_on(active_handles
=True)
234 async def test_namespace_exists(self
):
235 self
.helm_conn
._get
_namespaces
= asynctest
.CoroutineMock()
237 self
.helm_conn
._get
_namespaces
.return_value
= ["testk8s", "kube-system"]
238 result
= await self
.helm_conn
._namespace
_exists
(self
.cluster_id
, self
.namespace
)
239 self
.helm_conn
._get
_namespaces
.assert_called_once()
240 self
.assertEqual(result
, True)
242 self
.helm_conn
._get
_namespaces
.reset_mock()
243 result
= await self
.helm_conn
._namespace
_exists
(
244 self
.cluster_id
, "none-exists-namespace"
246 self
.helm_conn
._get
_namespaces
.assert_called_once()
247 self
.assertEqual(result
, False)
249 @asynctest.fail_on(active_handles
=True)
250 async def test_upgrade(self
):
251 kdu_model
= "stable/openldap:1.2.3"
252 kdu_instance
= "stable-openldap-0005399828"
255 "chart": "openldap-1.2.2",
256 "name": kdu_instance
,
257 "namespace": self
.namespace
,
259 "status": "DEPLOYED",
261 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
262 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
263 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
264 return_value
=instance_info
267 await self
.helm_conn
.upgrade(
268 self
.cluster_uuid
, kdu_instance
, kdu_model
, atomic
=True, db_dict
=db_dict
270 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
271 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
272 from_path
=self
.cluster_id
274 self
.helm_conn
._store
_status
.assert_called_with(
275 cluster_id
=self
.cluster_id
,
276 kdu_instance
=kdu_instance
,
277 namespace
=self
.namespace
,
284 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
285 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
286 "--namespace testk8s --atomic --output yaml --timeout 300s "
289 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
290 command
=command
, env
=self
.env
, raise_exception_on_error
=False
293 @asynctest.fail_on(active_handles
=True)
294 async def test_rollback(self
):
295 kdu_instance
= "stable-openldap-0005399828"
298 "chart": "openldap-1.2.3",
299 "name": kdu_instance
,
300 "namespace": self
.namespace
,
302 "status": "DEPLOYED",
304 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
305 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
306 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
307 return_value
=instance_info
310 await self
.helm_conn
.rollback(
311 self
.cluster_uuid
, kdu_instance
=kdu_instance
, revision
=1, db_dict
=db_dict
313 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
314 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
315 from_path
=self
.cluster_id
317 self
.helm_conn
._store
_status
.assert_called_with(
318 cluster_id
=self
.cluster_id
,
319 kdu_instance
=kdu_instance
,
320 namespace
=self
.namespace
,
322 operation
="rollback",
327 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
328 "rollback stable-openldap-0005399828 1 --namespace=testk8s --wait"
330 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
331 command
=command
, env
=self
.env
, raise_exception_on_error
=False
334 @asynctest.fail_on(active_handles
=True)
335 async def test_uninstall(self
):
336 kdu_instance
= "stable-openldap-0005399828"
338 "chart": "openldap-1.2.2",
339 "name": kdu_instance
,
340 "namespace": self
.namespace
,
342 "status": "DEPLOYED",
344 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
345 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
346 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
347 return_value
=instance_info
350 await self
.helm_conn
.uninstall(self
.cluster_uuid
, kdu_instance
)
351 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
352 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
353 from_path
=self
.cluster_id
356 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 uninstall {} --namespace={}"
357 ).format(kdu_instance
, self
.namespace
)
358 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
359 command
=command
, env
=self
.env
, raise_exception_on_error
=True
362 @asynctest.fail_on(active_handles
=True)
363 async def test_get_services(self
):
364 kdu_instance
= "test_services_1"
365 service
= {"name": "testservice", "type": "LoadBalancer"}
366 self
.helm_conn
._local
_async
_exec
_pipe
= asynctest
.CoroutineMock(
369 self
.helm_conn
._parse
_services
= Mock(return_value
=["testservice"])
370 self
.helm_conn
._get
_service
= asynctest
.CoroutineMock(return_value
=service
)
372 services
= await self
.helm_conn
.get_services(
373 self
.cluster_uuid
, kdu_instance
, self
.namespace
375 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
376 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
377 from_path
=self
.cluster_id
379 self
.helm_conn
._parse
_services
.assert_called_once()
381 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 get manifest {} --namespace=testk8s"
382 ).format(kdu_instance
)
383 command2
= "/usr/bin/kubectl get --namespace={} -f -".format(self
.namespace
)
384 self
.helm_conn
._local
_async
_exec
_pipe
.assert_called_once_with(
385 command1
, command2
, env
=self
.env
, raise_exception_on_error
=True
388 services
, [service
], "Invalid service returned from get_service"
391 @asynctest.fail_on(active_handles
=True)
392 async def test_get_service(self
):
393 service_name
= "service1"
395 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
396 await self
.helm_conn
.get_service(
397 self
.cluster_uuid
, service_name
, self
.namespace
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
405 "/usr/bin/kubectl --kubeconfig=./tmp/helm3_cluster_id/.kube/config "
406 "--namespace=testk8s get service service1 -o=yaml"
408 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
409 command
=command
, env
=self
.env
, raise_exception_on_error
=True
412 @asynctest.fail_on(active_handles
=True)
413 async def test_inspect_kdu(self
):
414 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
416 kdu_model
= "stable/openldap:1.2.4"
417 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
418 await self
.helm_conn
.inspect_kdu(kdu_model
, repo_url
)
421 "/usr/bin/helm3 show all openldap --repo "
422 "https://kubernetes-charts.storage.googleapis.com/ "
425 self
.helm_conn
._local
_async
_exec
.assert_called_with(
426 command
=command
, encode_utf8
=True
429 @asynctest.fail_on(active_handles
=True)
430 async def test_help_kdu(self
):
431 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
433 kdu_model
= "stable/openldap:1.2.4"
434 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
435 await self
.helm_conn
.help_kdu(kdu_model
, repo_url
)
438 "/usr/bin/helm3 show readme openldap --repo "
439 "https://kubernetes-charts.storage.googleapis.com/ "
442 self
.helm_conn
._local
_async
_exec
.assert_called_with(
443 command
=command
, encode_utf8
=True
446 @asynctest.fail_on(active_handles
=True)
447 async def test_values_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
.values_kdu(kdu_model
, repo_url
)
455 "/usr/bin/helm3 show values openldap --repo "
456 "https://kubernetes-charts.storage.googleapis.com/ "
459 self
.helm_conn
._local
_async
_exec
.assert_called_with(
460 command
=command
, encode_utf8
=True
463 @asynctest.fail_on(active_handles
=True)
464 async def test_instances_list(self
):
465 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
467 await self
.helm_conn
.instances_list(self
.cluster_uuid
)
468 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
469 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
470 from_path
=self
.cluster_id
472 command
= "/usr/bin/helm3 list --all-namespaces --output yaml"
473 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
474 command
=command
, env
=self
.env
, raise_exception_on_error
=True
477 @asynctest.fail_on(active_handles
=True)
478 async def test_status_kdu(self
):
479 kdu_instance
= "stable-openldap-0005399828"
480 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
482 await self
.helm_conn
._status
_kdu
(
483 self
.cluster_id
, kdu_instance
, self
.namespace
, yaml_format
=True
486 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 status {} --namespace={} --output yaml"
487 ).format(kdu_instance
, self
.namespace
)
488 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
491 raise_exception_on_error
=True,
492 show_error_log
=False,
495 @asynctest.fail_on(active_handles
=True)
496 async def test_store_status(self
):
497 kdu_instance
= "stable-openldap-0005399828"
501 "description": "Install complete",
504 "notes": "The openldap helm chart has been installed",
508 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=status
)
509 self
.helm_conn
.write_app_status_to_db
= asynctest
.CoroutineMock(
513 await self
.helm_conn
._store
_status
(
514 cluster_id
=self
.cluster_id
,
515 kdu_instance
=kdu_instance
,
516 namespace
=self
.namespace
,
522 self
.helm_conn
._status
_kdu
.assert_called_once_with(
523 cluster_id
=self
.cluster_id
,
524 kdu_instance
=kdu_instance
,
525 namespace
=self
.namespace
,
528 self
.helm_conn
.write_app_status_to_db
.assert_called_once_with(
530 status
="Install complete",
531 detailed_status
=str(status
),
535 @asynctest.fail_on(active_handles
=True)
536 async def test_reset_uninstall_false(self
):
537 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
539 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=False, uninstall_sw
=False)
540 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
541 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
542 self
.cluster_id
, ignore_non_exist
=True
544 self
.helm_conn
._uninstall
_sw
.assert_not_called()
546 @asynctest.fail_on(active_handles
=True)
547 async def test_reset_uninstall(self
):
548 kdu_instance
= "stable-openldap-0021099429"
551 "app_version": "2.4.48",
552 "chart": "openldap-1.2.3",
553 "name": kdu_instance
,
554 "namespace": self
.namespace
,
556 "status": "deployed",
557 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
560 self
.helm_conn
._get
_namespace
= Mock(return_value
=self
.namespace
)
561 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
562 self
.helm_conn
.instances_list
= asynctest
.CoroutineMock(return_value
=instances
)
563 self
.helm_conn
.uninstall
= asynctest
.CoroutineMock()
565 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=True, uninstall_sw
=True)
566 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
567 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
568 self
.cluster_id
, ignore_non_exist
=True
570 self
.helm_conn
._get
_namespace
.assert_called_once_with(
571 cluster_uuid
=self
.cluster_uuid
573 self
.helm_conn
.instances_list
.assert_called_once_with(
574 cluster_uuid
=self
.cluster_uuid
576 self
.helm_conn
.uninstall
.assert_called_once_with(
577 cluster_uuid
=self
.cluster_uuid
, kdu_instance
=kdu_instance
579 self
.helm_conn
._uninstall
_sw
.assert_called_once_with(
580 cluster_id
=self
.cluster_id
, namespace
=self
.namespace
583 @asynctest.fail_on(active_handles
=True)
584 async def test_sync_repos_add(self
):
588 "url": "https://kubernetes-charts.storage.googleapis.com/",
591 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
593 def get_one_result(*args
, **kwargs
):
594 if args
[0] == "k8sclusters":
597 "helm_chart_repos": ["4b5550a9-990d-4d95-8a48-1f4614d6ac9c"]
600 elif args
[0] == "k8srepos":
602 "_id": "4b5550a9-990d-4d95-8a48-1f4614d6ac9c",
603 "type": "helm-chart",
605 "url": "https://charts.bitnami.com/bitnami",
608 self
.helm_conn
.db
.get_one
= asynctest
.Mock()
609 self
.helm_conn
.db
.get_one
.side_effect
= get_one_result
611 self
.helm_conn
.repo_add
= asynctest
.CoroutineMock()
612 self
.helm_conn
.repo_remove
= asynctest
.CoroutineMock()
614 deleted_repo_list
, added_repo_dict
= await self
.helm_conn
.synchronize_repos(
617 self
.helm_conn
.repo_remove
.assert_not_called()
618 self
.helm_conn
.repo_add
.assert_called_once_with(
619 self
.cluster_uuid
, "bitnami", "https://charts.bitnami.com/bitnami"
621 self
.assertEqual(deleted_repo_list
, [], "Deleted repo list should be empty")
624 {"4b5550a9-990d-4d95-8a48-1f4614d6ac9c": "bitnami"},
625 "Repos added should include only one bitnami",
628 @asynctest.fail_on(active_handles
=True)
629 async def test_sync_repos_delete(self
):
633 "url": "https://kubernetes-charts.storage.googleapis.com/",
635 {"name": "bitnami", "url": "https://charts.bitnami.com/bitnami"},
637 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
639 def get_one_result(*args
, **kwargs
):
640 if args
[0] == "k8sclusters":
641 return {"_admin": {"helm_chart_repos": []}}
643 self
.helm_conn
.db
.get_one
= asynctest
.Mock()
644 self
.helm_conn
.db
.get_one
.side_effect
= get_one_result
646 self
.helm_conn
.repo_add
= asynctest
.CoroutineMock()
647 self
.helm_conn
.repo_remove
= asynctest
.CoroutineMock()
649 deleted_repo_list
, added_repo_dict
= await self
.helm_conn
.synchronize_repos(
652 self
.helm_conn
.repo_add
.assert_not_called()
653 self
.helm_conn
.repo_remove
.assert_called_once_with(self
.cluster_uuid
, "bitnami")
655 deleted_repo_list
, ["bitnami"], "Deleted repo list should be bitnami"
657 self
.assertEqual(added_repo_dict
, {}, "No repos should be added")