Skip to content

Commit

Permalink
Add HasPoint method to testutil.Accumulator
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson committed Oct 9, 2017
1 parent 0f452ad commit 761544f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 48 deletions.
92 changes: 44 additions & 48 deletions plugins/inputs/filestat/filestat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
)
Expand All @@ -24,28 +26,24 @@ func TestGatherNoMd5(t *testing.T) {
tags1 := map[string]string{
"file": dir + "log1.log",
}
fields1 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
}
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1)
require.True(t, acc.HasPoint("filestat", tags1,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1,
"exists", int64(1)))

tags2 := map[string]string{
"file": dir + "log2.log",
}
fields2 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
}
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2)
require.True(t, acc.HasPoint("filestat", tags2,
"size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2,
"exists", int64(1)))

tags3 := map[string]string{
"file": "/non/existant/file",
}
fields3 := map[string]interface{}{
"exists": int64(0),
}
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3)
require.True(t, acc.HasPoint("filestat", tags3,
"exists", int64(0)))
}

func TestGatherExplicitFiles(t *testing.T) {
Expand All @@ -64,30 +62,28 @@ func TestGatherExplicitFiles(t *testing.T) {
tags1 := map[string]string{
"file": dir + "log1.log",
}
fields1 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e",
}
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1)
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"))

tags2 := map[string]string{
"file": dir + "log2.log",
}
fields2 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e",
}
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2)
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"))

tags3 := map[string]string{
"file": "/non/existant/file",
}
fields3 := map[string]interface{}{
"exists": int64(0),
}
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3)
require.True(t, acc.HasPoint("filestat", tags3,
"exists", int64(0)))
}

func TestGatherGlob(t *testing.T) {
Expand Down Expand Up @@ -136,32 +132,32 @@ func TestGatherSuperAsterisk(t *testing.T) {
tags1 := map[string]string{
"file": dir + "log1.log",
}
fields1 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e",
}
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1)
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"))

tags2 := map[string]string{
"file": dir + "log2.log",
}
fields2 := map[string]interface{}{
"size_bytes": int64(0),
"exists": int64(1),
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e",
}
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2)
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"))

tags3 := map[string]string{
"file": dir + "test.conf",
}
fields3 := map[string]interface{}{
"size_bytes": int64(104),
"exists": int64(1),
"md5_sum": "5a7e9b77fa25e7bb411dbd17cf403c1f",
}
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3)
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 TestGetMd5(t *testing.T) {
Expand Down
25 changes: 25 additions & 0 deletions testutil/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,31 @@ func (a *Accumulator) AssertContainsFields(
assert.Fail(t, msg)
}

func (a *Accumulator) HasPoint(
measurement string,
tags map[string]string,
fieldKey string,
fieldValue interface{},
) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
if p.Measurement != measurement {
continue
}

if !reflect.DeepEqual(tags, p.Tags) {
continue
}

v, ok := p.Fields[fieldKey]
if ok && reflect.DeepEqual(v, fieldValue) {
return true
}
}
return false
}

func (a *Accumulator) AssertDoesNotContainMeasurement(t *testing.T, measurement string) {
a.Lock()
defer a.Unlock()
Expand Down

0 comments on commit 761544f

Please sign in to comment.