From bc9f41ed3e67ed0350951de62331b3cd87c6aa78 Mon Sep 17 00:00:00 2001 From: efiacor Date: Wed, 12 Feb 2020 23:31:16 +0000 Subject: Fix PMHS logs dir setup # Refactoring test|prod init Signed-off-by: efiacor Change-Id: Ib073cb762a7750fca7bdf415cf5080d04f7bda51 Issue-ID: DCAEGEN2-2078 --- components/pm-subscription-handler/Dockerfile | 5 ++++- .../pm-subscription-handler/pmsh_service/mod/__init__.py | 14 +------------- .../pm-subscription-handler/pmsh_service/pmsh_service.py | 4 ++-- .../pm-subscription-handler/tests/test_network_function.py | 12 +++++++++--- .../pm-subscription-handler/tests/test_subscription.py | 10 +++++++--- 5 files changed, 23 insertions(+), 22 deletions(-) (limited to 'components') diff --git a/components/pm-subscription-handler/Dockerfile b/components/pm-subscription-handler/Dockerfile index bb3f63c6..b1f3129b 100644 --- a/components/pm-subscription-handler/Dockerfile +++ b/components/pm-subscription-handler/Dockerfile @@ -30,7 +30,10 @@ ENV PMSHUSER=pmsh \ WORKDIR $APPDIR # add non root user & group -RUN addgroup --system $PMSHUSER && adduser --ingroup $PMSHUSER --system $PMSHUSER +RUN addgroup --system $PMSHUSER && adduser --ingroup $PMSHUSER --system $PMSHUSER && \ + # create and chown the LOGS_PATH + mkdir -p $LOGS_PATH && \ + chown -R $PMSHUSER:$PMSHUSER $LOGS_PATH COPY setup.py ./ COPY requirements.txt ./ diff --git a/components/pm-subscription-handler/pmsh_service/mod/__init__.py b/components/pm-subscription-handler/pmsh_service/mod/__init__.py index f8b1c598..722188ae 100644 --- a/components/pm-subscription-handler/pmsh_service/mod/__init__.py +++ b/components/pm-subscription-handler/pmsh_service/mod/__init__.py @@ -27,7 +27,7 @@ db = SQLAlchemy() basedir = os.path.abspath(os.path.dirname(__file__)) -def create_prod_app(): +def create_app(): logger.create_loggers(os.getenv('LOGS_PATH')) connex_app = App(__name__, specification_dir=basedir) app = connex_app.app @@ -38,18 +38,6 @@ def create_prod_app(): return app -def create_test_app(): - logger.create_loggers('./unit_test_logs') - connex_app = App(__name__, specification_dir=basedir) - app = connex_app.app - app.config['TESTING'] = True - app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - app.config['SQLALCHEMY_RECORD_QUERIES'] = True - app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('TEST_DB_URL', 'sqlite://') - db.init_app(app) - return app - - def get_db_connection_url(): pg_host = os.getenv('PMSH_PG_URL') pg_user = os.getenv('PMSH_PG_USERNAME') diff --git a/components/pm-subscription-handler/pmsh_service/pmsh_service.py b/components/pm-subscription-handler/pmsh_service/pmsh_service.py index 99689d01..c564a5e3 100755 --- a/components/pm-subscription-handler/pmsh_service/pmsh_service.py +++ b/components/pm-subscription-handler/pmsh_service/pmsh_service.py @@ -20,7 +20,7 @@ import time import mod.aai_client as aai_client import mod.pmsh_logging as logger -from mod import db, create_prod_app +from mod import db, create_app from mod.config_handler import ConfigHandler from mod.subscription import Subscription @@ -28,7 +28,7 @@ from mod.subscription import Subscription def main(): try: - app = create_prod_app() + app = create_app() app.app_context().push() db.create_all(app=app) diff --git a/components/pm-subscription-handler/tests/test_network_function.py b/components/pm-subscription-handler/tests/test_network_function.py index 2af1489b..267851d2 100755 --- a/components/pm-subscription-handler/tests/test_network_function.py +++ b/components/pm-subscription-handler/tests/test_network_function.py @@ -16,17 +16,23 @@ # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== import unittest +from test.support import EnvironmentVarGuard +from unittest import mock -from mod import db, create_test_app +from mod import db, create_app from mod.network_function import NetworkFunction class NetworkFunctionTests(unittest.TestCase): - def setUp(self): + @mock.patch('mod.get_db_connection_url') + def setUp(self, mock_get_db_url): + mock_get_db_url.return_value = 'sqlite://' self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried') self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active') - self.app = create_test_app() + self.env = EnvironmentVarGuard() + self.env.set('LOGS_PATH', './unit_test_logs') + self.app = create_app() self.app_context = self.app.app_context() self.app_context.push() db.create_all() diff --git a/components/pm-subscription-handler/tests/test_subscription.py b/components/pm-subscription-handler/tests/test_subscription.py index 3c7651d4..97c1d6a1 100755 --- a/components/pm-subscription-handler/tests/test_subscription.py +++ b/components/pm-subscription-handler/tests/test_subscription.py @@ -24,15 +24,17 @@ from unittest import mock from requests import Session import mod.aai_client as aai_client -from mod import db, create_test_app +from mod import db, create_app from mod.network_function import NetworkFunction from mod.subscription import Subscription, NetworkFunctionFilter class SubscriptionTest(unittest.TestCase): + @mock.patch('mod.get_db_connection_url') @mock.patch.object(Session, 'put') - def setUp(self, mock_session): + def setUp(self, mock_session, mock_get_db_url): + mock_get_db_url.return_value = 'sqlite://' with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: self.aai_response_data = data.read() mock_session.return_value.status_code = 200 @@ -40,6 +42,8 @@ class SubscriptionTest(unittest.TestCase): 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('TESTING', 'True') + self.env.set('LOGS_PATH', './unit_test_logs') with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: self.cbs_data_1 = json.load(data) with open(os.path.join(os.path.dirname(__file__), @@ -50,7 +54,7 @@ class SubscriptionTest(unittest.TestCase): self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried') self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active') self.xnf_filter = NetworkFunctionFilter(**self.sub_1.nfFilter) - self.app = create_test_app() + self.app = create_app() self.app_context = self.app.app_context() self.app_context.push() db.create_all() -- cgit 1.2.3-korg