diff options
author | ERIMROB <robertas.rimkus@est.tech> | 2020-02-27 10:05:37 +0000 |
---|---|---|
committer | ERIMROB <robertas.rimkus@est.tech> | 2020-03-23 14:17:18 +0000 |
commit | b074a929a43629a5d4ced09f1ebe4106241d776f (patch) | |
tree | ed2477888b4b5091736be7209b332ee2c43ab58b /components/pm-subscription-handler/tests | |
parent | 06ab83c7455d6474548d63a146754748f830922c (diff) |
[PMSH] Refactor subscription processor and policy response handler
Signed-off-by: ERIMROB <robertas.rimkus@est.tech>
Change-Id: I91964848df8f7455169650b138b46d8dfc326b6f
Issue-ID: DCAEGEN2-1820
Diffstat (limited to 'components/pm-subscription-handler/tests')
6 files changed, 242 insertions, 185 deletions
diff --git a/components/pm-subscription-handler/tests/test_healthcheck.py b/components/pm-subscription-handler/tests/test_healthcheck.py index 6e960d05..1c40c3ff 100755 --- a/components/pm-subscription-handler/tests/test_healthcheck.py +++ b/components/pm-subscription-handler/tests/test_healthcheck.py @@ -18,7 +18,7 @@ import unittest -from pmsh_service.mod.healthcheck import status +from mod.healthcheck import status class HealthcheckTestCase(unittest.TestCase): diff --git a/components/pm-subscription-handler/tests/test_pmsh_service.py b/components/pm-subscription-handler/tests/test_pmsh_service.py deleted file mode 100644 index cd28a5d9..00000000 --- a/components/pm-subscription-handler/tests/test_pmsh_service.py +++ /dev/null @@ -1,88 +0,0 @@ -# ============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 os -import json -from unittest import TestCase -from unittest.mock import patch - -import pmsh_service_main as pmsh_service -from mod.network_function import NetworkFunction - - -class PMSHServiceTest(TestCase): - - @patch('mod.create_app') - @patch('mod.subscription.Subscription') - @patch('mod.pmsh_utils._MrPub') - @patch('mod.config_handler.ConfigHandler') - def setUp(self, mock_config_handler, mock_mr_pub, - mock_sub, mock_app): - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data_1 = json.load(data) - self.mock_app = mock_app - 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') - @patch('pmsh_service_main.PeriodicTask') - @patch('pmsh_service_main.AppConfig') - def test_subscription_processor_changed_state(self, mock_app_conf, 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_aai_sub) - - self.mock_sub.process_subscription.assert_called_with(self.nfs, self.mock_mr_pub, - mock_app_conf.return_value) - - @patch('threading.Timer') - @patch('mod.pmsh_logging.debug') - @patch('mod.aai_client.get_pmsh_subscription_data') - def test_subscription_processor_unchanged_state(self, mock_get_aai, mock_logger, 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 - - pmsh_service.subscription_processor(self.mock_config_handler, 'UNLOCKED', self.mock_mr_pub, - self.mock_app, self.mock_aai_sub) - - mock_logger.assert_called_with('Administrative State did not change in the Config') - - @patch('threading.Timer') - @patch('mod.pmsh_logging.debug') - @patch('mod.aai_client.get_pmsh_subscription_data') - def test_subscription_processor_exception(self, mock_get_aai, mock_logger, 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 - 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_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_pmsh_utils.py b/components/pm-subscription-handler/tests/test_pmsh_utils.py index ea657f49..236331b4 100644 --- a/components/pm-subscription-handler/tests/test_pmsh_utils.py +++ b/components/pm-subscription-handler/tests/test_pmsh_utils.py @@ -23,13 +23,10 @@ from unittest.mock import patch import responses from requests import Session -from tenacity import stop_after_attempt from mod import db, get_db_connection_url, create_app -from mod.db_models import SubscriptionModel -from mod.pmsh_utils import AppConfig, policy_response_handle_functions +from mod.pmsh_utils import AppConfig from mod.subscription import Subscription -from mod.network_function import NetworkFunction class PmshUtilsTestCase(TestCase): @@ -126,93 +123,3 @@ class PmshUtilsTestCase(TestCase): self.env.set('PMSH_PG_PASSWORD', 'pass') with self.assertRaises(Exception): get_db_connection_url() - - @patch('mod.pmsh_utils.NetworkFunction.delete') - def test_handle_response_locked_success(self, mock_delete): - with patch.dict(policy_response_handle_functions, {'LOCKED': {'success': mock_delete}}): - administrative_state = 'LOCKED' - nf = NetworkFunction(nf_name='nf1') - self.policy_mr_sub._handle_response(self.sub.subscriptionName, administrative_state, - nf.nf_name, 'success') - - mock_delete.assert_called() - - @patch('mod.subscription.Subscription.update_sub_nf_status') - def test_handle_response_locked_failed(self, mock_update_sub_nf): - with patch.dict(policy_response_handle_functions, - {'LOCKED': {'failed': mock_update_sub_nf}}): - administrative_state = 'LOCKED' - nf = NetworkFunction(nf_name='nf1') - self.policy_mr_sub._handle_response(self.sub.subscriptionName, administrative_state, - nf.nf_name, 'failed') - mock_update_sub_nf.assert_called() - - @patch('mod.subscription.Subscription.update_sub_nf_status') - def test_handle_response_unlocked_success(self, mock_update_sub_nf): - with patch.dict(policy_response_handle_functions, - {'UNLOCKED': {'success': mock_update_sub_nf}}): - nf = NetworkFunction(nf_name='nf1') - self.policy_mr_sub._handle_response(self.sub.subscriptionName, - self.sub.administrativeState, - nf.nf_name, 'success') - mock_update_sub_nf.assert_called() - - @patch('mod.subscription.Subscription.update_sub_nf_status') - def test_handle_response_unlocked_failed(self, mock_update_sub_nf): - with patch.dict(policy_response_handle_functions, - {'UNLOCKED': {'failed': mock_update_sub_nf}}): - nf = NetworkFunction(nf_name='nf1') - self.policy_mr_sub._handle_response(self.sub.subscriptionName, - self.sub.administrativeState, - nf.nf_name, 'failed') - mock_update_sub_nf.assert_called() - - def test_handle_response_exception(self): - self.assertRaises(Exception, self.policy_mr_sub._handle_response, 'sub1', 'wrong_state', - 'nf1', 'wrong_message') - - @patch('mod.pmsh_utils._MrSub.get_from_topic') - @patch('mod.pmsh_utils._MrSub._handle_response') - @patch('mod.subscription.Subscription.get') - @patch('threading.Timer') - def test_poll_policy_topic_calls_methods_correct_sub(self, mock_thread, mock_get_sub, - mock_handle_response, mock_get_from_topic): - result_data = ['{"name": "ResponseEvent","status": { "subscriptionName": ' - '"ExtraPM-All-gNB-R2B", "nfName": "pnf300", "message": "success" } }'] - mock_get_from_topic.return_value = result_data - mock_thread.start.return_value = 1 - mock_get_sub.return_value = SubscriptionModel(subscription_name='ExtraPM-All-gNB-R2B', - status='UNLOCKED') - self.policy_mr_sub.poll_policy_topic(self.sub.subscriptionName, self.mock_app) - - mock_get_from_topic.assert_called() - mock_handle_response.assert_called_with(self.sub.subscriptionName, - 'UNLOCKED', 'pnf300', 'success') - - @patch('mod.pmsh_utils._MrSub.get_from_topic') - @patch('mod.pmsh_utils._MrSub._handle_response') - @patch('mod.subscription.Subscription.get') - @patch('threading.Timer') - def test_poll_policy_topic_no_method_calls_incorrect_sub(self, mock_thread, mock_get_sub, - mock_handle_response, - mock_get_from_topic): - result_data = ['{"name": "ResponseEvent","status": { "subscriptionName": ' - '"demo-subscription", "nfName": "pnf300", "message": "success" } }'] - mock_get_from_topic.return_value = result_data - mock_thread.start.return_value = 1 - mock_get_sub.return_value = SubscriptionModel(subscription_name='ExtraPM-All-gNB-R2B', - status='UNLOCKED') - self.policy_mr_sub.poll_policy_topic(self.sub, self.mock_app) - - mock_get_from_topic.assert_called() - mock_handle_response.assert_not_called() - - @patch('mod.subscription.Subscription.get') - @patch('mod.pmsh_utils._MrSub.get_from_topic') - def test_poll_policy_topic_exception(self, mock_get_from_topic, mock_get_sub): - mock_get_from_topic.return_value = 'wrong_return' - mock_get_sub.return_value = SubscriptionModel(subscription_name='ExtraPM-All-gNB-R2B', - status='UNLOCKED') - self.policy_mr_sub.poll_policy_topic.retry.stop = stop_after_attempt(1) - - self.assertRaises(Exception, self.policy_mr_sub.poll_policy_topic, 'sub1', self.mock_app) diff --git a/components/pm-subscription-handler/tests/test_policy_response_handler.py b/components/pm-subscription-handler/tests/test_policy_response_handler.py new file mode 100644 index 00000000..1cf947fb --- /dev/null +++ b/components/pm-subscription-handler/tests/test_policy_response_handler.py @@ -0,0 +1,133 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019-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 +import os +from unittest import TestCase +from unittest.mock import patch + +from tenacity import stop_after_attempt + +from mod.db_models import SubscriptionModel +from mod.network_function import NetworkFunction +from mod.subscription import AdministrativeState, SubNfState +from mod.policy_response_handler import PolicyResponseHandler, policy_response_handle_functions + + +class PolicyResponseHandlerTest(TestCase): + + @patch('mod.create_app') + @patch('mod.subscription.Subscription') + @patch('mod.pmsh_utils._MrSub') + def setUp(self, mock_mr_sub, mock_sub, mock_app): + with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: + self.cbs_data = json.load(data) + self.mock_policy_mr_sub = mock_mr_sub + self.mock_sub = mock_sub + self.mock_sub.subscriptionName = 'ExtraPM-All-gNB-R2B' + self.mock_app = mock_app + self.nf = NetworkFunction(nf_name='nf1') + self.policy_response_handler = PolicyResponseHandler(self.mock_policy_mr_sub, + self.mock_sub.subscriptionName, + self.mock_app) + + @patch('mod.network_function.NetworkFunction.delete') + def test_handle_response_locked_success(self, mock_delete): + with patch.dict(policy_response_handle_functions, + {AdministrativeState.LOCKED.value: {'success': mock_delete}}): + self.policy_response_handler._handle_response(self.mock_sub.subscriptionName, + AdministrativeState.LOCKED.value, + self.nf.nf_name, 'success') + + mock_delete.assert_called() + + @patch('mod.subscription.Subscription.update_sub_nf_status') + def test_handle_response_locked_failed(self, mock_update_sub_nf): + with patch.dict(policy_response_handle_functions, + {AdministrativeState.LOCKED.value: {'failed': mock_update_sub_nf}}): + self.policy_response_handler._handle_response(self.mock_sub.subscriptionName, + AdministrativeState.LOCKED.value, + self.nf.nf_name, 'failed') + mock_update_sub_nf.assert_called_with(subscription_name=self.mock_sub.subscriptionName, + status=SubNfState.DELETE_FAILED.value, + nf_name=self.nf.nf_name) + + @patch('mod.subscription.Subscription.update_sub_nf_status') + def test_handle_response_unlocked_success(self, mock_update_sub_nf): + with patch.dict(policy_response_handle_functions, + {AdministrativeState.UNLOCKED.value: {'success': mock_update_sub_nf}}): + self.policy_response_handler._handle_response(self.mock_sub.subscriptionName, + AdministrativeState.UNLOCKED.value, + self.nf.nf_name, 'success') + mock_update_sub_nf.assert_called_with(subscription_name=self.mock_sub.subscriptionName, + status=SubNfState.CREATED.value, + nf_name=self.nf.nf_name) + + @patch('mod.subscription.Subscription.update_sub_nf_status') + def test_handle_response_unlocked_failed(self, mock_update_sub_nf): + with patch.dict(policy_response_handle_functions, + {AdministrativeState.UNLOCKED.value: {'failed': mock_update_sub_nf}}): + self.policy_response_handler._handle_response(self.mock_sub.subscriptionName, + AdministrativeState.UNLOCKED.value, + self.nf.nf_name, 'failed') + mock_update_sub_nf.assert_called_with(subscription_name=self.mock_sub.subscriptionName, + status=SubNfState.CREATE_FAILED.value, + nf_name=self.nf.nf_name) + + def test_handle_response_exception(self): + self.assertRaises(Exception, self.policy_response_handler._handle_response, 'sub1', + 'wrong_state', 'nf1', 'wrong_message') + + @patch('mod.policy_response_handler.PolicyResponseHandler._handle_response') + @patch('mod.subscription.Subscription.get') + def test_poll_policy_topic_calls_methods_correct_sub(self, mock_get_sub, mock_handle_response): + response_data = ['{"name": "ResponseEvent","status": { "subscriptionName": ' + '"ExtraPM-All-gNB-R2B", "nfName": "pnf300", "message": "success" } }'] + self.mock_policy_mr_sub.get_from_topic.return_value = response_data + mock_get_sub.return_value = SubscriptionModel(subscription_name='ExtraPM-All-gNB-R2B', + status=AdministrativeState.UNLOCKED.value) + self.policy_response_handler.poll_policy_topic() + + self.mock_policy_mr_sub.get_from_topic.assert_called() + + mock_handle_response.assert_called_with(self.mock_sub.subscriptionName, + AdministrativeState.UNLOCKED.value, 'pnf300', + 'success') + + @patch('mod.policy_response_handler.PolicyResponseHandler._handle_response') + @patch('mod.subscription.Subscription.get') + def test_poll_policy_topic_no_method_calls_incorrect_sub(self, mock_get_sub, + mock_handle_response): + response_data = ['{"name": "ResponseEvent","status": { "subscriptionName": ' + '"Different_Subscription", "nfName": "pnf300", "message": "success" } }'] + self.mock_policy_mr_sub.get_from_topic.return_value = response_data + mock_get_sub.return_value = SubscriptionModel(subscription_name='ExtraPM-All-gNB-R2B', + status=AdministrativeState.UNLOCKED.value) + self.policy_response_handler.poll_policy_topic() + + self.mock_policy_mr_sub.get_from_topic.assert_called() + + mock_handle_response.assert_not_called() + + @patch('mod.subscription.Subscription.get') + def test_poll_policy_topic_exception(self, mock_get_sub): + self.mock_policy_mr_sub.get_from_topic.return_value = 'wrong_return' + mock_get_sub.return_value = SubscriptionModel(subscription_name='ExtraPM-All-gNB-R2B', + status=AdministrativeState.UNLOCKED.value) + self.policy_response_handler.poll_policy_topic.retry.stop = stop_after_attempt(1) + + self.assertRaises(Exception, self.policy_response_handler.poll_policy_topic) diff --git a/components/pm-subscription-handler/tests/test_subscription.py b/components/pm-subscription-handler/tests/test_subscription.py index bd39f28a..d152863d 100755 --- a/components/pm-subscription-handler/tests/test_subscription.py +++ b/components/pm-subscription-handler/tests/test_subscription.py @@ -26,9 +26,9 @@ from tenacity import stop_after_attempt import mod.aai_client as aai_client from mod import db, create_app -from mod.network_function import NetworkFunction +from mod.network_function import NetworkFunction, NetworkFunctionFilter from mod.pmsh_utils import AppConfig -from mod.subscription import Subscription, NetworkFunctionFilter +from mod.subscription import Subscription class SubscriptionTest(TestCase): diff --git a/components/pm-subscription-handler/tests/test_subscription_handler.py b/components/pm-subscription-handler/tests/test_subscription_handler.py new file mode 100644 index 00000000..0eed7c45 --- /dev/null +++ b/components/pm-subscription-handler/tests/test_subscription_handler.py @@ -0,0 +1,105 @@ +# ============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 os +import json +from unittest import TestCase +from unittest.mock import patch + +from mod.subscription_handler import SubscriptionHandler +from mod.subscription import AdministrativeState +from mod.network_function import NetworkFunction + + +class SubscriptionHandlerTest(TestCase): + + @patch('mod.create_app') + @patch('mod.subscription.Subscription') + @patch('mod.pmsh_utils._MrPub') + @patch('mod.pmsh_utils.PeriodicTask') + @patch('mod.config_handler.ConfigHandler') + @patch('mod.pmsh_utils.AppConfig') + def setUp(self, mock_app_conf, mock_config_handler, mock_aai_thread, mock_mr_pub, + mock_sub, mock_app): + with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: + self.cbs_data_1 = json.load(data) + self.mock_app = mock_app + self.mock_sub = mock_sub + self.mock_mr_pub = mock_mr_pub + self.mock_aai_thread = mock_aai_thread + self.mock_config_handler = mock_config_handler + self.mock_app_conf = mock_app_conf + 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('mod.pmsh_logging.debug') + @patch('mod.aai_client.get_pmsh_subscription_data') + def test_execute_no_change_of_state(self, mock_get_aai, mock_logger): + mock_get_aai.return_value = self.mock_sub, self.nfs + self.mock_config_handler.get_config.return_value = self.cbs_data_1 + sub_handler = SubscriptionHandler(self.mock_config_handler, + AdministrativeState.UNLOCKED.value, self.mock_mr_pub, + self.mock_aai_thread, self.mock_app, self.mock_app_conf) + sub_handler.execute() + + mock_logger.assert_called_with('Administrative State did not change in the Config') + + @patch('mod.aai_client.get_pmsh_subscription_data') + def test_execute_change_of_state_unlocked(self, mock_get_aai): + mock_get_aai.return_value = self.mock_sub, self.nfs + self.mock_aai_thread.return_value.start.return_value = 'start_method' + self.mock_config_handler.get_config.return_value = self.cbs_data_1 + sub_handler = SubscriptionHandler(self.mock_config_handler, + AdministrativeState.LOCKED.value, self.mock_mr_pub, + self.mock_aai_thread, self.mock_app, self.mock_app_conf) + sub_handler.execute() + + self.assertEqual(AdministrativeState.UNLOCKED.value, sub_handler.administrative_state) + self.mock_sub.process_subscription.assert_called_with(self.nfs, self.mock_mr_pub, + self.mock_app_conf) + self.mock_aai_thread.start.assert_called() + + @patch('mod.aai_client.get_pmsh_subscription_data') + def test_execute_change_of_state_locked(self, mock_get_aai): + mock_get_aai.return_value = self.mock_sub, self.nfs + self.mock_aai_thread.return_value.cancel.return_value = 'cancel_method' + self.cbs_data_1['policy']['subscription']['administrativeState'] = \ + AdministrativeState.LOCKED.value + self.mock_config_handler.get_config.return_value = self.cbs_data_1 + sub_handler = SubscriptionHandler(self.mock_config_handler, + AdministrativeState.UNLOCKED.value, self.mock_mr_pub, + self.mock_aai_thread, self.mock_app, self.mock_app_conf) + sub_handler.execute() + + self.assertEqual(AdministrativeState.LOCKED.value, sub_handler.administrative_state) + self.mock_sub.process_subscription.assert_called_with(self.nfs, self.mock_mr_pub, + self.mock_app_conf) + self.mock_aai_thread.cancel.assert_called() + + @patch('mod.pmsh_logging.debug') + @patch('mod.aai_client.get_pmsh_subscription_data') + def test_execute_exception(self, mock_get_aai, mock_logger): + mock_get_aai.return_value = self.mock_sub, self.nfs + self.mock_config_handler.get_config.return_value = self.cbs_data_1 + self.mock_sub.process_subscription.side_effect = Exception + sub_handler = SubscriptionHandler(self.mock_config_handler, + AdministrativeState.LOCKED.value, self.mock_mr_pub, + self.mock_aai_thread, self.mock_app, self.mock_app_conf) + sub_handler.execute() + + mock_logger.assert_called_with('Error occurred during the activation/deactivation process ') |