search_journalctl_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import pytest
  2. import search_journalctl
  3. def canned_search_journalctl(get_log_output=None):
  4. """Create a search_journalctl object with canned get_log_output method"""
  5. module = search_journalctl
  6. if get_log_output:
  7. module.get_log_output = get_log_output
  8. return module
  9. DEFAULT_TIMESTAMP = 1496341364
  10. def get_timestamp(modifier=0):
  11. return DEFAULT_TIMESTAMP + modifier
  12. def get_timestamp_microseconds(modifier=0):
  13. return get_timestamp(modifier) * 1000000
  14. def create_test_log_object(stamp, msg):
  15. return '{{"__REALTIME_TIMESTAMP": "{}", "MESSAGE": "{}"}}'.format(stamp, msg)
  16. @pytest.mark.parametrize('name,matchers,log_input,expected_matches,expected_errors', [
  17. (
  18. 'test with valid params',
  19. [
  20. {
  21. "start_regexp": r"Sample Logs Beginning",
  22. "regexp": r"test log message",
  23. "unit": "test",
  24. },
  25. ],
  26. [
  27. create_test_log_object(get_timestamp_microseconds(), "test log message"),
  28. create_test_log_object(get_timestamp_microseconds(), "Sample Logs Beginning"),
  29. ],
  30. ["test log message"],
  31. [],
  32. ),
  33. (
  34. 'test with invalid json in log input',
  35. [
  36. {
  37. "start_regexp": r"Sample Logs Beginning",
  38. "regexp": r"test log message",
  39. "unit": "test-unit",
  40. },
  41. ],
  42. [
  43. '{__REALTIME_TIMESTAMP: ' + str(get_timestamp_microseconds()) + ', "MESSAGE": "test log message"}',
  44. ],
  45. [],
  46. [
  47. ["invalid json", "test-unit", "test log message"],
  48. ],
  49. ),
  50. (
  51. 'test with invalid regexp',
  52. [
  53. {
  54. "start_regexp": r"Sample Logs Beginning",
  55. "regexp": r"test [ log message",
  56. "unit": "test",
  57. },
  58. ],
  59. [
  60. create_test_log_object(get_timestamp_microseconds(), "test log message"),
  61. create_test_log_object(get_timestamp_microseconds(), "sample log message"),
  62. create_test_log_object(get_timestamp_microseconds(), "fake log message"),
  63. create_test_log_object(get_timestamp_microseconds(), "dummy log message"),
  64. create_test_log_object(get_timestamp_microseconds(), "Sample Logs Beginning"),
  65. ],
  66. [],
  67. [
  68. ["invalid regular expression"],
  69. ],
  70. ),
  71. ], ids=lambda argval: argval[0])
  72. def test_get_log_matches(name, matchers, log_input, expected_matches, expected_errors):
  73. def get_log_output(matcher):
  74. return log_input
  75. module = canned_search_journalctl(get_log_output)
  76. matched_regexp, errors = module.get_log_matches(matchers, 500, 60 * 60)
  77. assert set(matched_regexp) == set(expected_matches)
  78. assert len(expected_errors) == len(errors)
  79. for idx, partial_err_set in enumerate(expected_errors):
  80. for partial_err_msg in partial_err_set:
  81. assert partial_err_msg in errors[idx]
  82. @pytest.mark.parametrize('name,matcher,log_count_lim,stamp_lim_seconds,log_input,expected_match', [
  83. (
  84. 'test with matching log message, but out of bounds of log_count_lim',
  85. {
  86. "start_regexp": r"Sample Logs Beginning",
  87. "regexp": r"dummy log message",
  88. "unit": "test",
  89. },
  90. 3,
  91. get_timestamp(-100 * 60 * 60),
  92. [
  93. create_test_log_object(get_timestamp_microseconds(), "test log message"),
  94. create_test_log_object(get_timestamp_microseconds(), "sample log message"),
  95. create_test_log_object(get_timestamp_microseconds(), "fake log message"),
  96. create_test_log_object(get_timestamp_microseconds(), "dummy log message"),
  97. create_test_log_object(get_timestamp_microseconds(), "Sample Logs Beginning"),
  98. ],
  99. None,
  100. ),
  101. (
  102. 'test with matching log message, but with timestamp too old',
  103. {
  104. "start_regexp": r"Sample Logs Beginning",
  105. "regexp": r"dummy log message",
  106. "unit": "test",
  107. },
  108. 100,
  109. get_timestamp(-10),
  110. [
  111. create_test_log_object(get_timestamp_microseconds(), "test log message"),
  112. create_test_log_object(get_timestamp_microseconds(), "sample log message"),
  113. create_test_log_object(get_timestamp_microseconds(), "fake log message"),
  114. create_test_log_object(get_timestamp_microseconds(-1000), "dummy log message"),
  115. create_test_log_object(get_timestamp_microseconds(-1000), "Sample Logs Beginning"),
  116. ],
  117. None,
  118. ),
  119. (
  120. 'test with matching log message, and timestamp within time limit',
  121. {
  122. "start_regexp": r"Sample Logs Beginning",
  123. "regexp": r"dummy log message",
  124. "unit": "test",
  125. },
  126. 100,
  127. get_timestamp(-1010),
  128. [
  129. create_test_log_object(get_timestamp_microseconds(), "test log message"),
  130. create_test_log_object(get_timestamp_microseconds(), "sample log message"),
  131. create_test_log_object(get_timestamp_microseconds(), "fake log message"),
  132. create_test_log_object(get_timestamp_microseconds(-1000), "dummy log message"),
  133. create_test_log_object(get_timestamp_microseconds(-1000), "Sample Logs Beginning"),
  134. ],
  135. create_test_log_object(get_timestamp_microseconds(-1000), "dummy log message"),
  136. ),
  137. ], ids=lambda argval: argval[0])
  138. def test_find_matches_skips_logs(name, matcher, log_count_lim, stamp_lim_seconds, log_input, expected_match):
  139. match = search_journalctl.find_matches(log_input, matcher, log_count_lim, stamp_lim_seconds)
  140. assert match == expected_match