grafana/pkg/tsdb/grafana-postgresql-datasource/proxy_test.go
Gábor Farkas 8159e1db3a
Revert "Postgres: Switch the datasource plugin from lib/pq to pgx (#8… (#85509)
Revert "Postgres: Switch the datasource plugin from lib/pq to pgx (#83768)"

This reverts commit ecd6de826a.
2024-04-03 11:02:22 +02:00

40 lines
893 B
Go

package postgres
import (
"database/sql"
"fmt"
"net"
"testing"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"golang.org/x/net/proxy"
)
type testDialer struct {
}
func (d *testDialer) Dial(network, addr string) (c net.Conn, err error) {
return nil, fmt.Errorf("test-dialer is not functional")
}
var _ proxy.Dialer = (&testDialer{})
func TestPostgresProxyDriver(t *testing.T) {
dbURL := "localhost:5432"
cnnstr := fmt.Sprintf("postgres://auser:password@%s/db?sslmode=disable", dbURL)
t.Run("Connector should use dialer context that routes through the socks proxy to db", func(t *testing.T) {
connector, err := pq.NewConnector(cnnstr)
require.NoError(t, err)
dialer := newPostgresProxyDialer(&testDialer{})
connector.Dialer(dialer)
db := sql.OpenDB(connector)
err = db.Ping()
require.Contains(t, err.Error(), "test-dialer is not functional")
})
}