diff options
Diffstat (limited to 'components/pm-subscription-handler/tests')
3 files changed, 14 insertions, 116 deletions
diff --git a/components/pm-subscription-handler/tests/test_aai_service.py b/components/pm-subscription-handler/tests/test_aai_service.py index aaf2bb14..1ee71554 100644 --- a/components/pm-subscription-handler/tests/test_aai_service.py +++ b/components/pm-subscription-handler/tests/test_aai_service.py @@ -32,7 +32,7 @@ class AaiClientTestCase(TestCase): def setUp(self): self.env = EnvironmentVarGuard() self.env.set('AAI_SERVICE_HOST', '1.2.3.4') - self.env.set('AAI_SERVICE_PORT_AAI_SSL', '8443') + self.env.set('AAI_SERVICE_PORT', '8443') with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: self.cbs_data = json.load(data) with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: diff --git a/components/pm-subscription-handler/tests/test_config_handler.py b/components/pm-subscription-handler/tests/test_config_handler.py deleted file mode 100755 index dce48fca..00000000 --- a/components/pm-subscription-handler/tests/test_config_handler.py +++ /dev/null @@ -1,95 +0,0 @@ -# ============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 unittest -from os import environ -from os import path - -import responses -from tenacity import wait_none - -from mod.config_handler import ConfigHandler - - -class ConfigHandlerTestCase(unittest.TestCase): - - def setUp(self): - self.env_vars = {'CONFIG_BINDING_SERVICE_SERVICE_HOST': 'cbs_hostname', - 'CONFIG_BINDING_SERVICE_SERVICE_PORT': '10000', - 'HOSTNAME': 'hostname'} - for key, value in self.env_vars.items(): - environ[key] = value - self.cbs_url = 'http://cbs_hostname:10000/service_component_all/hostname' - with open(path.join(path.dirname(__file__), 'data/cbs_data_2.json'))as json_file: - self.expected_config = json.load(json_file) - - def test_missing_environment_variable(self): - for key, value in self.env_vars.items(): - with self.assertRaises(KeyError): - environ.pop(key) - test_value = globals()[value] - test_value() - environ[key] = value - - @responses.activate - def test_get_config_success(self): - responses.add(responses.GET, self.cbs_url, json=self.expected_config, - status=200) - - config_handler = ConfigHandler() - config_handler.get_config.retry.wait = wait_none() - - self.assertEqual(self.expected_config, config_handler.get_config()) - - @responses.activate - def test_get_config_error(self): - responses.add(responses.GET, self.cbs_url, status=404) - config_handler = ConfigHandler() - config_handler.get_config.retry.wait = wait_none() - - with self.assertRaises(Exception): - config_handler.get_config() - - @responses.activate - def test_get_config_max_retries_error(self): - retry_limit = 5 - config_handler = ConfigHandler() - config_handler.get_config.retry.wait = wait_none() - - for __ in range(retry_limit): - responses.add(responses.GET, self.cbs_url, status=500) - - with self.assertRaises(Exception): - config_handler.get_config() - self.assertEqual(retry_limit, len(responses.calls)) - - @responses.activate - def test_get_config_less_than_5_retries_success(self): - retry_attempts = 4 - responses.add(responses.GET, self.cbs_url, status=500) - responses.add(responses.GET, self.cbs_url, status=400) - responses.add(responses.GET, self.cbs_url, status=300) - responses.add(responses.GET, self.cbs_url, json=json.dumps(self.expected_config), - status=200) - - config_handler = ConfigHandler() - config_handler.get_config.retry.wait = wait_none() - config_handler.get_config() - - self.assertEqual(retry_attempts, len(responses.calls)) diff --git a/components/pm-subscription-handler/tests/test_subscription_handler.py b/components/pm-subscription-handler/tests/test_subscription_handler.py index 168d0366..d922b96f 100644 --- a/components/pm-subscription-handler/tests/test_subscription_handler.py +++ b/components/pm-subscription-handler/tests/test_subscription_handler.py @@ -30,17 +30,15 @@ class SubscriptionHandlerTest(TestCase): @patch('mod.create_app') @patch('mod.subscription.Subscription') @patch('mod.pmsh_utils._MrPub') - @patch('mod.config_handler.ConfigHandler') @patch('mod.pmsh_utils.AppConfig') @patch('mod.pmsh_utils.PeriodicTask') - def setUp(self, mock_aai_event_thread, mock_app_conf, mock_config_handler, mock_mr_pub, + def setUp(self, mock_aai_event_thread, mock_app_conf, 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_app_conf = mock_app_conf self.mock_aai_event_thread = mock_aai_event_thread self.nf_1 = NetworkFunction(nf_name='pnf_1') @@ -51,25 +49,22 @@ class SubscriptionHandlerTest(TestCase): @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, + sub_handler = SubscriptionHandler(AdministrativeState.UNLOCKED.value, self.mock_mr_pub, self.mock_app, self.mock_app_conf, self.mock_aai_event_thread) - sub_handler.execute() - + with patch('mod.pmsh_utils.ConfigHandler.get_pmsh_config', return_value=self.cbs_data_1): + 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_event_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, + sub_handler = SubscriptionHandler(AdministrativeState.LOCKED.value, self.mock_mr_pub, self.mock_app, self.mock_app_conf, self.mock_aai_event_thread.return_value) - sub_handler.execute() + with patch('mod.pmsh_utils.ConfigHandler.get_pmsh_config', return_value=self.cbs_data_1): + 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, @@ -82,12 +77,11 @@ class SubscriptionHandlerTest(TestCase): self.mock_aai_event_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, + sub_handler = SubscriptionHandler(AdministrativeState.UNLOCKED.value, self.mock_mr_pub, self.mock_app, self.mock_app_conf, self.mock_aai_event_thread.return_value) - sub_handler.execute() + with patch('mod.pmsh_utils.ConfigHandler.get_pmsh_config', return_value=self.cbs_data_1): + 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, @@ -98,12 +92,11 @@ class SubscriptionHandlerTest(TestCase): @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, + sub_handler = SubscriptionHandler(AdministrativeState.LOCKED.value, self.mock_mr_pub, self.mock_app, self.mock_app_conf, self.mock_aai_event_thread) - sub_handler.execute() + with patch('mod.pmsh_utils.ConfigHandler.get_pmsh_config', return_value=self.cbs_data_1): + sub_handler.execute() mock_logger.assert_called_with('Error occurred during the activation/deactivation process ') |