1 # Copyright 2020 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from unittest
.mock
import Mock
22 from n2vc
.n2vc_juju_conn
import N2VCJujuConnector
23 from osm_common
import fslocal
24 from n2vc
.exceptions
import (
25 JujuK8sProxycharmNotSupported
,
26 N2VCBadArgumentsException
,
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")
39 mock__get_api_endpoints_db
=None,
42 mock_update_endpoints
=None,
43 mock__create_health_check_task
=None,
45 mock__get_api_endpoints_db
.return_value
= ["2.2.2.2:17070"]
46 loop
= asyncio
.get_event_loop()
50 "api_proxy": "api_proxy",
52 "k8s_cloud": "k8s_cloud",
55 logging
.disable(logging
.CRITICAL
)
57 N2VCJujuConnector
.get_public_key
= Mock()
58 self
.n2vc
= N2VCJujuConnector(
65 vca_config
=vca_config
,
68 N2VCJujuConnector
.get_public_key
.assert_not_called()
71 @asynctest.mock
.patch("n2vc.libjuju.Libjuju.get_metrics")
72 class GetMetricssTest(N2VCJujuConnTestCase
):
74 super(GetMetricssTest
, self
).setUp()
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()
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")
86 mock_get_metrics
.assert_called_once()
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
94 @asynctest.mock
.patch("n2vc.libjuju.Libjuju.deploy_charm")
95 @asynctest.mock
.patch("n2vc.libjuju.Libjuju.add_model")
96 class K8sProxyCharmsTest(N2VCJujuConnTestCase
):
98 super(K8sProxyCharmsTest
, self
).setUp()
101 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
, mock_model_exists
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/", {},
112 mock_add_model
.assert_called_once_with(
114 cloud_name
=self
.n2vc
.k8s_cloud
,
115 credential_name
=self
.n2vc
.k8s_cloud
117 mock_deploy_charm
.assert_called_once_with(
118 model_name
="ns-id-k8s",
119 application_name
="app-vnf-vnf-id-vdu-vdu",
123 progress_timeout
=None,
127 self
.assertEqual(ee_id
, "ns-id-k8s.app-vnf-vnf-id-vdu-vdu.k8s")
129 @asynctest.mock
.patch(
130 "n2vc.n2vc_juju_conn.N2VCJujuConnector.k8s_cloud",
131 new_callable
=asynctest
.PropertyMock
,
134 def test_no_k8s_cloud(
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/", {},
150 self
.assertIsNone(ee_id
)
152 def test_no_artifact_path(
153 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
, mock_model_exists
,
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", "", {},
161 self
.assertIsNone(ee_id
)
164 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
, mock_model_exists
,
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,
172 self
.assertIsNone(ee_id
)
174 def test_file_not_exists(
175 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
, mock_model_exists
,
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/", {},
184 self
.assertIsNone(ee_id
)
187 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
, mock_model_exists
,
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/", {},
199 self
.assertIsNone(ee_id
)