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"""
323 path_component
= "/osm_pla/test/"
324 real_path
= os
.path
.realpath(file)
325 if path_component
not in real_path
:
327 os
.path
.dirname(real_path
)
329 + os
.path
.basename(real_path
)
334 def _populate_pil_info(self
, file):
336 Note str(Path()) is a 3.5 thing
338 with
open(str(Path(self
._adjust
_path
(file)))) as pp_fd
:
339 test_data
= yaml
.safe_load_all(pp_fd
)
340 return next(test_data
)
342 def _get_ut_nsd_from_file(self
, nsd_file_name
):
344 creates the structure representing the nsd.
346 IMPORTANT NOTE: If using .yaml files from the NS packages for the unit tests (which we do),
347 then the files must be modified with respect to the way booleans are processed at on-boarding in OSM.
348 The following construct in the NS package yaml file:
349 mgmt-network: 'false'
350 will become a boolean in the MongoDB, and therefore the yaml used in these unit test must use yaml
352 mgmt-network: !!bool False
353 The modification also applies to 'true' => !!bool True
354 This will ensure that the object returned from this function is as expected by PLA.
356 with
open(str(Path(self
._adjust
_path
(nsd_file_name
)))) as nsd_fd
:
357 test_data
= yaml
.safe_load_all(nsd_fd
)
358 return next(test_data
)
360 def _produce_ut_vnf_price_list(self
):
361 price_list_file
= "vnf_price_list.yaml"
362 with
open(str(Path(self
._adjust
_path
(price_list_file
)))) as pl_fd
:
363 price_list_data
= yaml
.safe_load_all(pl_fd
)
365 i
["vnfd"]: {i1
["vim_name"]: i1
["price"] for i1
in i
["prices"]}
366 for i
in next(price_list_data
)
369 def _produce_ut_vnf_test_price_list(self
, price_list
):
370 price_list_file
= price_list
371 with
open(str(Path(self
._adjust
_path
(price_list_file
)))) as pl_fd
:
372 price_list_data
= yaml
.safe_load_all(pl_fd
)
374 i
["vnfd"]: {i1
["vim_name"]: i1
["price"] for i1
in i
["prices"]}
375 for i
in next(price_list_data
)
378 def test__produce_trp_link_characteristics_link_latency_with_more_vims(self
):
380 -test with more(other) vims compared to pil
409 nspdf
= NsPlacementDataFactory(
410 self
._produce
_ut
_vim
_accounts
_info
(
411 TestNsPlacementDataFactory
.vim_accounts_more_vims
413 self
._produce
_ut
_vnf
_price
_list
(),
415 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
418 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
419 content_produced
= [i
for row
in pil_latencies
for i
in row
]
421 Counter(content_expected
),
422 Counter(content_produced
),
423 "trp_link_latency incorrect",
426 def test__produce_trp_link_characteristics_link_latency_with_fewer_vims(self
):
428 -test with fewer vims compared to pil
431 content_expected
= [0, 0, 0, 120, 120, 140, 140, 240, 240]
432 nspdf
= NsPlacementDataFactory(
433 self
._produce
_ut
_vim
_accounts
_info
(
434 TestNsPlacementDataFactory
.vim_accounts_fewer_vims
436 self
._produce
_ut
_vnf
_price
_list
(),
438 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
441 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
442 content_produced
= [i
for row
in pil_latencies
for i
in row
]
444 Counter(content_expected
),
445 Counter(content_produced
),
446 "trp_link_latency incorrect",
449 def test__produce_trp_link_characteristic_not_supported(self
):
451 - test with non-supported characteristic
453 nspdf
= NsPlacementDataFactory(
454 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
455 self
._produce
_ut
_vnf
_price
_list
(),
457 pil_info
=self
._populate
_pil
_info
("pil_unittest1.yaml"),
461 with self
.assertRaises(Exception) as e
:
462 nspdf
._produce
_trp
_link
_characteristics
_data
("test_no_support")
465 r
"characteristic.*not supported",
466 "invalid exception content",
469 def test__produce_trp_link_characteristics_link_latency(self
):
471 -test with full set of vims as in pil
472 -test with fewer vims compared to pil
473 -test with more(other) vims compared to pil
474 -test with invalid/corrupt pil configuration file (e.g. missing endpoint), empty file, not yaml conformant
475 - test with non-supported characteristic
498 nspdf
= NsPlacementDataFactory(
499 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
500 self
._produce
_ut
_vnf
_price
_list
(),
502 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
505 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
506 content_produced
= [i
for row
in pil_latencies
for i
in row
]
508 Counter(content_expected
),
509 Counter(content_produced
),
510 "trp_link_latency incorrect",
513 def test__produce_trp_link_characteristics_link_jitter(self
):
515 -test with full set of vims as in pil
536 nspdf
= NsPlacementDataFactory(
537 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
538 self
._produce
_ut
_vnf
_price
_list
(),
540 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
543 pil_jitter
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
544 content_produced
= [i
for row
in pil_jitter
for i
in row
]
546 Counter(content_expected
),
547 Counter(content_produced
),
548 "trp_link_jitter incorrect",
551 def test__produce_trp_link_characteristics_link_jitter_with_fewer_vims(self
):
553 -test with fewer vims compared to pil, link jitter
555 content_expected
= [0, 0, 0, 1200, 1200, 1400, 1400, 2400, 2400]
556 nspdf
= NsPlacementDataFactory(
557 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_fewer_vims
),
558 self
._produce
_ut
_vnf
_price
_list
(),
560 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
563 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
564 content_produced
= [i
for row
in pil_latencies
for i
in row
]
566 Counter(content_expected
),
567 Counter(content_produced
),
568 "trp_link_jitter incorrect",
571 def test__produce_trp_link_characteristics_link_jitter_with_more_vims(self
):
573 -test with more vims compared to pil, link jitter
602 nspdf
= NsPlacementDataFactory(
603 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_more_vims
),
604 self
._produce
_ut
_vnf
_price
_list
(),
606 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
609 pil_latencies
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
610 content_produced
= [i
for row
in pil_latencies
for i
in row
]
612 Counter(content_expected
),
613 Counter(content_produced
),
614 "trp_link_jitter incorrect",
617 def test__produce_trp_link_characteristics_link_price(self
):
619 -test with full set of vims as in pil
621 content_expected
= [0, 0, 0, 0, 12, 12, 13, 13, 14, 14, 23, 23, 24, 24, 34, 34]
622 nspdf
= NsPlacementDataFactory(
623 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
624 self
._produce
_ut
_vnf
_price
_list
(),
626 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
629 pil_prices
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_price")
630 content_produced
= [i
for row
in pil_prices
for i
in row
]
632 Counter(content_expected
),
633 Counter(content_produced
),
634 "invalid trp link prices",
637 def test__produce_trp_link_characteristics_link_price_with_fewer_vims(self
):
639 -test with fewer vims compared to pil
641 content_expected
= [0, 0, 0, 12, 12, 14, 14, 24, 24]
642 nspdf
= NsPlacementDataFactory(
643 self
._produce
_ut
_vim
_accounts
_info
(self
.vim_accounts_fewer_vims
),
644 self
._produce
_ut
_vnf
_price
_list
(),
646 pil_info
=self
._populate
_pil
_info
("pil_unittest1_keys.yaml"),
649 pil_prices
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_price")
650 content_produced
= [i
for row
in pil_prices
for i
in row
]
652 Counter(content_expected
),
653 Counter(content_produced
),
654 "invalid trp link prices",
657 def test__produce_trp_link_characteristics_partly_constrained(self
):
676 nspdf
= NsPlacementDataFactory(
677 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
678 self
._produce
_ut
_vnf
_price
_list
(),
680 pil_info
=self
._populate
_pil
_info
("pil_unittest2_keys.yaml"),
683 pil_jitter
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_jitter")
684 content_produced
= [i
for row
in pil_jitter
for i
in row
]
686 Counter(content_expected
),
687 Counter(content_produced
),
688 "invalid trp link jitter, partly constrained",
691 def test__produce_vld_desc_partly_constrained(self
):
692 vld_desc_expected
= [
693 {"cp_refs": ["one", "two"], "jitter": 30},
694 {"cp_refs": ["two", "three"], "latency": 120},
697 nsd
= self
._get
_ut
_nsd
_from
_file
("nsd_unittest2.yaml")
698 nsd
= nsd
["nsd"]["nsd"][0]
699 nspdf
= NsPlacementDataFactory(
700 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
701 self
._produce
_ut
_vnf
_price
_list
(),
707 vld_desc_expected
, nspdf
._produce
_vld
_desc
(), "vld_desc incorrect"
710 def test__produce_trp_link_characteristics_link_latency_not_yaml_conformant(self
):
712 -test with invalid/corrupt pil configuration file (not yaml conformant)
714 with self
.assertRaises(Exception) as e
:
715 _
= NsPlacementDataFactory(
716 self
._produce
_ut
_vim
_accounts
_info
(
717 TestNsPlacementDataFactory
.vim_accounts
719 self
._produce
_ut
_vnf
_price
_list
(),
721 pil_info
=self
._populate
_pil
_info
("not_yaml_conformant.yaml"),
726 r
"mapping values are not allowed here.*",
727 "invalid exception content",
730 def test__produce_trp_link_characteristics_with_invalid_pil_config(self
):
732 -test with invalid/corrupt pil configuration file (missing endpoint)
734 nspdf
= NsPlacementDataFactory(
735 self
._produce
_ut
_vim
_accounts
_info
(TestNsPlacementDataFactory
.vim_accounts
),
736 self
._produce
_ut
_vnf
_price
_list
(),
738 pil_info
=self
._populate
_pil
_info
(
739 "corrupt_pil_endpoints_config_unittest1.yaml"
743 with self
.assertRaises(Exception) as e
:
744 _
= nspdf
._produce
_trp
_link
_characteristics
_data
("pil_latency")
746 "list index out of range", str(e
.exception
), "unexpected exception"
749 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__":