2016-03-14 09:27:07 +00:00
|
|
|
package postgresql_extensible
|
|
|
|
|
|
|
|
import (
|
2018-09-18 16:08:13 +00:00
|
|
|
"errors"
|
2016-03-14 09:27:07 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/postgresql"
|
2016-03-14 09:27:07 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
func queryRunner(t *testing.T, q query) *testutil.Accumulator {
|
2016-03-14 09:27:07 +00:00
|
|
|
p := &Postgresql{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: postgresql.Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
2018-08-01 22:44:10 +00:00
|
|
|
IsPgBouncer: false,
|
2018-01-06 00:03:09 +00:00
|
|
|
},
|
2016-03-14 09:27:07 +00:00
|
|
|
Databases: []string{"postgres"},
|
2017-09-25 17:58:10 +00:00
|
|
|
Query: q,
|
2016-03-14 09:27:07 +00:00
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
2018-01-06 00:03:09 +00:00
|
|
|
p.Start(&acc)
|
2017-09-25 17:58:10 +00:00
|
|
|
|
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2018-01-06 00:03:09 +00:00
|
|
|
return &acc
|
2017-09-25 17:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgresqlGeneratesMetrics(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
acc := queryRunner(t, query{{
|
2017-09-25 17:58:10 +00:00
|
|
|
Sqlquery: "select * from pg_stat_database",
|
|
|
|
Version: 901,
|
|
|
|
Withdbname: false,
|
|
|
|
Tagvalue: "",
|
|
|
|
}})
|
2016-03-14 09:27:07 +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",
|
2017-11-09 22:03:16 +00:00
|
|
|
"datid",
|
2016-03-14 09:27:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 22:03:16 +00:00
|
|
|
int32Metrics := []string{}
|
|
|
|
|
2016-03-14 09:27:07 +00:00
|
|
|
floatMetrics := []string{
|
|
|
|
"blk_read_time",
|
|
|
|
"blk_write_time",
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:36:36 +00:00
|
|
|
stringMetrics := []string{
|
|
|
|
"datname",
|
|
|
|
}
|
|
|
|
|
2016-03-14 09:27:07 +00:00
|
|
|
metricsCounted := 0
|
|
|
|
|
|
|
|
for _, metric := range intMetrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasInt64Field("postgresql", metric))
|
|
|
|
metricsCounted++
|
2016-03-14 09:27:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 20:36:36 +00:00
|
|
|
for _, metric := range int32Metrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasInt32Field("postgresql", metric))
|
|
|
|
metricsCounted++
|
2017-01-24 20:36:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 09:27:07 +00:00
|
|
|
for _, metric := range floatMetrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasFloatField("postgresql", metric))
|
|
|
|
metricsCounted++
|
2016-03-14 09:27:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 20:36:36 +00:00
|
|
|
for _, metric := range stringMetrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasStringField("postgresql", metric))
|
|
|
|
metricsCounted++
|
2017-01-24 20:36:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 09:27:07 +00:00
|
|
|
assert.True(t, metricsCounted > 0)
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.Equal(t, len(floatMetrics)+len(intMetrics)+len(int32Metrics)+len(stringMetrics), metricsCounted)
|
2016-03-14 09:27:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 17:58:10 +00:00
|
|
|
func TestPostgresqlQueryOutputTests(t *testing.T) {
|
|
|
|
const measurement = "postgresql"
|
|
|
|
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
examples := map[string]func(*testutil.Accumulator){
|
|
|
|
"SELECT 10.0::float AS myvalue": func(acc *testutil.Accumulator) {
|
|
|
|
v, found := acc.FloatField(measurement, "myvalue")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, 10.0, v)
|
|
|
|
},
|
|
|
|
"SELECT 10.0 AS myvalue": func(acc *testutil.Accumulator) {
|
|
|
|
v, found := acc.StringField(measurement, "myvalue")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "10.0", v)
|
|
|
|
},
|
|
|
|
"SELECT 'hello world' AS myvalue": func(acc *testutil.Accumulator) {
|
|
|
|
v, found := acc.StringField(measurement, "myvalue")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "hello world", v)
|
|
|
|
},
|
|
|
|
"SELECT true AS myvalue": func(acc *testutil.Accumulator) {
|
|
|
|
v, found := acc.BoolField(measurement, "myvalue")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, true, v)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for q, assertions := range examples {
|
2018-01-06 00:03:09 +00:00
|
|
|
acc := queryRunner(t, query{{
|
2017-09-25 17:58:10 +00:00
|
|
|
Sqlquery: q,
|
|
|
|
Version: 901,
|
|
|
|
Withdbname: false,
|
|
|
|
Tagvalue: "",
|
|
|
|
}})
|
|
|
|
assertions(acc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgresqlFieldOutput(t *testing.T) {
|
|
|
|
const measurement = "postgresql"
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
acc := queryRunner(t, query{{
|
2017-09-25 17:58:10 +00:00
|
|
|
Sqlquery: "select * from pg_stat_database",
|
|
|
|
Version: 901,
|
|
|
|
Withdbname: false,
|
|
|
|
Tagvalue: "",
|
|
|
|
}})
|
|
|
|
|
|
|
|
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",
|
2017-11-09 22:03:16 +00:00
|
|
|
"datid",
|
2017-09-25 17:58:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 22:03:16 +00:00
|
|
|
int32Metrics := []string{}
|
|
|
|
|
2017-09-25 17:58:10 +00:00
|
|
|
floatMetrics := []string{
|
|
|
|
"blk_read_time",
|
|
|
|
"blk_write_time",
|
|
|
|
}
|
|
|
|
|
|
|
|
stringMetrics := []string{
|
|
|
|
"datname",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range intMetrics {
|
|
|
|
_, found := acc.Int64Field(measurement, field)
|
|
|
|
assert.True(t, found, fmt.Sprintf("expected %s to be an integer", field))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range int32Metrics {
|
|
|
|
_, found := acc.Int32Field(measurement, field)
|
|
|
|
assert.True(t, found, fmt.Sprintf("expected %s to be an int32", field))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range floatMetrics {
|
|
|
|
_, found := acc.FloatField(measurement, field)
|
|
|
|
assert.True(t, found, fmt.Sprintf("expected %s to be a float64", field))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range stringMetrics {
|
|
|
|
_, found := acc.StringField(measurement, field)
|
|
|
|
assert.True(t, found, fmt.Sprintf("expected %s to be a str", field))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 09:27:07 +00:00
|
|
|
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Postgresql{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: postgresql.Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
|
|
|
},
|
2016-03-14 09:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
require.NoError(t, p.Start(&acc))
|
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
2017-09-25 17:58:10 +00:00
|
|
|
assert.NotEmpty(t, p.IgnoredColumns())
|
2016-03-14 09:27:07 +00:00
|
|
|
for col := range p.IgnoredColumns() {
|
|
|
|
assert.False(t, acc.HasMeasurement(col))
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 16:08:13 +00:00
|
|
|
|
|
|
|
func TestAccRow(t *testing.T) {
|
|
|
|
p := Postgresql{}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
columns := []string{"datname", "cat"}
|
|
|
|
|
|
|
|
testRows := []fakeRow{
|
|
|
|
{fields: []interface{}{1, "gato"}},
|
|
|
|
{fields: []interface{}{nil, "gato"}},
|
|
|
|
{fields: []interface{}{"name", "gato"}},
|
|
|
|
}
|
|
|
|
for i := range testRows {
|
|
|
|
err := p.accRow("pgTEST", testRows[i], &acc, columns)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Scan failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeRow struct {
|
|
|
|
fields []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fakeRow) Scan(dest ...interface{}) error {
|
|
|
|
if len(f.fields) != len(dest) {
|
|
|
|
return errors.New("Nada matchy buddy")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range dest {
|
|
|
|
switch d.(type) {
|
|
|
|
case (*interface{}):
|
|
|
|
*d.(*interface{}) = f.fields[i]
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Bad type %T", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|