Skip to content

Commit 3e8203a

Browse files
authored
test: Integration tests for Customer Certificate Authority support (#900)
Add integration test for instance that uses a private CA.
1 parent a8ed925 commit 3e8203a

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

.github/workflows/tests.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ jobs:
7676
POSTGRES_DB:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_DB
7777
POSTGRES_CAS_CONNECTION_NAME:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_CAS_CONNECTION_NAME
7878
POSTGRES_CAS_PASS:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_CAS_PASS
79+
POSTGRES_CUSTOMER_CAS_CONNECTION_NAME:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_CUSTOMER_CAS_CONNECTION_NAME
80+
POSTGRES_CUSTOMER_CAS_PASS:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_CUSTOMER_CAS_PASS
7981
SQLSERVER_CONNECTION_NAME:${{ vars.GOOGLE_CLOUD_PROJECT }}/SQLSERVER_CONNECTION_NAME
8082
SQLSERVER_USER:${{ vars.GOOGLE_CLOUD_PROJECT }}/SQLSERVER_USER
8183
SQLSERVER_PASS:${{ vars.GOOGLE_CLOUD_PROJECT }}/SQLSERVER_PASS
@@ -96,6 +98,8 @@ jobs:
9698
POSTGRES_DB: "${{ steps.secrets.outputs.POSTGRES_DB }}"
9799
POSTGRES_CAS_CONNECTION_NAME: "${{ steps.secrets.outputs.POSTGRES_CAS_CONNECTION_NAME }}"
98100
POSTGRES_CAS_PASS: "${{ steps.secrets.outputs.POSTGRES_CAS_PASS }}"
101+
POSTGRES_CUSTOMER_CAS_CONNECTION_NAME: "${{ steps.secrets.outputs.POSTGRES_CUSTOMER_CAS_CONNECTION_NAME }}"
102+
POSTGRES_CUSTOMER_CAS_PASS: "${{ steps.secrets.outputs.POSTGRES_CUSTOMER_CAS_PASS }}"
99103
SQLSERVER_CONNECTION_NAME: "${{ steps.secrets.outputs.SQLSERVER_CONNECTION_NAME }}"
100104
SQLSERVER_USER: "${{ steps.secrets.outputs.SQLSERVER_USER }}"
101105
SQLSERVER_PASS: "${{ steps.secrets.outputs.SQLSERVER_PASS }}"

e2e_postgres_test.go

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ import (
3838
)
3939

4040
var (
41-
postgresConnName = os.Getenv("POSTGRES_CONNECTION_NAME") // "Cloud SQL Postgres instance connection name, in the form of 'project:region:instance'.
42-
postgresCASConnName = os.Getenv("POSTGRES_CAS_CONNECTION_NAME") // "Cloud SQL Postgres CAS instance connection name, in the form of 'project:region:instance'.
43-
postgresUser = os.Getenv("POSTGRES_USER") // Name of database user.
44-
postgresPass = os.Getenv("POSTGRES_PASS") // Password for the database user; be careful when entering a password on the command line (it may go into your terminal's history).
45-
postgresCASPass = os.Getenv("POSTGRES_CAS_PASS") // Password for the database user for CAS instances; be careful when entering a password on the command line (it may go into your terminal's history).
46-
postgresDB = os.Getenv("POSTGRES_DB") // Name of the database to connect to.
47-
postgresUserIAM = os.Getenv("POSTGRES_USER_IAM") // Name of database IAM user.
41+
postgresConnName = os.Getenv("POSTGRES_CONNECTION_NAME") // "Cloud SQL Postgres instance connection name, in the form of 'project:region:instance'.
42+
postgresCASConnName = os.Getenv("POSTGRES_CAS_CONNECTION_NAME") // "Cloud SQL Postgres CAS instance connection name, in the form of 'project:region:instance'.
43+
postgresCustomerCASConnName = os.Getenv("POSTGRES_CUSTOMER_CAS_CONNECTION_NAME") // "Cloud SQL Postgres Customer CAS instance connection name, in the form of 'project:region:instance'.
44+
postgresUser = os.Getenv("POSTGRES_USER") // Name of database user.
45+
postgresPass = os.Getenv("POSTGRES_PASS") // Password for the database user; be careful when entering a password on the command line (it may go into your terminal's history).
46+
postgresCASPass = os.Getenv("POSTGRES_CAS_PASS") // Password for the database user for CAS instances; be careful when entering a password on the command line (it may go into your terminal's history).
47+
postgresCustomerCASPass = os.Getenv("POSTGRES_CUSTOMER_CAS_PASS") // Password for the database user for customer CAS instances; be careful when entering a password on the command line (it may go into your terminal's history).
48+
postgresDB = os.Getenv("POSTGRES_DB") // Name of the database to connect to.
49+
postgresUserIAM = os.Getenv("POSTGRES_USER_IAM") // Name of database IAM user.
4850
)
4951

5052
func requirePostgresVars(t *testing.T) {
@@ -53,12 +55,16 @@ func requirePostgresVars(t *testing.T) {
5355
t.Fatal("'POSTGRES_CONNECTION_NAME' env var not set")
5456
case postgresCASConnName:
5557
t.Fatal("'POSTGRES_CAS_CONNECTION_NAME' env var not set")
58+
case postgresCustomerCASConnName:
59+
t.Fatal("'POSTGRES_CUSTOMER_CAS_CONNECTION_NAME' env var not set")
5660
case postgresUser:
5761
t.Fatal("'POSTGRES_USER' env var not set")
5862
case postgresPass:
5963
t.Fatal("'POSTGRES_PASS' env var not set")
6064
case postgresCASPass:
6165
t.Fatal("'POSTGRES_CAS_PASS' env var not set")
66+
case postgresCustomerCASPass:
67+
t.Fatal("'POSTGRES_CUSTOMER_CAS_PASS' env var not set")
6268
case postgresDB:
6369
t.Fatal("'POSTGRES_DB' env var not set")
6470
case postgresUserIAM:
@@ -162,6 +168,54 @@ func TestPostgresCASConnect(t *testing.T) {
162168
t.Log(now)
163169
}
164170

171+
func TestPostgresCustomerCASConnect(t *testing.T) {
172+
if testing.Short() {
173+
t.Skip("skipping Postgres integration tests")
174+
}
175+
requirePostgresVars(t)
176+
177+
ctx := context.Background()
178+
179+
// Configure the driver to connect to the database
180+
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresCustomerCASPass, postgresDB)
181+
config, err := pgxpool.ParseConfig(dsn)
182+
if err != nil {
183+
t.Fatalf("failed to parse pgx config: %v", err)
184+
}
185+
186+
// Create a new dialer with any options
187+
d, err := cloudsqlconn.NewDialer(ctx)
188+
if err != nil {
189+
t.Fatalf("failed to init Dialer: %v", err)
190+
}
191+
192+
// call cleanup when you're done with the database connection to close dialer
193+
cleanup := func() error { return d.Close() }
194+
195+
// Tell the driver to use the Cloud SQL Go Connector to create connections
196+
// postgresConnName takes the form of 'project:region:instance'.
197+
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
198+
return d.Dial(ctx, postgresCustomerCASConnName)
199+
}
200+
201+
// Interact with the driver directly as you normally would
202+
pool, err := pgxpool.NewWithConfig(ctx, config)
203+
if err != nil {
204+
t.Fatalf("failed to create pool: %s", err)
205+
}
206+
// ... etc
207+
208+
defer cleanup()
209+
defer pool.Close()
210+
211+
var now time.Time
212+
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
213+
if err != nil {
214+
t.Fatalf("QueryRow failed: %s", err)
215+
}
216+
t.Log(now)
217+
}
218+
165219
type pgMockResolver struct {
166220
}
167221

0 commit comments

Comments
 (0)