test_parsers_log.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. """
  2. Tests for log line parsers (parsers/log.py).
  3. """
  4. from datetime import datetime
  5. from database.database import get_dynamic_models
  6. from database.models.models import DeniedLog
  7. from parsers.log import (
  8. FORMAT_AUTO,
  9. FORMAT_DEFAULT,
  10. FORMAT_DETAILED,
  11. detect_log_format,
  12. get_log_datetime,
  13. import_logs,
  14. parse_log_line,
  15. parse_log_line_default,
  16. parse_log_line_pipe_format,
  17. parse_log_line_space_format,
  18. )
  19. class TestParseLogLineDetailed:
  20. """Tests for DETAILED (default) log parsing mode."""
  21. def test_classic_squid_format(self):
  22. line = (
  23. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  24. "GET http://example.com/page user1 HIER_DIRECT/93.184.216.34 text/html"
  25. )
  26. result = parse_log_line(line)
  27. assert result is not None
  28. assert result["ip"] == "192.168.1.100"
  29. assert result["url"] == "http://example.com/page"
  30. assert result["response"] == 200
  31. assert result["data_transmitted"] == 1234
  32. assert result["method"] == "GET"
  33. assert result["is_denied"] is False
  34. def test_denied_entry(self):
  35. line = (
  36. "1609459200.000 1 10.0.0.5 TCP_DENIED/403 500 "
  37. "GET http://blocked.com/ user2 HIER_NONE/- text/html"
  38. )
  39. result = parse_log_line(line)
  40. assert result is not None
  41. assert result["is_denied"] is True
  42. assert result["response"] == 403
  43. def test_cache_object_ignored(self):
  44. line = "1609459200.000 cache_object://localhost/info"
  45. result = parse_log_line(line)
  46. assert result is None
  47. def test_error_transaction_ignored(self):
  48. line = (
  49. "1609459200.000 0 10.0.0.1 TAG_NONE/0 0 "
  50. "NONE error:transaction-end-before-headers HIER_NONE/- -"
  51. )
  52. result = parse_log_line(line)
  53. assert result is None
  54. def test_error_invalid_request_ignored(self):
  55. line = "1609459200.000 error:invalid-request something"
  56. result = parse_log_line(line)
  57. assert result is None
  58. def test_empty_line_returns_none(self):
  59. assert parse_log_line("") is None
  60. def test_malformed_line_returns_none(self):
  61. assert parse_log_line("this is not a log line") is None
  62. class TestPipeFormat:
  63. def test_valid_pipe_line(self):
  64. line = "1609459200|192.168.1.1|3128|user1|TCP_MISS|GET|http://example.com|host|200|1024|text/html|0|0|TCP_MISS/200"
  65. result = parse_log_line_pipe_format(line)
  66. assert result is not None
  67. assert result["username"] == "user1"
  68. assert result["ip"] == "192.168.1.1"
  69. assert result["url"] == "http://example.com"
  70. assert result["response"] == 200
  71. assert result["data_transmitted"] == 1024
  72. def test_anonymous_user_returns_none(self):
  73. line = "1609459200|192.168.1.1|3128|-|TCP_MISS|GET|http://example.com|host|200|1024|text/html|0|0|TCP_MISS/200"
  74. result = parse_log_line_pipe_format(line)
  75. assert result is None
  76. def test_short_pipe_line_returns_none(self):
  77. line = "a|b|c"
  78. result = parse_log_line_pipe_format(line)
  79. assert result is None
  80. class TestSpaceFormat:
  81. def test_valid_space_line(self):
  82. line = "1609459200.000 192.168.1.1 3128 user1 TCP_MISS GET http://example.com host 0 200 1024"
  83. result = parse_log_line_space_format(line)
  84. assert result is not None
  85. assert result["username"] == "user1"
  86. assert result["ip"] == "192.168.1.1"
  87. def test_anonymous_returns_none(self):
  88. line = "1609459200.000 192.168.1.1 3128 - TCP_MISS GET http://example.com host 0 200 1024"
  89. result = parse_log_line_space_format(line)
  90. assert result is None
  91. def test_short_line_returns_none(self):
  92. line = "1609459200.000 192.168.1.1"
  93. result = parse_log_line_space_format(line)
  94. assert result is None
  95. class TestDefaultFormat:
  96. def test_valid_default_line(self):
  97. line = (
  98. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  99. "GET http://example.com/page user1 HIER_DIRECT/93.184.216.34 text/html"
  100. )
  101. result = parse_log_line_default(line)
  102. assert result is not None
  103. assert result["url"] == "http://example.com/page"
  104. assert result["response"] == 200
  105. assert result["data_transmitted"] == 1234
  106. def test_short_line_returns_none(self):
  107. assert parse_log_line_default("a b c") is None
  108. def test_cache_object_returns_none(self):
  109. line = (
  110. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  111. "GET cache_object://localhost/info user1 HIER_DIRECT/93.184.216.34 text/html"
  112. )
  113. result = parse_log_line_default(line)
  114. assert result is None
  115. class TestAutomaticFormatDetection:
  116. def test_detects_default_file_format(self, tmp_path):
  117. log_file = tmp_path / "access.log"
  118. log_file.write_text(
  119. "1609459200.000 1 192.168.1.100 TCP_MISS/200 1234 "
  120. "GET http://example.com/page user1 HIER_DIRECT/- text/html\n",
  121. encoding="utf-8",
  122. )
  123. assert detect_log_format(log_file) == FORMAT_DEFAULT
  124. def test_detects_detailed_file_format(self, tmp_path):
  125. log_file = tmp_path / "access.log"
  126. log_file.write_text(
  127. "1609459200.000 192.168.1.100 - user1 "
  128. "[20/Aug/2026:14:00:00 -0400] "
  129. '"GET http://example.com/page HTTP/1.1" 200 1234 '
  130. "GET http://example.com/page 192.168.1.100 text/html "
  131. "- TCP_MISS/200 1234\n",
  132. encoding="utf-8",
  133. )
  134. assert detect_log_format(log_file) == FORMAT_DETAILED
  135. result = parse_log_line(
  136. log_file.read_text(encoding="utf-8").strip(),
  137. format_hint=FORMAT_DETAILED,
  138. )
  139. assert result == {
  140. "ip": "192.168.1.100",
  141. "username": "user1",
  142. "url": "http://example.com/page",
  143. "response": 200,
  144. "data_transmitted": 1234,
  145. "method": "GET",
  146. "status": "200",
  147. "is_denied": False,
  148. }
  149. def test_unknown_file_falls_back_to_per_line_detection(self, tmp_path):
  150. log_file = tmp_path / "access.log"
  151. log_file.write_text("not a Squid access log\n", encoding="utf-8")
  152. assert detect_log_format(log_file) == FORMAT_AUTO
  153. class TestLogImport:
  154. def test_log_datetime_comes_from_first_field(self):
  155. line = (
  156. "1790000000 1 192.168.1.100 TCP_MISS/200 1234 GET http://example.com user"
  157. )
  158. assert get_log_datetime(line) is not None
  159. assert get_log_datetime("not a timestamp") is None
  160. def test_import_routes_entries_to_their_daily_tables(self, tmp_path, patched_db):
  161. first_datetime = datetime(2026, 8, 16, 12, 0, 0)
  162. second_datetime = datetime(2026, 8, 17, 12, 0, 0)
  163. first_timestamp = first_datetime.timestamp()
  164. second_timestamp = second_datetime.timestamp()
  165. log_file = tmp_path / "access.log"
  166. log_file.write_text(
  167. f"{first_timestamp} 1 192.168.1.100 TCP_MISS/200 1234 "
  168. "GET http://first.example user1 HIER_DIRECT/- text/html\n"
  169. f"{second_timestamp} 1 192.168.1.101 TCP_MISS/204 42 "
  170. "GET http://second.example user2 HIER_DIRECT/- text/html\n",
  171. encoding="utf-8",
  172. )
  173. summary = import_logs(str(log_file))
  174. assert summary["processed_lines"] == 2
  175. assert summary["parsed_lines"] == 2
  176. assert summary["inserted_logs"] == 2
  177. assert summary["inserted_users"] == 2
  178. assert [item["date"] for item in summary["dates"]] == [
  179. "20260816",
  180. "20260817",
  181. ]
  182. for suffix, expected_url in (
  183. ("20260816", "http://first.example"),
  184. ("20260817", "http://second.example"),
  185. ):
  186. UserModel, LogModel = get_dynamic_models(suffix)
  187. user = patched_db.query(UserModel).one()
  188. log = patched_db.query(LogModel).one()
  189. assert user.id == log.user_id
  190. assert log.url == expected_url
  191. assert log.created_at.date().strftime("%Y%m%d") == suffix
  192. assert patched_db.query(DeniedLog).count() == 0