Pin black version in tox.ini to 23.12.1
[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, MagicMock
19 from unittest.mock import patch
20
21
22 import asynctest
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,
29 N2VCException,
30 JujuApplicationNotFound,
31 )
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
35 import yaml
36
37
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")
42 def setUp(
43 self,
44 mock_base64_to_cacert=None,
45 mock_get_connection=None,
46 mock_store=None,
47 ):
48 self.loop = asyncio.get_event_loop()
49 self.db = Mock()
50 mock_base64_to_cacert.return_value = """
51 -----BEGIN CERTIFICATE-----
52 SOMECERT
53 -----END CERTIFICATE-----"""
54 mock_store.return_value = AsyncMock()
55 mock_vca_connection = Mock()
56 mock_get_connection.return_value = mock_vca_connection
57 mock_vca_connection.data.return_value = ConnectionData(
58 **{
59 "endpoints": ["1.2.3.4:17070"],
60 "user": "user",
61 "secret": "secret",
62 "cacert": "cacert",
63 "pubkey": "pubkey",
64 "lxd-cloud": "cloud",
65 "lxd-credentials": "credentials",
66 "k8s-cloud": "k8s_cloud",
67 "k8s-credentials": "k8s_credentials",
68 "model-config": {},
69 "api-proxy": "api_proxy",
70 }
71 )
72 logging.disable(logging.CRITICAL)
73
74 N2VCJujuConnector.get_public_key = Mock()
75 self.n2vc = N2VCJujuConnector(
76 db=self.db,
77 fs=fslocal.FsLocal(),
78 log=None,
79 loop=self.loop,
80 on_update_db=None,
81 )
82 N2VCJujuConnector.get_public_key.assert_not_called()
83 self.n2vc.libjuju = Mock()
84
85
86 class GetMetricssTest(N2VCJujuConnTestCase):
87 def setUp(self):
88 super(GetMetricssTest, self).setUp()
89 self.n2vc.libjuju.get_metrics = AsyncMock()
90
91 def test_success(self):
92 _ = self.loop.run_until_complete(self.n2vc.get_metrics("model", "application"))
93 self.n2vc.libjuju.get_metrics.assert_called_once()
94
95 def test_except(self):
96 self.n2vc.libjuju.get_metrics.side_effect = Exception()
97 with self.assertRaises(Exception):
98 _ = self.loop.run_until_complete(
99 self.n2vc.get_metrics("model", "application")
100 )
101 self.n2vc.libjuju.get_metrics.assert_called_once()
102
103
104 class UpdateVcaStatusTest(N2VCJujuConnTestCase):
105 def setUp(self):
106 super(UpdateVcaStatusTest, self).setUp()
107 self.n2vc.libjuju.get_controller = AsyncMock()
108 self.n2vc.libjuju.get_model = AsyncMock()
109 self.n2vc.libjuju.get_executed_actions = AsyncMock()
110 self.n2vc.libjuju.get_actions = AsyncMock()
111 self.n2vc.libjuju.get_application_configs = AsyncMock()
112 self.n2vc.libjuju._get_application = AsyncMock()
113
114 def test_success(
115 self,
116 ):
117 self.loop.run_until_complete(
118 self.n2vc.update_vca_status(
119 {"model": {"applications": {"app": {"actions": {}}}}}
120 )
121 )
122 self.n2vc.libjuju.get_executed_actions.assert_called_once()
123 self.n2vc.libjuju.get_actions.assert_called_once()
124 self.n2vc.libjuju.get_application_configs.assert_called_once()
125
126 def test_exception(self):
127 self.n2vc.libjuju.get_model.return_value = None
128 self.n2vc.libjuju.get_executed_actions.side_effect = Exception()
129 with self.assertRaises(Exception):
130 self.loop.run_until_complete(
131 self.n2vc.update_vca_status(
132 {"model": {"applications": {"app": {"actions": {}}}}}
133 )
134 )
135 self.n2vc.libjuju.get_executed_actions.assert_not_called()
136 self.n2vc.libjuju.get_actions.assert_not_called_once()
137 self.n2vc.libjuju.get_application_configs.assert_not_called_once()
138
139
140 class K8sProxyCharmsTest(N2VCJujuConnTestCase):
141 def setUp(self):
142 super(K8sProxyCharmsTest, self).setUp()
143 self.n2vc.libjuju.model_exists = AsyncMock()
144 self.n2vc.libjuju.add_model = AsyncMock()
145 self.n2vc.libjuju.deploy_charm = AsyncMock()
146 self.n2vc.libjuju.model_exists.return_value = False
147 self.db = DbMemory()
148 self.fs = fslocal.FsLocal()
149 self.fs.path = "/"
150 self.n2vc.fs = self.fs
151 self.n2vc.db = self.db
152 self.db.create_list("nsrs", yaml.safe_load(descriptors.db_nsrs_text))
153 self.db.create_list("vnfrs", yaml.safe_load(descriptors.db_vnfrs_text))
154
155 @patch(
156 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
157 **{"return_value": "random"}
158 )
159 def test_success(
160 self,
161 mock_generate_random_alfanum_string,
162 ):
163 self.n2vc.fs.file_exists = MagicMock(create_autospec=True)
164 self.n2vc.fs.file_exists.return_value = True
165 ee_id = self.loop.run_until_complete(
166 self.n2vc.install_k8s_proxy_charm(
167 "simple",
168 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
169 "path",
170 {},
171 )
172 )
173
174 self.n2vc.libjuju.add_model.assert_called_once()
175 self.n2vc.libjuju.deploy_charm.assert_called_once_with(
176 model_name="dbfbd751-3de4-4e68-bd40-ec5ae0a53898-k8s",
177 application_name="simple-ee-z0-vnf1-vnf",
178 path="//path",
179 machine_id=None,
180 db_dict={},
181 progress_timeout=None,
182 total_timeout=None,
183 config=None,
184 )
185 self.assertEqual(
186 ee_id, "dbfbd751-3de4-4e68-bd40-ec5ae0a53898-k8s.simple-ee-z0-vnf1-vnf.k8s"
187 )
188
189 def test_no_artifact_path(
190 self,
191 ):
192 with self.assertRaises(N2VCBadArgumentsException):
193 ee_id = self.loop.run_until_complete(
194 self.n2vc.install_k8s_proxy_charm(
195 "simple",
196 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
197 "",
198 {},
199 )
200 )
201 self.assertIsNone(ee_id)
202
203 def test_no_db(
204 self,
205 ):
206 with self.assertRaises(N2VCBadArgumentsException):
207 ee_id = self.loop.run_until_complete(
208 self.n2vc.install_k8s_proxy_charm(
209 "simple",
210 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
211 "path",
212 None,
213 )
214 )
215 self.assertIsNone(ee_id)
216
217 def test_file_not_exists(
218 self,
219 ):
220 self.n2vc.fs.file_exists = MagicMock(create_autospec=True)
221 self.n2vc.fs.file_exists.return_value = False
222 with self.assertRaises(N2VCBadArgumentsException):
223 ee_id = self.loop.run_until_complete(
224 self.n2vc.install_k8s_proxy_charm(
225 "simple",
226 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
227 "path",
228 {},
229 )
230 )
231 self.assertIsNone(ee_id)
232
233 def test_exception(
234 self,
235 ):
236 self.n2vc.fs.file_exists = MagicMock(create_autospec=True)
237 self.n2vc.fs.file_exists.return_value = True
238 self.n2vc.fs.path = MagicMock(create_autospec=True)
239 self.n2vc.fs.path.return_value = "path"
240 self.n2vc.libjuju.deploy_charm.side_effect = Exception()
241 with self.assertRaises(N2VCException):
242 ee_id = self.loop.run_until_complete(
243 self.n2vc.install_k8s_proxy_charm(
244 "simple",
245 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
246 "path",
247 {},
248 )
249 )
250 self.assertIsNone(ee_id)
251
252
253 class AddRelationTest(N2VCJujuConnTestCase):
254 def setUp(self):
255 super(AddRelationTest, self).setUp()
256 self.n2vc.libjuju.add_relation = AsyncMock()
257 self.n2vc.libjuju.offer = AsyncMock()
258 self.n2vc.libjuju.get_controller = AsyncMock()
259 self.n2vc.libjuju.consume = AsyncMock()
260
261 def test_standard_relation(self):
262 relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
263 relation_endpoint_2 = RelationEndpoint("model-1.app2.1", None, "endpoint")
264 self.loop.run_until_complete(
265 self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
266 )
267 self.n2vc.libjuju.add_relation.assert_called_once_with(
268 model_name="model-1", endpoint_1="app1:endpoint", endpoint_2="app2:endpoint"
269 )
270 self.n2vc.libjuju.offer.assert_not_called()
271 self.n2vc.libjuju.consume.assert_not_called()
272
273 def test_cmr_relation_same_controller(self):
274 relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
275 relation_endpoint_2 = RelationEndpoint("model-2.app2.1", None, "endpoint")
276 offer = Offer("admin/model-1.app1")
277 self.n2vc.libjuju.offer.return_value = offer
278 self.n2vc.libjuju.consume.return_value = "saas"
279 self.loop.run_until_complete(
280 self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
281 )
282 self.n2vc.libjuju.offer.assert_called_once_with(relation_endpoint_1)
283 self.n2vc.libjuju.consume.assert_called_once()
284 self.n2vc.libjuju.add_relation.assert_called_once_with(
285 "model-2", "app2:endpoint", "saas"
286 )
287
288 def test_relation_exception(self):
289 relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
290 relation_endpoint_2 = RelationEndpoint("model-2.app2.1", None, "endpoint")
291 self.n2vc.libjuju.offer.side_effect = Exception()
292 with self.assertRaises(N2VCException):
293 self.loop.run_until_complete(
294 self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
295 )
296
297
298 class UpgradeCharmTest(N2VCJujuConnTestCase):
299 def setUp(self):
300 super(UpgradeCharmTest, self).setUp()
301 self.n2vc._get_libjuju = AsyncMock(return_value=self.n2vc.libjuju)
302 N2VCJujuConnector._get_ee_id_components = Mock()
303 self.n2vc.libjuju.upgrade_charm = AsyncMock()
304
305 def test_empty_ee_id(self):
306 with self.assertRaises(N2VCBadArgumentsException):
307 self.loop.run_until_complete(
308 self.n2vc.upgrade_charm(
309 "", "/sample_charm_path", "sample_charm_id", "native-charm", None
310 )
311 )
312 self.n2vc._get_libjuju.assert_called()
313 self.n2vc._get_ee_id_components.assert_not_called()
314 self.n2vc.libjuju.upgrade_charm.assert_not_called()
315
316 def test_wrong_ee_id(self):
317 N2VCJujuConnector._get_ee_id_components.side_effect = Exception
318 with self.assertRaises(N2VCBadArgumentsException):
319 self.loop.run_until_complete(
320 self.n2vc.upgrade_charm(
321 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
322 "/sample_charm_path",
323 "sample_charm_id",
324 "native-charm",
325 500,
326 )
327 )
328 self.n2vc._get_libjuju.assert_called()
329 self.n2vc._get_ee_id_components.assert_called()
330 self.n2vc.libjuju.upgrade_charm.assert_not_called()
331
332 def test_charm_upgrade_succeded(self):
333 N2VCJujuConnector._get_ee_id_components.return_value = (
334 "sample_model",
335 "sample_app",
336 "sample_machine_id",
337 )
338 self.loop.run_until_complete(
339 self.n2vc.upgrade_charm(
340 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
341 "/sample_charm_path",
342 "sample_charm_id",
343 "native-charm",
344 500,
345 )
346 )
347 self.n2vc._get_libjuju.assert_called()
348 self.n2vc._get_ee_id_components.assert_called()
349 self.n2vc.libjuju.upgrade_charm.assert_called_with(
350 application_name="sample_app",
351 path="/sample_charm_path",
352 model_name="sample_model",
353 total_timeout=500,
354 )
355
356 def test_charm_upgrade_failed(self):
357 N2VCJujuConnector._get_ee_id_components.return_value = (
358 "sample_model",
359 "sample_app",
360 "sample_machine_id",
361 )
362 self.n2vc.libjuju.upgrade_charm.side_effect = JujuApplicationNotFound
363 with self.assertRaises(N2VCException):
364 self.loop.run_until_complete(
365 self.n2vc.upgrade_charm(
366 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
367 "/sample_charm_path",
368 "sample_charm_id",
369 "native-charm",
370 None,
371 )
372 )
373 self.n2vc._get_libjuju.assert_called()
374 self.n2vc._get_ee_id_components.assert_called()
375 self.n2vc.libjuju.upgrade_charm.assert_called_with(
376 application_name="sample_app",
377 path="/sample_charm_path",
378 model_name="sample_model",
379 total_timeout=None,
380 )
381
382
383 class GenerateApplicationNameTest(N2VCJujuConnTestCase):
384
385 vnf_id = "dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
386
387 def setUp(self):
388 super(GenerateApplicationNameTest, self).setUp()
389 self.db = MagicMock(DbMemory)
390
391 @patch(
392 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
393 **{"return_value": "random"}
394 )
395 def test_generate_backward_compatible_application_name(
396 self, mock_generate_random_alfanum
397 ):
398 vdu_id = "mgmtVM"
399 vdu_count = "0"
400 expected_result = "app-vnf-ec5ae0a53898-vdu-mgmtVM-cnt-0-random"
401
402 application_name = self.n2vc._generate_backward_compatible_application_name(
403 GenerateApplicationNameTest.vnf_id, vdu_id, vdu_count
404 )
405 self.assertEqual(application_name, expected_result)
406
407 @patch(
408 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
409 **{"return_value": "random"}
410 )
411 def test_generate_backward_compatible_application_name_without_vnf_id_vdu_id(
412 self, mock_generate_random_alfanum
413 ):
414 vnf_id = None
415 vdu_id = ""
416 vdu_count = None
417 expected_result = "app--random"
418 application_name = self.n2vc._generate_backward_compatible_application_name(
419 vnf_id, vdu_id, vdu_count
420 )
421
422 self.assertEqual(application_name, expected_result)
423 self.assertLess(len(application_name), 50)
424
425 def test_find_charm_level_with_vnf_id(self):
426 vdu_id = ""
427 expected_result = "vnf-level"
428 charm_level = self.n2vc._find_charm_level(
429 GenerateApplicationNameTest.vnf_id, vdu_id
430 )
431 self.assertEqual(charm_level, expected_result)
432
433 def test_find_charm_level_with_vdu_id(self):
434 vnf_id = ""
435 vdu_id = "mgmtVM"
436 with self.assertRaises(N2VCException):
437 self.n2vc._find_charm_level(vnf_id, vdu_id)
438
439 def test_find_charm_level_with_vnf_id_and_vdu_id(self):
440 vdu_id = "mgmtVM"
441 expected_result = "vdu-level"
442 charm_level = self.n2vc._find_charm_level(
443 GenerateApplicationNameTest.vnf_id, vdu_id
444 )
445 self.assertEqual(charm_level, expected_result)
446
447 def test_find_charm_level_without_vnf_id_and_vdu_id(self):
448 vnf_id = ""
449 vdu_id = ""
450 expected_result = "ns-level"
451 charm_level = self.n2vc._find_charm_level(vnf_id, vdu_id)
452 self.assertEqual(charm_level, expected_result)
453
454 def test_generate_application_name_ns_charm(self):
455 charm_level = "ns-level"
456 vnfrs = {}
457 vca_records = [
458 {
459 "target_element": "ns",
460 "member-vnf-index": "",
461 "vdu_id": None,
462 "kdu_name": None,
463 "vdu_count_index": None,
464 "vnfd_id": None,
465 "vdu_name": None,
466 "type": "proxy_charm",
467 "ee_descriptor_id": None,
468 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh",
469 "ee_id": None,
470 "application": "",
471 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
472 },
473 ]
474 vnf_count = ""
475 vdu_count = ""
476 vdu_id = None
477 expected_result = "simple-ns-charm-abc-000-rrrr-nnnn-4444-h-ns"
478 application_name = self.n2vc._generate_application_name(
479 charm_level,
480 vnfrs,
481 vca_records,
482 vnf_count=vnf_count,
483 vdu_id=vdu_id,
484 vdu_count=vdu_count,
485 )
486 self.assertEqual(application_name, expected_result)
487 self.assertLess(len(application_name), 50)
488
489 def test_generate_application_name_ns_charm_empty_vca_records(self):
490 charm_level = "ns-level"
491 vnfrs = {}
492 vca_records = []
493 vnf_count = ""
494 vdu_count = ""
495 vdu_id = None
496 with self.assertRaises(N2VCException):
497 self.n2vc._generate_application_name(
498 charm_level,
499 vnfrs,
500 vca_records,
501 vnf_count=vnf_count,
502 vdu_id=vdu_id,
503 vdu_count=vdu_count,
504 )
505
506 def test_generate_application_name_vnf_charm(self):
507 charm_level = "vnf-level"
508 vnfrs = {
509 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
510 }
511 vca_records = [
512 {
513 "target_element": "vnf/vnf1",
514 "member-vnf-index": "vnf111-xxx-yyy-zzz",
515 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
516 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
517 "charm_name": "",
518 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
519 },
520 ]
521 vnf_count = "1"
522 vdu_count = ""
523 vdu_id = None
524 expected_result = "simple-ee-ab-1-vnf111-xxx-y-vnf"
525 application_name = self.n2vc._generate_application_name(
526 charm_level,
527 vnfrs,
528 vca_records,
529 vnf_count=vnf_count,
530 vdu_id=vdu_id,
531 vdu_count=vdu_count,
532 )
533 self.assertEqual(application_name, expected_result)
534 self.assertLess(len(application_name), 50)
535
536 def test_generate_application_name_vdu_charm_kdu_name_in_vca_record_is_none(self):
537 charm_level = "vdu-level"
538 vnfrs = {
539 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
540 "vdur": [
541 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
542 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
543 ],
544 }
545 vca_records = [
546 {
547 "target_element": "vnf/vnf1/mgmtvm",
548 "member-vnf-index": "vnf111-xxx-yyy-zzz",
549 "vdu_id": "mgmtVM",
550 "kdu_name": None,
551 "vdu_count_index": None,
552 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
553 "vdu_name": "mgmtvm",
554 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
555 "charm_name": "",
556 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
557 },
558 {
559 "target_element": "vnf/vnf1/dataVM",
560 "member-vnf-index": "vnf111-xxx-yyy-zzz",
561 "vdu_id": "dataVM",
562 "kdu_name": None,
563 "vdu_count_index": None,
564 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
565 "vdu_name": "datavm",
566 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
567 "charm_name": "",
568 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
569 },
570 ]
571 vnf_count = "2"
572 vdu_count = "0"
573 vdu_id = "mgmtVM"
574 expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
575 application_name = self.n2vc._generate_application_name(
576 charm_level,
577 vnfrs,
578 vca_records,
579 vnf_count=vnf_count,
580 vdu_id=vdu_id,
581 vdu_count=vdu_count,
582 )
583 self.assertEqual(application_name, expected_result)
584 self.assertLess(len(application_name), 50)
585
586 def test_generate_application_name_vdu_charm_vdu_id_kdu_name_in_vca_record_are_both_set(
587 self,
588 ):
589 charm_level = "vdu-level"
590 vnfrs = {
591 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
592 "vdur": [
593 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
594 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
595 ],
596 }
597 vca_records = [
598 {
599 "target_element": "vnf/vnf1/mgmtVM",
600 "member-vnf-index": "vnf111-xxx-yyy-zzz",
601 "vdu_id": "mgmtVM",
602 "kdu_name": "mgmtVM",
603 "vdu_count_index": None,
604 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
605 "vdu_name": "mgmtvm",
606 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
607 "charm_name": "",
608 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
609 },
610 {
611 "target_element": "vnf/vnf1/dataVM",
612 "member-vnf-index": "vnf111-xxx-yyy-zzz",
613 "vdu_id": "dataVM",
614 "kdu_name": None,
615 "vdu_count_index": None,
616 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
617 "vdu_name": "datavm",
618 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
619 "charm_name": "",
620 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
621 },
622 ]
623 vnf_count = "2"
624 vdu_count = "0"
625 vdu_id = "mgmtVM"
626 expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
627 application_name = self.n2vc._generate_application_name(
628 charm_level,
629 vnfrs,
630 vca_records,
631 vnf_count=vnf_count,
632 vdu_id=vdu_id,
633 vdu_count=vdu_count,
634 )
635 self.assertEqual(application_name, expected_result)
636 self.assertLess(len(application_name), 50)
637
638 def test_generate_application_name_vdu_charm_both_vdu_id_kdu_name_in_vca_record_are_none(
639 self,
640 ):
641 charm_level = "vdu-level"
642 vnfrs = {
643 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
644 }
645 vca_records = [
646 {
647 "target_element": "vnf/vnf1/mgmtVM",
648 "member-vnf-index": "vnf111-xxx-yyy-zzz",
649 "vdu_id": None,
650 "kdu_name": None,
651 "vdu_count_index": None,
652 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
653 "vdu_name": "mgmtvm",
654 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
655 "charm_name": "",
656 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
657 },
658 ]
659 vnf_count = "2"
660 vdu_count = "0"
661 vdu_id = "mgmtVM"
662 with self.assertRaises(KeyError):
663 self.n2vc._generate_application_name(
664 charm_level,
665 vnfrs,
666 vca_records,
667 vnf_count=vnf_count,
668 vdu_id=vdu_id,
669 vdu_count=vdu_count,
670 )
671
672 def test_generate_application_name_vdu_charm_given_vdu_id_is_none(self):
673 charm_level = "vdu-level"
674 vnfrs = {
675 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
676 }
677 vca_records = [
678 {
679 "target_element": "vnf/vnf1/mgmtvVM",
680 "member-vnf-index": "vnf111-xxx-yyy-zzz",
681 "vdu_id": None,
682 "kdu_name": "mgmtVM",
683 "vdu_count_index": None,
684 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
685 "vdu_name": "mgmtvm",
686 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
687 "charm_name": "",
688 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
689 },
690 ]
691 vnf_count = "2"
692 vdu_count = "0"
693 vdu_id = None
694 with self.assertRaises(N2VCException):
695 self.n2vc._generate_application_name(
696 charm_level,
697 vnfrs,
698 vca_records,
699 vnf_count=vnf_count,
700 vdu_id=vdu_id,
701 vdu_count=vdu_count,
702 )
703
704 def test_generate_application_name_vdu_charm_vdu_id_does_not_match_with_the_key_in_vca_record(
705 self,
706 ):
707 charm_level = "vdu-level"
708 vnfrs = {
709 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
710 }
711 vca_records = [
712 {
713 "target_element": "vnf/vnf1/mgmtVM",
714 "member-vnf-index": "vnf111-xxx-yyy-zzz",
715 "vdu_id": None,
716 "kdu_name": "mgmtVM",
717 "vdu_count_index": None,
718 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
719 "vdu_name": "mgmtvm",
720 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
721 "charm_name": "",
722 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
723 },
724 ]
725 vnf_count = "2"
726 vdu_count = "0"
727 vdu_id = "mgmtvm"
728 with self.assertRaises(KeyError):
729 self.n2vc._generate_application_name(
730 charm_level,
731 vnfrs,
732 vca_records,
733 vnf_count=vnf_count,
734 vdu_id=vdu_id,
735 vdu_count=vdu_count,
736 )
737
738 def test_generate_application_name_vdu_charm_vdu_id_in_vca_record_is_none(self):
739 charm_level = "vdu-level"
740 vnfrs = {
741 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
742 }
743 vca_records = [
744 {
745 "target_element": "vnf/vnf1/mgmtVM",
746 "member-vnf-index": "vnf111-xxx-yyy-zzz",
747 "vdu_id": None,
748 "kdu_name": "mgmtVM",
749 "vdu_count_index": None,
750 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
751 "vdu_name": "mgmtvm",
752 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
753 "charm_name": "",
754 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
755 },
756 ]
757 vnf_count = "2"
758 vdu_count = "0"
759 vdu_id = "mgmtVM"
760 expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
761 application_name = self.n2vc._generate_application_name(
762 charm_level,
763 vnfrs,
764 vca_records,
765 vnf_count=vnf_count,
766 vdu_id=vdu_id,
767 vdu_count=vdu_count,
768 )
769 self.assertEqual(application_name, expected_result)
770 self.assertLess(len(application_name), 50)
771
772 def test_get_vnf_count_db_vnfr_ns_charm(self):
773 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
774 charm_level = "ns-level"
775 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
776 with patch.object(self.n2vc, "db", self.db):
777 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
778 charm_level, vnf_id_and_count
779 )
780 self.assertEqual(vnf_count, "")
781 self.assertEqual(db_vnfr, {})
782
783 def test_get_vnf_count_db_vnfr_vnf_charm(self):
784 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
785 charm_level = "vnf-level"
786 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
787 with patch.object(self.n2vc, "db", self.db):
788 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
789 charm_level, vnf_id_and_count
790 )
791 self.assertEqual(vnf_count, "4")
792 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
793
794 def test_get_vnf_count_db_vnfr_vdu_charm(self):
795 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
796 charm_level = "vdu-level"
797 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-2"
798 with patch.object(self.n2vc, "db", self.db):
799 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
800 charm_level, vnf_id_and_count
801 )
802 self.assertEqual(vnf_count, "2")
803 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
804
805 def test_get_vca_records_vdu_charm(self):
806 charm_level = "vdu-level"
807 db_vnfr = {
808 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
809 "vdur": [
810 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
811 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
812 ],
813 }
814 db_nsr = {
815 "_admin": {
816 "deployed": {
817 "VCA": [
818 {
819 "target_element": "vnf/vnf1/mgmtvm",
820 "member-vnf-index": "vnf111-xxx-yyy-zzz",
821 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
822 "kdu_name": None,
823 "vdu_count_index": None,
824 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
825 "vdu_name": "mgmtvm",
826 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
827 "charm_name": "",
828 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
829 },
830 {
831 "target_element": "vnf/vnf2/datavm",
832 "member-vnf-index": "vnf222-xxx-yyy-zzz",
833 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
834 "kdu_name": None,
835 "vdu_count_index": None,
836 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
837 "vdu_name": "datavm",
838 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
839 "charm_name": "",
840 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
841 },
842 ],
843 },
844 },
845 }
846 expected_result = [
847 {
848 "target_element": "vnf/vnf1/mgmtvm",
849 "member-vnf-index": "vnf111-xxx-yyy-zzz",
850 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
851 "kdu_name": None,
852 "vdu_count_index": None,
853 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
854 "vdu_name": "mgmtvm",
855 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
856 "charm_name": "",
857 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
858 },
859 ]
860 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
861 self.assertEqual(vca_records, expected_result)
862
863 def test_get_vca_records_vnf_charm_member_vnf_index_mismatch(self):
864 charm_level = "vnf-level"
865 db_vnfr = {
866 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
867 }
868 db_nsr = {
869 "_admin": {
870 "deployed": {
871 "VCA": [
872 {
873 "target_element": "vnf/vnf1/mgmtvm",
874 "member-vnf-index": "vnf111-xxx-yyy-zzz",
875 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
876 "kdu_name": None,
877 "vdu_count_index": None,
878 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
879 "vdu_name": "mgmtvm",
880 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
881 "charm_name": "",
882 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
883 },
884 {
885 "target_element": "vnf/vnf1/mgmtvm",
886 "member-vnf-index": "vnf111-xxx-yyy-zzz",
887 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
888 "kdu_name": None,
889 "vdu_count_index": None,
890 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
891 "vdu_name": "datavm",
892 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
893 "charm_name": "",
894 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
895 },
896 ],
897 },
898 },
899 }
900 expected_result = []
901 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
902 self.assertEqual(vca_records, expected_result)
903
904 def test_get_vca_records_ns_charm(self):
905 charm_level = "ns-level"
906 db_vnfr = {
907 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
908 }
909 db_nsr = {
910 "_admin": {
911 "deployed": {
912 "VCA": [
913 {
914 "target_element": "vnf/vnf1/mgmtvm",
915 "member-vnf-index": "vnf111-xxx-yyy-zzz",
916 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
917 "kdu_name": None,
918 "vdu_count_index": None,
919 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
920 "vdu_name": "mgmtvm",
921 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
922 "charm_name": "",
923 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
924 },
925 {
926 "target_element": "ns",
927 "member-vnf-index": None,
928 "vdu_id": None,
929 "kdu_name": None,
930 "vdu_count_index": None,
931 "vnfd_id": "",
932 "vdu_name": "",
933 "ee_descriptor_id": "",
934 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
935 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
936 },
937 ],
938 },
939 },
940 }
941 expected_result = [
942 {
943 "target_element": "ns",
944 "member-vnf-index": None,
945 "vdu_id": None,
946 "kdu_name": None,
947 "vdu_count_index": None,
948 "vnfd_id": "",
949 "vdu_name": "",
950 "ee_descriptor_id": "",
951 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
952 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
953 },
954 ]
955 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
956 self.assertEqual(vca_records, expected_result)
957
958 def test_get_vca_records_ns_charm_empty_charm_name(self):
959 charm_level = "ns-level"
960 db_vnfr = {
961 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
962 }
963 db_nsr = {
964 "_admin": {
965 "deployed": {
966 "VCA": [
967 {
968 "target_element": "vnf/vnf1/mgmtvm",
969 "member-vnf-index": "vnf111-xxx-yyy-zzz",
970 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
971 "kdu_name": None,
972 "vdu_count_index": None,
973 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
974 "vdu_name": "mgmtvm",
975 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
976 "charm_name": "",
977 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
978 },
979 {
980 "target_element": "ns",
981 "member-vnf-index": None,
982 "vdu_id": None,
983 "kdu_name": None,
984 "vdu_count_index": None,
985 "vnfd_id": "",
986 "vdu_name": "",
987 "ee_descriptor_id": "",
988 "charm_name": "",
989 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
990 },
991 ],
992 },
993 },
994 }
995 expected_result = [
996 {
997 "target_element": "ns",
998 "member-vnf-index": None,
999 "vdu_id": None,
1000 "kdu_name": None,
1001 "vdu_count_index": None,
1002 "vnfd_id": "",
1003 "vdu_name": "",
1004 "ee_descriptor_id": "",
1005 "charm_name": "",
1006 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1007 },
1008 ]
1009 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
1010 self.assertEqual(vca_records, expected_result)
1011
1012 def test_get_application_name_vnf_charm(self):
1013 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1014 self.db.get_one.return_value = {
1015 "_admin": {
1016 "deployed": {
1017 "VCA": [
1018 {
1019 "target_element": "vnf/vnf1/mgmtvm",
1020 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1021 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1022 "kdu_name": None,
1023 "vdu_count_index": None,
1024 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1025 "vdu_name": "mgmtvm",
1026 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1027 "charm_name": "",
1028 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1029 },
1030 {
1031 "target_element": "ns",
1032 "member-vnf-index": None,
1033 "vdu_id": None,
1034 "kdu_name": None,
1035 "vdu_count_index": None,
1036 "vnfd_id": "",
1037 "vdu_name": "",
1038 "ee_descriptor_id": "",
1039 "charm_name": "",
1040 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1041 },
1042 ],
1043 },
1044 },
1045 }
1046 mock_vnf_count_and_record = MagicMock()
1047 db_vnfr = {
1048 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1049 }
1050 vnf_count = "0"
1051 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1052 expected_result = "simple-ee-ab-z0-vnf111-xxx-y-vnf"
1053 with patch.object(self.n2vc, "db", self.db), patch.object(
1054 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1055 ):
1056 application_name = self.n2vc._get_application_name(namespace)
1057 self.assertEqual(application_name, expected_result)
1058 self.assertLess(len(application_name), 50)
1059 mock_vnf_count_and_record.assert_called_once_with(
1060 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1061 )
1062 self.db.get_one.assert_called_once()
1063
1064 @patch(
1065 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1066 **{"return_value": "random"}
1067 )
1068 def test_get_application_name_vnf_charm_old_naming(
1069 self, mock_generate_random_alfanum
1070 ):
1071 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1072 self.db.get_one.return_value = {
1073 "_admin": {
1074 "deployed": {
1075 "VCA": [
1076 {
1077 "target_element": "vnf/vnf1/mgmtvm",
1078 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1079 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1080 "kdu_name": None,
1081 "vdu_count_index": None,
1082 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1083 "vdu_name": "mgmtvm",
1084 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1085 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1086 },
1087 {
1088 "target_element": "ns",
1089 "member-vnf-index": None,
1090 "vdu_id": None,
1091 "kdu_name": None,
1092 "vdu_count_index": None,
1093 "vnfd_id": "",
1094 "vdu_name": "",
1095 "ee_descriptor_id": "",
1096 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1097 },
1098 ],
1099 },
1100 },
1101 }
1102 mock_vnf_count_and_record = MagicMock()
1103 db_vnfr = {
1104 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1105 }
1106 vnf_count = "0"
1107 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1108 expected_result = "app-vnf-eb3161eec0-z0-random"
1109 with patch.object(self.n2vc, "db", self.db), patch.object(
1110 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1111 ):
1112 application_name = self.n2vc._get_application_name(namespace)
1113 self.assertEqual(application_name, expected_result)
1114 mock_vnf_count_and_record.assert_called_once_with(
1115 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1116 )
1117 self.db.get_one.assert_called_once()
1118
1119 def test_get_application_name_vnf_charm_vnf_index_ref_mismatch(self):
1120 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1121 self.db.get_one.return_value = {
1122 "_admin": {
1123 "deployed": {
1124 "VCA": [
1125 {
1126 "target_element": "vnf/vnf1/mgmtvm",
1127 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1128 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1129 "kdu_name": None,
1130 "vdu_count_index": None,
1131 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1132 "vdu_name": "mgmtvm",
1133 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1134 "charm_name": "",
1135 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1136 },
1137 {
1138 "target_element": "ns",
1139 "member-vnf-index": None,
1140 "vdu_id": None,
1141 "kdu_name": None,
1142 "vdu_count_index": None,
1143 "vnfd_id": "",
1144 "vdu_name": "",
1145 "ee_descriptor_id": "",
1146 "charm_name": "",
1147 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1148 },
1149 ],
1150 },
1151 },
1152 }
1153 mock_vnf_count_and_record = MagicMock()
1154 db_vnfr = {
1155 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
1156 }
1157 vnf_count = "0"
1158 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1159 with patch.object(self.n2vc, "db", self.db), patch.object(
1160 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1161 ):
1162 with self.assertRaises(N2VCException):
1163 self.n2vc._get_application_name(namespace)
1164 mock_vnf_count_and_record.assert_called_once_with(
1165 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1166 )
1167 self.db.get_one.assert_called_once()
1168
1169 def test_get_application_name_vdu_charm(self):
1170 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1171 self.db.get_one.return_value = {
1172 "_admin": {
1173 "deployed": {
1174 "VCA": [
1175 {
1176 "target_element": "vnf/vnf1/mgmtvm",
1177 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1178 "vdu_id": "mgmtVM",
1179 "kdu_name": None,
1180 "vdu_count_index": None,
1181 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1182 "vdu_name": "mgmtvm",
1183 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1184 "charm_name": "",
1185 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1186 },
1187 {
1188 "target_element": "ns",
1189 "member-vnf-index": None,
1190 "vdu_id": None,
1191 "kdu_name": None,
1192 "vdu_count_index": None,
1193 "vnfd_id": "",
1194 "vdu_name": "",
1195 "ee_descriptor_id": "",
1196 "charm_name": "",
1197 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1198 },
1199 ]
1200 }
1201 }
1202 }
1203 mock_vnf_count_and_record = MagicMock()
1204 db_vnfr = {
1205 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1206 "vdur": [
1207 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1208 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1209 ],
1210 }
1211 vnf_count = "0"
1212 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1213 expected_result = "simple-ee-ab-z0-vnf111-xxx-y-mgmtvm-z0-vdu"
1214 with patch.object(self.n2vc, "db", self.db), patch.object(
1215 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1216 ):
1217 application_name = self.n2vc._get_application_name(namespace)
1218 self.assertEqual(application_name, expected_result)
1219 self.assertLess(len(application_name), 50)
1220 mock_vnf_count_and_record.assert_called_once_with(
1221 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1222 )
1223 self.db.get_one.assert_called_once()
1224
1225 def test_get_application_name_kdu_charm(self):
1226 namespace = ".82b11965-e580-47c0-9ee0-329f318a305b.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.ldap"
1227 self.db.get_one.return_value = {
1228 "_admin": {
1229 "deployed": {
1230 "VCA": [
1231 {
1232 "target_element": "vnf/openldap/kdu/ldap",
1233 "member-vnf-index": "openldap",
1234 "vdu_id": None,
1235 "kdu_name": "ldap",
1236 "vdu_count_index": 0,
1237 "operational-status": "init",
1238 "detailed-status": "",
1239 "step": "initial-deploy",
1240 "vnfd_id": "openldap_knf",
1241 "vdu_name": None,
1242 "type": "lxc_proxy_charm",
1243 "ee_descriptor_id": "openldap-ee",
1244 "charm_name": "",
1245 "ee_id": "",
1246 "application": "openldap-ee-z0-openldap-vdu",
1247 "model": "82b11965-e580-47c0-9ee0-329f318a305b",
1248 "config_sw_installed": True,
1249 },
1250 ]
1251 }
1252 }
1253 }
1254 mock_vnf_count_and_record = MagicMock()
1255 db_vnfr = {
1256 "member-vnf-index-ref": "openldap",
1257 "vdur": {},
1258 }
1259 vnf_count = "0"
1260 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1261 expected_result = "openldap-ee-z0-openldap-ldap-vdu"
1262 with patch.object(self.n2vc, "db", self.db), patch.object(
1263 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1264 ):
1265 application_name = self.n2vc._get_application_name(namespace)
1266 self.assertEqual(application_name, expected_result)
1267 self.assertLess(len(application_name), 50)
1268 mock_vnf_count_and_record.assert_called_once_with(
1269 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1270 )
1271 self.db.get_one.assert_called_once()
1272
1273 @patch(
1274 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1275 **{"return_value": "random"}
1276 )
1277 def test_get_application_name_vdu_charm_old_naming(
1278 self, mock_generate_random_alfanum
1279 ):
1280 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1281 self.db.get_one.return_value = {
1282 "_admin": {
1283 "deployed": {
1284 "VCA": [
1285 {
1286 "target_element": "vnf/vnf1/mgmtVM",
1287 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1288 "vdu_id": "mgmtVM",
1289 "kdu_name": None,
1290 "vdu_count_index": None,
1291 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1292 "vdu_name": "mgmtvm",
1293 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1294 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1295 },
1296 {
1297 "target_element": "ns",
1298 "member-vnf-index": None,
1299 "vdu_id": None,
1300 "kdu_name": None,
1301 "vdu_count_index": None,
1302 "vnfd_id": "",
1303 "vdu_name": "",
1304 "ee_descriptor_id": "",
1305 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1306 },
1307 ],
1308 },
1309 },
1310 }
1311 mock_vnf_count_and_record = MagicMock()
1312 db_vnfr = {
1313 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1314 "vdur": [
1315 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1316 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1317 ],
1318 }
1319 vnf_count = "0"
1320 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1321 expected_result = "app-vnf-eb3161eec0-z0-vdu-mgmtvm-cnt-z0-random"
1322
1323 with patch.object(self.n2vc, "db", self.db), patch.object(
1324 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1325 ):
1326 application_name = self.n2vc._get_application_name(namespace)
1327 self.assertEqual(application_name, expected_result)
1328 self.assertLess(len(application_name), 50)
1329 mock_vnf_count_and_record.assert_called_once_with(
1330 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1331 )
1332 self.db.get_one.assert_called_once()
1333
1334 def test_get_application_name_ns_charm(self):
1335 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1336 self.db.get_one.return_value = {
1337 "_admin": {
1338 "deployed": {
1339 "VCA": [
1340 {
1341 "target_element": "ns",
1342 "member-vnf-index": None,
1343 "vdu_id": None,
1344 "kdu_name": None,
1345 "vdu_count_index": None,
1346 "vnfd_id": "",
1347 "vdu_name": "",
1348 "ee_descriptor_id": "",
1349 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1350 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1351 },
1352 ],
1353 },
1354 },
1355 }
1356 mock_vnf_count_and_record = MagicMock()
1357 db_vnfr = {}
1358 vnf_count = ""
1359 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1360 expected_result = "simple-ns-charm-abc-z000-rrrr-nnnn-z4444-h-ns"
1361 with patch.object(self.n2vc, "db", self.db), patch.object(
1362 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1363 ):
1364 application_name = self.n2vc._get_application_name(namespace)
1365 self.assertEqual(application_name, expected_result)
1366 self.assertLess(len(application_name), 50)
1367 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1368 self.db.get_one.assert_called_once()
1369
1370 def test_get_application_name_ns_charm_empty_charm_name(self):
1371 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1372 self.db.get_one.return_value = {
1373 "_admin": {
1374 "deployed": {
1375 "VCA": [
1376 {
1377 "target_element": "ns",
1378 "member-vnf-index": None,
1379 "vdu_id": None,
1380 "kdu_name": None,
1381 "vdu_count_index": None,
1382 "vnfd_id": "",
1383 "vdu_name": "",
1384 "ee_descriptor_id": "",
1385 "charm_name": "",
1386 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1387 },
1388 ],
1389 },
1390 },
1391 }
1392 mock_vnf_count_and_record = MagicMock()
1393 db_vnfr = {}
1394 vnf_count = ""
1395 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1396 with patch.object(self.n2vc, "db", self.db), patch.object(
1397 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1398 ):
1399 with self.assertRaises(N2VCException):
1400 self.n2vc._get_application_name(namespace)
1401 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1402 self.db.get_one.assert_called_once()
1403
1404 @patch(
1405 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1406 **{"return_value": "random"}
1407 )
1408 def test_get_application_name_ns_charm_old_naming(
1409 self, mock_generate_random_alfanum
1410 ):
1411 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1412 self.db.get_one.return_value = {
1413 "_admin": {
1414 "deployed": {
1415 "VCA": [
1416 {
1417 "target_element": "ns",
1418 "member-vnf-index": None,
1419 "vdu_id": None,
1420 "kdu_name": None,
1421 "vdu_count_index": None,
1422 "vnfd_id": "",
1423 "vdu_name": "",
1424 "ee_descriptor_id": "",
1425 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1426 },
1427 ],
1428 },
1429 },
1430 }
1431 mock_vnf_count_and_record = MagicMock()
1432 db_vnfr = {}
1433 vnf_count = ""
1434 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1435 expected_result = "app-random"
1436 with patch.object(self.n2vc, "db", self.db), patch.object(
1437 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1438 ):
1439 application_name = self.n2vc._get_application_name(namespace)
1440 self.assertEqual(application_name, expected_result)
1441 self.assertLess(len(application_name), 50)
1442 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1443 self.db.get_one.assert_called_once()