| |
@@ -0,0 +1,132 @@
|
| |
+ # Copyright ⓒ 2017 Red Hat, Inc.
|
| |
+ # This file is part of fegistry.
|
| |
+ #
|
| |
+ # fegistry is free software: you can redistribute it and/or modify
|
| |
+ # it under the terms of the GNU General Public License as published by
|
| |
+ # the Free Software Foundation, either version 3 of the License, or
|
| |
+ # (at your option) any later version.
|
| |
+ #
|
| |
+ # fegistry is distributed in the hope that it will be useful,
|
| |
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| |
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| |
+ # GNU General Public License for more details.
|
| |
+ #
|
| |
+ # You should have received a copy of the GNU General Public License
|
| |
+ # along with fegistry. If not, see <http://www.gnu.org/licenses/>.
|
| |
+ """This test suite contains tests on fegistry.config."""
|
| |
+ import logging
|
| |
+ import mock
|
| |
+ import unittest
|
| |
+
|
| |
+ import flask
|
| |
+
|
| |
+ from fegistry import config
|
| |
+
|
| |
+
|
| |
+ class LoadTestCase(unittest.TestCase):
|
| |
+ """A test class for testing the load() function."""
|
| |
+ @mock.patch('fegistry.config.open',
|
| |
+ mock.mock_open(read_data='# Some comment\n---\n# LOG_LEVEL: ERROR'))
|
| |
+ @mock.patch('fegistry.config.os.path.exists', return_value=True)
|
| |
+ def test_empty_config_file(self, exists):
|
| |
+ """Ensure that fegistry can handle an empty config file, and uses defaults in this case."""
|
| |
+ app = flask.Flask('test')
|
| |
+
|
| |
+ with mock.patch.object(app.logger, 'info'):
|
| |
+ with mock.patch.object(app.logger, 'addHandler'):
|
| |
+ config.load(app)
|
| |
+
|
| |
+ self.assertEqual(app.logger.addHandler.call_count, 1)
|
| |
+ handler = app.logger.addHandler.mock_calls[0][1][0]
|
| |
+
|
| |
+ info_logs = '\n'.join(c[1][0] for c in app.logger.info.mock_calls)
|
| |
+ self.assertEqual(
|
| |
+ info_logs,
|
| |
+ ('Reading config from /etc/fegistry/fegistry.yaml\nConfig file does not map to an '
|
| |
+ 'associative array. Using default config only\nConfiguring a syslog handler at '
|
| |
+ 'level: WARNING'))
|
| |
+
|
| |
+ exists.assert_called_once_with('/etc/fegistry/fegistry.yaml')
|
| |
+ self.assertTrue(isinstance(handler, logging.handlers.SysLogHandler))
|
| |
+ self.assertEqual(handler.address, '/dev/log')
|
| |
+ # The default value of WARNING should have been used since the config file was just comments
|
| |
+ self.assertEqual(handler.level, logging.WARNING)
|
| |
+
|
| |
+ @mock.patch('fegistry.config.open',
|
| |
+ mock.mock_open(read_data='# Some comment\n---\nLOG_LEVEL: ERROR'))
|
| |
+ @mock.patch('fegistry.config.os.path.exists', return_value=True)
|
| |
+ @mock.patch.dict('fegistry.config.os.environ', {'FEGISTRY_CONFIG': '/some/custom.yaml'})
|
| |
+ def test_fegistry_config_env_var_provided(self, exists):
|
| |
+ """Ensure correct operation when the FEGISTRY_CONFIG environment variable is provided."""
|
| |
+ app = flask.Flask('test')
|
| |
+
|
| |
+ with mock.patch.object(app.logger, 'info'):
|
| |
+ with mock.patch.object(app.logger, 'addHandler'):
|
| |
+ config.load(app)
|
| |
+
|
| |
+ self.assertEqual(app.logger.addHandler.call_count, 1)
|
| |
+ handler = app.logger.addHandler.mock_calls[0][1][0]
|
| |
+
|
| |
+ info_logs = '\n'.join(c[1][0] for c in app.logger.info.mock_calls)
|
| |
+ self.assertEqual(
|
| |
+ info_logs,
|
| |
+ ('Reading config from /some/custom.yaml\nConfiguring a syslog handler at level: '
|
| |
+ 'ERROR'))
|
| |
+
|
| |
+ exists.assert_called_once_with('/some/custom.yaml')
|
| |
+ self.assertTrue(isinstance(handler, logging.handlers.SysLogHandler))
|
| |
+ self.assertEqual(handler.address, '/dev/log')
|
| |
+ # The ERROR log level should have been used since the config file was configured as such.
|
| |
+ self.assertEqual(handler.level, logging.ERROR)
|
| |
+
|
| |
+ @mock.patch('fegistry.config.os.path.exists', return_value=False)
|
| |
+ def test_missing_config_file(self, exists):
|
| |
+ """Ensure that a missing config file is handled gracefully, with a log message and the use
|
| |
+ of defaults."""
|
| |
+ app = flask.Flask('test')
|
| |
+
|
| |
+ with mock.patch.object(app.logger, 'info'):
|
| |
+ with mock.patch.object(app.logger, 'addHandler'):
|
| |
+ config.load(app)
|
| |
+
|
| |
+ self.assertEqual(app.logger.addHandler.call_count, 1)
|
| |
+ handler = app.logger.addHandler.mock_calls[0][1][0]
|
| |
+
|
| |
+ info_logs = '\n'.join(c[1][0] for c in app.logger.info.mock_calls)
|
| |
+ self.assertEqual(
|
| |
+ info_logs,
|
| |
+ ('Config file /etc/fegistry/fegistry.yaml not found. Using default config\n'
|
| |
+ 'Configuring a syslog handler at level: WARNING'))
|
| |
+
|
| |
+ exists.assert_called_once_with('/etc/fegistry/fegistry.yaml')
|
| |
+ self.assertTrue(isinstance(handler, logging.handlers.SysLogHandler))
|
| |
+ self.assertEqual(handler.address, '/dev/log')
|
| |
+ # The default value of WARNING should have been used since the config file didn't exist
|
| |
+ self.assertEqual(handler.level, logging.WARNING)
|
| |
+
|
| |
+ @mock.patch('fegistry.config.open',
|
| |
+ mock.mock_open(read_data='# Some comment\n---\nLOG_LEVEL: DEBUG'))
|
| |
+ @mock.patch('fegistry.config.os.path.exists', return_value=True)
|
| |
+ def test_with_config_file(self, exists):
|
| |
+ """Ensure correct operation when the FEGISTRY_CONFIG environment variable is not
|
| |
+ provided and the config file is not empty."""
|
| |
+ app = flask.Flask('test')
|
| |
+
|
| |
+ with mock.patch.object(app.logger, 'info'):
|
| |
+ with mock.patch.object(app.logger, 'addHandler'):
|
| |
+ config.load(app)
|
| |
+
|
| |
+ self.assertEqual(app.logger.addHandler.call_count, 1)
|
| |
+ handler = app.logger.addHandler.mock_calls[0][1][0]
|
| |
+
|
| |
+ info_logs = '\n'.join(c[1][0] for c in app.logger.info.mock_calls)
|
| |
+ self.assertEqual(
|
| |
+ info_logs,
|
| |
+ ('Reading config from /etc/fegistry/fegistry.yaml\nConfiguring a syslog handler at '
|
| |
+ 'level: DEBUG'))
|
| |
+
|
| |
+ exists.assert_called_once_with('/etc/fegistry/fegistry.yaml')
|
| |
+ self.assertTrue(isinstance(handler, logging.handlers.SysLogHandler))
|
| |
+ self.assertEqual(handler.address, '/dev/log')
|
| |
+ # The DEBUG log level should have been used since the config file was configured as such.
|
| |
+ self.assertEqual(handler.level, logging.DEBUG)
|
| |
This commit adds a syslog handler to fegistry. It also adds a
simple YAML configuration system to fegistry, and uses it to allow
the user to configure the syslog handler's log level.
Signed-off-by: Randy Barlow randy@electronsweatshop.com