test_journald.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. import datetime
  5. from uuid import UUID
  6. # Salt testing libs
  7. from tests.support.unit import TestCase
  8. from tests.support.mock import Mock
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. # Salt libs
  11. import salt.beacons.journald as journald
  12. import salt.utils.data
  13. import logging
  14. log = logging.getLogger(__name__)
  15. _STUB_JOURNALD_ENTRY = {'_BOOT_ID': UUID('ad3915a5-9008-4fec-a635-140606525497'),
  16. '__MONOTONIC_TIMESTAMP': (datetime.timedelta(4, 28586, 703069),
  17. UUID('ad3915a5-9008-4fec-a635-140606525497')),
  18. '_AUDIT_LOGINUID': 1000, 'SYSLOG_FACILITY': 10,
  19. '_SYSTEMD_SLICE': u'system.slice',
  20. '_GID': 0,
  21. '__REALTIME_TIMESTAMP': datetime.datetime(2017, 6, 27,
  22. 20, 8, 16,
  23. 468468),
  24. '_AUDIT_SESSION': 351, 'PRIORITY': 6,
  25. '_TRANSPORT': u'syslog',
  26. '_HOSTNAME': u'hostname',
  27. '_CAP_EFFECTIVE': u'3fffffffff',
  28. '_SYSTEMD_UNIT': u'ssh.service',
  29. '_MACHINE_ID': UUID('14fab5bb-228d-414b-bdf4-cbc62cb7ba54'),
  30. '_PID': 15091,
  31. 'SYSLOG_IDENTIFIER': u'sshd',
  32. '_SOURCE_REALTIME_TIMESTAMP': datetime.datetime(2017,
  33. 6,
  34. 27,
  35. 20,
  36. 8,
  37. 16,
  38. 468454),
  39. '_SYSTEMD_CGROUP': u'/system.slice/ssh.service',
  40. '__CURSOR': 's=7711ee01b03446309383870171dd5839;i=a74e;b=ad3915a590084feca635140606525497;m=571f43f8 dd;t=552fc7ed1cdf4;x=4ca0a3d4f1905736',
  41. '_COMM': u'sshd',
  42. '_CMDLINE': u'sshd: gareth [priv]',
  43. '_SYSTEMD_INVOCATION_ID': u'38a5d5aad292426d93bfaab72a69c2ab',
  44. '_EXE': u'/usr/sbin/sshd',
  45. '_UID': 0,
  46. 'SYSLOG_PID': 15091,
  47. 'MESSAGE': u'pam_unix(sshd:session): session opened for user username by (uid=0)'}
  48. class SystemdJournaldMock(Mock):
  49. ''' Request Mock'''
  50. returned_once = False
  51. def get_next(self, *args, **kwargs):
  52. if not self.returned_once:
  53. self.returned_once = True
  54. return _STUB_JOURNALD_ENTRY
  55. else:
  56. return None
  57. def seek_tail(self, *args, **kwargs):
  58. return {}
  59. def get_previous(self, *args, **kwargs):
  60. return {}
  61. SYSTEMD_MOCK = SystemdJournaldMock()
  62. class JournaldBeaconTestCase(TestCase, LoaderModuleMockMixin):
  63. '''
  64. Test case for salt.beacons.journald
  65. '''
  66. def setup_loader_modules(self):
  67. return {
  68. journald: {
  69. '__context__': {
  70. 'systemd.journald': SYSTEMD_MOCK,
  71. },
  72. '__salt__': {},
  73. }
  74. }
  75. def test_non_list_config(self):
  76. config = {}
  77. ret = journald.validate(config)
  78. self.assertEqual(ret, (False, 'Configuration for journald beacon must'
  79. ' be a list.'))
  80. def test_empty_config(self):
  81. config = [{}]
  82. ret = journald.validate(config)
  83. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  84. def test_journald_match(self):
  85. config = [{'services': {'sshd': {'SYSLOG_IDENTIFIER': 'sshd',
  86. 'PRIORITY': 6}}}]
  87. ret = journald.validate(config)
  88. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  89. _expected_return = salt.utils.data.simple_types_filter(_STUB_JOURNALD_ENTRY)
  90. _expected_return['tag'] = 'sshd'
  91. ret = journald.beacon(config)
  92. self.assertEqual(ret, [_expected_return])