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.
19 from n2vc
.n2vc_juju_conn
import N2VCJujuConnector
20 from osm_common
import fslocal
21 from n2vc
.exceptions
import (
22 JujuK8sProxycharmNotSupported
,
23 N2VCBadArgumentsException
,
28 class N2VCJujuConnTestCase(asynctest
.TestCase
):
29 @asynctest.mock
.patch("n2vc.libjuju.Libjuju._create_health_check_task")
30 @asynctest.mock
.patch("juju.controller.Controller.update_endpoints")
31 @asynctest.mock
.patch("juju.client.connector.Connector.connect")
32 @asynctest.mock
.patch("juju.controller.Controller.connection")
33 @asynctest.mock
.patch("n2vc.libjuju.Libjuju._get_api_endpoints_db")
36 mock__get_api_endpoints_db
=None,
39 mock_update_endpoints
=None,
40 mock__create_health_check_task
=None,
42 mock__get_api_endpoints_db
.return_value
= ["2.2.2.2:17070"]
43 loop
= asyncio
.get_event_loop()
47 "api_proxy": "api_proxy",
49 "k8s_cloud": "k8s_cloud",
52 logging
.disable(logging
.CRITICAL
)
54 self
.n2vc
= N2VCJujuConnector(
61 vca_config
=vca_config
,
66 @asynctest.mock
.patch("n2vc.libjuju.Libjuju.get_metrics")
67 class GetMetricssTest(N2VCJujuConnTestCase
):
69 super(GetMetricssTest
, self
).setUp()
71 def test_success(self
, mock_get_metrics
):
72 _
= self
.loop
.run_until_complete(self
.n2vc
.get_metrics("model", "application"))
73 mock_get_metrics
.assert_called_once()
75 def test_except(self
, mock_get_metrics
):
76 mock_get_metrics
.side_effect
= Exception()
77 with self
.assertRaises(Exception):
78 _
= self
.loop
.run_until_complete(
79 self
.n2vc
.get_metrics("model", "application")
81 mock_get_metrics
.assert_called_once()
84 @asynctest.mock
.patch("osm_common.fslocal.FsLocal.file_exists")
85 @asynctest.mock
.patch(
86 "osm_common.fslocal.FsLocal.path", new_callable
=asynctest
.PropertyMock
, create
=True
88 @asynctest.mock
.patch("n2vc.libjuju.Libjuju.deploy_charm")
89 @asynctest.mock
.patch("n2vc.libjuju.Libjuju.add_model")
90 class K8sProxyCharmsTest(N2VCJujuConnTestCase
):
92 super(K8sProxyCharmsTest
, self
).setUp()
95 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
,
97 mock_file_exists
.return_value
= True
98 mock_path
.return_value
= "/path"
99 ee_id
= self
.loop
.run_until_complete(
100 self
.n2vc
.install_k8s_proxy_charm(
101 "charm", "nsi-id.ns-id.vnf-id.vdu", "////path/", {},
105 mock_add_model
.assert_called_once_with(
107 cloud_name
=self
.n2vc
.k8s_cloud
,
108 credential_name
=self
.n2vc
.k8s_cloud
110 mock_deploy_charm
.assert_called_once_with(
111 model_name
="ns-id-k8s",
112 application_name
="app-vnf-vnf-id-vdu-vdu",
116 progress_timeout
=None,
120 self
.assertEqual(ee_id
, "ns-id-k8s.app-vnf-vnf-id-vdu-vdu.k8s")
122 @asynctest.mock
.patch(
123 "n2vc.n2vc_juju_conn.N2VCJujuConnector.k8s_cloud",
124 new_callable
=asynctest
.PropertyMock
,
127 def test_no_k8s_cloud(
135 mock_k8s_cloud
.return_value
= None
136 with self
.assertRaises(JujuK8sProxycharmNotSupported
):
137 ee_id
= self
.loop
.run_until_complete(
138 self
.n2vc
.install_k8s_proxy_charm(
139 "charm", "nsi-id.ns-id.vnf-id.vdu", "/path/", {},
142 self
.assertIsNone(ee_id
)
144 def test_no_artifact_path(
145 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
,
147 with self
.assertRaises(N2VCBadArgumentsException
):
148 ee_id
= self
.loop
.run_until_complete(
149 self
.n2vc
.install_k8s_proxy_charm(
150 "charm", "nsi-id.ns-id.vnf-id.vdu", "", {},
153 self
.assertIsNone(ee_id
)
156 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
,
158 with self
.assertRaises(N2VCBadArgumentsException
):
159 ee_id
= self
.loop
.run_until_complete(
160 self
.n2vc
.install_k8s_proxy_charm(
161 "charm", "nsi-id.ns-id.vnf-id.vdu", "/path/", None,
164 self
.assertIsNone(ee_id
)
166 def test_file_not_exists(
167 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
,
169 mock_file_exists
.return_value
= False
170 with self
.assertRaises(N2VCBadArgumentsException
):
171 ee_id
= self
.loop
.run_until_complete(
172 self
.n2vc
.install_k8s_proxy_charm(
173 "charm", "nsi-id.ns-id.vnf-id.vdu", "/path/", {},
176 self
.assertIsNone(ee_id
)
179 self
, mock_add_model
, mock_deploy_charm
, mock_path
, mock_file_exists
,
181 mock_file_exists
.return_value
= True
182 mock_path
.return_value
= "/path"
183 mock_deploy_charm
.side_effect
= Exception()
184 with self
.assertRaises(N2VCException
):
185 ee_id
= self
.loop
.run_until_complete(
186 self
.n2vc
.install_k8s_proxy_charm(
187 "charm", "nsi-id.ns-id.vnf-id.vdu", "path/", {},
190 self
.assertIsNone(ee_id
)