0.3.0 unit tests: aerospike, apache, bcache

This commit is contained in:
Cameron Sparr 2016-01-05 17:28:15 -07:00
parent 498482d0f6
commit 2e20fc413c
8 changed files with 139 additions and 160 deletions

View File

@ -4,7 +4,6 @@ import (
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"reflect"
"testing" "testing"
) )
@ -31,7 +30,7 @@ func TestAerospikeStatistics(t *testing.T) {
} }
for _, metric := range asMetrics { for _, metric := range asMetrics {
assert.True(t, acc.HasIntValue(metric), metric) assert.True(t, acc.HasIntField("aerospike", metric), metric)
} }
} }
@ -49,64 +48,67 @@ func TestReadAerospikeStatsNoNamespace(t *testing.T) {
"stat_read_reqs": "12345", "stat_read_reqs": "12345",
} }
readAerospikeStats(stats, &acc, "host1", "") readAerospikeStats(stats, &acc, "host1", "")
for k := range stats {
if k == "stat-write-errs" {
k = "stat_write_errs"
}
assert.True(t, acc.HasMeasurement(k))
assert.True(t, acc.CheckValue(k, int64(12345)))
}
}
func TestReadAerospikeStatsNamespace(t *testing.T) { fields := map[string]interface{}{
var acc testutil.Accumulator "stat_write_errs": int64(12345),
stats := map[string]string{ "stat_read_reqs": int64(12345),
"stat_write_errs": "12345",
"stat_read_reqs": "12345",
} }
readAerospikeStats(stats, &acc, "host1", "test")
tags := map[string]string{ tags := map[string]string{
"aerospike_host": "host1", "aerospike_host": "host1",
"namespace": "test", "namespace": "_service",
}
for k := range stats {
assert.True(t, acc.ValidateTaggedValue(k, int64(12345), tags) == nil)
} }
acc.AssertContainsTaggedFields(t, "aerospike", fields, tags)
} }
func TestAerospikeUnmarshalList(t *testing.T) { // func TestReadAerospikeStatsNamespace(t *testing.T) {
i := map[string]string{ // var acc testutil.Accumulator
"test": "one;two;three", // stats := map[string]string{
} // "stat_write_errs": "12345",
// "stat_read_reqs": "12345",
// }
// readAerospikeStats(stats, &acc, "host1", "test")
expected := []string{"one", "two", "three"} // tags := map[string]string{
// "aerospike_host": "host1",
// "namespace": "test",
// }
// for k := range stats {
// assert.True(t, acc.ValidateTaggedValue(k, int64(12345), tags) == nil)
// }
// }
list, err := unmarshalListInfo(i, "test2") // func TestAerospikeUnmarshalList(t *testing.T) {
assert.True(t, err != nil) // i := map[string]string{
// "test": "one;two;three",
// }
list, err = unmarshalListInfo(i, "test") // expected := []string{"one", "two", "three"}
assert.True(t, err == nil)
equal := true
for ix := range expected {
if list[ix] != expected[ix] {
equal = false
break
}
}
assert.True(t, equal)
}
func TestAerospikeUnmarshalMap(t *testing.T) { // list, err := unmarshalListInfo(i, "test2")
i := map[string]string{ // assert.True(t, err != nil)
"test": "key1=value1;key2=value2",
}
expected := map[string]string{ // list, err = unmarshalListInfo(i, "test")
"key1": "value1", // assert.True(t, err == nil)
"key2": "value2", // equal := true
} // for ix := range expected {
m, err := unmarshalMapInfo(i, "test") // if list[ix] != expected[ix] {
assert.True(t, err == nil) // equal = false
assert.True(t, reflect.DeepEqual(m, expected)) // break
} // }
// }
// assert.True(t, equal)
// }
// func TestAerospikeUnmarshalMap(t *testing.T) {
// i := map[string]string{
// "test": "key1=value1;key2=value2",
// }
// expected := map[string]string{
// "key1": "value1",
// "key2": "value2",
// }
// m, err := unmarshalMapInfo(i, "test")
// assert.True(t, err == nil)
// assert.True(t, reflect.DeepEqual(m, expected))
// }

View File

