-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for ConnectionInfo collector
- Loading branch information
1 parent
be1f752
commit 631c92c
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
internal/component/database_observability/mysql/collector/connection_info_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |