From 3db36f7edc6697ac3d7b0128ffd92f7c2179d119 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Fri, 6 Sep 2019 12:38:20 -0700 Subject: [PATCH] Fix filecount for paths with trailing slash (#6332) (cherry picked from commit 7ac5dc541683ecf86fa5dbe8100a1bea847948fc) --- plugins/inputs/filecount/filecount.go | 4 +-- plugins/inputs/filecount/filecount_test.go | 33 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/filecount/filecount.go b/plugins/inputs/filecount/filecount.go index 929ec66a7f6f2..965f41d2cc380 100644 --- a/plugins/inputs/filecount/filecount.go +++ b/plugins/inputs/filecount/filecount.go @@ -267,11 +267,11 @@ func (fc *FileCount) onlyDirectories(directories []string) []string { func (fc *FileCount) getDirs() []string { dirs := make([]string, len(fc.Directories)) for i, dir := range fc.Directories { - dirs[i] = dir + dirs[i] = filepath.Clean(dir) } if fc.Directory != "" { - dirs = append(dirs, fc.Directory) + dirs = append(dirs, filepath.Clean(fc.Directory)) } return dirs diff --git a/plugins/inputs/filecount/filecount_test.go b/plugins/inputs/filecount/filecount_test.go index 99213104b1a89..9cd7c747cfb61 100644 --- a/plugins/inputs/filecount/filecount_test.go +++ b/plugins/inputs/filecount/filecount_test.go @@ -2,11 +2,13 @@ package filecount import ( "os" + "path/filepath" "runtime" "strings" "testing" "time" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" @@ -117,6 +119,37 @@ func TestMTimeFilter(t *testing.T) { fileCountEquals(t, fc, len(matches), 0) } +// 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), + ), + } + + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) +} + func getNoFilterFileCount() FileCount { return FileCount{ Directories: []string{getTestdataDir()},