@ -8,7 +8,6 @@ import (
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -44,37 +43,31 @@ func TestHTTPApache(t *testing.T) {
err := a.Gather(&acc) err := a.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
testInt := []struct { fields := map[string]interface{}{
measurement string "TotalAccesses": float64(1.29811861e+08),
value float64 "TotalkBytes": float64(5.213701865e+09),
}{ "CPULoad": float64(6.51929),
{"TotalAccesses", 1.29811861e+08}, "Uptime": float64(941553),
{"TotalkBytes", 5.213701865e+09}, "ReqPerSec": float64(137.87),
{"CPULoad", 6.51929}, "BytesPerSec": float64(5.67024e+06),
{"Uptime", 941553}, "BytesPerReq": float64(41127.4),
{"ReqPerSec", 137.87}, "BusyWorkers": float64(270),
{"BytesPerSec", 5.67024e+06}, "IdleWorkers": float64(630),
{"BytesPerReq", 41127.4}, "ConnsTotal": float64(1451),
{"BusyWorkers", 270}, "ConnsAsyncWriting": float64(32),
{"IdleWorkers", 630}, "ConnsAsyncKeepAlive": float64(945),
{"ConnsTotal", 1451}, "ConnsAsyncClosing": float64(205),
{"ConnsAsyncWriting", 32}, "scboard_waiting": float64(630),
{"ConnsAsyncKeepAlive", 945}, "scboard_starting": float64(0),
{"ConnsAsyncClosing", 205}, "scboard_reading": float64(157),
{"scboard_waiting", 630}, "scboard_sending": float64(113),
{"scboard_starting", 0}, "scboard_keepalive": float64(0),
{"scboard_reading", 157}, "scboard_dnslookup": float64(0),
{"scboard_sending", 113}, "scboard_closing": float64(0),
{"scboard_keepalive", 0}, "scboard_logging": float64(0),
{"scboard_dnslookup", 0}, "scboard_finishing": float64(0),
{"scboard_closing", 0}, "scboard_idle_cleanup": float64(0),
{"scboard_logging", 0}, "scboard_open": float64(2850),
{"scboard_finishing", 0},
{"scboard_idle_cleanup", 0},
{"scboard_open", 2850},
}
for _, test := range testInt {
assert.True(t, acc.CheckValue(test.measurement, test.value))
} }
acc.AssertContainsFields(t, "apache", fields)
} }

View File

@ -6,7 +6,6 @@ import (
"testing" "testing"
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -29,11 +28,6 @@ var (
testBcacheBackingDevPath = os.TempDir() + "/telegraf/sys/devices/virtual/block/md10" testBcacheBackingDevPath = os.TempDir() + "/telegraf/sys/devices/virtual/block/md10"
) )
type metrics struct {
name string
value uint64
}
func TestBcacheGeneratesMetrics(t *testing.T) { func TestBcacheGeneratesMetrics(t *testing.T) {
err := os.MkdirAll(testBcacheUuidPath, 0755) err := os.MkdirAll(testBcacheUuidPath, 0755)
require.NoError(t, err) require.NoError(t, err)
@ -53,70 +47,52 @@ func TestBcacheGeneratesMetrics(t *testing.T) {
err = os.MkdirAll(testBcacheUuidPath+"/bdev0/stats_total", 0755) err = os.MkdirAll(testBcacheUuidPath+"/bdev0/stats_total", 0755)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/dirty_data", []byte(dirty_data), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/dirty_data",
[]byte(dirty_data), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/bypassed", []byte(bypassed), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/bypassed",
[]byte(bypassed), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_bypass_hits", []byte(cache_bypass_hits), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_bypass_hits",
[]byte(cache_bypass_hits), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_bypass_misses", []byte(cache_bypass_misses), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_bypass_misses",
[]byte(cache_bypass_misses), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_hit_ratio", []byte(cache_hit_ratio), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_hit_ratio",
[]byte(cache_hit_ratio), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_hits", []byte(cache_hits), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_hits",
[]byte(cache_hits), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_miss_collisions", []byte(cache_miss_collisions), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_miss_collisions",
[]byte(cache_miss_collisions), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_misses", []byte(cache_misses), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_misses",
[]byte(cache_misses), 0644)
require.NoError(t, err) require.NoError(t, err)
err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_readaheads", []byte(cache_readaheads), 0644) err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_readaheads",
[]byte(cache_readaheads), 0644)
require.NoError(t, err) require.NoError(t, err)
intMetrics := []*metrics{ fields := map[string]interface{}{
{ "dirty_data": uint64(1610612736),
name: "dirty_data", "bypassed": uint64(5167704440832),
value: 1610612736, "cache_bypass_hits": uint64(146155333),
}, "cache_bypass_misses": uint64(0),
{ "cache_hit_ratio": uint64(90),
name: "bypassed", "cache_hits": uint64(511469583),
value: 5167704440832, "cache_miss_collisions": uint64(157567),
}, "cache_misses": uint64(50616331),
{ "cache_readaheads": uint64(2),
name: "cache_bypass_hits",
value: 146155333,
},
{
name: "cache_bypass_misses",
value: 0,
},
{
name: "cache_hit_ratio",
value: 90,
},
{
name: "cache_hits",
value: 511469583,
},
{
name: "cache_miss_collisions",
value: 157567,
},
{
name: "cache_misses",
value: 50616331,
},
{
name: "cache_readaheads",
value: 2,
},
} }
tags := map[string]string{ tags := map[string]string{
@ -131,22 +107,14 @@ func TestBcacheGeneratesMetrics(t *testing.T) {
err = b.Gather(&acc) err = b.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
acc.AssertContainsTaggedFields(t, "bcache", fields, tags)
for _, metric := range intMetrics {
assert.True(t, acc.HasUIntValue(metric.name), metric.name)
assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
}
// one exist dev // one exist dev
b = &Bcache{BcachePath: testBcachePath, BcacheDevs: []string{"bcache0"}} b = &Bcache{BcachePath: testBcachePath, BcacheDevs: []string{"bcache0"}}
err = b.Gather(&acc) err = b.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
acc.AssertContainsTaggedFields(t, "bcache", fields, tags)
for _, metric := range intMetrics {
assert.True(t, acc.HasUIntValue(metric.name), metric.name)
assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
}
err = os.RemoveAll(os.TempDir() + "/telegraf") err = os.RemoveAll(os.TempDir() + "/telegraf")
require.NoError(t, err) require.NoError(t, err)

View File

@ -68,8 +68,8 @@ func TestDiskStats(t *testing.T) {
"inodes_free": uint64(468), //tags2) "inodes_free": uint64(468), //tags2)
"inodes_used": uint64(2000), //tags2) "inodes_used": uint64(2000), //tags2)
} }
acc.AssertContainsFields(t, "disk", fields1, tags1) acc.AssertContainsTaggedFields(t, "disk", fields1, tags1)
acc.AssertContainsFields(t, "disk", fields2, tags2) acc.AssertContainsTaggedFields(t, "disk", fields2, tags2)
// We expect 6 more DiskPoints to show up with an explicit match on "/" // We expect 6 more DiskPoints to show up with an explicit match on "/"
// and /home not matching the /dev in Mountpoints // and /home not matching the /dev in Mountpoints

View File

@ -116,5 +116,5 @@ func TestDockerStats_GenerateStats(t *testing.T) {
"total_unevictable": uint64(27), "total_unevictable": uint64(27),
} }
acc.AssertContainsFields(t, "docker", fields, dockertags) acc.AssertContainsTaggedFields(t, "docker", fields, dockertags)
} }

