Skip to content

Commit 5c3fdda

Browse files
committed
test: Add e2e test to access instance using DNS SRV record. part of #842
WIP: e2e test changes
1 parent d97b2e2 commit 5c3fdda

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

e2e_mysql_test.go

+43-13
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
package cloudsqlconn_test
1616

1717
import (
18+
"context"
1819
"database/sql"
20+
"fmt"
1921
"os"
2022
"testing"
2123
"time"
2224

2325
"cloud.google.com/go/cloudsqlconn"
26+
"cloud.google.com/go/cloudsqlconn/instance"
2427
"cloud.google.com/go/cloudsqlconn/mysql/mysql"
2528
gomysql "github.com/go-sql-driver/mysql"
2629
)
@@ -54,25 +57,52 @@ func requireMySQLVars(t *testing.T) {
5457
}
5558
}
5659

60+
type mockResolver struct {
61+
}
62+
63+
func (r *mockResolver) Resolve(_ context.Context, name string) (instanceName instance.ConnName, err error) {
64+
if name == "mysql.example.com" {
65+
return instance.ParseConnNameWithDomainName(mysqlConnName, "mysql.example.com")
66+
}
67+
return instance.ConnName{}, fmt.Errorf("no resolution for %v", name)
68+
}
69+
5770
func TestMySQLDriver(t *testing.T) {
5871
if testing.Short() {
5972
t.Skip("skipping MySQL integration tests")
6073
}
6174

6275
tcs := []struct {
63-
desc string
64-
driverName string
65-
opts []cloudsqlconn.Option
76+
desc string
77+
driverName string
78+
instanceName string
79+
user string
80+
password string
81+
opts []cloudsqlconn.Option
6682
}{
6783
{
68-
desc: "default options",
69-
driverName: "cloudsql-mysql",
70-
opts: nil,
84+
desc: "default options",
85+
driverName: "cloudsql-mysql",
86+
opts: nil,
87+
instanceName: mysqlConnName,
88+
user: mysqlUser,
89+
password: mysqlPass,
90+
},
91+
{
92+
desc: "auto IAM authn",
93+
driverName: "cloudsql-mysql-iam",
94+
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
95+
instanceName: mysqlIAMConnName,
96+
user: mysqlIAMUser,
97+
password: "password",
7198
},
7299
{
73-
desc: "auto IAM authn",
74-
driverName: "cloudsql-mysql-iam",
75-
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
100+
desc: "with dns",
101+
driverName: "cloudsql-mysql-dns",
102+
opts: []cloudsqlconn.Option{cloudsqlconn.WithResolver(&mockResolver{})},
103+
instanceName: "mysql.example.com",
104+
user: mysqlUser,
105+
password: mysqlPass,
76106
},
77107
}
78108

@@ -85,18 +115,18 @@ func TestMySQLDriver(t *testing.T) {
85115
}
86116
t.Log(now)
87117
}
88-
cleanup, err := mysql.RegisterDriver(tc.driverName)
118+
cleanup, err := mysql.RegisterDriver(tc.driverName, tc.opts...)
89119
if err != nil {
90120
t.Fatalf("failed to register driver: %v", err)
91121
}
92122
defer cleanup()
93123
cfg := gomysql.NewConfig()
94124
cfg.CheckConnLiveness = true
95-
cfg.User = mysqlUser
96-
cfg.Passwd = mysqlPass
125+
cfg.User = tc.user
126+
cfg.Passwd = tc.password
97127
cfg.DBName = mysqlDB
98128
cfg.Net = tc.driverName
99-
cfg.Addr = mysqlConnName
129+
cfg.Addr = tc.instanceName
100130
cfg.Params = map[string]string{"parseTime": "true"}
101131

102132
db, err := sql.Open("mysql", cfg.FormatDSN())

e2e_postgres_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"cloud.google.com/go/cloudsqlconn"
31+
"cloud.google.com/go/cloudsqlconn/instance"
3132
"github.com/jackc/pgx/v5/pgxpool"
3233
"golang.org/x/oauth2"
3334
"golang.org/x/oauth2/google"
@@ -161,6 +162,49 @@ func TestPostgresCASConnect(t *testing.T) {
161162
t.Log(now)
162163
}
163164

165+
type pgMockResolver struct {
166+
}
167+
168+
func (r *pgMockResolver) Resolve(_ context.Context, name string) (instanceName instance.ConnName, err error) {
169+
if name == "pg.example.com" {
170+
return instance.ParseConnNameWithDomainName(postgresConnName, "pg.example.com")
171+
}
172+
return instance.ConnName{}, fmt.Errorf("no resolution for %v", name)
173+
}
174+
175+
func TestPostgresPgxPoolConnectDomainName(t *testing.T) {
176+
if testing.Short() {
177+
t.Skip("skipping Postgres integration tests")
178+
}
179+
requirePostgresVars(t)
180+
pgxv5.RegisterDriver("pgxpool-connect")
181+
182+
ctx := context.Background()
183+
184+
// Configure the driver to connect to the database
185+
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresPass, postgresDB)
186+
config, err := pgxpool.ParseConfig(dsn)
187+
if err != nil {
188+
t.Fatalf("failed to parse pgx config: %v", err)
189+
}
190+
191+
// Create a new dialer with any options
192+
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithResolver(&pgMockResolver{}))
193+
if err != nil {
194+
t.Fatalf("failed to init Dialer: %v", err)
195+
}
196+
197+
// call cleanup when you're done with the database connection to close dialer
198+
cleanup := func() { d.Close() }
199+
t.Cleanup(cleanup)
200+
201+
// Tell the driver to use the Cloud SQL Go Connector to create connections
202+
// postgresConnName takes the form of 'project:region:instance'.
203+
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, addr string) (net.Conn, error) {
204+
return d.Dial(ctx, "pg.example.com") //TODO: Replace this with a real DNS instance.
205+
}
206+
}
207+
164208
func TestPostgresConnectWithIAMUser(t *testing.T) {
165209
if testing.Short() {
166210
t.Skip("skipping Postgres integration tests")
@@ -264,6 +308,7 @@ func TestPostgresV5Hook(t *testing.T) {
264308
driver string
265309
source string
266310
IAMAuthN bool
311+
resolver bool
267312
}{
268313
{
269314
driver: "cloudsql-postgres-v5",
@@ -277,6 +322,13 @@ func TestPostgresV5Hook(t *testing.T) {
277322
postgresConnName, postgresUserIAM, postgresDB),
278323
IAMAuthN: true,
279324
},
325+
{
326+
driver: "cloudsql-postgres-v5-dns",
327+
source: fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
328+
"pg.example.com", postgresUser, postgresPass, postgresDB),
329+
IAMAuthN: false,
330+
resolver: true,
331+
},
280332
}
281333

282334
if testing.Short() {
@@ -293,6 +345,8 @@ func TestPostgresV5Hook(t *testing.T) {
293345
for _, tc := range tests {
294346
if tc.IAMAuthN {
295347
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithIAMAuthN())
348+
} else if tc.resolver {
349+
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithResolver(&pgMockResolver{}))
296350
} else {
297351
pgxv5.RegisterDriver(tc.driver)
298352
}

0 commit comments

Comments
 (0)