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