1
0

test_log.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt testing libs
  5. from tests.support.unit import TestCase
  6. from tests.support.mock import patch, mock_open
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. # Salt libs
  9. import salt.beacons.log as log
  10. import logging
  11. _log = logging.getLogger(__name__)
  12. _STUB_LOG_ENTRY = 'Jun 29 12:58:51 hostname sshd[6536]: ' \
  13. 'pam_unix(sshd:session): session opened ' \
  14. 'for user username by (uid=0)\n'
  15. class LogBeaconTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test case for salt.beacons.log
  18. '''
  19. def setup_loader_modules(self):
  20. return {
  21. log: {
  22. '__context__': {'log.loc': 2},
  23. '__salt__': {},
  24. }
  25. }
  26. def test_non_list_config(self):
  27. config = {}
  28. ret = log.validate(config)
  29. self.assertEqual(ret, (False, 'Configuration for log beacon must'
  30. ' be a list.'))
  31. def test_empty_config(self):
  32. config = [{}]
  33. ret = log.validate(config)
  34. self.assertEqual(ret, (False, 'Configuration for log beacon '
  35. 'must contain file option.'))
  36. def test_log_match(self):
  37. with patch('salt.utils.files.fopen',
  38. mock_open(read_data=_STUB_LOG_ENTRY)):
  39. config = [{'file': '/var/log/auth.log',
  40. 'tags': {'sshd': {'regex': '.*sshd.*'}}
  41. }]
  42. ret = log.validate(config)
  43. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  44. _expected_return = [{'error': '',
  45. 'match': 'yes',
  46. 'raw': _STUB_LOG_ENTRY.rstrip('\n'),
  47. 'tag': 'sshd'
  48. }]
  49. ret = log.beacon(config)
  50. self.assertEqual(ret, _expected_return)