telegraf/plugins/postgresql/postgresql_test.go

147 lines
2.8 KiB
Go
Raw Normal View History

2015-05-18 17:46:44 +00:00
package postgresql
import (
"fmt"
2015-05-18 17:46:44 +00:00
"testing"
"github.com/influxdb/telegraf/testutil"
2015-05-18 17:46:44 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPostgresqlGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
2015-05-18 17:46:44 +00:00
p := &Postgresql{
2016-01-07 00:25:40 +00:00
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
Databases: []string{"postgres"},
2015-05-18 17:46:44 +00:00
}
var acc testutil.Accumulator
err := p.Gather(&acc)
require.NoError(t, err)
availableColumns := make(map[string]bool)
2016-01-07 00:25:40 +00:00
for _, col := range p.OrderedColumns {
availableColumns[col] = true
}
2015-05-18 17:46:44 +00:00
intMetrics := []string{
"xact_commit",
"xact_rollback",
"blks_read",
"blks_hit",
"tup_returned",
"tup_fetched",
"tup_inserted",
"tup_updated",
"tup_deleted",
"conflicts",
"temp_files",
"temp_bytes",
"deadlocks",
"numbackends",
2015-05-18 17:46:44 +00:00
}
floatMetrics := []string{
"blk_read_time",
"blk_write_time",
2015-05-18 17:46:44 +00:00
}
metricsCounted := 0
2015-05-18 17:46:44 +00:00
for _, metric := range intMetrics {
_, ok := availableColumns[metric]
if ok {
2016-01-07 00:25:40 +00:00
assert.True(t, acc.HasIntField("postgresql", metric), metric)
metricsCounted++
}
2015-05-18 17:46:44 +00:00
}
for _, metric := range floatMetrics {
_, ok := availableColumns[metric]
if ok {
2016-01-07 00:25:40 +00:00
assert.True(t, acc.HasFloatField("postgresql", metric), metric)
metricsCounted++
}
2015-05-18 17:46:44 +00:00
}
assert.True(t, metricsCounted > 0)
2015-10-16 18:54:05 +00:00
assert.Equal(t, len(availableColumns)-len(p.IgnoredColumns()), metricsCounted)
2015-05-18 17:46:44 +00:00
}
func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
2015-05-18 17:46:44 +00:00
p := &Postgresql{
2016-01-07 00:25:40 +00:00
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
Databases: []string{"postgres"},
2015-05-18 17:46:44 +00:00
}
var acc testutil.Accumulator
err := p.Gather(&acc)
require.NoError(t, err)
2016-01-07 00:25:40 +00:00
point, ok := acc.Get("postgresql")
2015-05-18 17:46:44 +00:00
require.True(t, ok)
assert.Equal(t, "postgres", point.Tags["db"])
}
func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
2015-05-18 17:46:44 +00:00
p := &Postgresql{
2016-01-07 00:25:40 +00:00
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
2015-05-18 17:46:44 +00:00
}
var acc testutil.Accumulator
err := p.Gather(&acc)
require.NoError(t, err)
var found bool
for _, pnt := range acc.Points {
2016-01-07 00:25:40 +00:00
if pnt.Measurement == "postgresql" {
2015-05-18 17:46:44 +00:00
if pnt.Tags["db"] == "postgres" {
found = true
break
}
}
}
assert.True(t, found)
}
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
2015-09-13 08:34:54 +00:00
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &Postgresql{
2016-01-07 00:25:40 +00:00
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
}
var acc testutil.Accumulator
err := p.Gather(&acc)
require.NoError(t, err)
for col := range p.IgnoredColumns() {
assert.False(t, acc.HasMeasurement(col))
}
}