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