validations-libs/validations_libs/tests/test_logger.py
Jiri Podivin bb98dea067 Restoring logging facility and implementing default logger
The logging statements in our modules, including CLI, are now
using a dedicated VF logger with Syslog and stdout handlers.

This will ensure, that all messages will be properly journaled
and that only messages >warn will be displayed to user by default.
Patch only changes behavior of the logging statements in the VF
code itself. Log statements in imported modules, such as ansible-runner,
will behave as before.

In the absence of available '/dev/log' device the user will be notified,
during logger init, by a warning level message.

Tests for the new code are included.

Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: If8ed6510dad16dc8495717789bb132b957828e0d
2022-09-20 09:46:02 +02:00

45 lines
1.4 KiB
Python

# Copyright 2022 Red Hat, Inc.
#
# 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.
#
try:
from unittest import mock
except ImportError:
import mock
from unittest import TestCase
import logging
from validations_libs import logger
class TestLogger(TestCase):
def setUp(self) -> None:
super().setUp()
@mock.patch('os.path.exists', reutrn_value=True)
def test_logger_init(self, mock_exists):
new_logger = logger.getLogger("fooo")
mock_exists.assert_called_once_with('/dev/log')
self.assertEqual(logging.Logger, type(new_logger))
@mock.patch('logging.Logger.warning')
@mock.patch('os.path.exists', return_value=False)
def test_logger_init_no_journal(self, mock_exists, mock_warning):
new_logger = logger.getLogger("fooo")
mock_exists.assert_called_once_with('/dev/log')
mock_warning.assert_called_once()
self.assertEqual(logging.Logger, type(new_logger))