Skip to content

Commit 7214664

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

File tree

2 files changed

+110
-13
lines changed

2 files changed

+110
-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

+67
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,62 @@ 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, _ string) (net.Conn, error) {
204+
return d.Dial(ctx, "pg.example.com") //TODO: Replace this with a real DNS instance.
205+
}
206+
207+
pool, err := pgxpool.NewWithConfig(ctx, config)
208+
if err != nil {
209+
t.Fatalf("failed to create pool: %s", err)
210+
}
211+
defer pool.Close()
212+
213+
var now time.Time
214+
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
215+
if err != nil {
216+
t.Fatalf("QueryRow failed: %s", err)
217+
}
218+
t.Log(now)
219+
}
220+
164221
func TestPostgresConnectWithIAMUser(t *testing.T) {
165222
if testing.Short() {
166223
t.Skip("skipping Postgres integration tests")
@@ -264,6 +321,7 @@ func TestPostgresV5Hook(t *testing.T) {
264321
driver string
265322
source string
266323
IAMAuthN bool
324+
resolver bool
267325
}{
268326
{
269327
driver: "cloudsql-postgres-v5",
@@ -277,6 +335,13 @@ func TestPostgresV5Hook(t *testing.T) {
277335
postgresConnName, postgresUserIAM, postgresDB),
278336
IAMAuthN: true,
279337
},
338+
{
339+
driver: "cloudsql-postgres-v5-dns",
340+
source: fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
341+
"pg.example.com", postgresUser, postgresPass, postgresDB),
342+
IAMAuthN: false,
343+
resolver: true,
344+
},
280345
}
281346

282347
if testing.Short() {
@@ -293,6 +358,8 @@ func TestPostgresV5Hook(t *testing.T) {
293358
for _, tc := range tests {
294359
if tc.IAMAuthN {
295360
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithIAMAuthN())
361+
} else if tc.resolver {
362+
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithResolver(&pgMockResolver{}))
296363
} else {
297364
pgxv5.RegisterDriver(tc.driver)
298365
}

0 commit comments

Comments
 (0)