Add HasPoint method to testutil.Accumulator

This commit is contained in:
Daniel Nelson 2017-10-09 15:02:57 -07:00
parent 0f452ad0df
commit 761544f56d
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
2 changed files with 69 additions and 48 deletions

View File

@ -5,6 +5,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -24,28 +26,24 @@ func TestGatherNoMd5(t *testing.T) {
tags1 := map[string]string{ tags1 := map[string]string{
"file": dir + "log1.log", "file": dir + "log1.log",
} }
fields1 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags1,
"size_bytes": int64(0), "size_bytes", int64(0)))
"exists": int64(1), require.True(t, acc.HasPoint("filestat", tags1,
} "exists", int64(1)))
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1)
tags2 := map[string]string{ tags2 := map[string]string{
"file": dir + "log2.log", "file": dir + "log2.log",
} }
fields2 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags2,
"size_bytes": int64(0), "size_bytes", int64(0)))
"exists": int64(1), require.True(t, acc.HasPoint("filestat", tags2,
} "exists", int64(1)))
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2)
tags3 := map[string]string{ tags3 := map[string]string{
"file": "/non/existant/file", "file": "/non/existant/file",
} }
fields3 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags3,
"exists": int64(0), "exists", int64(0)))
}
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3)
} }
func TestGatherExplicitFiles(t *testing.T) { func TestGatherExplicitFiles(t *testing.T) {
@ -64,30 +62,28 @@ func TestGatherExplicitFiles(t *testing.T) {
tags1 := map[string]string{ tags1 := map[string]string{
"file": dir + "log1.log", "file": dir + "log1.log",
} }
fields1 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags1,
"size_bytes": int64(0), "size_bytes", int64(0)))
"exists": int64(1), require.True(t, acc.HasPoint("filestat", tags1,
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", "exists", int64(1)))
} require.True(t, acc.HasPoint("filestat", tags1,
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1) "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags2 := map[string]string{ tags2 := map[string]string{
"file": dir + "log2.log", "file": dir + "log2.log",
} }
fields2 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags2,
"size_bytes": int64(0), "size_bytes", int64(0)))
"exists": int64(1), require.True(t, acc.HasPoint("filestat", tags2,
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", "exists", int64(1)))
} require.True(t, acc.HasPoint("filestat", tags2,
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2) "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags3 := map[string]string{ tags3 := map[string]string{
"file": "/non/existant/file", "file": "/non/existant/file",
} }
fields3 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags3,
"exists": int64(0), "exists", int64(0)))
}
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3)
} }
func TestGatherGlob(t *testing.T) { func TestGatherGlob(t *testing.T) {
@ -136,32 +132,32 @@ func TestGatherSuperAsterisk(t *testing.T) {
tags1 := map[string]string{ tags1 := map[string]string{
"file": dir + "log1.log", "file": dir + "log1.log",
} }
fields1 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags1,
"size_bytes": int64(0), "size_bytes", int64(0)))
"exists": int64(1), require.True(t, acc.HasPoint("filestat", tags1,
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", "exists", int64(1)))
} require.True(t, acc.HasPoint("filestat", tags1,
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1) "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags2 := map[string]string{ tags2 := map[string]string{
"file": dir + "log2.log", "file": dir + "log2.log",
} }
fields2 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags2,
"size_bytes": int64(0), "size_bytes", int64(0)))
"exists": int64(1), require.True(t, acc.HasPoint("filestat", tags2,
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", "exists", int64(1)))
} require.True(t, acc.HasPoint("filestat", tags2,
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2) "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags3 := map[string]string{ tags3 := map[string]string{
"file": dir + "test.conf", "file": dir + "test.conf",
} }
fields3 := map[string]interface{}{ require.True(t, acc.HasPoint("filestat", tags3,
"size_bytes": int64(104), "size_bytes", int64(104)))
"exists": int64(1), require.True(t, acc.HasPoint("filestat", tags3,
"md5_sum": "5a7e9b77fa25e7bb411dbd17cf403c1f", "exists", int64(1)))
} require.True(t, acc.HasPoint("filestat", tags3,
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3) "md5_sum", "5a7e9b77fa25e7bb411dbd17cf403c1f"))
} }
func TestGetMd5(t *testing.T) { func TestGetMd5(t *testing.T) {

View File

@ -297,6 +297,31 @@ func (a *Accumulator) AssertContainsFields(
assert.Fail(t, msg) 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) { func (a *Accumulator) AssertDoesNotContainMeasurement(t *testing.T, measurement string) {
a.Lock() a.Lock()
defer a.Unlock() defer a.Unlock()