telegraf/plugins/inputs/postgresql/postgresql_test.go

319 lines
6.7 KiB
Go
Raw Normal View History

2015-05-19 01:46:44 +08:00
package postgresql
import (
"fmt"
2015-05-19 01:46:44 +08:00
"testing"
"github.com/docker/go-connections/nat"
2015-05-19 01:46:44 +08:00
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
2015-05-19 01:46:44 +08:00
)
const servicePort = "5432"
func launchTestContainer(t *testing.T) *testutil.Container {
container := testutil.Container{
Image: "postgres:alpine",
ExposedPorts: []string{servicePort},
Env: map[string]string{
"POSTGRES_HOST_AUTH_METHOD": "trust",
},
WaitingFor: wait.ForAll(
// the database comes up twice, once right away, then again a second
2024-01-06 05:34:02 +08:00
// time after the docker entrypoint starts configuration
wait.ForLog("database system is ready to accept connections").WithOccurrence(2),
wait.ForListeningPort(nat.Port(servicePort)),
),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
return &container
}
2021-01-27 02:06:12 +08:00
func TestPostgresqlGeneratesMetricsIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
container := launchTestContainer(t)
defer container.Terminate()
addr := fmt.Sprintf(
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
)
2015-05-19 01:46:44 +08:00
p := &Postgresql{
Service: Service{
Address: config.NewSecret([]byte(addr)),
2018-08-02 06:44:10 +08:00
IsPgBouncer: false,
},
Databases: []string{"postgres"},
2015-05-19 01:46:44 +08:00
}
var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
require.NoError(t, p.Gather(&acc))
2015-05-19 01:46:44 +08: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",
"buffers_alloc",
"buffers_backend",
"buffers_backend_fsync",
"buffers_checkpoint",
"buffers_clean",
"checkpoints_req",
"checkpoints_timed",
"maxwritten_clean",
2017-11-10 06:03:16 +08:00
"datid",
"numbackends",
}
2017-11-10 06:03:16 +08:00
int32Metrics := []string{}
2015-05-19 01:46:44 +08:00
floatMetrics := []string{
"blk_read_time",
"blk_write_time",
"checkpoint_write_time",
"checkpoint_sync_time",
}
stringMetrics := []string{
"datname",
2015-05-19 01:46:44 +08:00
}
metricsCounted := 0
2015-05-19 01:46:44 +08:00
for _, metric := range intMetrics {
require.True(t, acc.HasInt64Field("postgresql", metric))
metricsCounted++
2015-05-19 01:46:44 +08:00
}
for _, metric := range int32Metrics {
require.True(t, acc.HasInt32Field("postgresql", metric))
metricsCounted++
}
2015-05-19 01:46:44 +08:00
for _, metric := range floatMetrics {
require.True(t, acc.HasFloatField("postgresql", metric))
metricsCounted++
2015-05-19 01:46:44 +08:00
}
for _, metric := range stringMetrics {
require.True(t, acc.HasStringField("postgresql", metric))
metricsCounted++
}
require.Greater(t, metricsCounted, 0)
require.Equal(t, len(floatMetrics)+len(intMetrics)+len(int32Metrics)+len(stringMetrics), metricsCounted)
2015-05-19 01:46:44 +08:00
}
2021-01-27 02:06:12 +08:00
func TestPostgresqlTagsMetricsWithDatabaseNameIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
container := launchTestContainer(t)
defer container.Terminate()
addr := fmt.Sprintf(
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
)
2015-05-19 01:46:44 +08:00
p := &Postgresql{
Service: Service{
Address: config.NewSecret([]byte(addr)),
},
Databases: []string{"postgres"},
2015-05-19 01:46:44 +08:00
}
var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
require.NoError(t, p.Gather(&acc))
2015-05-19 01:46:44 +08:00
point, ok := acc.Get("postgresql")
2015-05-19 01:46:44 +08:00
require.True(t, ok)
require.Equal(t, "postgres", point.Tags["db"])
2015-05-19 01:46:44 +08:00
}
2021-01-27 02:06:12 +08:00
func TestPostgresqlDefaultsToAllDatabasesIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
container := launchTestContainer(t)
defer container.Terminate()
addr := fmt.Sprintf(
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
)
2015-05-19 01:46:44 +08:00
p := &Postgresql{
Service: Service{
Address: config.NewSecret([]byte(addr)),
},
2015-05-19 01:46:44 +08:00
}
var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
require.NoError(t, p.Gather(&acc))
2015-05-19 01:46:44 +08:00
var found bool
for _, pnt := range acc.Metrics {
if pnt.Measurement == "postgresql" {
2015-05-19 01:46:44 +08:00
if pnt.Tags["db"] == "postgres" {
found = true
break
}
}
}
require.True(t, found)
2015-05-19 01:46:44 +08:00
}
2021-01-27 02:06:12 +08:00
func TestPostgresqlIgnoresUnwantedColumnsIntegration(t *testing.T) {
2015-09-13 16:34:54 +08:00
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
container := launchTestContainer(t)
defer container.Terminate()
addr := fmt.Sprintf(
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
)
p := &Postgresql{
Service: Service{
Address: config.NewSecret([]byte(addr)),
},
}
var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
require.NoError(t, p.Gather(&acc))
for col := range p.IgnoredColumns() {
require.False(t, acc.HasMeasurement(col))
}
}
2021-01-27 02:06:12 +08:00
func TestPostgresqlDatabaseWhitelistTestIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
container := launchTestContainer(t)
defer container.Terminate()
addr := fmt.Sprintf(
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
)
p := &Postgresql{
Service: Service{
Address: config.NewSecret([]byte(addr)),
},
Databases: []string{"template0"},
}
var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
require.NoError(t, p.Gather(&acc))
var foundTemplate0 = false
var foundTemplate1 = false
for _, pnt := range acc.Metrics {
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template0" {
foundTemplate0 = true
}
}
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template1" {
foundTemplate1 = true
}
}
}
require.True(t, foundTemplate0)
require.False(t, foundTemplate1)
}
2021-01-27 02:06:12 +08:00
func TestPostgresqlDatabaseBlacklistTestIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
container := launchTestContainer(t)
defer container.Terminate()
addr := fmt.Sprintf(
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
)
p := &Postgresql{
Service: Service{
Address: config.NewSecret([]byte(addr)),
},
IgnoredDatabases: []string{"template0"},
}
var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
require.NoError(t, p.Gather(&acc))
var foundTemplate0 = false
var foundTemplate1 = false
for _, pnt := range acc.Metrics {
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template0" {
foundTemplate0 = true
}
}
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template1" {
foundTemplate1 = true
}
}
}
require.False(t, foundTemplate0)
require.True(t, foundTemplate1)
}