1 # Copyright 2020 ArctosLabs Scandinavia AB
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from collections
import Counter
18 from pathlib
import Path
19 from unittest
import TestCase
, mock
20 from unittest
.mock
import call
24 from osm_pla
.placement
.mznplacement
import NsPlacementDataFactory
27 class TestNsPlacementDataFactory(TestCase
):
30 "vim_password": "FxtnynxBCnouzAT4Hkerhg==",
33 "modified": 1564579854.0480285,
34 "created": 1564579854.0480285,
35 "operationalState": "ENABLED",
36 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
38 "RO-account": "6beb4e2e-b397-11e9-a7a3-02420aff0008",
39 "RO": "6bcfc3fc-b397-11e9-a7a3-02420aff0008",
41 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
42 "detailed-status": "Done",
45 "vim_type": "openstack",
46 "_id": "92b056a7-38f5-438d-b8ee-3f93b3531f87",
47 "schema_version": "1.1",
49 "vim_url": "http://10.234.12.47:5000/v3",
50 "vim_tenant_name": "admin",
54 "vim_tenant_name": "osm_demo",
55 "schema_version": "1.1",
57 "vim_password": "gK5v4Gh2Pl41o6Skwp6RCw==",
58 "vim_type": "openstack",
60 "modified": 1567148372.2490237,
61 "created": 1567148372.2490237,
62 "operationalState": "ENABLED",
63 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
65 "RO-account": "b7fb0034-caf3-11e9-9388-02420aff000a",
66 "RO": "b7f129ce-caf3-11e9-9388-02420aff000a",
68 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
69 "detailed-status": "Done",
72 "vim_url": "http://10.234.12.44:5000/v3",
73 "_id": "6618d412-d7fc-4eb0-a6f8-d2c258e0e900",
77 "schema_version": "1.1",
79 "vim_password": "1R2FoMQnaL6rNSosoRP2hw==",
80 "vim_type": "openstack",
81 "vim_tenant_name": "osm_demo",
83 "modified": 1567599746.689582,
84 "created": 1567599746.689582,
85 "operationalState": "ENABLED",
86 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
88 "RO-account": "a8161f54-cf0e-11e9-9388-02420aff000a",
89 "RO": "a80b6280-cf0e-11e9-9388-02420aff000a",
91 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
92 "detailed-status": "Done",
95 "vim_url": "http://10.234.12.46:5000/v3",
96 "_id": "331ffdec-44a8-4707-94a1-af7a292d9735",
100 "schema_version": "1.1",
101 "name": "OpenStack4",
102 "vim_password": "6LScyPeMq3QFh3GRb/xwZw==",
103 "vim_type": "openstack",
104 "vim_tenant_name": "osm_demo",
106 "modified": 1567599911.5108898,
107 "created": 1567599911.5108898,
108 "operationalState": "ENABLED",
109 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
111 "RO-account": "0a651200-cf0f-11e9-9388-02420aff000a",
112 "RO": "0a4defc6-cf0f-11e9-9388-02420aff000a",
114 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
115 "detailed-status": "Done",
118 "vim_url": "http://10.234.12.43:5000/v3",
119 "_id": "eda92f47-29b9-4007-9709-c1833dbfbe31",
123 vim_accounts_fewer_vims
= [
125 "vim_password": "FxtnynxBCnouzAT4Hkerhg==",
128 "modified": 1564579854.0480285,
129 "created": 1564579854.0480285,
130 "operationalState": "ENABLED",
131 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
133 "RO-account": "6beb4e2e-b397-11e9-a7a3-02420aff0008",
134 "RO": "6bcfc3fc-b397-11e9-a7a3-02420aff0008",
136 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
137 "detailed-status": "Done",
139 "name": "OpenStack1",
140 "vim_type": "openstack",
141 "_id": "92b056a7-38f5-438d-b8ee-3f93b3531f87",
142 "schema_version": "1.1",
144 "vim_url": "http://10.234.12.47:5000/v3",
145 "vim_tenant_name": "admin",
149 "vim_tenant_name": "osm_demo",
150 "schema_version": "1.1",
151 "name": "OpenStack2",
152 "vim_password": "gK5v4Gh2Pl41o6Skwp6RCw==",
153 "vim_type": "openstack",
155 "modified": 1567148372.2490237,
156 "created": 1567148372.2490237,
157 "operationalState": "ENABLED",
158 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
160 "RO-account": "b7fb0034-caf3-11e9-9388-02420aff000a",
161 "RO": "b7f129ce-caf3-11e9-9388-02420aff000a",
163 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
164 "detailed-status": "Done",
167 "vim_url": "http://10.234.12.44:5000/v3",
168 "_id": "6618d412-d7fc-4eb0-a6f8-d2c258e0e900",
172 "schema_version": "1.1",
173 "name": "OpenStack4",
174 "vim_password": "6LScyPeMq3QFh3GRb/xwZw==",
175 "vim_type": "openstack",
176 "vim_tenant_name": "osm_demo",
178 "modified": 1567599911.5108898,
179 "created": 1567599911.5108898,
180 "operationalState": "ENABLED",
181 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
183 "RO-account": "0a651200-cf0f-11e9-9388-02420aff000a",
184 "RO": "0a4defc6-cf0f-11e9-9388-02420aff000a",
186 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
187 "detailed-status": "Done",
190 "vim_url": "http://10.234.12.43:5000/v3",
191 "_id": "eda92f47-29b9-4007-9709-c1833dbfbe31",
195 vim_accounts_more_vims
= [
197 "vim_password": "FxtnynxBCnouzAT4Hkerhg==",
200 "modified": 1564579854.0480285,
201 "created": 1564579854.0480285,
202 "operationalState": "ENABLED",
203 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
205 "RO-account": "6beb4e2e-b397-11e9-a7a3-02420aff0008",
206 "RO": "6bcfc3fc-b397-11e9-a7a3-02420aff0008",
208 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
209 "detailed-status": "Done",
211 "name": "OpenStack1",
212 "vim_type": "openstack",
213 "_id": "92b056a7-38f5-438d-b8ee-3f93b3531f87",
214 "schema_version": "1.1",
216 "vim_url": "http://10.234.12.47:5000/v3",
217 "vim_tenant_name": "admin",
221 "vim_tenant_name": "osm_demo",
222 "schema_version": "1.1",
223 "name": "OpenStack2",
224 "vim_password": "gK5v4Gh2Pl41o6Skwp6RCw==",
225 "vim_type": "openstack",
227 "modified": 1567148372.2490237,
228 "created": 1567148372.2490237,
229 "operationalState": "ENABLED",
230 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
232 "RO-account": "b7fb0034-caf3-11e9-9388-02420aff000a",
233 "RO": "b7f129ce-caf3-11e9-9388-02420aff000a",
235 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
236 "detailed-status": "Done",
239 "vim_url": "http://10.234.12.44:5000/v3",
240 "_id": "6618d412-d7fc-4eb0-a6f8-d2c258e0e900",
244 "schema_version": "1.1",
245 "name": "OpenStack4",
246 "vim_password": "6LScyPeMq3QFh3GRb/xwZw==",
247 "vim_type": "openstack",
248 "vim_tenant_name": "osm_demo",
250 "modified": 1567599911.5108898,
251 "created": 1567599911.5108898,
252 "operationalState": "ENABLED",
253 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
255 "RO-account": "0a651200-cf0f-11e9-9388-02420aff000a",
256 "RO": "0a4defc6-cf0f-11e9-9388-02420aff000a",
258 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
259 "detailed-status": "Done",
262 "vim_url": "http://10.234.12.43:5000/v3",
263 "_id": "eda92f47-29b9-4007-9709-c1833dbfbe31",
267 "schema_version": "1.1",
268 "name": "OpenStack3",
269 "vim_password": "6LScyPeMq3QFh3GRb/xwZw==",
270 "vim_type": "openstack",
271 "vim_tenant_name": "osm_demo",
273 "modified": 1567599911.5108898,
274 "created": 1567599911.5108898,
275 "operationalState": "ENABLED",
276 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
278 "RO-account": "0a651200-cf0f-11e9-9388-02420aff000a",
279 "RO": "0a4defc6-cf0f-11e9-9388-02420aff000a",
281 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
282 "detailed-status": "Done",
285 "vim_url": "http://10.234.12.46:5000/v3",
286 "_id": "eda92f47-29b9-4007-9709-c1833dbfbe31",
290 "schema_version": "1.1",
291 "name": "OpenStack5",
292 "vim_password": "6LScyPeMq3QFh3GRb/xwZw==",
293 "vim_type": "openstack",
294 "vim_tenant_name": "osm_demo",
296 "modified": 1567599911.5108898,
297 "created": 1567599911.5108898,
298 "operationalState": "ENABLED",
299 "projects_read": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
301 "RO-account": "0a651200-cf0f-11e9-9388-02420aff000a",
302 "RO": "0a4defc6-cf0f-11e9-9388-02420aff000a",
304 "projects_write": ["69915588-e5e2-46d3-96b0-a29bedef6f73"],
305 "detailed-status": "Done",
308 "vim_url": "http://1.1.1.1:5000/v3",
309 "_id": "ffffffff-29b9-4007-9709-c1833dbfbe31",
313 def _produce_ut_vim_accounts_info(self
, vim_accounts
):
315 FIXME temporary, we will need more control over vim_urls and _id for test purpose - make a generator
316 :return: vim_url and _id as dict, i.e. extract these from vim_accounts data
318 return {_
["name"]: _
["_id"] for _
in vim_accounts
}
320 def _adjust_path(self
, file):
321 """In case we are not running from test directory,
322 then assume we are in top level directory (e.g. running from tox) and adjust file path accordingly
324 path_component
= "/osm_pla/test/"
325 real_path
= os
.path
.realpath(file)
326 if path_component
not in real_path
:
328 os
.path
.dirname(real_path
)
330 + os
.path
.basename(real_path
)
335 def _populate_pil_info(self
, file):
337 Note str(Path()) is a 3.5 thing
339 with
open(str(Path(self
._adjust
_path
(file)))) as pp_fd
:
340 test_data
= yaml
.safe_load_all(pp_fd
)
341 return next(test_data
)
343 def _get_ut_nsd_from_file(self
, nsd_file_name
):
345 creates the structure representing the nsd.
347 IMPORTANT NOTE: If using .yaml files from the NS packages for the unit tests (which we do),
348 then the files must be modified with respect to the way booleans are processed at on-boarding in OSM.
349 The following construct in the NS package yaml file:
350 mgmt-network: 'false'
351 will become a boolean in the MongoDB, and therefore the yaml used in these unit test must use yaml
353 mgmt-network: !!bool False
354 The modification also applies to 'true' => !!bool True
355 This will ensure that the object returned from this function is as expected by PLA.
357 with
open(str(Path(self
._adjust
_path
(nsd_file_name
)))) as nsd_fd
:
358 test_data
= yaml
.safe_load_all(nsd_fd
)
359 return next(test_data
)
361 def _produce_ut_vnf_price_list(self
):
362 price_list_file
= "vnf_price_list.yaml"
363 with
open(str(Path(self
._adjust
_path
(price_list_file
)))) as pl_fd
:
364 price_list_data
= yaml
.safe_load_all(pl_fd
)
366 i
["vnfd"]: {i1
["vim_name"]: i1
["price"] for i1
in i
["prices"]}
367 for i
in next(price_list_data
)
370 def _produce_ut_vnf_test_price_list(self
, price_list
):
371 price_list_file
= price_list
372 with
open(str(Path(self
._adjust
_path
(price_list_file
)))) as pl_fd
:
373 price_list_data
= yaml
.safe_load_all(pl_fd
)
375 i
["vnfd"]: {i1
["vim_name"]: i1
["price"] for i1
in i
["prices"]}
376 for i
in next(price_list_data
)
379 def test__produce_trp_link_characteristics_link_latency_with_more_vims(self
):
381 -test with more(other) vims compared to pil
410 nspdf
= NsPlacementDataFactory(
411 self
._produce
_ut
_vim
_accounts
_info
(
412 TestNsPlacementDataFactory
.vim_accounts_more_vims
414 self
._produce
_ut
_vnf
_price
_list
(),
416 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
419 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
420 content_produced
= [i
for row
in pil_latencies
for i
in row
]
422 Counter(content_expected
),
423 Counter(content_produced
),
424 "trp_link_latency incorrect",
427 def test__produce_trp_link_characteristics_link_latency_with_fewer_vims(self
):
429 -test with fewer vims compared to pil
432 content_expected
= [0, 0, 0, 120, 120, 140, 140, 240, 240]
433 nspdf
= NsPlacementDataFactory(
434 self
._produce
_ut
_vim
_accounts
_info
(
435 TestNsPlacementDataFactory
.vim_accounts_fewer_vims
437 self
._produce
_ut
_vnf
_price
_list
(),
439 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
442 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
443 content_produced
= [i
for row
in pil_latencies
for i
in row
]
445 Counter(content_expected
),
446 Counter(content_produced
),
447 "trp_link_latency incorrect",
450 def test__produce_trp_link_characteristic_not_supported(self
):
452 - test with non-supported characteristic
454 nspdf
= NsPlacementDataFactory(
455 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
456 self
._produce
_ut
_vnf
_price
_list
(),
458 pil_info
=self
._populate
_pil
_info
("pil_unittest1.yaml"),
462 with self
.assertRaises(Exception) as e
:
463 nspdf
._produce
_trp
_link
_characteristics
_data
("test_no_support")
466 r
"characteristic.*not supported",
467 "invalid exception content",
470 def test__produce_trp_link_characteristics_link_latency(self
):
472 -test with full set of vims as in pil
473 -test with fewer vims compared to pil
474 -test with more(other) vims compared to pil
475 -test with invalid/corrupt pil configuration file (e.g. missing endpoint), empty file, not yaml conformant
476 - test with non-supported characteristic
499 nspdf
= NsPlacementDataFactory(
500 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
501 self
._produce
_ut
_vnf
_price
_list
(),
503 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
506 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
507 content_produced
= [i
for row
in pil_latencies
for i
in row
]
509 Counter(content_expected
),
510 Counter(content_produced
),
511 "trp_link_latency incorrect",
514 def test__produce_trp_link_characteristics_link_jitter(self
):
516 -test with full set of vims as in pil
537 nspdf
= NsPlacementDataFactory(
538 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
539 self
._produce
_ut
_vnf
_price
_list
(),
541 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
544 pil_jitter
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
545 content_produced
= [i
for row
in pil_jitter
for i
in row
]
547 Counter(content_expected
),
548 Counter(content_produced
),
549 "trp_link_jitter incorrect",
552 def test__produce_trp_link_characteristics_link_jitter_with_fewer_vims(self
):
554 -test with fewer vims compared to pil, link jitter
556 content_expected
= [0, 0, 0, 1200, 1200, 1400, 1400, 2400, 2400]
557 nspdf
= NsPlacementDataFactory(
558 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_fewer_vims
),
559 self
._produce
_ut
_vnf
_price
_list
(),
561 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
564 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
565 content_produced
= [i
for row
in pil_latencies
for i
in row
]
567 Counter(content_expected
),
568 Counter(content_produced
),
569 "trp_link_jitter incorrect",
572 def test__produce_trp_link_characteristics_link_jitter_with_more_vims(self
):
574 -test with more vims compared to pil, link jitter
603 nspdf
= NsPlacementDataFactory(
604 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_more_vims
),
605 self
._produce
_ut
_vnf
_price
_list
(),
607 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
610 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
611 content_produced
= [i
for row
in pil_latencies
for i
in row
]
613 Counter(content_expected
),
614 Counter(content_produced
),
615 "trp_link_jitter incorrect",
618 def test__produce_trp_link_characteristics_link_price(self
):
620 -test with full set of vims as in pil
622 content_expected
= [0, 0, 0, 0, 12, 12, 13, 13, 14, 14, 23, 23, 24, 24, 34, 34]
623 nspdf
= NsPlacementDataFactory(
624 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
625 self
._produce
_ut
_vnf
_price
_list
(),
627 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
630 pil_prices
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_price")
631 content_produced
= [i
for row
in pil_prices
for i
in row
]
633 Counter(content_expected
),
634 Counter(content_produced
),
635 "invalid trp link prices",
638 def test__produce_trp_link_characteristics_link_price_with_fewer_vims(self
):
640 -test with fewer vims compared to pil
642 content_expected
= [0, 0, 0, 12, 12, 14, 14, 24, 24]
643 nspdf
= NsPlacementDataFactory(
644 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_fewer_vims
),
645 self
._produce
_ut
_vnf
_price
_list
(),
647 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
650 pil_prices
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_price")
651 content_produced
= [i
for row
in pil_prices
for i
in row
]
653 Counter(content_expected
),
654 Counter(content_produced
),
655 "invalid trp link prices",
658 def test__produce_trp_link_characteristics_partly_constrained(self
):
677 nspdf
= NsPlacementDataFactory(
678 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
679 self
._produce
_ut
_vnf
_price
_list
(),
681 pil_info
=self
._populate
_pil
_info
("pil_unittest2_keys.yaml"),
684 pil_jitter
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
685 content_produced
= [i
for row
in pil_jitter
for i
in row
]
687 Counter(content_expected
),
688 Counter(content_produced
),
689 "invalid trp link jitter, partly constrained",
692 def test__produce_vld_desc_partly_constrained(self
):
693 vld_desc_expected
= [
694 {"cp_refs": ["one", "two"], "jitter": 30},
695 {"cp_refs": ["two", "three"], "latency": 120},
698 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest2.yaml")
699 nsd
= nsd
["nsd"]["nsd"][0]
700 nspdf
= NsPlacementDataFactory(
701 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
702 self
._produce
_ut
_vnf
_price
_list
(),
708 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
711 def test__produce_trp_link_characteristics_link_latency_not_yaml_conformant(self
):
713 -test with invalid/corrupt pil configuration file (not yaml conformant)
715 with self
.assertRaises(Exception) as e
:
716 _
= NsPlacementDataFactory(
717 self
._produce
_ut
_vim
_accounts
_info
(
718 TestNsPlacementDataFactory
.vim_accounts
720 self
._produce
_ut
_vnf
_price
_list
(),
722 pil_info
=self
._populate
_pil
_info
("not_yaml_conformant.yaml"),
727 r
"mapping values are not allowed here.*",
728 "invalid exception content",
731 def test__produce_trp_link_characteristics_with_invalid_pil_config(self
):
733 -test with invalid/corrupt pil configuration file (missing endpoint)
735 nspdf
= NsPlacementDataFactory(
736 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
737 self
._produce
_ut
_vnf
_price
_list
(),
739 pil_info
=self
._populate
_pil
_info
(
740 "corrupt_pil_endpoints_config_unittest1.yaml"
744 with self
.assertRaises(Exception) as e
:
745 _
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
747 "list index out of range", str(e
.exception
), "unexpected exception"
750 def test__produce_vld_desc_w_instantiate_override(self
):
751 vld_desc_expected
= [
752 {"cp_refs": ["one", "two"], "latency": 150, "jitter": 30},
753 {"cp_refs": ["two", "three"], "latency": 90, "jitter": 30},
756 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest_no_vld_constraints.yaml")
757 nsd
= nsd
["nsd"]["nsd"][0]
758 nspdf
= NsPlacementDataFactory(
759 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
760 self
._produce
_ut
_vnf
_price
_list
(),
764 order_constraints
=None,
768 nspdf
._produce
_vld
_desc
(), vld_desc_expected
, "vld_desc incorrect"
771 def test__produce_vld_desc_nsd_w_instantiate_wo(self
):
773 nsd w/ constraints, instantiate w/o constraints
776 vld_desc_expected
= [
777 {"cp_refs": ["one", "two"], "latency": 150, "jitter": 30},
778 {"cp_refs": ["two", "three"], "latency": 90, "jitter": 30},
781 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest3.yaml")
782 nsd
= nsd
["nsd"]["nsd"][0]
783 nspdf
= NsPlacementDataFactory(
784 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
785 self
._produce
_ut
_vnf
_price
_list
(),
789 order_constraints
=None,
793 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
796 def test__produce_vld_desc_nsd_w_instantiate_w(self
):
798 nsd w/ constraints, instantiate w/ constraints => override
801 vld_desc_expected
= [
802 {"cp_refs": ["one", "two"], "latency": 120, "jitter": 21},
803 {"cp_refs": ["two", "three"], "latency": 121, "jitter": 22},
806 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest3.yaml")
807 nsd
= nsd
["nsd"]["nsd"][0]
808 nspdf
= NsPlacementDataFactory(
809 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
810 self
._produce
_ut
_vnf
_price
_list
(),
817 "id": "three_vnf_constrained_nsd_vld1",
818 "link-constraints": {"latency": 120, "jitter": 21},
821 "id": "three_vnf_constrained_nsd_vld2",
822 "link-constraints": {"latency": 121, "jitter": 22},
829 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
832 def test__produce_vld_desc_nsd_wo_instantiate_wo(self
):
834 nsd w/o constraints, instantiate w/o constraints = no constraints in model
837 vld_desc_expected
= [{"cp_refs": ["one", "two"]}, {"cp_refs": ["two", "three"]}]
839 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest_no_vld_constraints.yaml")
840 nsd
= nsd
["nsd"]["nsd"][0]
841 nspdf
= NsPlacementDataFactory(
842 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
843 self
._produce
_ut
_vnf
_price
_list
(),
847 order_constraints
=None,
851 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
854 def test__produce_vld_desc_nsd_wo_instantiate_w(self
):
856 nsd w/o constraints, instantiate w/ constraints => add constraints
859 vld_desc_expected
= [
860 {"cp_refs": ["one", "two"], "latency": 140, "jitter": 41},
861 {"cp_refs": ["two", "three"], "latency": 141, "jitter": 42},
864 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest_no_vld_constraints.yaml")
865 nsd
= nsd
["nsd"]["nsd"][0]
866 nspdf
= NsPlacementDataFactory(
867 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
868 self
._produce
_ut
_vnf
_price
_list
(),
875 "id": "three_vnf_constrained_nsd_vld1",
876 "link-constraints": {"latency": 140, "jitter": 41},
879 "id": "three_vnf_constrained_nsd_vld2",
880 "link-constraints": {"latency": 141, "jitter": 42},
887 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
890 def test__produce_vld_desc_nsd_wo_instantiate_w_faulty_input(self
):
892 nsd w/o constraints, instantiate w/ constraints => add constraints that can be parsed
895 vld_desc_expected
= [
896 {"cp_refs": ["one", "two"]},
897 {"cp_refs": ["two", "three"], "latency": 151},
900 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest_no_vld_constraints.yaml")
901 nsd
= nsd
["nsd"]["nsd"][0]
902 nspdf
= NsPlacementDataFactory(
903 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
904 self
._produce
_ut
_vnf
_price
_list
(),
911 "id": "not_included_vld",
912 "misspelled-constraints": {"latency": 120, "jitter": 20},
915 "id": "three_vnf_constrained_nsd_vld2",
916 "link-constraints": {"latency": 151},
923 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
926 def test__produce_vld_desc_nsd_wo_instantiate_w_faulty_input_again(self
):
928 nsd w/o constraints, instantiate w/ faulty constraints => add constraints that can be parsed
931 vld_desc_expected
= [
932 {"cp_refs": ["one", "two"], "jitter": 21},
933 {"cp_refs": ["two", "three"]},
936 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest_no_vld_constraints.yaml")
937 nsd
= nsd
["nsd"]["nsd"][0]
938 nspdf
= NsPlacementDataFactory(
939 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
940 self
._produce
_ut
_vnf
_price
_list
(),
947 "id": "three_vnf_constrained_nsd_vld1",
948 "link-constraints": {"delay": 120, "jitter": 21},
951 "id": "three_vnf_constrained_nsd_vld2",
952 "misspelled-constraints": {"latency": 121, "jitter": 22},
959 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
962 def test__produce_vld_desc_mgmt_network(self
):
963 vld_desc_expected
= [
964 {"cp_refs": ["1", "2"], "latency": 120, "jitter": 20},
965 {"cp_refs": ["2", "4"], "latency": 50, "jitter": 10},
966 {"cp_refs": ["2", "3"], "latency": 20, "jitter": 10},
969 nsd
= self
._get
_ut
_nsd
_from
_file
("test_five_nsd.yaml")
970 nsd
= nsd
["nsd"]["nsd"][0]
971 nspdf
= NsPlacementDataFactory(
972 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
973 self
._produce
_ut
_vnf
_price
_list
(),
977 order_constraints
=None,
981 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
984 def test__produce_vld_desc_single_vnf_nsd(self
):
985 vld_desc_expected
= []
987 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest4.yaml")
988 nsd
= nsd
["nsd"]["nsd"][0]
989 nspdf
= NsPlacementDataFactory(
990 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
991 self
._produce
_ut
_vnf
_price
_list
(),
995 order_constraints
=None,
999 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc_incorrect"
1002 def test__produce_vld_desc_slice_nsd(self
):
1003 vld_desc_expected
= []
1004 nsd
= self
._get
_ut
_nsd
_from
_file
("slice_hackfest_middle_nsd.yaml")
1005 nsd
= nsd
["nsd"]["nsd"][0]
1006 nspdf
= NsPlacementDataFactory(
1007 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
1008 self
._produce
_ut
_vnf
_price
_list
(),
1012 order_constraints
=None,
1016 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc_incorrect"
1019 def test__produce_vld_desc(self
):
1024 vld_desc_expected
= [
1025 {"cp_refs": ["one", "two"], "latency": 150, "jitter": 30},
1026 {"cp_refs": ["two", "three"], "latency": 90, "jitter": 30},
1029 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest3.yaml")
1030 nsd
= nsd
["nsd"]["nsd"][0]
1031 nspdf
= NsPlacementDataFactory(
1032 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
1033 self
._produce
_ut
_vnf
_price
_list
(),
1037 order_constraints
=None,
1041 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
1044 def test__produce_ns_desc(self
):
1047 - price list sheet with more vims than associated with session
1048 - price list sheet with fewer vims than associated with session
1049 - nsd with different vndfd-id-refs
1050 - fault case scenarios with non-existing vims, non-existing vnfds
1052 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest3.yaml")
1053 nsd
= nsd
["nsd"]["nsd"][0]
1054 nspdf
= NsPlacementDataFactory(
1055 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
1056 self
._produce
_ut
_vnf
_price
_list
(),
1062 ns_desc
= nspdf
._produce
_ns
_desc
()
1063 # check that all expected member-vnf-index are present
1064 vnfs
= [e
["vnf_id"] for e
in ns_desc
]
1066 Counter(["one", "two", "three"]), Counter(vnfs
), "vnf_id invalid"
1069 expected_keys
= ["vnf_id", "vnf_price_per_vim"]
1071 # check that vnf_price_per_vim has proper values
1073 Counter([5, 10, 30, 30]),
1074 Counter(e
["vnf_price_per_vim"]),
1075 "vnf_price_per_vim invalid",
1077 # check that no pinning directives included
1079 Counter(expected_keys
), Counter(e
.keys()), "pinning directive misplaced"
1082 def test__produce_ns_desc_with_more_vims(self
):
1083 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest1.yaml")
1084 nsd
= nsd
["nsd"]["nsd"][0]
1085 nspdf
= NsPlacementDataFactory(
1086 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_more_vims
),
1087 self
._produce
_ut
_vnf
_test
_price
_list
("vnf_price_list_more_vims.yaml"),
1093 ns_desc
= nspdf
._produce
_ns
_desc
()
1094 # check that all expected member-vnf-index are present
1095 vnfs
= [e
["vnf_id"] for e
in ns_desc
]
1097 Counter({"1": 1, "2": 1, "3": 1}), Counter(vnfs
), "vnf_id invalid"
1100 expected_keys
= ["vnf_id", "vnf_price_per_vim"]
1102 # check that vnf_price_per_vim has proper values
1104 Counter([5, 10, 30, 30, 3]),
1105 Counter(e
["vnf_price_per_vim"]),
1106 "vnf_price_per_vim invalid",
1108 # check that no pinning directives included
1110 Counter(expected_keys
), Counter(e
.keys()), "pinning directive misplaced"
1113 def test__produce_ns_desc_with_fewer_vims(self
):
1114 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest1.yaml")
1115 nsd
= nsd
["nsd"]["nsd"][0]
1116 nspdf
= NsPlacementDataFactory(
1117 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_fewer_vims
),
1118 self
._produce
_ut
_vnf
_price
_list
(),
1124 ns_desc
= nspdf
._produce
_ns
_desc
()
1125 # check that all expected member-vnf-index are present
1126 vnfs
= [e
["vnf_id"] for e
in ns_desc
]
1128 Counter({"1": 1, "2": 1, "3": 1}), Counter(vnfs
), "vnf_id invalid"
1131 expected_keys
= ["vnf_id", "vnf_price_per_vim"]
1133 # check that vnf_price_per_vim has proper values
1135 Counter([5, 10, 30]),
1136 Counter(e
["vnf_price_per_vim"]),
1137 "vnf_price_per_vim invalid",
1139 # check that no pinning directives included
1141 Counter(expected_keys
), Counter(e
.keys()), "pinning directive misplaced"
1144 def test__produce_ns_desc_w_pinning(self
):
1145 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest3.yaml")
1146 nsd
= nsd
["nsd"]["nsd"][0]
1149 "member-vnf-index": "two",
1150 "vimAccountId": "331ffdec-44a8-4707-94a1-af7a292d9735",
1153 nspdf
= NsPlacementDataFactory(
1154 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
1155 self
._produce
_ut
_vnf
_price
_list
(),
1160 ns_desc
= nspdf
._produce
_ns
_desc
()
1161 # check that all expected member-vnf-index are present
1162 vnfs
= [e
["vnf_id"] for e
in ns_desc
]
1164 Counter(["one", "three", "two"]), Counter(vnfs
), "vnf_id invalid"
1168 # check that vnf_price_per_vim has proper values
1170 Counter([5, 10, 30, 30]),
1171 Counter(e
["vnf_price_per_vim"]),
1172 "vnf_price_per_vim invalid",
1174 # check that member-vnf-index 2 is pinned correctly
1175 if e
["vnf_id"] == "two":
1176 self
.assertTrue("vim_account" in e
.keys(), "missing pinning directive")
1178 pinning
[0]["vimAccountId"]
1179 == e
["vim_account"][3:].replace("_", "-"),
1180 "invalid pinning vim-account",
1184 "vim-account" not in e
.keys(), "pinning directive misplaced"
1187 @mock.patch
.object(NsPlacementDataFactory
, "_produce_trp_link_characteristics_data")
1188 @mock.patch
.object(NsPlacementDataFactory
, "_produce_vld_desc")
1189 @mock.patch
.object(NsPlacementDataFactory
, "_produce_ns_desc")
1190 def test_create_ns_placement_data_wo_order(
1191 self
, mock_prd_ns_desc
, mock_prd_vld_desc
, mock_prd_trp_link_char
1196 vim_accounts_expected
= [
1199 "vim92b056a7-38f5-438d-b8ee-3f93b3531f87",
1200 "vim6618d412-d7fc-4eb0-a6f8-d2c258e0e900",
1201 "vim331ffdec-44a8-4707-94a1-af7a292d9735",
1202 "vimeda92f47-29b9-4007-9709-c1833dbfbe31",
1206 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest3.yaml")
1207 nsd
= nsd
["nsd"]["nsd"][0]
1208 nspdf
= NsPlacementDataFactory(
1209 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
1210 self
._produce
_ut
_vnf
_price
_list
(),
1212 pil_info
=self
._populate
_pil
_info
("pil_unittest1.yaml"),
1214 order_constraints
=None,
1216 nspd
= nspdf
.create_ns_placement_data()
1218 Counter(vim_accounts_expected
),
1219 Counter(nspd
["vim_accounts"]),
1220 "vim_accounts incorrect",
1222 # mock1.assert_called_once() Note for python > 3.5
1223 self
.assertTrue(mock_prd_ns_desc
.called
, "_produce_ns_desc not called")
1224 # mock2.assert_called_once() Note for python > 3.5
1225 self
.assertTrue(mock_prd_vld_desc
.called
, " _produce_vld_desc not called")
1226 mock_prd_trp_link_char
.assert_has_calls(
1227 [call("pil_latency"), call("pil_jitter"), call("pil_price")]
1232 r
".*'file':.*mznplacement.py",
1233 r
".*'time':.*datetime.datetime\(.*\)",
1235 generator_data
= str(nspd
["generator_data"])
1236 for regex
in regexps
:
1237 self
.assertRegex(generator_data
, regex
, "generator data invalid")
1239 @mock.patch
.object(NsPlacementDataFactory
, "_produce_trp_link_characteristics_data")
1240 @mock.patch
.object(NsPlacementDataFactory
, "_produce_vld_desc")
1241 @mock.patch
.object(NsPlacementDataFactory
, "_produce_ns_desc")
1242 def test_create_ns_placement_data_w_order(
1243 self
, mock_prd_ns_desc
, mock_prd_vld_desc
, mock_prd_trp_link_char
1248 vim_accounts_expected
= [
1251 "vim92b056a7-38f5-438d-b8ee-3f93b3531f87",
1252 "vim6618d412-d7fc-4eb0-a6f8-d2c258e0e900",
1253 "vim331ffdec-44a8-4707-94a1-af7a292d9735",
1254 "vimeda92f47-29b9-4007-9709-c1833dbfbe31",
1258 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest3.yaml")
1259 nsd
= nsd
["nsd"]["nsd"][0]
1260 nspdf
= NsPlacementDataFactory(
1261 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
1262 self
._produce
_ut
_vnf
_price
_list
(),
1264 pil_info
=self
._populate
_pil
_info
("pil_unittest1.yaml"),
1267 "vld-constraints": [
1269 "id": "three_vnf_constrained_nsd_vld1",
1270 "link-constraints": {"latency": 120, "jitter": 21},
1273 "id": "three_vnf_constrained_nsd_vld2",
1274 "link-constraints": {"latency": 121, "jitter": 22},
1279 nspd
= nspdf
.create_ns_placement_data()
1281 Counter(vim_accounts_expected
),
1282 Counter(nspd
["vim_accounts"]),
1283 "vim_accounts incorrect",
1285 # mock1.assert_called_once() Note for python > 3.5
1286 self
.assertTrue(mock_prd_ns_desc
.called
, "_produce_ns_desc not called")
1287 # mock2.assert_called_once() Note for python > 3.5
1288 self
.assertTrue(mock_prd_vld_desc
.called
, " _produce_vld_desc not called")
1289 mock_prd_trp_link_char
.assert_has_calls(
1290 [call("pil_latency"), call("pil_jitter"), call("pil_price")]
1295 r
".*'file':.*mznplacement.py",
1296 r
".*'time':.*datetime.datetime\(.*\)",
1298 generator_data
= str(nspd
["generator_data"])
1299 for regex
in regexps
:
1300 self
.assertRegex(generator_data
, regex
, "generator data invalid")
1303 if __name__
== "__main__":
1304 if __name__
== "__main__":