@@ -28,6 +28,7 @@ import (
28
28
"time"
29
29
30
30
"cloud.google.com/go/cloudsqlconn"
31
+ "cloud.google.com/go/cloudsqlconn/instance"
31
32
"github.com/jackc/pgx/v5/pgxpool"
32
33
"golang.org/x/oauth2"
33
34
"golang.org/x/oauth2/google"
@@ -161,6 +162,49 @@ func TestPostgresCASConnect(t *testing.T) {
161
162
t .Log (now )
162
163
}
163
164
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
+
164
208
func TestPostgresConnectWithIAMUser (t * testing.T ) {
165
209
if testing .Short () {
166
210
t .Skip ("skipping Postgres integration tests" )
@@ -264,6 +308,7 @@ func TestPostgresV5Hook(t *testing.T) {
264
308
driver string
265
309
source string
266
310
IAMAuthN bool
311
+ resolver bool
267
312
}{
268
313
{
269
314
driver : "cloudsql-postgres-v5" ,
@@ -277,6 +322,13 @@ func TestPostgresV5Hook(t *testing.T) {
277
322
postgresConnName , postgresUserIAM , postgresDB ),
278
323
IAMAuthN : true ,
279
324
},
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
+ },
280
332
}
281
333
282
334
if testing .Short () {
@@ -293,6 +345,8 @@ func TestPostgresV5Hook(t *testing.T) {
293
345
for _ , tc := range tests {
294
346
if tc .IAMAuthN {
295
347
pgxv5 .RegisterDriver (tc .driver , cloudsqlconn .WithIAMAuthN ())
348
+ } else if tc .resolver {
349
+ pgxv5 .RegisterDriver (tc .driver , cloudsqlconn .WithResolver (& pgMockResolver {}))
296
350
} else {
297
351
pgxv5 .RegisterDriver (tc .driver )
298
352
}
0 commit comments