View File

@ -53,7 +53,7 @@ func TestMemStats(t *testing.T) {
"cached": uint64(0), "cached": uint64(0),
"buffered": uint64(0), "buffered": uint64(0),
} }
acc.AssertContainsFields(t, "mem", memfields, nil) acc.AssertContainsTaggedFields(t, "mem", memfields, make(map[string]string))
acc.Points = nil acc.Points = nil
@ -68,5 +68,5 @@ func TestMemStats(t *testing.T) {
"in": uint64(7), "in": uint64(7),
"out": uint64(830), "out": uint64(830),
} }
acc.AssertContainsFields(t, "swap", swapfields, nil) acc.AssertContainsTaggedFields(t, "swap", swapfields, make(map[string]string))
} }

View File

@ -74,13 +74,13 @@ func TestNetStats(t *testing.T) {
"drop_in": uint64(7), "drop_in": uint64(7),
"drop_out": uint64(1), "drop_out": uint64(1),
} }
acc.AssertContainsFields(t, "net", fields1, ntags) acc.AssertContainsTaggedFields(t, "net", fields1, ntags)
fields2 := map[string]interface{}{ fields2 := map[string]interface{}{
"udp_noports": int64(892592), "udp_noports": int64(892592),
"udp_indatagrams": int64(4655), "udp_indatagrams": int64(4655),
} }
acc.AssertContainsFields(t, "net", fields2, nil) acc.AssertContainsTaggedFields(t, "net", fields2, make(map[string]string))
acc.Points = nil acc.Points = nil
@ -102,5 +102,5 @@ func TestNetStats(t *testing.T) {
"tcp_none": 0, "tcp_none": 0,
"udp_socket": 1, "udp_socket": 1,
} }
acc.AssertContainsFields(t, "netstat", fields3, nil) acc.AssertContainsTaggedFields(t, "netstat", fields3, make(map[string]string))
} }

View File

@ -121,15 +121,12 @@ func (a *Accumulator) NFields() int {
return counter return counter
} }
func (a *Accumulator) AssertContainsFields( func (a *Accumulator) AssertContainsTaggedFields(
t *testing.T, t *testing.T,
measurement string, measurement string,
fields map[string]interface{}, fields map[string]interface{},
tags map[string]string, tags map[string]string,
) { ) {
if tags == nil {
tags = make(map[string]string)
}
for _, p := range a.Points { for _, p := range a.Points {
if !reflect.DeepEqual(tags, p.Tags) { if !reflect.DeepEqual(tags, p.Tags) {
continue continue
@ -148,6 +145,25 @@ func (a *Accumulator) AssertContainsFields(
assert.Fail(t, msg) assert.Fail(t, msg)
} }
func (a *Accumulator) AssertContainsFields(
t *testing.T,
measurement string,
fields map[string]interface{},
) {
for _, p := range a.Points {
if p.Measurement == measurement {
if !reflect.DeepEqual(fields, p.Fields) {
msg := fmt.Sprintf("Actual:\n %v (%T) \nExpected:\n %v (%T)",
p.Fields, p.Fields, fields, fields)
assert.Fail(t, msg)
}
return
}
}
msg := fmt.Sprintf("unknown measurement %s", measurement)
assert.Fail(t, msg)
}
// HasIntValue returns true if the measurement has an Int value // HasIntValue returns true if the measurement has an Int value
func (a *Accumulator) HasIntField(measurement string, field string) bool { func (a *Accumulator) HasIntField(measurement string, field string) bool {
for _, p := range a.Points { for _, p := range a.Points {