Repository navigation
Expand file tree
/
Copy pathtail_test.go
More file actions
412 lines (347 loc) · 9.85 KB
/
Copy pathtail_test.go
File metadata and controls
412 lines (347 loc) · 9.85 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"bufio"
"bytes"
"context"
"errors"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"testing"
"time"
)
func TestTailFileWritesAppendedLines(t *testing.T) {
tailDir := t.TempDir()
sourcePath := filepath.Join(tailDir, "source.log")
destPath := filepath.Join(tailDir, "dest.log")
if err := os.WriteFile(sourcePath, []byte{}, 0o644); err != nil {
t.Fatalf("write source file: %v", err)
}
dest, err := os.Create(destPath)
if err != nil {
t.Fatalf("create dest file: %v", err)
}
defer func() {
if err := dest.Close(); err != nil {
t.Errorf("close dest file: %v", err)
}
}()
ctx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go tailFile(ctx, sourcePath, true, dest)
appended := []string{"first line", "second line"}
for _, line := range appended {
f, err := os.OpenFile(sourcePath, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
t.Fatalf("open source for append: %v", err)
}
if _, err := f.WriteString(line + "\n"); err != nil {
_ = f.Close()
t.Fatalf("append source line: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close source file: %v", err)
}
}
deadline := time.Now().Add(5 * time.Second)
for {
got, err := os.ReadFile(destPath)
if err != nil {
cancel()
wg.Wait()
t.Fatalf("read dest file: %v", err)
}
if strings.Split(strings.TrimSpace(string(got)), "\n")[0] == appended[0] && strings.Contains(string(got), appended[1]) {
break
}
if time.Now().After(deadline) {
cancel()
wg.Wait()
t.Fatalf("timed out waiting for tailed lines, got %q", string(got))
}
time.Sleep(50 * time.Millisecond)
}
cancel()
wg.Wait()
got, err := os.ReadFile(destPath)
if err != nil {
t.Fatalf("read final dest file: %v", err)
}
scanner := bufio.NewScanner(strings.NewReader(string(got)))
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
t.Fatalf("scan dest file: %v", err)
}
if len(lines) != len(appended) {
t.Fatalf("expected %d tailed lines, got %d (%q)", len(appended), len(lines), string(got))
}
for i, want := range appended {
if lines[i] != want {
t.Fatalf("line %d mismatch: got %q want %q", i, lines[i], want)
}
}
}
func TestTailFileStopsWhenContextCanceled(t *testing.T) {
tailDir := t.TempDir()
sourcePath := filepath.Join(tailDir, "source.log")
destPath := filepath.Join(tailDir, "dest.log")
if err := os.WriteFile(sourcePath, []byte{}, 0o644); err != nil {
t.Fatalf("write source file: %v", err)
}
dest, err := os.Create(destPath)
if err != nil {
t.Fatalf("create dest file: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go tailFile(ctx, sourcePath, true, dest)
time.Sleep(200 * time.Millisecond)
cancel()
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("tailFile did not stop after context cancellation")
}
if err := dest.Close(); err != nil {
t.Fatalf("close dest file: %v", err)
}
got, err := os.ReadFile(destPath)
if err != nil {
t.Fatalf("read dest file: %v", err)
}
if string(got) != "" {
t.Fatalf("expected no tailed output, got %q", string(got))
}
}
// errWriter is an io.Writer whose every Write fails.
type errWriter struct {
err error
}
func (w errWriter) Write(p []byte) (int, error) {
return 0, w.err
}
func TestTailFileStopsOnWriteError(t *testing.T) {
tailDir := t.TempDir()
sourcePath := filepath.Join(tailDir, "source.log")
if err := os.WriteFile(sourcePath, []byte{}, 0o644); err != nil {
t.Fatalf("write source file: %v", err)
}
var logBuf bytes.Buffer
prevOutput := log.Writer()
log.SetOutput(&logBuf)
t.Cleanup(func() { log.SetOutput(prevOutput) })
writeErr := errors.New("destination is closed")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg.Add(1)
go tailFile(ctx, sourcePath, true, errWriter{err: writeErr})
time.Sleep(200 * time.Millisecond)
f, err := os.OpenFile(sourcePath, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
t.Fatalf("open source for append: %v", err)
}
if _, err := f.WriteString("line\n"); err != nil {
_ = f.Close()
t.Fatalf("append source line: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close source file: %v", err)
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("tailFile did not return after a write error")
}
if got := logBuf.String(); !strings.Contains(got, "unable to write tailed line from") || !strings.Contains(got, writeErr.Error()) {
t.Fatalf("expected write error in log output, got %q", got)
}
}
func TestTailFilePicksUpFileCreatedLater(t *testing.T) {
resetGlobals(t)
logs := captureLog(t)
tailDir := t.TempDir()
sourcePath := filepath.Join(tailDir, "later.log")
dest, destPath := newDestFile(t, tailDir)
stop := startTailFile(t, sourcePath, false, dest)
waitForLogContaining(t, logs, "Warning: unable to stat "+sourcePath)
if err := os.WriteFile(sourcePath, []byte("created late\nappended later\n"), 0o644); err != nil {
t.Fatalf("create source file: %v", err)
}
waitForFileContaining(t, destPath, "created late", "appended later")
stop()
}
func TestTailFileReadsNamedPipe(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("named pipes via syscall.Mkfifo are not available on Windows")
}
resetGlobals(t)
tailDir := t.TempDir()
pipePath := filepath.Join(tailDir, "pipe.log")
if err := syscall.Mkfifo(pipePath, 0o600); err != nil {
t.Fatalf("create fifo: %v", err)
}
dest, destPath := newDestFile(t, tailDir)
stop := startTailFile(t, pipePath, false, dest)
// Opening the write end blocks until tailFile opens the read end, so bound it with a timeout.
writerCh := make(chan *os.File, 1)
errCh := make(chan error, 1)
go func() {
w, err := os.OpenFile(pipePath, os.O_WRONLY, 0)
if err != nil {
errCh <- err
return
}
writerCh <- w
}()
var writer *os.File
select {
case writer = <-writerCh:
t.Cleanup(func() { _ = writer.Close() })
case err := <-errCh:
t.Fatalf("open fifo for writing: %v", err)
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for tailFile to open the fifo for reading")
}
if _, err := writer.WriteString("from pipe\nsecond pipe line\n"); err != nil {
t.Fatalf("write to fifo: %v", err)
}
waitForFileContaining(t, destPath, "from pipe", "second pipe line")
// The tail library blocks reading the pipe until the write end closes, so close it before stopping.
if err := writer.Close(); err != nil {
t.Fatalf("close fifo writer: %v", err)
}
stop()
}
func TestTailFilePollModeReadsExistingAndAppendedLines(t *testing.T) {
resetGlobals(t)
tailDir := t.TempDir()
sourcePath := filepath.Join(tailDir, "poll.log")
dest, destPath := newDestFile(t, tailDir)
if err := os.WriteFile(sourcePath, []byte("existing line\n"), 0o644); err != nil {
t.Fatalf("write source file: %v", err)
}
stop := startTailFile(t, sourcePath, true, dest)
f, err := os.OpenFile(sourcePath, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
t.Fatalf("open source for append: %v", err)
}
if _, err := f.WriteString("appended line\n"); err != nil {
_ = f.Close()
t.Fatalf("append source line: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close source file: %v", err)
}
waitForFileContaining(t, destPath, "existing line", "appended line")
stop()
}
// newDestFile creates a destination file in dir and closes it when the test ends.
func newDestFile(t *testing.T, dir string) (*os.File, string) {
t.Helper()
destPath := filepath.Join(dir, "dest.log")
dest, err := os.Create(destPath)
if err != nil {
t.Fatalf("create dest file: %v", err)
}
t.Cleanup(func() { _ = dest.Close() })
return dest, destPath
}
// startTailFile runs tailFile in the background. The returned stop func cancels it and fails
// the test if it has not returned within five seconds; it is also called on test cleanup.
func startTailFile(t *testing.T, file string, poll bool, dest *os.File) (stop func()) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go tailFile(ctx, file, poll, dest)
stop = func() {
cancel()
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("tailFile did not return after context cancellation")
}
}
t.Cleanup(stop)
return stop
}
// syncBuffer receives log output from the tailFile goroutine while the test reads it.
type syncBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}
func (b *syncBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}
func (b *syncBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}
// captureLog sends the standard logger to a buffer until the test ends.
func captureLog(t *testing.T) *syncBuffer {
t.Helper()
var buf syncBuffer
oldOutput := log.Writer()
log.SetOutput(io.Writer(&buf))
t.Cleanup(func() { log.SetOutput(oldOutput) })
return &buf
}
func waitForLogContaining(t *testing.T, logs *syncBuffer, want string) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for !strings.Contains(logs.String(), want) {
if time.Now().After(deadline) {
t.Fatalf("timed out waiting for log %q, got %q", want, logs.String())
}
time.Sleep(50 * time.Millisecond)
}
}
func waitForFileContaining(t *testing.T, path string, wants ...string) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for {
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
all := true
for _, want := range wants {
if !strings.Contains(string(got), want) {
all = false
break
}
}
if all {
return
}
if time.Now().After(deadline) {
t.Fatalf("timed out waiting for %q in %s, got %q", wants, path, string(got))
}
time.Sleep(50 * time.Millisecond)
}
}