salt/tests/unit/beacons/test_log.py
Erik Johnson f72bbc5036
Rename beacon unit test modules to conform with naming convention
Also consolidate pyroute2 network_settings tests into network_settings
beacon test module.
2018-08-14 13:53:29 -05:00

71 lines
2.0 KiB
Python

# coding: utf-8
# Python libs
from __future__ import absolute_import
# Salt testing libs
from tests.support.unit import skipIf, TestCase
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, mock_open
from tests.support.mixins import LoaderModuleMockMixin
# Salt libs
import salt.beacons.log as log
import logging
_log = logging.getLogger(__name__)
_STUB_LOG_ENTRY = 'Jun 29 12:58:51 hostname sshd[6536]: ' \
'pam_unix(sshd:session): session opened ' \
'for user username by (uid=0)\n'
@skipIf(NO_MOCK, NO_MOCK_REASON)
class LogBeaconTestCase(TestCase, LoaderModuleMockMixin):
'''
Test case for salt.beacons.log
'''
def setup_loader_modules(self):
return {
log: {
'__context__': {'log.loc': 2},
'__salt__': {},
}
}
def test_non_list_config(self):
config = {}
ret = log.validate(config)
self.assertEqual(ret, (False, 'Configuration for log beacon must'
' be a list.'))
def test_empty_config(self):
config = [{}]
ret = log.validate(config)
self.assertEqual(ret, (False, 'Configuration for log beacon '
'must contain file option.'))
def test_log_match(self):
with patch('salt.utils.files.fopen',
mock_open(read_data=_STUB_LOG_ENTRY)):
config = [{'file': '/var/log/auth.log',
'tags': {'sshd': {'regex': '.*sshd.*'}}
}]
ret = log.validate(config)
self.assertEqual(ret, (True, 'Valid beacon configuration'))
_expected_return = [{'error': '',
'match': 'yes',
'raw': _STUB_LOG_ENTRY.rstrip('\n'),
'tag': 'sshd'
}]
ret = log.beacon(config)
self.assertEqual(ret, _expected_return)