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