0.3.0 unit tests: system plugins

This commit is contained in:
Cameron Sparr 2016-01-05 16:58:35 -07:00
parent a532d917b9
commit 68c4d1cea9
8 changed files with 311 additions and 353 deletions

View File

@ -1,6 +1,7 @@
package system package system
import ( import (
"fmt"
"testing" "testing"
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
@ -52,23 +53,19 @@ func TestCPUStats(t *testing.T) {
err := cs.Gather(&acc) err := cs.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
numCPUPoints := len(acc.Points)
expectedCPUPoints := 10
assert.Equal(t, expectedCPUPoints, numCPUPoints)
// Computed values are checked with delta > 0 becasue of floating point arithmatic // Computed values are checked with delta > 0 becasue of floating point arithmatic
// imprecision // imprecision
assertContainsTaggedFloat(t, &acc, "time_user", 3.1, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_user", 3.1, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_system", 8.2, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_system", 8.2, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_idle", 80.1, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_idle", 80.1, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_nice", 1.3, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_nice", 1.3, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_iowait", 0.2, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_iowait", 0.2, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_irq", 0.1, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_irq", 0.1, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_softirq", 0.11, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_softirq", 0.11, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_steal", 0.0511, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_steal", 0.0511, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_guest", 8.1, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_guest", 8.1, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_guest_nice", 0.324, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "time_guest_nice", 0.324, 0, cputags)
mps2 := MockPS{} mps2 := MockPS{}
mps2.On("CPUTimes").Return([]cpu.CPUTimesStat{cts2}, nil) mps2.On("CPUTimes").Return([]cpu.CPUTimesStat{cts2}, nil)
@ -78,29 +75,74 @@ func TestCPUStats(t *testing.T) {
err = cs.Gather(&acc) err = cs.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
numCPUPoints = len(acc.Points) - numCPUPoints assertContainsTaggedFloat(t, &acc, "cpu", "time_user", 11.4, 0, cputags)
expectedCPUPoints = 20 assertContainsTaggedFloat(t, &acc, "cpu", "time_system", 10.9, 0, cputags)
assert.Equal(t, expectedCPUPoints, numCPUPoints) assertContainsTaggedFloat(t, &acc, "cpu", "time_idle", 158.8699, 0, cputags)
assertContainsTaggedFloat(t, &acc, "cpu", "time_nice", 2.5, 0, cputags)
assertContainsTaggedFloat(t, &acc, "cpu", "time_iowait", 0.7, 0, cputags)
assertContainsTaggedFloat(t, &acc, "cpu", "time_irq", 1.2, 0, cputags)
assertContainsTaggedFloat(t, &acc, "cpu", "time_softirq", 0.31, 0, cputags)
assertContainsTaggedFloat(t, &acc, "cpu", "time_steal", 0.2812, 0, cputags)
assertContainsTaggedFloat(t, &acc, "cpu", "time_guest", 12.9, 0, cputags)
assertContainsTaggedFloat(t, &acc, "cpu", "time_guest_nice", 2.524, 0, cputags)
assertContainsTaggedFloat(t, &acc, "time_user", 11.4, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_user", 8.3, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_system", 10.9, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_system", 2.7, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_idle", 158.8699, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_idle", 78.7699, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_nice", 2.5, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_nice", 1.2, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_iowait", 0.7, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_iowait", 0.5, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_irq", 1.2, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_irq", 1.1, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_softirq", 0.31, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_softirq", 0.2, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_steal", 0.2812, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_steal", 0.2301, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_guest", 12.9, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_guest", 4.8, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "time_guest_nice", 2.524, 0, cputags) assertContainsTaggedFloat(t, &acc, "cpu", "usage_guest_nice", 2.2, 0.0005, cputags)
}
assertContainsTaggedFloat(t, &acc, "usage_user", 8.3, 0.0005, cputags)
assertContainsTaggedFloat(t, &acc, "usage_system", 2.7, 0.0005, cputags) // Asserts that a given accumulator contains a measurment of type float64 with
assertContainsTaggedFloat(t, &acc, "usage_idle", 78.7699, 0.0005, cputags) // specific tags within a certain distance of a given expected value. Asserts a failure
assertContainsTaggedFloat(t, &acc, "usage_nice", 1.2, 0.0005, cputags) // if the measurement is of the wrong type, or if no matching measurements are found
assertContainsTaggedFloat(t, &acc, "usage_iowait", 0.5, 0.0005, cputags) //
assertContainsTaggedFloat(t, &acc, "usage_irq", 1.1, 0.0005, cputags) // Paramaters:
assertContainsTaggedFloat(t, &acc, "usage_softirq", 0.2, 0.0005, cputags) // t *testing.T : Testing object to use
assertContainsTaggedFloat(t, &acc, "usage_steal", 0.2301, 0.0005, cputags) // acc testutil.Accumulator: Accumulator to examine
assertContainsTaggedFloat(t, &acc, "usage_guest", 4.8, 0.0005, cputags) // measurement string : Name of the measurement to examine
assertContainsTaggedFloat(t, &acc, "usage_guest_nice", 2.2, 0.0005, cputags) // expectedValue float64 : Value to search for within the measurement
// delta float64 : Maximum acceptable distance of an accumulated value
// from the expectedValue parameter. Useful when
// floating-point arithmatic imprecision makes looking
// for an exact match impractical
// tags map[string]string : Tag set the found measurement must have. Set to nil to
// ignore the tag set.
func assertContainsTaggedFloat(
t *testing.T,
acc *testutil.Accumulator,
measurement string,
field string,
expectedValue float64,
delta float64,
tags map[string]string,
) {
var actualValue float64
for _, pt := range acc.Points {
if pt.Measurement == measurement {
for fieldname, value := range pt.Fields {
if fieldname == field {
if value, ok := value.(float64); ok {
actualValue = value
if (value >= expectedValue-delta) && (value <= expectedValue+delta) {
// Found the point, return without failing
return
}
} else {
assert.Fail(t, fmt.Sprintf("Measurement \"%s\" does not have type float64",
measurement))
}
}
}
}
}
msg := fmt.Sprintf(
"Could not find measurement \"%s\" with requested tags within %f of %f, Actual: %f",
measurement, delta, expectedValue, actualValue)
assert.Fail(t, msg)
} }

View File

@ -39,7 +39,7 @@ func TestDiskStats(t *testing.T) {
err = (&DiskStats{ps: &mps}).Gather(&acc) err = (&DiskStats{ps: &mps}).Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
numDiskPoints := len(acc.Points) numDiskPoints := acc.NFields()
expectedAllDiskPoints := 12 expectedAllDiskPoints := 12
assert.Equal(t, expectedAllDiskPoints, numDiskPoints) assert.Equal(t, expectedAllDiskPoints, numDiskPoints)
@ -52,110 +52,114 @@ func TestDiskStats(t *testing.T) {
"fstype": "ext4", "fstype": "ext4",
} }
assert.True(t, acc.CheckTaggedValue("total", uint64(128), tags1)) fields1 := map[string]interface{}{
assert.True(t, acc.CheckTaggedValue("used", uint64(105), tags1)) "total": uint64(128), //tags1)
assert.True(t, acc.CheckTaggedValue("free", uint64(23), tags1)) "used": uint64(105), //tags1)
assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(1234), tags1)) "free": uint64(23), //tags1)
assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(234), tags1)) "inodes_total": uint64(1234), //tags1)
assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(1000), tags1)) "inodes_free": uint64(234), //tags1)
assert.True(t, acc.CheckTaggedValue("total", uint64(256), tags2)) "inodes_used": uint64(1000), //tags1)
assert.True(t, acc.CheckTaggedValue("used", uint64(210), tags2)) }
assert.True(t, acc.CheckTaggedValue("free", uint64(46), tags2)) fields2 := map[string]interface{}{
assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(2468), tags2)) "total": uint64(256), //tags2)
assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(468), tags2)) "used": uint64(210), //tags2)
assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(2000), tags2)) "free": uint64(46), //tags2)
"inodes_total": uint64(2468), //tags2)
"inodes_free": uint64(468), //tags2)
"inodes_used": uint64(2000), //tags2)
}
acc.AssertContainsFields(t, "disk", fields1, tags1)
acc.AssertContainsFields(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
err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/dev"}}).Gather(&acc) err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/dev"}}).Gather(&acc)
assert.Equal(t, expectedAllDiskPoints+6, len(acc.Points)) assert.Equal(t, expectedAllDiskPoints+6, acc.NFields())
// We should see all the diskpoints as Mountpoints includes both // We should see all the diskpoints as Mountpoints includes both
// / and /home // / and /home
err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/home"}}).Gather(&acc) err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/home"}}).Gather(&acc)
assert.Equal(t, 2*expectedAllDiskPoints+6, len(acc.Points)) assert.Equal(t, 2*expectedAllDiskPoints+6, acc.NFields())
} }
func TestDiskIOStats(t *testing.T) { // func TestDiskIOStats(t *testing.T) {
var mps MockPS // var mps MockPS
defer mps.AssertExpectations(t) // defer mps.AssertExpectations(t)
var acc testutil.Accumulator // var acc testutil.Accumulator
var err error // var err error
diskio1 := disk.DiskIOCountersStat{ // diskio1 := disk.DiskIOCountersStat{
// ReadCount: 888,
// WriteCount: 5341,
// ReadBytes: 100000,
// WriteBytes: 200000,
// ReadTime: 7123,
// WriteTime: 9087,
// Name: "sda1",
// IoTime: 123552,
// SerialNumber: "ab-123-ad",
// }
// diskio2 := disk.DiskIOCountersStat{
// ReadCount: 444,
// WriteCount: 2341,
// ReadBytes: 200000,
// WriteBytes: 400000,
// ReadTime: 3123,
// WriteTime: 6087,
// Name: "sdb1",
// IoTime: 246552,
// SerialNumber: "bb-123-ad",
// }
ReadCount: 888, // mps.On("DiskIO").Return(
WriteCount: 5341, // map[string]disk.DiskIOCountersStat{"sda1": diskio1, "sdb1": diskio2},
ReadBytes: 100000, // nil)
WriteBytes: 200000,
ReadTime: 7123,
WriteTime: 9087,
Name: "sda1",
IoTime: 123552,
SerialNumber: "ab-123-ad",
}
diskio2 := disk.DiskIOCountersStat{
ReadCount: 444,
WriteCount: 2341,
ReadBytes: 200000,
WriteBytes: 400000,
ReadTime: 3123,
WriteTime: 6087,
Name: "sdb1",
IoTime: 246552,
SerialNumber: "bb-123-ad",
}
mps.On("DiskIO").Return( // err = (&DiskIOStats{ps: &mps}).Gather(&acc)
map[string]disk.DiskIOCountersStat{"sda1": diskio1, "sdb1": diskio2}, // require.NoError(t, err)
nil)
err = (&DiskIOStats{ps: &mps}).Gather(&acc) // numDiskIOPoints := acc.NFields()
require.NoError(t, err) // expectedAllDiskIOPoints := 14
// assert.Equal(t, expectedAllDiskIOPoints, numDiskIOPoints)
numDiskIOPoints := len(acc.Points) // dtags1 := map[string]string{
expectedAllDiskIOPoints := 14 // "name": "sda1",
assert.Equal(t, expectedAllDiskIOPoints, numDiskIOPoints) // "serial": "ab-123-ad",
// }
// dtags2 := map[string]string{
// "name": "sdb1",
// "serial": "bb-123-ad",
// }
dtags1 := map[string]string{ // assert.True(t, acc.CheckTaggedValue("reads", uint64(888), dtags1))
"name": "sda1", // assert.True(t, acc.CheckTaggedValue("writes", uint64(5341), dtags1))
"serial": "ab-123-ad", // assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(100000), dtags1))
} // assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(200000), dtags1))
dtags2 := map[string]string{ // assert.True(t, acc.CheckTaggedValue("read_time", uint64(7123), dtags1))
"name": "sdb1", // assert.True(t, acc.CheckTaggedValue("write_time", uint64(9087), dtags1))
"serial": "bb-123-ad", // assert.True(t, acc.CheckTaggedValue("io_time", uint64(123552), dtags1))
} // assert.True(t, acc.CheckTaggedValue("reads", uint64(444), dtags2))
// assert.True(t, acc.CheckTaggedValue("writes", uint64(2341), dtags2))
// assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(200000), dtags2))
// assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(400000), dtags2))
// assert.True(t, acc.CheckTaggedValue("read_time", uint64(3123), dtags2))
// assert.True(t, acc.CheckTaggedValue("write_time", uint64(6087), dtags2))
// assert.True(t, acc.CheckTaggedValue("io_time", uint64(246552), dtags2))
assert.True(t, acc.CheckTaggedValue("reads", uint64(888), dtags1)) // // We expect 7 more DiskIOPoints to show up with an explicit match on "sdb1"
assert.True(t, acc.CheckTaggedValue("writes", uint64(5341), dtags1)) // // and serial should be missing from the tags with SkipSerialNumber set
assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(100000), dtags1)) // err = (&DiskIOStats{ps: &mps, Devices: []string{"sdb1"}, SkipSerialNumber: true}).Gather(&acc)
assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(200000), dtags1)) // assert.Equal(t, expectedAllDiskIOPoints+7, acc.NFields())
assert.True(t, acc.CheckTaggedValue("read_time", uint64(7123), dtags1))
assert.True(t, acc.CheckTaggedValue("write_time", uint64(9087), dtags1))
assert.True(t, acc.CheckTaggedValue("io_time", uint64(123552), dtags1))
assert.True(t, acc.CheckTaggedValue("reads", uint64(444), dtags2))
assert.True(t, acc.CheckTaggedValue("writes", uint64(2341), dtags2))
assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(200000), dtags2))
assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(400000), dtags2))
assert.True(t, acc.CheckTaggedValue("read_time", uint64(3123), dtags2))
assert.True(t, acc.CheckTaggedValue("write_time", uint64(6087), dtags2))
assert.True(t, acc.CheckTaggedValue("io_time", uint64(246552), dtags2))
// We expect 7 more DiskIOPoints to show up with an explicit match on "sdb1" // dtags3 := map[string]string{
// and serial should be missing from the tags with SkipSerialNumber set // "name": "sdb1",
err = (&DiskIOStats{ps: &mps, Devices: []string{"sdb1"}, SkipSerialNumber: true}).Gather(&acc) // }
assert.Equal(t, expectedAllDiskIOPoints+7, len(acc.Points))
dtags3 := map[string]string{ // assert.True(t, acc.CheckTaggedValue("reads", uint64(444), dtags3))
"name": "sdb1", // assert.True(t, acc.CheckTaggedValue("writes", uint64(2341), dtags3))
} // assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(200000), dtags3))
// assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(400000), dtags3))
assert.True(t, acc.CheckTaggedValue("reads", uint64(444), dtags3)) // assert.True(t, acc.CheckTaggedValue("read_time", uint64(3123), dtags3))
assert.True(t, acc.CheckTaggedValue("writes", uint64(2341), dtags3)) // assert.True(t, acc.CheckTaggedValue("write_time", uint64(6087), dtags3))
assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(200000), dtags3)) // assert.True(t, acc.CheckTaggedValue("io_time", uint64(246552), dtags3))
assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(400000), dtags3)) // }
assert.True(t, acc.CheckTaggedValue("read_time", uint64(3123), dtags3))
assert.True(t, acc.CheckTaggedValue("write_time", uint64(6087), dtags3))
assert.True(t, acc.CheckTaggedValue("io_time", uint64(246552), dtags3))
}

