-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_test.go
More file actions
225 lines (179 loc) · 9.93 KB
/
Copy pathcluster_test.go
File metadata and controls
225 lines (179 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package logparser
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExtractPatterns_RemoteServiceException(t *testing.T) {
logs := []string{
"Failed to get latest location by identifier: USJOT | p44.exception.RemoteServiceException: Failed to make remote service call.\nApiErrorDto(httpStatusCode=404, httpMessage=Not Found, errorMessage=null, errors=[MessageDto(severity=ERROR, message=There does not exist any locations for type PORT_UN_LOCODE and value USJOT, diagnostic=null, source=null)], supportReferenceId=9ea963cd-7ba3-411f-8a3f-b01d569574bf)",
"Failed to get latest location by identifier: USCVG | p44.exception.RemoteServiceException: Failed to make remote service call.\nApiErrorDto(httpStatusCode=404, httpMessage=Not Found, errorMessage=null, errors=[MessageDto(severity=ERROR, message=There does not exist any locations for type PORT_UN_LOCODE and value USCVG, diagnostic=null, source=null)], supportReferenceId=6dbbd508-607a-4316-86e0-35aa0ea61d4d)",
"Failed to get latest location by identifier: USSLC | p44.exception.RemoteServiceException: Failed to make remote service call.\nApiErrorDto(httpStatusCode=404, httpMessage=Not Found, errorMessage=null, errors=[MessageDto(severity=ERROR, message=There does not exist any locations for type PORT_UN_LOCODE and value USSLC, diagnostic=null, source=null)], supportReferenceId=f99855b7-171b-4e5f-bc23-d4ea7f0e2c4a)",
"Failed to get latest location by identifier: USZLV | p44.exception.RemoteServiceException: Failed to make remote service call.\nApiErrorDto(httpStatusCode=404, httpMessage=Not Found, errorMessage=null, errors=[MessageDto(severity=ERROR, message=There does not exist any locations for type PORT_UN_LOCODE and value USZLV, diagnostic=null, source=null)], supportReferenceId=c078713c-6a53-4902-8475-b002158637a7)",
"Failed to get latest location by identifier: USCEF | p44.exception.RemoteServiceException: Failed to make remote service call.\nApiErrorDto(httpStatusCode=404, httpMessage=Not Found, errorMessage=null, errors=[MessageDto(severity=ERROR, message=There does not exist any locations for type PORT_UN_LOCODE and value USCEF, diagnostic=null, source=null)], supportReferenceId=9e3a5242-abeb-444d-ab9b-9c8bbbf0f9a8)",
}
patterns := ExtractPatterns(logs, 10)
// Should find 1 pattern for all RemoteServiceException logs
assert.Equal(t, 1, len(patterns), "Should cluster all similar RemoteServiceException logs into 1 pattern")
if len(patterns) > 0 {
pattern := patterns[0]
assert.Equal(t, 5, pattern.Count, "Pattern should match all 5 logs")
assert.Equal(t, 100.0, pattern.Percentage, "Pattern should represent 100% of logs")
assert.Contains(t, pattern.Template, "Failed to get latest location by identifier", "Template should contain main error message")
assert.Contains(t, pattern.Template, "*", "Template should contain wildcards for variable parts")
assert.NotEmpty(t, pattern.Example, "Pattern should have an example log")
}
}
func TestExtractPatterns_MixedExceptions(t *testing.T) {
logs := []string{
"Failed to get latest location by identifier: USJOT | p44.exception.RemoteServiceException: Failed to make remote service call.",
"Failed to get latest location by identifier: USCVG | p44.exception.RemoteServiceException: Failed to make remote service call.",
"DetectEtaChanges failed | java.lang.NullPointerException",
"DetectEtaChanges failed | java.lang.NullPointerException",
"DetectEtaChanges failed | java.lang.NullPointerException",
"Failed to merge location: LocationDto(id=null, specifiedId=null, masterLocationId=null, version=0, tenantId=null)",
}
patterns := ExtractPatterns(logs, 10)
// Should find 3 distinct patterns
assert.GreaterOrEqual(t, len(patterns), 2, "Should find at least 2 distinct patterns")
// First pattern should be most frequent (NullPointerException - 3 occurrences)
assert.Equal(t, 3, patterns[0].Count, "First pattern should have 3 occurrences")
assert.Contains(t, patterns[0].Template, "DetectEtaChanges", "First pattern should be NullPointerException")
// Second pattern should be RemoteServiceException (2 occurrences)
assert.Equal(t, 2, patterns[1].Count, "Second pattern should have 2 occurrences")
}
func TestExtractPatterns_EmptyInput(t *testing.T) {
patterns := ExtractPatterns([]string{}, 10)
assert.Equal(t, 0, len(patterns), "Empty input should return empty patterns")
}
func TestExtractPatterns_SingleLog(t *testing.T) {
logs := []string{
"Failed to get latest location by identifier: USJOT | p44.exception.RemoteServiceException",
}
patterns := ExtractPatterns(logs, 10)
assert.Equal(t, 1, len(patterns), "Single log should return 1 pattern")
if len(patterns) > 0 {
assert.Equal(t, 1, patterns[0].Count, "Pattern count should be 1")
assert.Equal(t, 100.0, patterns[0].Percentage, "Pattern percentage should be 100%")
}
}
func TestExtractPatterns_MaxPatternsLimit(t *testing.T) {
logs := []string{
"Error type A occurred in service 1",
"Error type B occurred in service 2",
"Error type C occurred in service 3",
"Error type D occurred in service 4",
"Error type E occurred in service 5",
}
patterns := ExtractPatterns(logs, 3)
assert.LessOrEqual(t, len(patterns), 3, "Should respect maxPatterns limit")
}
func TestExtractPatterns_WithUUIDs(t *testing.T) {
logs := []string{
"Request failed with ID: 9ea963cd-7ba3-411f-8a3f-b01d569574bf",
"Request failed with ID: 6dbbd508-607a-4316-86e0-35aa0ea61d4d",
"Request failed with ID: f99855b7-171b-4e5f-bc23-d4ea7f0e2c4a",
}
patterns := ExtractPatterns(logs, 10)
// All UUIDs should be replaced with wildcards, creating 1 pattern
assert.Equal(t, 1, len(patterns), "Should cluster logs with different UUIDs into 1 pattern")
if len(patterns) > 0 {
assert.Contains(t, patterns[0].Template, "*", "Template should contain wildcard for UUID")
assert.Equal(t, 3, patterns[0].Count, "Pattern should match all 3 logs")
}
}
func TestExtractPatterns_SortedByFrequency(t *testing.T) {
logs := []string{
"Database connection failed to host-123", // 1 occurrence
"Network timeout on endpoint /api/users", "Network timeout on endpoint /api/users", // 2 occurrences
"NullPointerException in service.process()", "NullPointerException in service.process()", "NullPointerException in service.process()", // 3 occurrences
}
patterns := ExtractPatterns(logs, 10)
assert.GreaterOrEqual(t, len(patterns), 1, "Should find at least 1 pattern")
// Verify sorted by count (descending)
for i := 0; i < len(patterns)-1; i++ {
assert.GreaterOrEqual(t, patterns[i].Count, patterns[i+1].Count,
"Patterns should be sorted by count (descending)")
}
// Most frequent pattern should have 3 occurrences
if len(patterns) > 0 {
assert.Equal(t, 3, patterns[0].Count, "First pattern should have highest count")
}
}
func TestExtractPatterns_WithNumbersAndCodes(t *testing.T) {
logs := []string{
"HTTP 404 error for endpoint /api/users/12345",
"HTTP 500 error for endpoint /api/users/67890",
"HTTP 404 error for endpoint /api/users/11111",
}
patterns := ExtractPatterns(logs, 10)
// Should group by HTTP status code pattern
assert.GreaterOrEqual(t, len(patterns), 1, "Should find at least 1 pattern")
if len(patterns) > 0 {
// Numbers and user IDs should be replaced with wildcards
assert.Contains(t, patterns[0].Template, "*", "Template should contain wildcards for numbers")
}
}
// TestPatternExtractor_Streaming tests the streaming API for memory-efficient processing
func TestPatternExtractor_Streaming(t *testing.T) {
extractor, err := NewPatternExtractor()
assert.Nil(t, err, "NewPatternExtractor should not return error")
assert.NotNil(t, extractor, "Extractor should not be nil")
// Simulate streaming logs one at a time
logs := []string{
"Failed to get latest location by identifier: USJOT | p44.exception.RemoteServiceException",
"Failed to get latest location by identifier: USCVG | p44.exception.RemoteServiceException",
"Failed to get latest location by identifier: USSLC | p44.exception.RemoteServiceException",
"DetectEtaChanges failed | java.lang.NullPointerException",
"DetectEtaChanges failed | java.lang.NullPointerException",
}
for _, log := range logs {
err := extractor.AddLog(log)
assert.Nil(t, err, "AddLog should not return error")
}
assert.Equal(t, 5, extractor.TotalLogs(), "Should have processed 5 logs")
patterns := extractor.GetPatterns(10)
assert.Equal(t, 2, len(patterns), "Should find 2 patterns")
// Most frequent pattern should be NullPointerException
assert.Equal(t, 3, patterns[0].Count, "First pattern should have 3 occurrences")
assert.Contains(t, patterns[0].Template, "RemoteServiceException", "First pattern should be RemoteServiceException")
// Second pattern
assert.Equal(t, 2, patterns[1].Count, "Second pattern should have 2 occurrences")
assert.Contains(t, patterns[1].Template, "NullPointerException", "Second pattern should be NullPointerException")
}
// TestPatternExtractor_EmptyLinesSkipped tests that empty lines are skipped but counted
func TestPatternExtractor_EmptyLinesSkipped(t *testing.T) {
extractor, err := NewPatternExtractor()
assert.Nil(t, err)
logs := []string{
"Database connection failed",
"",
" ",
"NullPointerException occurred",
"\t\n",
}
for _, log := range logs {
_ = extractor.AddLog(log)
}
// Empty lines are skipped in AddLog (return early)
assert.Equal(t, 2, extractor.TotalLogs(), "Should count only non-empty logs")
patterns := extractor.GetPatterns(10)
assert.Equal(t, 2, len(patterns), "Should find 2 patterns for 2 distinct logs")
}
// TestPatternExtractor_MaxPatternsLimit tests that maxPatterns is respected
func TestPatternExtractor_MaxPatternsLimit(t *testing.T) {
extractor, err := NewPatternExtractor()
assert.Nil(t, err)
// Add 5 distinct error types
logs := []string{
"Error type A occurred",
"Error type B occurred",
"Error type C occurred",
"Error type D occurred",
"Error type E occurred",
}
for _, log := range logs {
_ = extractor.AddLog(log)
}
patterns := extractor.GetPatterns(3)
assert.LessOrEqual(t, len(patterns), 3, "Should respect maxPatterns limit")
}