@@ -6,9 +6,12 @@ import (
6
6
"testing"
7
7
8
8
"github.com/stretchr/testify/require"
9
+
10
+ "github.com/grafana/loki/v3/pkg/logql/log/pattern"
9
11
)
10
12
11
13
func TestDrain_TrainExtractsPatterns (t * testing.T ) {
14
+ t .Parallel ()
12
15
tests := []struct {
13
16
name string
14
17
drain * Drain
@@ -116,3 +119,118 @@ func TestDrain_TrainExtractsPatterns(t *testing.T) {
116
119
})
117
120
}
118
121
}
122
+
123
+ func TestDrain_TrainGeneratesMatchablePatterns (t * testing.T ) {
124
+ t .Parallel ()
125
+ tests := []struct {
126
+ name string
127
+ drain * Drain
128
+ inputLines []string
129
+ }{
130
+ {
131
+ name : "should match each line against a pattern" ,
132
+ drain : New (DefaultConfig ()),
133
+ inputLines : []string {
134
+ `test test test` ,
135
+ `test test test` ,
136
+ `test test test` ,
137
+ `test test test` ,
138
+ },
139
+ },
140
+ {
141
+ name : "should also match newlines" ,
142
+ drain : New (DefaultConfig ()),
143
+ inputLines : []string {
144
+ "test test test\n " ,
145
+ "test test test\n " ,
146
+ "test test test\n " ,
147
+ "test test test\n " ,
148
+ },
149
+ },
150
+ }
151
+ for _ , tt := range tests {
152
+ tt := tt
153
+ t .Run (tt .name , func (t * testing.T ) {
154
+ for _ , line := range tt .inputLines {
155
+ tt .drain .Train (line , 0 )
156
+ }
157
+ t .Log ("Learned clusters" , tt .drain .Clusters ())
158
+
159
+ for _ , line := range tt .inputLines {
160
+ match := tt .drain .Match (line )
161
+ require .NotNil (t , match , "Line should match a cluster" )
162
+ }
163
+ })
164
+ }
165
+
166
+ }
167
+
168
+ func TestDrain_TrainGeneratesPatternsMatchableByLokiPatternFilter (t * testing.T ) {
169
+ t .Parallel ()
170
+ tests := []struct {
171
+ name string
172
+ drain * Drain
173
+ inputLines []string
174
+ }{
175
+ {
176
+ name : "should extract patterns that all lines match" ,
177
+ drain : New (DefaultConfig ()),
178
+ inputLines : []string {
179
+ `test 1 test` ,
180
+ `test 2 test` ,
181
+ `test 3 test` ,
182
+ `test 4 test` ,
183
+ },
184
+ },
185
+ {
186
+ name : "should extract patterns that match if line ends with newlines" ,
187
+ drain : New (DefaultConfig ()),
188
+ inputLines : []string {
189
+ "test 1 test\n " ,
190
+ "test 2 test\n " ,
191
+ "test 3 test\n " ,
192
+ "test 4 test\n " ,
193
+ },
194
+ },
195
+ {
196
+ name : "should extract patterns that match if line ends with empty space" ,
197
+ drain : New (DefaultConfig ()),
198
+ inputLines : []string {
199
+ "test 1 test " ,
200
+ "test 2 test " ,
201
+ "test 3 test " ,
202
+ "test 4 test " ,
203
+ },
204
+ },
205
+ {
206
+ name : "should extract patterns that match if line starts with empty space" ,
207
+ drain : New (DefaultConfig ()),
208
+ inputLines : []string {
209
+ " test 1 test" ,
210
+ " test 2 test" ,
211
+ " test 3 test" ,
212
+ " test 4 test" ,
213
+ },
214
+ },
215
+ }
216
+ for _ , tt := range tests {
217
+ tt := tt
218
+ t .Run (tt .name , func (t * testing.T ) {
219
+ for _ , line := range tt .inputLines {
220
+ tt .drain .Train (line , 0 )
221
+ }
222
+ require .Equal (t , 1 , len (tt .drain .Clusters ()))
223
+ cluster := tt .drain .Clusters ()[0 ]
224
+ t .Log ("Extracted cluster: " , cluster )
225
+
226
+ matcher , err := pattern .ParseLineFilter ([]byte (cluster .String ()))
227
+ require .NoError (t , err )
228
+
229
+ for _ , line := range tt .inputLines {
230
+ passes := matcher .Test ([]byte (line ))
231
+ require .Truef (t , passes , "Line %q should match extracted pattern" , line )
232
+ }
233
+ })
234
+ }
235
+
236
+ }
0 commit comments