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
, MagicMock
19 from unittest
.mock
import patch
23 from n2vc
.definitions
import Offer
, RelationEndpoint
24 from n2vc
.n2vc_juju_conn
import N2VCJujuConnector
25 from osm_common
import fslocal
26 from osm_common
.dbmemory
import DbMemory
27 from n2vc
.exceptions
import (
28 N2VCBadArgumentsException
,
30 JujuApplicationNotFound
,
32 from n2vc
.tests
.unit
.utils
import AsyncMock
33 from n2vc
.vca
.connection_data
import ConnectionData
34 from n2vc
.tests
.unit
.testdata
import test_db_descriptors
as descriptors
38 class N2VCJujuConnTestCase(asynctest
.TestCase
):
39 @asynctest.mock
.patch("n2vc.n2vc_juju_conn.MotorStore")
40 @asynctest.mock
.patch("n2vc.n2vc_juju_conn.get_connection")
41 @asynctest.mock
.patch("n2vc.vca.connection_data.base64_to_cacert")
43 self
, mock_base64_to_cacert
=None, mock_get_connection
=None, mock_store
=None
45 self
.loop
= asyncio
.get_event_loop()
47 mock_base64_to_cacert
.return_value
= """
48 -----BEGIN CERTIFICATE-----
50 -----END CERTIFICATE-----"""
51 mock_store
.return_value
= AsyncMock()
52 mock_vca_connection
= Mock()
53 mock_get_connection
.return_value
= mock_vca_connection
54 mock_vca_connection
.data
.return_value
= ConnectionData(
56 "endpoints": ["1.2.3.4:17070"],
62 "lxd-credentials": "credentials",
63 "k8s-cloud": "k8s_cloud",
64 "k8s-credentials": "k8s_credentials",
66 "api-proxy": "api_proxy",
69 logging
.disable(logging
.CRITICAL
)
71 N2VCJujuConnector
.get_public_key
= Mock()
72 self
.n2vc
= N2VCJujuConnector(
79 N2VCJujuConnector
.get_public_key
.assert_not_called()
80 self
.n2vc
.libjuju
= Mock()
83 class GetMetricssTest(N2VCJujuConnTestCase
):
85 super(GetMetricssTest
, self
).setUp()
86 self
.n2vc
.libjuju
.get_metrics
= AsyncMock()
88 def test_success(self
):
89 _
= self
.loop
.run_until_complete(self
.n2vc
.get_metrics("model", "application"))
90 self
.n2vc
.libjuju
.get_metrics
.assert_called_once()
92 def test_except(self
):
93 self
.n2vc
.libjuju
.get_metrics
.side_effect
= Exception()
94 with self
.assertRaises(Exception):
95 _
= self
.loop
.run_until_complete(
96 self
.n2vc
.get_metrics("model", "application")
98 self
.n2vc
.libjuju
.get_metrics
.assert_called_once()
101 class UpdateVcaStatusTest(N2VCJujuConnTestCase
):
103 super(UpdateVcaStatusTest
, self
).setUp()
104 self
.n2vc
.libjuju
.get_controller
= AsyncMock()
105 self
.n2vc
.libjuju
.get_model
= AsyncMock()
106 self
.n2vc
.libjuju
.get_executed_actions
= AsyncMock()
107 self
.n2vc
.libjuju
.get_actions
= AsyncMock()
108 self
.n2vc
.libjuju
.get_application_configs
= AsyncMock()
109 self
.n2vc
.libjuju
._get
_application
= AsyncMock()
114 self
.loop
.run_until_complete(
115 self
.n2vc
.update_vca_status(
116 {"model": {"applications": {"app": {"actions": {}}}}}
119 self
.n2vc
.libjuju
.get_executed_actions
.assert_called_once()
120 self
.n2vc
.libjuju
.get_actions
.assert_called_once()
121 self
.n2vc
.libjuju
.get_application_configs
.assert_called_once()
123 def test_exception(self
):
124 self
.n2vc
.libjuju
.get_model
.return_value
= None
125 self
.n2vc
.libjuju
.get_executed_actions
.side_effect
= Exception()
126 with self
.assertRaises(Exception):
127 self
.loop
.run_until_complete(
128 self
.n2vc
.update_vca_status(
129 {"model": {"applications": {"app": {"actions": {}}}}}
132 self
.n2vc
.libjuju
.get_executed_actions
.assert_not_called()
133 self
.n2vc
.libjuju
.get_actions
.assert_not_called_once()
134 self
.n2vc
.libjuju
.get_application_configs
.assert_not_called_once()
137 class K8sProxyCharmsTest(N2VCJujuConnTestCase
):
139 super(K8sProxyCharmsTest
, self
).setUp()
140 self
.n2vc
.libjuju
.model_exists
= AsyncMock()
141 self
.n2vc
.libjuju
.add_model
= AsyncMock()
142 self
.n2vc
.libjuju
.deploy_charm
= AsyncMock()
143 self
.n2vc
.libjuju
.model_exists
.return_value
= False
145 self
.fs
= fslocal
.FsLocal()
147 self
.n2vc
.fs
= self
.fs
148 self
.n2vc
.db
= self
.db
149 self
.db
.create_list("nsrs", yaml
.safe_load(descriptors
.db_nsrs_text
))
150 self
.db
.create_list("vnfrs", yaml
.safe_load(descriptors
.db_vnfrs_text
))
153 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
154 **{"return_value": "random"}
156 def test_success(self
, mock_generate_random_alfanum_string
):
157 self
.n2vc
.fs
.file_exists
= MagicMock(create_autospec
=True)
158 self
.n2vc
.fs
.file_exists
.return_value
= True
159 ee_id
= self
.loop
.run_until_complete(
160 self
.n2vc
.install_k8s_proxy_charm(
162 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
168 self
.n2vc
.libjuju
.add_model
.assert_called_once()
169 self
.n2vc
.libjuju
.deploy_charm
.assert_called_once_with(
170 model_name
="dbfbd751-3de4-4e68-bd40-ec5ae0a53898-k8s",
171 application_name
="simple-ee-z0-vnf1-vnf",
175 progress_timeout
=None,
180 ee_id
, "dbfbd751-3de4-4e68-bd40-ec5ae0a53898-k8s.simple-ee-z0-vnf1-vnf.k8s"
183 def test_no_artifact_path(
186 with self
.assertRaises(N2VCBadArgumentsException
):
187 ee_id
= self
.loop
.run_until_complete(
188 self
.n2vc
.install_k8s_proxy_charm(
190 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
195 self
.assertIsNone(ee_id
)
200 with self
.assertRaises(N2VCBadArgumentsException
):
201 ee_id
= self
.loop
.run_until_complete(
202 self
.n2vc
.install_k8s_proxy_charm(
204 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
209 self
.assertIsNone(ee_id
)
211 def test_file_not_exists(
214 self
.n2vc
.fs
.file_exists
= MagicMock(create_autospec
=True)
215 self
.n2vc
.fs
.file_exists
.return_value
= False
216 with self
.assertRaises(N2VCBadArgumentsException
):
217 ee_id
= self
.loop
.run_until_complete(
218 self
.n2vc
.install_k8s_proxy_charm(
220 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
225 self
.assertIsNone(ee_id
)
230 self
.n2vc
.fs
.file_exists
= MagicMock(create_autospec
=True)
231 self
.n2vc
.fs
.file_exists
.return_value
= True
232 self
.n2vc
.fs
.path
= MagicMock(create_autospec
=True)
233 self
.n2vc
.fs
.path
.return_value
= "path"
234 self
.n2vc
.libjuju
.deploy_charm
.side_effect
= Exception()
235 with self
.assertRaises(N2VCException
):
236 ee_id
= self
.loop
.run_until_complete(
237 self
.n2vc
.install_k8s_proxy_charm(
239 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
244 self
.assertIsNone(ee_id
)
247 class AddRelationTest(N2VCJujuConnTestCase
):
249 super(AddRelationTest
, self
).setUp()
250 self
.n2vc
.libjuju
.add_relation
= AsyncMock()
251 self
.n2vc
.libjuju
.offer
= AsyncMock()
252 self
.n2vc
.libjuju
.get_controller
= AsyncMock()
253 self
.n2vc
.libjuju
.consume
= AsyncMock()
255 def test_standard_relation_same_model_and_controller(self
):
256 relation_endpoint_1
= RelationEndpoint("model-1.app1.0", None, "endpoint1")
257 relation_endpoint_2
= RelationEndpoint("model-1.app2.1", None, "endpoint2")
258 self
.loop
.run_until_complete(
259 self
.n2vc
.add_relation(relation_endpoint_1
, relation_endpoint_2
)
261 self
.n2vc
.libjuju
.add_relation
.assert_called_once_with(
262 model_name
="model-1",
263 endpoint_1
="app1:endpoint1",
264 endpoint_2
="app2:endpoint2",
266 self
.n2vc
.libjuju
.offer
.assert_not_called()
267 self
.n2vc
.libjuju
.consume
.assert_not_called()
269 def test_cmr_relation_same_controller(self
):
270 relation_endpoint_1
= RelationEndpoint("model-1.app1.0", None, "endpoint")
271 relation_endpoint_2
= RelationEndpoint("model-2.app2.1", None, "endpoint")
272 offer
= Offer("admin/model-1.app1")
273 self
.n2vc
.libjuju
.offer
.return_value
= offer
274 self
.n2vc
.libjuju
.consume
.return_value
= "saas"
275 self
.loop
.run_until_complete(
276 self
.n2vc
.add_relation(relation_endpoint_1
, relation_endpoint_2
)
278 self
.n2vc
.libjuju
.offer
.assert_called_once_with(relation_endpoint_1
)
279 self
.n2vc
.libjuju
.consume
.assert_called_once()
280 self
.n2vc
.libjuju
.add_relation
.assert_called_once_with(
281 "model-2", "app2:endpoint", "saas"
284 def test_cmr_relation_different_controller(self
):
285 self
.n2vc
._get
_libjuju
= AsyncMock(return_value
=self
.n2vc
.libjuju
)
286 relation_endpoint_1
= RelationEndpoint(
287 "model-1.app1.0", "vca-id-1", "endpoint1"
289 relation_endpoint_2
= RelationEndpoint(
290 "model-1.app2.1", "vca-id-2", "endpoint2"
292 offer
= Offer("admin/model-1.app1")
293 self
.n2vc
.libjuju
.offer
.return_value
= offer
294 self
.n2vc
.libjuju
.consume
.return_value
= "saas"
295 self
.loop
.run_until_complete(
296 self
.n2vc
.add_relation(relation_endpoint_1
, relation_endpoint_2
)
298 self
.n2vc
.libjuju
.offer
.assert_called_once_with(relation_endpoint_1
)
299 self
.n2vc
.libjuju
.consume
.assert_called_once()
300 self
.n2vc
.libjuju
.add_relation
.assert_called_once_with(
301 "model-1", "app2:endpoint2", "saas"
304 def test_relation_exception(self
):
305 relation_endpoint_1
= RelationEndpoint("model-1.app1.0", None, "endpoint")
306 relation_endpoint_2
= RelationEndpoint("model-2.app2.1", None, "endpoint")
307 self
.n2vc
.libjuju
.offer
.side_effect
= Exception()
308 with self
.assertRaises(N2VCException
):
309 self
.loop
.run_until_complete(
310 self
.n2vc
.add_relation(relation_endpoint_1
, relation_endpoint_2
)
314 class UpgradeCharmTest(N2VCJujuConnTestCase
):
316 super(UpgradeCharmTest
, self
).setUp()
317 self
.n2vc
._get
_libjuju
= AsyncMock(return_value
=self
.n2vc
.libjuju
)
318 N2VCJujuConnector
._get
_ee
_id
_components
= Mock()
319 self
.n2vc
.libjuju
.upgrade_charm
= AsyncMock()
321 def test_empty_ee_id(self
):
322 with self
.assertRaises(N2VCBadArgumentsException
):
323 self
.loop
.run_until_complete(
324 self
.n2vc
.upgrade_charm(
325 "", "/sample_charm_path", "sample_charm_id", "native-charm", None
328 self
.n2vc
._get
_libjuju
.assert_called()
329 self
.n2vc
._get
_ee
_id
_components
.assert_not_called()
330 self
.n2vc
.libjuju
.upgrade_charm
.assert_not_called()
332 def test_wrong_ee_id(self
):
333 N2VCJujuConnector
._get
_ee
_id
_components
.side_effect
= Exception
334 with self
.assertRaises(N2VCBadArgumentsException
):
335 self
.loop
.run_until_complete(
336 self
.n2vc
.upgrade_charm(
337 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
338 "/sample_charm_path",
344 self
.n2vc
._get
_libjuju
.assert_called()
345 self
.n2vc
._get
_ee
_id
_components
.assert_called()
346 self
.n2vc
.libjuju
.upgrade_charm
.assert_not_called()
348 def test_charm_upgrade_succeded(self
):
349 N2VCJujuConnector
._get
_ee
_id
_components
.return_value
= (
354 self
.loop
.run_until_complete(
355 self
.n2vc
.upgrade_charm(
356 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
357 "/sample_charm_path",
363 self
.n2vc
._get
_libjuju
.assert_called()
364 self
.n2vc
._get
_ee
_id
_components
.assert_called()
365 self
.n2vc
.libjuju
.upgrade_charm
.assert_called_with(
366 application_name
="sample_app",
367 path
="/sample_charm_path",
368 model_name
="sample_model",
372 def test_charm_upgrade_failed(self
):
373 N2VCJujuConnector
._get
_ee
_id
_components
.return_value
= (
378 self
.n2vc
.libjuju
.upgrade_charm
.side_effect
= JujuApplicationNotFound
379 with self
.assertRaises(N2VCException
):
380 self
.loop
.run_until_complete(
381 self
.n2vc
.upgrade_charm(
382 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
383 "/sample_charm_path",
389 self
.n2vc
._get
_libjuju
.assert_called()
390 self
.n2vc
._get
_ee
_id
_components
.assert_called()
391 self
.n2vc
.libjuju
.upgrade_charm
.assert_called_with(
392 application_name
="sample_app",
393 path
="/sample_charm_path",
394 model_name
="sample_model",
399 class GenerateApplicationNameTest(N2VCJujuConnTestCase
):
400 vnf_id
= "dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
403 super(GenerateApplicationNameTest
, self
).setUp()
404 self
.db
= MagicMock(DbMemory
)
407 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
408 **{"return_value": "random"}
410 def test_generate_backward_compatible_application_name(
411 self
, mock_generate_random_alfanum
415 expected_result
= "app-vnf-ec5ae0a53898-vdu-mgmtVM-cnt-0-random"
417 application_name
= self
.n2vc
._generate
_backward
_compatible
_application
_name
(
418 GenerateApplicationNameTest
.vnf_id
, vdu_id
, vdu_count
420 self
.assertEqual(application_name
, expected_result
)
423 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
424 **{"return_value": "random"}
426 def test_generate_backward_compatible_application_name_without_vnf_id_vdu_id(
427 self
, mock_generate_random_alfanum
432 expected_result
= "app--random"
433 application_name
= self
.n2vc
._generate
_backward
_compatible
_application
_name
(
434 vnf_id
, vdu_id
, vdu_count
437 self
.assertEqual(application_name
, expected_result
)
438 self
.assertLess(len(application_name
), 50)
440 def test_find_charm_level_with_vnf_id(self
):
442 expected_result
= "vnf-level"
443 charm_level
= self
.n2vc
._find
_charm
_level
(
444 GenerateApplicationNameTest
.vnf_id
, vdu_id
446 self
.assertEqual(charm_level
, expected_result
)
448 def test_find_charm_level_with_vdu_id(self
):
451 with self
.assertRaises(N2VCException
):
452 self
.n2vc
._find
_charm
_level
(vnf_id
, vdu_id
)
454 def test_find_charm_level_with_vnf_id_and_vdu_id(self
):
456 expected_result
= "vdu-level"
457 charm_level
= self
.n2vc
._find
_charm
_level
(
458 GenerateApplicationNameTest
.vnf_id
, vdu_id
460 self
.assertEqual(charm_level
, expected_result
)
462 def test_find_charm_level_without_vnf_id_and_vdu_id(self
):
465 expected_result
= "ns-level"
466 charm_level
= self
.n2vc
._find
_charm
_level
(vnf_id
, vdu_id
)
467 self
.assertEqual(charm_level
, expected_result
)
469 def test_generate_application_name_ns_charm(self
):
470 charm_level
= "ns-level"
474 "target_element": "ns",
475 "member-vnf-index": "",
478 "vdu_count_index": None,
481 "type": "proxy_charm",
482 "ee_descriptor_id": None,
483 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh",
486 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
492 expected_result
= "simple-ns-charm-abc-000-rrrr-nnnn-4444-h-ns"
493 application_name
= self
.n2vc
._generate
_application
_name
(
501 self
.assertEqual(application_name
, expected_result
)
502 self
.assertLess(len(application_name
), 50)
504 def test_generate_application_name_ns_charm_empty_vca_records(self
):
505 charm_level
= "ns-level"
511 with self
.assertRaises(N2VCException
):
512 self
.n2vc
._generate
_application
_name
(
521 def test_generate_application_name_vnf_charm(self
):
522 charm_level
= "vnf-level"
523 vnfrs
= {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
526 "target_element": "vnf/vnf1",
527 "member-vnf-index": "vnf111-xxx-yyy-zzz",
528 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
529 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
531 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
537 expected_result
= "simple-ee-ab-1-vnf111-xxx-y-vnf"
538 application_name
= self
.n2vc
._generate
_application
_name
(
546 self
.assertEqual(application_name
, expected_result
)
547 self
.assertLess(len(application_name
), 50)
549 def test_generate_application_name_vdu_charm_kdu_name_in_vca_record_is_none(self
):
550 charm_level
= "vdu-level"
552 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
554 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
555 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
560 "target_element": "vnf/vnf1/mgmtvm",
561 "member-vnf-index": "vnf111-xxx-yyy-zzz",
564 "vdu_count_index": None,
565 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
566 "vdu_name": "mgmtvm",
567 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
569 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
572 "target_element": "vnf/vnf1/dataVM",
573 "member-vnf-index": "vnf111-xxx-yyy-zzz",
576 "vdu_count_index": None,
577 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
578 "vdu_name": "datavm",
579 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
581 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
587 expected_result
= "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
588 application_name
= self
.n2vc
._generate
_application
_name
(
596 self
.assertEqual(application_name
, expected_result
)
597 self
.assertLess(len(application_name
), 50)
599 def test_generate_application_name_vdu_charm_vdu_id_kdu_name_in_vca_record_are_both_set(
602 charm_level
= "vdu-level"
604 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
606 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
607 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
612 "target_element": "vnf/vnf1/mgmtVM",
613 "member-vnf-index": "vnf111-xxx-yyy-zzz",
615 "kdu_name": "mgmtVM",
616 "vdu_count_index": None,
617 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
618 "vdu_name": "mgmtvm",
619 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
621 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
624 "target_element": "vnf/vnf1/dataVM",
625 "member-vnf-index": "vnf111-xxx-yyy-zzz",
628 "vdu_count_index": None,
629 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
630 "vdu_name": "datavm",
631 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
633 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
639 expected_result
= "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
640 application_name
= self
.n2vc
._generate
_application
_name
(
648 self
.assertEqual(application_name
, expected_result
)
649 self
.assertLess(len(application_name
), 50)
651 def test_generate_application_name_vdu_charm_both_vdu_id_kdu_name_in_vca_record_are_none(
654 charm_level
= "vdu-level"
655 vnfrs
= {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
658 "target_element": "vnf/vnf1/mgmtVM",
659 "member-vnf-index": "vnf111-xxx-yyy-zzz",
662 "vdu_count_index": None,
663 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
664 "vdu_name": "mgmtvm",
665 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
667 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
673 with self
.assertRaises(KeyError):
674 self
.n2vc
._generate
_application
_name
(
683 def test_generate_application_name_vdu_charm_given_vdu_id_is_none(self
):
684 charm_level
= "vdu-level"
685 vnfrs
= {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
688 "target_element": "vnf/vnf1/mgmtvVM",
689 "member-vnf-index": "vnf111-xxx-yyy-zzz",
691 "kdu_name": "mgmtVM",
692 "vdu_count_index": None,
693 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
694 "vdu_name": "mgmtvm",
695 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
697 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
703 with self
.assertRaises(N2VCException
):
704 self
.n2vc
._generate
_application
_name
(
713 def test_generate_application_name_vdu_charm_vdu_id_does_not_match_with_the_key_in_vca_record(
716 charm_level
= "vdu-level"
717 vnfrs
= {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
720 "target_element": "vnf/vnf1/mgmtVM",
721 "member-vnf-index": "vnf111-xxx-yyy-zzz",
723 "kdu_name": "mgmtVM",
724 "vdu_count_index": None,
725 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
726 "vdu_name": "mgmtvm",
727 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
729 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
735 with self
.assertRaises(KeyError):
736 self
.n2vc
._generate
_application
_name
(
745 def test_generate_application_name_vdu_charm_vdu_id_in_vca_record_is_none(self
):
746 charm_level
= "vdu-level"
747 vnfrs
= {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
750 "target_element": "vnf/vnf1/mgmtVM",
751 "member-vnf-index": "vnf111-xxx-yyy-zzz",
753 "kdu_name": "mgmtVM",
754 "vdu_count_index": None,
755 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
756 "vdu_name": "mgmtvm",
757 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
759 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
765 expected_result
= "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
766 application_name
= self
.n2vc
._generate
_application
_name
(
774 self
.assertEqual(application_name
, expected_result
)
775 self
.assertLess(len(application_name
), 50)
777 def test_get_vnf_count_db_vnfr_ns_charm(self
):
778 self
.db
.get_one
.return_value
= {"member-vnf-index-ref": "sample-ref"}
779 charm_level
= "ns-level"
780 vnf_id_and_count
= "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
781 with patch
.object(self
.n2vc
, "db", self
.db
):
782 vnf_count
, db_vnfr
= self
.n2vc
._get
_vnf
_count
_and
_record
(
783 charm_level
, vnf_id_and_count
785 self
.assertEqual(vnf_count
, "")
786 self
.assertEqual(db_vnfr
, {})
788 def test_get_vnf_count_db_vnfr_vnf_charm(self
):
789 self
.db
.get_one
.return_value
= {"member-vnf-index-ref": "sample-ref"}
790 charm_level
= "vnf-level"
791 vnf_id_and_count
= "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
792 with patch
.object(self
.n2vc
, "db", self
.db
):
793 vnf_count
, db_vnfr
= self
.n2vc
._get
_vnf
_count
_and
_record
(
794 charm_level
, vnf_id_and_count
796 self
.assertEqual(vnf_count
, "4")
797 self
.assertEqual(db_vnfr
, {"member-vnf-index-ref": "sample-ref"})
799 def test_get_vnf_count_db_vnfr_vdu_charm(self
):
800 self
.db
.get_one
.return_value
= {"member-vnf-index-ref": "sample-ref"}
801 charm_level
= "vdu-level"
802 vnf_id_and_count
= "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-2"
803 with patch
.object(self
.n2vc
, "db", self
.db
):
804 vnf_count
, db_vnfr
= self
.n2vc
._get
_vnf
_count
_and
_record
(
805 charm_level
, vnf_id_and_count
807 self
.assertEqual(vnf_count
, "2")
808 self
.assertEqual(db_vnfr
, {"member-vnf-index-ref": "sample-ref"})
810 def test_get_vca_records_vdu_charm(self
):
811 charm_level
= "vdu-level"
813 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
815 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
816 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
824 "target_element": "vnf/vnf1/mgmtvm",
825 "member-vnf-index": "vnf111-xxx-yyy-zzz",
826 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
828 "vdu_count_index": None,
829 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
830 "vdu_name": "mgmtvm",
831 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
833 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
836 "target_element": "vnf/vnf2/datavm",
837 "member-vnf-index": "vnf222-xxx-yyy-zzz",
838 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
840 "vdu_count_index": None,
841 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
842 "vdu_name": "datavm",
843 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
845 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
853 "target_element": "vnf/vnf1/mgmtvm",
854 "member-vnf-index": "vnf111-xxx-yyy-zzz",
855 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
857 "vdu_count_index": None,
858 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
859 "vdu_name": "mgmtvm",
860 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
862 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
865 vca_records
= self
.n2vc
._get
_vca
_records
(charm_level
, db_nsr
, db_vnfr
)
866 self
.assertEqual(vca_records
, expected_result
)
868 def test_get_vca_records_vnf_charm_member_vnf_index_mismatch(self
):
869 charm_level
= "vnf-level"
870 db_vnfr
= {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
876 "target_element": "vnf/vnf1/mgmtvm",
877 "member-vnf-index": "vnf111-xxx-yyy-zzz",
878 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
880 "vdu_count_index": None,
881 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
882 "vdu_name": "mgmtvm",
883 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
885 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
888 "target_element": "vnf/vnf1/mgmtvm",
889 "member-vnf-index": "vnf111-xxx-yyy-zzz",
890 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
892 "vdu_count_index": None,
893 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
894 "vdu_name": "datavm",
895 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
897 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
904 vca_records
= self
.n2vc
._get
_vca
_records
(charm_level
, db_nsr
, db_vnfr
)
905 self
.assertEqual(vca_records
, expected_result
)
907 def test_get_vca_records_ns_charm(self
):
908 charm_level
= "ns-level"
909 db_vnfr
= {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
915 "target_element": "vnf/vnf1/mgmtvm",
916 "member-vnf-index": "vnf111-xxx-yyy-zzz",
917 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
919 "vdu_count_index": None,
920 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
921 "vdu_name": "mgmtvm",
922 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
924 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
927 "target_element": "ns",
928 "member-vnf-index": None,
931 "vdu_count_index": None,
934 "ee_descriptor_id": "",
935 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
936 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
944 "target_element": "ns",
945 "member-vnf-index": None,
948 "vdu_count_index": None,
951 "ee_descriptor_id": "",
952 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
953 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
956 vca_records
= self
.n2vc
._get
_vca
_records
(charm_level
, db_nsr
, db_vnfr
)
957 self
.assertEqual(vca_records
, expected_result
)
959 def test_get_vca_records_ns_charm_empty_charm_name(self
):
960 charm_level
= "ns-level"
961 db_vnfr
= {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
967 "target_element": "vnf/vnf1/mgmtvm",
968 "member-vnf-index": "vnf111-xxx-yyy-zzz",
969 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
971 "vdu_count_index": None,
972 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
973 "vdu_name": "mgmtvm",
974 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
976 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
979 "target_element": "ns",
980 "member-vnf-index": None,
983 "vdu_count_index": None,
986 "ee_descriptor_id": "",
988 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
996 "target_element": "ns",
997 "member-vnf-index": None,
1000 "vdu_count_index": None,
1003 "ee_descriptor_id": "",
1005 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1008 vca_records
= self
.n2vc
._get
_vca
_records
(charm_level
, db_nsr
, db_vnfr
)
1009 self
.assertEqual(vca_records
, expected_result
)
1011 def test_get_application_name_vnf_charm(self
):
1012 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1013 self
.db
.get_one
.return_value
= {
1018 "target_element": "vnf/vnf1/mgmtvm",
1019 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1020 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1022 "vdu_count_index": None,
1023 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1024 "vdu_name": "mgmtvm",
1025 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1027 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1030 "target_element": "ns",
1031 "member-vnf-index": None,
1034 "vdu_count_index": None,
1037 "ee_descriptor_id": "",
1039 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1045 mock_vnf_count_and_record
= MagicMock()
1046 db_vnfr
= {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
1048 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1049 expected_result
= "simple-ee-ab-z0-vnf111-xxx-y-vnf"
1050 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1051 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1053 application_name
= self
.n2vc
._get
_application
_name
(namespace
)
1054 self
.assertEqual(application_name
, expected_result
)
1055 self
.assertLess(len(application_name
), 50)
1056 mock_vnf_count_and_record
.assert_called_once_with(
1057 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1059 self
.db
.get_one
.assert_called_once()
1062 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1063 **{"return_value": "random"}
1065 def test_get_application_name_vnf_charm_old_naming(
1066 self
, mock_generate_random_alfanum
1068 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1069 self
.db
.get_one
.return_value
= {
1074 "target_element": "vnf/vnf1/mgmtvm",
1075 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1076 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1078 "vdu_count_index": None,
1079 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1080 "vdu_name": "mgmtvm",
1081 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1082 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1085 "target_element": "ns",
1086 "member-vnf-index": None,
1089 "vdu_count_index": None,
1092 "ee_descriptor_id": "",
1093 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1099 mock_vnf_count_and_record
= MagicMock()
1100 db_vnfr
= {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
1102 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1103 expected_result
= "app-vnf-eb3161eec0-z0-random"
1104 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1105 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1107 application_name
= self
.n2vc
._get
_application
_name
(namespace
)
1108 self
.assertEqual(application_name
, expected_result
)
1109 mock_vnf_count_and_record
.assert_called_once_with(
1110 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1112 self
.db
.get_one
.assert_called_once()
1114 def test_get_application_name_vnf_charm_vnf_index_ref_mismatch(self
):
1115 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1116 self
.db
.get_one
.return_value
= {
1121 "target_element": "vnf/vnf1/mgmtvm",
1122 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1123 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1125 "vdu_count_index": None,
1126 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1127 "vdu_name": "mgmtvm",
1128 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1130 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1133 "target_element": "ns",
1134 "member-vnf-index": None,
1137 "vdu_count_index": None,
1140 "ee_descriptor_id": "",
1142 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1148 mock_vnf_count_and_record
= MagicMock()
1149 db_vnfr
= {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
1151 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1152 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1153 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1155 with self
.assertRaises(N2VCException
):
1156 self
.n2vc
._get
_application
_name
(namespace
)
1157 mock_vnf_count_and_record
.assert_called_once_with(
1158 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1160 self
.db
.get_one
.assert_called_once()
1162 def test_get_application_name_vdu_charm(self
):
1163 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1164 self
.db
.get_one
.return_value
= {
1169 "target_element": "vnf/vnf1/mgmtvm",
1170 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1173 "vdu_count_index": None,
1174 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1175 "vdu_name": "mgmtvm",
1176 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1178 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1181 "target_element": "ns",
1182 "member-vnf-index": None,
1185 "vdu_count_index": None,
1188 "ee_descriptor_id": "",
1190 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1196 mock_vnf_count_and_record
= MagicMock()
1198 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1200 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1201 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1205 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1206 expected_result
= "simple-ee-ab-z0-vnf111-xxx-y-mgmtvm-z0-vdu"
1207 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1208 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1210 application_name
= self
.n2vc
._get
_application
_name
(namespace
)
1211 self
.assertEqual(application_name
, expected_result
)
1212 self
.assertLess(len(application_name
), 50)
1213 mock_vnf_count_and_record
.assert_called_once_with(
1214 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1216 self
.db
.get_one
.assert_called_once()
1218 def test_get_application_name_kdu_charm(self
):
1219 namespace
= ".82b11965-e580-47c0-9ee0-329f318a305b.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.ldap"
1220 self
.db
.get_one
.return_value
= {
1225 "target_element": "vnf/openldap/kdu/ldap",
1226 "member-vnf-index": "openldap",
1229 "vdu_count_index": 0,
1230 "operational-status": "init",
1231 "detailed-status": "",
1232 "step": "initial-deploy",
1233 "vnfd_id": "openldap_knf",
1235 "type": "lxc_proxy_charm",
1236 "ee_descriptor_id": "openldap-ee",
1239 "application": "openldap-ee-z0-openldap-vdu",
1240 "model": "82b11965-e580-47c0-9ee0-329f318a305b",
1241 "config_sw_installed": True,
1247 mock_vnf_count_and_record
= MagicMock()
1248 db_vnfr
= {"member-vnf-index-ref": "openldap", "vdur": {}}
1250 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1251 expected_result
= "openldap-ee-z0-openldap-ldap-vdu"
1252 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1253 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1255 application_name
= self
.n2vc
._get
_application
_name
(namespace
)
1256 self
.assertEqual(application_name
, expected_result
)
1257 self
.assertLess(len(application_name
), 50)
1258 mock_vnf_count_and_record
.assert_called_once_with(
1259 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1261 self
.db
.get_one
.assert_called_once()
1264 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1265 **{"return_value": "random"}
1267 def test_get_application_name_vdu_charm_old_naming(
1268 self
, mock_generate_random_alfanum
1270 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1271 self
.db
.get_one
.return_value
= {
1276 "target_element": "vnf/vnf1/mgmtVM",
1277 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1280 "vdu_count_index": None,
1281 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1282 "vdu_name": "mgmtvm",
1283 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1284 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1287 "target_element": "ns",
1288 "member-vnf-index": None,
1291 "vdu_count_index": None,
1294 "ee_descriptor_id": "",
1295 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1301 mock_vnf_count_and_record
= MagicMock()
1303 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1305 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1306 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1310 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1311 expected_result
= "app-vnf-eb3161eec0-z0-vdu-mgmtvm-cnt-z0-random"
1313 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1314 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1316 application_name
= self
.n2vc
._get
_application
_name
(namespace
)
1317 self
.assertEqual(application_name
, expected_result
)
1318 self
.assertLess(len(application_name
), 50)
1319 mock_vnf_count_and_record
.assert_called_once_with(
1320 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1322 self
.db
.get_one
.assert_called_once()
1324 def test_get_application_name_ns_charm(self
):
1325 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1326 self
.db
.get_one
.return_value
= {
1331 "target_element": "ns",
1332 "member-vnf-index": None,
1335 "vdu_count_index": None,
1338 "ee_descriptor_id": "",
1339 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1340 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1346 mock_vnf_count_and_record
= MagicMock()
1349 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1350 expected_result
= "simple-ns-charm-abc-z000-rrrr-nnnn-z4444-h-ns"
1351 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1352 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1354 application_name
= self
.n2vc
._get
_application
_name
(namespace
)
1355 self
.assertEqual(application_name
, expected_result
)
1356 self
.assertLess(len(application_name
), 50)
1357 mock_vnf_count_and_record
.assert_called_once_with("ns-level", None)
1358 self
.db
.get_one
.assert_called_once()
1360 def test_get_application_name_ns_charm_empty_charm_name(self
):
1361 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1362 self
.db
.get_one
.return_value
= {
1367 "target_element": "ns",
1368 "member-vnf-index": None,
1371 "vdu_count_index": None,
1374 "ee_descriptor_id": "",
1376 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1382 mock_vnf_count_and_record
= MagicMock()
1385 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1386 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1387 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1389 with self
.assertRaises(N2VCException
):
1390 self
.n2vc
._get
_application
_name
(namespace
)
1391 mock_vnf_count_and_record
.assert_called_once_with("ns-level", None)
1392 self
.db
.get_one
.assert_called_once()
1395 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1396 **{"return_value": "random"}
1398 def test_get_application_name_ns_charm_old_naming(
1399 self
, mock_generate_random_alfanum
1401 namespace
= ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1402 self
.db
.get_one
.return_value
= {
1407 "target_element": "ns",
1408 "member-vnf-index": None,
1411 "vdu_count_index": None,
1414 "ee_descriptor_id": "",
1415 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1421 mock_vnf_count_and_record
= MagicMock()
1424 mock_vnf_count_and_record
.return_value
= (vnf_count
, db_vnfr
)
1425 expected_result
= "app-random"
1426 with patch
.object(self
.n2vc
, "db", self
.db
), patch
.object(
1427 self
.n2vc
, "_get_vnf_count_and_record", mock_vnf_count_and_record
1429 application_name
= self
.n2vc
._get
_application
_name
(namespace
)
1430 self
.assertEqual(application_name
, expected_result
)
1431 self
.assertLess(len(application_name
), 50)
1432 mock_vnf_count_and_record
.assert_called_once_with("ns-level", None)
1433 self
.db
.get_one
.assert_called_once()