Skip to content

Commit

Permalink
Fix filecount for paths with trailing slash (#6332)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7ac5dc5)
  • Loading branch information
danielnelson committed Sep 6, 2019
1 parent e6ce132 commit 3db36f7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugins/inputs/filecount/filecount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions plugins/inputs/filecount/filecount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()},
Expand Down

0 comments on commit 3db36f7

Please sign in to comment.