Support for helm v3
[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(return_value=("", 0))
45 cluster_dir = self.fs.path + self.cluster_id
46 self.kube_config = self.fs.path + self.cluster_id + "/.kube/config"
47 self.helm_home = self.fs.path + self.cluster_id + "/.helm"
48 self.env = {
49 "HELM_HOME": "{}/.helm".format(cluster_dir),
50 "KUBECONFIG": "{}/.kube/config".format(cluster_dir)
51 }
52 self.helm_conn = K8sHelmConnector(self.fs, self.db,
53 log=self.logger)
54 self.logger.debug("Set up executed")
55
56 @asynctest.fail_on(active_handles=True)
57 async def test_init_env(self):
58 # TODO
59 pass
60
61 @asynctest.fail_on(active_handles=True)
62 async def test_repo_add(self):
63 repo_name = "bitnami"
64 repo_url = "https://charts.bitnami.com/bitnami"
65 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
66
67 await self.helm_conn.repo_add(self.cluster_uuid, repo_name, repo_url)
68
69 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
70 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
71 self.assertEqual(self.helm_conn._local_async_exec.call_count, 2,
72 "local_async_exec expected 2 calls, called {}".format(
73 self.helm_conn._local_async_exec.call_count))
74
75 repo_update_command = "/usr/bin/helm repo update"
76 repo_add_command = "/usr/bin/helm repo add {} {}".format(repo_name, repo_url)
77 calls = self.helm_conn._local_async_exec.call_args_list
78 call0_kargs = calls[0][1]
79 self.assertEqual(call0_kargs.get("command"), repo_update_command,
80 "Invalid repo update command: {}".format(call0_kargs.get("command")))
81 self.assertEqual(call0_kargs.get("env"), self.env,
82 "Invalid env for update command: {}".format(call0_kargs.get("env")))
83 call1_kargs = calls[1][1]
84 self.assertEqual(call1_kargs.get("command"), repo_add_command,
85 "Invalid repo add command: {}".format(call1_kargs.get("command")))
86 self.assertEqual(call1_kargs.get("env"), self.env,
87 "Invalid env for add command: {}".format(call1_kargs.get("env")))
88
89 @asynctest.fail_on(active_handles=True)
90 async def test_repo_list(self):
91 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
92
93 await self.helm_conn.repo_list(self.cluster_uuid)
94
95 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
96 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
97 command = "/usr/bin/helm repo list --output yaml"
98 self.helm_conn._local_async_exec.assert_called_with(command=command, env=self.env,
99 raise_exception_on_error=False)
100
101 @asynctest.fail_on(active_handles=True)
102 async def test_repo_remove(self):
103 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
104 repo_name = "bitnami"
105 await self.helm_conn.repo_remove(self.cluster_uuid, repo_name)
106
107 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
108 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
109 command = "/usr/bin/helm repo remove {}".format(repo_name)
110 self.helm_conn._local_async_exec.assert_called_once_with(command=command, env=self.env,
111 raise_exception_on_error=True)
112
113 @asynctest.fail_on(active_handles=True)
114 async def test_install(self):
115 kdu_model = "stable/openldap:1.2.2"
116 kdu_instance = "stable-openldap-0005399828"
117 db_dict = {}
118 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
119 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=None)
120 self.helm_conn._store_status = asynctest.CoroutineMock()
121 self.helm_conn._generate_release_name = Mock(return_value="stable-openldap-0005399828")
122
123 kdu_instance = await self.helm_conn.install(self.cluster_uuid,
124 kdu_model,
125 atomic=True,
126 namespace=self.namespace,
127 db_dict=db_dict)
128
129 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
130 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
131 self.helm_conn._generate_release_name.assert_called_once_with("stable/openldap")
132 self.helm_conn._status_kdu.assert_called_once_with(cluster_id=self.cluster_id,
133 kdu_instance=kdu_instance,
134 namespace=self.namespace,
135 show_error_log=False)
136 self.helm_conn._store_status.assert_called_with(cluster_id=self.cluster_id,
137 kdu_instance=kdu_instance,
138 namespace=self.namespace,
139 db_dict=db_dict,
140 operation="install",
141 run_once=True,
142 check_every=0)
143 command = "/usr/bin/helm install --atomic --output yaml --timeout 300 " \
144 "--name=stable-openldap-0005399828 --namespace testk8s stable/openldap " \
145 "--version 1.2.2"
146 self.helm_conn._local_async_exec.assert_called_once_with(command=command,
147 env=self.env,
148 raise_exception_on_error=False)
149
150 @asynctest.fail_on(active_handles=True)
151 async def test_upgrade(self):
152 kdu_model = "stable/openldap:1.2.3"
153 kdu_instance = "stable-openldap-0005399828"
154 db_dict = {}
155 instance_info = {
156 "chart": "openldap-1.2.2",
157 "name": kdu_instance,
158 "namespace": self.namespace,
159 "revision": 1,
160 "status": "DEPLOYED"
161 }
162 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
163 self.helm_conn._store_status = asynctest.CoroutineMock()
164 self.helm_conn.get_instance_info = asynctest.CoroutineMock(return_value=instance_info)
165
166 await self.helm_conn.upgrade(self.cluster_uuid,
167 kdu_instance,
168 kdu_model,
169 atomic=True,
170 db_dict=db_dict)
171 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
172 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
173 self.helm_conn._store_status.assert_called_with(cluster_id=self.cluster_id,
174 kdu_instance=kdu_instance,
175 namespace=self.namespace,
176 db_dict=db_dict,
177 operation="upgrade",
178 run_once=True,
179 check_every=0)
180 command = "/usr/bin/helm upgrade --atomic --output yaml --timeout 300 " \
181 "stable-openldap-0005399828 stable/openldap --version 1.2.3"
182 self.helm_conn._local_async_exec.assert_called_once_with(command=command,
183 env=self.env,
184 raise_exception_on_error=False)
185
186 @asynctest.fail_on(active_handles=True)
187 async def test_rollback(self):
188 kdu_instance = "stable-openldap-0005399828"
189 db_dict = {}
190 instance_info = {
191 "chart": "openldap-1.2.3",
192 "name": kdu_instance,
193 "namespace": self.namespace,
194 "revision": 2,
195 "status": "DEPLOYED"
196 }
197 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
198 self.helm_conn._store_status = asynctest.CoroutineMock()
199 self.helm_conn.get_instance_info = asynctest.CoroutineMock(return_value=instance_info)
200
201 await self.helm_conn.rollback(self.cluster_uuid,
202 kdu_instance=kdu_instance,
203 revision=1,
204 db_dict=db_dict)
205 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
206 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
207 self.helm_conn._store_status.assert_called_with(cluster_id=self.cluster_id,
208 kdu_instance=kdu_instance,
209 namespace=self.namespace,
210 db_dict=db_dict,
211 operation="rollback",
212 run_once=True,
213 check_every=0)
214 command = "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
215 self.helm_conn._local_async_exec.assert_called_once_with(command=command,
216 env=self.env,
217 raise_exception_on_error=False)
218
219 @asynctest.fail_on(active_handles=True)
220 async def test_uninstall(self):
221 kdu_instance = "stable-openldap-0005399828"
222 instance_info = {
223 "chart": "openldap-1.2.2",
224 "name": kdu_instance,
225 "namespace": self.namespace,
226 "revision": 3,
227 "status": "DEPLOYED"
228 }
229 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
230 self.helm_conn._store_status = asynctest.CoroutineMock()
231 self.helm_conn.get_instance_info = asynctest.CoroutineMock(return_value=instance_info)
232
233 await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
234 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
235 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
236 command = "/usr/bin/helm delete --purge {}".format(kdu_instance)
237 self.helm_conn._local_async_exec.assert_called_once_with(command=command,
238 env=self.env,
239 raise_exception_on_error=True)
240
241 @asynctest.fail_on(active_handles=True)
242 async def test_get_services(self):
243 kdu_instance = "test_services_1"
244 service = {
245 "name": "testservice",
246 "type": "LoadBalancer"
247 }
248 self.helm_conn._local_async_exec_pipe = asynctest.CoroutineMock(return_value=("", 0))
249 self.helm_conn._parse_services = Mock(return_value=["testservice"])
250 self.helm_conn._get_service = asynctest.CoroutineMock(return_value=service)
251
252 services = await self.helm_conn.get_services(self.cluster_uuid, kdu_instance,
253 self.namespace)
254 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
255 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
256 self.helm_conn._parse_services.assert_called_once()
257 command1 = "/usr/bin/helm get manifest {} ".format(kdu_instance)
258 command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
259 self.helm_conn._local_async_exec_pipe.assert_called_once_with(command1, command2,
260 env=self.env,
261 raise_exception_on_error=True)
262 self.assertEqual(services, [service], "Invalid service returned from get_service")
263
264 @asynctest.fail_on(active_handles=True)
265 async def test_get_service(self):
266 service_name = "service1"
267
268 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
269 await self.helm_conn.get_service(self.cluster_uuid, service_name, self.namespace)
270
271 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
272 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
273 command = "/usr/bin/kubectl --kubeconfig=./tmp/helm_cluster_id/.kube/config " \
274 "--namespace=testk8s get service service1 -o=yaml"
275 self.helm_conn._local_async_exec.assert_called_once_with(command=command,
276 env=self.env,
277 raise_exception_on_error=True)
278
279 @asynctest.fail_on(active_handles=True)
280 async def test_inspect_kdu(self):
281 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
282
283 kdu_model = "stable/openldap:1.2.4"
284 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
285 await self.helm_conn.inspect_kdu(kdu_model, repo_url)
286
287 command = "/usr/bin/helm inspect openldap --repo " \
288 "https://kubernetes-charts.storage.googleapis.com/ " \
289 "--version 1.2.4"
290 self.helm_conn._local_async_exec.assert_called_with(command=command, encode_utf8=True)
291
292 @asynctest.fail_on(active_handles=True)
293 async def test_help_kdu(self):
294 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
295
296 kdu_model = "stable/openldap:1.2.4"
297 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
298 await self.helm_conn.help_kdu(kdu_model, repo_url)
299
300 command = "/usr/bin/helm inspect readme openldap --repo " \
301 "https://kubernetes-charts.storage.googleapis.com/ " \
302 "--version 1.2.4"
303 self.helm_conn._local_async_exec.assert_called_with(command=command, encode_utf8=True)
304
305 @asynctest.fail_on(active_handles=True)
306 async def test_values_kdu(self):
307 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
308
309 kdu_model = "stable/openldap:1.2.4"
310 repo_url = "https://kubernetes-charts.storage.googleapis.com/"
311 await self.helm_conn.values_kdu(kdu_model, repo_url)
312
313 command = "/usr/bin/helm inspect values openldap --repo " \
314 "https://kubernetes-charts.storage.googleapis.com/ " \
315 "--version 1.2.4"
316 self.helm_conn._local_async_exec.assert_called_with(command=command, encode_utf8=True)
317
318 @asynctest.fail_on(active_handles=True)
319 async def test_instances_list(self):
320 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
321
322 await self.helm_conn.instances_list(self.cluster_uuid)
323 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
324 self.helm_conn.fs.reverse_sync.assert_called_once_with(from_path=self.cluster_id)
325 command = "/usr/bin/helm list --output yaml"
326 self.helm_conn._local_async_exec.assert_called_once_with(command=command,
327 env=self.env,
328 raise_exception_on_error=True)
329
330 @asynctest.fail_on(active_handles=True)
331 async def test_status_kdu(self):
332 kdu_instance = "stable-openldap-0005399828"
333 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
334
335 await self.helm_conn._status_kdu(self.cluster_id, kdu_instance,
336 self.namespace, return_text=True)
337 command = "/usr/bin/helm status {} --output yaml".format(kdu_instance)
338 self.helm_conn._local_async_exec.assert_called_once_with(command=command,
339 env=self.env,
340 raise_exception_on_error=True,
341 show_error_log=False)
342
343 @asynctest.fail_on(active_handles=True)
344 async def test_store_status(self):
345 kdu_instance = "stable-openldap-0005399828"
346 db_dict = {}
347 status = {
348 "info": {
349 "description": "Install complete",
350 "status": {
351 "code": "1",
352 "notes": "The openldap helm chart has been installed"
353 }
354 }
355 }
356 self.helm_conn._status_kdu = asynctest.CoroutineMock(return_value=status)
357 self.helm_conn.write_app_status_to_db = asynctest.CoroutineMock(return_value=status)
358
359 await self.helm_conn._store_status(cluster_id=self.cluster_id,
360 kdu_instance=kdu_instance,
361 namespace=self.namespace,
362 db_dict=db_dict,
363 operation="install",
364 run_once=True,
365 check_every=0)
366 self.helm_conn._status_kdu.assert_called_once_with(cluster_id=self.cluster_id,
367 kdu_instance=kdu_instance,
368 namespace=self.namespace,
369 return_text=False)
370 self.helm_conn.write_app_status_to_db.assert_called_once_with(db_dict=db_dict,
371 status="Install complete",
372 detailed_status=str(status),
373 operation="install")
374
375 @asynctest.fail_on(active_handles=True)
376 async def test_reset_uninstall_false(self):
377 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
378
379 await self.helm_conn.reset(self.cluster_uuid, force=False, uninstall_sw=False)
380 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
381 self.helm_conn.fs.file_delete.assert_called_once_with(self.cluster_id,
382 ignore_non_exist=True)
383 self.helm_conn._uninstall_sw.assert_not_called()
384
385 @asynctest.fail_on(active_handles=True)
386 async def test_reset_uninstall(self):
387 kdu_instance = 'stable-openldap-0021099429'
388 instances = [
389 {
390 'app_version': '2.4.48',
391 'chart': 'openldap-1.2.3',
392 'name': kdu_instance,
393 'namespace': self.namespace,
394 'revision': '1',
395 'status': 'deployed',
396 'updated': '2020-10-30 11:11:20.376744191 +0000 UTC'
397 }
398 ]
399 self.helm_conn._uninstall_sw = asynctest.CoroutineMock()
400 self.helm_conn.instances_list = asynctest.CoroutineMock(return_value=instances)
401 self.helm_conn.uninstall = asynctest.CoroutineMock()
402
403 await self.helm_conn.reset(self.cluster_uuid, force=True, uninstall_sw=True)
404 self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
405 self.helm_conn.fs.file_delete.assert_called_once_with(self.cluster_id,
406 ignore_non_exist=True)
407 self.helm_conn.instances_list.assert_called_once_with(cluster_uuid=self.cluster_uuid)
408 self.helm_conn.uninstall.assert_called_once_with(cluster_uuid=self.cluster_uuid,
409 kdu_instance=kdu_instance)
410 self.helm_conn._uninstall_sw.assert_called_once_with(self.cluster_id, self.namespace)
411
412 @asynctest.fail_on(active_handles=True)
413 async def test_uninstall_sw_namespace(self):
414 self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
415
416 await self.helm_conn._uninstall_sw(self.cluster_id, self.namespace)
417 calls = self.helm_conn._local_async_exec.call_args_list
418 self.assertEqual(len(calls), 3, "To uninstall should have executed three commands")
419 call0_kargs = calls[0][1]
420 command_0 = "/usr/bin/helm --kubeconfig={} --home={} reset".format(self.kube_config,
421 self.helm_home)
422 self.assertEqual(call0_kargs,
423 {"command": command_0,
424 "raise_exception_on_error": True,
425 "env": self.env}, "Invalid args for first call to local_exec")
426 call1_kargs = calls[1][1]
427 command_1 = "/usr/bin/kubectl --kubeconfig={} delete " \
428 "clusterrolebinding.rbac.authorization.k8s.io/osm-tiller-cluster-rule".\
429 format(self.kube_config)
430 self.assertEqual(call1_kargs,
431 {"command": command_1,
432 "raise_exception_on_error": False,
433 "env": self.env}, "Invalid args for second call to local_exec")
434 call2_kargs = calls[2][1]
435 command_2 = "/usr/bin/kubectl --kubeconfig={} --namespace kube-system delete " \
436 "serviceaccount/{}".\
437 format(self.kube_config, self.service_account)
438 self.assertEqual(call2_kargs,
439 {"command": command_2,
440 "raise_exception_on_error": False,
441 "env": self.env}, "Invalid args for third call to local_exec")