309fc1f474953f60621c031a8cf024dca23fa6b1
[osm/LCM.git] / osm_lcm / tests / test_vim_sdn.py
1 # Copyright 2021 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import asyncio
16 from unittest import TestCase
17 from unittest.mock import Mock, patch, MagicMock
18
19
20 from osm_common import msgbase
21 from osm_common.dbbase import DbException
22 from osm_lcm.vim_sdn import K8sClusterLcm, VcaLcm
23
24
25 class AsyncMock(MagicMock):
26 async def __call__(self, *args, **kwargs):
27 return super(AsyncMock, self).__call__(*args, **kwargs)
28
29
30 class TestVcaLcm(TestCase):
31 @patch("osm_lcm.lcm_utils.Database")
32 @patch("osm_lcm.lcm_utils.Filesystem")
33 def setUp(self, mock_filesystem, mock_database):
34 self.loop = asyncio.get_event_loop()
35 self.msg = Mock(msgbase.MsgBase())
36 self.lcm_tasks = Mock()
37 self.config = {"database": {"driver": "mongo"}}
38 self.vca_lcm = VcaLcm(self.msg, self.lcm_tasks, self.config, self.loop)
39 self.vca_lcm.db = Mock()
40 self.vca_lcm.fs = Mock()
41
42 def test_vca_lcm_create(self):
43 vca_content = {"op_id": "order-id", "_id": "id"}
44 db_vca = {
45 "_id": "vca-id",
46 "secret": "secret",
47 "cacert": "cacert",
48 "schema_version": "1.11",
49 }
50 order_id = "order-id"
51 self.lcm_tasks.lock_HA.return_value = True
52 self.vca_lcm.db.get_one.return_value = db_vca
53 self.vca_lcm.n2vc.validate_vca = AsyncMock()
54 self.vca_lcm.update_db_2 = Mock()
55
56 self.loop.run_until_complete(self.vca_lcm.create(vca_content, order_id))
57
58 self.lcm_tasks.lock_HA.assert_called_with("vca", "create", "order-id")
59 self.vca_lcm.db.encrypt_decrypt_fields.assert_called_with(
60 db_vca,
61 "decrypt",
62 ["secret", "cacert"],
63 schema_version="1.11",
64 salt="vca-id",
65 )
66 self.vca_lcm.update_db_2.assert_called_with(
67 "vca",
68 "id",
69 {
70 "_admin.operationalState": "ENABLED",
71 "_admin.detailed-status": "Connectivity: ok",
72 },
73 )
74 self.lcm_tasks.unlock_HA.assert_called_with(
75 "vca",
76 "create",
77 "order-id",
78 operationState="COMPLETED",
79 detailed_status="VCA validated",
80 )
81 self.lcm_tasks.remove.assert_called_with("vca", "id", "order-id")
82
83 def test_vca_lcm_create_exception(self):
84 vca_content = {"op_id": "order-id", "_id": "id"}
85 db_vca = {
86 "_id": "vca-id",
87 "secret": "secret",
88 "cacert": "cacert",
89 "schema_version": "1.11",
90 }
91 order_id = "order-id"
92 self.lcm_tasks.lock_HA.return_value = True
93 self.vca_lcm.db.get_one.return_value = db_vca
94 self.vca_lcm.n2vc.validate_vca = AsyncMock()
95 self.vca_lcm.n2vc.validate_vca.side_effect = Exception("failed")
96 self.vca_lcm.update_db_2 = Mock()
97 self.vca_lcm.update_db_2.side_effect = DbException("failed")
98 self.loop.run_until_complete(self.vca_lcm.create(vca_content, order_id))
99
100 self.lcm_tasks.lock_HA.assert_called_with("vca", "create", "order-id")
101 self.vca_lcm.db.encrypt_decrypt_fields.assert_called_with(
102 db_vca,
103 "decrypt",
104 ["secret", "cacert"],
105 schema_version="1.11",
106 salt="vca-id",
107 )
108 self.vca_lcm.update_db_2.assert_called_with(
109 "vca",
110 "id",
111 {
112 "_admin.operationalState": "ERROR",
113 "_admin.detailed-status": "Failed with exception: failed",
114 },
115 )
116 self.lcm_tasks.unlock_HA.assert_not_called()
117 self.lcm_tasks.remove.assert_called_with("vca", "id", "order-id")
118
119 def test_vca_lcm_delete(self):
120 vca_content = {"op_id": "order-id", "_id": "id"}
121 order_id = "order-id"
122 self.lcm_tasks.lock_HA.return_value = True
123 self.vca_lcm.update_db_2 = Mock()
124
125 self.loop.run_until_complete(self.vca_lcm.delete(vca_content, order_id))
126
127 self.lcm_tasks.lock_HA.assert_called_with("vca", "delete", "order-id")
128 self.vca_lcm.db.del_one.assert_called_with("vca", {"_id": "id"})
129 self.vca_lcm.update_db_2.assert_called_with("vca", "id", None)
130 self.lcm_tasks.unlock_HA.assert_called_with(
131 "vca",
132 "delete",
133 "order-id",
134 operationState="COMPLETED",
135 detailed_status="deleted",
136 )
137 self.lcm_tasks.remove.assert_called_with("vca", "id", "order-id")
138
139 def test_vca_lcm_delete_exception(self):
140 vca_content = {"op_id": "order-id", "_id": "id"}
141 order_id = "order-id"
142 self.lcm_tasks.lock_HA.return_value = True
143 self.vca_lcm.update_db_2 = Mock()
144 self.vca_lcm.db.del_one.side_effect = Exception("failed deleting")
145 self.vca_lcm.update_db_2.side_effect = DbException("failed")
146
147 self.loop.run_until_complete(self.vca_lcm.delete(vca_content, order_id))
148
149 self.lcm_tasks.lock_HA.assert_called_with("vca", "delete", "order-id")
150 self.vca_lcm.db.del_one.assert_called_with("vca", {"_id": "id"})
151 self.vca_lcm.update_db_2.assert_called_with(
152 "vca",
153 "id",
154 {
155 "_admin.operationalState": "ERROR",
156 "_admin.detailed-status": "Failed with exception: failed deleting",
157 },
158 )
159 self.lcm_tasks.unlock_HA.not_called()
160 self.lcm_tasks.remove.assert_called_with("vca", "id", "order-id")
161
162
163 class TestK8SClusterLcm(TestCase):
164 @patch("osm_lcm.vim_sdn.K8sHelmConnector")
165 @patch("osm_lcm.vim_sdn.K8sHelm3Connector")
166 @patch("osm_lcm.vim_sdn.K8sJujuConnector")
167 @patch("osm_lcm.lcm_utils.Database")
168 @patch("osm_lcm.lcm_utils.Filesystem")
169 def setUp(
170 self,
171 mock_filesystem,
172 mock_database,
173 juju_connector,
174 helm3_connector,
175 helm_connector,
176 ):
177 self.loop = asyncio.get_event_loop()
178 self.msg = Mock(msgbase.MsgBase())
179 self.lcm_tasks = Mock()
180 self.config = {"database": {"driver": "mongo"}}
181 self.vca_config = {
182 "VCA": {
183 "helmpath": "/usr/local/bin/helm",
184 "helm3path": "/usr/local/bin/helm3",
185 "kubectlpath": "/usr/bin/kubectl",
186 }
187 }
188 self.k8scluster_lcm = K8sClusterLcm(
189 self.msg, self.lcm_tasks, self.vca_config, self.loop
190 )
191 self.k8scluster_lcm.db = Mock()
192 self.k8scluster_lcm.fs = Mock()
193
194 def test_k8scluster_edit(self):
195 k8scluster_content = {"op_id": "op-id", "_id": "id"}
196 order_id = "order-id"
197 self.lcm_tasks.lock_HA.return_value = True
198 self.loop.run_until_complete(
199 self.k8scluster_lcm.edit(k8scluster_content, order_id)
200 )
201 self.lcm_tasks.unlock_HA.assert_called_with(
202 "k8scluster",
203 "edit",
204 "op-id",
205 operationState="COMPLETED",
206 detailed_status="Not implemented",
207 )
208 self.lcm_tasks.remove.assert_called_with("k8scluster", "id", order_id)
209
210 def test_k8scluster_edit_lock_false(self):
211 k8scluster_content = {"op_id": "op-id", "_id": "id"}
212 order_id = "order-id"
213 self.lcm_tasks.lock_HA.return_value = False
214 self.loop.run_until_complete(
215 self.k8scluster_lcm.edit(k8scluster_content, order_id)
216 )
217 self.lcm_tasks.unlock_HA.assert_not_called()
218 self.lcm_tasks.remove.assert_not_called()
219
220 def test_k8scluster_edit_no_opid(self):
221 k8scluster_content = {"_id": "id"}
222 order_id = "order-id"
223 self.lcm_tasks.lock_HA.return_value = True
224 self.loop.run_until_complete(
225 self.k8scluster_lcm.edit(k8scluster_content, order_id)
226 )
227 self.lcm_tasks.unlock_HA.assert_called_with(
228 "k8scluster",
229 "edit",
230 None,
231 operationState="COMPLETED",
232 detailed_status="Not implemented",
233 )
234 self.lcm_tasks.remove.assert_called_with("k8scluster", "id", order_id)
235
236 def test_k8scluster_edit_no_orderid(self):
237 k8scluster_content = {"op_id": "op-id", "_id": "id"}
238 order_id = None
239 self.lcm_tasks.lock_HA.return_value = True
240 self.loop.run_until_complete(
241 self.k8scluster_lcm.edit(k8scluster_content, order_id)
242 )
243 self.lcm_tasks.unlock_HA.assert_called_with(
244 "k8scluster",
245 "edit",
246 "op-id",
247 operationState="COMPLETED",
248 detailed_status="Not implemented",
249 )
250 self.lcm_tasks.remove.assert_called_with("k8scluster", "id", order_id)