View File

@ -75,42 +75,46 @@ func TestDockerStats_GenerateStats(t *testing.T) {
"command": "", "command": "",
} }
assert.True(t, acc.CheckTaggedValue("user", 3.1, dockertags)) fields := map[string]interface{}{
assert.True(t, acc.CheckTaggedValue("system", 8.2, dockertags)) "user": 3.1,
assert.True(t, acc.CheckTaggedValue("idle", 80.1, dockertags)) "system": 8.2,
assert.True(t, acc.CheckTaggedValue("nice", 1.3, dockertags)) "idle": 80.1,
assert.True(t, acc.CheckTaggedValue("iowait", 0.2, dockertags)) "nice": 1.3,
assert.True(t, acc.CheckTaggedValue("irq", 0.1, dockertags)) "iowait": 0.2,
assert.True(t, acc.CheckTaggedValue("softirq", 0.11, dockertags)) "irq": 0.1,
assert.True(t, acc.CheckTaggedValue("steal", 0.0001, dockertags)) "softirq": 0.11,
assert.True(t, acc.CheckTaggedValue("guest", 8.1, dockertags)) "steal": 0.0001,
assert.True(t, acc.CheckTaggedValue("guest_nice", 0.324, dockertags)) "guest": 8.1,
"guest_nice": 0.324,
assert.True(t, acc.CheckTaggedValue("cache", uint64(1), dockertags)) "cache": uint64(1),
assert.True(t, acc.CheckTaggedValue("rss", uint64(2), dockertags)) "rss": uint64(2),
assert.True(t, acc.CheckTaggedValue("rss_huge", uint64(3), dockertags)) "rss_huge": uint64(3),
assert.True(t, acc.CheckTaggedValue("mapped_file", uint64(4), dockertags)) "mapped_file": uint64(4),
assert.True(t, acc.CheckTaggedValue("swap_in", uint64(5), dockertags)) "swap_in": uint64(5),
assert.True(t, acc.CheckTaggedValue("swap_out", uint64(6), dockertags)) "swap_out": uint64(6),
assert.True(t, acc.CheckTaggedValue("page_fault", uint64(7), dockertags)) "page_fault": uint64(7),
assert.True(t, acc.CheckTaggedValue("page_major_fault", uint64(8), dockertags)) "page_major_fault": uint64(8),
assert.True(t, acc.CheckTaggedValue("inactive_anon", uint64(9), dockertags)) "inactive_anon": uint64(9),
assert.True(t, acc.CheckTaggedValue("active_anon", uint64(10), dockertags)) "active_anon": uint64(10),
assert.True(t, acc.CheckTaggedValue("inactive_file", uint64(11), dockertags)) "inactive_file": uint64(11),
assert.True(t, acc.CheckTaggedValue("active_file", uint64(12), dockertags)) "active_file": uint64(12),
assert.True(t, acc.CheckTaggedValue("unevictable", uint64(13), dockertags)) "unevictable": uint64(13),
assert.True(t, acc.CheckTaggedValue("memory_limit", uint64(14), dockertags)) "memory_limit": uint64(14),
assert.True(t, acc.CheckTaggedValue("total_cache", uint64(15), dockertags)) "total_cache": uint64(15),
assert.True(t, acc.CheckTaggedValue("total_rss", uint64(16), dockertags)) "total_rss": uint64(16),
assert.True(t, acc.CheckTaggedValue("total_rss_huge", uint64(17), dockertags)) "total_rss_huge": uint64(17),
assert.True(t, acc.CheckTaggedValue("total_mapped_file", uint64(18), dockertags)) "total_mapped_file": uint64(18),
assert.True(t, acc.CheckTaggedValue("total_swap_in", uint64(19), dockertags)) "total_swap_in": uint64(19),
assert.True(t, acc.CheckTaggedValue("total_swap_out", uint64(20), dockertags)) "total_swap_out": uint64(20),
assert.True(t, acc.CheckTaggedValue("total_page_fault", uint64(21), dockertags)) "total_page_fault": uint64(21),
assert.True(t, acc.CheckTaggedValue("total_page_major_fault", uint64(22), dockertags)) "total_page_major_fault": uint64(22),
assert.True(t, acc.CheckTaggedValue("total_inactive_anon", uint64(23), dockertags)) "total_inactive_anon": uint64(23),
assert.True(t, acc.CheckTaggedValue("total_active_anon", uint64(24), dockertags)) "total_active_anon": uint64(24),
assert.True(t, acc.CheckTaggedValue("total_inactive_file", uint64(25), dockertags)) "total_inactive_file": uint64(25),
assert.True(t, acc.CheckTaggedValue("total_active_file", uint64(26), dockertags)) "total_active_file": uint64(26),
assert.True(t, acc.CheckTaggedValue("total_unevictable", uint64(27), dockertags)) "total_unevictable": uint64(27),
}
acc.AssertContainsFields(t, "docker", fields, dockertags)
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
"github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -44,30 +43,30 @@ func TestMemStats(t *testing.T) {
err = (&MemStats{&mps}).Gather(&acc) err = (&MemStats{&mps}).Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
vmtags := map[string]string(nil) memfields := map[string]interface{}{
"total": uint64(12400),
assert.True(t, acc.CheckTaggedValue("total", uint64(12400), vmtags)) "available": uint64(7600),
assert.True(t, acc.CheckTaggedValue("available", uint64(7600), vmtags)) "used": uint64(5000),
assert.True(t, acc.CheckTaggedValue("used", uint64(5000), vmtags)) "available_percent": float64(7600) / float64(12400) * 100,
assert.True(t, acc.CheckTaggedValue("available_percent", "used_percent": float64(5000) / float64(12400) * 100,
float64(7600)/float64(12400)*100, "free": uint64(1235),
vmtags)) "cached": uint64(0),
assert.True(t, acc.CheckTaggedValue("used_percent", "buffered": uint64(0),
float64(5000)/float64(12400)*100, }
vmtags)) acc.AssertContainsFields(t, "mem", memfields, nil)
assert.True(t, acc.CheckTaggedValue("free", uint64(1235), vmtags))
acc.Points = nil acc.Points = nil
err = (&SwapStats{&mps}).Gather(&acc) err = (&SwapStats{&mps}).Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
swaptags := map[string]string(nil) swapfields := map[string]interface{}{
"total": uint64(8123),
assert.NoError(t, acc.ValidateTaggedValue("total", uint64(8123), swaptags)) "used": uint64(1232),
assert.NoError(t, acc.ValidateTaggedValue("used", uint64(1232), swaptags)) "used_percent": float64(12.2),
assert.NoError(t, acc.ValidateTaggedValue("used_percent", float64(12.2), swaptags)) "free": uint64(6412),
assert.NoError(t, acc.ValidateTaggedValue("free", uint64(6412), swaptags)) "in": uint64(7),
assert.NoError(t, acc.ValidateTaggedValue("in", uint64(7), swaptags)) "out": uint64(830),
assert.NoError(t, acc.ValidateTaggedValue("out", uint64(830), swaptags)) }
acc.AssertContainsFields(t, "swap", swapfields, nil)
} }

