Skip to content

Commit 19cc0d4

Browse files
committed
Generate some tests for log_dedup with Chatgpt(prompting with actual logs)
1 parent db3d958 commit 19cc0d4

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

tests/orchestrator/log_dedup.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# test_log_deduplication.py
2+
import os
3+
import unittest
4+
from aiopslab.orchestrator.actions import log_deduplication as ld
5+
6+
7+
class TestTimestampDetection(unittest.TestCase):
8+
def test_find_timestamp_spans_iso(self):
9+
line = "2025-09-24 18:41:09 some log message"
10+
spans = ld.find_timestamp_spans(line, ld.DEFAULT_TS_RX)
11+
self.assertEqual(len(spans), 1)
12+
start, end = spans[0]
13+
self.assertEqual(line[start:end], "2025-09-24 18:41:09")
14+
15+
def test_find_timestamp_spans_multiple(self):
16+
line = "Start 2025-09-24T18:41:09Z end 2025-09-24T19:00:00Z"
17+
spans = ld.find_timestamp_spans(line, ld.DEFAULT_TS_RX)
18+
self.assertEqual(len(spans), 2)
19+
20+
def test_find_timestamp_spans_no_match(self):
21+
line = "no timestamp here"
22+
spans = ld.find_timestamp_spans(line, ld.DEFAULT_TS_RX)
23+
self.assertEqual(spans, [])
24+
25+
26+
class TestMakeBlocks(unittest.TestCase):
27+
def test_make_blocks_basic(self):
28+
lines = ["a", "b", "c", "d", "e"]
29+
blocks = ld.make_blocks(lines, 2)
30+
self.assertEqual(blocks, ["a\nb", "c\nd", "e"])
31+
32+
def test_make_blocks_invalid(self):
33+
with self.assertRaises(ValueError):
34+
ld.make_blocks(["x"], 0)
35+
36+
37+
class TestGreedyCompressPass(unittest.TestCase):
38+
def setUp(self):
39+
self.ts_rx = ld.DEFAULT_TS_RX
40+
41+
def test_no_lines(self):
42+
result = ld.greedy_compress_pass([], self.ts_rx, 1)
43+
self.assertEqual(result, [])
44+
45+
def test_dedup_identical_except_timestamps(self):
46+
lines = [
47+
'{"time":"2025-09-25T04:36:33Z","msg":"foo"}',
48+
'{"time":"2025-09-25T04:36:34Z","msg":"foo"}',
49+
]
50+
result = ld.greedy_compress_pass(lines, self.ts_rx, 1)
51+
self.assertEqual(len(result), 1)
52+
self.assertIn('"foo"', result[0])
53+
54+
def test_different_messages_not_deduped(self):
55+
lines = [
56+
'{"time":"2025-09-25T04:36:33Z","msg":"foo"}',
57+
'{"time":"2025-09-25T04:36:33Z","msg":"bar"}',
58+
]
59+
result = ld.greedy_compress_pass(lines, self.ts_rx, 1)
60+
self.assertEqual(len(result), 2)
61+
62+
63+
class TestGreedyCompressLines(unittest.TestCase):
64+
def setUp(self):
65+
self.ts_rx = ld.DEFAULT_TS_RX
66+
67+
def test_env_disabled(self):
68+
"""Should return input unchanged when LOG_TRIM is unset or invalid."""
69+
os.environ.pop("LOG_TRIM", None)
70+
text = "some logs\nmore logs"
71+
result = ld.greedy_compress_lines(text)
72+
self.assertEqual(result, text)
73+
74+
def test_env_enabled(self):
75+
"""Should dedup when LOG_TRIM is set."""
76+
os.environ["LOG_TRIM"] = "2"
77+
text = (
78+
'{"time":"2025-09-25T04:36:33Z","message":"TLS disabled."}\n'
79+
'{"time":"2025-09-25T04:36:34Z","message":"TLS disabled."}\n'
80+
)
81+
result = ld.greedy_compress_lines(text, self.ts_rx)
82+
# Expect only one entry remains after deduplication
83+
self.assertIn("TLS disabled", result)
84+
self.assertEqual(result.count("TLS disabled"), 1)
85+
86+
def test_invalid_env(self):
87+
"""Invalid LOG_TRIM should skip dedup."""
88+
os.environ["LOG_TRIM"] = "invalid"
89+
text = "hello world"
90+
result = ld.greedy_compress_lines(text)
91+
self.assertEqual(result, text)
92+
93+
94+
class TestRealisticLogs(unittest.TestCase):
95+
"""Integration-style tests using the provided log sequences."""
96+
97+
def setUp(self):
98+
self.ts_rx = ld.DEFAULT_TS_RX
99+
os.environ["LOG_TRIM"] = "3"
100+
101+
def test_dedup_kubernetes_events(self):
102+
sample = """
103+
Error: Your service/namespace does not exist. Use kubectl to check.
104+
{"level":"info","time":"2025-09-25T04:36:33Z","message":"TLS disabled."}
105+
{"level":"info","time":"2025-09-25T04:36:33Z","message":"TLS disabled."}
106+
{"level":"info","time":"2025-09-25T04:36:34Z","message":"TLS disabled."}
107+
"""
108+
result = ld.greedy_compress_lines(sample.strip(), self.ts_rx)
109+
# Should remove duplicate lines differing only by timestamps
110+
self.assertIn("TLS disabled", result)
111+
self.assertEqual(result.count("TLS disabled"), 1)
112+
113+
def test_k8s_container_events(self):
114+
sample = """
115+
2025-09-25T04:36:33Z INF cmd/geo/db.go:29 > New session successfull...
116+
2025-09-25T04:36:34Z INF cmd/geo/db.go:29 > New session successfull...
117+
2025-09-25T04:36:35Z INF cmd/geo/db.go:31 > Generating test data...
118+
"""
119+
result = ld.greedy_compress_lines(sample.strip(), self.ts_rx)
120+
# Should collapse identical log messages differing only by timestamp
121+
self.assertIn("New session successfull...", result)
122+
self.assertEqual(result.count("New session successfull..."), 1)
123+
# Should preserve distinct lines
124+
self.assertIn("Generating test data...", result)
125+
126+
def test_multiblock_dedup(self):
127+
sample = "\n".join(
128+
[
129+
f'{{"level":"info","time":"2025-09-25T04:37:{i:02d}Z","message":"Tune: setGCPercent to 100"}}'
130+
for i in range(3)
131+
]
132+
)
133+
result = ld.greedy_compress_lines(sample, self.ts_rx)
134+
self.assertIn("Tune: setGCPercent to 100", result)
135+
self.assertEqual(result.count("Tune: setGCPercent to 100"), 1)
136+
137+
138+
if __name__ == "__main__":
139+
unittest.main()
140+

0 commit comments

Comments
 (0)