diff options
Diffstat (limited to 'components/pm-subscription-handler/tests')
5 files changed, 83 insertions, 11 deletions
diff --git a/components/pm-subscription-handler/tests/data/mr_aai_events.json b/components/pm-subscription-handler/tests/data/mr_aai_events.json new file mode 100755 index 00000000..202d1339 --- /dev/null +++ b/components/pm-subscription-handler/tests/data/mr_aai_events.json @@ -0,0 +1,6 @@ +{
+ "mr_response": [
+ "{\"cambria.partition\":\"AAI\",\"event-header\":{\"severity\":\"NORMAL\",\"entity-type\":\"pnf\",\"top-entity-type\":\"pnf\",\"entity-link\":\"/aai/v16/network/pnfs/pnf/pnf_newly_discovered\",\"event-type\":\"AAI-EVENT\",\"domain\":\"dev\",\"action\":\"UPDATE\",\"sequence-number\":\"0\",\"id\":\"db09e090-196e-4f84-9645-e449b1cd3640\",\"source-name\":\"dcae-curl\",\"version\":\"v16\",\"timestamp\":\"20200203-15:14:08:807\"},\"entity\":{\"ipaddress-v4-oam\":\"10.10.10.37\",\"nf-role\":\"gNB\",\"equip-type\":\"val8\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"service-instance\",\"relationship-data\":[{\"relationship-value\":\"Demonstration\",\"relationship-key\":\"customer.global-customer-id\"},{\"relationship-value\":\"vCPE\",\"relationship-key\":\"service-subscription.service-type\"},{\"relationship-value\":\"2c03b2a8-e31a-4749-9e99-3089ab441400\",\"relationship-key\":\"service-instance.service-instance-id\"}],\"related-link\":\"/aai/v16/business/customers/customer/Demonstration/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/2c03b2a8-e31a-4749-9e99-3089ab441400\",\"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\"related-to-property\":[{\"property-key\":\"service-instance.service-instance-name\",\"property-value\":\"Svc6_1\"}]}]},\"equip-vendor\":\"Ericsson\",\"serial-number\":\"6061ZW3\",\"ipaddress-v6-oam\":\"2001:0db8:0:0:0:0:1428:57ab\",\"equip-model\":\"val6\",\"in-maint\":false,\"resource-version\":\"1578668956804\",\"sw-version\":\"val7\",\"pnf-id\":\"eabcfaf7-b7f3-45fb-94e7-e6112fb3e8b8\",\"pnf-name\":\"pnf_newly_discovered\",\"orchestration-status\":\"Active\"}}",
+ "{\"cambria.partition\":\"AAI\",\"event-header\":{\"severity\":\"NORMAL\",\"entity-type\":\"pnf\",\"top-entity-type\":\"pnf\",\"entity-link\":\"/aai/v16/network/pnfs/pnf/pnf_newly_discovered\",\"event-type\":\"AAI-EVENT\",\"domain\":\"dev\",\"action\":\"UPDATE\",\"sequence-number\":\"0\",\"id\":\"db09e090-196e-4f84-9645-e449b1cd3640\",\"source-name\":\"dcae-curl\",\"version\":\"v16\",\"timestamp\":\"20200203-15:14:08:807\"},\"entity\":{\"orchestration-status\":\"Active\",\"ipaddress-v4-oam\":\"10.10.10.37\",\"nf-role\":\"gNB\",\"equip-type\":\"val8\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"service-instance\",\"relationship-data\":[{\"relationship-value\":\"Demonstration\",\"relationship-key\":\"customer.global-customer-id\"},{\"relationship-value\":\"vCPE\",\"relationship-key\":\"service-subscription.service-type\"},{\"relationship-value\":\"2c03b2a8-e31a-4749-9e99-3089ab441400\",\"relationship-key\":\"service-instance.service-instance-id\"}],\"related-link\":\"/aai/v16/business/customers/customer/Demonstration/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/2c03b2a8-e31a-4749-9e99-3089ab441400\",\"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\"related-to-property\":[{\"property-key\":\"service-instance.service-instance-name\",\"property-value\":\"Svc6_1\"}]}]},\"equip-vendor\":\"Ericsson\",\"serial-number\":\"6061ZW3\",\"ipaddress-v6-oam\":\"2001:0db8:0:0:0:0:1428:57ab\",\"equip-model\":\"val6\",\"in-maint\":false,\"resource-version\":\"1578668956804\",\"sw-version\":\"val7\",\"pnf-id\":\"eabcfaf7-b7f3-45fb-94e7-e6112fb3e8b8\",\"pnf-name\":\"pnf_already_active\",\"orchestration-status\":\"Active\"}}",
+ "{\"cambria.partition\":\"AAI\",\"event-header\":{\"severity\":\"NORMAL\",\"entity-type\":\"pnf\",\"top-entity-type\":\"pnf\",\"entity-link\":\"/aai/v16/network/pnfs/pnf/pnf_newly_discovered\",\"event-type\":\"AAI-EVENT\",\"domain\":\"dev\",\"action\":\"DELETE\",\"sequence-number\":\"0\",\"id\":\"db09e090-196e-4f84-9645-e449b1cd3640\",\"source-name\":\"dcae-curl\",\"version\":\"v16\",\"timestamp\":\"20200203-15:14:08:807\"},\"entity\":{\"ipaddress-v4-oam\":\"10.10.10.37\",\"nf-role\":\"gNB\",\"equip-type\":\"val8\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"service-instance\",\"relationship-data\":[{\"relationship-value\":\"Demonstration\",\"relationship-key\":\"customer.global-customer-id\"},{\"relationship-value\":\"vCPE\",\"relationship-key\":\"service-subscription.service-type\"},{\"relationship-value\":\"2c03b2a8-e31a-4749-9e99-3089ab441400\",\"relationship-key\":\"service-instance.service-instance-id\"}],\"related-link\":\"/aai/v16/business/customers/customer/Demonstration/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/2c03b2a8-e31a-4749-9e99-3089ab441400\",\"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\"related-to-property\":[{\"property-key\":\"service-instance.service-instance-name\",\"property-value\":\"Svc6_1\"}]}]},\"equip-vendor\":\"Ericsson\",\"serial-number\":\"6061ZW3\",\"ipaddress-v6-oam\":\"2001:0db8:0:0:0:0:1428:57ab\",\"equip-model\":\"val6\",\"in-maint\":false,\"resource-version\":\"1578668956804\",\"sw-version\":\"val7\",\"pnf-id\":\"eabcfaf7-b7f3-45fb-94e7-e6112fb3e8b8\",\"pnf-name\":\"pnf_to_be_deleted\",\"orchestration-status\":\"Active\"}}"]
+}
diff --git a/components/pm-subscription-handler/tests/test_aai_event_handler.py b/components/pm-subscription-handler/tests/test_aai_event_handler.py new file mode 100755 index 00000000..add7b3f5 --- /dev/null +++ b/components/pm-subscription-handler/tests/test_aai_event_handler.py @@ -0,0 +1,53 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2020 Nordix Foundation. +# ============================================================================ +# 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== +import json +from os import path +from unittest import TestCase +from unittest.mock import patch, Mock + +from mod.aai_event_handler import OrchestrationStatus, process_aai_events +from mod.network_function import NetworkFunction + + +class AAIEventHandlerTest(TestCase): + + def setUp(self): + with open(path.join(path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: + self.cbs_data_1 = json.load(data) + with open(path.join(path.dirname(__file__), 'data/mr_aai_events.json'), 'r') as data: + self.mr_aai_events = json.load(data)["mr_response"] + self.mock_sub = Mock(nfFilter={'swVersions': ['1.0.0', '1.0.1'], + 'nfNames': ['^pnf.*', '^vnf.*']}) + self.mock_mr_sub = Mock(get_from_topic=Mock(return_value=self.mr_aai_events)) + self.mock_mr_pub = Mock() + self.mock_app = Mock() + + @patch('mod.aai_event_handler.NetworkFunction.delete') + @patch('mod.aai_event_handler.NetworkFunction.get') + def test_process_aai_update_and_delete_events(self, mock_nf_get, mock_nf_delete): + pnf_already_active = NetworkFunction(nf_name='pnf_already_active', + orchestration_status=OrchestrationStatus.ACTIVE.value) + mock_nf_get.side_effect = [None, pnf_already_active] + expected_nf_for_processing = NetworkFunction( + nf_name='pnf_newly_discovered', orchestration_status=OrchestrationStatus.ACTIVE.value) + + process_aai_events(self.mock_mr_sub, self.mock_sub, self.mock_mr_pub, self.mock_app) + + self.mock_sub.process_subscription.assert_called_once_with([expected_nf_for_processing], + self.mock_mr_pub) + mock_nf_delete.assert_called_once_with(nf_name='pnf_to_be_deleted') diff --git a/components/pm-subscription-handler/tests/test_network_function.py b/components/pm-subscription-handler/tests/test_network_function.py index e9394b46..4fca0778 100755 --- a/components/pm-subscription-handler/tests/test_network_function.py +++ b/components/pm-subscription-handler/tests/test_network_function.py @@ -21,6 +21,7 @@ from unittest.mock import patch from mod import db, create_app from mod.network_function import NetworkFunction +from mod.subscription import Subscription class NetworkFunctionTests(TestCase): @@ -69,7 +70,13 @@ class NetworkFunctionTests(TestCase): def test_delete_network_function(self): self.nf_1.create() self.nf_2.create() - self.nf_1.delete(nf_name='pnf_1') - nfs = NetworkFunction.get_all() + sub = Subscription(**{"subscriptionName": "sub"}) + sub.add_network_functions_to_subscription([self.nf_1, self.nf_2]) + + NetworkFunction.delete(nf_name=self.nf_1.nf_name) + nfs = NetworkFunction.get_all() self.assertEqual(1, len(nfs)) + self.assertEqual(1, len(Subscription.get_all_nfs_subscription_relations())) + pnf_1_deleted = [nf for nf in nfs if nf.nf_name != self.nf_1.nf_name] + self.assertTrue(pnf_1_deleted) diff --git a/components/pm-subscription-handler/tests/test_pmsh_service.py b/components/pm-subscription-handler/tests/test_pmsh_service.py index 4a6032b5..b7225677 100644 --- a/components/pm-subscription-handler/tests/test_pmsh_service.py +++ b/components/pm-subscription-handler/tests/test_pmsh_service.py @@ -38,19 +38,22 @@ class PMSHServiceTest(TestCase): self.mock_sub = mock_sub self.mock_mr_pub = mock_mr_pub self.mock_config_handler = mock_config_handler + self.mock_aai_sub = mock_sub self.nf_1 = NetworkFunction(nf_name='pnf_1') self.nf_2 = NetworkFunction(nf_name='pnf_2') self.nfs = [self.nf_1, self.nf_2] @patch('threading.Timer') @patch('mod.aai_client.get_pmsh_subscription_data') - def test_subscription_processor_changed_state(self, mock_get_aai, mock_thread): + @patch('pmsh_service_main.PeriodicTask') + def test_subscription_processor_changed_state(self, periodic_task, mock_get_aai, mock_thread): self.mock_config_handler.get_config.return_value = self.cbs_data_1 mock_get_aai.return_value = self.mock_sub, self.nfs mock_thread.start.return_value = 1 + periodic_task.start.return_value = 1 pmsh_service.subscription_processor(self.mock_config_handler, 'LOCKED', - self.mock_mr_pub, self.mock_app) + self.mock_mr_pub, self.mock_app, self.mock_aai_sub) self.mock_sub.process_subscription.assert_called_with(self.nfs, self.mock_mr_pub) @@ -63,7 +66,7 @@ class PMSHServiceTest(TestCase): mock_thread.start.return_value = 1 pmsh_service.subscription_processor(self.mock_config_handler, 'UNLOCKED', self.mock_mr_pub, - self.mock_app) + self.mock_app, self.mock_aai_sub) mock_logger.assert_called_with('Administrative State did not change in the Config') @@ -77,6 +80,6 @@ class PMSHServiceTest(TestCase): self.mock_sub.process_subscription.side_effect = Exception pmsh_service.subscription_processor(self.mock_config_handler, 'LOCKED', self.mock_mr_pub, - self.mock_app) + self.mock_app, self.mock_aai_sub) mock_logger.assert_called_with(f'Error occurred during the ' f'activation/deactivation process ') diff --git a/components/pm-subscription-handler/tests/test_subscription.py b/components/pm-subscription-handler/tests/test_subscription.py index 8fe233e4..c357ad79 100755 --- a/components/pm-subscription-handler/tests/test_subscription.py +++ b/components/pm-subscription-handler/tests/test_subscription.py @@ -134,13 +134,16 @@ class SubscriptionTest(TestCase): self.assertEqual('new_status', sub.status) def test_delete_subscription(self): - self.sub_1.create() - subs = self.sub_1.get_all() - self.assertEqual(1, len(subs)) + self.sub_1.add_network_functions_to_subscription([self.nf_1, self.nf_2]) + self.sub_2.add_network_functions_to_subscription([self.nf_2]) self.sub_1.delete_subscription() - new_subs = self.sub_1.get_all() - self.assertEqual(0, len(new_subs)) + + self.assertEqual(1, len(Subscription.get_all())) + self.assertEqual(None, Subscription.get(self.sub_1.subscriptionName)) + self.assertEqual(1, len(Subscription.get_all_nfs_subscription_relations())) + self.assertEqual(1, len(NetworkFunction.get_all())) + self.assertEqual(None, NetworkFunction.get(nf_name=self.nf_1.nf_name)) def test_update_sub_nf_status(self): sub_name = 'ExtraPM-All-gNB-R2B' |