Skip to content

Commit

Permalink
add test for ConnectionInfo collector
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiangreco committed Nov 21, 2024
1 parent be1f752 commit 631c92c
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package collector

import (
"context"
"fmt"
"strings"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)

func TestConnectionInfoRun(t *testing.T) {
const baseExpectedMetrics = `
# HELP connection_info Information about the connection
# TYPE connection_info gauge
connection_info{db_instance_identifier="%s",provider_name="%s",region="%s"} 1
`

testCases := []struct {
name string
dsn string
expectedMetrics string
}{
{
name: "generic dsn",
dsn: "user:pass@tcp(localhost:3306)/db",
expectedMetrics: fmt.Sprintf(baseExpectedMetrics, "", "", ""),
},
{
name: "AWS/RDS dsn",
dsn: "user:pass@tcp(products-db.abc123xyz.us-east-1.rds.amazonaws.com:3306)/db",
expectedMetrics: fmt.Sprintf(baseExpectedMetrics, "products-db", "aws", "us-east-1"),
},
}

for _, tc := range testCases {
reg := prometheus.NewRegistry()

collector, err := NewConnectionInfo(ConnectionInfoArguments{
DSN: tc.dsn,
Registry: reg,
})
require.NoError(t, err)
require.NotNil(t, collector)

err = collector.Run(context.Background())
require.NoError(t, err)

err = testutil.GatherAndCompare(reg, strings.NewReader(tc.expectedMetrics))
require.NoError(t, err)
}
}

0 comments on commit 631c92c

Please sign in to comment.