1
0

test_log.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt testing libs
  5. from tests.support.unit import skipIf, TestCase
  6. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, 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. @skipIf(NO_MOCK, NO_MOCK_REASON)
  16. class LogBeaconTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test case for salt.beacons.log
  19. '''
  20. def setup_loader_modules(self):
  21. return {
  22. log: {
  23. '__context__': {'log.loc': 2},
  24. '__salt__': {},
  25. }
  26. }
  27. def test_non_list_config(self):
  28. config = {}
  29. ret = log.validate(config)
  30. self.assertEqual(ret, (False, 'Configuration for log beacon must'
  31. ' be a list.'))
  32. def test_empty_config(self):
  33. config = [{}]
  34. ret = log.validate(config)
  35. self.assertEqual(ret, (False, 'Configuration for log beacon '
  36. 'must contain file option.'))
  37. def test_log_match(self):
  38. with patch('salt.utils.files.fopen',
  39. mock_open(read_data=_STUB_LOG_ENTRY)):
  40. config = [{'file': '/var/log/auth.log',
  41. 'tags': {'sshd': {'regex': '.*sshd.*'}}
  42. }]
  43. ret = log.validate(config)
  44. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  45. _expected_return = [{'error': '',
  46. 'match': 'yes',
  47. 'raw': _STUB_LOG_ENTRY.rstrip('\n'),
  48. 'tag': 'sshd'
  49. }]
  50. ret = log.beacon(config)
  51. self.assertEqual(ret, _expected_return)