Skip to content

Commit

Permalink
Add symlink test
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Apr 21, 2021
1 parent e9834df commit 25673ce
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions filebeat/input/filestream/fswatch_test_non_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -44,13 +45,14 @@ func TestFileScannerSymlinks(t *testing.T) {
testCases := map[string]struct {
paths []string
excludedFiles []match.Matcher
includedFiles []match.Matcher
symlinks bool
expectedFiles []string
}{
// covers test_input.py/test_skip_symlinks
"skip symlinks": {
paths: []string{
filepath.Join(tmpDir, "symlink_to_included_file"),
filepath.Join(tmpDir, "symlink_to_0"),
filepath.Join(tmpDir, "included_file"),
},
symlinks: false,
Expand All @@ -60,22 +62,37 @@ func TestFileScannerSymlinks(t *testing.T) {
},
"return a file once if symlinks are enabled": {
paths: []string{
filepath.Join(tmpDir, "symlink_to_included_file"),
filepath.Join(tmpDir, "symlink_to_0"),
filepath.Join(tmpDir, "included_file"),
},
symlinks: true,
expectedFiles: []string{
mustAbsPath(filepath.Join(tmpDir, "included_file")),
},
},
"do not return symlink if original file is not allowed": {
paths: []string{
filepath.Join(tmpDir, "symlink_to_1"),
filepath.Join(tmpDir, "included_file"),
},
excludedFiles: []match.Matcher{
match.MustCompile("original_" + excludedFileName),
},
symlinks: true,
expectedFiles: []string{
mustAbsPath(filepath.Join(tmpDir, "included_file")),
},
},
}

err := os.Symlink(
mustAbsPath(filepath.Join(tmpDir, "included_file")),
mustAbsPath(filepath.Join(tmpDir, "symlink_to_included_file")),
)
if err != nil {
t.Fatal(err)
for i, filename := range []string{"included_file", "excluded_file"} {
err := os.Symlink(
mustAbsPath(filepath.Join(tmpDir, "original_"+filename)),
mustAbsPath(filepath.Join(tmpDir, "symlink_to_"+strconv.Itoa(i))),
)
if err != nil {
t.Fatal(err)
}
}

for name, test := range testCases {
Expand All @@ -84,6 +101,7 @@ func TestFileScannerSymlinks(t *testing.T) {
t.Run(name, func(t *testing.T) {
cfg := fileScannerConfig{
ExcludedFiles: test.excludedFiles,
IncludedFiles: test.includedFiles,
Symlinks: true,
RecursiveGlob: false,
}
Expand Down Expand Up @@ -150,3 +168,11 @@ func TestFileWatcherRenamedFile(t *testing.T) {
assert.Equal(t, testPath, evt.OldPath)
assert.Equal(t, renamedPath, evt.NewPath)
}

func mustAbsPath(filename string) string {
abspath, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
return abspath
}

0 comments on commit 25673ce

Please sign in to comment.