Skip to content

Commit 1e0296b

Browse files
committed
test: Add e2e test to access instance using DNS SRV record. part of #842
1 parent fca359a commit 1e0296b

File tree

2 files changed

+107
-13
lines changed

2 files changed

+107
-13
lines changed

e2e_mysql_test.go

+46-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package cloudsqlconn_test
1616

1717
import (
18+
"context"
1819
"database/sql"
20+
"fmt"
21+
"net"
1922
"os"
2023
"testing"
2124
"time"
@@ -54,25 +57,55 @@ func requireMySQLVars(t *testing.T) {
5457
}
5558
}
5659

60+
type mockResolver struct {
61+
}
62+
63+
func (r *mockResolver) LookupSRV(_ context.Context, _, _, name string) (cname string, addrs []*net.SRV, err error) {
64+
if name == "mysql.example.com" {
65+
return "", []*net.SRV{
66+
&net.SRV{Target: mysqlConnName},
67+
}, nil
68+
}
69+
70+
return "", nil, fmt.Errorf("no resolution for %v", name)
71+
}
72+
5773
func TestMySQLDriver(t *testing.T) {
5874
if testing.Short() {
5975
t.Skip("skipping MySQL integration tests")
6076
}
6177

6278
tcs := []struct {
63-
desc string
64-
driverName string
65-
opts []cloudsqlconn.Option
79+
desc string
80+
driverName string
81+
instanceName string
82+
user string
83+
password string
84+
opts []cloudsqlconn.Option
6685
}{
6786
{
68-
desc: "default options",
69-
driverName: "cloudsql-mysql",
70-
opts: nil,
87+
desc: "default options",
88+
driverName: "cloudsql-mysql",
89+
opts: nil,
90+
instanceName: mysqlConnName,
91+
user: mysqlUser,
92+
password: mysqlPass,
93+
},
94+
{
95+
desc: "auto IAM authn",
96+
driverName: "cloudsql-mysql-iam",
97+
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
98+
instanceName: mysqlIAMConnName,
99+
user: mysqlIAMUser,
100+
password: "password",
71101
},
72102
{
73-
desc: "auto IAM authn",
74-
driverName: "cloudsql-mysql-iam",
75-
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
103+
desc: "with dns",
104+
driverName: "cloudsql-mysql-dns",
105+
opts: []cloudsqlconn.Option{cloudsqlconn.WithResolver(&mockResolver{})},
106+
instanceName: "mysql.example.com",
107+
user: mysqlUser,
108+
password: mysqlPass,
76109
},
77110
}
78111

@@ -85,18 +118,18 @@ func TestMySQLDriver(t *testing.T) {
85118
}
86119
t.Log(now)
87120
}
88-
cleanup, err := mysql.RegisterDriver(tc.driverName)
121+
cleanup, err := mysql.RegisterDriver(tc.driverName, tc.opts...)
89122
if err != nil {
90123
t.Fatalf("failed to register driver: %v", err)
91124
}
92125
defer cleanup()
93126
cfg := gomysql.NewConfig()
94127
cfg.CheckConnLiveness = true
95-
cfg.User = mysqlUser
96-
cfg.Passwd = mysqlPass
128+
cfg.User = tc.user
129+
cfg.Passwd = tc.password
97130
cfg.DBName = mysqlDB
98131
cfg.Net = tc.driverName
99-
cfg.Addr = mysqlConnName
132+
cfg.Addr = tc.instanceName
100133
cfg.Params = map[string]string{"parseTime": "true"}
101134

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

e2e_postgres_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,67 @@ func TestPostgresPgxPoolConnect(t *testing.T) {
107107
t.Log(now)
108108
}
109109

110+
type pgMockResolver struct {
111+
}
112+
113+
func (r *pgMockResolver) LookupSRV(_ context.Context, _, _, name string) (cname string, addrs []*net.SRV, err error) {
114+
if name == "pg.example.com" {
115+
return "", []*net.SRV{
116+
&net.SRV{Target: postgresConnName},
117+
}, nil
118+
}
119+
120+
return "", nil, fmt.Errorf("no resolution for %v", name)
121+
}
122+
123+
func TestPostgresPgxPoolConnectDomainName(t *testing.T) {
124+
if testing.Short() {
125+
t.Skip("skipping Postgres integration tests")
126+
}
127+
requirePostgresVars(t)
128+
129+
ctx := context.Background()
130+
131+
// Configure the driver to connect to the database
132+
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresPass, postgresDB)
133+
config, err := pgxpool.ParseConfig(dsn)
134+
if err != nil {
135+
t.Fatalf("failed to parse pgx config: %v", err)
136+
}
137+
138+
// Create a new dialer with any options
139+
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithResolver(&pgMockResolver{}))
140+
if err != nil {
141+
t.Fatalf("failed to init Dialer: %v", err)
142+
}
143+
144+
// call cleanup when you're done with the database connection to close dialer
145+
cleanup := func() error { return d.Close() }
146+
147+
// Tell the driver to use the Cloud SQL Go Connector to create connections
148+
// postgresConnName takes the form of 'project:region:instance'.
149+
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, addr string) (net.Conn, error) {
150+
return d.Dial(ctx, "pg.example.com")
151+
}
152+
153+
// Interact with the driver directly as you normally would
154+
pool, err := pgxpool.NewWithConfig(ctx, config)
155+
if err != nil {
156+
t.Fatalf("failed to create pool: %s", err)
157+
}
158+
// ... etc
159+
160+
defer cleanup()
161+
defer pool.Close()
162+
163+
var now time.Time
164+
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
165+
if err != nil {
166+
t.Fatalf("QueryRow failed: %s", err)
167+
}
168+
t.Log(now)
169+
}
170+
110171
func TestPostgresConnectWithIAMUser(t *testing.T) {
111172
if testing.Short() {
112173
t.Skip("skipping Postgres integration tests")

0 commit comments

Comments
 (0)