2018-07-31 22:05:55 +00:00
|
|
|
package filecount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoFilters(t *testing.T) {
|
2018-10-05 19:55:23 +00:00
|
|
|
fc := getNoFilterFileCount("*")
|
|
|
|
matches := []string{"foo", "bar", "baz", "qux", "subdir/", "subdir/quux", "subdir/quuz"}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoFiltersOnChildDir(t *testing.T) {
|
|
|
|
fc := getNoFilterFileCount("testdata/*")
|
|
|
|
matches := []string{"subdir/quux", "subdir/quuz"}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata/subdir", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameFilter(t *testing.T) {
|
2018-10-05 19:55:23 +00:00
|
|
|
fc := getNoFilterFileCount("testdata")
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.Name = "ba*"
|
|
|
|
matches := []string{"bar", "baz"}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonRecursive(t *testing.T) {
|
2018-10-05 19:55:23 +00:00
|
|
|
fc := getNoFilterFileCount("testdata")
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.Recursive = false
|
|
|
|
matches := []string{"foo", "bar", "baz", "qux", "subdir"}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegularOnlyFilter(t *testing.T) {
|
2018-10-05 19:55:23 +00:00
|
|
|
fc := getNoFilterFileCount("testdata")
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.RegularOnly = true
|
|
|
|
matches := []string{
|
|
|
|
"foo", "bar", "baz", "qux", "subdir/quux", "subdir/quuz",
|
|
|
|
}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSizeFilter(t *testing.T) {
|
2018-10-05 19:55:23 +00:00
|
|
|
fc := getNoFilterFileCount("testdata")
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.Size = -100
|
2018-10-05 19:55:23 +00:00
|
|
|
matches := []string{"foo", "bar", "baz", "subdir/quux", "subdir/quuz"}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
|
|
|
|
fc.Size = 100
|
|
|
|
matches = []string{"qux"}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMTimeFilter(t *testing.T) {
|
2018-10-05 19:55:23 +00:00
|
|
|
oldFile := filepath.Join(getTestdataDir("testdata"), "baz")
|
2018-07-31 22:05:55 +00:00
|
|
|
mtime := time.Date(1979, time.December, 14, 18, 25, 5, 0, time.UTC)
|
|
|
|
if err := os.Chtimes(oldFile, mtime, mtime); err != nil {
|
|
|
|
t.Skip("skipping mtime filter test.")
|
|
|
|
}
|
|
|
|
fileAge := time.Since(mtime) - (60 * time.Second)
|
|
|
|
|
2018-10-05 19:55:23 +00:00
|
|
|
fc := getNoFilterFileCount("testdata")
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.MTime = internal.Duration{Duration: -fileAge}
|
2018-10-05 19:55:23 +00:00
|
|
|
matches := []string{"foo", "bar", "qux", "subdir/", "subdir/quux", "subdir/quuz"}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
|
|
|
|
fc.MTime = internal.Duration{Duration: fileAge}
|
|
|
|
matches = []string{"baz"}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
|
|
|
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 19:55:23 +00:00
|
|
|
func getNoFilterFileCount(dir string) FileCount {
|
2018-07-31 22:05:55 +00:00
|
|
|
return FileCount{
|
2018-10-05 19:55:23 +00:00
|
|
|
Directories: []string{getTestdataDir(dir)},
|
2018-07-31 22:05:55 +00:00
|
|
|
Name: "*",
|
|
|
|
Recursive: true,
|
|
|
|
RegularOnly: false,
|
|
|
|
Size: 0,
|
|
|
|
MTime: internal.Duration{Duration: 0},
|
|
|
|
fileFilters: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:55:23 +00:00
|
|
|
func getTestdataDir(dir string) string {
|
2018-07-31 22:05:55 +00:00
|
|
|
_, filename, _, _ := runtime.Caller(1)
|
2018-10-05 19:55:23 +00:00
|
|
|
return strings.Replace(filename, "filecount_test.go", dir, 1)
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 19:55:23 +00:00
|
|
|
func assertFileCount(acc *testutil.Accumulator, expectedDir string, expectedCount int) bool {
|
|
|
|
tags := map[string]string{"directory": getTestdataDir(expectedDir)}
|
2018-07-31 22:05:55 +00:00
|
|
|
return acc.HasPoint("filecount", tags, "count", int64(expectedCount))
|
|
|
|
}
|