8a0c1eaf3f711f5b0bbb1e5df9503eb42985fce9
[osm/N2VC.git] / n2vc / tests / unit / test_k8s_helm_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
22 from osm_common.dbmemory import DbMemory
23 from osm_common.fslocal import FsLocal
24 from n2vc.k8s_helm_conn import K8sHelmConnector
25
26 __author__ = "Isabel Lloret <illoret@indra.es>"
27
28
29 class TestK8sHelmConn(asynctest.TestCase):
30 logging.basicConfig(level=logging.DEBUG)
31 logger = logging.getLogger(__name__)
32 logger.setLevel(logging.DEBUG)
33
34 async def setUp(self):
35 self.db = Mock(DbMemory())
36 self.fs = asynctest.Mock(FsLocal())
37 self.fs.path = "./tmp/"
38 self.namespace = "testk8s"
39 self.service_account = "osm"
40 self.cluster_id = "helm_cluster_id"
41 self.cluster_uuid = "{}:{}".format(self.namespace, self.cluster_id)
42 # pass fake kubectl and helm commands to make sure it does not call actual commands
43 K8sHelmConnector._check_file_exists = asynctest.Mock(return_value=True)
44 K8sHelmConnector._local_async_exec = asynctest.CoroutineMock(
45 return_value=(0, "")
46 )
47 cluster_dir = self.fs.path + self.cluster_id
48 self.kube_config = self.fs.path + self.cluster_id + "/.kube/config"
49 self.helm_home = self.fs.path + self.cluster_id + "/.helm"
50 self.env = {
51 "HELM_HOME": "{}/.helm".format(cluster_dir),
52 "KUBECONFIG": "{}/.kube/config".format(cluster_dir),
53 }
54 self.helm_conn = K8sHelmConnector(self.fs, self.db, log=self.logger)
55 self.logger.debug("Set up executed")
56
57 @asynctest.fail_on(active_handles=True)
58 async def test_init_env(self):
59 # TODO
60 pass
61
62 @asynctest.fail_on(active_handles=True)
63 async def test_repo_add(self):
64 repo_name = "bitnami"
65 repo_url = "https://charts.bitnami.com/bitnami"
66 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
67
68 await self.helm_conn.repo_add(self.cluster_uuid, repo_name, repo_url)
69
70 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
71 self.helm_conn.fs.reverse_sync.assert_called_once_with(
72 from_path=self.cluster_id
73 )
74 self.assertEqual(
75 self.helm_conn._local_async_exec.call_count,
76 2,
77 "local_async_exec expected 2 calls, called {}".format(
78 self.helm_conn._local_async_exec.call_count
79 ),
80 )
81
82 repo_update_command = (
83 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo update {}"
84 ).format(repo_name)
85 repo_add_command = (
86 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo add {} {}"
87 ).format(repo_name, repo_url)
88 calls = self.helm_conn._local_async_exec.call_args_list
89 call0_kargs = calls[0][1]
90 self.assertEqual(
91 call0_kargs.get("command"),
92 repo_add_command,
93 "Invalid repo add command: {}".format(call0_kargs.get("command")),
94 )
95 self.assertEqual(
96 call0_kargs.get("env"),
97 self.env,
98 "Invalid env for add command: {}".format(call0_kargs.get("env")),
99 )
100 call1_kargs = calls[1][1]
101 self.assertEqual(
102 call1_kargs.get("command"),
103 repo_update_command,
104 "Invalid repo update command: {}".format(call1_kargs.get("command")),
105 )
106 self.assertEqual(
107 call1_kargs.get("env"),
108 self.env,
109 "Invalid env for update command: {}".format(call1_kargs.get("env")),
110 )
111
112 @asynctest.fail_on(active_handles=True)
113 async def test_repo_list(self):
114 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
115
116 await self.helm_conn.repo_list(self.cluster_uuid)
117
118 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
119 self.helm_conn.fs.reverse_sync.assert_called_once_with(
120 from_path=self.cluster_id
121 )
122 command = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo list --output yaml"
123 self.helm_conn._local_async_exec.assert_called_with(
124 command=command, env=self.env, raise_exception_on_error=False
125 )
126
127 @asynctest.fail_on(active_handles=True)
128 async def test_repo_remove(self):
129 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
130 repo_name = "bitnami"
131 await self.helm_conn.repo_remove(self.cluster_uuid, repo_name)
132
133 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
134 self.helm_conn.fs.reverse_sync.assert_called_once_with(
135 from_path=self.cluster_id
136 )
137 command = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm repo remove {}".format(
138 repo_name
139 )
140 self.helm_conn._local_async_exec.assert_called_once_with(
141 command=command, env=self.env, raise_exception_on_error=True
142 )
143
144 @asynctest.fail_on(active_handles=True)
145 async def test_install(self):
146 kdu_model = "stable/openldap:1.2.2"
147 kdu_instance = "stable-openldap-0005399828"
148 db_dict = {}
149 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
150 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=None)
151 self.helm_conn._store_status = asynctest.CoroutineMock()
152 self.helm_conn.generate_kdu_instance_name = Mock(return_value=kdu_instance)
153
154 await self.helm_conn.install(
155 self.cluster_uuid,
156 kdu_model,
157 kdu_instance,
158 atomic=True,
159 namespace=self.namespace,
160 db_dict=db_dict,
161 )
162
163 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
164 self.helm_conn.fs.reverse_sync.assert_called_once_with(
165 from_path=self.cluster_id
166 )
167 self.helm_conn._store_status.assert_called_with(
168 cluster_id=self.cluster_id,
169 kdu_instance=kdu_instance,
170 namespace=self.namespace,
171 db_dict=db_dict,
172 operation="install",
173 )
174 command = (
175 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm install "
176 "--atomic --output yaml --timeout 300 "
177 "--name=stable-openldap-0005399828 --namespace testk8s stable/openldap "
178 "--version 1.2.2"
179 )
180 self.helm_conn._local_async_exec.assert_called_once_with(
181 command=command, env=self.env, raise_exception_on_error=False
182 )
183
184 @asynctest.fail_on(active_handles=True)
185 async def test_upgrade(self):
186 kdu_model = "stable/openldap:1.2.3"
187 kdu_instance = "stable-openldap-0005399828"
188 db_dict = {}
189 instance_info = {
190 "chart": "openldap-1.2.2",
191 "name": kdu_instance,
192 "namespace": self.namespace,
193 "revision": 1,
194 "status": "DEPLOYED",
195 }
196 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
197 self.helm_conn._store_status = asynctest.CoroutineMock()
198 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
199 return_value=instance_info
200 )
201
202 await self.helm_conn.upgrade(
203 self.cluster_uuid, kdu_instance, kdu_model, atomic=True, db_dict=db_dict
204 )
205 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
206 self.helm_conn.fs.reverse_sync.assert_called_once_with(
207 from_path=self.cluster_id
208 )
209 self.helm_conn._store_status.assert_called_with(
210 cluster_id=self.cluster_id,
211 kdu_instance=kdu_instance,
212 namespace=self.namespace,
213 db_dict=db_dict,
214 operation="upgrade",
215 )
216 command = (
217 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm upgrade "
218 "--atomic --output yaml --timeout 300 stable-openldap-0005399828 stable/openldap --version 1.2.3"
219 )
220 self.helm_conn._local_async_exec.assert_called_once_with(
221 command=command, env=self.env, raise_exception_on_error=False
222 )
223
224 @asynctest.fail_on(active_handles=True)
225 async def test_rollback(self):
226 kdu_instance = "stable-openldap-0005399828"
227 db_dict = {}
228 instance_info = {
229 "chart": "openldap-1.2.3",
230 "name": kdu_instance,
231 "namespace": self.namespace,
232 "revision": 2,
233 "status": "DEPLOYED",
234 }
235 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
236 self.helm_conn._store_status = asynctest.CoroutineMock()
237 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
238 return_value=instance_info
239 )
240
241 await self.helm_conn.rollback(
242 self.cluster_uuid, kdu_instance=kdu_instance, revision=1, db_dict=db_dict
243 )
244 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
245 self.helm_conn.fs.reverse_sync.assert_called_once_with(
246 from_path=self.cluster_id
247 )
248 self.helm_conn._store_status.assert_called_with(
249 cluster_id=self.cluster_id,
250 kdu_instance=kdu_instance,
251 namespace=self.namespace,
252 db_dict=db_dict,
253 operation="rollback",
254 )
255 command = (
256 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config "
257 "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
258 )
259 self.helm_conn._local_async_exec.assert_called_once_with(
260 command=command, env=self.env, raise_exception_on_error=False
261 )
262
263 @asynctest.fail_on(active_handles=True)
264 async def test_uninstall(self):
265 kdu_instance = "stable-openldap-0005399828"
266 instance_info = {
267 "chart": "openldap-1.2.2",
268 "name": kdu_instance,
269 "namespace": self.namespace,
270 "revision": 3,
271 "status": "DEPLOYED",
272 }
273 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
274 self.helm_conn._store_status = asynctest.CoroutineMock()
275 self.helm_conn.get_instance_info = asynctest.CoroutineMock(
276 return_value=instance_info
277 )
278
279 await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
280 self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
281 self.helm_conn.fs.reverse_sync.assert_called_once_with(
282 from_path=self.cluster_id
283 )
284 command = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm delete --purge {}".format(
285 kdu_instance
286 )
287 self.helm_conn._local_async_exec.assert_called_once_with(
288 command=command, env=self.env, raise_exception_on_error=True
289 )
290
291 @asynctest.fail_on(active_handles=True)
292 async def test_get_services(self):
293 kdu_instance = "test_services_1"
294 service = {"name": "testservice", "type": "LoadBalancer"}
295 self.helm_conn._local_async_exec_pipe = asynctest.CoroutineMock(
296 return_value=("", 0)
297 )
298 self.helm_conn._parse_services = Mock(return_value=["testservice"])
299 self.helm_conn._get_service = asynctest.CoroutineMock(return_value=service)
300
301 services = await self.helm_conn.get_services(
302 self.cluster_uuid, kdu_instance, self.namespace
303 )
304 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
305 self.helm_conn.fs.reverse_sync.assert_called_once_with(
306 from_path=self.cluster_id
307 )
308 self.helm_conn._parse_services.assert_called_once()
309 command1 = "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm get manifest {} ".format(
310 kdu_instance
311 )
312 command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
313 self.helm_conn._local_async_exec_pipe.assert_called_once_with(
314 command1, command2, env=self.env, raise_exception_on_error=True
315 )
316 self.assertEqual(
317 services, [service], "Invalid service returned from get_service"
318 )
319
320 @asynctest.fail_on(active_handles=True)
321 async def test_get_service(self):
322 service_name = "service1"
323
324 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
325 await self.helm_conn.get_service(
326 self.cluster_uuid, service_name, self.namespace
327 )
328
329 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
330 self.helm_conn.fs.reverse_sync.assert_called_once_with(
331 from_path=self.cluster_id
332 )
333 command = (
334 "/usr/bin/kubectl --kubeconfig=./tmp/helm_cluster_id/.kube/config "
335 "--namespace=testk8s get service service1 -o=yaml"
336 )
337 self.helm_conn._local_async_exec.assert_called_once_with(
338 command=command, env=self.env, raise_exception_on_error=True
339 )
340
341 @asynctest.fail_on(active_handles=True)
342 async def test_inspect_kdu(self):
343 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
344
345 kdu_model = "stable/openldap:1.2.4"
346 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
347 await self.helm_conn.inspect_kdu(kdu_model, repo_url)
348
349 command = (
350 "/usr/bin/helm inspect openldap --repo "
351 "https://kubernetes-charts.storage.googleapis.com/ "
352 "--version 1.2.4"
353 )
354 self.helm_conn._local_async_exec.assert_called_with(
355 command=command, encode_utf8=True
356 )
357
358 @asynctest.fail_on(active_handles=True)
359 async def test_help_kdu(self):
360 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
361
362 kdu_model = "stable/openldap:1.2.4"
363 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
364 await self.helm_conn.help_kdu(kdu_model, repo_url)
365
366 command = (
367 "/usr/bin/helm inspect readme openldap --repo "
368 "https://kubernetes-charts.storage.googleapis.com/ "
369 "--version 1.2.4"
370 )
371 self.helm_conn._local_async_exec.assert_called_with(
372 command=command, encode_utf8=True
373 )
374
375 @asynctest.fail_on(active_handles=True)
376 async def test_values_kdu(self):
377 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
378
379 kdu_model = "stable/openldap:1.2.4"
380 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
381 await self.helm_conn.values_kdu(kdu_model, repo_url)
382
383 command = (
384 "/usr/bin/helm inspect values openldap --repo "
385 "https://kubernetes-charts.storage.googleapis.com/ "
386 "--version 1.2.4"
387 )
388 self.helm_conn._local_async_exec.assert_called_with(
389 command=command, encode_utf8=True
390 )
391
392 @asynctest.fail_on(active_handles=True)
393 async def test_instances_list(self):
394 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
395
396 await self.helm_conn.instances_list(self.cluster_uuid)
397 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
398 self.helm_conn.fs.reverse_sync.assert_called_once_with(
399 from_path=self.cluster_id
400 )
401 command = "/usr/bin/helm list --output yaml"
402 self.helm_conn._local_async_exec.assert_called_once_with(
403 command=command, env=self.env, raise_exception_on_error=True
404 )
405
406 @asynctest.fail_on(active_handles=True)
407 async def test_status_kdu(self):
408 kdu_instance = "stable-openldap-0005399828"
409 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
410
411 await self.helm_conn._status_kdu(
412 self.cluster_id, kdu_instance, self.namespace, yaml_format=True
413 )
414 command = (
415 "env KUBECONFIG=./tmp/helm_cluster_id/.kube/config /usr/bin/helm status {} --output yaml"
416 ).format(kdu_instance)
417 self.helm_conn._local_async_exec.assert_called_once_with(
418 command=command,
419 env=self.env,
420 raise_exception_on_error=True,
421 show_error_log=False,
422 )
423
424 @asynctest.fail_on(active_handles=True)
425 async def test_store_status(self):
426 kdu_instance = "stable-openldap-0005399828"
427 db_dict = {}
428 status = {
429 "info": {
430 "description": "Install complete",
431 "status": {
432 "code": "1",
433 "notes": "The openldap helm chart has been installed",
434 },
435 }
436 }
437 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=status)
438 self.helm_conn.write_app_status_to_db = asynctest.CoroutineMock(
439 return_value=status
440 )
441
442 await self.helm_conn._store_status(
443 cluster_id=self.cluster_id,
444 kdu_instance=kdu_instance,
445 namespace=self.namespace,
446 db_dict=db_dict,
447 operation="install",
448 )
449 self.helm_conn._status_kdu.assert_called_once_with(
450 cluster_id=self.cluster_id,
451 kdu_instance=kdu_instance,
452 namespace=self.namespace,
453 yaml_format=False,
454 )
455 self.helm_conn.write_app_status_to_db.assert_called_once_with(
456 db_dict=db_dict,
457 status="Install complete",
458 detailed_status=str(status),
459 operation="install",
460 )
461
462 @asynctest.fail_on(active_handles=True)
463 async def test_reset_uninstall_false(self):
464 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
465
466 await self.helm_conn.reset(self.cluster_uuid, force=False, uninstall_sw=False)
467 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
468 self.helm_conn.fs.file_delete.assert_called_once_with(
469 self.cluster_id, ignore_non_exist=True
470 )
471 self.helm_conn._uninstall_sw.assert_not_called()
472
473 @asynctest.fail_on(active_handles=True)
474 async def test_reset_uninstall(self):
475 kdu_instance = "stable-openldap-0021099429"
476 instances = [
477 {
478 "app_version": "2.4.48",
479 "chart": "openldap-1.2.3",
480 "name": kdu_instance,
481 "namespace": self.namespace,
482 "revision": "1",
483 "status": "deployed",
484 "updated": "2020-10-30 11:11:20.376744191 +0000 UTC",
485 }
486 ]
487 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
488 self.helm_conn.instances_list = asynctest.CoroutineMock(return_value=instances)
489 self.helm_conn.uninstall = asynctest.CoroutineMock()
490
491 await self.helm_conn.reset(self.cluster_uuid, force=True, uninstall_sw=True)
492 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
493 self.helm_conn.fs.file_delete.assert_called_once_with(
494 self.cluster_id, ignore_non_exist=True
495 )
496 self.helm_conn.instances_list.assert_called_once_with(
497 cluster_uuid=self.cluster_uuid
498 )
499 self.helm_conn.uninstall.assert_called_once_with(
500 cluster_uuid=self.cluster_uuid, kdu_instance=kdu_instance
501 )
502 self.helm_conn._uninstall_sw.assert_called_once_with(
503 self.cluster_id, self.namespace
504 )
505
506 @asynctest.fail_on(active_handles=True)
507 async def test_uninstall_sw_namespace(self):
508 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
509
510 await self.helm_conn._uninstall_sw(self.cluster_id, self.namespace)
511 calls = self.helm_conn._local_async_exec.call_args_list
512 self.assertEqual(
513 len(calls), 3, "To uninstall should have executed three commands"
514 )
515 call0_kargs = calls[0][1]
516 command_0 = "/usr/bin/helm --kubeconfig={} --home={} reset".format(
517 self.kube_config, self.helm_home
518 )
519 self.assertEqual(
520 call0_kargs,
521 {"command": command_0, "raise_exception_on_error": True, "env": self.env},
522 "Invalid args for first call to local_exec",
523 )
524 call1_kargs = calls[1][1]
525 command_1 = (
526 "/usr/bin/kubectl --kubeconfig={} delete "
527 "clusterrolebinding.rbac.authorization.k8s.io/osm-tiller-cluster-rule".format(
528 self.kube_config
529 )
530 )
531 self.assertEqual(
532 call1_kargs,
533 {"command": command_1, "raise_exception_on_error": False, "env": self.env},
534 "Invalid args for second call to local_exec",
535 )
536 call2_kargs = calls[2][1]
537 command_2 = (
538 "/usr/bin/kubectl --kubeconfig={} --namespace kube-system delete "
539 "serviceaccount/{}".format(self.kube_config, self.service_account)
540 )
541 self.assertEqual(
542 call2_kargs,
543 {"command": command_2, "raise_exception_on_error": False, "env": self.env},
544 "Invalid args for third call to local_exec",
545 )