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