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
_release
_name
= Mock(return_value
="stable-openldap-0005399828")
123 kdu_instance
= await self
.helm_conn
.install(self
.cluster_uuid
,
126 namespace
=self
.namespace
,
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
,
143 command
= "/usr/bin/helm install --atomic --output yaml --timeout 300 " \
144 "--name=stable-openldap-0005399828 --namespace testk8s stable/openldap " \
146 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
148 raise_exception_on_error
=False)
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"
156 "chart": "openldap-1.2.2",
157 "name": kdu_instance
,
158 "namespace": self
.namespace
,
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
)
166 await self
.helm_conn
.upgrade(self
.cluster_uuid
,
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
,
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
,
184 raise_exception_on_error
=False)
186 @asynctest.fail_on(active_handles
=True)
187 async def test_rollback(self
):
188 kdu_instance
= "stable-openldap-0005399828"
191 "chart": "openldap-1.2.3",
192 "name": kdu_instance
,
193 "namespace": self
.namespace
,
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
)
201 await self
.helm_conn
.rollback(self
.cluster_uuid
,
202 kdu_instance
=kdu_instance
,
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
,
211 operation
="rollback",
214 command
= "/usr/bin/helm rollback stable-openldap-0005399828 1 --wait"
215 self
.helm_conn
._local
_async
_exec
.assert_called_once_with(command
=command
,
217 raise_exception_on_error
=False)
219 @asynctest.fail_on(active_handles
=True)
220 async def test_uninstall(self
):
221 kdu_instance
= "stable-openldap-0005399828"
223 "chart": "openldap-1.2.2",
224 "name": kdu_instance
,
225 "namespace": self
.namespace
,
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
)
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
,
239 raise_exception_on_error
=True)
241 @asynctest.fail_on(active_handles
=True)
242 async def test_get_services(self
):
243 kdu_instance
= "test_services_1"
245 "name": "testservice",
246 "type": "LoadBalancer"
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
)
252 services
= await self
.helm_conn
.get_services(self
.cluster_uuid
, kdu_instance
,
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
,
261 raise_exception_on_error
=True)
262 self
.assertEqual(services
, [service
], "Invalid service returned from get_service")
264 @asynctest.fail_on(active_handles
=True)
265 async def test_get_service(self
):
266 service_name
= "service1"
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
)
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
,
277 raise_exception_on_error
=True)
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))
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
)
287 command
= "/usr/bin/helm inspect openldap --repo " \
288 "https://kubernetes-charts.storage.googleapis.com/ " \
290 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
, encode_utf8
=True)
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))
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
)
300 command
= "/usr/bin/helm inspect readme openldap --repo " \
301 "https://kubernetes-charts.storage.googleapis.com/ " \
303 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
, encode_utf8
=True)
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))
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
)
313 command
= "/usr/bin/helm inspect values openldap --repo " \
314 "https://kubernetes-charts.storage.googleapis.com/ " \
316 self
.helm_conn
._local
_async
_exec
.assert_called_with(command
=command
, encode_utf8
=True)
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))
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
,
328 raise_exception_on_error
=True)
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))
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
,
340 raise_exception_on_error
=True,
341 show_error_log
=False)
343 @asynctest.fail_on(active_handles
=True)
344 async def test_store_status(self
):
345 kdu_instance
= "stable-openldap-0005399828"
349 "description": "Install complete",
352 "notes": "The openldap helm chart has been installed"
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
)
359 await self
.helm_conn
._store
_status
(cluster_id
=self
.cluster_id
,
360 kdu_instance
=kdu_instance
,
361 namespace
=self
.namespace
,
366 self
.helm_conn
._status
_kdu
.assert_called_once_with(cluster_id
=self
.cluster_id
,
367 kdu_instance
=kdu_instance
,
368 namespace
=self
.namespace
,
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
),
375 @asynctest.fail_on(active_handles
=True)
376 async def test_reset_uninstall_false(self
):
377 self
.helm_conn
._uninstall
_sw
= asynctest
.CoroutineMock()
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()
385 @asynctest.fail_on(active_handles
=True)
386 async def test_reset_uninstall(self
):
387 kdu_instance
= 'stable-openldap-0021099429'
390 'app_version': '2.4.48',
391 'chart': 'openldap-1.2.3',
392 'name': kdu_instance
,
393 'namespace': self
.namespace
,
395 'status': 'deployed',
396 'updated': '2020-10-30 11:11:20.376744191 +0000 UTC'
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()
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
)
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))
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
,
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")