47838b3ed44721aceaed3c2e03f1d5f3d4f88f15
[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({
38 "database": {
39 "driver": "memory"
40 }
41 }).instance.db)
42 Database().instance.db = self.db
43
44 Filesystem.instance = None
45 self.fs = asynctest.Mock(Filesystem({
46 "storage": {
47 "driver": "local",
48 "path": "/"
49 }
50 }).instance.fs)
51
52 Filesystem.instance.fs = self.fs
53 self.fs.path = "/"
54
55 vca_config = {
56 "helmpath": "/usr/local/bin/helm",
57 "helm3path": "/usr/local/bin/helm3",
58 "kubectlpath": "/usr/bin/kubectl"
59 }
60 lcm_helm_conn.K8sHelmConnector = asynctest.Mock(lcm_helm_conn.K8sHelmConnector)
61 lcm_helm_conn.K8sHelm3Connector = asynctest.Mock(lcm_helm_conn.K8sHelm3Connector)
62 self.helm_conn = LCMHelmConn(loop=self.loop, vca_config=vca_config, log=self.logger)
63
64 @asynctest.fail_on(active_handles=True)
65 async def test_create_execution_environment(self):
66 namespace = "testnamespace"
67 db_dict = {}
68 artifact_path = "helm_sample_charm"
69 helm_chart_id = "helm_sample_charm_0001"
70 self.helm_conn._k8sclusterhelm3.install = asynctest.CoroutineMock(return_value=None)
71 self.helm_conn._k8sclusterhelm3.generate_kdu_instance_name = Mock()
72 self.helm_conn._k8sclusterhelm3.generate_kdu_instance_name.return_value = helm_chart_id
73 self.helm_conn._k8sclusterhelm2.generate_kdu_instance_name = Mock()
74 self.helm_conn._k8sclusterhelm2.generate_kdu_instance_name.return_value = helm_chart_id
75
76 self.db.get_one.return_value = {"_admin": {"helm-chart-v3": {"id": "myk8s_id"}}}
77 ee_id, _ = await self.helm_conn.create_execution_environment(namespace,
78 db_dict,
79 artifact_path=artifact_path,
80 vca_type="helm-v3")
81 self.assertEqual(ee_id, "{}:{}.{}".format("helm-v3", "osm", helm_chart_id),
82 "Check ee_id format: <helm-version>:<default namespace>.<helm_chart-id>")
83 self.helm_conn._k8sclusterhelm3.install.assert_called_once_with("myk8s_id",
84 kdu_model="/helm_sample_charm",
85 kdu_instance=helm_chart_id,
86 namespace="osm", db_dict=db_dict,
87 params=None, timeout=None)
88
89 @asynctest.fail_on(active_handles=True)
90 async def test_get_ee_ssh_public__key(self):
91 ee_id = "osm.helm_sample_charm_0001"
92 db_dict = {}
93 lcm_helm_conn.socket.gethostbyname = asynctest.Mock()
94 mock_pub_key = "ssh-rsapubkey"
95 self.db.get_one.return_value = {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
96 self.helm_conn._get_ssh_key = asynctest.CoroutineMock(return_value=mock_pub_key)
97 pub_key = await self.helm_conn.get_ee_ssh_public__key(ee_id=ee_id, db_dict=db_dict)
98 self.assertEqual(pub_key, mock_pub_key)
99
100 @asynctest.fail_on(active_handles=True)
101 async def test_execute_primitive(self):
102 lcm_helm_conn.socket.gethostbyname = asynctest.Mock()
103 ee_id = "osm.helm_sample_charm_0001"
104 primitive_name = "sleep"
105 params = {}
106 self.db.get_one.return_value = {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
107 self.helm_conn._execute_primitive_internal = asynctest.CoroutineMock(return_value=("OK", "test-ok"))
108 message = await self.helm_conn.exec_primitive(ee_id, primitive_name, params)
109 self.assertEqual(message, "test-ok")
110
111 @asynctest.fail_on(active_handles=True)
112 async def test_execute_config_primitive(self):
113 self.logger.debug("Execute config primitive")
114 lcm_helm_conn.socket.gethostbyname = asynctest.Mock()
115 ee_id = "osm.helm_sample_charm_0001"
116 primitive_name = "config"
117 params = {"ssh-host-name": "host1"}
118 self.db.get_one.return_value = {"_admin": {"helm-chart": {"id": "myk8s_id"}}}
119 self.helm_conn._execute_primitive_internal = asynctest.CoroutineMock(return_value=("OK", "CONFIG OK"))
120 message = await self.helm_conn.exec_primitive(ee_id, primitive_name, params)
121 self.assertEqual(message, "CONFIG OK")
122
123 @asynctest.fail_on(active_handles=True)
124 async def test_delete_execution_environment(self):
125 ee_id = "helm-v3:osm.helm_sample_charm_0001"
126 self.db.get_one.return_value = {"_admin": {"helm-chart-v3": {"id": "myk8s_id"}}}
127 self.helm_conn._k8sclusterhelm3.uninstall = asynctest.CoroutineMock(return_value="")
128 await self.helm_conn.delete_execution_environment(ee_id)
129 self.helm_conn._k8sclusterhelm3.uninstall.assert_called_once_with("myk8s_id", "helm_sample_charm_0001")
130
131
132 if __name__ == '__main__':
133 asynctest.main()