Add HasPoint method to testutil.Accumulator

This commit is contained in:
Daniel Nelson 2017-10-09 15:02:57 -07:00
parent abfeafd248
commit 6ed5dc444d
2 changed files with 69 additions and 48 deletions

View File

@ -5,6 +5,8 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
)
@ -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) {
@ -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) {
@ -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) {

View File

@ -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()