Fix black errors
[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 vnf_id = "dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
385
386 def setUp(self):
387 super(GenerateApplicationNameTest, self).setUp()
388 self.db = MagicMock(DbMemory)
389
390 @patch(
391 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
392 **{"return_value": "random"}
393 )
394 def test_generate_backward_compatible_application_name(
395 self, mock_generate_random_alfanum
396 ):
397 vdu_id = "mgmtVM"
398 vdu_count = "0"
399 expected_result = "app-vnf-ec5ae0a53898-vdu-mgmtVM-cnt-0-random"
400
401 application_name = self.n2vc._generate_backward_compatible_application_name(
402 GenerateApplicationNameTest.vnf_id, vdu_id, vdu_count
403 )
404 self.assertEqual(application_name, expected_result)
405
406 @patch(
407 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
408 **{"return_value": "random"}
409 )
410 def test_generate_backward_compatible_application_name_without_vnf_id_vdu_id(
411 self, mock_generate_random_alfanum
412 ):
413 vnf_id = None
414 vdu_id = ""
415 vdu_count = None
416 expected_result = "app--random"
417 application_name = self.n2vc._generate_backward_compatible_application_name(
418 vnf_id, vdu_id, vdu_count
419 )
420
421 self.assertEqual(application_name, expected_result)
422 self.assertLess(len(application_name), 50)
423
424 def test_find_charm_level_with_vnf_id(self):
425 vdu_id = ""
426 expected_result = "vnf-level"
427 charm_level = self.n2vc._find_charm_level(
428 GenerateApplicationNameTest.vnf_id, vdu_id
429 )
430 self.assertEqual(charm_level, expected_result)
431
432 def test_find_charm_level_with_vdu_id(self):
433 vnf_id = ""
434 vdu_id = "mgmtVM"
435 with self.assertRaises(N2VCException):
436 self.n2vc._find_charm_level(vnf_id, vdu_id)
437
438 def test_find_charm_level_with_vnf_id_and_vdu_id(self):
439 vdu_id = "mgmtVM"
440 expected_result = "vdu-level"
441 charm_level = self.n2vc._find_charm_level(
442 GenerateApplicationNameTest.vnf_id, vdu_id
443 )
444 self.assertEqual(charm_level, expected_result)
445
446 def test_find_charm_level_without_vnf_id_and_vdu_id(self):
447 vnf_id = ""
448 vdu_id = ""
449 expected_result = "ns-level"
450 charm_level = self.n2vc._find_charm_level(vnf_id, vdu_id)
451 self.assertEqual(charm_level, expected_result)
452
453 def test_generate_application_name_ns_charm(self):
454 charm_level = "ns-level"
455 vnfrs = {}
456 vca_records = [
457 {
458 "target_element": "ns",
459 "member-vnf-index": "",
460 "vdu_id": None,
461 "kdu_name": None,
462 "vdu_count_index": None,
463 "vnfd_id": None,
464 "vdu_name": None,
465 "type": "proxy_charm",
466 "ee_descriptor_id": None,
467 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh",
468 "ee_id": None,
469 "application": "",
470 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
471 },
472 ]
473 vnf_count = ""
474 vdu_count = ""
475 vdu_id = None
476 expected_result = "simple-ns-charm-abc-000-rrrr-nnnn-4444-h-ns"
477 application_name = self.n2vc._generate_application_name(
478 charm_level,
479 vnfrs,
480 vca_records,
481 vnf_count=vnf_count,
482 vdu_id=vdu_id,
483 vdu_count=vdu_count,
484 )
485 self.assertEqual(application_name, expected_result)
486 self.assertLess(len(application_name), 50)
487
488 def test_generate_application_name_ns_charm_empty_vca_records(self):
489 charm_level = "ns-level"
490 vnfrs = {}
491 vca_records = []
492 vnf_count = ""
493 vdu_count = ""
494 vdu_id = None
495 with self.assertRaises(N2VCException):
496 self.n2vc._generate_application_name(
497 charm_level,
498 vnfrs,
499 vca_records,
500 vnf_count=vnf_count,
501 vdu_id=vdu_id,
502 vdu_count=vdu_count,
503 )
504
505 def test_generate_application_name_vnf_charm(self):
506 charm_level = "vnf-level"
507 vnfrs = {
508 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
509 }
510 vca_records = [
511 {
512 "target_element": "vnf/vnf1",
513 "member-vnf-index": "vnf111-xxx-yyy-zzz",
514 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
515 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
516 "charm_name": "",
517 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
518 },
519 ]
520 vnf_count = "1"
521 vdu_count = ""
522 vdu_id = None
523 expected_result = "simple-ee-ab-1-vnf111-xxx-y-vnf"
524 application_name = self.n2vc._generate_application_name(
525 charm_level,
526 vnfrs,
527 vca_records,
528 vnf_count=vnf_count,
529 vdu_id=vdu_id,
530 vdu_count=vdu_count,
531 )
532 self.assertEqual(application_name, expected_result)
533 self.assertLess(len(application_name), 50)
534
535 def test_generate_application_name_vdu_charm_kdu_name_in_vca_record_is_none(self):
536 charm_level = "vdu-level"
537 vnfrs = {
538 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
539 "vdur": [
540 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
541 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
542 ],
543 }
544 vca_records = [
545 {
546 "target_element": "vnf/vnf1/mgmtvm",
547 "member-vnf-index": "vnf111-xxx-yyy-zzz",
548 "vdu_id": "mgmtVM",
549 "kdu_name": None,
550 "vdu_count_index": None,
551 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
552 "vdu_name": "mgmtvm",
553 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
554 "charm_name": "",
555 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
556 },
557 {
558 "target_element": "vnf/vnf1/dataVM",
559 "member-vnf-index": "vnf111-xxx-yyy-zzz",
560 "vdu_id": "dataVM",
561 "kdu_name": None,
562 "vdu_count_index": None,
563 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
564 "vdu_name": "datavm",
565 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
566 "charm_name": "",
567 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
568 },
569 ]
570 vnf_count = "2"
571 vdu_count = "0"
572 vdu_id = "mgmtVM"
573 expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
574 application_name = self.n2vc._generate_application_name(
575 charm_level,
576 vnfrs,
577 vca_records,
578 vnf_count=vnf_count,
579 vdu_id=vdu_id,
580 vdu_count=vdu_count,
581 )
582 self.assertEqual(application_name, expected_result)
583 self.assertLess(len(application_name), 50)
584
585 def test_generate_application_name_vdu_charm_vdu_id_kdu_name_in_vca_record_are_both_set(
586 self,
587 ):
588 charm_level = "vdu-level"
589 vnfrs = {
590 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
591 "vdur": [
592 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
593 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
594 ],
595 }
596 vca_records = [
597 {
598 "target_element": "vnf/vnf1/mgmtVM",
599 "member-vnf-index": "vnf111-xxx-yyy-zzz",
600 "vdu_id": "mgmtVM",
601 "kdu_name": "mgmtVM",
602 "vdu_count_index": None,
603 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
604 "vdu_name": "mgmtvm",
605 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
606 "charm_name": "",
607 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
608 },
609 {
610 "target_element": "vnf/vnf1/dataVM",
611 "member-vnf-index": "vnf111-xxx-yyy-zzz",
612 "vdu_id": "dataVM",
613 "kdu_name": None,
614 "vdu_count_index": None,
615 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
616 "vdu_name": "datavm",
617 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
618 "charm_name": "",
619 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
620 },
621 ]
622 vnf_count = "2"
623 vdu_count = "0"
624 vdu_id = "mgmtVM"
625 expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
626 application_name = self.n2vc._generate_application_name(
627 charm_level,
628 vnfrs,
629 vca_records,
630 vnf_count=vnf_count,
631 vdu_id=vdu_id,
632 vdu_count=vdu_count,
633 )
634 self.assertEqual(application_name, expected_result)
635 self.assertLess(len(application_name), 50)
636
637 def test_generate_application_name_vdu_charm_both_vdu_id_kdu_name_in_vca_record_are_none(
638 self,
639 ):
640 charm_level = "vdu-level"
641 vnfrs = {
642 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
643 }
644 vca_records = [
645 {
646 "target_element": "vnf/vnf1/mgmtVM",
647 "member-vnf-index": "vnf111-xxx-yyy-zzz",
648 "vdu_id": None,
649 "kdu_name": None,
650 "vdu_count_index": None,
651 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
652 "vdu_name": "mgmtvm",
653 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
654 "charm_name": "",
655 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
656 },
657 ]
658 vnf_count = "2"
659 vdu_count = "0"
660 vdu_id = "mgmtVM"
661 with self.assertRaises(KeyError):
662 self.n2vc._generate_application_name(
663 charm_level,
664 vnfrs,
665 vca_records,
666 vnf_count=vnf_count,
667 vdu_id=vdu_id,
668 vdu_count=vdu_count,
669 )
670
671 def test_generate_application_name_vdu_charm_given_vdu_id_is_none(self):
672 charm_level = "vdu-level"
673 vnfrs = {
674 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
675 }
676 vca_records = [
677 {
678 "target_element": "vnf/vnf1/mgmtvVM",
679 "member-vnf-index": "vnf111-xxx-yyy-zzz",
680 "vdu_id": None,
681 "kdu_name": "mgmtVM",
682 "vdu_count_index": None,
683 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
684 "vdu_name": "mgmtvm",
685 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
686 "charm_name": "",
687 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
688 },
689 ]
690 vnf_count = "2"
691 vdu_count = "0"
692 vdu_id = None
693 with self.assertRaises(N2VCException):
694 self.n2vc._generate_application_name(
695 charm_level,
696 vnfrs,
697 vca_records,
698 vnf_count=vnf_count,
699 vdu_id=vdu_id,
700 vdu_count=vdu_count,
701 )
702
703 def test_generate_application_name_vdu_charm_vdu_id_does_not_match_with_the_key_in_vca_record(
704 self,
705 ):
706 charm_level = "vdu-level"
707 vnfrs = {
708 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
709 }
710 vca_records = [
711 {
712 "target_element": "vnf/vnf1/mgmtVM",
713 "member-vnf-index": "vnf111-xxx-yyy-zzz",
714 "vdu_id": None,
715 "kdu_name": "mgmtVM",
716 "vdu_count_index": None,
717 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
718 "vdu_name": "mgmtvm",
719 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
720 "charm_name": "",
721 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
722 },
723 ]
724 vnf_count = "2"
725 vdu_count = "0"
726 vdu_id = "mgmtvm"
727 with self.assertRaises(KeyError):
728 self.n2vc._generate_application_name(
729 charm_level,
730 vnfrs,
731 vca_records,
732 vnf_count=vnf_count,
733 vdu_id=vdu_id,
734 vdu_count=vdu_count,
735 )
736
737 def test_generate_application_name_vdu_charm_vdu_id_in_vca_record_is_none(self):
738 charm_level = "vdu-level"
739 vnfrs = {
740 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
741 }
742 vca_records = [
743 {
744 "target_element": "vnf/vnf1/mgmtVM",
745 "member-vnf-index": "vnf111-xxx-yyy-zzz",
746 "vdu_id": None,
747 "kdu_name": "mgmtVM",
748 "vdu_count_index": None,
749 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
750 "vdu_name": "mgmtvm",
751 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
752 "charm_name": "",
753 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
754 },
755 ]
756 vnf_count = "2"
757 vdu_count = "0"
758 vdu_id = "mgmtVM"
759 expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
760 application_name = self.n2vc._generate_application_name(
761 charm_level,
762 vnfrs,
763 vca_records,
764 vnf_count=vnf_count,
765 vdu_id=vdu_id,
766 vdu_count=vdu_count,
767 )
768 self.assertEqual(application_name, expected_result)
769 self.assertLess(len(application_name), 50)
770
771 def test_get_vnf_count_db_vnfr_ns_charm(self):
772 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
773 charm_level = "ns-level"
774 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
775 with patch.object(self.n2vc, "db", self.db):
776 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
777 charm_level, vnf_id_and_count
778 )
779 self.assertEqual(vnf_count, "")
780 self.assertEqual(db_vnfr, {})
781
782 def test_get_vnf_count_db_vnfr_vnf_charm(self):
783 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
784 charm_level = "vnf-level"
785 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
786 with patch.object(self.n2vc, "db", self.db):
787 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
788 charm_level, vnf_id_and_count
789 )
790 self.assertEqual(vnf_count, "4")
791 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
792
793 def test_get_vnf_count_db_vnfr_vdu_charm(self):
794 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
795 charm_level = "vdu-level"
796 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-2"
797 with patch.object(self.n2vc, "db", self.db):
798 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
799 charm_level, vnf_id_and_count
800 )
801 self.assertEqual(vnf_count, "2")
802 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
803
804 def test_get_vca_records_vdu_charm(self):
805 charm_level = "vdu-level"
806 db_vnfr = {
807 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
808 "vdur": [
809 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
810 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
811 ],
812 }
813 db_nsr = {
814 "_admin": {
815 "deployed": {
816 "VCA": [
817 {
818 "target_element": "vnf/vnf1/mgmtvm",
819 "member-vnf-index": "vnf111-xxx-yyy-zzz",
820 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
821 "kdu_name": None,
822 "vdu_count_index": None,
823 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
824 "vdu_name": "mgmtvm",
825 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
826 "charm_name": "",
827 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
828 },
829 {
830 "target_element": "vnf/vnf2/datavm",
831 "member-vnf-index": "vnf222-xxx-yyy-zzz",
832 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
833 "kdu_name": None,
834 "vdu_count_index": None,
835 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
836 "vdu_name": "datavm",
837 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
838 "charm_name": "",
839 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
840 },
841 ],
842 },
843 },
844 }
845 expected_result = [
846 {
847 "target_element": "vnf/vnf1/mgmtvm",
848 "member-vnf-index": "vnf111-xxx-yyy-zzz",
849 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
850 "kdu_name": None,
851 "vdu_count_index": None,
852 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
853 "vdu_name": "mgmtvm",
854 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
855 "charm_name": "",
856 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
857 },
858 ]
859 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
860 self.assertEqual(vca_records, expected_result)
861
862 def test_get_vca_records_vnf_charm_member_vnf_index_mismatch(self):
863 charm_level = "vnf-level"
864 db_vnfr = {
865 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
866 }
867 db_nsr = {
868 "_admin": {
869 "deployed": {
870 "VCA": [
871 {
872 "target_element": "vnf/vnf1/mgmtvm",
873 "member-vnf-index": "vnf111-xxx-yyy-zzz",
874 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
875 "kdu_name": None,
876 "vdu_count_index": None,
877 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
878 "vdu_name": "mgmtvm",
879 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
880 "charm_name": "",
881 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
882 },
883 {
884 "target_element": "vnf/vnf1/mgmtvm",
885 "member-vnf-index": "vnf111-xxx-yyy-zzz",
886 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
887 "kdu_name": None,
888 "vdu_count_index": None,
889 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
890 "vdu_name": "datavm",
891 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
892 "charm_name": "",
893 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
894 },
895 ],
896 },
897 },
898 }
899 expected_result = []
900 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
901 self.assertEqual(vca_records, expected_result)
902
903 def test_get_vca_records_ns_charm(self):
904 charm_level = "ns-level"
905 db_vnfr = {
906 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
907 }
908 db_nsr = {
909 "_admin": {
910 "deployed": {
911 "VCA": [
912 {
913 "target_element": "vnf/vnf1/mgmtvm",
914 "member-vnf-index": "vnf111-xxx-yyy-zzz",
915 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
916 "kdu_name": None,
917 "vdu_count_index": None,
918 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
919 "vdu_name": "mgmtvm",
920 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
921 "charm_name": "",
922 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
923 },
924 {
925 "target_element": "ns",
926 "member-vnf-index": None,
927 "vdu_id": None,
928 "kdu_name": None,
929 "vdu_count_index": None,
930 "vnfd_id": "",
931 "vdu_name": "",
932 "ee_descriptor_id": "",
933 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
934 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
935 },
936 ],
937 },
938 },
939 }
940 expected_result = [
941 {
942 "target_element": "ns",
943 "member-vnf-index": None,
944 "vdu_id": None,
945 "kdu_name": None,
946 "vdu_count_index": None,
947 "vnfd_id": "",
948 "vdu_name": "",
949 "ee_descriptor_id": "",
950 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
951 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
952 },
953 ]
954 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
955 self.assertEqual(vca_records, expected_result)
956
957 def test_get_vca_records_ns_charm_empty_charm_name(self):
958 charm_level = "ns-level"
959 db_vnfr = {
960 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
961 }
962 db_nsr = {
963 "_admin": {
964 "deployed": {
965 "VCA": [
966 {
967 "target_element": "vnf/vnf1/mgmtvm",
968 "member-vnf-index": "vnf111-xxx-yyy-zzz",
969 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
970 "kdu_name": None,
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",
975 "charm_name": "",
976 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
977 },
978 {
979 "target_element": "ns",
980 "member-vnf-index": None,
981 "vdu_id": None,
982 "kdu_name": None,
983 "vdu_count_index": None,
984 "vnfd_id": "",
985 "vdu_name": "",
986 "ee_descriptor_id": "",
987 "charm_name": "",
988 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
989 },
990 ],
991 },
992 },
993 }
994 expected_result = [
995 {
996 "target_element": "ns",
997 "member-vnf-index": None,
998 "vdu_id": None,
999 "kdu_name": None,
1000 "vdu_count_index": None,
1001 "vnfd_id": "",
1002 "vdu_name": "",
1003 "ee_descriptor_id": "",
1004 "charm_name": "",
1005 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1006 },
1007 ]
1008 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
1009 self.assertEqual(vca_records, expected_result)
1010
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 = {
1014 "_admin": {
1015 "deployed": {
1016 "VCA": [
1017 {
1018 "target_element": "vnf/vnf1/mgmtvm",
1019 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1020 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1021 "kdu_name": None,
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",
1026 "charm_name": "",
1027 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1028 },
1029 {
1030 "target_element": "ns",
1031 "member-vnf-index": None,
1032 "vdu_id": None,
1033 "kdu_name": None,
1034 "vdu_count_index": None,
1035 "vnfd_id": "",
1036 "vdu_name": "",
1037 "ee_descriptor_id": "",
1038 "charm_name": "",
1039 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1040 },
1041 ],
1042 },
1043 },
1044 }
1045 mock_vnf_count_and_record = MagicMock()
1046 db_vnfr = {
1047 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1048 }
1049 vnf_count = "0"
1050 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1051 expected_result = "simple-ee-ab-z0-vnf111-xxx-y-vnf"
1052 with patch.object(self.n2vc, "db", self.db), patch.object(
1053 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1054 ):
1055 application_name = self.n2vc._get_application_name(namespace)
1056 self.assertEqual(application_name, expected_result)
1057 self.assertLess(len(application_name), 50)
1058 mock_vnf_count_and_record.assert_called_once_with(
1059 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1060 )
1061 self.db.get_one.assert_called_once()
1062
1063 @patch(
1064 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1065 **{"return_value": "random"}
1066 )
1067 def test_get_application_name_vnf_charm_old_naming(
1068 self, mock_generate_random_alfanum
1069 ):
1070 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1071 self.db.get_one.return_value = {
1072 "_admin": {
1073 "deployed": {
1074 "VCA": [
1075 {
1076 "target_element": "vnf/vnf1/mgmtvm",
1077 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1078 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1079 "kdu_name": None,
1080 "vdu_count_index": None,
1081 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1082 "vdu_name": "mgmtvm",
1083 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1084 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1085 },
1086 {
1087 "target_element": "ns",
1088 "member-vnf-index": None,
1089 "vdu_id": None,
1090 "kdu_name": None,
1091 "vdu_count_index": None,
1092 "vnfd_id": "",
1093 "vdu_name": "",
1094 "ee_descriptor_id": "",
1095 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1096 },
1097 ],
1098 },
1099 },
1100 }
1101 mock_vnf_count_and_record = MagicMock()
1102 db_vnfr = {
1103 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1104 }
1105 vnf_count = "0"
1106 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1107 expected_result = "app-vnf-eb3161eec0-z0-random"
1108 with patch.object(self.n2vc, "db", self.db), patch.object(
1109 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1110 ):
1111 application_name = self.n2vc._get_application_name(namespace)
1112 self.assertEqual(application_name, expected_result)
1113 mock_vnf_count_and_record.assert_called_once_with(
1114 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1115 )
1116 self.db.get_one.assert_called_once()
1117
1118 def test_get_application_name_vnf_charm_vnf_index_ref_mismatch(self):
1119 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1120 self.db.get_one.return_value = {
1121 "_admin": {
1122 "deployed": {
1123 "VCA": [
1124 {
1125 "target_element": "vnf/vnf1/mgmtvm",
1126 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1127 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1128 "kdu_name": None,
1129 "vdu_count_index": None,
1130 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1131 "vdu_name": "mgmtvm",
1132 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1133 "charm_name": "",
1134 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1135 },
1136 {
1137 "target_element": "ns",
1138 "member-vnf-index": None,
1139 "vdu_id": None,
1140 "kdu_name": None,
1141 "vdu_count_index": None,
1142 "vnfd_id": "",
1143 "vdu_name": "",
1144 "ee_descriptor_id": "",
1145 "charm_name": "",
1146 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1147 },
1148 ],
1149 },
1150 },
1151 }
1152 mock_vnf_count_and_record = MagicMock()
1153 db_vnfr = {
1154 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
1155 }
1156 vnf_count = "0"
1157 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1158 with patch.object(self.n2vc, "db", self.db), patch.object(
1159 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1160 ):
1161 with self.assertRaises(N2VCException):
1162 self.n2vc._get_application_name(namespace)
1163 mock_vnf_count_and_record.assert_called_once_with(
1164 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1165 )
1166 self.db.get_one.assert_called_once()
1167
1168 def test_get_application_name_vdu_charm(self):
1169 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1170 self.db.get_one.return_value = {
1171 "_admin": {
1172 "deployed": {
1173 "VCA": [
1174 {
1175 "target_element": "vnf/vnf1/mgmtvm",
1176 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1177 "vdu_id": "mgmtVM",
1178 "kdu_name": None,
1179 "vdu_count_index": None,
1180 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1181 "vdu_name": "mgmtvm",
1182 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1183 "charm_name": "",
1184 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1185 },
1186 {
1187 "target_element": "ns",
1188 "member-vnf-index": None,
1189 "vdu_id": None,
1190 "kdu_name": None,
1191 "vdu_count_index": None,
1192 "vnfd_id": "",
1193 "vdu_name": "",
1194 "ee_descriptor_id": "",
1195 "charm_name": "",
1196 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1197 },
1198 ]
1199 }
1200 }
1201 }
1202 mock_vnf_count_and_record = MagicMock()
1203 db_vnfr = {
1204 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1205 "vdur": [
1206 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1207 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1208 ],
1209 }
1210 vnf_count = "0"
1211 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1212 expected_result = "simple-ee-ab-z0-vnf111-xxx-y-mgmtvm-z0-vdu"
1213 with patch.object(self.n2vc, "db", self.db), patch.object(
1214 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1215 ):
1216 application_name = self.n2vc._get_application_name(namespace)
1217 self.assertEqual(application_name, expected_result)
1218 self.assertLess(len(application_name), 50)
1219 mock_vnf_count_and_record.assert_called_once_with(
1220 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1221 )
1222 self.db.get_one.assert_called_once()
1223
1224 def test_get_application_name_kdu_charm(self):
1225 namespace = ".82b11965-e580-47c0-9ee0-329f318a305b.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.ldap"
1226 self.db.get_one.return_value = {
1227 "_admin": {
1228 "deployed": {
1229 "VCA": [
1230 {
1231 "target_element": "vnf/openldap/kdu/ldap",
1232 "member-vnf-index": "openldap",
1233 "vdu_id": None,
1234 "kdu_name": "ldap",
1235 "vdu_count_index": 0,
1236 "operational-status": "init",
1237 "detailed-status": "",
1238 "step": "initial-deploy",
1239 "vnfd_id": "openldap_knf",
1240 "vdu_name": None,
1241 "type": "lxc_proxy_charm",
1242 "ee_descriptor_id": "openldap-ee",
1243 "charm_name": "",
1244 "ee_id": "",
1245 "application": "openldap-ee-z0-openldap-vdu",
1246 "model": "82b11965-e580-47c0-9ee0-329f318a305b",
1247 "config_sw_installed": True,
1248 },
1249 ]
1250 }
1251 }
1252 }
1253 mock_vnf_count_and_record = MagicMock()
1254 db_vnfr = {
1255 "member-vnf-index-ref": "openldap",
1256 "vdur": {},
1257 }
1258 vnf_count = "0"
1259 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1260 expected_result = "openldap-ee-z0-openldap-ldap-vdu"
1261 with patch.object(self.n2vc, "db", self.db), patch.object(
1262 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1263 ):
1264 application_name = self.n2vc._get_application_name(namespace)
1265 self.assertEqual(application_name, expected_result)
1266 self.assertLess(len(application_name), 50)
1267 mock_vnf_count_and_record.assert_called_once_with(
1268 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1269 )
1270 self.db.get_one.assert_called_once()
1271
1272 @patch(
1273 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1274 **{"return_value": "random"}
1275 )
1276 def test_get_application_name_vdu_charm_old_naming(
1277 self, mock_generate_random_alfanum
1278 ):
1279 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1280 self.db.get_one.return_value = {
1281 "_admin": {
1282 "deployed": {
1283 "VCA": [
1284 {
1285 "target_element": "vnf/vnf1/mgmtVM",
1286 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1287 "vdu_id": "mgmtVM",
1288 "kdu_name": None,
1289 "vdu_count_index": None,
1290 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1291 "vdu_name": "mgmtvm",
1292 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1293 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1294 },
1295 {
1296 "target_element": "ns",
1297 "member-vnf-index": None,
1298 "vdu_id": None,
1299 "kdu_name": None,
1300 "vdu_count_index": None,
1301 "vnfd_id": "",
1302 "vdu_name": "",
1303 "ee_descriptor_id": "",
1304 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1305 },
1306 ],
1307 },
1308 },
1309 }
1310 mock_vnf_count_and_record = MagicMock()
1311 db_vnfr = {
1312 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1313 "vdur": [
1314 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1315 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1316 ],
1317 }
1318 vnf_count = "0"
1319 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1320 expected_result = "app-vnf-eb3161eec0-z0-vdu-mgmtvm-cnt-z0-random"
1321
1322 with patch.object(self.n2vc, "db", self.db), patch.object(
1323 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1324 ):
1325 application_name = self.n2vc._get_application_name(namespace)
1326 self.assertEqual(application_name, expected_result)
1327 self.assertLess(len(application_name), 50)
1328 mock_vnf_count_and_record.assert_called_once_with(
1329 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1330 )
1331 self.db.get_one.assert_called_once()
1332
1333 def test_get_application_name_ns_charm(self):
1334 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1335 self.db.get_one.return_value = {
1336 "_admin": {
1337 "deployed": {
1338 "VCA": [
1339 {
1340 "target_element": "ns",
1341 "member-vnf-index": None,
1342 "vdu_id": None,
1343 "kdu_name": None,
1344 "vdu_count_index": None,
1345 "vnfd_id": "",
1346 "vdu_name": "",
1347 "ee_descriptor_id": "",
1348 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1349 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1350 },
1351 ],
1352 },
1353 },
1354 }
1355 mock_vnf_count_and_record = MagicMock()
1356 db_vnfr = {}
1357 vnf_count = ""
1358 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1359 expected_result = "simple-ns-charm-abc-z000-rrrr-nnnn-z4444-h-ns"
1360 with patch.object(self.n2vc, "db", self.db), patch.object(
1361 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1362 ):
1363 application_name = self.n2vc._get_application_name(namespace)
1364 self.assertEqual(application_name, expected_result)
1365 self.assertLess(len(application_name), 50)
1366 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1367 self.db.get_one.assert_called_once()
1368
1369 def test_get_application_name_ns_charm_empty_charm_name(self):
1370 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1371 self.db.get_one.return_value = {
1372 "_admin": {
1373 "deployed": {
1374 "VCA": [
1375 {
1376 "target_element": "ns",
1377 "member-vnf-index": None,
1378 "vdu_id": None,
1379 "kdu_name": None,
1380 "vdu_count_index": None,
1381 "vnfd_id": "",
1382 "vdu_name": "",
1383 "ee_descriptor_id": "",
1384 "charm_name": "",
1385 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1386 },
1387 ],
1388 },
1389 },
1390 }
1391 mock_vnf_count_and_record = MagicMock()
1392 db_vnfr = {}
1393 vnf_count = ""
1394 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1395 with patch.object(self.n2vc, "db", self.db), patch.object(
1396 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1397 ):
1398 with self.assertRaises(N2VCException):
1399 self.n2vc._get_application_name(namespace)
1400 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1401 self.db.get_one.assert_called_once()
1402
1403 @patch(
1404 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1405 **{"return_value": "random"}
1406 )
1407 def test_get_application_name_ns_charm_old_naming(
1408 self, mock_generate_random_alfanum
1409 ):
1410 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1411 self.db.get_one.return_value = {
1412 "_admin": {
1413 "deployed": {
1414 "VCA": [
1415 {
1416 "target_element": "ns",
1417 "member-vnf-index": None,
1418 "vdu_id": None,
1419 "kdu_name": None,
1420 "vdu_count_index": None,
1421 "vnfd_id": "",
1422 "vdu_name": "",
1423 "ee_descriptor_id": "",
1424 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1425 },
1426 ],
1427 },
1428 },
1429 }
1430 mock_vnf_count_and_record = MagicMock()
1431 db_vnfr = {}
1432 vnf_count = ""
1433 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1434 expected_result = "app-random"
1435 with patch.object(self.n2vc, "db", self.db), patch.object(
1436 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1437 ):
1438 application_name = self.n2vc._get_application_name(namespace)
1439 self.assertEqual(application_name, expected_result)
1440 self.assertLess(len(application_name), 50)
1441 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1442 self.db.get_one.assert_called_once()