Fix bug 1505
[osm/N2VC.git] / n2vc / tests / unit / test_n2vc_juju_conn.py
1 # Copyright 2020 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
16 import asyncio
17 import logging
18 from unittest.mock import Mock
19
20
21 import asynctest
22 from n2vc.n2vc_juju_conn import N2VCJujuConnector
23 from osm_common import fslocal
24 from n2vc.exceptions import (
25 JujuK8sProxycharmNotSupported,
26 N2VCBadArgumentsException,
27 N2VCException,
28 )
29
30
31 class N2VCJujuConnTestCase(asynctest.TestCase):
32 @asynctest.mock.patch("n2vc.libjuju.Libjuju._create_health_check_task")
33 @asynctest.mock.patch("juju.controller.Controller.update_endpoints")
34 @asynctest.mock.patch("juju.client.connector.Connector.connect")
35 @asynctest.mock.patch("juju.controller.Controller.connection")
36 @asynctest.mock.patch("n2vc.libjuju.Libjuju._get_api_endpoints_db")
37 def setUp(
38 self,
39 mock__get_api_endpoints_db=None,
40 mock_connection=None,
41 mock_connect=None,
42 mock_update_endpoints=None,
43 mock__create_health_check_task=None,
44 ):
45 mock__get_api_endpoints_db.return_value = ["2.2.2.2:17070"]
46 loop = asyncio.get_event_loop()
47 db = {}
48 vca_config = {
49 "secret": "secret",
50 "api_proxy": "api_proxy",
51 "cloud": "cloud",
52 "k8s_cloud": "k8s_cloud",
53 }
54
55 logging.disable(logging.CRITICAL)
56
57 N2VCJujuConnector.get_public_key = Mock()
58 self.n2vc = N2VCJujuConnector(
59 db=db,
60 fs=fslocal.FsLocal(),
61 log=None,
62 loop=loop,
63 url="2.2.2.2:17070",
64 username="admin",
65 vca_config=vca_config,
66 on_update_db=None,
67 )
68 N2VCJujuConnector.get_public_key.assert_not_called()
69
70
71 @asynctest.mock.patch("n2vc.libjuju.Libjuju.get_metrics")
72 class GetMetricssTest(N2VCJujuConnTestCase):
73 def setUp(self):
74 super(GetMetricssTest, self).setUp()
75
76 def test_success(self, mock_get_metrics):
77 _ = self.loop.run_until_complete(self.n2vc.get_metrics("model", "application"))
78 mock_get_metrics.assert_called_once()
79
80 def test_except(self, mock_get_metrics):
81 mock_get_metrics.side_effect = Exception()
82 with self.assertRaises(Exception):
83 _ = self.loop.run_until_complete(
84 self.n2vc.get_metrics("model", "application")
85 )
86 mock_get_metrics.assert_called_once()
87
88
89 @asynctest.mock.patch("n2vc.libjuju.Libjuju.model_exists")
90 @asynctest.mock.patch("osm_common.fslocal.FsLocal.file_exists")
91 @asynctest.mock.patch(
92 "osm_common.fslocal.FsLocal.path", new_callable=asynctest.PropertyMock, create=True
93 )
94 @asynctest.mock.patch("n2vc.libjuju.Libjuju.deploy_charm")
95 @asynctest.mock.patch("n2vc.libjuju.Libjuju.add_model")
96 class K8sProxyCharmsTest(N2VCJujuConnTestCase):
97 def setUp(self):
98 super(K8sProxyCharmsTest, self).setUp()
99
100 def test_success(
101 self, mock_add_model, mock_deploy_charm, mock_path, mock_file_exists, mock_model_exists
102 ):
103 mock_model_exists.return_value = None
104 mock_file_exists.return_value = True
105 mock_path.return_value = "/path"
106 ee_id = self.loop.run_until_complete(
107 self.n2vc.install_k8s_proxy_charm(
108 "charm", "nsi-id.ns-id.vnf-id.vdu", "////path/", {},
109 )
110 )
111
112 mock_add_model.assert_called_once_with(
113 "ns-id-k8s",
114 cloud_name=self.n2vc.k8s_cloud,
115 credential_name=self.n2vc.k8s_cloud
116 )
117 mock_deploy_charm.assert_called_once_with(
118 model_name="ns-id-k8s",
119 application_name="app-vnf-vnf-id-vdu-vdu",
120 path="/path/path/",
121 machine_id=None,
122 db_dict={},
123 progress_timeout=None,
124 total_timeout=None,
125 config=None,
126 )
127 self.assertEqual(ee_id, "ns-id-k8s.app-vnf-vnf-id-vdu-vdu.k8s")
128
129 @asynctest.mock.patch(
130 "n2vc.n2vc_juju_conn.N2VCJujuConnector.k8s_cloud",
131 new_callable=asynctest.PropertyMock,
132 create=True,
133 )
134 def test_no_k8s_cloud(
135 self,
136 mock_k8s_cloud,
137 mock_add_model,
138 mock_deploy_charm,
139 mock_path,
140 mock_file_exists,
141 mock_model_exists,
142 ):
143 mock_k8s_cloud.return_value = None
144 with self.assertRaises(JujuK8sProxycharmNotSupported):
145 ee_id = self.loop.run_until_complete(
146 self.n2vc.install_k8s_proxy_charm(
147 "charm", "nsi-id.ns-id.vnf-id.vdu", "/path/", {},
148 )
149 )
150 self.assertIsNone(ee_id)
151
152 def test_no_artifact_path(
153 self, mock_add_model, mock_deploy_charm, mock_path, mock_file_exists, mock_model_exists,
154 ):
155 with self.assertRaises(N2VCBadArgumentsException):
156 ee_id = self.loop.run_until_complete(
157 self.n2vc.install_k8s_proxy_charm(
158 "charm", "nsi-id.ns-id.vnf-id.vdu", "", {},
159 )
160 )
161 self.assertIsNone(ee_id)
162
163 def test_no_db(
164 self, mock_add_model, mock_deploy_charm, mock_path, mock_file_exists, mock_model_exists,
165 ):
166 with self.assertRaises(N2VCBadArgumentsException):
167 ee_id = self.loop.run_until_complete(
168 self.n2vc.install_k8s_proxy_charm(
169 "charm", "nsi-id.ns-id.vnf-id.vdu", "/path/", None,
170 )
171 )
172 self.assertIsNone(ee_id)
173
174 def test_file_not_exists(
175 self, mock_add_model, mock_deploy_charm, mock_path, mock_file_exists, mock_model_exists,
176 ):
177 mock_file_exists.return_value = False
178 with self.assertRaises(N2VCBadArgumentsException):
179 ee_id = self.loop.run_until_complete(
180 self.n2vc.install_k8s_proxy_charm(
181 "charm", "nsi-id.ns-id.vnf-id.vdu", "/path/", {},
182 )
183 )
184 self.assertIsNone(ee_id)
185
186 def test_exception(
187 self, mock_add_model, mock_deploy_charm, mock_path, mock_file_exists, mock_model_exists,
188 ):
189 mock_model_exists.return_value = None
190 mock_file_exists.return_value = True
191 mock_path.return_value = "/path"
192 mock_deploy_charm.side_effect = Exception()
193 with self.assertRaises(N2VCException):
194 ee_id = self.loop.run_until_complete(
195 self.n2vc.install_k8s_proxy_charm(
196 "charm", "nsi-id.ns-id.vnf-id.vdu", "path/", {},
197 )
198 )
199 self.assertIsNone(ee_id)