From: sousaedu Date: Mon, 22 Nov 2021 23:56:28 +0000 (+0000) Subject: Extracting Ns._get_resource_allocation_params() and creating unit test X-Git-Tag: v12.0.0rc1~52 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FRO.git;a=commitdiff_plain;h=refs%2Fchanges%2F93%2F11393%2F1 Extracting Ns._get_resource_allocation_params() and creating unit test Change-Id: I5081c94f48a745b957cb2410403830812a5a1913 Signed-off-by: sousaedu --- diff --git a/NG-RO/osm_ng_ro/ns.py b/NG-RO/osm_ng_ro/ns.py index 70f91d99..f57e7bb1 100644 --- a/NG-RO/osm_ng_ro/ns.py +++ b/NG-RO/osm_ng_ro/ns.py @@ -499,6 +499,32 @@ class Ns(object): return {"find_params": find_params} + @staticmethod + def _get_resource_allocation_params( + quota_descriptor: Dict[str, Any], + ) -> Dict[str, Any]: + """Read the quota_descriptor from vnfd and fetch the resource allocation properties from the + descriptor object. + + Args: + quota_descriptor (Dict[str, Any]): cpu/mem/vif/disk-io quota descriptor + + Returns: + Dict[str, Any]: quota params for limit, reserve, shares from the descriptor object + """ + quota = {} + + if quota_descriptor.get("limit"): + quota["limit"] = int(quota_descriptor["limit"]) + + if quota_descriptor.get("reserve"): + quota["reserve"] = int(quota_descriptor["reserve"]) + + if quota_descriptor.get("shares"): + quota["shares"] = int(quota_descriptor["shares"]) + + return quota + def deploy(self, session, indata, version, nsr_id, *args, **kwargs): self.logger.debug("ns.deploy nsr_id={} indata={}".format(nsr_id, indata)) validate_input(indata, deploy_schema) @@ -556,26 +582,6 @@ class Ns(object): index += 1 def _process_flavor_params(target_flavor, vim_info, target_record_id): - def _get_resource_allocation_params(quota_descriptor): - """ - read the quota_descriptor from vnfd and fetch the resource allocation properties from the - descriptor object - :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor - :return: quota params for limit, reserve, shares from the descriptor object - """ - quota = {} - - if quota_descriptor.get("limit"): - quota["limit"] = int(quota_descriptor["limit"]) - - if quota_descriptor.get("reserve"): - quota["reserve"] = int(quota_descriptor["reserve"]) - - if quota_descriptor.get("shares"): - quota["shares"] = int(quota_descriptor["shares"]) - - return quota - nonlocal indata flavor_data = { @@ -686,7 +692,7 @@ class Ns(object): epa_vcpu_set = True if target_flavor["guest-epa"].get("cpu-quota") and not epa_vcpu_set: - cpuquota = _get_resource_allocation_params( + cpuquota = Ns._get_resource_allocation_params( target_flavor["guest-epa"].get("cpu-quota") ) @@ -694,7 +700,7 @@ class Ns(object): extended["cpu-quota"] = cpuquota if target_flavor["guest-epa"].get("mem-quota"): - vduquota = _get_resource_allocation_params( + vduquota = Ns._get_resource_allocation_params( target_flavor["guest-epa"].get("mem-quota") ) @@ -702,7 +708,7 @@ class Ns(object): extended["mem-quota"] = vduquota if target_flavor["guest-epa"].get("disk-io-quota"): - diskioquota = _get_resource_allocation_params( + diskioquota = Ns._get_resource_allocation_params( target_flavor["guest-epa"].get("disk-io-quota") ) @@ -710,7 +716,7 @@ class Ns(object): extended["disk-io-quota"] = diskioquota if target_flavor["guest-epa"].get("vif-quota"): - vifquota = _get_resource_allocation_params( + vifquota = Ns._get_resource_allocation_params( target_flavor["guest-epa"].get("vif-quota") ) diff --git a/NG-RO/osm_ng_ro/tests/test_ns.py b/NG-RO/osm_ng_ro/tests/test_ns.py index 7e6757aa..4cfc30bc 100644 --- a/NG-RO/osm_ng_ro/tests/test_ns.py +++ b/NG-RO/osm_ng_ro/tests/test_ns.py @@ -162,7 +162,9 @@ class TestNs(unittest.TestCase): expected_result = { "find_params": {}, } - target_image = {"no_image": "to_see_here"} + target_image = { + "no_image": "to_see_here", + } result = Ns._process_image_params( target_image=target_image, @@ -180,7 +182,9 @@ class TestNs(unittest.TestCase): }, }, } - target_image = {"image": "cirros"} + target_image = { + "image": "cirros", + } result = Ns._process_image_params( target_image=target_image, @@ -198,7 +202,9 @@ class TestNs(unittest.TestCase): }, }, } - target_image = {"vim_image_id": "123456"} + target_image = { + "vim_image_id": "123456", + } result = Ns._process_image_params( target_image=target_image, @@ -216,7 +222,9 @@ class TestNs(unittest.TestCase): }, }, } - target_image = {"image_checksum": "e3fc50a88d0a364313df4b21ef20c29e"} + target_image = { + "image_checksum": "e3fc50a88d0a364313df4b21ef20c29e", + } result = Ns._process_image_params( target_image=target_image, @@ -225,3 +233,85 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) + + def test__get_resource_allocation_params_with_empty_target_image(self): + expected_result = {} + quota_descriptor = {} + + result = Ns._get_resource_allocation_params( + quota_descriptor=quota_descriptor, + ) + + self.assertDictEqual(expected_result, result) + + def test__get_resource_allocation_params_with_wrong_target_image(self): + expected_result = {} + quota_descriptor = { + "no_quota": "present_here", + } + + result = Ns._get_resource_allocation_params( + quota_descriptor=quota_descriptor, + ) + + self.assertDictEqual(expected_result, result) + + def test__get_resource_allocation_params_with_limit(self): + expected_result = { + "limit": 10, + } + quota_descriptor = { + "limit": "10", + } + + result = Ns._get_resource_allocation_params( + quota_descriptor=quota_descriptor, + ) + + self.assertDictEqual(expected_result, result) + + def test__get_resource_allocation_params_with_reserve(self): + expected_result = { + "reserve": 20, + } + quota_descriptor = { + "reserve": "20", + } + + result = Ns._get_resource_allocation_params( + quota_descriptor=quota_descriptor, + ) + + self.assertDictEqual(expected_result, result) + + def test__get_resource_allocation_params_with_shares(self): + expected_result = { + "shares": 30, + } + quota_descriptor = { + "shares": "30", + } + + result = Ns._get_resource_allocation_params( + quota_descriptor=quota_descriptor, + ) + + self.assertDictEqual(expected_result, result) + + def test__get_resource_allocation_params(self): + expected_result = { + "limit": 10, + "reserve": 20, + "shares": 30, + } + quota_descriptor = { + "limit": "10", + "reserve": "20", + "shares": "30", + } + + result = Ns._get_resource_allocation_params( + quota_descriptor=quota_descriptor, + ) + + self.assertDictEqual(expected_result, result) diff --git a/releasenotes/notes/extracting_get_resource_allocation_params-7c96275fcde1d249.yaml b/releasenotes/notes/extracting_get_resource_allocation_params-7c96275fcde1d249.yaml new file mode 100644 index 00000000..cc5ec331 --- /dev/null +++ b/releasenotes/notes/extracting_get_resource_allocation_params-7c96275fcde1d249.yaml @@ -0,0 +1,23 @@ +####################################################################################### +# Copyright ETSI Contributors and Others. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +####################################################################################### +--- +other: + - | + Extraction of _get_resource_allocation_params() from being a nested function inside Ns.deploy(). + The _get_resource_allocation_params() function is now a static method inside the Ns class. This + eases the testability of _get_resource_allocation_params(). + With this extraction a unit test was introduced to cover the extracted function.