Fix filecount for paths with trailing slash (#6332)

This commit is contained in:
Daniel Nelson 2019-09-06 12:38:20 -07:00 committed by GitHub
parent 7d2cffe056
commit 7ac5dc5416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -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

View File

@ -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()},