View File

@ -86,13 +86,15 @@ func (s *NetIOStats) Gather(acc plugins.Accumulator) error {
// Get system wide stats for different network protocols // Get system wide stats for different network protocols
// (ignore these stats if the call fails) // (ignore these stats if the call fails)
netprotos, _ := s.ps.NetProto() netprotos, _ := s.ps.NetProto()
fields := make(map[string]interface{})
for _, proto := range netprotos { for _, proto := range netprotos {
for stat, value := range proto.Stats { for stat, value := range proto.Stats {
name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol), name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol),
strings.ToLower(stat)) strings.ToLower(stat))
acc.Add(name, value, nil) fields[name] = value
} }
} }
acc.AddFields("net", fields, nil)
return nil return nil
} }

View File

@ -6,7 +6,6 @@ import (
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
"github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -65,24 +64,43 @@ func TestNetStats(t *testing.T) {
"interface": "eth0", "interface": "eth0",
} }
assert.NoError(t, acc.ValidateTaggedValue("bytes_sent", uint64(1123), ntags)) fields1 := map[string]interface{}{
assert.NoError(t, acc.ValidateTaggedValue("bytes_recv", uint64(8734422), ntags)) "bytes_sent": uint64(1123),
assert.NoError(t, acc.ValidateTaggedValue("packets_sent", uint64(781), ntags)) "bytes_recv": uint64(8734422),
assert.NoError(t, acc.ValidateTaggedValue("packets_recv", uint64(23456), ntags)) "packets_sent": uint64(781),
assert.NoError(t, acc.ValidateTaggedValue("err_in", uint64(832), ntags)) "packets_recv": uint64(23456),
assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), ntags)) "err_in": uint64(832),
assert.NoError(t, acc.ValidateTaggedValue("drop_in", uint64(7), ntags)) "err_out": uint64(8),
assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), ntags)) "drop_in": uint64(7),
assert.NoError(t, acc.ValidateValue("udp_noports", int64(892592))) "drop_out": uint64(1),
assert.NoError(t, acc.ValidateValue("udp_indatagrams", int64(4655))) }
acc.AssertContainsFields(t, "net", fields1, ntags)
fields2 := map[string]interface{}{
"udp_noports": int64(892592),
"udp_indatagrams": int64(4655),
}
acc.AssertContainsFields(t, "net", fields2, nil)
acc.Points = nil acc.Points = nil
err = (&NetStats{&mps}).Gather(&acc) err = (&NetStats{&mps}).Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
netstattags := map[string]string(nil)
assert.NoError(t, acc.ValidateTaggedValue("tcp_established", 2, netstattags)) fields3 := map[string]interface{}{
assert.NoError(t, acc.ValidateTaggedValue("tcp_close", 1, netstattags)) "tcp_established": 2,
assert.NoError(t, acc.ValidateTaggedValue("udp_socket", 1, netstattags)) "tcp_syn_sent": 0,
"tcp_syn_recv": 0,
"tcp_fin_wait1": 0,
"tcp_fin_wait2": 0,
"tcp_time_wait": 0,
"tcp_close": 1,
"tcp_close_wait": 0,
"tcp_last_ack": 0,
"tcp_listen": 0,
"tcp_closing": 0,
"tcp_none": 0,
"udp_socket": 1,
}
acc.AssertContainsFields(t, "netstat", fields3, nil)
} }

