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