2018-07-14 06:22:59 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefreshFilePaths(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
r := File{
|
2018-08-17 20:45:22 +00:00
|
|
|
Files: []string{filepath.Join(wd, "dev/testfiles/**.log")},
|
2018-07-14 06:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = r.refreshFilePaths()
|
|
|
|
require.NoError(t, err)
|
2018-08-17 20:45:22 +00:00
|
|
|
assert.Equal(t, 2, len(r.filenames))
|
2018-07-14 06:22:59 +00:00
|
|
|
}
|
2019-11-13 21:00:41 +00:00
|
|
|
|
|
|
|
func TestFileTag(t *testing.T) {
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
r := File{
|
|
|
|
Files: []string{filepath.Join(wd, "dev/testfiles/json_a.log")},
|
|
|
|
FileTag: "filename",
|
|
|
|
}
|
|
|
|
|
|
|
|
parserConfig := parsers.Config{
|
|
|
|
DataFormat: "json",
|
|
|
|
}
|
|
|
|
nParser, err := parsers.NewParser(&parserConfig)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.parser = nParser
|
|
|
|
|
|
|
|
err = r.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, m := range acc.Metrics {
|
|
|
|
for key, value := range m.Tags {
|
|
|
|
assert.Equal(t, r.FileTag, key)
|
|
|
|
assert.Equal(t, filepath.Base(r.Files[0]), value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 06:22:59 +00:00
|
|
|
func TestJSONParserCompile(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
r := File{
|
2018-08-17 20:45:22 +00:00
|
|
|
Files: []string{filepath.Join(wd, "dev/testfiles/json_a.log")},
|
2018-07-14 06:22:59 +00:00
|
|
|
}
|
|
|
|
parserConfig := parsers.Config{
|
|
|
|
DataFormat: "json",
|
|
|
|
TagKeys: []string{"parent_ignored_child"},
|
|
|
|
}
|
|
|
|
nParser, err := parsers.NewParser(&parserConfig)
|
|
|
|
assert.NoError(t, err)
|
2018-08-17 20:45:22 +00:00
|
|
|
r.parser = nParser
|
2018-07-14 06:22:59 +00:00
|
|
|
|
|
|
|
r.Gather(&acc)
|
|
|
|
assert.Equal(t, map[string]string{"parent_ignored_child": "hi"}, acc.Metrics[0].Tags)
|
|
|
|
assert.Equal(t, 5, len(acc.Metrics[0].Fields))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrokParser(t *testing.T) {
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
r := File{
|
2018-08-17 20:45:22 +00:00
|
|
|
Files: []string{filepath.Join(wd, "dev/testfiles/grok_a.log")},
|
2018-07-14 06:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parserConfig := parsers.Config{
|
|
|
|
DataFormat: "grok",
|
|
|
|
GrokPatterns: []string{"%{COMMON_LOG_FORMAT}"},
|
|
|
|
}
|
|
|
|
|
|
|
|
nParser, err := parsers.NewParser(&parserConfig)
|
|
|
|
r.parser = nParser
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.Gather(&acc)
|
2018-08-17 20:45:22 +00:00
|
|
|
assert.Equal(t, len(acc.Metrics), 2)
|
2018-07-14 06:22:59 +00:00
|
|
|
}
|