2018-07-31 22:05:55 +00:00
|
|
|
package filecount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-09-06 19:38:20 +00:00
|
|
|
"path/filepath"
|
2018-07-31 22:05:55 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-09-06 19:38:20 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-07-31 22:05:55 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoFilters(t *testing.T) {
|
2018-12-13 20:25:49 +00:00
|
|
|
fc := getNoFilterFileCount()
|
|
|
|
matches := []string{"foo", "bar", "baz", "qux",
|
|
|
|
"subdir/", "subdir/quux", "subdir/quuz",
|
|
|
|
"subdir/nested2", "subdir/nested2/qux"}
|
2019-06-24 18:03:05 +00:00
|
|
|
fileCountEquals(t, fc, len(matches), 5096)
|
2018-12-13 20:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoFiltersOnChildDir(t *testing.T) {
|
|
|
|
fc := getNoFilterFileCount()
|
|
|
|
fc.Directories = []string{getTestdataDir() + "/*"}
|
|
|
|
matches := []string{"subdir/quux", "subdir/quuz",
|
|
|
|
"subdir/nested2/qux", "subdir/nested2"}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
tags := map[string]string{"directory": getTestdataDir() + "/subdir"}
|
2018-10-05 19:55:23 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
2018-12-13 20:25:49 +00:00
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "count", int64(len(matches))))
|
2019-06-24 18:03:05 +00:00
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "size_bytes", int64(600)))
|
2018-10-05 19:55:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
func TestNoRecursiveButSuperMeta(t *testing.T) {
|
|
|
|
fc := getNoFilterFileCount()
|
|
|
|
fc.Recursive = false
|
|
|
|
fc.Directories = []string{getTestdataDir() + "/**"}
|
|
|
|
matches := []string{"subdir/quux", "subdir/quuz", "subdir/nested2"}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
tags := map[string]string{"directory": getTestdataDir() + "/subdir"}
|
2018-10-05 19:55:23 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "count", int64(len(matches))))
|
2019-06-24 18:03:05 +00:00
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "size_bytes", int64(200)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameFilter(t *testing.T) {
|
2018-12-13 20:25:49 +00:00
|
|
|
fc := getNoFilterFileCount()
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.Name = "ba*"
|
|
|
|
matches := []string{"bar", "baz"}
|
2018-12-13 20:25:49 +00:00
|
|
|
fileCountEquals(t, fc, len(matches), 0)
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonRecursive(t *testing.T) {
|
2018-12-13 20:25:49 +00:00
|
|
|
fc := getNoFilterFileCount()
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.Recursive = false
|
|
|
|
matches := []string{"foo", "bar", "baz", "qux", "subdir"}
|
2019-06-24 18:03:05 +00:00
|
|
|
|
|
|
|
fileCountEquals(t, fc, len(matches), 4496)
|
2018-12-13 20:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoubleAndSimpleStar(t *testing.T) {
|
|
|
|
fc := getNoFilterFileCount()
|
|
|
|
fc.Directories = []string{getTestdataDir() + "/**/*"}
|
|
|
|
matches := []string{"qux"}
|
2019-06-24 18:03:05 +00:00
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
tags := map[string]string{"directory": getTestdataDir() + "/subdir/nested2"}
|
2018-10-05 19:55:23 +00:00
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "count", int64(len(matches))))
|
2019-06-24 18:03:05 +00:00
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "size_bytes", int64(400)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegularOnlyFilter(t *testing.T) {
|
2018-12-13 20:25:49 +00:00
|
|
|
fc := getNoFilterFileCount()
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.RegularOnly = true
|
|
|
|
matches := []string{
|
|
|
|
"foo", "bar", "baz", "qux", "subdir/quux", "subdir/quuz",
|
2018-12-13 20:25:49 +00:00
|
|
|
"subdir/nested2/qux"}
|
2019-06-24 18:03:05 +00:00
|
|
|
|
|
|
|
fileCountEquals(t, fc, len(matches), 800)
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSizeFilter(t *testing.T) {
|
2018-12-13 20:25:49 +00:00
|
|
|
fc := getNoFilterFileCount()
|
2018-10-19 18:17:18 +00:00
|
|
|
fc.Size = internal.Size{Size: -100}
|
2018-12-13 20:25:49 +00:00
|
|
|
matches := []string{"foo", "bar", "baz",
|
|
|
|
"subdir/quux", "subdir/quuz"}
|
|
|
|
fileCountEquals(t, fc, len(matches), 0)
|
2018-07-31 22:05:55 +00:00
|
|
|
|
2018-10-19 18:17:18 +00:00
|
|
|
fc.Size = internal.Size{Size: 100}
|
2018-12-13 20:25:49 +00:00
|
|
|
matches = []string{"qux", "subdir/nested2//qux"}
|
2019-06-24 18:03:05 +00:00
|
|
|
|
|
|
|
fileCountEquals(t, fc, len(matches), 800)
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMTimeFilter(t *testing.T) {
|
2019-06-24 18:03:05 +00:00
|
|
|
|
|
|
|
mtime := time.Date(2011, time.December, 14, 18, 25, 5, 0, time.UTC)
|
2018-07-31 22:05:55 +00:00
|
|
|
fileAge := time.Since(mtime) - (60 * time.Second)
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
fc := getNoFilterFileCount()
|
2018-07-31 22:05:55 +00:00
|
|
|
fc.MTime = internal.Duration{Duration: -fileAge}
|
2018-12-13 20:25:49 +00:00
|
|
|
matches := []string{"foo", "bar", "qux",
|
|
|
|
"subdir/", "subdir/quux", "subdir/quuz",
|
2019-06-24 18:03:05 +00:00
|
|
|
"subdir/nested2", "subdir/nested2/qux"}
|
|
|
|
|
|
|
|
fileCountEquals(t, fc, len(matches), 5096)
|
2018-07-31 22:05:55 +00:00
|
|
|
|
|
|
|
fc.MTime = internal.Duration{Duration: fileAge}
|
|
|
|
matches = []string{"baz"}
|
2018-12-13 20:25:49 +00:00
|
|
|
fileCountEquals(t, fc, len(matches), 0)
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:38:20 +00:00
|
|
|
// Paths with a trailing slash will not exactly match paths produced during the
|
|
|
|
// walk as these paths are cleaned before being returned from godirwalk. #6329
|
|
|
|
func TestDirectoryWithTrailingSlash(t *testing.T) {
|
|
|
|
plugin := &FileCount{
|
|
|
|
Directories: []string{getTestdataDir() + string(filepath.Separator)},
|
|
|
|
Name: "*",
|
|
|
|
Recursive: true,
|
|
|
|
Fs: getFakeFileSystem(getTestdataDir()),
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := plugin.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"filecount",
|
|
|
|
map[string]string{
|
|
|
|
"directory": getTestdataDir(),
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"count": 9,
|
|
|
|
"size_bytes": 5096,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
2019-11-27 01:31:36 +00:00
|
|
|
telegraf.Gauge,
|
2019-09-06 19:38:20 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
|
|
|
}
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
func getNoFilterFileCount() FileCount {
|
2018-07-31 22:05:55 +00:00
|
|
|
return FileCount{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2018-12-13 20:25:49 +00:00
|
|
|
Directories: []string{getTestdataDir()},
|
2018-07-31 22:05:55 +00:00
|
|
|
Name: "*",
|
|
|
|
Recursive: true,
|
|
|
|
RegularOnly: false,
|
2018-10-19 18:17:18 +00:00
|
|
|
Size: internal.Size{Size: 0},
|
2018-07-31 22:05:55 +00:00
|
|
|
MTime: internal.Duration{Duration: 0},
|
|
|
|
fileFilters: nil,
|
2019-06-24 18:03:05 +00:00
|
|
|
Fs: getFakeFileSystem(getTestdataDir()),
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
func getTestdataDir() string {
|
2019-06-24 18:03:05 +00:00
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
// if we cannot even establish the test directory, further progress is meaningless
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var chunks []string
|
|
|
|
var testDirectory string
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
chunks = strings.Split(dir, "\\")
|
|
|
|
testDirectory = strings.Join(chunks[:], "\\") + "\\testdata"
|
|
|
|
} else {
|
|
|
|
chunks = strings.Split(dir, "/")
|
|
|
|
testDirectory = strings.Join(chunks[:], "/") + "/testdata"
|
|
|
|
}
|
|
|
|
return testDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFakeFileSystem(basePath string) fakeFileSystem {
|
|
|
|
// create our desired "filesystem" object, complete with an internal map allowing our funcs to return meta data as requested
|
|
|
|
|
|
|
|
mtime := time.Date(2015, time.December, 14, 18, 25, 5, 0, time.UTC)
|
|
|
|
olderMtime := time.Date(2010, time.December, 14, 18, 25, 5, 0, time.UTC)
|
|
|
|
|
|
|
|
// set file permisions
|
|
|
|
var fmask uint32 = 0666
|
|
|
|
var dmask uint32 = 0666
|
|
|
|
|
|
|
|
// set directory bit
|
|
|
|
dmask |= (1 << uint(32-1))
|
|
|
|
|
|
|
|
// create a lookup map for getting "files" from the "filesystem"
|
|
|
|
fileList := map[string]fakeFileInfo{
|
|
|
|
basePath: {name: "testdata", size: int64(4096), filemode: uint32(dmask), modtime: mtime, isdir: true},
|
|
|
|
basePath + "/foo": {name: "foo", filemode: uint32(fmask), modtime: mtime},
|
|
|
|
basePath + "/bar": {name: "bar", filemode: uint32(fmask), modtime: mtime},
|
|
|
|
basePath + "/baz": {name: "baz", filemode: uint32(fmask), modtime: olderMtime},
|
|
|
|
basePath + "/qux": {name: "qux", size: int64(400), filemode: uint32(fmask), modtime: mtime},
|
|
|
|
basePath + "/subdir": {name: "subdir", size: int64(4096), filemode: uint32(dmask), modtime: mtime, isdir: true},
|
|
|
|
basePath + "/subdir/quux": {name: "quux", filemode: uint32(fmask), modtime: mtime},
|
|
|
|
basePath + "/subdir/quuz": {name: "quuz", filemode: uint32(fmask), modtime: mtime},
|
|
|
|
basePath + "/subdir/nested2": {name: "nested2", size: int64(200), filemode: uint32(dmask), modtime: mtime, isdir: true},
|
|
|
|
basePath + "/subdir/nested2/qux": {name: "qux", filemode: uint32(fmask), modtime: mtime, size: int64(400)},
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := fakeFileSystem{files: fileList}
|
|
|
|
return fs
|
|
|
|
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 20:25:49 +00:00
|
|
|
func fileCountEquals(t *testing.T, fc FileCount, expectedCount int, expectedSize int) {
|
|
|
|
tags := map[string]string{"directory": getTestdataDir()}
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fc.Gather)
|
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "count", int64(expectedCount)))
|
|
|
|
require.True(t, acc.HasPoint("filecount", tags, "size_bytes", int64(expectedSize)))
|
2018-07-31 22:05:55 +00:00
|
|
|
}
|