Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr committed Jun 19, 2023
1 parent bfb6387 commit 7d7659b
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions filebeat/input/filestream/fswatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,77 @@ func TestFileWatchNewDeleteModified(t *testing.T) {
}
}

func TestFileWatcherTruncate(t *testing.T) {
oldTs := time.Now()
newTs := oldTs.Add(time.Second)
testCases := map[string]struct {
prevFiles map[string]os.FileInfo
nextFiles map[string]os.FileInfo
expectedEvents []loginp.FSEvent
}{
"truncated file, only size changes": {
prevFiles: map[string]os.FileInfo{
"path": testFileInfo{"path", 42, oldTs, nil},
},
nextFiles: map[string]os.FileInfo{
"path": testFileInfo{"path", 0, oldTs, nil},
},
expectedEvents: []loginp.FSEvent{
{Op: loginp.OpTruncate, OldPath: "path", NewPath: "path", Info: testFileInfo{"path", 0, oldTs, nil}},
},
},
"truncated file, mod time and size changes": {
prevFiles: map[string]os.FileInfo{
"path": testFileInfo{"path", 42, oldTs, nil},
},
nextFiles: map[string]os.FileInfo{
"path": testFileInfo{"path", 0, newTs, nil},
},
expectedEvents: []loginp.FSEvent{
{Op: loginp.OpTruncate, OldPath: "path", NewPath: "path", Info: testFileInfo{"path", 0, newTs, nil}},
},
},
"no file change": {
prevFiles: map[string]os.FileInfo{
"path": testFileInfo{"path", 42, oldTs, nil},
},
nextFiles: map[string]os.FileInfo{
"path": testFileInfo{"path", 42, oldTs, nil},
},
expectedEvents: []loginp.FSEvent{},
},
}

for name, test := range testCases {
t.Run(name, func(t *testing.T) {
w := fileWatcher{
log: logp.L(),
prev: test.prevFiles,
scanner: &mockScanner{test.nextFiles},
events: make(chan loginp.FSEvent, len(test.expectedEvents)),
sameFileFunc: testSameFile,
}

w.watch(context.Background())
close(w.events)

actual := []loginp.FSEvent{}
for evt := range w.events {
actual = append(actual, evt)
}

if len(actual) != len(test.expectedEvents) {
t.Fatalf("expecting %d elements, got %d", len(test.expectedEvents), len(actual))
}
for i := range test.expectedEvents {
if test.expectedEvents[i] != actual[i] {
t.Errorf("element [%d] differ. Expecting:\n%#v\nGot:\n%#v\n", i, test.expectedEvents[i], actual[i])
}
}
})
}
}

type mockScanner struct {
files map[string]os.FileInfo
}
Expand Down

0 comments on commit 7d7659b

Please sign in to comment.