Feature 10944 Change naming of charms
[osm/N2VC.git] / n2vc / tests / unit / test_n2vc_juju_conn.py
1 # Copyright 2020 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 import asyncio
17 import logging
18 from unittest.mock import Mock, MagicMock
19 from unittest.mock import patch
20
21
22 import asynctest
23 from n2vc.definitions import Offer, RelationEndpoint
24 from n2vc.n2vc_juju_conn import N2VCJujuConnector
25 from osm_common import fslocal
26 from osm_common.dbmemory import DbMemory
27 from n2vc.exceptions import (
28 N2VCBadArgumentsException,
29 N2VCException,
30 JujuApplicationNotFound,
31 )
32 from n2vc.tests.unit.utils import AsyncMock
33 from n2vc.vca.connection_data import ConnectionData
34 from n2vc.tests.unit.testdata import test_db_descriptors as descriptors
35 import yaml
36
37
38 class N2VCJujuConnTestCase(asynctest.TestCase):
39 @asynctest.mock.patch("n2vc.n2vc_juju_conn.MotorStore")
40 @asynctest.mock.patch("n2vc.n2vc_juju_conn.get_connection")
41 @asynctest.mock.patch("n2vc.vca.connection_data.base64_to_cacert")
42 def setUp(
43 self,
44 mock_base64_to_cacert=None,
45 mock_get_connection=None,
46 mock_store=None,
47 ):
48 self.loop = asyncio.get_event_loop()
49 self.db = Mock()
50 mock_base64_to_cacert.return_value = """
51 -----BEGIN CERTIFICATE-----
52 SOMECERT
53 -----END CERTIFICATE-----"""
54 mock_store.return_value = AsyncMock()
55 mock_vca_connection = Mock()
56 mock_get_connection.return_value = mock_vca_connection
57 mock_vca_connection.data.return_value = ConnectionData(
58 **{
59 "endpoints": ["1.2.3.4:17070"],
60 "user": "user",
61 "secret": "secret",
62 "cacert": "cacert",
63 "pubkey": "pubkey",
64 "lxd-cloud": "cloud",
65 "lxd-credentials": "credentials",
66 "k8s-cloud": "k8s_cloud",
67 "k8s-credentials": "k8s_credentials",
68 "model-config": {},
69 "api-proxy": "api_proxy",
70 }
71 )
72 logging.disable(logging.CRITICAL)
73
74 N2VCJujuConnector.get_public_key = Mock()
75 self.n2vc = N2VCJujuConnector(
76 db=self.db,
77 fs=fslocal.FsLocal(),
78 log=None,
79 loop=self.loop,
80 on_update_db=None,
81 )
82 N2VCJujuConnector.get_public_key.assert_not_called()
83 self.n2vc.libjuju = Mock()
84
85
86 class GetMetricssTest(N2VCJujuConnTestCase):
87 def setUp(self):
88 super(GetMetricssTest, self).setUp()
89 self.n2vc.libjuju.get_metrics = AsyncMock()
90
91 def test_success(self):
92 _ = self.loop.run_until_complete(self.n2vc.get_metrics("model", "application"))
93 self.n2vc.libjuju.get_metrics.assert_called_once()
94
95 def test_except(self):
96 self.n2vc.libjuju.get_metrics.side_effect = Exception()
97 with self.assertRaises(Exception):
98 _ = self.loop.run_until_complete(
99 self.n2vc.get_metrics("model", "application")
100 )
101 self.n2vc.libjuju.get_metrics.assert_called_once()
102
103
104 class UpdateVcaStatusTest(N2VCJujuConnTestCase):
105 def setUp(self):
106 super(UpdateVcaStatusTest, self).setUp()
107 self.n2vc.libjuju.get_controller = AsyncMock()
108 self.n2vc.libjuju.get_model = AsyncMock()
109 self.n2vc.libjuju.get_executed_actions = AsyncMock()
110 self.n2vc.libjuju.get_actions = AsyncMock()
111 self.n2vc.libjuju.get_application_configs = AsyncMock()
112 self.n2vc.libjuju._get_application = AsyncMock()
113
114 def test_success(
115 self,
116 ):
117 self.loop.run_until_complete(
118 self.n2vc.update_vca_status(
119 {"model": {"applications": {"app": {"actions": {}}}}}
120 )
121 )
122 self.n2vc.libjuju.get_executed_actions.assert_called_once()
123 self.n2vc.libjuju.get_actions.assert_called_once()
124 self.n2vc.libjuju.get_application_configs.assert_called_once()
125
126 def test_exception(self):
127 self.n2vc.libjuju.get_model.return_value = None
128 self.n2vc.libjuju.get_executed_actions.side_effect = Exception()
129 with self.assertRaises(Exception):
130 self.loop.run_until_complete(
131 self.n2vc.update_vca_status(
132 {"model": {"applications": {"app": {"actions": {}}}}}
133 )
134 )
135 self.n2vc.libjuju.get_executed_actions.assert_not_called()
136 self.n2vc.libjuju.get_actions.assert_not_called_once()
137 self.n2vc.libjuju.get_application_configs.assert_not_called_once()
138
139
140 class K8sProxyCharmsTest(N2VCJujuConnTestCase):
141 def setUp(self):
142 super(K8sProxyCharmsTest, self).setUp()
143 self.n2vc.libjuju.model_exists = AsyncMock()
144 self.n2vc.libjuju.add_model = AsyncMock()
145 self.n2vc.libjuju.deploy_charm = AsyncMock()
146 self.n2vc.libjuju.model_exists.return_value = False
147 self.db = DbMemory()
148 self.fs = fslocal.FsLocal()
149 self.fs.path = "/"
150 self.n2vc.fs = self.fs
151 self.n2vc.db = self.db
152 self.db.create_list("nsrs", yaml.safe_load(descriptors.db_nsrs_text))
153 self.db.create_list("vnfrs", yaml.safe_load(descriptors.db_vnfrs_text))
154
155 @patch(
156 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
157 **{"return_value": "random"}
158 )
159 def test_success(
160 self,
161 mock_generate_random_alfanum_string,
162 ):
163 self.n2vc.fs.file_exists = MagicMock(create_autospec=True)
164 self.n2vc.fs.file_exists.return_value = True
165 ee_id = self.loop.run_until_complete(
166 self.n2vc.install_k8s_proxy_charm(
167 "simple",
168 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
169 "path",
170 {},
171 )
172 )
173
174 self.n2vc.libjuju.add_model.assert_called_once()
175 self.n2vc.libjuju.deploy_charm.assert_called_once_with(
176 model_name="dbfbd751-3de4-4e68-bd40-ec5ae0a53898-k8s",
177 application_name="simple-ee-z0-vnf1-vnf",
178 path="//path",
179 machine_id=None,
180 db_dict={},
181 progress_timeout=None,
182 total_timeout=None,
183 config=None,
184 )
185 self.assertEqual(
186 ee_id, "dbfbd751-3de4-4e68-bd40-ec5ae0a53898-k8s.simple-ee-z0-vnf1-vnf.k8s"
187 )
188
189 def test_no_artifact_path(
190 self,
191 ):
192 with self.assertRaises(N2VCBadArgumentsException):
193 ee_id = self.loop.run_until_complete(
194 self.n2vc.install_k8s_proxy_charm(
195 "simple",
196 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
197 "",
198 {},
199 )
200 )
201 self.assertIsNone(ee_id)
202
203 def test_no_db(
204 self,
205 ):
206 with self.assertRaises(N2VCBadArgumentsException):
207 ee_id = self.loop.run_until_complete(
208 self.n2vc.install_k8s_proxy_charm(
209 "simple",
210 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
211 "path",
212 None,
213 )
214 )
215 self.assertIsNone(ee_id)
216
217 def test_file_not_exists(
218 self,
219 ):
220 self.n2vc.fs.file_exists = MagicMock(create_autospec=True)
221 self.n2vc.fs.file_exists.return_value = False
222 with self.assertRaises(N2VCBadArgumentsException):
223 ee_id = self.loop.run_until_complete(
224 self.n2vc.install_k8s_proxy_charm(
225 "simple",
226 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
227 "path",
228 {},
229 )
230 )
231 self.assertIsNone(ee_id)
232
233 def test_exception(
234 self,
235 ):
236 self.n2vc.fs.file_exists = MagicMock(create_autospec=True)
237 self.n2vc.fs.file_exists.return_value = True
238 self.n2vc.fs.path = MagicMock(create_autospec=True)
239 self.n2vc.fs.path.return_value = "path"
240 self.n2vc.libjuju.deploy_charm.side_effect = Exception()
241 with self.assertRaises(N2VCException):
242 ee_id = self.loop.run_until_complete(
243 self.n2vc.install_k8s_proxy_charm(
244 "simple",
245 ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0",
246 "path",
247 {},
248 )
249 )
250 self.assertIsNone(ee_id)
251
252
253 class AddRelationTest(N2VCJujuConnTestCase):
254 def setUp(self):
255 super(AddRelationTest, self).setUp()
256 self.n2vc.libjuju.add_relation = AsyncMock()
257 self.n2vc.libjuju.offer = AsyncMock()
258 self.n2vc.libjuju.get_controller = AsyncMock()
259 self.n2vc.libjuju.consume = AsyncMock()
260
261 def test_standard_relation(self):
262 relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
263 relation_endpoint_2 = RelationEndpoint("model-1.app2.1", None, "endpoint")
264 self.loop.run_until_complete(
265 self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
266 )
267 self.n2vc.libjuju.add_relation.assert_called_once_with(
268 model_name="model-1", endpoint_1="app1:endpoint", endpoint_2="app2:endpoint"
269 )
270 self.n2vc.libjuju.offer.assert_not_called()
271 self.n2vc.libjuju.consume.assert_not_called()
272
273 def test_cmr_relation_same_controller(self):
274 relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
275 relation_endpoint_2 = RelationEndpoint("model-2.app2.1", None, "endpoint")
276 offer = Offer("admin/model-1.app1")
277 self.n2vc.libjuju.offer.return_value = offer
278 self.n2vc.libjuju.consume.return_value = "saas"
279 self.loop.run_until_complete(
280 self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
281 )
282 self.n2vc.libjuju.offer.assert_called_once_with(relation_endpoint_1)
283 self.n2vc.libjuju.consume.assert_called_once()
284 self.n2vc.libjuju.add_relation.assert_called_once_with(
285 "model-2", "app2:endpoint", "saas"
286 )
287
288 def test_relation_exception(self):
289 relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
290 relation_endpoint_2 = RelationEndpoint("model-2.app2.1", None, "endpoint")
291 self.n2vc.libjuju.offer.side_effect = Exception()
292 with self.assertRaises(N2VCException):
293 self.loop.run_until_complete(
294 self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
295 )
296
297
298 class UpgradeCharmTest(N2VCJujuConnTestCase):
299 def setUp(self):
300 super(UpgradeCharmTest, self).setUp()
301 self.n2vc._get_libjuju = AsyncMock(return_value=self.n2vc.libjuju)
302 N2VCJujuConnector._get_ee_id_components = Mock()
303 self.n2vc.libjuju.upgrade_charm = AsyncMock()
304
305 def test_empty_ee_id(self):
306 with self.assertRaises(N2VCBadArgumentsException):
307 self.loop.run_until_complete(
308 self.n2vc.upgrade_charm(
309 "", "/sample_charm_path", "sample_charm_id", "native-charm", None
310 )
311 )
312 self.n2vc._get_libjuju.assert_called()
313 self.n2vc._get_ee_id_components.assert_not_called()
314 self.n2vc.libjuju.upgrade_charm.assert_not_called()
315
316 def test_wrong_ee_id(self):
317 N2VCJujuConnector._get_ee_id_components.side_effect = Exception
318 with self.assertRaises(N2VCBadArgumentsException):
319 self.loop.run_until_complete(
320 self.n2vc.upgrade_charm(
321 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
322 "/sample_charm_path",
323 "sample_charm_id",
324 "native-charm",
325 500,
326 )
327 )
328 self.n2vc._get_libjuju.assert_called()
329 self.n2vc._get_ee_id_components.assert_called()
330 self.n2vc.libjuju.upgrade_charm.assert_not_called()
331
332 def test_charm_upgrade_succeded(self):
333 N2VCJujuConnector._get_ee_id_components.return_value = (
334 "sample_model",
335 "sample_app",
336 "sample_machine_id",
337 )
338 self.loop.run_until_complete(
339 self.n2vc.upgrade_charm(
340 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
341 "/sample_charm_path",
342 "sample_charm_id",
343 "native-charm",
344 500,
345 )
346 )
347 self.n2vc._get_libjuju.assert_called()
348 self.n2vc._get_ee_id_components.assert_called()
349 self.n2vc.libjuju.upgrade_charm.assert_called_with(
350 application_name="sample_app",
351 path="/sample_charm_path",
352 model_name="sample_model",
353 total_timeout=500,
354 )
355
356 def test_charm_upgrade_failed(self):
357 N2VCJujuConnector._get_ee_id_components.return_value = (
358 "sample_model",
359 "sample_app",
360 "sample_machine_id",
361 )
362 self.n2vc.libjuju.upgrade_charm.side_effect = JujuApplicationNotFound
363 with self.assertRaises(N2VCException):
364 self.loop.run_until_complete(
365 self.n2vc.upgrade_charm(
366 "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s",
367 "/sample_charm_path",
368 "sample_charm_id",
369 "native-charm",
370 None,
371 )
372 )
373 self.n2vc._get_libjuju.assert_called()
374 self.n2vc._get_ee_id_components.assert_called()
375 self.n2vc.libjuju.upgrade_charm.assert_called_with(
376 application_name="sample_app",
377 path="/sample_charm_path",
378 model_name="sample_model",
379 total_timeout=None,
380 )
381
382
383 class GenerateApplicationNameTest(N2VCJujuConnTestCase):
384
385 vnf_id = "dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
386
387 def setUp(self):
388 super(GenerateApplicationNameTest, self).setUp()
389 self.db = MagicMock(DbMemory)
390
391 @patch(
392 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
393 **{"return_value": "random"}
394 )
395 def test_generate_backward_compatible_application_name(
396 self, mock_generate_random_alfanum
397 ):
398 vdu_id = "mgmtVM"
399 vdu_count = "0"
400 expected_result = "app-vnf-ec5ae0a53898-vdu-mgmtVM-cnt-0-random"
401
402 application_name = self.n2vc._generate_backward_compatible_application_name(
403 GenerateApplicationNameTest.vnf_id, vdu_id, vdu_count
404 )
405 self.assertEqual(application_name, expected_result)
406
407 @patch(
408 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
409 **{"return_value": "random"}
410 )
411 def test_generate_backward_compatible_application_name_without_vnf_id_vdu_id(
412 self, mock_generate_random_alfanum
413 ):
414 vnf_id = None
415 vdu_id = ""
416 vdu_count = None
417 expected_result = "app--random"
418 application_name = self.n2vc._generate_backward_compatible_application_name(
419 vnf_id, vdu_id, vdu_count
420 )
421
422 self.assertEqual(application_name, expected_result)
423 self.assertLess(len(application_name), 50)
424
425 def test_find_charm_level_with_vnf_id(self):
426 vdu_id = ""
427 expected_result = "vnf-level"
428 charm_level = self.n2vc._find_charm_level(
429 GenerateApplicationNameTest.vnf_id, vdu_id
430 )
431 self.assertEqual(charm_level, expected_result)
432
433 def test_find_charm_level_with_vdu_id(self):
434 vnf_id = ""
435 vdu_id = "mgmtVM"
436 with self.assertRaises(N2VCException):
437 self.n2vc._find_charm_level(vnf_id, vdu_id)
438
439 def test_find_charm_level_with_vnf_id_and_vdu_id(self):
440 vdu_id = "mgmtVM"
441 expected_result = "vdu-level"
442 charm_level = self.n2vc._find_charm_level(
443 GenerateApplicationNameTest.vnf_id, vdu_id
444 )
445 self.assertEqual(charm_level, expected_result)
446
447 def test_find_charm_level_without_vnf_id_and_vdu_id(self):
448 vnf_id = ""
449 vdu_id = ""
450 expected_result = "ns-level"
451 charm_level = self.n2vc._find_charm_level(vnf_id, vdu_id)
452 self.assertEqual(charm_level, expected_result)
453
454 def test_generate_application_name_ns_charm(self):
455 charm_level = "ns-level"
456 vnfrs = {}
457 vca_records = [
458 {
459 "target_element": "ns",
460 "member-vnf-index": "",
461 "vdu_id": None,
462 "kdu_name": None,
463 "vdu_count_index": None,
464 "vnfd_id": None,
465 "vdu_name": None,
466 "type": "proxy_charm",
467 "ee_descriptor_id": None,
468 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh",
469 "ee_id": None,
470 "application": "",
471 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
472 },
473 ]
474 vnf_count = ""
475 vdu_count = ""
476 expected_result = "simple-ns-charm-abc-000-rrrr-nnnn-4444-h-ns"
477 application_name = self.n2vc._generate_application_name(
478 charm_level,
479 vnfrs,
480 vca_records,
481 vnf_count=vnf_count,
482 vdu_count=vdu_count,
483 )
484 self.assertEqual(application_name, expected_result)
485 self.assertLess(len(application_name), 50)
486
487 def test_generate_application_name_ns_charm_empty_vca_records(self):
488 charm_level = "ns-level"
489 vnfrs = {}
490 vca_records = []
491 vnf_count = ""
492 vdu_count = ""
493 with self.assertRaises(N2VCException):
494 self.n2vc._generate_application_name(
495 charm_level,
496 vnfrs,
497 vca_records,
498 vnf_count=vnf_count,
499 vdu_count=vdu_count,
500 )
501
502 def test_generate_application_name_vnf_charm(self):
503 charm_level = "vnf-level"
504 vnfrs = {
505 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
506 }
507 vca_records = [
508 {
509 "target_element": "vnf/vnf1",
510 "member-vnf-index": "vnf111-xxx-yyy-zzz",
511 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
512 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
513 "charm_name": "",
514 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
515 },
516 ]
517 vnf_count = "1"
518 vdu_count = ""
519 expected_result = "simple-ee-ab-1-vnf111-xxx-y-vnf"
520 application_name = self.n2vc._generate_application_name(
521 charm_level,
522 vnfrs,
523 vca_records,
524 vnf_count=vnf_count,
525 vdu_count=vdu_count,
526 )
527 self.assertEqual(application_name, expected_result)
528 self.assertLess(len(application_name), 50)
529
530 def test_generate_application_name_vdu_charm(self):
531 charm_level = "vdu-level"
532 vnfrs = {
533 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
534 "vdur": [
535 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
536 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
537 ],
538 }
539 vca_records = [
540 {
541 "target_element": "vnf/vnf1/mgmtvm",
542 "member-vnf-index": "vnf111-xxx-yyy-zzz",
543 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
544 "kdu_name": None,
545 "vdu_count_index": None,
546 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
547 "vdu_name": "mgmtvm",
548 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
549 "charm_name": "",
550 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
551 },
552 {
553 "target_element": "vnf/vnf1/datavm",
554 "member-vnf-index": "vnf111-xxx-yyy-zzz",
555 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
556 "kdu_name": None,
557 "vdu_count_index": None,
558 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
559 "vdu_name": "datavm",
560 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
561 "charm_name": "",
562 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
563 },
564 ]
565 vnf_count = "2"
566 vdu_count = "0"
567 expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
568 application_name = self.n2vc._generate_application_name(
569 charm_level,
570 vnfrs,
571 vca_records,
572 vnf_count=vnf_count,
573 vdu_count=vdu_count,
574 )
575 self.assertEqual(application_name, expected_result)
576 self.assertLess(len(application_name), 50)
577
578 def test_generate_application_name_vdu_charm_wrong_vnfrs(self):
579 charm_level = "vdu-level"
580 vnfrs = {
581 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
582 }
583 vca_records = [
584 {
585 "target_element": "vnf/vnf1/mgmtvm",
586 "member-vnf-index": "vnf111-xxx-yyy-zzz",
587 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
588 "kdu_name": None,
589 "vdu_count_index": None,
590 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
591 "vdu_name": "mgmtvm",
592 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
593 "charm_name": "",
594 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
595 },
596 ]
597 vnf_count = "2"
598 vdu_count = "0"
599 with self.assertRaises(KeyError):
600 self.n2vc._generate_application_name(
601 charm_level,
602 vnfrs,
603 vca_records,
604 vnf_count=vnf_count,
605 vdu_count=vdu_count,
606 )
607
608 def test_get_vnf_count_db_vnfr_ns_charm(self):
609 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
610 charm_level = "ns-level"
611 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
612 with patch.object(self.n2vc, "db", self.db):
613 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
614 charm_level, vnf_id_and_count
615 )
616 self.assertEqual(vnf_count, "")
617 self.assertEqual(db_vnfr, {})
618
619 def test_get_vnf_count_db_vnfr_vnf_charm(self):
620 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
621 charm_level = "vnf-level"
622 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-4"
623 with patch.object(self.n2vc, "db", self.db):
624 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
625 charm_level, vnf_id_and_count
626 )
627 self.assertEqual(vnf_count, "4")
628 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
629
630 def test_get_vnf_count_db_vnfr_vdu_charm(self):
631 self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"}
632 charm_level = "vdu-level"
633 vnf_id_and_count = "m7fbd751-3de4-4e68-bd40-ec5ae0a53898-2"
634 with patch.object(self.n2vc, "db", self.db):
635 vnf_count, db_vnfr = self.n2vc._get_vnf_count_and_record(
636 charm_level, vnf_id_and_count
637 )
638 self.assertEqual(vnf_count, "2")
639 self.assertEqual(db_vnfr, {"member-vnf-index-ref": "sample-ref"})
640
641 def test_get_vca_records_vdu_charm(self):
642 charm_level = "vdu-level"
643 db_vnfr = {
644 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
645 "vdur": [
646 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
647 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
648 ],
649 }
650 db_nsr = {
651 "_admin": {
652 "deployed": {
653 "VCA": [
654 {
655 "target_element": "vnf/vnf1/mgmtvm",
656 "member-vnf-index": "vnf111-xxx-yyy-zzz",
657 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
658 "kdu_name": None,
659 "vdu_count_index": None,
660 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
661 "vdu_name": "mgmtvm",
662 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
663 "charm_name": "",
664 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
665 },
666 {
667 "target_element": "vnf/vnf2/datavm",
668 "member-vnf-index": "vnf222-xxx-yyy-zzz",
669 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
670 "kdu_name": None,
671 "vdu_count_index": None,
672 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
673 "vdu_name": "datavm",
674 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
675 "charm_name": "",
676 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
677 },
678 ],
679 },
680 },
681 }
682 expected_result = [
683 {
684 "target_element": "vnf/vnf1/mgmtvm",
685 "member-vnf-index": "vnf111-xxx-yyy-zzz",
686 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
687 "kdu_name": None,
688 "vdu_count_index": None,
689 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
690 "vdu_name": "mgmtvm",
691 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
692 "charm_name": "",
693 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
694 },
695 ]
696 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
697 self.assertEqual(vca_records, expected_result)
698
699 def test_get_vca_records_vnf_charm_member_vnf_index_mismatch(self):
700 charm_level = "vnf-level"
701 db_vnfr = {
702 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
703 }
704 db_nsr = {
705 "_admin": {
706 "deployed": {
707 "VCA": [
708 {
709 "target_element": "vnf/vnf1/mgmtvm",
710 "member-vnf-index": "vnf111-xxx-yyy-zzz",
711 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
712 "kdu_name": None,
713 "vdu_count_index": None,
714 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
715 "vdu_name": "mgmtvm",
716 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
717 "charm_name": "",
718 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
719 },
720 {
721 "target_element": "vnf/vnf1/mgmtvm",
722 "member-vnf-index": "vnf111-xxx-yyy-zzz",
723 "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a",
724 "kdu_name": None,
725 "vdu_count_index": None,
726 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
727 "vdu_name": "datavm",
728 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444",
729 "charm_name": "",
730 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
731 },
732 ],
733 },
734 },
735 }
736 expected_result = []
737 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
738 self.assertEqual(vca_records, expected_result)
739
740 def test_get_vca_records_ns_charm(self):
741 charm_level = "ns-level"
742 db_vnfr = {
743 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
744 }
745 db_nsr = {
746 "_admin": {
747 "deployed": {
748 "VCA": [
749 {
750 "target_element": "vnf/vnf1/mgmtvm",
751 "member-vnf-index": "vnf111-xxx-yyy-zzz",
752 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
753 "kdu_name": None,
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 "target_element": "ns",
763 "member-vnf-index": None,
764 "vdu_id": None,
765 "kdu_name": None,
766 "vdu_count_index": None,
767 "vnfd_id": "",
768 "vdu_name": "",
769 "ee_descriptor_id": "",
770 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
771 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
772 },
773 ],
774 },
775 },
776 }
777 expected_result = [
778 {
779 "target_element": "ns",
780 "member-vnf-index": None,
781 "vdu_id": None,
782 "kdu_name": None,
783 "vdu_count_index": None,
784 "vnfd_id": "",
785 "vdu_name": "",
786 "ee_descriptor_id": "",
787 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
788 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
789 },
790 ]
791 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
792 self.assertEqual(vca_records, expected_result)
793
794 def test_get_vca_records_ns_charm_empty_charm_name(self):
795 charm_level = "ns-level"
796 db_vnfr = {
797 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
798 }
799 db_nsr = {
800 "_admin": {
801 "deployed": {
802 "VCA": [
803 {
804 "target_element": "vnf/vnf1/mgmtvm",
805 "member-vnf-index": "vnf111-xxx-yyy-zzz",
806 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
807 "kdu_name": None,
808 "vdu_count_index": None,
809 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
810 "vdu_name": "mgmtvm",
811 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
812 "charm_name": "",
813 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
814 },
815 {
816 "target_element": "ns",
817 "member-vnf-index": None,
818 "vdu_id": None,
819 "kdu_name": None,
820 "vdu_count_index": None,
821 "vnfd_id": "",
822 "vdu_name": "",
823 "ee_descriptor_id": "",
824 "charm_name": "",
825 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
826 },
827 ],
828 },
829 },
830 }
831 expected_result = [
832 {
833 "target_element": "ns",
834 "member-vnf-index": None,
835 "vdu_id": None,
836 "kdu_name": None,
837 "vdu_count_index": None,
838 "vnfd_id": "",
839 "vdu_name": "",
840 "ee_descriptor_id": "",
841 "charm_name": "",
842 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
843 },
844 ]
845 vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
846 self.assertEqual(vca_records, expected_result)
847
848 def test_get_application_name_vnf_charm(self):
849 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
850 self.db.get_one.return_value = {
851 "_admin": {
852 "deployed": {
853 "VCA": [
854 {
855 "target_element": "vnf/vnf1/mgmtvm",
856 "member-vnf-index": "vnf111-xxx-yyy-zzz",
857 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
858 "kdu_name": None,
859 "vdu_count_index": None,
860 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
861 "vdu_name": "mgmtvm",
862 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
863 "charm_name": "",
864 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
865 },
866 {
867 "target_element": "ns",
868 "member-vnf-index": None,
869 "vdu_id": None,
870 "kdu_name": None,
871 "vdu_count_index": None,
872 "vnfd_id": "",
873 "vdu_name": "",
874 "ee_descriptor_id": "",
875 "charm_name": "",
876 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
877 },
878 ],
879 },
880 },
881 }
882 mock_vnf_count_and_record = MagicMock()
883 db_vnfr = {
884 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
885 }
886 vnf_count = "0"
887 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
888 expected_result = "simple-ee-ab-z0-vnf111-xxx-y-vnf"
889 with patch.object(self.n2vc, "db", self.db), patch.object(
890 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
891 ):
892 application_name = self.n2vc._get_application_name(namespace)
893 self.assertEqual(application_name, expected_result)
894 self.assertLess(len(application_name), 50)
895 mock_vnf_count_and_record.assert_called_once_with(
896 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
897 )
898 self.db.get_one.assert_called_once()
899
900 @patch(
901 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
902 **{"return_value": "random"}
903 )
904 def test_get_application_name_vnf_charm_old_naming(
905 self, mock_generate_random_alfanum
906 ):
907 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
908 self.db.get_one.return_value = {
909 "_admin": {
910 "deployed": {
911 "VCA": [
912 {
913 "target_element": "vnf/vnf1/mgmtvm",
914 "member-vnf-index": "vnf111-xxx-yyy-zzz",
915 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
916 "kdu_name": None,
917 "vdu_count_index": None,
918 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
919 "vdu_name": "mgmtvm",
920 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
921 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
922 },
923 {
924 "target_element": "ns",
925 "member-vnf-index": None,
926 "vdu_id": None,
927 "kdu_name": None,
928 "vdu_count_index": None,
929 "vnfd_id": "",
930 "vdu_name": "",
931 "ee_descriptor_id": "",
932 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
933 },
934 ],
935 },
936 },
937 }
938 mock_vnf_count_and_record = MagicMock()
939 db_vnfr = {
940 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
941 }
942 vnf_count = "0"
943 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
944 expected_result = "app-vnf-eb3161eec0-z0-random"
945 with patch.object(self.n2vc, "db", self.db), patch.object(
946 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
947 ):
948 application_name = self.n2vc._get_application_name(namespace)
949 self.assertEqual(application_name, expected_result)
950 mock_vnf_count_and_record.assert_called_once_with(
951 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
952 )
953 self.db.get_one.assert_called_once()
954
955 def test_get_application_name_vnf_charm_vnf_index_ref_mismatch(self):
956 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
957 self.db.get_one.return_value = {
958 "_admin": {
959 "deployed": {
960 "VCA": [
961 {
962 "target_element": "vnf/vnf1/mgmtvm",
963 "member-vnf-index": "vnf111-xxx-yyy-zzz",
964 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
965 "kdu_name": None,
966 "vdu_count_index": None,
967 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
968 "vdu_name": "mgmtvm",
969 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
970 "charm_name": "",
971 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
972 },
973 {
974 "target_element": "ns",
975 "member-vnf-index": None,
976 "vdu_id": None,
977 "kdu_name": None,
978 "vdu_count_index": None,
979 "vnfd_id": "",
980 "vdu_name": "",
981 "ee_descriptor_id": "",
982 "charm_name": "",
983 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
984 },
985 ],
986 },
987 },
988 }
989 mock_vnf_count_and_record = MagicMock()
990 db_vnfr = {
991 "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
992 }
993 vnf_count = "0"
994 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
995 with patch.object(self.n2vc, "db", self.db), patch.object(
996 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
997 ):
998 with self.assertRaises(N2VCException):
999 self.n2vc._get_application_name(namespace)
1000 mock_vnf_count_and_record.assert_called_once_with(
1001 "vnf-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1002 )
1003 self.db.get_one.assert_called_once()
1004
1005 def test_get_application_name_vdu_charm(self):
1006 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-0"
1007 self.db.get_one.return_value = {
1008 "_admin": {
1009 "deployed": {
1010 "VCA": [
1011 {
1012 "target_element": "vnf/vnf1/mgmtvm",
1013 "member-vnf-index": "vnf111-xxx-yyy-zzz",
1014 "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
1015 "kdu_name": None,
1016 "vdu_count_index": None,
1017 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
1018 "vdu_name": "mgmtvm",
1019 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1020 "charm_name": "",
1021 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1022 },
1023 {
1024 "target_element": "ns",
1025 "member-vnf-index": None,
1026 "vdu_id": None,
1027 "kdu_name": None,
1028 "vdu_count_index": None,
1029 "vnfd_id": "",
1030 "vdu_name": "",
1031 "ee_descriptor_id": "",
1032 "charm_name": "",
1033 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1034 },
1035 ]
1036 }
1037 }
1038 }
1039 mock_vnf_count_and_record = MagicMock()
1040 db_vnfr = {
1041 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1042 "vdur": [
1043 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1044 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1045 ],
1046 }
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-mgmtvm-z0-vdu"
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 "vdu-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_vdu_charm_old_naming(
1066 self, mock_generate_random_alfanum
1067 ):
1068 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.mgmtVM-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 = {
1101 "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
1102 "vdur": [
1103 {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"},
1104 {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"},
1105 ],
1106 }
1107 vnf_count = "0"
1108 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1109 expected_result = "app-vnf-eb3161eec0-z0-vdu-mgmtvm-cnt-z0-random"
1110
1111 with patch.object(self.n2vc, "db", self.db), patch.object(
1112 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1113 ):
1114 application_name = self.n2vc._get_application_name(namespace)
1115 self.assertEqual(application_name, expected_result)
1116 self.assertLess(len(application_name), 50)
1117 mock_vnf_count_and_record.assert_called_once_with(
1118 "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0"
1119 )
1120 self.db.get_one.assert_called_once()
1121
1122 def test_get_application_name_ns_charm(self):
1123 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1124 self.db.get_one.return_value = {
1125 "_admin": {
1126 "deployed": {
1127 "VCA": [
1128 {
1129 "target_element": "ns",
1130 "member-vnf-index": None,
1131 "vdu_id": None,
1132 "kdu_name": None,
1133 "vdu_count_index": None,
1134 "vnfd_id": "",
1135 "vdu_name": "",
1136 "ee_descriptor_id": "",
1137 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
1138 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1139 },
1140 ],
1141 },
1142 },
1143 }
1144 mock_vnf_count_and_record = MagicMock()
1145 db_vnfr = {}
1146 vnf_count = ""
1147 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1148 expected_result = "simple-ns-charm-abc-z000-rrrr-nnnn-z4444-h-ns"
1149 with patch.object(self.n2vc, "db", self.db), patch.object(
1150 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1151 ):
1152 application_name = self.n2vc._get_application_name(namespace)
1153 self.assertEqual(application_name, expected_result)
1154 self.assertLess(len(application_name), 50)
1155 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1156 self.db.get_one.assert_called_once()
1157
1158 def test_get_application_name_ns_charm_empty_charm_name(self):
1159 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1160 self.db.get_one.return_value = {
1161 "_admin": {
1162 "deployed": {
1163 "VCA": [
1164 {
1165 "target_element": "ns",
1166 "member-vnf-index": None,
1167 "vdu_id": None,
1168 "kdu_name": None,
1169 "vdu_count_index": None,
1170 "vnfd_id": "",
1171 "vdu_name": "",
1172 "ee_descriptor_id": "",
1173 "charm_name": "",
1174 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1175 },
1176 ],
1177 },
1178 },
1179 }
1180 mock_vnf_count_and_record = MagicMock()
1181 db_vnfr = {}
1182 vnf_count = ""
1183 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1184 with patch.object(self.n2vc, "db", self.db), patch.object(
1185 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1186 ):
1187 with self.assertRaises(N2VCException):
1188 self.n2vc._get_application_name(namespace)
1189 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1190 self.db.get_one.assert_called_once()
1191
1192 @patch(
1193 "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
1194 **{"return_value": "random"}
1195 )
1196 def test_get_application_name_ns_charm_old_naming(
1197 self, mock_generate_random_alfanum
1198 ):
1199 namespace = ".dbfbd751-3de4-4e68-bd40-ec5ae0a53898"
1200 self.db.get_one.return_value = {
1201 "_admin": {
1202 "deployed": {
1203 "VCA": [
1204 {
1205 "target_element": "ns",
1206 "member-vnf-index": None,
1207 "vdu_id": None,
1208 "kdu_name": None,
1209 "vdu_count_index": None,
1210 "vnfd_id": "",
1211 "vdu_name": "",
1212 "ee_descriptor_id": "",
1213 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
1214 },
1215 ],
1216 },
1217 },
1218 }
1219 mock_vnf_count_and_record = MagicMock()
1220 db_vnfr = {}
1221 vnf_count = ""
1222 mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
1223 expected_result = "app-random"
1224 with patch.object(self.n2vc, "db", self.db), patch.object(
1225 self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
1226 ):
1227 application_name = self.n2vc._get_application_name(namespace)
1228 self.assertEqual(application_name, expected_result)
1229 self.assertLess(len(application_name), 50)
1230 mock_vnf_count_and_record.assert_called_once_with("ns-level", None)
1231 self.db.get_one.assert_called_once()