-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexgen_test.go
256 lines (235 loc) · 6.22 KB
/
indexgen_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
package zlog
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDailyIndexNameGenerator(t *testing.T) {
tests := []struct {
name string
config IndexConfig
timeFunc func() time.Time
want string
}{
{
name: "UTC with dot format",
config: IndexConfig{
BaseIndexName: "logs",
Format: string(DateFormatDot),
Location: time.UTC,
},
timeFunc: func() time.Time {
return time.Date(2024, 1, 25, 12, 0, 0, 0, time.UTC)
},
want: "logs-2024.01.25",
},
{
name: "Tokyo timezone with dash format",
config: IndexConfig{
BaseIndexName: "app-logs",
Format: string(DateFormatDash),
Location: MustLoadLocation("Asia/Tokyo"),
},
timeFunc: func() time.Time {
// This is 2024-01-25 00:00:00 UTC, which should be
// 2024-01-25 09:00:00 in Tokyo
return time.Date(2024, 1, 25, 0, 0, 0, 0, time.UTC)
},
want: "app-logs-2024-01-25",
},
{
name: "Short format",
config: IndexConfig{
BaseIndexName: "metrics",
Format: string(DateFormatShort),
Location: time.UTC,
},
timeFunc: func() time.Time {
return time.Date(2024, 1, 25, 12, 0, 0, 0, time.UTC)
},
want: "metrics-20240125",
},
{
name: "Custom hourly format",
config: IndexConfig{
BaseIndexName: "hourly-logs",
Format: "2006-01-02-15",
Location: time.UTC,
},
timeFunc: func() time.Time {
return time.Date(2024, 1, 25, 14, 30, 0, 0, time.UTC)
},
want: "hourly-logs-2024-01-25-14",
},
{
name: "Default format when empty",
config: IndexConfig{
BaseIndexName: "default-logs",
Format: "",
Location: time.UTC,
},
timeFunc: func() time.Time {
return time.Date(2024, 1, 25, 12, 0, 0, 0, time.UTC)
},
want: "default-logs-2024.01.25",
},
{
name: "Default UTC when location is nil",
config: IndexConfig{
BaseIndexName: "utc-logs",
Format: string(DateFormatDot),
Location: nil,
},
timeFunc: func() time.Time {
return time.Date(2024, 1, 25, 12, 0, 0, 0, time.UTC)
},
want: "utc-logs-2024.01.25",
},
}
// Store original time.Now
originalTimeNow := timeNow
defer func() { timeNow = originalTimeNow }()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock time.Now for this test
timeNow = tt.timeFunc
generator := NewIndexGenerator(tt.config)
got := generator.GetIndexName()
assert.Equal(t, tt.want, got)
})
}
}
func TestIndexRotation(t *testing.T) {
// Store original time.Now
originalTimeNow := timeNow
defer func() { timeNow = originalTimeNow }()
// Create a generator with UTC timezone
generator := NewIndexGenerator(IndexConfig{
BaseIndexName: "app-logs",
Format: string(DateFormatDot),
Location: time.UTC,
})
// Test scenarios for rotation
scenarios := []struct {
timestamp time.Time
expected string
}{
{
// Day 1
timestamp: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
expected: "app-logs-2024.01.01",
},
{
// Same day, different hour
timestamp: time.Date(2024, 1, 1, 23, 59, 59, 0, time.UTC),
expected: "app-logs-2024.01.01",
},
{
// Next day
timestamp: time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC),
expected: "app-logs-2024.01.02",
},
{
// Month transition
timestamp: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
expected: "app-logs-2024.02.01",
},
{
// Year transition
timestamp: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
expected: "app-logs-2025.01.01",
},
}
for _, sc := range scenarios {
timeNow = func() time.Time { return sc.timestamp }
got := generator.GetIndexName()
assert.Equal(t, sc.expected, got, "Time: "+sc.timestamp.String())
}
}
func TestHourlyRotation(t *testing.T) {
// Store original time.Now
originalTimeNow := timeNow
defer func() { timeNow = originalTimeNow }()
// Create a generator with hourly rotation
generator := NewIndexGenerator(IndexConfig{
BaseIndexName: "hourly-logs",
Format: "2006.01.02-15",
Location: time.UTC,
})
scenarios := []struct {
timestamp time.Time
expected string
}{
{
timestamp: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
expected: "hourly-logs-2024.01.01-00",
},
{
// Same hour
timestamp: time.Date(2024, 1, 1, 0, 59, 59, 0, time.UTC),
expected: "hourly-logs-2024.01.01-00",
},
{
// Next hour
timestamp: time.Date(2024, 1, 1, 1, 0, 0, 0, time.UTC),
expected: "hourly-logs-2024.01.01-01",
},
{
// Day transition
timestamp: time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC),
expected: "hourly-logs-2024.01.02-00",
},
}
for _, sc := range scenarios {
timeNow = func() time.Time { return sc.timestamp }
got := generator.GetIndexName()
assert.Equal(t, sc.expected, got, "Time: "+sc.timestamp.String())
}
}
func TestTimezoneRotation(t *testing.T) {
// Store original time.Now
originalTimeNow := timeNow
defer func() { timeNow = originalTimeNow }()
// Create generators for different timezones
utcGen := NewIndexGenerator(IndexConfig{
BaseIndexName: "logs",
Format: string(DateFormatDot),
Location: time.UTC,
})
tokyoGen := NewIndexGenerator(IndexConfig{
BaseIndexName: "logs",
Format: string(DateFormatDot),
Location: MustLoadLocation("Asia/Tokyo"),
})
// Test UTC midnight and corresponding Tokyo time
scenarios := []struct {
timestamp time.Time
expectedUTC string
expectedJST string
description string
}{
{
// When it's UTC midnight (00:00), it's 09:00 JST
timestamp: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
expectedUTC: "logs-2024.01.01",
expectedJST: "logs-2024.01.01",
description: "UTC midnight",
},
{
// When it's 15:00 UTC, it's 00:00 JST next day
timestamp: time.Date(2024, 1, 1, 15, 0, 0, 0, time.UTC),
expectedUTC: "logs-2024.01.01",
expectedJST: "logs-2024.01.02",
description: "JST midnight",
},
}
for _, sc := range scenarios {
t.Run(sc.description, func(t *testing.T) {
timeNow = func() time.Time { return sc.timestamp }
gotUTC := utcGen.GetIndexName()
assert.Equal(t, sc.expectedUTC, gotUTC, "UTC index at "+sc.timestamp.String())
gotJST := tokyoGen.GetIndexName()
assert.Equal(t, sc.expectedJST, gotJST, "JST index at "+sc.timestamp.String())
})
}
}