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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # For those usages not covered by the Apache License, Version 2.0 please
15 # contact: alfonso.tiernosepulveda@telefonica.com
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
26 __author__
= "Isabel Lloret <illoret@indra.es>"
29 class TestK8sHelmConn(asynctest
.TestCase
):
30 logging
.basicConfig(level
=logging
.DEBUG
)
31 logger
= logging
.getLogger(__name__
)
32 logger
.setLevel(logging
.DEBUG
)
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"
49 "HELM_HOME": "{}/.helm".format(cluster_dir
),
50 "KUBECONFIG": "{}/.kube/config".format(cluster_dir
)
52 self
.helm_conn
= K8sHelmConnector(self
.fs
, self
.db
,
54 self
.logger
.debug("Set up executed")
56 @asynctest.fail_on(active_handles
=True)
57 async def test_init_env(self
):
61 @asynctest.fail_on(active_handles
=True)
62 async def test_repo_add(self
):
64 repo_url
= "https://charts.bitnami.com/bitnami"
65 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
67 await self
.helm_conn
.repo_add(self
.cluster_uuid
, repo_name
, repo_url
)
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
))
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")))
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))
93 await self
.helm_conn
.repo_list(self
.cluster_uuid
)
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)
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
)
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)
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"
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_kdu_instance_name
= Mock(return_value
=kdu_instance
)
123 await self
.helm_conn
.install(
128 namespace
=self
.namespace
,
132 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
133 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(from_path
=self
.cluster_id
)
134 self
.helm_conn
._store
_status
.assert_called_with(cluster_id
=self
.cluster_id
,
135 kdu_instance
=kdu_instance
,
136 namespace
=self
.namespace
,
141 command
= "/usr/bin/helm install --atomic --output yaml --timeout 300 " \
142 "--name=stable-openldap-0005399828 --namespace testk8s stable/openldap " \
144 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
146 raise_exception_on_error
=False)
148 @asynctest.fail_on(active_handles
=True)
149 async def test_upgrade(self
):
150 kdu_model
= "stable/openldap:1.2.3"
151 kdu_instance
= "stable-openldap-0005399828"
154 "chart": "openldap-1.2.2",
155 "name": kdu_instance
,
156 "namespace": self
.namespace
,
160 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
161 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
162 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(return_value
=instance_info
)
164 await self
.helm_conn
.upgrade(self
.cluster_uuid
,
169 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
170 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(from_path
=self
.cluster_id
)
171 self
.helm_conn
._store
_status
.assert_called_with(cluster_id
=self
.cluster_id
,
172 kdu_instance
=kdu_instance
,
173 namespace
=self
.namespace
,
178 command
= "/usr/bin/helm upgrade --atomic --output yaml --timeout 300 " \
179 "stable-openldap-0005399828 stable/openldap --version 1.2.3"
180 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
182 raise_exception_on_error
=False)
184 @asynctest.fail_on(active_handles
=True)
185 async def test_rollback(self
):
186 kdu_instance
= "stable-openldap-0005399828"
189 "chart": "openldap-1.2.3",
190 "name": kdu_instance
,
191 "namespace": self
.namespace
,
195 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
196 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
197 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(return_value
=instance_info
)
199 await self
.helm_conn
.rollback(self
.cluster_uuid
,
200 kdu_instance
=kdu_instance
,
203 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
204 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(from_path
=self
.cluster_id
)
205 self
.helm_conn
._store
_status
.assert_called_with(cluster_id
=self
.cluster_id
,
206 kdu_instance
=kdu_instance
,
207 namespace
=self
.namespace
,
209 operation
="rollback",
212 command
= "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
213 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
215 raise_exception_on_error
=False)
217 @asynctest.fail_on(active_handles
=True)
218 async def test_uninstall(self
):
219 kdu_instance
= "stable-openldap-0005399828"
221 "chart": "openldap-1.2.2",
222 "name": kdu_instance
,
223 "namespace": self
.namespace
,
227 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
228 self
.helm_conn
._store
_status
= asynctest
.CoroutineMock()
229 self
.helm_conn
.get_instance_info
= asynctest
.CoroutineMock(return_value
=instance_info
)
231 await self
.helm_conn
.uninstall(self
.cluster_uuid
, kdu_instance
)
232 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
233 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(from_path
=self
.cluster_id
)
234 command
= "/usr/bin/helm delete --purge {}".format(kdu_instance
)
235 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
237 raise_exception_on_error
=True)
239 @asynctest.fail_on(active_handles
=True)
240 async def test_get_services(self
):
241 kdu_instance
= "test_services_1"
243 "name": "testservice",
244 "type": "LoadBalancer"
246 self
.helm_conn
._local
_async
_exec
_pipe
= asynctest
.CoroutineMock(return_value
=("", 0))
247 self
.helm_conn
._parse
_services
= Mock(return_value
=["testservice"])
248 self
.helm_conn
._get
_service
= asynctest
.CoroutineMock(return_value
=service
)
250 services
= await self
.helm_conn
.get_services(self
.cluster_uuid
, kdu_instance
,
252 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
253 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(from_path
=self
.cluster_id
)
254 self
.helm_conn
._parse
_services
.assert_called_once()
255 command1
= "/usr/bin/helm get manifest {} ".format(kdu_instance
)
256 command2
= "/usr/bin/kubectl get --namespace={} -f -".format(self
.namespace
)
257 self
.helm_conn
._local
_async
_exec
_pipe
.assert_called_once_with(command1
, command2
,
259 raise_exception_on_error
=True)
260 self
.assertEqual(services
, [service
], "Invalid service returned from get_service")
262 @asynctest.fail_on(active_handles
=True)
263 async def test_get_service(self
):
264 service_name
= "service1"
266 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
267 await self
.helm_conn
.get_service(self
.cluster_uuid
, service_name
, self
.namespace
)
269 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
270 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(from_path
=self
.cluster_id
)
271 command
= "/usr/bin/kubectl --kubeconfig=./tmp/helm_cluster_id/.kube/config " \
272 "--namespace=testk8s get service service1 -o=yaml"
273 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
275 raise_exception_on_error
=True)
277 @asynctest.fail_on(active_handles
=True)
278 async def test_inspect_kdu(self
):
279 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
281 kdu_model
= "stable/openldap:1.2.4"
282 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
283 await self
.helm_conn
.inspect_kdu(kdu_model
, repo_url
)
285 command
= "/usr/bin/helm inspect openldap --repo " \
286 "https://kubernetes-charts.storage.googleapis.com/ " \
288 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
, encode_utf8
=True)
290 @asynctest.fail_on(active_handles
=True)
291 async def test_help_kdu(self
):
292 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
294 kdu_model
= "stable/openldap:1.2.4"
295 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
296 await self
.helm_conn
.help_kdu(kdu_model
, repo_url
)
298 command
= "/usr/bin/helm inspect readme openldap --repo " \
299 "https://kubernetes-charts.storage.googleapis.com/ " \
301 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
, encode_utf8
=True)
303 @asynctest.fail_on(active_handles
=True)
304 async def test_values_kdu(self
):
305 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
307 kdu_model
= "stable/openldap:1.2.4"
308 repo_url
= "https://kubernetes-charts.storage.googleapis.com/"
309 await self
.helm_conn
.values_kdu(kdu_model
, repo_url
)
311 command
= "/usr/bin/helm inspect values openldap --repo " \
312 "https://kubernetes-charts.storage.googleapis.com/ " \
314 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
, encode_utf8
=True)
316 @asynctest.fail_on(active_handles
=True)
317 async def test_instances_list(self
):
318 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
320 await self
.helm_conn
.instances_list(self
.cluster_uuid
)
321 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
322 self
.helm_conn
.fs
.reverse_sync
.assert_called_once_with(from_path
=self
.cluster_id
)
323 command
= "/usr/bin/helm list --output yaml"
324 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
326 raise_exception_on_error
=True)
328 @asynctest.fail_on(active_handles
=True)
329 async def test_status_kdu(self
):
330 kdu_instance
= "stable-openldap-0005399828"
331 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
333 await self
.helm_conn
._status
_kdu
(self
.cluster_id
, kdu_instance
,
334 self
.namespace
, return_text
=True)
335 command
= "/usr/bin/helm status {} --output yaml".format(kdu_instance
)
336 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
338 raise_exception_on_error
=True,
339 show_error_log
=False)
341 @asynctest.fail_on(active_handles
=True)
342 async def test_store_status(self
):
343 kdu_instance
= "stable-openldap-0005399828"
347 "description": "Install complete",
350 "notes": "The openldap helm chart has been installed"
354 self
.helm_conn
._status
_kdu
= asynctest
.CoroutineMock(return_value
=status
)
355 self
.helm_conn
.write_app_status_to_db
= asynctest
.CoroutineMock(return_value
=status
)
357 await self
.helm_conn
._store
_status
(cluster_id
=self
.cluster_id
,
358 kdu_instance
=kdu_instance
,
359 namespace
=self
.namespace
,
364 self
.helm_conn
._status
_kdu
.assert_called_once_with(cluster_id
=self
.cluster_id
,
365 kdu_instance
=kdu_instance
,
366 namespace
=self
.namespace
,
368 self
.helm_conn
.write_app_status_to_db
.assert_called_once_with(db_dict
=db_dict
,
369 status
="Install complete",
370 detailed_status
=str(status
),
373 @asynctest.fail_on(active_handles
=True)
374 async def test_reset_uninstall_false(self
):
375 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
377 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=False, uninstall_sw
=False)
378 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
379 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(self
.cluster_id
,
380 ignore_non_exist
=True)
381 self
.helm_conn
._uninstall
_sw
.assert_not_called()
383 @asynctest.fail_on(active_handles
=True)
384 async def test_reset_uninstall(self
):
385 kdu_instance
= 'stable-openldap-0021099429'
388 'app_version': '2.4.48',
389 'chart': 'openldap-1.2.3',
390 'name': kdu_instance
,
391 'namespace': self
.namespace
,
393 'status': 'deployed',
394 'updated': '2020-10-30 11:11:20.376744191 +0000 UTC'
397 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
398 self
.helm_conn
.instances_list
= asynctest
.CoroutineMock(return_value
=instances
)
399 self
.helm_conn
.uninstall
= asynctest
.CoroutineMock()
401 await self
.helm_conn
.reset(self
.cluster_uuid
, force
=True, uninstall_sw
=True)
402 self
.helm_conn
.fs
.sync
.assert_called_once_with(from_path
=self
.cluster_id
)
403 self
.helm_conn
.fs
.file_delete
.assert_called_once_with(self
.cluster_id
,
404 ignore_non_exist
=True)
405 self
.helm_conn
.instances_list
.assert_called_once_with(cluster_uuid
=self
.cluster_uuid
)
406 self
.helm_conn
.uninstall
.assert_called_once_with(cluster_uuid
=self
.cluster_uuid
,
407 kdu_instance
=kdu_instance
)
408 self
.helm_conn
._uninstall
_sw
.assert_called_once_with(self
.cluster_id
, self
.namespace
)
410 @asynctest.fail_on(active_handles
=True)
411 async def test_uninstall_sw_namespace(self
):
412 self
.helm_conn
._local
_async
_exec
= asynctest
.CoroutineMock(return_value
=("", 0))
414 await self
.helm_conn
._uninstall
_sw
(self
.cluster_id
, self
.namespace
)
415 calls
= self
.helm_conn
._local
_async
_exec
.call_args_list
416 self
.assertEqual(len(calls
), 3, "To uninstall should have executed three commands")
417 call0_kargs
= calls
[0][1]
418 command_0
= "/usr/bin/helm --kubeconfig={} --home={} reset".format(self
.kube_config
,
420 self
.assertEqual(call0_kargs
,
421 {"command": command_0
,
422 "raise_exception_on_error": True,
423 "env": self
.env
}, "Invalid args for first call to local_exec")
424 call1_kargs
= calls
[1][1]
425 command_1
= "/usr/bin/kubectl --kubeconfig={} delete " \
426 "clusterrolebinding.rbac.authorization.k8s.io/osm-tiller-cluster-rule".\
427 format(self
.kube_config
)
428 self
.assertEqual(call1_kargs
,
429 {"command": command_1
,
430 "raise_exception_on_error": False,
431 "env": self
.env
}, "Invalid args for second call to local_exec")
432 call2_kargs
= calls
[2][1]
433 command_2
= "/usr/bin/kubectl --kubeconfig={} --namespace kube-system delete " \
434 "serviceaccount/{}".\
435 format(self
.kube_config
, self
.service_account
)
436 self
.assertEqual(call2_kargs
,
437 {"command": command_2
,
438 "raise_exception_on_error": False,
439 "env": self
.env
}, "Invalid args for third call to local_exec")