0.3.0 unit tests: mysql, nginx, phpfpm, ping, postgres

This commit is contained in:
Cameron Sparr 2016-01-06 17:37:56 -07:00
parent 6a4bf9fcff
commit 6eb49dee5d
5 changed files with 59 additions and 142 deletions

View File

@ -2,7 +2,6 @@ package mysql
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
@ -10,64 +9,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestMysqlGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
}
var acc testutil.Accumulator
err := m.Gather(&acc)
require.NoError(t, err)
prefixes := []struct {
prefix string
count int
}{
{"commands", 139},
{"handler", 16},
{"bytes", 2},
{"innodb", 46},
{"threads", 4},
{"aborted", 2},
{"created", 3},
{"key", 7},
{"open", 7},
{"opened", 3},
{"qcache", 8},
{"table", 1},
}
intMetrics := []string{
"queries",
"slow_queries",
"connections",
}
for _, prefix := range prefixes {
var count int
for _, p := range acc.Points {
if strings.HasPrefix(p.Measurement, prefix.prefix) {
count++
}
}
if prefix.count > count {
t.Errorf("Expected less than %d measurements with prefix %s, got %d",
count, prefix.prefix, prefix.count)
}
}
for _, metric := range intMetrics {
assert.True(t, acc.HasIntValue(metric))
}
}
func TestMysqlDefaultsToLocal(t *testing.T) { func TestMysqlDefaultsToLocal(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("Skipping integration test in short mode") t.Skip("Skipping integration test in short mode")
@ -82,7 +23,7 @@ func TestMysqlDefaultsToLocal(t *testing.T) {
err := m.Gather(&acc) err := m.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
assert.True(t, len(acc.Points) > 0) assert.True(t, acc.HasMeasurement("mysql"))
} }
func TestMysqlParseDSN(t *testing.T) { func TestMysqlParseDSN(t *testing.T) {

View File

@ -54,17 +54,14 @@ func TestNginxGeneratesMetrics(t *testing.T) {
err := n.Gather(&acc) err := n.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
metrics := []struct { fields := map[string]interface{}{
name string "active": uint64(585),
value uint64 "accepts": uint64(85340),
}{ "handled": uint64(85340),
{"active", 585}, "requests": uint64(35085),
{"accepts", 85340}, "reading": uint64(4),
{"handled", 85340}, "writing": uint64(135),
{"requests", 35085}, "waiting": uint64(446),
{"reading", 4},
{"writing", 135},
{"waiting", 446},
} }
addr, err := url.Parse(ts.URL) addr, err := url.Parse(ts.URL)
if err != nil { if err != nil {
@ -84,8 +81,5 @@ func TestNginxGeneratesMetrics(t *testing.T) {
} }
tags := map[string]string{"server": host, "port": port} tags := map[string]string{"server": host, "port": port}
acc.AssertContainsTaggedFields(t, "nginx", fields, tags)
for _, m := range metrics {
assert.NoError(t, acc.ValidateTaggedValue(m.name, m.value, tags))
}
} }

View File

@ -32,27 +32,21 @@ func TestPhpFpmGeneratesMetrics(t *testing.T) {
"url": ts.Listener.Addr().String(), "url": ts.Listener.Addr().String(),
"pool": "www", "pool": "www",
} }
assert.NoError(t, acc.ValidateTaggedValue("accepted_conn", int64(3), tags))
checkInt := []struct { fields := map[string]interface{}{
name string "accepted_conn": int64(3),
value int64 "listen_queue": int64(1),
}{ "max_listen_queue": int64(0),
{"accepted_conn", 3}, "listen_queue_len": int64(0),
{"listen_queue", 1}, "idle_processes": int64(1),
{"max_listen_queue", 0}, "active_processes": int64(1),
{"listen_queue_len", 0}, "total_processes": int64(2),
{"idle_processes", 1}, "max_active_processes": int64(1),
{"active_processes", 1}, "max_children_reached": int64(2),
{"total_processes", 2}, "slow_requests": int64(1),
{"max_active_processes", 1},
{"max_children_reached", 2},
{"slow_requests", 1},
} }
for _, c := range checkInt { acc.AssertContainsTaggedFields(t, "phpfpm", fields, tags)
assert.Equal(t, true, acc.CheckValue(c.name, c.value))
}
} }
//When not passing server config, we default to localhost //When not passing server config, we default to localhost

View File

