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
= (
106 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo update {}"
109 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo add {} {}"
110 ).format(repo_name
, repo_url
)
111 calls
= self
.helm_conn
._local
_async
_exec
.call_args_list
112 call0_kargs
= calls
[0][1]
114 call0_kargs
.get("command"),
116 "Invalid repo add command: {}".format(call0_kargs
.get("command")),
119 call0_kargs
.get("env"),
121 "Invalid env for add command: {}".format(call0_kargs
.get("env")),
123 call1_kargs
= calls
[1][1]
125 call1_kargs
.get("command"),
127 "Invalid repo update command: {}".format(call1_kargs
.get("command")),
130 call1_kargs
.get("env"),
132 "Invalid env for update command: {}".format(call1_kargs
.get("env")),
135 @asynctest.fail_on(active_handles
=True)
136 async def test_repo_list(self
):
137 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
139 await self
.helm_conn
.repo_list(self
.cluster_uuid
)
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
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
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
)
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_has_calls(
199 asynctest
.call(from_path
=self
.cluster_id
),
200 asynctest
.call(from_path
=self
.cluster_id
),
203 self
.helm_conn
.fs
.reverse_sync
.assert_has_calls(
205 asynctest
.call(from_path
=self
.cluster_id
),
206 asynctest
.call(from_path
=self
.cluster_id
),
209 self
.helm_conn
._store
_status
.assert_called_with(
210 cluster_id
=self
.cluster_id
,
211 kdu_instance
=kdu_instance
,
212 namespace
=self
.namespace
,
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"
221 self
.helm_conn
._local
_async
_exec
.assert_called_with(
222 command
=command
, env
=self
.env
, raise_exception_on_error
=False
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()
229 with self
.assertRaises(K8sException
):
230 await self
.helm_conn
.install(
235 namespace
=self
.namespace
,
239 @asynctest.fail_on(active_handles
=True)
240 async def test_namespace_exists(self
):
241 self
.helm_conn
._get
_namespaces
= asynctest
.CoroutineMock()
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)
248 self
.helm_conn
._get
_namespaces
.reset_mock()
249 result
= await self
.helm_conn
._namespace
_exists
(
250 self
.cluster_id
, "none-exists-namespace"
252 self
.helm_conn
._get
_namespaces
.assert_called_once()
253 self
.assertEqual(result
, False)
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"
261 "chart": "openldap-1.2.2",
262 "name": kdu_instance
,
263 "namespace": self
.namespace
,
265 "status": "DEPLOYED",
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
273 await self
.helm_conn
.upgrade(
274 self
.cluster_uuid
, kdu_instance
, kdu_model
, atomic
=True, db_dict
=db_dict
276 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
277 self
.helm_conn
.fs
.reverse_sync
.assert_has_calls(
279 asynctest
.call(from_path
=self
.cluster_id
),
280 asynctest
.call(from_path
=self
.cluster_id
),
283 self
.helm_conn
._store
_status
.assert_called_with(
284 cluster_id
=self
.cluster_id
,
285 kdu_instance
=kdu_instance
,
286 namespace
=self
.namespace
,
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 "--reuse-values --version 1.2.3"
296 self
.helm_conn
._local
_async
_exec
.assert_called_with(
297 command
=command
, env
=self
.env
, raise_exception_on_error
=False
300 @asynctest.fail_on(active_handles
=True)
301 async def test_scale(self
):
302 kdu_model
= "stable/openldap:1.2.3"
303 kdu_instance
= "stable-openldap-0005399828"
306 "chart": "openldap-1.2.3",
307 "name": kdu_instance
,
308 "namespace": self
.namespace
,
310 "status": "DEPLOYED",
315 "url": "https://kubernetes-charts.storage.googleapis.com/",
319 # Default values for openldap.
320 # This is a YAML-formatted file.
321 # Declare variables to be passed into your templates.
328 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
329 self
.helm_conn
.values_kdu
= asynctest
.CoroutineMock(return_value
=kdu_values
)
330 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
331 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
332 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
333 return_value
=instance_info
337 await self
.helm_conn
.scale(
342 cluster_uuid
=self
.cluster_uuid
,
347 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
348 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
349 "--namespace testk8s --atomic --output yaml --set replicaCount=2 --timeout 1800s "
350 "--reuse-values --version 1.2.3"
352 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
353 command
=command
, env
=self
.env
, raise_exception_on_error
=False
356 await self
.helm_conn
.scale(
361 cluster_uuid
=self
.cluster_uuid
,
366 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
367 "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
368 "--namespace testk8s --atomic --output yaml --set dummy-app.replicas=3 --timeout 1800s "
369 "--reuse-values --version 1.2.3"
371 self
.helm_conn
._local
_async
_exec
.assert_called_with(
372 command
=command
, env
=self
.env
, raise_exception_on_error
=False
374 self
.helm_conn
.fs
.reverse_sync
.assert_called_with(from_path
=self
.cluster_id
)
375 self
.helm_conn
._store
_status
.assert_called_with(
376 cluster_id
=self
.cluster_id
,
377 kdu_instance
=kdu_instance
,
378 namespace
=self
.namespace
,
383 @asynctest.fail_on(active_handles
=True)
384 async def test_rollback(self
):
385 kdu_instance
= "stable-openldap-0005399828"
388 "chart": "openldap-1.2.3",
389 "name": kdu_instance
,
390 "namespace": self
.namespace
,
392 "status": "DEPLOYED",
394 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
395 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
396 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
397 return_value
=instance_info
400 await self
.helm_conn
.rollback(
401 self
.cluster_uuid
, kdu_instance
=kdu_instance
, revision
=1, db_dict
=db_dict
403 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
404 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
405 from_path
=self
.cluster_id
407 self
.helm_conn
._store
_status
.assert_called_with(
408 cluster_id
=self
.cluster_id
,
409 kdu_instance
=kdu_instance
,
410 namespace
=self
.namespace
,
412 operation
="rollback",
415 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
416 "rollback stable-openldap-0005399828 1 --namespace=testk8s --wait"
418 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
419 command
=command
, env
=self
.env
, raise_exception_on_error
=False
422 @asynctest.fail_on(active_handles
=True)
423 async def test_uninstall(self
):
424 kdu_instance
= "stable-openldap-0005399828"
426 "chart": "openldap-1.2.2",
427 "name": kdu_instance
,
428 "namespace": self
.namespace
,
430 "status": "DEPLOYED",
432 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
433 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
434 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(
435 return_value
=instance_info
438 await self
.helm_conn
.uninstall(self
.cluster_uuid
, kdu_instance
)
439 self
.helm_conn
.fs
.sync
.assert_called_with(from_path
=self
.cluster_id
)
440 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
441 from_path
=self
.cluster_id
444 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 uninstall {} --namespace={}"
445 ).format(kdu_instance
, self
.namespace
)
446 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
447 command
=command
, env
=self
.env
, raise_exception_on_error
=True
450 @asynctest.fail_on(active_handles
=True)
451 async def test_get_services(self
):
452 kdu_instance
= "test_services_1"
453 service
= {"name": "testservice", "type": "LoadBalancer"}
454 self
.helm_conn
._local
_async
_exec
_pipe
= asynctest
.CoroutineMock(
457 self
.helm_conn
._parse
_services
= Mock(return_value
=["testservice"])
458 self
.helm_conn
._get
_service
= asynctest
.CoroutineMock(return_value
=service
)
460 services
= await self
.helm_conn
.get_services(
461 self
.cluster_uuid
, kdu_instance
, self
.namespace
463 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
464 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
465 from_path
=self
.cluster_id
467 self
.helm_conn
._parse
_services
.assert_called_once()
469 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 get manifest {} --namespace=testk8s"
470 ).format(kdu_instance
)
471 command2
= "/usr/bin/kubectl get --namespace={} -f -".format(self
.namespace
)
472 self
.helm_conn
._local
_async
_exec
_pipe
.assert_called_once_with(
473 command1
, command2
, env
=self
.env
, raise_exception_on_error
=True
476 services
, [service
], "Invalid service returned from get_service"
479 @asynctest.fail_on(active_handles
=True)
480 async def test_get_service(self
):
481 service_name
= "service1"
483 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
484 await self
.helm_conn
.get_service(
485 self
.cluster_uuid
, service_name
, self
.namespace
488 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
489 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
490 from_path
=self
.cluster_id
493 "/usr/bin/kubectl --kubeconfig=./tmp/helm3_cluster_id/.kube/config "
494 "--namespace=testk8s get service service1 -o=yaml"
496 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
497 command
=command
, env
=self
.env
, raise_exception_on_error
=True
500 @asynctest.fail_on(active_handles
=True)
501 async def test_inspect_kdu(self
):
502 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
504 kdu_model
= "stable/openldap:1.2.4"
505 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
506 await self
.helm_conn
.inspect_kdu(kdu_model
, repo_url
)
509 "/usr/bin/helm3 show all openldap --repo "
510 "https://kubernetes-charts.storage.googleapis.com/ "
513 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
515 @asynctest.fail_on(active_handles
=True)
516 async def test_help_kdu(self
):
517 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
519 kdu_model
= "stable/openldap:1.2.4"
520 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
521 await self
.helm_conn
.help_kdu(kdu_model
, repo_url
)
524 "/usr/bin/helm3 show readme openldap --repo "
525 "https://kubernetes-charts.storage.googleapis.com/ "
528 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
530 @asynctest.fail_on(active_handles
=True)
531 async def test_values_kdu(self
):
532 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
534 kdu_model
= "stable/openldap:1.2.4"
535 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
536 await self
.helm_conn
.values_kdu(kdu_model
, repo_url
)
539 "/usr/bin/helm3 show values openldap --repo "
540 "https://kubernetes-charts.storage.googleapis.com/ "
543 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
545 @asynctest.fail_on(active_handles
=True)
546 async def test_get_values_kdu(self
):
547 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
549 kdu_instance
= "stable-openldap-0005399828"
550 await self
.helm_conn
.get_values_kdu(
551 kdu_instance
, self
.namespace
, self
.env
["KUBECONFIG"]
555 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 get values "
556 "stable-openldap-0005399828 --namespace=testk8s --output yaml"
558 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
)
560 @asynctest.fail_on(active_handles
=True)
561 async def test_instances_list(self
):
562 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
564 await self
.helm_conn
.instances_list(self
.cluster_uuid
)
565 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
566 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(
567 from_path
=self
.cluster_id
569 command
= "/usr/bin/helm3 list --all-namespaces --output yaml"
570 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
571 command
=command
, env
=self
.env
, raise_exception_on_error
=True
574 @asynctest.fail_on(active_handles
=True)
575 async def test_status_kdu(self
):
576 kdu_instance
= "stable-openldap-0005399828"
577 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
579 await self
.helm_conn
._status
_kdu
(
580 self
.cluster_id
, kdu_instance
, self
.namespace
, yaml_format
=True
583 "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 status {} --namespace={} --output yaml"
584 ).format(kdu_instance
, self
.namespace
)
585 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(
588 raise_exception_on_error
=True,
589 show_error_log
=False,
592 @asynctest.fail_on(active_handles
=True)
593 async def test_store_status(self
):
594 kdu_instance
= "stable-openldap-0005399828"
598 "description": "Install complete",
601 "notes": "The openldap helm chart has been installed",
605 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=status
)
606 self
.helm_conn
.write_app_status_to_db
= asynctest
.CoroutineMock(
610 await self
.helm_conn
._store
_status
(
611 cluster_id
=self
.cluster_id
,
612 kdu_instance
=kdu_instance
,
613 namespace
=self
.namespace
,
617 self
.helm_conn
._status
_kdu
.assert_called_once_with(
618 cluster_id
=self
.cluster_id
,
619 kdu_instance
=kdu_instance
,
620 namespace
=self
.namespace
,
623 self
.helm_conn
.write_app_status_to_db
.assert_called_once_with(
625 status
="Install complete",
626 detailed_status
=str(status
),
630 @asynctest.fail_on(active_handles
=True)
631 async def test_reset_uninstall_false(self
):
632 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
634 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=False, uninstall_sw
=False)
635 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
636 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
637 self
.cluster_id
, ignore_non_exist
=True
639 self
.helm_conn
._uninstall
_sw
.assert_not_called()
641 @asynctest.fail_on(active_handles
=True)
642 async def test_reset_uninstall(self
):
643 kdu_instance
= "stable-openldap-0021099429"
646 "app_version": "2.4.48",
647 "chart": "openldap-1.2.3",
648 "name": kdu_instance
,
649 "namespace": self
.namespace
,
651 "status": "deployed",
652 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
655 self
.helm_conn
._get
_namespace
= Mock(return_value
=self
.namespace
)
656 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
657 self
.helm_conn
.instances_list
= asynctest
.CoroutineMock(return_value
=instances
)
658 self
.helm_conn
.uninstall
= asynctest
.CoroutineMock()
660 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=True, uninstall_sw
=True)
661 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
662 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(
663 self
.cluster_id
, ignore_non_exist
=True
665 self
.helm_conn
._get
_namespace
.assert_called_once_with(
666 cluster_uuid
=self
.cluster_uuid
668 self
.helm_conn
.instances_list
.assert_called_once_with(
669 cluster_uuid
=self
.cluster_uuid
671 self
.helm_conn
.uninstall
.assert_called_once_with(
672 cluster_uuid
=self
.cluster_uuid
, kdu_instance
=kdu_instance
674 self
.helm_conn
._uninstall
_sw
.assert_called_once_with(
675 cluster_id
=self
.cluster_id
, namespace
=self
.namespace
678 @asynctest.fail_on(active_handles
=True)
679 async def test_sync_repos_add(self
):
683 "url": "https://kubernetes-charts.storage.googleapis.com/",
686 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
688 def get_one_result(*args
, **kwargs
):
689 if args
[0] == "k8sclusters":
692 "helm_chart_repos": ["4b5550a9-990d-4d95-8a48-1f4614d6ac9c"]
695 elif args
[0] == "k8srepos":
697 "_id": "4b5550a9-990d-4d95-8a48-1f4614d6ac9c",
698 "type": "helm-chart",
700 "url": "https://charts.bitnami.com/bitnami",
703 self
.helm_conn
.db
.get_one
= asynctest
.Mock()
704 self
.helm_conn
.db
.get_one
.side_effect
= get_one_result
706 self
.helm_conn
.repo_add
= asynctest
.CoroutineMock()
707 self
.helm_conn
.repo_remove
= asynctest
.CoroutineMock()
709 deleted_repo_list
, added_repo_dict
= await self
.helm_conn
.synchronize_repos(
712 self
.helm_conn
.repo_remove
.assert_not_called()
713 self
.helm_conn
.repo_add
.assert_called_once_with(
714 self
.cluster_uuid
, "bitnami", "https://charts.bitnami.com/bitnami"
716 self
.assertEqual(deleted_repo_list
, [], "Deleted repo list should be empty")
719 {"4b5550a9-990d-4d95-8a48-1f4614d6ac9c": "bitnami"},
720 "Repos added should include only one bitnami",
723 @asynctest.fail_on(active_handles
=True)
724 async def test_sync_repos_delete(self
):
728 "url": "https://kubernetes-charts.storage.googleapis.com/",
730 {"name": "bitnami", "url": "https://charts.bitnami.com/bitnami"},
732 self
.helm_conn
.repo_list
= asynctest
.CoroutineMock(return_value
=repo_list
)
734 def get_one_result(*args
, **kwargs
):
735 if args
[0] == "k8sclusters":
736 return {"_admin": {"helm_chart_repos": []}}
738 self
.helm_conn
.db
.get_one
= asynctest
.Mock()
739 self
.helm_conn
.db
.get_one
.side_effect
= get_one_result
741 self
.helm_conn
.repo_add
= asynctest
.CoroutineMock()
742 self
.helm_conn
.repo_remove
= asynctest
.CoroutineMock()
744 deleted_repo_list
, added_repo_dict
= await self
.helm_conn
.synchronize_repos(
747 self
.helm_conn
.repo_add
.assert_not_called()
748 self
.helm_conn
.repo_remove
.assert_called_once_with(self
.cluster_uuid
, "bitnami")
750 deleted_repo_list
, ["bitnami"], "Deleted repo list should be bitnami"
752 self
.assertEqual(added_repo_dict
, {}, "No repos should be added")