-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Logrotate] Update log rotate configuration via ConfigDB (#61)
* Add log rotation configuration mechanism * Handle changes in LOGGING table Signed-off-by: Yevhen Fastiuk <[email protected]> * Added UT for logrotate service in hostcfgd * Align run_cmd function input arguments Signed-off-by: Yevhen Fastiuk <[email protected]> --------- Signed-off-by: Yevhen Fastiuk <[email protected]>
- Loading branch information
Showing
3 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import importlib.machinery | ||
import importlib.util | ||
import os | ||
import sys | ||
|
||
from copy import copy | ||
from swsscommon import swsscommon | ||
from syslog import syslog, LOG_ERR | ||
from tests.hostcfgd.test_logging_vectors \ | ||
import HOSTCFGD_TEST_LOGGING_VECTOR as logging_test_data | ||
from tests.common.mock_configdb import MockConfigDb, MockDBConnector | ||
from unittest import TestCase, mock | ||
|
||
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
modules_path = os.path.dirname(test_path) | ||
scripts_path = os.path.join(modules_path, "scripts") | ||
src_path = os.path.dirname(modules_path) | ||
templates_path = os.path.join(src_path, "sonic-host-services-data/templates") | ||
output_path = os.path.join(test_path, "hostcfgd/output") | ||
sample_output_path = os.path.join(test_path, "hostcfgd/sample_output") | ||
sys.path.insert(0, modules_path) | ||
|
||
# Load the file under test | ||
hostcfgd_path = os.path.join(scripts_path, 'hostcfgd') | ||
loader = importlib.machinery.SourceFileLoader('hostcfgd', hostcfgd_path) | ||
spec = importlib.util.spec_from_loader(loader.name, loader) | ||
hostcfgd = importlib.util.module_from_spec(spec) | ||
loader.exec_module(hostcfgd) | ||
sys.modules['hostcfgd'] = hostcfgd | ||
|
||
# Mock swsscommon classes | ||
hostcfgd.ConfigDBConnector = MockConfigDb | ||
hostcfgd.DBConnector = MockDBConnector | ||
hostcfgd.Table = mock.Mock() | ||
hostcfgd.run_cmd = mock.Mock() | ||
|
||
|
||
class TestHostcfgLogging(TestCase): | ||
""" | ||
Test hostcfgd daemon - LogRotate | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(TestHostcfgLogging, self).__init__(*args, **kwargs) | ||
self.host_config_daemon = None | ||
|
||
def setUp(self): | ||
MockConfigDb.set_config_db(logging_test_data['initial']) | ||
self.host_config_daemon = hostcfgd.HostConfigDaemon() | ||
|
||
logging_config = self.host_config_daemon.config_db.get_table( | ||
swsscommon.CFG_LOGGING_TABLE_NAME) | ||
|
||
assert self.host_config_daemon.loggingcfg.cache == {} | ||
self.host_config_daemon.loggingcfg.load(logging_config) | ||
assert self.host_config_daemon.loggingcfg.cache != {} | ||
|
||
# Reset run_cmd mock | ||
hostcfgd.run_cmd.reset_mock() | ||
|
||
def tearDown(self): | ||
self.host_config_daemon = None | ||
MockConfigDb.set_config_db({}) | ||
|
||
def update_config(self, config_name): | ||
MockConfigDb.mod_config_db(logging_test_data[config_name]) | ||
|
||
syslog_data = logging_test_data[config_name]['LOGGING']['syslog'] | ||
debug_data = logging_test_data[config_name]['LOGGING']['debug'] | ||
|
||
self.host_config_daemon.logging_handler(key='syslog', op=None, | ||
data=syslog_data) | ||
self.host_config_daemon.logging_handler(key='debug', op=None, | ||
data=debug_data) | ||
|
||
def assert_applied(self, config_name): | ||
"""Assert that updated config triggered appropriate services | ||
Args: | ||
config_name: str: Test vectors config name | ||
Assert: | ||
Assert when config wasn't used | ||
""" | ||
orig_cache = copy(self.host_config_daemon.loggingcfg.cache) | ||
self.update_config(config_name) | ||
assert self.host_config_daemon.loggingcfg.cache != orig_cache | ||
hostcfgd.run_cmd.assert_called() | ||
|
||
def test_rsyslog_handle_modified(self): | ||
self.assert_applied('modified') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
''' | ||
hostcfgd test logging configuration vector | ||
''' | ||
|
||
HOSTCFGD_TEST_LOGGING_VECTOR = { | ||
'initial': { | ||
'DEVICE_METADATA': { | ||
'localhost': { | ||
'hostname': 'logrotate', | ||
}, | ||
}, | ||
'LOGGING': { | ||
'syslog': { | ||
'disk_percentage': '', | ||
'frequency': 'daily', | ||
'max_number': '20', | ||
'size': '10.0' | ||
}, | ||
'debug': { | ||
'disk_percentage': '', | ||
'frequency': 'daily', | ||
'max_number': '10', | ||
'size': '20.0' | ||
} | ||
}, | ||
"SSH_SERVER": { | ||
"POLICIES" :{ | ||
"max_sessions": "100" | ||
} | ||
} | ||
}, | ||
'modified': { | ||
'LOGGING': { | ||
'syslog': { | ||
'disk_percentage': '', | ||
'frequency': 'weekly', | ||
'max_number': '100', | ||
'size': '20.0' | ||
}, | ||
'debug': { | ||
'disk_percentage': '', | ||
'frequency': 'weekly', | ||
'max_number': '20', | ||
'size': '100.0' | ||
} | ||
} | ||
} | ||
} |