View File

@ -1,16 +1,12 @@
package system package system
import ( import (
"fmt"
gonet "net" gonet "net"
"os" "os"
"reflect"
"strings" "strings"
"testing"
"github.com/influxdb/telegraf/internal" "github.com/influxdb/telegraf/internal"
"github.com/influxdb/telegraf/plugins" "github.com/influxdb/telegraf/plugins"
"github.com/influxdb/telegraf/testutil"
dc "github.com/fsouza/go-dockerclient" dc "github.com/fsouza/go-dockerclient"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
@ -18,8 +14,6 @@ import (
"github.com/shirou/gopsutil/docker" "github.com/shirou/gopsutil/docker"
"github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
"github.com/stretchr/testify/assert"
) )
type DockerContainerStat struct { type DockerContainerStat struct {
@ -172,49 +166,3 @@ func (s *systemPS) DockerStat() ([]*DockerContainerStat, error) {
return stats, nil return stats, nil
} }
// Asserts that a given accumulator contains a measurment of type float64 with
// specific tags within a certain distance of a given expected value. Asserts a failure
// if the measurement is of the wrong type, or if no matching measurements are found
//
// Paramaters:
// t *testing.T : Testing object to use
// acc testutil.Accumulator: Accumulator to examine
// measurement string : Name of the measurement to examine
// expectedValue float64 : Value to search for within the measurement
// delta float64 : Maximum acceptable distance of an accumulated value
// from the expectedValue parameter. Useful when
// floating-point arithmatic imprecision makes looking
// for an exact match impractical
// tags map[string]string : Tag set the found measurement must have. Set to nil to
// ignore the tag set.
func assertContainsTaggedFloat(
t *testing.T,
acc *testutil.Accumulator,
measurement string,
expectedValue float64,
delta float64,
tags map[string]string,
) {
var actualValue float64
for _, pt := range acc.Points {
if pt.Measurement == measurement {
if (tags == nil) || reflect.DeepEqual(pt.Tags, tags) {
if value, ok := pt.Fields["value"].(float64); ok {
actualValue = value
if (value >= expectedValue-delta) && (value <= expectedValue+delta) {
// Found the point, return without failing
return
}
} else {
assert.Fail(t, fmt.Sprintf("Measurement \"%s\" does not have type float64",
measurement))
}
}
}
}
msg := fmt.Sprintf("Could not find measurement \"%s\" with requested tags within %f of %f, Actual: %f",
measurement, delta, expectedValue, actualValue)
assert.Fail(t, msg)
}

View File

@ -4,7 +4,10 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"sync" "sync"
"testing"
"time" "time"
"github.com/stretchr/testify/assert"
) )
// Point defines a single point measurement // Point defines a single point measurement
@ -106,70 +109,26 @@ func (a *Accumulator) Get(measurement string) (*Point, bool) {
return nil, false return nil, false
} }
// CheckValue calls CheckFieldsValue passing a single-value map as fields // NFields returns the total number of fields in the accumulator, across all
func (a *Accumulator) CheckValue(measurement string, val interface{}) bool { // measurements
return a.CheckFieldsValue(measurement, map[string]interface{}{"value": val}) func (a *Accumulator) NFields() int {
} counter := 0
for _, pt := range a.Points {
// CheckValue checks that the accumulators point for the given measurement for _, _ = range pt.Fields {
// is the same as the given value. counter++
func (a *Accumulator) CheckFieldsValue(measurement string, fields map[string]interface{}) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
if reflect.DeepEqual(fields, p.Fields) {
return true
} else {
fmt.Printf("Measurement %s Failure, expected: %v, got %v\n",
measurement, fields, p.Fields)
return false
}
} }
} }
fmt.Printf("Measurement %s, fields %s not found\n", measurement, fields) return counter
return false
} }
// CheckTaggedValue calls ValidateTaggedValue func (a *Accumulator) AssertContainsFields(
func (a *Accumulator) CheckTaggedValue( t *testing.T,
measurement string,
val interface{},
tags map[string]string,
) bool {
return a.ValidateTaggedValue(measurement, val, tags) == nil
}
// ValidateTaggedValue calls ValidateTaggedFieldsValue passing a single-value map as fields
func (a *Accumulator) ValidateTaggedValue(
measurement string,
val interface{},
tags map[string]string,
) error {
return a.ValidateTaggedFieldsValue(measurement, map[string]interface{}{"value": val}, tags)
}
// ValidateValue calls ValidateTaggedValue
func (a *Accumulator) ValidateValue(measurement string, val interface{}) error {
return a.ValidateTaggedValue(measurement, val, nil)
}
// CheckTaggedFieldsValue calls ValidateTaggedFieldsValue
func (a *Accumulator) CheckTaggedFieldsValue(
measurement string, measurement string,
fields map[string]interface{}, fields map[string]interface{},
tags map[string]string, tags map[string]string,
) bool { ) {
return a.ValidateTaggedFieldsValue(measurement, fields, tags) == nil
}
// ValidateTaggedValue validates that the given measurement and value exist
// in the accumulator and with the given tags.
func (a *Accumulator) ValidateTaggedFieldsValue(
measurement string,
fields map[string]interface{},
tags map[string]string,
) error {
if tags == nil { if tags == nil {
tags = map[string]string{} 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) {
@ -178,53 +137,27 @@ func (a *Accumulator) ValidateTaggedFieldsValue(
if p.Measurement == measurement { if p.Measurement == measurement {
if !reflect.DeepEqual(fields, p.Fields) { if !reflect.DeepEqual(fields, p.Fields) {
return fmt.Errorf("%v != %v ", fields, p.Fields) msg := fmt.Sprintf("Actual:\n %v (%T) \nExpected:\n %v (%T)",
}
return nil
}
}
return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags)
}
// ValidateFieldsValue calls ValidateTaggedFieldsValue
func (a *Accumulator) ValidateFieldsValue(
measurement string,
fields map[string]interface{},
) error {
return a.ValidateTaggedValue(measurement, fields, nil)
}
func (a *Accumulator) ValidateTaggedFields(
measurement string,
fields map[string]interface{},
tags map[string]string,
) error {
if tags == nil {
tags = map[string]string{}
}
for _, p := range a.Points {
if !reflect.DeepEqual(tags, p.Tags) {
continue
}
if p.Measurement == measurement {
if !reflect.DeepEqual(fields, p.Fields) {
return fmt.Errorf("%v (%T) != %v (%T)",
p.Fields, p.Fields, fields, fields) p.Fields, p.Fields, fields, fields)
assert.Fail(t, msg)
} }
return nil return
} }
} }
return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags) msg := fmt.Sprintf("unknown measurement %s with tags %v", measurement, tags)
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) HasIntValue(measurement string) bool { func (a *Accumulator) HasIntField(measurement string, field string) bool {
for _, p := range a.Points { for _, p := range a.Points {
if p.Measurement == measurement { if p.Measurement == measurement {
_, ok := p.Fields["value"].(int64) for fieldname, value := range p.Fields {
return ok if fieldname == field {
_, ok := value.(int64)
return ok
}
}
} }
} }
@ -232,11 +165,15 @@ func (a *Accumulator) HasIntValue(measurement string) bool {
} }
// HasUIntValue returns true if the measurement has a UInt value // HasUIntValue returns true if the measurement has a UInt value
func (a *Accumulator) HasUIntValue(measurement string) bool { func (a *Accumulator) HasUIntField(measurement string, field string) bool {
for _, p := range a.Points { for _, p := range a.Points {
if p.Measurement == measurement { if p.Measurement == measurement {
_, ok := p.Fields["value"].(uint64) for fieldname, value := range p.Fields {
return ok if fieldname == field {
_, ok := value.(uint64)
return ok
}
}
} }
} }
@ -244,11 +181,15 @@ func (a *Accumulator) HasUIntValue(measurement string) bool {
} }
// HasFloatValue returns true if the given measurement has a float value // HasFloatValue returns true if the given measurement has a float value
func (a *Accumulator) HasFloatValue(measurement string) bool { func (a *Accumulator) HasFloatField(measurement string, field string) bool {
for _, p := range a.Points { for _, p := range a.Points {
if p.Measurement == measurement { if p.Measurement == measurement {
_, ok := p.Fields["value"].(float64) for fieldname, value := range p.Fields {
return ok if fieldname == field {
_, ok := value.(float64)
return ok
}
}
} }
} }