2016-04-21 01:51:25 +00:00
|
|
|
package filestat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-10-09 22:02:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2016-04-21 01:51:25 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGatherNoMd5(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
|
|
|
fs := NewFileStat()
|
2019-09-23 22:39:50 +00:00
|
|
|
fs.Log = testutil.Logger{}
|
2016-04-21 01:51:25 +00:00
|
|
|
fs.Files = []string{
|
|
|
|
dir + "log1.log",
|
|
|
|
dir + "log2.log",
|
|
|
|
"/non/existant/file",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.GatherError(fs.Gather)
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"file": dir + "log1.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags2 := map[string]string{
|
|
|
|
"file": dir + "log2.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags3 := map[string]string{
|
|
|
|
"file": "/non/existant/file",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0)))
|
2016-04-21 01:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherExplicitFiles(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
|
|
|
fs := NewFileStat()
|
2019-09-23 22:39:50 +00:00
|
|
|
fs.Log = testutil.Logger{}
|
2016-04-21 01:51:25 +00:00
|
|
|
fs.Md5 = true
|
|
|
|
fs.Files = []string{
|
|
|
|
dir + "log1.log",
|
|
|
|
dir + "log2.log",
|
|
|
|
"/non/existant/file",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.GatherError(fs.Gather)
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"file": dir + "log1.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags2 := map[string]string{
|
|
|
|
"file": dir + "log2.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags3 := map[string]string{
|
|
|
|
"file": "/non/existant/file",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0)))
|
2016-04-21 01:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherGlob(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
|
|
|
fs := NewFileStat()
|
2019-09-23 22:39:50 +00:00
|
|
|
fs.Log = testutil.Logger{}
|
2016-04-21 01:51:25 +00:00
|
|
|
fs.Md5 = true
|
|
|
|
fs.Files = []string{
|
|
|
|
dir + "*.log",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.GatherError(fs.Gather)
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"file": dir + "log1.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags2 := map[string]string{
|
|
|
|
"file": dir + "log2.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
|
2016-04-21 01:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherSuperAsterisk(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
|
|
|
fs := NewFileStat()
|
2019-09-23 22:39:50 +00:00
|
|
|
fs.Log = testutil.Logger{}
|
2016-04-21 01:51:25 +00:00
|
|
|
fs.Md5 = true
|
|
|
|
fs.Files = []string{
|
|
|
|
dir + "**",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.GatherError(fs.Gather)
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"file": dir + "log1.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags2 := map[string]string{
|
|
|
|
"file": dir + "log2.log",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
|
2016-04-21 01:51:25 +00:00
|
|
|
|
|
|
|
tags3 := map[string]string{
|
|
|
|
"file": dir + "test.conf",
|
|
|
|
}
|
2017-11-07 22:32:48 +00:00
|
|
|
require.True(t, acc.HasPoint("filestat", tags3, "size_bytes", int64(104)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags3, "md5_sum", "5a7e9b77fa25e7bb411dbd17cf403c1f"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModificationTime(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
|
|
|
fs := NewFileStat()
|
2019-09-23 22:39:50 +00:00
|
|
|
fs.Log = testutil.Logger{}
|
2017-11-07 22:32:48 +00:00
|
|
|
fs.Files = []string{
|
|
|
|
dir + "log1.log",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fs.Gather)
|
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"file": dir + "log1.log",
|
|
|
|
}
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
|
|
|
|
require.True(t, acc.HasInt64Field("filestat", "modification_time"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoModificationTime(t *testing.T) {
|
|
|
|
fs := NewFileStat()
|
2019-09-23 22:39:50 +00:00
|
|
|
fs.Log = testutil.Logger{}
|
2017-11-07 22:32:48 +00:00
|
|
|
fs.Files = []string{
|
|
|
|
"/non/existant/file",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.GatherError(fs.Gather)
|
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"file": "/non/existant/file",
|
|
|
|
}
|
|
|
|
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(0)))
|
|
|
|
require.False(t, acc.HasInt64Field("filestat", "modification_time"))
|
2016-04-21 01:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMd5(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
|
|
|
md5, err := getMd5(dir + "test.conf")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "5a7e9b77fa25e7bb411dbd17cf403c1f", md5)
|
|
|
|
|
|
|
|
md5, err = getMd5("/tmp/foo/bar/fooooo")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestdataDir() string {
|
|
|
|
_, filename, _, _ := runtime.Caller(1)
|
|
|
|
return strings.Replace(filename, "filestat_test.go", "testdata/", 1)
|
|
|
|
}
|