Skip to content

Commit

Permalink
test: Add e2e test to access instance using DNS SRV record. part of #842
Browse files Browse the repository at this point in the history


WIP: e2e test changes
  • Loading branch information
hessjcg committed Aug 2, 2024
1 parent 490e76a commit 8f8263a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 13 deletions.
56 changes: 43 additions & 13 deletions e2e_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
package cloudsqlconn_test

import (
"context"
"database/sql"
"fmt"
"os"
"testing"
"time"

"cloud.google.com/go/cloudsqlconn"
"cloud.google.com/go/cloudsqlconn/instance"
"cloud.google.com/go/cloudsqlconn/mysql/mysql"
gomysql "github.com/go-sql-driver/mysql"
)
Expand Down Expand Up @@ -54,25 +57,52 @@ func requireMySQLVars(t *testing.T) {
}
}

type mockResolver struct {
}

func (r *mockResolver) Resolve(_ context.Context, name string) (instanceName instance.ConnName, err error) {
if name == "mysql.example.com" {
return instance.ParseConnNameWithDomainName(mysqlConnName, "mysql.example.com")
}
return instance.ConnName{}, fmt.Errorf("no resolution for %v", name)
}

func TestMySQLDriver(t *testing.T) {
if testing.Short() {
t.Skip("skipping MySQL integration tests")
}

tcs := []struct {
desc string
driverName string
opts []cloudsqlconn.Option
desc string
driverName string
instanceName string
user string
password string
opts []cloudsqlconn.Option
}{
{
desc: "default options",
driverName: "cloudsql-mysql",
opts: nil,
desc: "default options",
driverName: "cloudsql-mysql",
opts: nil,
instanceName: mysqlConnName,
user: mysqlUser,
password: mysqlPass,
},
{
desc: "auto IAM authn",
driverName: "cloudsql-mysql-iam",
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
instanceName: mysqlIAMConnName,
user: mysqlIAMUser,
password: "password",
},
{
desc: "auto IAM authn",
driverName: "cloudsql-mysql-iam",
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
desc: "with dns",
driverName: "cloudsql-mysql-dns",
opts: []cloudsqlconn.Option{cloudsqlconn.WithResolver(&mockResolver{})},
instanceName: "mysql.example.com",
user: mysqlUser,
password: mysqlPass,
},
}

Expand All @@ -85,18 +115,18 @@ func TestMySQLDriver(t *testing.T) {
}
t.Log(now)
}
cleanup, err := mysql.RegisterDriver(tc.driverName)
cleanup, err := mysql.RegisterDriver(tc.driverName, tc.opts...)
if err != nil {
t.Fatalf("failed to register driver: %v", err)
}
defer cleanup()
cfg := gomysql.NewConfig()
cfg.CheckConnLiveness = true
cfg.User = mysqlUser
cfg.Passwd = mysqlPass
cfg.User = tc.user
cfg.Passwd = tc.password
cfg.DBName = mysqlDB
cfg.Net = tc.driverName
cfg.Addr = mysqlConnName
cfg.Addr = tc.instanceName
cfg.Params = map[string]string{"parseTime": "true"}

db, err := sql.Open("mysql", cfg.FormatDSN())
Expand Down
70 changes: 70 additions & 0 deletions e2e_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"cloud.google.com/go/cloudsqlconn"
"cloud.google.com/go/cloudsqlconn/instance"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
Expand Down Expand Up @@ -107,6 +108,65 @@ func TestPostgresPgxPoolConnect(t *testing.T) {
t.Log(now)
}

type pgMockResolver struct {
}

func (r *pgMockResolver) Resolve(_ context.Context, name string) (instanceName instance.ConnName, err error) {
if name == "pg.example.com" {
return instance.ParseConnNameWithDomainName(postgresConnName, "pg.example.com")
}
return instance.ConnName{}, fmt.Errorf("no resolution for %v", name)
}

func TestPostgresPgxPoolConnectDomainName(t *testing.T) {
if testing.Short() {
t.Skip("skipping Postgres integration tests")
}
requirePostgresVars(t)
pgxv5.RegisterDriver("pgxpool-connect")

ctx := context.Background()

// Configure the driver to connect to the database
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresPass, postgresDB)
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
t.Fatalf("failed to parse pgx config: %v", err)
}

// Create a new dialer with any options
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithResolver(&pgMockResolver{}))
if err != nil {
t.Fatalf("failed to init Dialer: %v", err)
}

// call cleanup when you're done with the database connection to close dialer
cleanup := func() error { return d.Close() }

// Tell the driver to use the Cloud SQL Go Connector to create connections
// postgresConnName takes the form of 'project:region:instance'.
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, addr string) (net.Conn, error) {

Check failure on line 148 in e2e_postgres_test.go

View workflow job for this annotation

GitHub Actions / Run lint

unused-parameter: parameter 'addr' seems to be unused, consider removing or renaming it as _ (revive)
return d.Dial(ctx, "pg.example.com")
}

// Interact with the driver directly as you normally would
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
t.Fatalf("failed to create pool: %s", err)
}
// ... etc

defer cleanup()
defer pool.Close()

var now time.Time
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
if err != nil {
t.Fatalf("QueryRow failed: %s", err)
}
t.Log(now)
}

func TestPostgresConnectWithIAMUser(t *testing.T) {
if testing.Short() {
t.Skip("skipping Postgres integration tests")
Expand Down Expand Up @@ -210,6 +270,7 @@ func TestPostgresV5Hook(t *testing.T) {
driver string
source string
IAMAuthN bool
resolver bool
}{
{
driver: "cloudsql-postgres-v5",
Expand All @@ -223,6 +284,13 @@ func TestPostgresV5Hook(t *testing.T) {
postgresConnName, postgresUserIAM, postgresDB),
IAMAuthN: true,
},
{
driver: "cloudsql-postgres-v5-dns",
source: fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
"pg.example.com", postgresUser, postgresPass, postgresDB),
IAMAuthN: false,
resolver: true,
},
}

if testing.Short() {
Expand All @@ -239,6 +307,8 @@ func TestPostgresV5Hook(t *testing.T) {
for _, tc := range tests {
if tc.IAMAuthN {
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithIAMAuthN())
} else if tc.resolver {
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithResolver(&pgMockResolver{}))
} else {
pgxv5.RegisterDriver(tc.driver)
}
Expand Down

0 comments on commit 8f8263a

Please sign in to comment.