Skip to content

Commit

Permalink
Do not run symlink tests on Windows (#21472)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Oct 5, 2020
1 parent 9aab0c0 commit c912167
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 90 deletions.
146 changes: 57 additions & 89 deletions filebeat/input/filestream/fswatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import (
"github.com/elastic/beats/v7/libbeat/logp"
)

var (
excludedFilePath = filepath.Join("testdata", "excluded_file")
includedFilePath = filepath.Join("testdata", "included_file")
directoryPath = filepath.Join("testdata", "unharvestable_dir")
)

func TestFileScanner(t *testing.T) {
testCases := map[string]struct {
paths []string
Expand All @@ -39,56 +45,30 @@ func TestFileScanner(t *testing.T) {
expectedFiles []string
}{
"select all files": {
paths: []string{
filepath.Join("testdata", "excluded_file"),
filepath.Join("testdata", "included_file"),
},
paths: []string{excludedFilePath, includedFilePath},
expectedFiles: []string{
mustAbsPath(filepath.Join("testdata", "excluded_file")),
mustAbsPath(filepath.Join("testdata", "included_file")),
mustAbsPath(excludedFilePath),
mustAbsPath(includedFilePath),
},
},
"skip excluded files": {
paths: []string{
filepath.Join("testdata", "excluded_file"),
filepath.Join("testdata", "included_file"),
},
paths: []string{excludedFilePath, includedFilePath},
excludedFiles: []match.Matcher{
match.MustCompile(filepath.Join("testdata", "excluded_file")),
},
expectedFiles: []string{
mustAbsPath(filepath.Join("testdata", "included_file")),
},
},
// covers test_input.py/test_skip_symlinks
"skip symlinks": {
paths: []string{
filepath.Join("testdata", "symlink_to_included_file"),
filepath.Join("testdata", "included_file"),
match.MustCompile("excluded_file"),
},
symlinks: false,
expectedFiles: []string{
mustAbsPath(filepath.Join("testdata", "included_file")),
},
},
"return a file once if symlinks are enabled": {
paths: []string{
filepath.Join("testdata", "symlink_to_included_file"),
filepath.Join("testdata", "included_file"),
},
symlinks: true,
expectedFiles: []string{
mustAbsPath(filepath.Join("testdata", "included_file")),
mustAbsPath(includedFilePath),
},
},
"skip directories": {
paths: []string{
filepath.Join("testdata", "unharvestable_dir"),
},
paths: []string{directoryPath},
expectedFiles: []string{},
},
}

setupFilesForScannerTest(t)
defer removeFilesOfScannerTest(t)

for name, test := range testCases {
test := test

Expand All @@ -107,11 +87,50 @@ func TestFileScanner(t *testing.T) {
for p, _ := range files {
paths = append(paths, p)
}
assert.Equal(t, test.expectedFiles, paths)
assert.True(t, checkIfSameContents(test.expectedFiles, paths))
})
}
}

func setupFilesForScannerTest(t *testing.T) {
err := os.MkdirAll(directoryPath, 0750)
if err != nil {
t.Fatal(t)
}

for _, path := range []string{excludedFilePath, includedFilePath} {
f, err := os.Create(path)
if err != nil {
t.Fatalf("file %s, error %v", path, err)
}
f.Close()
}
}

func removeFilesOfScannerTest(t *testing.T) {
err := os.RemoveAll("testdata")
if err != nil {
t.Fatal(err)
}
}

// only handles sets
func checkIfSameContents(one, other []string) bool {
if len(one) != len(other) {
return false
}

mustFind := len(one)
for _, oneElem := range one {
for _, otherElem := range other {
if oneElem == otherElem {
mustFind--
}
}
}
return mustFind == 0
}

func TestFileWatchNewDeleteModified(t *testing.T) {
oldTs := time.Now()
newTs := oldTs.Add(5 * time.Second)
Expand Down Expand Up @@ -201,9 +220,7 @@ func TestFileWatchNewDeleteModified(t *testing.T) {
events: make(chan loginp.FSEvent),
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go w.watch(ctx)
go w.watch(context.Background())

for _, expectedEvent := range test.expectedEvents {
evt := w.Event()
Expand All @@ -213,55 +230,6 @@ func TestFileWatchNewDeleteModified(t *testing.T) {
}
}

func TestFileWatcherRenamedFile(t *testing.T) {
testPath := mustAbsPath(filepath.Join("testdata", "first_name"))
renamedPath := mustAbsPath(filepath.Join("testdata", "renamed"))

f, err := os.Create(testPath)
if err != nil {
t.Fatal(err)
}
f.Close()
fi, err := os.Stat(testPath)
if err != nil {
t.Fatal(err)
}

cfg := fileScannerConfig{
ExcludedFiles: nil,
Symlinks: false,
RecursiveGlob: false,
}
scanner, err := newFileScanner([]string{testPath, renamedPath}, cfg)
if err != nil {
t.Fatal(err)
}
w := fileWatcher{
log: logp.L(),
scanner: scanner,
events: make(chan loginp.FSEvent),
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go w.watch(ctx)
assert.Equal(t, loginp.FSEvent{Op: loginp.OpCreate, OldPath: "", NewPath: testPath, Info: fi}, w.Event())

err = os.Rename(testPath, renamedPath)
if err != nil {
t.Fatal(err)
}
defer os.Remove(renamedPath)
fi, err = os.Stat(renamedPath)
if err != nil {
t.Fatal(err)
}

go w.watch(ctx)
assert.Equal(t, loginp.FSEvent{Op: loginp.OpRename, OldPath: testPath, NewPath: renamedPath, Info: fi}, w.Event())
}

type mockScanner struct {
files map[string]os.FileInfo
}
Expand Down
144 changes: 144 additions & 0 deletions filebeat/input/filestream/fswatch_test_non_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// +build !windows

package filestream

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

loginp "github.com/elastic/beats/v7/filebeat/input/filestream/internal/input-logfile"
"github.com/elastic/beats/v7/libbeat/common/match"
"github.com/elastic/beats/v7/libbeat/logp"
)

func TestFileScannerSymlinks(t *testing.T) {
testCases := map[string]struct {
paths []string
excludedFiles []match.Matcher
symlinks bool
expectedFiles []string
}{
// covers test_input.py/test_skip_symlinks
"skip symlinks": {
paths: []string{
filepath.Join("testdata", "symlink_to_included_file"),
filepath.Join("testdata", "included_file"),
},
symlinks: false,
expectedFiles: []string{
mustAbsPath(filepath.Join("testdata", "included_file")),
},
},
"return a file once if symlinks are enabled": {
paths: []string{
filepath.Join("testdata", "symlink_to_included_file"),
filepath.Join("testdata", "included_file"),
},
symlinks: true,
expectedFiles: []string{
mustAbsPath(filepath.Join("testdata", "included_file")),
},
},
}

err := os.Symlink(
mustAbsPath(filepath.Join("testdata", "included_file")),
mustAbsPath(filepath.Join("testdata", "symlink_to_included_file")),
)
if err != nil {
t.Fatal(err)
}

for name, test := range testCases {
test := test

t.Run(name, func(t *testing.T) {
cfg := fileScannerConfig{
ExcludedFiles: test.excludedFiles,
Symlinks: true,
RecursiveGlob: false,
}
fs, err := newFileScanner(test.paths, cfg)
if err != nil {
t.Fatal(err)
}
files := fs.GetFiles()
paths := make([]string, 0)
for p, _ := range files {
paths = append(paths, p)
}
assert.Equal(t, test.expectedFiles, paths)
})
}
}

func TestFileWatcherRenamedFile(t *testing.T) {
testPath := mustAbsPath("first_name")
renamedPath := mustAbsPath("renamed")

f, err := os.Create(testPath)
if err != nil {
t.Fatal(err)
}
f.Close()
fi, err := os.Stat(testPath)
if err != nil {
t.Fatal(err)
}

cfg := fileScannerConfig{
ExcludedFiles: nil,
Symlinks: false,
RecursiveGlob: false,
}
scanner, err := newFileScanner([]string{testPath, renamedPath}, cfg)
if err != nil {
t.Fatal(err)
}
w := fileWatcher{
log: logp.L(),
scanner: scanner,
events: make(chan loginp.FSEvent),
}

go w.watch(context.Background())
assert.Equal(t, loginp.FSEvent{Op: loginp.OpCreate, OldPath: "", NewPath: testPath, Info: fi}, w.Event())

err = os.Rename(testPath, renamedPath)
if err != nil {
t.Fatal(err)
}
defer os.Remove(renamedPath)
fi, err = os.Stat(renamedPath)
if err != nil {
t.Fatal(err)
}

go w.watch(context.Background())
evt := w.Event()

assert.Equal(t, loginp.OpRename, evt.Op)
assert.Equal(t, testPath, evt.OldPath)
assert.Equal(t, renamedPath, evt.NewPath)
}
Empty file.
Empty file.

This file was deleted.

0 comments on commit c912167

Please sign in to comment.