test_parsers_log.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. """
  2. Tests for log line parsers (parsers/log.py).
  3. """
  4. from parsers.log import (
  5. FORMAT_AUTO,
  6. FORMAT_DEFAULT,
  7. FORMAT_DETAILED,
  8. detect_log_format,
  9. parse_log_line,
  10. parse_log_line_default,
  11. parse_log_line_pipe_format,
  12. parse_log_line_space_format,
  13. )
  14. class TestParseLogLineDetailed:
  15. """Tests for DETAILED (default) log parsing mode."""
  16. def test_classic_squid_format(self):
  17. line = (
  18. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  19. "GET http://example.com/page user1 HIER_DIRECT/93.184.216.34 text/html"
  20. )
  21. result = parse_log_line(line)
  22. assert result is not None
  23. assert result["ip"] == "192.168.1.100"
  24. assert result["url"] == "http://example.com/page"
  25. assert result["response"] == 200
  26. assert result["data_transmitted"] == 1234
  27. assert result["method"] == "GET"
  28. assert result["is_denied"] is False
  29. def test_denied_entry(self):
  30. line = (
  31. "1609459200.000 1 10.0.0.5 TCP_DENIED/403 500 "
  32. "GET http://blocked.com/ user2 HIER_NONE/- text/html"
  33. )
  34. result = parse_log_line(line)
  35. assert result is not None
  36. assert result["is_denied"] is True
  37. assert result["response"] == 403
  38. def test_cache_object_ignored(self):
  39. line = "1609459200.000 cache_object://localhost/info"
  40. result = parse_log_line(line)
  41. assert result is None
  42. def test_error_transaction_ignored(self):
  43. line = (
  44. "1609459200.000 0 10.0.0.1 TAG_NONE/0 0 "
  45. "NONE error:transaction-end-before-headers HIER_NONE/- -"
  46. )
  47. result = parse_log_line(line)
  48. assert result is None
  49. def test_error_invalid_request_ignored(self):
  50. line = "1609459200.000 error:invalid-request something"
  51. result = parse_log_line(line)
  52. assert result is None
  53. def test_empty_line_returns_none(self):
  54. assert parse_log_line("") is None
  55. def test_malformed_line_returns_none(self):
  56. assert parse_log_line("this is not a log line") is None
  57. class TestPipeFormat:
  58. def test_valid_pipe_line(self):
  59. line = "1609459200|192.168.1.1|3128|user1|TCP_MISS|GET|http://example.com|host|200|1024|text/html|0|0|TCP_MISS/200"
  60. result = parse_log_line_pipe_format(line)
  61. assert result is not None
  62. assert result["username"] == "user1"
  63. assert result["ip"] == "192.168.1.1"
  64. assert result["url"] == "http://example.com"
  65. assert result["response"] == 200
  66. assert result["data_transmitted"] == 1024
  67. def test_anonymous_user_returns_none(self):
  68. line = "1609459200|192.168.1.1|3128|-|TCP_MISS|GET|http://example.com|host|200|1024|text/html|0|0|TCP_MISS/200"
  69. result = parse_log_line_pipe_format(line)
  70. assert result is None
  71. def test_short_pipe_line_returns_none(self):
  72. line = "a|b|c"
  73. result = parse_log_line_pipe_format(line)
  74. assert result is None
  75. class TestSpaceFormat:
  76. def test_valid_space_line(self):
  77. line = "1609459200.000 192.168.1.1 3128 user1 TCP_MISS GET http://example.com host 0 200 1024"
  78. result = parse_log_line_space_format(line)
  79. assert result is not None
  80. assert result["username"] == "user1"
  81. assert result["ip"] == "192.168.1.1"
  82. def test_anonymous_returns_none(self):
  83. line = "1609459200.000 192.168.1.1 3128 - TCP_MISS GET http://example.com host 0 200 1024"
  84. result = parse_log_line_space_format(line)
  85. assert result is None
  86. def test_short_line_returns_none(self):
  87. line = "1609459200.000 192.168.1.1"
  88. result = parse_log_line_space_format(line)
  89. assert result is None
  90. class TestDefaultFormat:
  91. def test_valid_default_line(self):
  92. line = (
  93. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  94. "GET http://example.com/page user1 HIER_DIRECT/93.184.216.34 text/html"
  95. )
  96. result = parse_log_line_default(line)
  97. assert result is not None
  98. assert result["url"] == "http://example.com/page"
  99. assert result["response"] == 200
  100. assert result["data_transmitted"] == 1234
  101. def test_short_line_returns_none(self):
  102. assert parse_log_line_default("a b c") is None
  103. def test_cache_object_returns_none(self):
  104. line = (
  105. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  106. "GET cache_object://localhost/info user1 HIER_DIRECT/93.184.216.34 text/html"
  107. )
  108. result = parse_log_line_default(line)
  109. assert result is None
  110. class TestAutomaticFormatDetection:
  111. def test_detects_default_file_format(self, tmp_path):
  112. log_file = tmp_path / "access.log"
  113. log_file.write_text(
  114. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  115. "GET http://example.com/page user1 HIER_DIRECT/- text/html\n",
  116. encoding="utf-8",
  117. )
  118. assert detect_log_format(log_file) == FORMAT_DEFAULT
  119. def test_detects_detailed_file_format(self, tmp_path):
  120. log_file = tmp_path / "access.log"
  121. log_file.write_text(
  122. "1609459200.000 192.168.1.100 - user1 "
  123. "[20/Aug/2026:14:00:00 -0400] "
  124. '"GET http://example.com/page HTTP/1.1" 200 1234 '
  125. "GET http://example.com/page 192.168.1.100 text/html "
  126. "- TCP_MISS/200 1234\n",
  127. encoding="utf-8",
  128. )
  129. assert detect_log_format(log_file) == FORMAT_DETAILED
  130. result = parse_log_line(
  131. log_file.read_text(encoding="utf-8").strip(),
  132. format_hint=FORMAT_DETAILED,
  133. )
  134. assert result == {
  135. "ip": "192.168.1.100",
  136. "username": "user1",
  137. "url": "http://example.com/page",
  138. "response": 200,
  139. "data_transmitted": 1234,
  140. "method": "GET",
  141. "status": "200",
  142. "is_denied": False,
  143. }
  144. def test_unknown_file_falls_back_to_per_line_detection(self, tmp_path):
  145. log_file = tmp_path / "access.log"
  146. log_file.write_text("not a Squid access log\n", encoding="utf-8")
  147. assert detect_log_format(log_file) == FORMAT_AUTO