diff --git a/bin/lib/ail_logger.py b/bin/lib/ail_logger.py index 1790c793..9bc76144 100755 --- a/bin/lib/ail_logger.py +++ b/bin/lib/ail_logger.py @@ -51,3 +51,24 @@ def get_access_config(create=False): logger.propagate = False return logger + +def get_test_config(create=False): + logger = logging.getLogger('test.log') + + if create: + formatter = logging.Formatter("%(asctime)s %(levelname)s:%(message)s") + + # STDOUT + handler = logging.StreamHandler() + handler.setLevel(logging.INFO) + logger.addHandler(handler) + + # FILE + handler = logging.handlers.RotatingFileHandler(filename=os.path.join(LOGS_DIR, f'test.log'), + maxBytes=10*1024*1024, backupCount=5) + handler.setLevel(logging.INFO) + handler.setFormatter(formatter) + logger.addHandler(handler) + + logger.propagate = False + return logger diff --git a/tests/test_api.py b/tests/test_api.py index 5f8a7a67..cd1d3fa4 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -5,30 +5,43 @@ import os import sys import unittest -from pyail import PyAIL +from pyail import PyAIL, PyAILError sys.path.append(os.environ['AIL_BIN']) ################################## # Import Project packages ################################## from lib import ail_users - +from lib import ail_logger from lib.ConfigLoader import ConfigLoader +test_logger = ail_logger.get_test_config(create=True) + class TestApiV1(unittest.TestCase): def setUp(self): config = ConfigLoader() port = config.get_config_str('Flask', 'port') - self.ail = PyAIL(f'https://localhost:{port}', ail_users.get_user_token('admin@admin.test'), ssl=False) + try: + self.ail = PyAIL(f'https://localhost:{port}', ail_users.get_user_token('admin@admin.test'), ssl=False) + except Exception as e: + print() + print('----------------------------------------------------') + test_logger.warning(f'Flask / Web interface is unreachable: {e}') + print('----------------------------------------------------') + print() + raise e # GET /api/v1/ping def test_0001_api_ping(self): - r = self.ail.ping_ail() - self.assertEqual(r.get('status'), 'pong') print() print('----------------------------------------------------') - print(' AIL successfully reached Flask / Web interface') + try: + r = self.ail.ping() + self.assertEqual(r.get('status'), 'pong') + test_logger.info('AIL successfully reached Flask / Web interface') + except (AssertionError, PyAILError) as ae: + test_logger.warning(f'Flask / Web interface is unreachable: {ae}') print('----------------------------------------------------') print()