-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeepcurrent_test.go
More file actions
273 lines (250 loc) · 7.36 KB
/
keepcurrent_test.go
File metadata and controls
273 lines (250 loc) · 7.36 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package keepcurrent
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/rand"
"encoding/json"
"io"
"io/ioutil"
"os"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestReadWriteSameFile(t *testing.T) {
b := make([]byte, 1024*1024)
_, err := rand.Read(b)
assert.NoError(t, err)
name, content := writeTempFile(t, b)
defer os.Remove(name)
runner := New(FromFile(name), ToFile(name))
runner.OnSourceError = func(err error, tries int) time.Duration {
assert.Equal(t, 1, tries)
assert.NoError(t, err)
return 0
}
runner.OnSinkError = func(s Sink, err error) {
assert.NoError(t, err)
}
stop := runner.Start(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
stop()
b, err = ioutil.ReadFile(name)
assert.NoError(t, err)
assert.Equal(t, b, content)
}
func TestReadWriteWithPreprocessor(t *testing.T) {
rot13 := func(r rune) rune {
if r >= 'a' && r <= 'z' {
// Rotate lowercase letters 13 places.
if r >= 'm' {
return r - 13
} else {
return r + 13
}
} else if r >= 'A' && r <= 'Z' {
// Rotate uppercase letters 13 places.
if r >= 'M' {
return r - 13
} else {
return r + 13
}
}
// Do nothing.
return r
}
name, content := writeTempFile(t, []byte(strings.Map(rot13, "test input")))
defer os.Remove(name)
preWrite := func(r io.Reader) (io.Reader, error) {
b, _ := ioutil.ReadAll(r)
s := strings.Map(rot13, string(b))
return strings.NewReader(s), nil
}
postRead := func(r io.ReadCloser) (io.ReadCloser, error) {
b, _ := ioutil.ReadAll(r)
s := strings.Map(rot13, string(b))
return ioutil.NopCloser(strings.NewReader(s)), nil
}
runner := New(FromFileWithPreprocessor(name, postRead), ToFileWithPreprocessor(name, preWrite))
runner.OnSourceError = func(err error, tries int) time.Duration {
assert.Equal(t, 1, tries)
assert.NoError(t, err)
return 0
}
runner.OnSinkError = func(s Sink, err error) {
assert.NoError(t, err)
}
stop := runner.Start(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
stop()
b, err := ioutil.ReadFile(name)
assert.NoError(t, err)
assert.Equal(t, b, content)
}
func writeTempFile(t *testing.T, b []byte) (string, []byte) {
f, err := ioutil.TempFile(os.TempDir(), "keep_current_test")
assert.NoError(t, err)
_, err = f.Write(b)
assert.NoError(t, err)
f.Close()
return f.Name(), b
}
func TestUpdateFromWeb(t *testing.T) {
ch := make(chan []byte)
url := "https://httpbin.org/get"
runner := New(FromWeb(url), ToChannel(ch))
runner.OnSourceError = func(err error, tries int) time.Duration {
assert.Fail(t, "unexpected source error "+err.Error())
return 0
}
runner.OnSinkError = func(s Sink, err error) {
assert.Fail(t, "unexpected sink error "+err.Error())
}
stop := runner.Start(10 * time.Second)
got := make(chan bool, 1)
go func() {
for b := range ch {
data := make(map[string]interface{})
if assert.NoError(t, json.Unmarshal(b, &data)) {
assert.EqualValues(t, data["url"], url)
got <- true
}
}
}()
<-got
stop()
}
type byteSource struct {
remainingFailures int32
calls int32
lastModified time.Time
}
func (s *byteSource) Fetch(ifNewerThan time.Time) (io.ReadCloser, error) {
if ifNewerThan.After(s.lastModified) {
return nil, ErrUnmodified
}
atomic.AddInt32(&s.calls, 1)
if atomic.AddInt32(&s.remainingFailures, -1) > 0 {
// keeps failing with io.ErrUnexpectedEOF
return ioutil.NopCloser(s), nil
}
return ioutil.NopCloser(bytes.NewBuffer([]byte("abcde"))), nil
}
func (s *byteSource) Read(p []byte) (int, error) {
return 0, io.ErrUnexpectedEOF
}
func TestIfNewerThan(t *testing.T) {
ch := make(chan []byte)
var updates int32
go func() {
for range ch {
atomic.AddInt32(&updates, 1)
}
}()
s := byteSource{lastModified: time.Now()}
runner := New(&s, ToChannel(ch))
stop := runner.Start(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
stop()
assert.EqualValues(t, 1, atomic.LoadInt32(&s.calls))
assert.EqualValues(t, 1, atomic.LoadInt32(&updates))
// Now make sure it does not fetch the source if it's not newer than what
// got from InitFrom.
runner = New(&s, ToChannel(ch))
runner.InitFrom(&s)
assert.EqualValues(t, 2, atomic.LoadInt32(&s.calls))
stop = runner.Start(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
stop()
assert.EqualValues(t, 2, atomic.LoadInt32(&s.calls))
}
func TestBackoffOnFail(t *testing.T) {
ch := make(chan []byte)
var updates int32
go func() {
for range ch {
atomic.AddInt32(&updates, 1)
}
}()
s := byteSource{lastModified: time.Now(), remainingFailures: 5}
runner := New(&s, ToChannel(ch))
var finalFailures int32
runner.OnSourceError = ExpBackoffThenFail(time.Millisecond, 3, func(err error) {
atomic.AddInt32(&finalFailures, 1)
})
stop := runner.Start(100 * time.Millisecond)
defer stop()
time.Sleep(50 * time.Millisecond)
// The first synchronization should have failed
assert.EqualValues(t, 3, atomic.LoadInt32(&s.calls))
assert.EqualValues(t, 0, atomic.LoadInt32(&updates))
assert.EqualValues(t, 1, atomic.LoadInt32(&finalFailures))
time.Sleep(150 * time.Millisecond)
// The second round of synchronization should have completed
assert.EqualValues(t, 5, atomic.LoadInt32(&s.calls))
assert.EqualValues(t, 1, atomic.LoadInt32(&updates))
assert.EqualValues(t, 1, atomic.LoadInt32(&finalFailures))
}
// bytesSource is a Source that returns a fixed byte slice every Fetch.
type bytesSource struct{ data []byte }
func (b *bytesSource) Fetch(time.Time) (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(b.data)), nil
}
// makeTarGz builds an in-memory .tar.gz containing a single file stored
// under the given path (e.g. "dir/file") so tests can exercise the basename-
// match path without shipping a fixture.
func makeTarGz(t *testing.T, storedPath string, payload []byte) []byte {
t.Helper()
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
tw := tar.NewWriter(gz)
if err := tw.WriteHeader(&tar.Header{
Name: storedPath,
Mode: 0644,
Size: int64(len(payload)),
Typeflag: tar.TypeReg,
ModTime: time.Now(),
}); err != nil {
t.Fatalf("tar header: %v", err)
}
if _, err := tw.Write(payload); err != nil {
t.Fatalf("tar write: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatalf("tar close: %v", err)
}
if err := gz.Close(); err != nil {
t.Fatalf("gz close: %v", err)
}
return buf.Bytes()
}
// Regression test for the two bugs fixed in PR #7: FromTarGz must extract
// a file whose stored path includes a directory prefix (as MaxMind ships
// its mmdb tarballs), matched by the basename the caller passes.
func TestFromTarGzBasenameMatch(t *testing.T) {
want := []byte("hello world")
tgz := makeTarGz(t, "GeoLite2-City_20260116/GeoLite2-City.mmdb", want)
src := FromTarGz(&bytesSource{data: tgz}, "GeoLite2-City.mmdb")
rc, err := src.Fetch(time.Time{})
assert.NoError(t, err, "Fetch should find the file by basename")
if err != nil {
return
}
defer rc.Close()
got, err := io.ReadAll(rc)
assert.NoError(t, err)
assert.Equal(t, want, got)
}
// When the requested file isn't in the archive we should return a clear
// error, rather than the silent "no extraction format" that hid the
// original regression.
func TestFromTarGzMissingFile(t *testing.T) {
tgz := makeTarGz(t, "some/other-file.txt", []byte("x"))
src := FromTarGz(&bytesSource{data: tgz}, "GeoLite2-City.mmdb")
_, err := src.Fetch(time.Time{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "not found")
}