forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sort_test.go
257 lines (237 loc) · 7.45 KB
/
sort_test.go
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package filter
import (
"fmt"
"os"
"path/filepath"
"regexp"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewSortOptionErr(t *testing.T) {
var err error
_, err = SortNumeric("", true)
require.Error(t, err)
_, err = SortNumeric("", false)
require.Error(t, err)
_, err = SortAlphabetical("", true)
require.Error(t, err)
_, err = SortAlphabetical("", false)
require.Error(t, err)
_, err = SortTemporal("", true, "%%Y%m%d%H", "")
require.Error(t, err)
_, err = SortTemporal("", false, "%%Y%m%d%H", "")
require.Error(t, err)
_, err = SortTemporal("foo", true, "", "")
require.Error(t, err)
_, err = SortTemporal("foo", false, "", "")
require.Error(t, err)
_, err = SortTemporal("foo", true, "%%Y%m%d%H", "nowhere")
require.Error(t, err)
_, err = SortTemporal("foo", false, "%%Y%m%d%H", "nowhere")
require.Error(t, err)
}
type withOpts func(t *testing.T, ascending bool) []Option
func TestSort(t *testing.T) {
cases := []struct {
name string
withOpts withOpts
regex string
values []string
expectApplyErr string
expectAscending []string
}{
{
name: "Numeric",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortNumeric("num", ascending)
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<num>\d{2}).*log`,
values: []string{"55.log", "22.log", "66.log", "44.log"},
expectAscending: []string{"22.log", "44.log", "55.log", "66.log"},
},
{
name: "NumericParseErr",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortNumeric("num", ascending)
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<num>[a-z0-9]{2}).*log`,
values: []string{"bb.log", "66.log", "aa.log", "zz.log", "44.log"},
expectApplyErr: "strconv.Atoi: parsing \"zz\": invalid syntax; strconv.Atoi: parsing \"aa\": invalid syntax; strconv.Atoi: parsing \"bb\": invalid syntax",
expectAscending: []string{"44.log", "66.log"},
},
{
name: "Alphabetical",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortAlphabetical("word", ascending)
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<word>[a-z0-9]+).*log`,
values: []string{"5b2c.log", "a.log", "foo.log", "99.log"},
expectAscending: []string{"5b2c.log", "99.log", "a.log", "foo.log"},
},
{
name: "Temporal",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortTemporal("time", ascending, "%Y%m%d%H", "")
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<time>\d{4}\d{2}\d{2}\d{2}).*log`,
values: []string{"2023020611.log", "2023020612.log", "2023020610.log", "2023020609.log"},
expectAscending: []string{"2023020609.log", "2023020610.log", "2023020611.log", "2023020612.log"},
},
{
name: "TemporalParseErr",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortTemporal("time", ascending, "%Y%m%d%H", "")
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<time>[0-9a-z]+).*log`,
values: []string{"2023020xyz611.log", "2023020612.log", "2023asdf020610.log", "2023020609.log"},
expectApplyErr: "parsing time \"2023asdf020610\" as \"2006010215\": cannot parse \"asdf020610\" as \"01\"; parsing time \"2023020xyz611\" as \"2006010215\": cannot parse \"0xyz611\" as \"02\"",
expectAscending: []string{"2023020609.log", "2023020612.log"},
},
{
name: "SoloItem",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortNumeric("num", ascending)
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<num>[a-z0-9]{2}).*log`,
values: []string{"22.log"},
expectAscending: []string{"22.log"},
},
{
name: "SoloErr",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortNumeric("num", ascending)
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<num>[a-z0-9]{2}).*log`,
values: []string{"aa.log"},
expectApplyErr: `strconv.Atoi: parsing "aa": invalid syntax`,
expectAscending: []string{},
},
{
name: "AllErr",
withOpts: func(t *testing.T, ascending bool) []Option {
o, err := SortNumeric("num", ascending)
require.NoError(t, err)
return []Option{o}
},
regex: `(?P<num>[a-z0-9]{2}).*log`,
values: []string{"bb.log", "aa.log", "zz.log"},
expectApplyErr: "strconv.Atoi: parsing \"zz\": invalid syntax; strconv.Atoi: parsing \"aa\": invalid syntax; strconv.Atoi: parsing \"bb\": invalid syntax",
expectAscending: []string{},
},
}
for _, tc := range cases {
for _, ascending := range []bool{true, false} {
t.Run(fmt.Sprintf("%s/%t", tc.name, ascending), func(t *testing.T) {
result, err := Filter(tc.values, regexp.MustCompile(tc.regex), tc.withOpts(t, ascending)...)
if tc.expectApplyErr != "" {
assert.EqualError(t, err, tc.expectApplyErr)
} else {
assert.NoError(t, err)
}
if ascending {
assert.Equal(t, tc.expectAscending, result)
} else {
descending := make([]string, 0, len(tc.expectAscending))
for i := len(tc.expectAscending) - 1; i >= 0; i-- {
descending = append(descending, tc.expectAscending[i])
}
assert.Equal(t, descending, result)
}
})
}
}
}
func TestMTimeFilter(t *testing.T) {
epoch := time.Unix(0, 0)
cases := []struct {
name string
files []string
fileMTimes []time.Time
ascending bool
expectedErr string
expect []string
}{
{
name: "No files",
files: []string{},
fileMTimes: []time.Time{},
expect: []string{},
},
{
name: "Single file",
files: []string{"a.log"},
fileMTimes: []time.Time{epoch},
expect: []string{"a.log"},
},
{
name: "Multiple files",
files: []string{"a.log", "b.log"},
fileMTimes: []time.Time{epoch, epoch.Add(time.Hour)},
expect: []string{"b.log", "a.log"},
},
{
name: "Multiple files in order, ascending sort",
files: []string{"a.log", "b.log"},
fileMTimes: []time.Time{epoch, epoch.Add(time.Hour)},
ascending: true,
expect: []string{"a.log", "b.log"},
},
{
name: "Multiple files out of order, ascending sort",
files: []string{"a.log", "b.log"},
fileMTimes: []time.Time{epoch.Add(time.Hour), epoch},
ascending: true,
expect: []string{"b.log", "a.log"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tmpDir := t.TempDir()
items := []*item{}
// Create files with specified mtime
for i, file := range tc.files {
mtime := tc.fileMTimes[i]
fullPath := filepath.Join(tmpDir, file)
f, err := os.Create(fullPath)
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.Chtimes(fullPath, epoch, mtime))
it, err := newItem(fullPath, nil)
require.NoError(t, err)
items = append(items, it)
}
f := SortMtime(tc.ascending)
result, err := f.apply(items)
if tc.expectedErr != "" {
require.EqualError(t, err, tc.expectedErr)
} else {
require.NoError(t, err)
}
relativeResult := []string{}
for _, r := range result {
rel, err := filepath.Rel(tmpDir, r.value)
require.NoError(t, err)
relativeResult = append(relativeResult, rel)
}
require.Equal(t, tc.expect, relativeResult)
})
}
}