From ccb0dd3d383a314a8bda62e2353aea6bdd805f80 Mon Sep 17 00:00:00 2001 From: Hiromu Asahina Date: Fri, 10 Dec 2021 11:06:02 +0900 Subject: [PATCH] Fix filter data creation The tacker-server returns 500 error if a request body of the subscription creation [1] that doesn't have `vnfInstanceSubscriptionFilter` parameter, although it's an optional parameter. The happens simply because the `vnfInstanceSubscriptionFilter` is not handled as an optional parameter in the current codes. This patch fixes such codes to correctly handle the request without a `vnfInstanceSubscriptionFilter` parameter. [1]: https://docs.openstack.org/api-ref/nfv-orchestration/v1/vnflcm.html?expanded=create-a-new-subscription-detail Change-Id: Ibf9310c4ddffcc41e23eea8c5dfd212fd3a9a145 Closes-bug: #1947303 --- tacker/objects/vnf_lcm_subscriptions.py | 9 +-- tacker/tests/unit/objects/fakes.py | 1 + .../objects/test_vnf_lcm_subscriptions.py | 78 ++++++------------- 3 files changed, 27 insertions(+), 61 deletions(-) diff --git a/tacker/objects/vnf_lcm_subscriptions.py b/tacker/objects/vnf_lcm_subscriptions.py index 37e25c209..b292cd303 100644 --- a/tacker/objects/vnf_lcm_subscriptions.py +++ b/tacker/objects/vnf_lcm_subscriptions.py @@ -308,12 +308,11 @@ def _vnf_lcm_subscriptions_id_get(context, def _add_filter_data(context, subscription_id, filter): with db_api.context_manager.writer.using(context): - vnf_instance_subscription_filter = \ - filter.get('vnfInstanceSubscriptionFilter') - vnf_products_from_providers = \ - vnf_instance_subscription_filter.get( - 'vnfProductsFromProviders') + vnf_products_from_providers = ( + filter.get('vnfInstanceSubscriptionFilter', {}) + .get('vnfProductsFromProviders')) + if vnf_products_from_providers: vnf_products_from_providers = vnf_products_from_providers[0] diff --git a/tacker/tests/unit/objects/fakes.py b/tacker/tests/unit/objects/fakes.py index 553db1e7f..7f23de934 100644 --- a/tacker/tests/unit/objects/fakes.py +++ b/tacker/tests/unit/objects/fakes.py @@ -62,6 +62,7 @@ filter = { "usageState": ["NOT_IN_USE"], "vnfPkgId": ["f04857cb-abdc-405f-8254-01501f3fa059"], "vnfdId": ["b1bb0ce7-5555-0001-95ed-4840d70a1209"], + "vnfInstanceSubscriptionFilter": {"vnfdIds": []}, "vnfProductsFromProviders": [ { "vnfProvider": "xxxxx", diff --git a/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py b/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py index b635bcc5d..ef68f1ed5 100644 --- a/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py +++ b/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py @@ -12,6 +12,8 @@ # limitations under the License. from unittest import mock +import ddt + from tacker import context from tacker import objects from tacker.tests.unit.db.base import SqlTestCase @@ -19,6 +21,7 @@ from tacker.tests.unit.objects import fakes from tacker.tests import uuidsentinel +@ddt.ddt class TestVnfLcmSubScriptions(SqlTestCase): def setUp(self): @@ -112,63 +115,26 @@ class TestVnfLcmSubScriptions(SqlTestCase): return subscription_obj @mock.patch.object(objects.vnf_lcm_subscriptions, - '_vnf_lcm_subscriptions_create') - def test_create(self, mock_vnf_lcm_subscriptions_create): - filter = fakes.filter - subscription_obj = \ + '_vnf_lcm_subscriptions_id_get') + @ddt.data( + fakes.filter, + {"notificationTypes": ["VnfIdentifierCreationNotification"]} + ) + def test_create(self, filter, mock_vnf_lcm_subscriptions_id_get): + """Test that the `create` method returns a created filter + + Also, test `_vnf_lcm_subscriptions_create` method, which is called + from the `create` method, works correctly and a created filter will be + returned. + + Case 1: All available parameters are set + Case 2: Only the required parameter is set + """ + + mock_vnf_lcm_subscriptions_id_get.return_value = None + subscription_obj = ( objects.vnf_lcm_subscriptions.LccnSubscriptionRequest( - context=self.context) - mock_vnf_lcm_subscriptions_create.return_value = \ - '{\ - "filter": "{"operationStates": ["COMPLETED"],\ - "vnfInstanceNames": ["xxxxxxxxxxxxxxxxxx"],\ - "operationTypes": ["INSTANTIATE"],\ - "vnfdIds": ["405d73c7-e964-4c8b-a914-41478ccd7c42"],\ - "vnfProductsFromProviders": [{\ - "vnfProvider": "x2x", \ - "vnfProducts": [{\ - "vnfProductName": "x2xx", \ - "versions": [{\ - "vnfSoftwareVersion": "xx2XX", \ - "vnfdVersions": ["ss2"]\ - }]\ - }]\ - }, \ - {\ - "vnfProvider": "z2z",\ - "vnfProducts": [{\ - "vnfProductName": "z2zx", \ - "versions": [{\ - "vnfSoftwareVersion": "xx3XX",\ - "vnfdVersions": \ - ["s3sx", "s3sa"]\ - }\ - ]},\ - {\ - "vnfProductName": "zz3ex",\ - "versions": [{\ - "vnfSoftwareVersion": "xxe3eXz",\ - "vnfdVersions": ["ss3xz", "s3esaz"]\ - },\ - {\ - "vnfSoftwareVersion": "xxeeeXw", \ - "vnfdVersions": ["ss3xw", "ss3w"]\ - }]\ - }]\ - }],\ - "notificationTypes": [\ - "VnfLcmOperationOccurrenceNotification"],\ - "vnfInstanceIds": ["fb0b9a12-4b55-47ac-9ca8-5fdd52c4c07f"]}",\ - "callbackUri": "http://localhost/xxx",\ - "_links": {\ - "self": {\ - "href":\ - "http://localhost:9890//vnflcm/v1/subscriptions\ - /530a3c43-043a-4b84-9d65-aa0df49f7ced"\ - }\ - },\ - "id": "530a3c43-043a-4b84-9d65-aa0df49f7ced"\ - }' + context=self.context, **fakes.subscription_data)) result = subscription_obj.create(filter) self.assertTrue(filter, result)