@ -120,18 +120,16 @@ func TestPingGather(t *testing.T) {
p.Gather(&acc) p.Gather(&acc)
tags := map[string]string{"url": "www.google.com"} tags := map[string]string{"url": "www.google.com"}
assert.NoError(t, acc.ValidateTaggedValue("packets_transmitted", 5, tags)) fields := map[string]interface{}{
assert.NoError(t, acc.ValidateTaggedValue("packets_received", 5, tags)) "packets_transmitted": 5,
assert.NoError(t, acc.ValidateTaggedValue("percent_packet_loss", 0.0, tags)) "packets_received": 5,
assert.NoError(t, acc.ValidateTaggedValue("average_response_ms", "percent_packet_loss": 0.0,
43.628, tags)) "average_response_ms": 43.628,
}
acc.AssertContainsTaggedFields(t, "ping", fields, tags)
tags = map[string]string{"url": "www.reddit.com"} tags = map[string]string{"url": "www.reddit.com"}
assert.NoError(t, acc.ValidateTaggedValue("packets_transmitted", 5, tags)) acc.AssertContainsTaggedFields(t, "ping", fields, tags)
assert.NoError(t, acc.ValidateTaggedValue("packets_received", 5, tags))
assert.NoError(t, acc.ValidateTaggedValue("percent_packet_loss", 0.0, tags))
assert.NoError(t, acc.ValidateTaggedValue("average_response_ms",
43.628, tags))
} }
var lossyPingOutput = ` var lossyPingOutput = `
@ -159,10 +157,13 @@ func TestLossyPingGather(t *testing.T) {
p.Gather(&acc) p.Gather(&acc)
tags := map[string]string{"url": "www.google.com"} tags := map[string]string{"url": "www.google.com"}
assert.NoError(t, acc.ValidateTaggedValue("packets_transmitted", 5, tags)) fields := map[string]interface{}{
assert.NoError(t, acc.ValidateTaggedValue("packets_received", 3, tags)) "packets_transmitted": 5,
assert.NoError(t, acc.ValidateTaggedValue("percent_packet_loss", 40.0, tags)) "packets_received": 3,
assert.NoError(t, acc.ValidateTaggedValue("average_response_ms", 44.033, tags)) "percent_packet_loss": 40.0,
"average_response_ms": 44.033,
}
acc.AssertContainsTaggedFields(t, "ping", fields, tags)
} }
var errorPingOutput = ` var errorPingOutput = `
@ -188,10 +189,13 @@ func TestBadPingGather(t *testing.T) {
p.Gather(&acc) p.Gather(&acc)
tags := map[string]string{"url": "www.amazon.com"} tags := map[string]string{"url": "www.amazon.com"}
assert.NoError(t, acc.ValidateTaggedValue("packets_transmitted", 2, tags)) fields := map[string]interface{}{
assert.NoError(t, acc.ValidateTaggedValue("packets_received", 0, tags)) "packets_transmitted": 2,
assert.NoError(t, acc.ValidateTaggedValue("percent_packet_loss", 100.0, tags)) "packets_received": 0,
assert.NoError(t, acc.ValidateTaggedValue("average_response_ms", 0.0, tags)) "percent_packet_loss": 100.0,
"average_response_ms": 0.0,
}
acc.AssertContainsTaggedFields(t, "ping", fields, tags)
} }
func mockFatalHostPinger(args ...string) (string, error) { func mockFatalHostPinger(args ...string) (string, error) {

View File

@ -15,13 +15,9 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
} }
p := &Postgresql{ p := &Postgresql{
Servers: []*Server{ Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
{ testutil.GetLocalHost()),
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", Databases: []string{"postgres"},
testutil.GetLocalHost()),
Databases: []string{"postgres"},
},
},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -30,7 +26,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
availableColumns := make(map[string]bool) availableColumns := make(map[string]bool)
for _, col := range p.Servers[0].OrderedColumns { for _, col := range p.OrderedColumns {
availableColumns[col] = true availableColumns[col] = true
} }
@ -61,7 +57,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
for _, metric := range intMetrics { for _, metric := range intMetrics {
_, ok := availableColumns[metric] _, ok := availableColumns[metric]
if ok { if ok {
assert.True(t, acc.HasIntValue(metric)) assert.True(t, acc.HasIntField("postgresql", metric))
metricsCounted++ metricsCounted++
} }
} }
@ -69,7 +65,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
for _, metric := range floatMetrics { for _, metric := range floatMetrics {
_, ok := availableColumns[metric] _, ok := availableColumns[metric]
if ok { if ok {
assert.True(t, acc.HasFloatValue(metric)) assert.True(t, acc.HasFloatField("postgresql", metric))
metricsCounted++ metricsCounted++
} }
} }
@ -84,13 +80,9 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
} }
p := &Postgresql{ p := &Postgresql{
Servers: []*Server{ Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
{ testutil.GetLocalHost()),
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", Databases: []string{"postgres"},
testutil.GetLocalHost()),
Databases: []string{"postgres"},
},
},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -98,7 +90,7 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
err := p.Gather(&acc) err := p.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
point, ok := acc.Get("xact_commit") point, ok := acc.Get("postgresql")
require.True(t, ok) require.True(t, ok)
assert.Equal(t, "postgres", point.Tags["db"]) assert.Equal(t, "postgres", point.Tags["db"])
@ -110,12 +102,8 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
} }
p := &Postgresql{ p := &Postgresql{
Servers: []*Server{ Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
{ testutil.GetLocalHost()),
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
},
},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -126,7 +114,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
var found bool var found bool
for _, pnt := range acc.Points { for _, pnt := range acc.Points {
if pnt.Measurement == "xact_commit" { if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "postgres" { if pnt.Tags["db"] == "postgres" {
found = true found = true
break break
@ -143,12 +131,8 @@ func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
} }
p := &Postgresql{ p := &Postgresql{
Servers: []*Server{ Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
{ testutil.GetLocalHost()),
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
},
},
} }
var acc testutil.Accumulator var acc testutil.Accumulator