b16ec76dd6fd44b024cf021c6b86e5ab1a95243a
[osm/N2VC.git] / n2vc / tests / unit / test_k8s_helm3_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, patch
22 from osm_common.dbmemory import DbMemory
23 from osm_common.fslocal import FsLocal
24 from n2vc.k8s_helm3_conn import K8sHelm3Connector, K8sException
25
26 __author__ = "Isabel Lloret <illoret@indra.es>"
27
28
29 class TestK8sHelm3Conn(asynctest.TestCase):
30 logging.basicConfig(level=logging.DEBUG)
31 logger = logging.getLogger(__name__)
32 logger.setLevel(logging.DEBUG)
33
34 @patch("n2vc.k8s_helm_base_conn.EnvironConfig")
35 async def setUp(self, mock_env):
36 mock_env.return_value = {
37 "stablerepourl": "https://charts.helm.sh/stable"
38 }
39 self.db = Mock(DbMemory())
40 self.fs = asynctest.Mock(FsLocal())
41 self.fs.path = "./tmp/"
42 self.namespace = "testk8s"
43 self.cluster_id = "helm3_cluster_id"
44 self.cluster_uuid = "{}:{}".format(self.namespace, self.cluster_id)
45 # pass fake kubectl and helm commands to make sure it does not call actual commands
46 K8sHelm3Connector._check_file_exists = asynctest.Mock(return_value=True)
47 cluster_dir = self.fs.path + self.cluster_id
48 self.env = {
49 "HELM_CACHE_HOME": "{}/.cache/helm".format(cluster_dir),
50 "HELM_CONFIG_HOME": "{}/.config/helm".format(cluster_dir),
51 "HELM_DATA_HOME": "{}/.local/share/helm".format(cluster_dir),
52 "KUBECONFIG": "{}/.kube/config".format(cluster_dir),
53 }
54 self.helm_conn = K8sHelm3Connector(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 k8s_creds = "false_credentials_string"
60 self.helm_conn._get_namespaces = asynctest.CoroutineMock(return_value=[])
61 self.helm_conn._create_namespace = asynctest.CoroutineMock()
62 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=[])
63 self.helm_conn.repo_add = asynctest.CoroutineMock()
64
65 k8scluster_uuid, installed = await self.helm_conn.init_env(
66 k8s_creds, namespace=self.namespace, reuse_cluster_uuid=self.cluster_id
67 )
68
69 self.assertEqual(
70 k8scluster_uuid,
71 "{}:{}".format(self.namespace, self.cluster_id),
72 "Check cluster_uuid format: <namespace>.<cluster_id>",
73 )
74 self.helm_conn._get_namespaces.assert_called_once_with(self.cluster_id)
75 self.helm_conn._create_namespace.assert_called_once_with(
76 self.cluster_id, self.namespace
77 )
78 self.helm_conn.repo_list.assert_called_once_with(k8scluster_uuid)
79 self.helm_conn.repo_add.assert_called_once_with(
80 k8scluster_uuid, "stable", "https://charts.helm.sh/stable"
81 )
82 self.helm_conn.fs.reverse_sync.assert_called_once_with(
83 from_path=self.cluster_id
84 )
85 self.logger.debug(f"cluster_uuid: {k8scluster_uuid}")
86
87 @asynctest.fail_on(active_handles=True)
88 async def test_repo_add(self):
89 repo_name = "bitnami"
90 repo_url = "https://charts.bitnami.com/bitnami"
91 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
92
93 await self.helm_conn.repo_add(self.cluster_uuid, repo_name, repo_url)
94
95 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
96 self.helm_conn.fs.reverse_sync.assert_called_once_with(
97 from_path=self.cluster_id
98 )
99 self.assertEqual(
100 self.helm_conn._local_async_exec.call_count,
101 2,
102 "local_async_exec expected 2 calls, called {}".format(
103 self.helm_conn._local_async_exec.call_count
104 ),
105 )
106
107 repo_update_command = "/usr/bin/helm3 repo update"
108 repo_add_command = "/usr/bin/helm3 repo add {} {}".format(repo_name, repo_url)
109 calls = self.helm_conn._local_async_exec.call_args_list
110 call0_kargs = calls[0][1]
111 self.assertEqual(
112 call0_kargs.get("command"),
113 repo_update_command,
114 "Invalid repo update command: {}".format(call0_kargs.get("command")),
115 )
116 self.assertEqual(
117 call0_kargs.get("env"),
118 self.env,
119 "Invalid env for update command: {}".format(call0_kargs.get("env")),
120 )
121 call1_kargs = calls[1][1]
122 self.assertEqual(
123 call1_kargs.get("command"),
124 repo_add_command,
125 "Invalid repo add command: {}".format(call1_kargs.get("command")),
126 )
127 self.assertEqual(
128 call1_kargs.get("env"),
129 self.env,
130 "Invalid env for add command: {}".format(call1_kargs.get("env")),
131 )
132
133 @asynctest.fail_on(active_handles=True)
134 async def test_repo_list(self):
135
136 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
137
138 await self.helm_conn.repo_list(self.cluster_uuid)
139
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
143 )
144 command = "/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
147 )
148
149 @asynctest.fail_on(active_handles=True)
150 async def test_repo_remove(self):
151
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)
155
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
159 )
160 command = "/usr/bin/helm3 repo remove {}".format(repo_name)
161 self.helm_conn._local_async_exec.assert_called_with(
162 command=command, env=self.env, raise_exception_on_error=True
163 )
164
165 @asynctest.fail_on(active_handles=True)
166 async def test_install(self):
167 kdu_model = "stable/openldap:1.2.2"
168 kdu_instance = "stable-openldap-0005399828"
169 db_dict = {}
170 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
171 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=None)
172 self.helm_conn._store_status = asynctest.CoroutineMock()
173 self.kdu_instance = "stable-openldap-0005399828"
174 self.helm_conn.generate_kdu_instance_name = Mock(return_value=self.kdu_instance)
175 self.helm_conn._get_namespaces = asynctest.CoroutineMock(return_value=[])
176 self.helm_conn._namespace_exists = asynctest.CoroutineMock(side_effect=self.helm_conn._namespace_exists)
177 self.helm_conn._create_namespace = asynctest.CoroutineMock()
178
179 await self.helm_conn.install(
180 self.cluster_uuid,
181 kdu_model,
182 self.kdu_instance,
183 atomic=True,
184 namespace=self.namespace,
185 db_dict=db_dict,
186 )
187
188 self.helm_conn._namespace_exists.assert_called_once()
189 self.helm_conn._get_namespaces.assert_called_once()
190 self.helm_conn._create_namespace.assert_called_once_with(
191 self.cluster_id, self.namespace
192 )
193 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
194 self.helm_conn.fs.reverse_sync.assert_called_once_with(
195 from_path=self.cluster_id
196 )
197 self.helm_conn._store_status.assert_called_with(
198 cluster_id=self.cluster_id,
199 kdu_instance=kdu_instance,
200 namespace=self.namespace,
201 db_dict=db_dict,
202 operation="install",
203 run_once=True,
204 check_every=0,
205 )
206 command = (
207 "/usr/bin/helm3 install stable-openldap-0005399828 --atomic --output yaml "
208 "--timeout 300s --namespace testk8s stable/openldap --version 1.2.2"
209 )
210 self.helm_conn._local_async_exec.assert_called_once_with(
211 command=command, env=self.env, raise_exception_on_error=False
212 )
213
214 # Exception test if namespace could not being created for some reason
215 self.helm_conn._namespace_exists.return_value = False
216 self.helm_conn._create_namespace.side_effect = Exception()
217
218 with self.assertRaises(K8sException):
219 await self.helm_conn.install(
220 self.cluster_uuid,
221 kdu_model,
222 self.kdu_instance,
223 atomic=True,
224 namespace=self.namespace,
225 db_dict=db_dict,
226 )
227
228 @asynctest.fail_on(active_handles=True)
229 async def test_namespace_exists(self):
230 self.helm_conn._get_namespaces = asynctest.CoroutineMock()
231
232 self.helm_conn._get_namespaces.return_value = ['testk8s', 'kube-system']
233 result = await self.helm_conn._namespace_exists(self.cluster_id, self.namespace)
234 self.helm_conn._get_namespaces.assert_called_once()
235 self.assertEqual(result, True)
236
237 self.helm_conn._get_namespaces.reset_mock()
238 result = await self.helm_conn._namespace_exists(self.cluster_id, 'none-exists-namespace')
239 self.helm_conn._get_namespaces.assert_called_once()
240 self.assertEqual(result, False)
241
242 @asynctest.fail_on(active_handles=True)
243 async def test_upgrade(self):
244 kdu_model = "stable/openldap:1.2.3"
245 kdu_instance = "stable-openldap-0005399828"
246 db_dict = {}
247 instance_info = {
248 "chart": "openldap-1.2.2",
249 "name": kdu_instance,
250 "namespace": self.namespace,
251 "revision": 1,
252 "status": "DEPLOYED",
253 }
254 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
255 self.helm_conn._store_status = asynctest.CoroutineMock()
256 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
257 return_value=instance_info
258 )
259
260 await self.helm_conn.upgrade(
261 self.cluster_uuid, kdu_instance, kdu_model, atomic=True, db_dict=db_dict
262 )
263 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
264 self.helm_conn.fs.reverse_sync.assert_called_once_with(
265 from_path=self.cluster_id
266 )
267 self.helm_conn._store_status.assert_called_with(
268 cluster_id=self.cluster_id,
269 kdu_instance=kdu_instance,
270 namespace=self.namespace,
271 db_dict=db_dict,
272 operation="upgrade",
273 run_once=True,
274 check_every=0,
275 )
276 command = (
277 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
278 "--namespace testk8s --atomic --output yaml --timeout 300s "
279 "--version 1.2.3"
280 )
281 self.helm_conn._local_async_exec.assert_called_once_with(
282 command=command, env=self.env, raise_exception_on_error=False
283 )
284
285 @asynctest.fail_on(active_handles=True)
286 async def test_rollback(self):
287 kdu_instance = "stable-openldap-0005399828"
288 db_dict = {}
289 instance_info = {
290 "chart": "openldap-1.2.3",
291 "name": kdu_instance,
292 "namespace": self.namespace,
293 "revision": 2,
294 "status": "DEPLOYED",
295 }
296 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
297 self.helm_conn._store_status = asynctest.CoroutineMock()
298 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
299 return_value=instance_info
300 )
301
302 await self.helm_conn.rollback(
303 self.cluster_uuid, kdu_instance=kdu_instance, revision=1, db_dict=db_dict
304 )
305 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
306 self.helm_conn.fs.reverse_sync.assert_called_once_with(
307 from_path=self.cluster_id
308 )
309 self.helm_conn._store_status.assert_called_with(
310 cluster_id=self.cluster_id,
311 kdu_instance=kdu_instance,
312 namespace=self.namespace,
313 db_dict=db_dict,
314 operation="rollback",
315 run_once=True,
316 check_every=0,
317 )
318 command = "/usr/bin/helm3 rollback stable-openldap-0005399828 1 --namespace=testk8s --wait"
319 self.helm_conn._local_async_exec.assert_called_once_with(
320 command=command, env=self.env, raise_exception_on_error=False
321 )
322
323 @asynctest.fail_on(active_handles=True)
324 async def test_uninstall(self):
325 kdu_instance = "stable-openldap-0005399828"
326 instance_info = {
327 "chart": "openldap-1.2.2",
328 "name": kdu_instance,
329 "namespace": self.namespace,
330 "revision": 3,
331 "status": "DEPLOYED",
332 }
333 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
334 self.helm_conn._store_status = asynctest.CoroutineMock()
335 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
336 return_value=instance_info
337 )
338
339 await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
340 self.helm_conn.fs.sync.assert_called_once_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 command = "/usr/bin/helm3 uninstall {} --namespace={}".format(
345 kdu_instance, self.namespace
346 )
347 self.helm_conn._local_async_exec.assert_called_once_with(
348 command=command, env=self.env, raise_exception_on_error=True
349 )
350
351 @asynctest.fail_on(active_handles=True)
352 async def test_get_services(self):
353 kdu_instance = "test_services_1"
354 service = {"name": "testservice", "type": "LoadBalancer"}
355 self.helm_conn._local_async_exec_pipe = asynctest.CoroutineMock(
356 return_value=("", 0)
357 )
358 self.helm_conn._parse_services = Mock(return_value=["testservice"])
359 self.helm_conn._get_service = asynctest.CoroutineMock(return_value=service)
360
361 services = await self.helm_conn.get_services(
362 self.cluster_uuid, kdu_instance, self.namespace
363 )
364 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
365 self.helm_conn.fs.reverse_sync.assert_called_once_with(
366 from_path=self.cluster_id
367 )
368 self.helm_conn._parse_services.assert_called_once()
369 command1 = "/usr/bin/helm3 get manifest {} --namespace=testk8s".format(
370 kdu_instance
371 )
372 command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
373 self.helm_conn._local_async_exec_pipe.assert_called_once_with(
374 command1, command2, env=self.env, raise_exception_on_error=True
375 )
376 self.assertEqual(
377 services, [service], "Invalid service returned from get_service"
378 )
379
380 @asynctest.fail_on(active_handles=True)
381 async def test_get_service(self):
382 service_name = "service1"
383
384 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
385 await self.helm_conn.get_service(
386 self.cluster_uuid, service_name, self.namespace
387 )
388
389 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
390 self.helm_conn.fs.reverse_sync.assert_called_once_with(
391 from_path=self.cluster_id
392 )
393 command = (
394 "/usr/bin/kubectl --kubeconfig=./tmp/helm3_cluster_id/.kube/config "
395 "--namespace=testk8s get service service1 -o=yaml"
396 )
397 self.helm_conn._local_async_exec.assert_called_once_with(
398 command=command, env=self.env, raise_exception_on_error=True
399 )
400
401 @asynctest.fail_on(active_handles=True)
402 async def test_inspect_kdu(self):
403 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
404
405 kdu_model = "stable/openldap:1.2.4"
406 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
407 await self.helm_conn.inspect_kdu(kdu_model, repo_url)
408
409 command = (
410 "/usr/bin/helm3 show all openldap --repo "
411 "https://kubernetes-charts.storage.googleapis.com/ "
412 "--version 1.2.4"
413 )
414 self.helm_conn._local_async_exec.assert_called_with(
415 command=command, encode_utf8=True
416 )
417
418 @asynctest.fail_on(active_handles=True)
419 async def test_help_kdu(self):
420 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
421
422 kdu_model = "stable/openldap:1.2.4"
423 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
424 await self.helm_conn.help_kdu(kdu_model, repo_url)
425
426 command = (
427 "/usr/bin/helm3 show readme openldap --repo "
428 "https://kubernetes-charts.storage.googleapis.com/ "
429 "--version 1.2.4"
430 )
431 self.helm_conn._local_async_exec.assert_called_with(
432 command=command, encode_utf8=True
433 )
434
435 @asynctest.fail_on(active_handles=True)
436 async def test_values_kdu(self):
437 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
438
439 kdu_model = "stable/openldap:1.2.4"
440 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
441 await self.helm_conn.values_kdu(kdu_model, repo_url)
442
443 command = (
444 "/usr/bin/helm3 show values openldap --repo "
445 "https://kubernetes-charts.storage.googleapis.com/ "
446 "--version 1.2.4"
447 )
448 self.helm_conn._local_async_exec.assert_called_with(
449 command=command, encode_utf8=True
450 )
451
452 @asynctest.fail_on(active_handles=True)
453 async def test_instances_list(self):
454 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
455
456 await self.helm_conn.instances_list(self.cluster_uuid)
457 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
458 self.helm_conn.fs.reverse_sync.assert_called_once_with(
459 from_path=self.cluster_id
460 )
461 command = "/usr/bin/helm3 list --all-namespaces --output yaml"
462 self.helm_conn._local_async_exec.assert_called_once_with(
463 command=command, env=self.env, raise_exception_on_error=True
464 )
465
466 @asynctest.fail_on(active_handles=True)
467 async def test_status_kdu(self):
468 kdu_instance = "stable-openldap-0005399828"
469 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
470
471 await self.helm_conn._status_kdu(
472 self.cluster_id, kdu_instance, self.namespace, return_text=True
473 )
474 command = "/usr/bin/helm3 status {} --namespace={} --output yaml".format(
475 kdu_instance, self.namespace
476 )
477 self.helm_conn._local_async_exec.assert_called_once_with(
478 command=command,
479 env=self.env,
480 raise_exception_on_error=True,
481 show_error_log=False,
482 )
483
484 @asynctest.fail_on(active_handles=True)
485 async def test_store_status(self):
486 kdu_instance = "stable-openldap-0005399828"
487 db_dict = {}
488 status = {
489 "info": {
490 "description": "Install complete",
491 "status": {
492 "code": "1",
493 "notes": "The openldap helm chart has been installed",
494 },
495 }
496 }
497 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=status)
498 self.helm_conn.write_app_status_to_db = asynctest.CoroutineMock(
499 return_value=status
500 )
501
502 await self.helm_conn._store_status(
503 cluster_id=self.cluster_id,
504 kdu_instance=kdu_instance,
505 namespace=self.namespace,
506 db_dict=db_dict,
507 operation="install",
508 run_once=True,
509 check_every=0,
510 )
511 self.helm_conn._status_kdu.assert_called_once_with(
512 cluster_id=self.cluster_id,
513 kdu_instance=kdu_instance,
514 namespace=self.namespace,
515 return_text=False,
516 )
517 self.helm_conn.write_app_status_to_db.assert_called_once_with(
518 db_dict=db_dict,
519 status="Install complete",
520 detailed_status=str(status),
521 operation="install",
522 )
523
524 @asynctest.fail_on(active_handles=True)
525 async def test_reset_uninstall_false(self):
526 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
527
528 await self.helm_conn.reset(self.cluster_uuid, force=False, uninstall_sw=False)
529 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
530 self.helm_conn.fs.file_delete.assert_called_once_with(
531 self.cluster_id, ignore_non_exist=True
532 )
533 self.helm_conn._uninstall_sw.assert_not_called()
534
535 @asynctest.fail_on(active_handles=True)
536 async def test_reset_uninstall(self):
537 kdu_instance = "stable-openldap-0021099429"
538 instances = [
539 {
540 "app_version": "2.4.48",
541 "chart": "openldap-1.2.3",
542 "name": kdu_instance,
543 "namespace": self.namespace,
544 "revision": "1",
545 "status": "deployed",
546 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
547 }
548 ]
549 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
550 self.helm_conn.instances_list = asynctest.CoroutineMock(return_value=instances)
551 self.helm_conn.uninstall = asynctest.CoroutineMock()
552
553 await self.helm_conn.reset(self.cluster_uuid, force=True, uninstall_sw=True)
554 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
555 self.helm_conn.fs.file_delete.assert_called_once_with(
556 self.cluster_id, ignore_non_exist=True
557 )
558 self.helm_conn.instances_list.assert_called_once_with(
559 cluster_uuid=self.cluster_uuid
560 )
561 self.helm_conn.uninstall.assert_called_once_with(
562 cluster_uuid=self.cluster_uuid, kdu_instance=kdu_instance
563 )
564 self.helm_conn._uninstall_sw.assert_called_once_with(
565 self.cluster_id, self.namespace
566 )
567
568 @asynctest.fail_on(active_handles=True)
569 async def test_sync_repos_add(self):
570 repo_list = [
571 {
572 "name": "stable",
573 "url": "https://kubernetes-charts.storage.googleapis.com/",
574 }
575 ]
576 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=repo_list)
577
578 def get_one_result(*args, **kwargs):
579 if args[0] == "k8sclusters":
580 return {
581 "_admin": {
582 "helm_chart_repos": ["4b5550a9-990d-4d95-8a48-1f4614d6ac9c"]
583 }
584 }
585 elif args[0] == "k8srepos":
586 return {
587 "_id": "4b5550a9-990d-4d95-8a48-1f4614d6ac9c",
588 "type": "helm-chart",
589 "name": "bitnami",
590 "url": "https://charts.bitnami.com/bitnami",
591 }
592
593 self.helm_conn.db.get_one = asynctest.Mock()
594 self.helm_conn.db.get_one.side_effect = get_one_result
595
596 self.helm_conn.repo_add = asynctest.CoroutineMock()
597 self.helm_conn.repo_remove = asynctest.CoroutineMock()
598
599 deleted_repo_list, added_repo_dict = await self.helm_conn.synchronize_repos(
600 self.cluster_uuid
601 )
602 self.helm_conn.repo_remove.assert_not_called()
603 self.helm_conn.repo_add.assert_called_once_with(
604 self.cluster_uuid, "bitnami", "https://charts.bitnami.com/bitnami"
605 )
606 self.assertEqual(deleted_repo_list, [], "Deleted repo list should be empty")
607 self.assertEqual(
608 added_repo_dict,
609 {"4b5550a9-990d-4d95-8a48-1f4614d6ac9c": "bitnami"},
610 "Repos added should include only one bitnami",
611 )
612
613 @asynctest.fail_on(active_handles=True)
614 async def test_sync_repos_delete(self):
615 repo_list = [
616 {
617 "name": "stable",
618 "url": "https://kubernetes-charts.storage.googleapis.com/",
619 },
620 {"name": "bitnami", "url": "https://charts.bitnami.com/bitnami"},
621 ]
622 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=repo_list)
623
624 def get_one_result(*args, **kwargs):
625 if args[0] == "k8sclusters":
626 return {"_admin": {"helm_chart_repos": []}}
627
628 self.helm_conn.db.get_one = asynctest.Mock()
629 self.helm_conn.db.get_one.side_effect = get_one_result
630
631 self.helm_conn.repo_add = asynctest.CoroutineMock()
632 self.helm_conn.repo_remove = asynctest.CoroutineMock()
633
634 deleted_repo_list, added_repo_dict = await self.helm_conn.synchronize_repos(
635 self.cluster_uuid
636 )
637 self.helm_conn.repo_add.assert_not_called()
638 self.helm_conn.repo_remove.assert_called_once_with(self.cluster_uuid, "bitnami")
639 self.assertEqual(
640 deleted_repo_list, ["bitnami"], "Deleted repo list should be bitnami"
641 )
642 self.assertEqual(added_repo_dict, {}, "No repos should be added")