1
0

test_journald.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 skipIf, TestCase
  8. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, 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. @skipIf(NO_MOCK, NO_MOCK_REASON)
  63. class JournaldBeaconTestCase(TestCase, LoaderModuleMockMixin):
  64. '''
  65. Test case for salt.beacons.journald
  66. '''
  67. def setup_loader_modules(self):
  68. return {
  69. journald: {
  70. '__context__': {
  71. 'systemd.journald': SYSTEMD_MOCK,
  72. },
  73. '__salt__': {},
  74. }
  75. }
  76. def test_non_list_config(self):
  77. config = {}
  78. ret = journald.validate(config)
  79. self.assertEqual(ret, (False, 'Configuration for journald beacon must'
  80. ' be a list.'))
  81. def test_empty_config(self):
  82. config = [{}]
  83. ret = journald.validate(config)
  84. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  85. def test_journald_match(self):
  86. config = [{'services': {'sshd': {'SYSLOG_IDENTIFIER': 'sshd',
  87. 'PRIORITY': 6}}}]
  88. ret = journald.validate(config)
  89. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  90. _expected_return = salt.utils.data.simple_types_filter(_STUB_JOURNALD_ENTRY)
  91. _expected_return['tag'] = 'sshd'
  92. ret = journald.beacon(config)
  93. self.assertEqual(ret, [_expected_return])