Skip to content

Commit 5b9d984

Browse files
authored
test: update ip type dialer option for e2e tests (GoogleCloudPlatform#963)
1 parent b91df10 commit 5b9d984

File tree

3 files changed

+134
-34
lines changed

3 files changed

+134
-34
lines changed

e2e_mysql_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ var (
3737
mysqlPass = os.Getenv("MYSQL_PASS")
3838
// Name of the database to connect to.
3939
mysqlDB = os.Getenv("MYSQL_DB")
40+
// IP Type of the MySQL instance to connect to.
41+
ipType = os.Getenv("IP_TYPE")
4042
)
4143

4244
func requireMySQLVars(t *testing.T) {
@@ -57,6 +59,11 @@ func TestMySQLDriver(t *testing.T) {
5759
t.Skip("skipping MySQL integration tests")
5860
}
5961

62+
var opts []cloudsqlconn.Option
63+
if ipType == "private" {
64+
opts = append(opts, cloudsqlconn.WithDefaultDialOptions(cloudsqlconn.WithPrivateIP()))
65+
}
66+
6067
tcs := []struct {
6168
desc string
6269
driverName string
@@ -68,15 +75,15 @@ func TestMySQLDriver(t *testing.T) {
6875
{
6976
desc: "default options",
7077
driverName: "cloudsql-mysql",
71-
opts: nil,
78+
opts: opts,
7279
instanceName: mysqlConnName,
7380
user: mysqlUser,
7481
password: mysqlPass,
7582
},
7683
{
7784
desc: "auto IAM authn",
7885
driverName: "cloudsql-mysql-iam",
79-
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
86+
opts: append(opts, cloudsqlconn.WithIAMAuthN()),
8087
instanceName: mysqlConnName,
8188
user: mysqlIAMUser,
8289
password: "password",

e2e_postgres_test.go

Lines changed: 118 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ var (
5151
postgresSANInvalidDomainName = os.Getenv("POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME") // Cloud SQL Postgres CAS domain name that IS NOT an instance Custom SAN of the postgresSANConnName instance.
5252
)
5353

54+
func AddIPTypeOptions(opts []cloudsqlconn.Option) []cloudsqlconn.Option {
55+
if ipType == "private" {
56+
return append(opts, cloudsqlconn.WithDefaultDialOptions(cloudsqlconn.WithPrivateIP()))
57+
}
58+
return opts
59+
}
60+
5461
func requirePostgresVars(t *testing.T) {
5562
switch "" {
5663
case postgresConnName:
@@ -95,8 +102,12 @@ func TestPostgresPgxPoolConnect(t *testing.T) {
95102
t.Fatalf("failed to parse pgx config: %v", err)
96103
}
97104

105+
// Use WithPrivateIP option if the ipType is set to private
106+
var opts []cloudsqlconn.Option
107+
opts = AddIPTypeOptions(opts)
108+
98109
// Create a new dialer with any options
99-
d, err := cloudsqlconn.NewDialer(ctx)
110+
d, err := cloudsqlconn.NewDialer(ctx, opts...)
100111
if err != nil {
101112
t.Fatalf("failed to init Dialer: %v", err)
102113
}
@@ -143,8 +154,12 @@ func TestPostgresCASConnect(t *testing.T) {
143154
t.Fatalf("failed to parse pgx config: %v", err)
144155
}
145156

157+
// Use WithPrivateIP option if the ipType is set to private
158+
var opts []cloudsqlconn.Option
159+
opts = AddIPTypeOptions(opts)
160+
146161
// Create a new dialer with any options
147-
d, err := cloudsqlconn.NewDialer(ctx)
162+
d, err := cloudsqlconn.NewDialer(ctx, opts...)
148163
if err != nil {
149164
t.Fatalf("failed to init Dialer: %v", err)
150165
}
@@ -191,8 +206,12 @@ func TestPostgresConnectWithQuotaProject(t *testing.T) {
191206
t.Fatalf("failed to parse pgx config: %v", err)
192207
}
193208

209+
// Use WithPrivateIP option if the ipType is set to private
210+
var opts []cloudsqlconn.Option
211+
opts = AddIPTypeOptions(opts)
212+
194213
// Create a new dialer with any options
195-
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithQuotaProject(project))
214+
d, err := cloudsqlconn.NewDialer(ctx, append(opts, cloudsqlconn.WithQuotaProject(project))...)
196215
if err != nil {
197216
t.Fatalf("failed to init Dialer: %v", err)
198217
}
@@ -239,8 +258,12 @@ func TestPostgresCustomerCASConnect(t *testing.T) {
239258
t.Fatalf("failed to parse pgx config: %v", err)
240259
}
241260

261+
// Use WithPrivateIP option if the ipType is set to private
262+
var opts []cloudsqlconn.Option
263+
opts = AddIPTypeOptions(opts)
264+
242265
// Create a new dialer with any options
243-
d, err := cloudsqlconn.NewDialer(ctx)
266+
d, err := cloudsqlconn.NewDialer(ctx, opts...)
244267
if err != nil {
245268
t.Fatalf("failed to init Dialer: %v", err)
246269
}
@@ -287,8 +310,12 @@ func TestPostgresSANDomainConnect(t *testing.T) {
287310
t.Fatalf("failed to parse pgx config: %v", err)
288311
}
289312

313+
// Use WithPrivateIP option if the ipType is set to private
314+
var opts []cloudsqlconn.Option
315+
opts = AddIPTypeOptions(opts)
316+
290317
// Create a new dialer with any options
291-
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithDNSResolver())
318+
d, err := cloudsqlconn.NewDialer(ctx, append(opts, cloudsqlconn.WithDNSResolver())...)
292319

293320
if err != nil {
294321
t.Fatalf("failed to init Dialer: %v", err)
@@ -336,8 +363,12 @@ func TestPostgresSANBadDomainCausesConnectError(t *testing.T) {
336363
t.Fatalf("failed to parse pgx config: %v", err)
337364
}
338365

366+
// Use WithPrivateIP option if the ipType is set to private
367+
var opts []cloudsqlconn.Option
368+
opts = AddIPTypeOptions(opts)
369+
339370
// Create a new dialer with any options
340-
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithDNSResolver())
371+
d, err := cloudsqlconn.NewDialer(ctx, append(opts, cloudsqlconn.WithDNSResolver())...)
341372

342373
if err != nil {
343374
t.Fatalf("failed to init Dialer: %v", err)
@@ -389,8 +420,12 @@ func TestPostgresPgxPoolConnectDomainName(t *testing.T) {
389420
t.Fatalf("failed to parse pgx config: %v", err)
390421
}
391422

423+
// Use WithPrivateIP option if the ipType is set to private
424+
var opts []cloudsqlconn.Option
425+
opts = AddIPTypeOptions(opts)
426+
392427
// Create a new dialer with any options
393-
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithDNSResolver())
428+
d, err := cloudsqlconn.NewDialer(ctx, append(opts, cloudsqlconn.WithDNSResolver())...)
394429
if err != nil {
395430
t.Fatalf("failed to init Dialer: %v", err)
396431
}
@@ -433,7 +468,12 @@ func TestPostgresConnectWithIAMUser(t *testing.T) {
433468
if err != nil {
434469
t.Fatalf("failed to parse pgx config: %v", err)
435470
}
436-
d, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithIAMAuthN())
471+
472+
// Use WithPrivateIP option if the ipType is set to private
473+
var opts []cloudsqlconn.Option
474+
opts = AddIPTypeOptions(opts)
475+
476+
d, err := cloudsqlconn.NewDialer(ctx, append(opts, cloudsqlconn.WithIAMAuthN())...)
437477
if err != nil {
438478
t.Fatalf("failed to initiate Dialer: %v", err)
439479
}
@@ -471,10 +511,14 @@ func TestPostgresConnectWithLazyRefresh(t *testing.T) {
471511
if err != nil {
472512
t.Fatalf("failed to parse pgx config: %v", err)
473513
}
514+
515+
// Use WithPrivateIP option if the ipType is set to private
516+
var opts []cloudsqlconn.Option
517+
opts = AddIPTypeOptions(opts)
518+
474519
d, err := cloudsqlconn.NewDialer(
475520
ctx,
476-
cloudsqlconn.WithLazyRefresh(),
477-
cloudsqlconn.WithIAMAuthN(), // use IAM AuthN to exercise all paths
521+
append(opts, cloudsqlconn.WithLazyRefresh(), cloudsqlconn.WithIAMAuthN())...,
478522
)
479523
if err != nil {
480524
t.Fatalf("failed to initiate Dialer: %v", err)
@@ -504,7 +548,12 @@ func TestPostgresEngineVersion(t *testing.T) {
504548
}
505549
ctx, cancel := context.WithCancel(context.Background())
506550
defer cancel()
507-
d, err := cloudsqlconn.NewDialer(context.Background())
551+
552+
// Use WithPrivateIP option if the ipType is set to private
553+
var opts []cloudsqlconn.Option
554+
opts = AddIPTypeOptions(opts)
555+
556+
d, err := cloudsqlconn.NewDialer(context.Background(), opts...)
508557
if err != nil {
509558
t.Fatalf("failed to init Dialer: %v", err)
510559
}
@@ -518,30 +567,39 @@ func TestPostgresEngineVersion(t *testing.T) {
518567
}
519568

520569
func TestPostgresV5Hook(t *testing.T) {
570+
571+
// Use WithPrivateIP option if the ipType is set to private
572+
var opts []cloudsqlconn.Option
573+
opts = AddIPTypeOptions(opts)
574+
521575
tests := []struct {
522576
driver string
523577
source string
524578
IAMAuthN bool
525579
resolver bool
580+
opts []cloudsqlconn.Option
526581
}{
527582
{
528583
driver: "cloudsql-postgres-v5",
529584
source: fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
530585
postgresConnName, postgresUser, postgresPass, postgresDB),
531586
IAMAuthN: false,
587+
opts: opts,
532588
},
533589
{
534590
driver: "cloudsql-postgres-iam-v5",
535591
source: fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable",
536592
postgresConnName, postgresUserIAM, postgresDB),
537593
IAMAuthN: true,
594+
opts: opts,
538595
},
539596
{
540597
driver: "cloudsql-postgres-v5-dns",
541598
source: fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
542599
postgresSANDomainName, postgresUser, postgresCustomerCASPass, postgresDB),
543600
IAMAuthN: false,
544601
resolver: true,
602+
opts: opts,
545603
},
546604
}
547605

@@ -557,13 +615,15 @@ func TestPostgresV5Hook(t *testing.T) {
557615
}
558616

559617
for _, tc := range tests {
618+
var registerOpts []cloudsqlconn.Option
619+
registerOpts = append(registerOpts, tc.opts...)
560620
if tc.IAMAuthN {
561-
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithIAMAuthN())
562-
} else if tc.resolver {
563-
pgxv5.RegisterDriver(tc.driver, cloudsqlconn.WithDNSResolver())
564-
} else {
565-
pgxv5.RegisterDriver(tc.driver)
621+
registerOpts = append(registerOpts, cloudsqlconn.WithIAMAuthN())
622+
}
623+
if tc.resolver {
624+
registerOpts = append(registerOpts, cloudsqlconn.WithDNSResolver())
566625
}
626+
pgxv5.RegisterDriver(tc.driver, registerOpts...)
567627
db, err := sql.Open(tc.driver, tc.source)
568628

569629
if err != nil {
@@ -575,22 +635,30 @@ func TestPostgresV5Hook(t *testing.T) {
575635
}
576636

577637
func TestPostgresV4Hook(t *testing.T) {
638+
639+
// Use WithPrivateIP option if the ipType is set to private
640+
var opts []cloudsqlconn.Option
641+
opts = AddIPTypeOptions(opts)
642+
578643
tests := []struct {
579644
driver string
580645
source string
581646
IAMAuthN bool
647+
opts []cloudsqlconn.Option
582648
}{
583649
{
584650
driver: "cloudsql-postgres-v4",
585651
source: fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
586652
postgresConnName, postgresUser, postgresPass, postgresDB),
587653
IAMAuthN: false,
654+
opts: opts,
588655
},
589656
{
590657
driver: "cloudsql-postgres-iam-v4",
591658
source: fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable",
592659
postgresConnName, postgresUserIAM, postgresDB),
593660
IAMAuthN: true,
661+
opts: opts,
594662
},
595663
}
596664
if testing.Short() {
@@ -605,11 +673,12 @@ func TestPostgresV4Hook(t *testing.T) {
605673
}
606674

607675
for _, tc := range tests {
676+
var registerOpts []cloudsqlconn.Option
677+
registerOpts = append(registerOpts, tc.opts...)
608678
if tc.IAMAuthN {
609-
pgxv4.RegisterDriver(tc.driver, cloudsqlconn.WithIAMAuthN())
610-
} else {
611-
pgxv4.RegisterDriver(tc.driver)
679+
registerOpts = append(registerOpts, cloudsqlconn.WithIAMAuthN())
612680
}
681+
pgxv4.RegisterDriver(tc.driver, registerOpts...)
613682
db, err := sql.Open(tc.driver, tc.source)
614683
if err != nil {
615684
t.Fatalf("sql.Open want err = nil, got = %v", err)
@@ -633,6 +702,9 @@ func removeAuthEnvVar(t *testing.T) (*oauth2.Token, string, func()) {
633702
if err != nil {
634703
t.Errorf("failed to get token: %v", err)
635704
}
705+
if ipType == "private" {
706+
return tok, "", func() {}
707+
}
636708
path, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
637709
if !ok {
638710
t.Fatalf("GOOGLE_APPLICATION_CREDENTIALS was not set in the environment")
@@ -662,8 +734,12 @@ func TestPostgresAuthentication(t *testing.T) {
662734
t.Skip("skipping Postgres integration tests")
663735
}
664736
requirePostgresVars(t)
665-
666-
creds := keyfile(t)
737+
var creds string
738+
var opts []cloudsqlconn.Option
739+
if ipType != "private" {
740+
creds = keyfile(t)
741+
opts = AddIPTypeOptions(opts)
742+
}
667743
tok, path, cleanup := removeAuthEnvVar(t)
668744
defer cleanup()
669745

@@ -673,19 +749,30 @@ func TestPostgresAuthentication(t *testing.T) {
673749
}{
674750
{
675751
desc: "with token",
676-
opts: []cloudsqlconn.Option{cloudsqlconn.WithTokenSource(
752+
opts: append(opts, cloudsqlconn.WithTokenSource(
677753
oauth2.StaticTokenSource(tok),
678-
)},
679-
},
680-
{
681-
desc: "with credentials file",
682-
opts: []cloudsqlconn.Option{cloudsqlconn.WithCredentialsFile(path)},
683-
},
684-
{
685-
desc: "with credentials JSON",
686-
opts: []cloudsqlconn.Option{cloudsqlconn.WithCredentialsJSON([]byte(creds))},
754+
)),
687755
},
688756
}
757+
758+
if ipType != "private" {
759+
tcs = append(tcs,
760+
struct {
761+
desc string
762+
opts []cloudsqlconn.Option
763+
}{
764+
desc: "with credentials file",
765+
opts: append(opts, cloudsqlconn.WithCredentialsFile(path)),
766+
},
767+
struct {
768+
desc string
769+
opts []cloudsqlconn.Option
770+
}{
771+
desc: "with credentials JSON",
772+
opts: append(opts, cloudsqlconn.WithCredentialsJSON([]byte(creds))),
773+
},
774+
)
775+
}
689776
for _, tc := range tcs {
690777
t.Run(tc.desc, func(t *testing.T) {
691778
ctx := context.Background()

e2e_sqlserver_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
"cloud.google.com/go/cloudsqlconn"
2728
"cloud.google.com/go/cloudsqlconn/sqlserver/mssql"
2829
)
2930

@@ -53,14 +54,19 @@ func TestSqlServerHook(t *testing.T) {
5354
}
5455
requireSQLServerVars(t)
5556

57+
var opts []cloudsqlconn.Option
58+
if ipType == "private" {
59+
opts = append(opts, cloudsqlconn.WithDefaultDialOptions(cloudsqlconn.WithPrivateIP()))
60+
}
61+
5662
testConn := func(db *sql.DB) {
5763
var now time.Time
5864
if err := db.QueryRow("SELECT getdate()").Scan(&now); err != nil {
5965
t.Fatalf("QueryRow failed: %v", err)
6066
}
6167
t.Log(now)
6268
}
63-
cleanup, err := mssql.RegisterDriver("cloudsql-sqlserver")
69+
cleanup, err := mssql.RegisterDriver("cloudsql-sqlserver", opts...)
6470
if err != nil {
6571
t.Fatalf("failed to register driver: %v", err)
6672
}

0 commit comments

Comments
 (0)