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