Update create_execution_environment to pass the chart_model
[osm/LCM.git] / osm_lcm / tests / test_lcm_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 osm_lcm import lcm_helm_conn
22 from osm_lcm.lcm_helm_conn import LCMHelmConn
23 from asynctest.mock import Mock
24 from osm_lcm.data_utils.database.database import Database
25 from osm_lcm.data_utils.filesystem.filesystem import Filesystem
26
27 __author__ = "Isabel Lloret <illoret@indra.es>"
28
29
30 class TestLcmHelmConn(asynctest.TestCase):
31 logging.basicConfig(level=logging.DEBUG)
32 logger = logging.getLogger(__name__)
33 logger.setLevel(logging.DEBUG)
34
35 async def setUp(self):
36 Database.instance = None
37 self.db = Mock(Database({"database": {"driver": "memory"}}).instance.db)
38 Database().instance.db = self.db
39
40 Filesystem.instance = None
41 self.fs = asynctest.Mock(
42 Filesystem({"storage": {"driver": "local", "path": "/"}}).instance.fs
43 )
44
45 Filesystem.instance.fs = self.fs
46 self.fs.path = "/"
47
48 vca_config = {
49 "helmpath": "/usr/local/bin/helm",
50 "helm3path": "/usr/local/bin/helm3",
51 "kubectlpath": "/usr/bin/kubectl",
52 }
53 lcm_helm_conn.K8sHelmConnector = asynctest.Mock(lcm_helm_conn.K8sHelmConnector)
54 lcm_helm_conn.K8sHelm3Connector = asynctest.Mock(
55 lcm_helm_conn.K8sHelm3Connector
56 )
57 self.helm_conn = LCMHelmConn(
58 loop=self.loop, vca_config=vca_config, log=self.logger
59 )
60
61 @asynctest.fail_on(active_handles=True)
62 async def test_create_execution_environment(self):
63 namespace = "testnamespace"
64 db_dict = {}
65 artifact_path = "helm_sample_charm"
66 chart_model = "helm_sample_charm"
67 helm_chart_id = "helm_sample_charm_0001"
68 self.helm_conn._k8sclusterhelm3.install = asynctest.CoroutineMock(
69 return_value=None
70 )
71 self.helm_conn._k8sclusterhelm3.generate_kdu_instance_name = Mock()
72 self.helm_conn._k8sclusterhelm3.generate_kdu_instance_name.return_value = (
73 helm_chart_id
74 )
75 self.helm_conn._k8sclusterhelm2.generate_kdu_instance_name = Mock()
76 self.helm_conn._k8sclusterhelm2.generate_kdu_instance_name.return_value = (
77 helm_chart_id
78 )
79
80 self.db.get_one.return_value = {"_admin": {"helm-chart-v3": {"id": "myk8s_id"}}}
81 ee_id, _ = await self.helm_conn.create_execution_environment(
82 namespace, db_dict, artifact_path=artifact_path,
83 chart_model=chart_model, vca_type="helm-v3"
84 )
85 self.assertEqual(
86 ee_id,
87 "{}:{}.{}".format("helm-v3", "osm", helm_chart_id),
88 "Check ee_id format: <helm-version>:<default namespace>.<helm_chart-id>",
89 )
90 self.helm_conn._k8sclusterhelm3.install.assert_called_once_with(
91 "myk8s_id",
92 kdu_model="/helm_sample_charm",
93 kdu_instance=helm_chart_id,
94 namespace="osm",
95 db_dict=db_dict,
96 params=None,
97 timeout=None,
98 )
99
100 @asynctest.fail_on(active_handles=True)
101 async def test_get_ee_ssh_public__key(self):
102 ee_id = "osm.helm_sample_charm_0001"
103 db_dict = {}
104 lcm_helm_conn.socket.gethostbyname = asynctest.Mock()
105 mock_pub_key = "ssh-rsapubkey"
106 self.db.get_one.return_value = {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
107 self.helm_conn._get_ssh_key = asynctest.CoroutineMock(return_value=mock_pub_key)
108 pub_key = await self.helm_conn.get_ee_ssh_public__key(
109 ee_id=ee_id, db_dict=db_dict
110 )
111 self.assertEqual(pub_key, mock_pub_key)
112
113 @asynctest.fail_on(active_handles=True)
114 async def test_execute_primitive(self):
115 lcm_helm_conn.socket.gethostbyname = asynctest.Mock()
116 ee_id = "osm.helm_sample_charm_0001"
117 primitive_name = "sleep"
118 params = {}
119 self.db.get_one.return_value = {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
120 self.helm_conn._execute_primitive_internal = asynctest.CoroutineMock(
121 return_value=("OK", "test-ok")
122 )
123 message = await self.helm_conn.exec_primitive(ee_id, primitive_name, params)
124 self.assertEqual(message, "test-ok")
125
126 @asynctest.fail_on(active_handles=True)
127 async def test_execute_config_primitive(self):
128 self.logger.debug("Execute config primitive")
129 lcm_helm_conn.socket.gethostbyname = asynctest.Mock()
130 ee_id = "osm.helm_sample_charm_0001"
131 primitive_name = "config"
132 params = {"ssh-host-name": "host1"}
133 self.db.get_one.return_value = {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
134 self.helm_conn._execute_primitive_internal = asynctest.CoroutineMock(
135 return_value=("OK", "CONFIG OK")
136 )
137 message = await self.helm_conn.exec_primitive(ee_id, primitive_name, params)
138 self.assertEqual(message, "CONFIG OK")
139
140 @asynctest.fail_on(active_handles=True)
141 async def test_delete_execution_environment(self):
142 ee_id = "helm-v3:osm.helm_sample_charm_0001"
143 self.db.get_one.return_value = {"_admin": {"helm-chart-v3": {"id": "myk8s_id"}}}
144 self.helm_conn._k8sclusterhelm3.uninstall = asynctest.CoroutineMock(
145 return_value=""
146 )
147 await self.helm_conn.delete_execution_environment(ee_id)
148 self.helm_conn._k8sclusterhelm3.uninstall.assert_called_once_with(
149 "myk8s_id", "helm_sample_charm_0001"
150 )
151
152
153 if __name__ == "__main__":
154 asynctest.main()