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 osm_lcm
import lcm_helm_conn
22 from osm_lcm
.lcm_helm_conn
import LCMHelmConn
23 from osm_common
.fslocal
import FsLocal
24 from asynctest
.mock
import Mock
25 from osm_common
.dbmemory
import DbMemory
27 __author__
= "Isabel Lloret <illoret@indra.es>"
30 class TestLcmHelmConn(asynctest
.TestCase
):
31 logging
.basicConfig(level
=logging
.DEBUG
)
32 logger
= logging
.getLogger(__name__
)
33 logger
.setLevel(logging
.DEBUG
)
35 async def setUp(self
):
36 self
.db
= Mock(DbMemory())
37 self
.fs
= asynctest
.Mock(FsLocal())
38 self
.fs
.path
= "/app/storage"
40 "helmpath": "/usr/local/bin/helm",
41 "helm3path": "/usr/local/bin/helm3",
42 "kubectlpath": "/usr/bin/kubectl"
44 lcm_helm_conn
.K8sHelmConnector
= asynctest
.Mock(lcm_helm_conn
.K8sHelmConnector
)
45 lcm_helm_conn
.K8sHelm3Connector
= asynctest
.Mock(lcm_helm_conn
.K8sHelm3Connector
)
46 self
.helm_conn
= LCMHelmConn(self
.db
, self
.fs
, loop
=self
.loop
, vca_config
=vca_config
, log
=self
.logger
)
48 @asynctest.fail_on(active_handles
=True)
49 async def test_create_execution_environment(self
):
50 namespace
= "testnamespace"
52 artifact_path
= "helm_sample_charm"
53 helm_chart_id
= "helm_sample_charm_0001"
54 self
.helm_conn
._k
8sclusterhelm
3.install
= asynctest
.CoroutineMock(return_value
=helm_chart_id
)
55 self
.db
.get_one
.return_value
= {"_admin": {"helm-chart-v3": {"id": "myk8s_id"}}}
56 ee_id
, _
= await self
.helm_conn
.create_execution_environment(namespace
,
58 artifact_path
=artifact_path
,
60 self
.assertEqual(ee_id
, "{}:{}.{}".format("helm-v3", "osm", helm_chart_id
),
61 "Check ee_id format: <helm-version>:<default namespace>.<helm_chart-id>")
62 self
.helm_conn
._k
8sclusterhelm
3.install
.assert_called_once_with("myk8s_id",
63 kdu_model
="/app/storage/helm_sample_charm",
64 namespace
="osm", db_dict
=db_dict
,
65 params
=None, timeout
=None)
67 @asynctest.fail_on(active_handles
=True)
68 async def test_get_ee_ssh_public__key(self
):
69 ee_id
= "osm.helm_sample_charm_0001"
71 lcm_helm_conn
.socket
.gethostbyname
= asynctest
.Mock()
72 mock_pub_key
= "ssh-rsapubkey"
73 self
.db
.get_one
.return_value
= {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
74 self
.helm_conn
._get
_ssh
_key
= asynctest
.CoroutineMock(return_value
=mock_pub_key
)
75 pub_key
= await self
.helm_conn
.get_ee_ssh_public__key(ee_id
=ee_id
, db_dict
=db_dict
)
76 self
.assertEqual(pub_key
, mock_pub_key
)
78 @asynctest.fail_on(active_handles
=True)
79 async def test_execute_primitive(self
):
80 lcm_helm_conn
.socket
.gethostbyname
= asynctest
.Mock()
81 ee_id
= "osm.helm_sample_charm_0001"
82 primitive_name
= "sleep"
84 self
.db
.get_one
.return_value
= {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
85 self
.helm_conn
._execute
_primitive
_internal
= asynctest
.CoroutineMock(return_value
=("OK", "test-ok"))
86 message
= await self
.helm_conn
.exec_primitive(ee_id
, primitive_name
, params
)
87 self
.assertEqual(message
, "test-ok")
89 @asynctest.fail_on(active_handles
=True)
90 async def test_execute_config_primitive(self
):
91 self
.logger
.debug("Execute config primitive")
92 lcm_helm_conn
.socket
.gethostbyname
= asynctest
.Mock()
93 ee_id
= "osm.helm_sample_charm_0001"
94 primitive_name
= "config"
95 params
= {"ssh-host-name": "host1"}
96 self
.db
.get_one
.return_value
= {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
97 self
.helm_conn
._execute
_primitive
_internal
= asynctest
.CoroutineMock(return_value
=("OK", "CONFIG OK"))
98 message
= await self
.helm_conn
.exec_primitive(ee_id
, primitive_name
, params
)
99 self
.assertEqual(message
, "CONFIG OK")
101 @asynctest.fail_on(active_handles
=True)
102 async def test_delete_execution_environment(self
):
103 ee_id
= "helm-v3:osm.helm_sample_charm_0001"
104 self
.db
.get_one
.return_value
= {"_admin": {"helm-chart-v3": {"id": "myk8s_id"}}}
105 self
.helm_conn
._k
8sclusterhelm
3.uninstall
= asynctest
.CoroutineMock(return_value
="")
106 await self
.helm_conn
.delete_execution_environment(ee_id
)
107 self
.helm_conn
._k
8sclusterhelm
3.uninstall
.assert_called_once_with("myk8s_id", "helm_sample_charm_0001")
110 if __name__
== '__main__':