test_journald.py 3.2 KB

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