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