Fix Bug 2181
[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(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": "45512ff7-5bdd-4228-911f-c2bee259c44a",
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_none(self):
587 charm_level = "vdu-level"
588 vnfrs = {
589 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
590 }
591 vca_records = [
592 {
593 "target_element": "vnf/vnf1/mgmtvm",
594 "member-vnf-index": "vnf111-xxx-yyy-zzz",
595 "vdu_id": None,
596 "kdu_name": None,
597 "vdu_count_index": None,
598 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
599 "vdu_name": "mgmtvm",
600 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
601 "charm_name": "",
602 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
603 },
604 ]
605 vnf_count = "2"
606 vdu_count = "0"
607 vdu_id = None
608 with self.assertRaises(N2VCException):
609 self.n2vc._generate_application_name(
610 charm_level,
611 vnfrs,
612 vca_records,
613 vnf_count=vnf_count,
614 vdu_id=vdu_id,
615 vdu_count=vdu_count,
616 )
617
618 def test_get_vnf_count_db_vnfr_ns_charm(self):
619 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
620 charm_level = "ns-level"
621 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
622 with patch.object(self.n2vc, "db", self.db):
623 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
624 charm_level, vnf_id_and_count
625 )
626 self.assertEqual(vnf_count, "")
627 self.assertEqual(db_vnfr, {})
628
629 def test_get_vnf_count_db_vnfr_vnf_charm(self):
630 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
631 charm_level = "vnf-level"
632 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
633 with patch.object(self.n2vc, "db", self.db):
634 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
635 charm_level, vnf_id_and_count
636 )
637 self.assertEqual(vnf_count, "4")
638 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
639
640 def test_get_vnf_count_db_vnfr_vdu_charm(self):
641 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
642 charm_level = "vdu-level"
643 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-2"
644 with patch.object(self.n2vc, "db", self.db):
645 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
646 charm_level, vnf_id_and_count
647 )
648 self.assertEqual(vnf_count, "2")
649 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
650
651 def test_get_vca_records_vdu_charm(self):
652 charm_level = "vdu-level"
653 db_vnfr = {
654 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
655 "vdur": [
656 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
657 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
658 ],
659 }
660 db_nsr = {
661 "_admin": {
662 "deployed": {
663 "VCA": [
664 {
665 "target_element": "vnf/vnf1/mgmtvm",
666 "member-vnf-index": "vnf111-xxx-yyy-zzz",
667 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
668 "kdu_name": None,
669 "vdu_count_index": None,
670 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
671 "vdu_name": "mgmtvm",
672 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
673 "charm_name": "",
674 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
675 },
676 {
677 "target_element": "vnf/vnf2/datavm",
678 "member-vnf-index": "vnf222-xxx-yyy-zzz",
679 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
680 "kdu_name": None,
681 "vdu_count_index": None,
682 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
683 "vdu_name": "datavm",
684 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
685 "charm_name": "",
686 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
687 },
688 ],
689 },
690 },
691 }
692 expected_result = [
693 {
694 "target_element": "vnf/vnf1/mgmtvm",
695 "member-vnf-index": "vnf111-xxx-yyy-zzz",
696 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
697 "kdu_name": None,
698 "vdu_count_index": None,
699 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
700 "vdu_name": "mgmtvm",
701 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
702 "charm_name": "",
703 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
704 },
705 ]
706 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
707 self.assertEqual(vca_records, expected_result)
708
709 def test_get_vca_records_vnf_charm_member_vnf_index_mismatch(self):
710 charm_level = "vnf-level"
711 db_vnfr = {
712 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
713 }
714 db_nsr = {
715 "_admin": {
716 "deployed": {
717 "VCA": [
718 {
719 "target_element": "vnf/vnf1/mgmtvm",
720 "member-vnf-index": "vnf111-xxx-yyy-zzz",
721 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
722 "kdu_name": None,
723 "vdu_count_index": None,
724 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
725 "vdu_name": "mgmtvm",
726 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
727 "charm_name": "",
728 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
729 },
730 {
731 "target_element": "vnf/vnf1/mgmtvm",
732 "member-vnf-index": "vnf111-xxx-yyy-zzz",
733 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
734 "kdu_name": None,
735 "vdu_count_index": None,
736 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
737 "vdu_name": "datavm",
738 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
739 "charm_name": "",
740 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
741 },
742 ],
743 },
744 },
745 }
746 expected_result = []
747 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
748 self.assertEqual(vca_records, expected_result)
749
750 def test_get_vca_records_ns_charm(self):
751 charm_level = "ns-level"
752 db_vnfr = {
753 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
754 }
755 db_nsr = {
756 "_admin": {
757 "deployed": {
758 "VCA": [
759 {
760 "target_element": "vnf/vnf1/mgmtvm",
761 "member-vnf-index": "vnf111-xxx-yyy-zzz",
762 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
763 "kdu_name": None,
764 "vdu_count_index": None,
765 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
766 "vdu_name": "mgmtvm",
767 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
768 "charm_name": "",
769 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
770 },
771 {
772 "target_element": "ns",
773 "member-vnf-index": None,
774 "vdu_id": None,
775 "kdu_name": None,
776 "vdu_count_index": None,
777 "vnfd_id": "",
778 "vdu_name": "",
779 "ee_descriptor_id": "",
780 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
781 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
782 },
783 ],
784 },
785 },
786 }
787 expected_result = [
788 {
789 "target_element": "ns",
790 "member-vnf-index": None,
791 "vdu_id": None,
792 "kdu_name": None,
793 "vdu_count_index": None,
794 "vnfd_id": "",
795 "vdu_name": "",
796 "ee_descriptor_id": "",
797 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
798 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
799 },
800 ]
801 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
802 self.assertEqual(vca_records, expected_result)
803
804 def test_get_vca_records_ns_charm_empty_charm_name(self):
805 charm_level = "ns-level"
806 db_vnfr = {
807 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
808 }
809 db_nsr = {
810 "_admin": {
811 "deployed": {
812 "VCA": [
813 {
814 "target_element": "vnf/vnf1/mgmtvm",
815 "member-vnf-index": "vnf111-xxx-yyy-zzz",
816 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
817 "kdu_name": None,
818 "vdu_count_index": None,
819 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
820 "vdu_name": "mgmtvm",
821 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
822 "charm_name": "",
823 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
824 },
825 {
826 "target_element": "ns",
827 "member-vnf-index": None,
828 "vdu_id": None,
829 "kdu_name": None,
830 "vdu_count_index": None,
831 "vnfd_id": "",
832 "vdu_name": "",
833 "ee_descriptor_id": "",
834 "charm_name": "",
835 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
836 },
837 ],
838 },
839 },
840 }
841 expected_result = [
842 {
843 "target_element": "ns",
844 "member-vnf-index": None,
845 "vdu_id": None,
846 "kdu_name": None,
847 "vdu_count_index": None,
848 "vnfd_id": "",
849 "vdu_name": "",
850 "ee_descriptor_id": "",
851 "charm_name": "",
852 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
853 },
854 ]
855 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
856 self.assertEqual(vca_records, expected_result)
857
858 def test_get_application_name_vnf_charm(self):
859 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
860 self.db.get_one.return_value = {
861 "_admin": {
862 "deployed": {
863 "VCA": [
864 {
865 "target_element": "vnf/vnf1/mgmtvm",
866 "member-vnf-index": "vnf111-xxx-yyy-zzz",
867 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
868 "kdu_name": None,
869 "vdu_count_index": None,
870 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
871 "vdu_name": "mgmtvm",
872 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
873 "charm_name": "",
874 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
875 },
876 {
877 "target_element": "ns",
878 "member-vnf-index": None,
879 "vdu_id": None,
880 "kdu_name": None,
881 "vdu_count_index": None,
882 "vnfd_id": "",
883 "vdu_name": "",
884 "ee_descriptor_id": "",
885 "charm_name": "",
886 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
887 },
888 ],
889 },
890 },
891 }
892 mock_vnf_count_and_record = MagicMock()
893 db_vnfr = {
894 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
895 }
896 vnf_count = "0"
897 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
898 expected_result = "simple-ee-ab-z0-vnf111-xxx-y-vnf"
899 with patch.object(self.n2vc, "db", self.db), patch.object(
900 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
901 ):
902 application_name = self.n2vc._get_application_name(namespace)
903 self.assertEqual(application_name, expected_result)
904 self.assertLess(len(application_name), 50)
905 mock_vnf_count_and_record.assert_called_once_with(
906 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
907 )
908 self.db.get_one.assert_called_once()
909
910 @patch(
911 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
912 **{"return_value": "random"}
913 )
914 def test_get_application_name_vnf_charm_old_naming(
915 self, mock_generate_random_alfanum
916 ):
917 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
918 self.db.get_one.return_value = {
919 "_admin": {
920 "deployed": {
921 "VCA": [
922 {
923 "target_element": "vnf/vnf1/mgmtvm",
924 "member-vnf-index": "vnf111-xxx-yyy-zzz",
925 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
926 "kdu_name": None,
927 "vdu_count_index": None,
928 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
929 "vdu_name": "mgmtvm",
930 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
931 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
932 },
933 {
934 "target_element": "ns",
935 "member-vnf-index": None,
936 "vdu_id": None,
937 "kdu_name": None,
938 "vdu_count_index": None,
939 "vnfd_id": "",
940 "vdu_name": "",
941 "ee_descriptor_id": "",
942 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
943 },
944 ],
945 },
946 },
947 }
948 mock_vnf_count_and_record = MagicMock()
949 db_vnfr = {
950 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
951 }
952 vnf_count = "0"
953 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
954 expected_result = "app-vnf-eb3161eec0-z0-random"
955 with patch.object(self.n2vc, "db", self.db), patch.object(
956 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
957 ):
958 application_name = self.n2vc._get_application_name(namespace)
959 self.assertEqual(application_name, expected_result)
960 mock_vnf_count_and_record.assert_called_once_with(
961 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
962 )
963 self.db.get_one.assert_called_once()
964
965 def test_get_application_name_vnf_charm_vnf_index_ref_mismatch(self):
966 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
967 self.db.get_one.return_value = {
968 "_admin": {
969 "deployed": {
970 "VCA": [
971 {
972 "target_element": "vnf/vnf1/mgmtvm",
973 "member-vnf-index": "vnf111-xxx-yyy-zzz",
974 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
975 "kdu_name": None,
976 "vdu_count_index": None,
977 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
978 "vdu_name": "mgmtvm",
979 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
980 "charm_name": "",
981 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
982 },
983 {
984 "target_element": "ns",
985 "member-vnf-index": None,
986 "vdu_id": None,
987 "kdu_name": None,
988 "vdu_count_index": None,
989 "vnfd_id": "",
990 "vdu_name": "",
991 "ee_descriptor_id": "",
992 "charm_name": "",
993 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
994 },
995 ],
996 },
997 },
998 }
999 mock_vnf_count_and_record = MagicMock()
1000 db_vnfr = {
1001 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
1002 }
1003 vnf_count = "0"
1004 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1005 with patch.object(self.n2vc, "db", self.db), patch.object(
1006 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1007 ):
1008 with self.assertRaises(N2VCException):
1009 self.n2vc._get_application_name(namespace)
1010 mock_vnf_count_and_record.assert_called_once_with(
1011 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1012 )
1013 self.db.get_one.assert_called_once()
1014
1015 def test_get_application_name_vdu_charm(self):
1016 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1017 self.db.get_one.return_value = {
1018 "_admin": {
1019 "deployed": {
1020 "VCA": [
1021 {
1022 "target_element": "vnf/vnf1/mgmtvm",
1023 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1024 "vdu_id": "mgmtVM",
1025 "kdu_name": None,
1026 "vdu_count_index": None,
1027 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1028 "vdu_name": "mgmtvm",
1029 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1030 "charm_name": "",
1031 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1032 },
1033 {
1034 "target_element": "ns",
1035 "member-vnf-index": None,
1036 "vdu_id": None,
1037 "kdu_name": None,
1038 "vdu_count_index": None,
1039 "vnfd_id": "",
1040 "vdu_name": "",
1041 "ee_descriptor_id": "",
1042 "charm_name": "",
1043 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1044 },
1045 ]
1046 }
1047 }
1048 }
1049 mock_vnf_count_and_record = MagicMock()
1050 db_vnfr = {
1051 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1052 "vdur": [
1053 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1054 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1055 ],
1056 }
1057 vnf_count = "0"
1058 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1059 expected_result = "simple-ee-ab-z0-vnf111-xxx-y-mgmtvm-z0-vdu"
1060 with patch.object(self.n2vc, "db", self.db), patch.object(
1061 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1062 ):
1063 application_name = self.n2vc._get_application_name(namespace)
1064 self.assertEqual(application_name, expected_result)
1065 self.assertLess(len(application_name), 50)
1066 mock_vnf_count_and_record.assert_called_once_with(
1067 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1068 )
1069 self.db.get_one.assert_called_once()
1070
1071 def test_get_application_name_kdu_charm(self):
1072 namespace = ".82b11965-e580-47c0-9ee0-329f318a305b.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.ldap"
1073 self.db.get_one.return_value = {
1074 "_admin": {
1075 "deployed": {
1076 "VCA": [
1077 {
1078 "target_element": "vnf/openldap/kdu/ldap",
1079 "member-vnf-index": "openldap",
1080 "vdu_id": "ldap",
1081 "kdu_name": "ldap",
1082 "vdu_count_index": 0,
1083 "operational-status": "init",
1084 "detailed-status": "",
1085 "step": "initial-deploy",
1086 "vnfd_id": "openldap_knf",
1087 "vdu_name": None,
1088 "type": "lxc_proxy_charm",
1089 "ee_descriptor_id": "openldap-ee",
1090 "charm_name": "",
1091 "ee_id": "",
1092 "application": "openldap-ee-z0-openldap-vdu",
1093 "model": "82b11965-e580-47c0-9ee0-329f318a305b",
1094 "config_sw_installed": True,
1095 },
1096 ]
1097 }
1098 }
1099 }
1100 mock_vnf_count_and_record = MagicMock()
1101 db_vnfr = {
1102 "member-vnf-index-ref": "openldap",
1103 "vdur": {},
1104 }
1105 vnf_count = "0"
1106 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1107 expected_result = "openldap-ee-z0-openldap-ldap-vdu"
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 self.assertLess(len(application_name), 50)
1114 mock_vnf_count_and_record.assert_called_once_with(
1115 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1116 )
1117 self.db.get_one.assert_called_once()
1118
1119 @patch(
1120 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1121 **{"return_value": "random"}
1122 )
1123 def test_get_application_name_vdu_charm_old_naming(
1124 self, mock_generate_random_alfanum
1125 ):
1126 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1127 self.db.get_one.return_value = {
1128 "_admin": {
1129 "deployed": {
1130 "VCA": [
1131 {
1132 "target_element": "vnf/vnf1/mgmtvm",
1133 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1134 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1135 "kdu_name": None,
1136 "vdu_count_index": None,
1137 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1138 "vdu_name": "mgmtvm",
1139 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1140 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1141 },
1142 {
1143 "target_element": "ns",
1144 "member-vnf-index": None,
1145 "vdu_id": None,
1146 "kdu_name": None,
1147 "vdu_count_index": None,
1148 "vnfd_id": "",
1149 "vdu_name": "",
1150 "ee_descriptor_id": "",
1151 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1152 },
1153 ],
1154 },
1155 },
1156 }
1157 mock_vnf_count_and_record = MagicMock()
1158 db_vnfr = {
1159 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1160 "vdur": [
1161 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1162 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1163 ],
1164 }
1165 vnf_count = "0"
1166 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1167 expected_result = "app-vnf-eb3161eec0-z0-vdu-mgmtvm-cnt-z0-random"
1168
1169 with patch.object(self.n2vc, "db", self.db), patch.object(
1170 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1171 ):
1172 application_name = self.n2vc._get_application_name(namespace)
1173 self.assertEqual(application_name, expected_result)
1174 self.assertLess(len(application_name), 50)
1175 mock_vnf_count_and_record.assert_called_once_with(
1176 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1177 )
1178 self.db.get_one.assert_called_once()
1179
1180 def test_get_application_name_ns_charm(self):
1181 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1182 self.db.get_one.return_value = {
1183 "_admin": {
1184 "deployed": {
1185 "VCA": [
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": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1196 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1197 },
1198 ],
1199 },
1200 },
1201 }
1202 mock_vnf_count_and_record = MagicMock()
1203 db_vnfr = {}
1204 vnf_count = ""
1205 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1206 expected_result = "simple-ns-charm-abc-z000-rrrr-nnnn-z4444-h-ns"
1207 with patch.object(self.n2vc, "db", self.db), patch.object(
1208 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1209 ):
1210 application_name = self.n2vc._get_application_name(namespace)
1211 self.assertEqual(application_name, expected_result)
1212 self.assertLess(len(application_name), 50)
1213 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1214 self.db.get_one.assert_called_once()
1215
1216 def test_get_application_name_ns_charm_empty_charm_name(self):
1217 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1218 self.db.get_one.return_value = {
1219 "_admin": {
1220 "deployed": {
1221 "VCA": [
1222 {
1223 "target_element": "ns",
1224 "member-vnf-index": None,
1225 "vdu_id": None,
1226 "kdu_name": None,
1227 "vdu_count_index": None,
1228 "vnfd_id": "",
1229 "vdu_name": "",
1230 "ee_descriptor_id": "",
1231 "charm_name": "",
1232 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1233 },
1234 ],
1235 },
1236 },
1237 }
1238 mock_vnf_count_and_record = MagicMock()
1239 db_vnfr = {}
1240 vnf_count = ""
1241 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1242 with patch.object(self.n2vc, "db", self.db), patch.object(
1243 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1244 ):
1245 with self.assertRaises(N2VCException):
1246 self.n2vc._get_application_name(namespace)
1247 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1248 self.db.get_one.assert_called_once()
1249
1250 @patch(
1251 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1252 **{"return_value": "random"}
1253 )
1254 def test_get_application_name_ns_charm_old_naming(
1255 self, mock_generate_random_alfanum
1256 ):
1257 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1258 self.db.get_one.return_value = {
1259 "_admin": {
1260 "deployed": {
1261 "VCA": [
1262 {
1263 "target_element": "ns",
1264 "member-vnf-index": None,
1265 "vdu_id": None,
1266 "kdu_name": None,
1267 "vdu_count_index": None,
1268 "vnfd_id": "",
1269 "vdu_name": "",
1270 "ee_descriptor_id": "",
1271 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1272 },
1273 ],
1274 },
1275 },
1276 }
1277 mock_vnf_count_and_record = MagicMock()
1278 db_vnfr = {}
1279 vnf_count = ""
1280 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1281 expected_result = "app-random"
1282 with patch.object(self.n2vc, "db", self.db), patch.object(
1283 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1284 ):
1285 application_name = self.n2vc._get_application_name(namespace)
1286 self.assertEqual(application_name, expected_result)
1287 self.assertLess(len(application_name), 50)
1288 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1289 self.db.get_one.assert_called_once()