1
0

test_log_beacon.py 1.8 KB

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