Update only the repo that was added instead of all repos
[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 = 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 self.cluster_id,
70 "Check cluster_uuid",
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
138 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
139
140 await self.helm_conn.repo_list(self.cluster_uuid)
141
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
145 )
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
149 )
150
151 @asynctest.fail_on(active_handles=True)
152 async def test_repo_remove(self):
153
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)
157
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
161 )
162 command = "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo remove {}".format(
163 repo_name
164 )
165 self.helm_conn._local_async_exec.assert_called_with(
166 command=command, env=self.env, raise_exception_on_error=True
167 )
168
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"
173 db_dict = {}
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
182 )
183 self.helm_conn._create_namespace = asynctest.CoroutineMock()
184
185 await self.helm_conn.install(
186 self.cluster_uuid,
187 kdu_model,
188 self.kdu_instance,
189 atomic=True,
190 namespace=self.namespace,
191 db_dict=db_dict,
192 )
193
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
198 )
199 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
200 self.helm_conn.fs.reverse_sync.assert_called_once_with(
201 from_path=self.cluster_id
202 )
203 self.helm_conn._store_status.assert_called_with(
204 cluster_id=self.cluster_id,
205 kdu_instance=kdu_instance,
206 namespace=self.namespace,
207 db_dict=db_dict,
208 operation="install",
209 run_once=True,
210 check_every=0,
211 )
212 command = (
213 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
214 "install stable-openldap-0005399828 --atomic --output yaml "
215 "--timeout 300s --namespace testk8s stable/openldap --version 1.2.2"
216 )
217 self.helm_conn._local_async_exec.assert_called_once_with(
218 command=command, env=self.env, raise_exception_on_error=False
219 )
220
221 # Exception test if namespace could not being created for some reason
222 self.helm_conn._namespace_exists.return_value = False
223 self.helm_conn._create_namespace.side_effect = Exception()
224
225 with self.assertRaises(K8sException):
226 await self.helm_conn.install(
227 self.cluster_uuid,
228 kdu_model,
229 self.kdu_instance,
230 atomic=True,
231 namespace=self.namespace,
232 db_dict=db_dict,
233 )
234
235 @asynctest.fail_on(active_handles=True)
236 async def test_namespace_exists(self):
237 self.helm_conn._get_namespaces = asynctest.CoroutineMock()
238
239 self.helm_conn._get_namespaces.return_value = ["testk8s", "kube-system"]
240 result = await self.helm_conn._namespace_exists(self.cluster_id, self.namespace)
241 self.helm_conn._get_namespaces.assert_called_once()
242 self.assertEqual(result, True)
243
244 self.helm_conn._get_namespaces.reset_mock()
245 result = await self.helm_conn._namespace_exists(
246 self.cluster_id, "none-exists-namespace"
247 )
248 self.helm_conn._get_namespaces.assert_called_once()
249 self.assertEqual(result, False)
250
251 @asynctest.fail_on(active_handles=True)
252 async def test_upgrade(self):
253 kdu_model = "stable/openldap:1.2.3"
254 kdu_instance = "stable-openldap-0005399828"
255 db_dict = {}
256 instance_info = {
257 "chart": "openldap-1.2.2",
258 "name": kdu_instance,
259 "namespace": self.namespace,
260 "revision": 1,
261 "status": "DEPLOYED",
262 }
263 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
264 self.helm_conn._store_status = asynctest.CoroutineMock()
265 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
266 return_value=instance_info
267 )
268
269 await self.helm_conn.upgrade(
270 self.cluster_uuid, kdu_instance, kdu_model, atomic=True, db_dict=db_dict
271 )
272 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
273 self.helm_conn.fs.reverse_sync.assert_called_once_with(
274 from_path=self.cluster_id
275 )
276 self.helm_conn._store_status.assert_called_with(
277 cluster_id=self.cluster_id,
278 kdu_instance=kdu_instance,
279 namespace=self.namespace,
280 db_dict=db_dict,
281 operation="upgrade",
282 run_once=True,
283 check_every=0,
284 )
285 command = (
286 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
287 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
288 "--namespace testk8s --atomic --output yaml --timeout 300s "
289 "--version 1.2.3"
290 )
291 self.helm_conn._local_async_exec.assert_called_once_with(
292 command=command, env=self.env, raise_exception_on_error=False
293 )
294
295 @asynctest.fail_on(active_handles=True)
296 async def test_scale(self):
297 kdu_model = "stable/openldap:1.2.3"
298 kdu_instance = "stable-openldap-0005399828"
299 db_dict = {}
300 instance_info = {
301 "chart": "openldap-1.2.3",
302 "name": kdu_instance,
303 "namespace": self.namespace,
304 "revision": 1,
305 "status": "DEPLOYED",
306 }
307 repo_list = [
308 {
309 "name": "stable",
310 "url": "https://kubernetes-charts.storage.googleapis.com/",
311 }
312 ]
313 kdu_values = """
314 # Default values for openldap.
315 # This is a YAML-formatted file.
316 # Declare variables to be passed into your templates.
317
318 replicaCount: 1
319 dummy-app:
320 replicas: 2
321 """
322
323 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=repo_list)
324 self.helm_conn.values_kdu = asynctest.CoroutineMock(return_value=kdu_values)
325 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
326 self.helm_conn._store_status = asynctest.CoroutineMock()
327 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
328 return_value=instance_info
329 )
330
331 # TEST-1
332 await self.helm_conn.scale(
333 kdu_instance,
334 2,
335 "",
336 kdu_model=kdu_model,
337 cluster_uuid=self.cluster_uuid,
338 atomic=True,
339 db_dict=db_dict,
340 )
341 command = (
342 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
343 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
344 "--namespace testk8s --atomic --output yaml --set replicaCount=2 --timeout 1800s "
345 "--version 1.2.3"
346 )
347 self.helm_conn._local_async_exec.assert_called_once_with(
348 command=command, env=self.env, raise_exception_on_error=False
349 )
350 # TEST-2
351 await self.helm_conn.scale(
352 kdu_instance,
353 3,
354 "dummy-app",
355 kdu_model=kdu_model,
356 cluster_uuid=self.cluster_uuid,
357 atomic=True,
358 db_dict=db_dict,
359 )
360 command = (
361 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
362 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
363 "--namespace testk8s --atomic --output yaml --set dummy-app.replicas=3 --timeout 1800s "
364 "--version 1.2.3"
365 )
366 self.helm_conn._local_async_exec.assert_called_with(
367 command=command, env=self.env, raise_exception_on_error=False
368 )
369 self.helm_conn.fs.reverse_sync.assert_called_with(from_path=self.cluster_id)
370 self.helm_conn._store_status.assert_called_with(
371 cluster_id=self.cluster_id,
372 kdu_instance=kdu_instance,
373 namespace=self.namespace,
374 db_dict=db_dict,
375 operation="scale",
376 run_once=True,
377 check_every=0,
378 )
379
380 @asynctest.fail_on(active_handles=True)
381 async def test_rollback(self):
382 kdu_instance = "stable-openldap-0005399828"
383 db_dict = {}
384 instance_info = {
385 "chart": "openldap-1.2.3",
386 "name": kdu_instance,
387 "namespace": self.namespace,
388 "revision": 2,
389 "status": "DEPLOYED",
390 }
391 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
392 self.helm_conn._store_status = asynctest.CoroutineMock()
393 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
394 return_value=instance_info
395 )
396
397 await self.helm_conn.rollback(
398 self.cluster_uuid, kdu_instance=kdu_instance, revision=1, db_dict=db_dict
399 )
400 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
401 self.helm_conn.fs.reverse_sync.assert_called_once_with(
402 from_path=self.cluster_id
403 )
404 self.helm_conn._store_status.assert_called_with(
405 cluster_id=self.cluster_id,
406 kdu_instance=kdu_instance,
407 namespace=self.namespace,
408 db_dict=db_dict,
409 operation="rollback",
410 run_once=True,
411 check_every=0,
412 )
413 command = (
414 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
415 "rollback stable-openldap-0005399828 1 --namespace=testk8s --wait"
416 )
417 self.helm_conn._local_async_exec.assert_called_once_with(
418 command=command, env=self.env, raise_exception_on_error=False
419 )
420
421 @asynctest.fail_on(active_handles=True)
422 async def test_uninstall(self):
423 kdu_instance = "stable-openldap-0005399828"
424 instance_info = {
425 "chart": "openldap-1.2.2",
426 "name": kdu_instance,
427 "namespace": self.namespace,
428 "revision": 3,
429 "status": "DEPLOYED",
430 }
431 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
432 self.helm_conn._store_status = asynctest.CoroutineMock()
433 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
434 return_value=instance_info
435 )
436
437 await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
438 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
439 self.helm_conn.fs.reverse_sync.assert_called_once_with(
440 from_path=self.cluster_id
441 )
442 command = (
443 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 uninstall {} --namespace={}"
444 ).format(kdu_instance, self.namespace)
445 self.helm_conn._local_async_exec.assert_called_once_with(
446 command=command, env=self.env, raise_exception_on_error=True
447 )
448
449 @asynctest.fail_on(active_handles=True)
450 async def test_get_services(self):
451 kdu_instance = "test_services_1"
452 service = {"name": "testservice", "type": "LoadBalancer"}
453 self.helm_conn._local_async_exec_pipe = asynctest.CoroutineMock(
454 return_value=("", 0)
455 )
456 self.helm_conn._parse_services = Mock(return_value=["testservice"])
457 self.helm_conn._get_service = asynctest.CoroutineMock(return_value=service)
458
459 services = await self.helm_conn.get_services(
460 self.cluster_uuid, kdu_instance, self.namespace
461 )
462 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
463 self.helm_conn.fs.reverse_sync.assert_called_once_with(
464 from_path=self.cluster_id
465 )
466 self.helm_conn._parse_services.assert_called_once()
467 command1 = (
468 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 get manifest {} --namespace=testk8s"
469 ).format(kdu_instance)
470 command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
471 self.helm_conn._local_async_exec_pipe.assert_called_once_with(
472 command1, command2, env=self.env, raise_exception_on_error=True
473 )
474 self.assertEqual(
475 services, [service], "Invalid service returned from get_service"
476 )
477
478 @asynctest.fail_on(active_handles=True)
479 async def test_get_service(self):
480 service_name = "service1"
481
482 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
483 await self.helm_conn.get_service(
484 self.cluster_uuid, service_name, self.namespace
485 )
486
487 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
488 self.helm_conn.fs.reverse_sync.assert_called_once_with(
489 from_path=self.cluster_id
490 )
491 command = (
492 "/usr/bin/kubectl --kubeconfig=./tmp/helm3_cluster_id/.kube/config "
493 "--namespace=testk8s get service service1 -o=yaml"
494 )
495 self.helm_conn._local_async_exec.assert_called_once_with(
496 command=command, env=self.env, raise_exception_on_error=True
497 )
498
499 @asynctest.fail_on(active_handles=True)
500 async def test_inspect_kdu(self):
501 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
502
503 kdu_model = "stable/openldap:1.2.4"
504 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
505 await self.helm_conn.inspect_kdu(kdu_model, repo_url)
506
507 command = (
508 "/usr/bin/helm3 show all openldap --repo "
509 "https://kubernetes-charts.storage.googleapis.com/ "
510 "--version 1.2.4"
511 )
512 self.helm_conn._local_async_exec.assert_called_with(command=command)
513
514 @asynctest.fail_on(active_handles=True)
515 async def test_help_kdu(self):
516 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
517
518 kdu_model = "stable/openldap:1.2.4"
519 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
520 await self.helm_conn.help_kdu(kdu_model, repo_url)
521
522 command = (
523 "/usr/bin/helm3 show readme openldap --repo "
524 "https://kubernetes-charts.storage.googleapis.com/ "
525 "--version 1.2.4"
526 )
527 self.helm_conn._local_async_exec.assert_called_with(command=command)
528
529 @asynctest.fail_on(active_handles=True)
530 async def test_values_kdu(self):
531 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
532
533 kdu_model = "stable/openldap:1.2.4"
534 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
535 await self.helm_conn.values_kdu(kdu_model, repo_url)
536
537 command = (
538 "/usr/bin/helm3 show values openldap --repo "
539 "https://kubernetes-charts.storage.googleapis.com/ "
540 "--version 1.2.4"
541 )
542 self.helm_conn._local_async_exec.assert_called_with(command=command)
543
544 @asynctest.fail_on(active_handles=True)
545 async def test_get_values_kdu(self):
546 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
547
548 kdu_instance = "stable-openldap-0005399828"
549 await self.helm_conn.get_values_kdu(
550 kdu_instance, self.namespace, self.env["KUBECONFIG"]
551 )
552
553 command = (
554 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 get values "
555 "stable-openldap-0005399828 --namespace=testk8s --output yaml"
556 )
557 self.helm_conn._local_async_exec.assert_called_with(command=command)
558
559 @asynctest.fail_on(active_handles=True)
560 async def test_instances_list(self):
561 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
562
563 await self.helm_conn.instances_list(self.cluster_uuid)
564 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
565 self.helm_conn.fs.reverse_sync.assert_called_once_with(
566 from_path=self.cluster_id
567 )
568 command = "/usr/bin/helm3 list --all-namespaces --output yaml"
569 self.helm_conn._local_async_exec.assert_called_once_with(
570 command=command, env=self.env, raise_exception_on_error=True
571 )
572
573 @asynctest.fail_on(active_handles=True)
574 async def test_status_kdu(self):
575 kdu_instance = "stable-openldap-0005399828"
576 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
577
578 await self.helm_conn._status_kdu(
579 self.cluster_id, kdu_instance, self.namespace, yaml_format=True
580 )
581 command = (
582 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 status {} --namespace={} --output yaml"
583 ).format(kdu_instance, self.namespace)
584 self.helm_conn._local_async_exec.assert_called_once_with(
585 command=command,
586 env=self.env,
587 raise_exception_on_error=True,
588 show_error_log=False,
589 )
590
591 @asynctest.fail_on(active_handles=True)
592 async def test_store_status(self):
593 kdu_instance = "stable-openldap-0005399828"
594 db_dict = {}
595 status = {
596 "info": {
597 "description": "Install complete",
598 "status": {
599 "code": "1",
600 "notes": "The openldap helm chart has been installed",
601 },
602 }
603 }
604 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=status)
605 self.helm_conn.write_app_status_to_db = asynctest.CoroutineMock(
606 return_value=status
607 )
608
609 await self.helm_conn._store_status(
610 cluster_id=self.cluster_id,
611 kdu_instance=kdu_instance,
612 namespace=self.namespace,
613 db_dict=db_dict,
614 operation="install",
615 run_once=True,
616 check_every=0,
617 )
618 self.helm_conn._status_kdu.assert_called_once_with(
619 cluster_id=self.cluster_id,
620 kdu_instance=kdu_instance,
621 namespace=self.namespace,
622 yaml_format=False,
623 )
624 self.helm_conn.write_app_status_to_db.assert_called_once_with(
625 db_dict=db_dict,
626 status="Install complete",
627 detailed_status=str(status),
628 operation="install",
629 )
630
631 @asynctest.fail_on(active_handles=True)
632 async def test_reset_uninstall_false(self):
633 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
634
635 await self.helm_conn.reset(self.cluster_uuid, force=False, uninstall_sw=False)
636 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
637 self.helm_conn.fs.file_delete.assert_called_once_with(
638 self.cluster_id, ignore_non_exist=True
639 )
640 self.helm_conn._uninstall_sw.assert_not_called()
641
642 @asynctest.fail_on(active_handles=True)
643 async def test_reset_uninstall(self):
644 kdu_instance = "stable-openldap-0021099429"
645 instances = [
646 {
647 "app_version": "2.4.48",
648 "chart": "openldap-1.2.3",
649 "name": kdu_instance,
650 "namespace": self.namespace,
651 "revision": "1",
652 "status": "deployed",
653 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
654 }
655 ]
656 self.helm_conn._get_namespace = Mock(return_value=self.namespace)
657 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
658 self.helm_conn.instances_list = asynctest.CoroutineMock(return_value=instances)
659 self.helm_conn.uninstall = asynctest.CoroutineMock()
660
661 await self.helm_conn.reset(self.cluster_uuid, force=True, uninstall_sw=True)
662 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
663 self.helm_conn.fs.file_delete.assert_called_once_with(
664 self.cluster_id, ignore_non_exist=True
665 )
666 self.helm_conn._get_namespace.assert_called_once_with(
667 cluster_uuid=self.cluster_uuid
668 )
669 self.helm_conn.instances_list.assert_called_once_with(
670 cluster_uuid=self.cluster_uuid
671 )
672 self.helm_conn.uninstall.assert_called_once_with(
673 cluster_uuid=self.cluster_uuid, kdu_instance=kdu_instance
674 )
675 self.helm_conn._uninstall_sw.assert_called_once_with(
676 cluster_id=self.cluster_id, namespace=self.namespace
677 )
678
679 @asynctest.fail_on(active_handles=True)
680 async def test_sync_repos_add(self):
681 repo_list = [
682 {
683 "name": "stable",
684 "url": "https://kubernetes-charts.storage.googleapis.com/",
685 }
686 ]
687 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=repo_list)
688
689 def get_one_result(*args, **kwargs):
690 if args[0] == "k8sclusters":
691 return {
692 "_admin": {
693 "helm_chart_repos": ["4b5550a9-990d-4d95-8a48-1f4614d6ac9c"]
694 }
695 }
696 elif args[0] == "k8srepos":
697 return {
698 "_id": "4b5550a9-990d-4d95-8a48-1f4614d6ac9c",
699 "type": "helm-chart",
700 "name": "bitnami",
701 "url": "https://charts.bitnami.com/bitnami",
702 }
703
704 self.helm_conn.db.get_one = asynctest.Mock()
705 self.helm_conn.db.get_one.side_effect = get_one_result
706
707 self.helm_conn.repo_add = asynctest.CoroutineMock()
708 self.helm_conn.repo_remove = asynctest.CoroutineMock()
709
710 deleted_repo_list, added_repo_dict = await self.helm_conn.synchronize_repos(
711 self.cluster_uuid
712 )
713 self.helm_conn.repo_remove.assert_not_called()
714 self.helm_conn.repo_add.assert_called_once_with(
715 self.cluster_uuid, "bitnami", "https://charts.bitnami.com/bitnami"
716 )
717 self.assertEqual(deleted_repo_list, [], "Deleted repo list should be empty")
718 self.assertEqual(
719 added_repo_dict,
720 {"4b5550a9-990d-4d95-8a48-1f4614d6ac9c": "bitnami"},
721 "Repos added should include only one bitnami",
722 )
723
724 @asynctest.fail_on(active_handles=True)
725 async def test_sync_repos_delete(self):
726 repo_list = [
727 {
728 "name": "stable",
729 "url": "https://kubernetes-charts.storage.googleapis.com/",
730 },
731 {"name": "bitnami", "url": "https://charts.bitnami.com/bitnami"},
732 ]
733 self.helm_conn.repo_list = asynctest.CoroutineMock(return_value=repo_list)
734
735 def get_one_result(*args, **kwargs):
736 if args[0] == "k8sclusters":
737 return {"_admin": {"helm_chart_repos": []}}
738
739 self.helm_conn.db.get_one = asynctest.Mock()
740 self.helm_conn.db.get_one.side_effect = get_one_result
741
742 self.helm_conn.repo_add = asynctest.CoroutineMock()
743 self.helm_conn.repo_remove = asynctest.CoroutineMock()
744
745 deleted_repo_list, added_repo_dict = await self.helm_conn.synchronize_repos(
746 self.cluster_uuid
747 )
748 self.helm_conn.repo_add.assert_not_called()
749 self.helm_conn.repo_remove.assert_called_once_with(self.cluster_uuid, "bitnami")
750 self.assertEqual(
751 deleted_repo_list, ["bitnami"], "Deleted repo list should be bitnami"
752 )
753 self.assertEqual(added_repo_dict, {}, "No repos should be added")