2018-06-21 18:26:14 +00:00
|
|
|
package reader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefreshFilePaths(t *testing.T) {
|
|
|
|
testDir := getPluginDir()
|
|
|
|
r := Reader{
|
|
|
|
Filepaths: []string{testDir + "/logparser/grok/testdata/**.log"},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.refreshFilePaths()
|
|
|
|
//log.Printf("filenames: %v", filenames)
|
|
|
|
assert.Equal(t, len(r.Filenames), 2)
|
|
|
|
}
|
|
|
|
func TestJSONParserCompile(t *testing.T) {
|
|
|
|
testDir := getPluginDir()
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
r := Reader{
|
2018-06-21 23:23:06 +00:00
|
|
|
Filepaths: []string{testDir + "/reader/testfiles/json_a.log"},
|
2018-06-21 18:26:14 +00:00
|
|
|
DataFormat: "json",
|
|
|
|
Tags: []string{"parent_ignored_child"},
|
|
|
|
}
|
|
|
|
r.compileParser()
|
|
|
|
r.Gather(&acc)
|
|
|
|
log.Printf("acc: %v", acc.Metrics[0].Tags)
|
|
|
|
assert.Equal(t, map[string]string{"parent_ignored_child": "hi"}, acc.Metrics[0].Tags)
|
|
|
|
assert.Equal(t, 5, len(acc.Metrics[0].Fields))
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:56:20 +00:00
|
|
|
func TestGrokParser(t *testing.T) {
|
|
|
|
testDir := getPluginDir()
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
r := Reader{
|
|
|
|
Filepaths: []string{testDir + "/reader/testfiles/grok_a.log"},
|
|
|
|
DataFormat: "grok",
|
|
|
|
Patterns: []string{"%{COMMON_LOG_FORMAT}"},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.compileParser()
|
|
|
|
err := r.Gather(&acc)
|
|
|
|
log.Printf("err: %v", err)
|
|
|
|
log.Printf("metric[0]_tags: %v, metric[0]_fields: %v", acc.Metrics[0].Tags, acc.Metrics[0].Fields)
|
|
|
|
log.Printf("metric[1]_tags: %v, metric[1]_fields: %v", acc.Metrics[1].Tags, acc.Metrics[1].Fields)
|
2018-06-21 23:12:26 +00:00
|
|
|
assert.Equal(t, 2, len(acc.Metrics))
|
2018-06-21 22:56:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 18:26:14 +00:00
|
|
|
func getPluginDir() string {
|
|
|
|
_, filename, _, _ := runtime.Caller(1)
|
|
|
|
return strings.Replace(filename, "/reader/reader_test.go", "", 1)
|
|
|
|
}
|