Skip to content

Commit 7545140

Browse files
authored
test: Integration tests for CAS instance with public ip (#2066)
Add an integration test that connects to an instance with CAS certificates enabled.
1 parent 4332ffc commit 7545140

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

.github/workflows/tests.yml

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ jobs:
138138
POSTGRES_IAM_USER:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_USER_IAM_JAVA
139139
POSTGRES_PASS:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_PASS
140140
POSTGRES_DB:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_DB
141+
POSTGRES_CAS_CONNECTION_NAME:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_CAS_CONNECTION_NAME
142+
POSTGRES_CAS_PASS:${{ vars.GOOGLE_CLOUD_PROJECT }}/POSTGRES_CAS_PASS
141143
SQLSERVER_CONNECTION_NAME:${{ vars.GOOGLE_CLOUD_PROJECT }}/SQLSERVER_CONNECTION_NAME
142144
SQLSERVER_USER:${{ vars.GOOGLE_CLOUD_PROJECT }}/SQLSERVER_USER
143145
SQLSERVER_PASS:${{ vars.GOOGLE_CLOUD_PROJECT }}/SQLSERVER_PASS
@@ -158,6 +160,8 @@ jobs:
158160
POSTGRES_IAM_USER: "${{ steps.secrets.outputs.POSTGRES_IAM_USER }}"
159161
POSTGRES_PASS: "${{ steps.secrets.outputs.POSTGRES_PASS }}"
160162
POSTGRES_DB: "${{ steps.secrets.outputs.POSTGRES_DB }}"
163+
POSTGRES_CAS_CONNECTION_NAME: "${{ steps.secrets.outputs.POSTGRES_CAS_CONNECTION_NAME }}"
164+
POSTGRES_CAS_PASS: "${{ steps.secrets.outputs.POSTGRES_CAS_PASS }}"
161165
SQLSERVER_CONNECTION_NAME: "${{ steps.secrets.outputs.SQLSERVER_CONNECTION_NAME }}"
162166
SQLSERVER_USER: "${{ steps.secrets.outputs.SQLSERVER_USER }}"
163167
SQLSERVER_PASS: "${{ steps.secrets.outputs.SQLSERVER_PASS }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.sql.postgres;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
21+
22+
import com.google.common.collect.ImmutableList;
23+
import com.zaxxer.hikari.HikariConfig;
24+
import com.zaxxer.hikari.HikariDataSource;
25+
import java.sql.*;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import java.util.Properties;
29+
import java.util.concurrent.TimeUnit;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Rule;
33+
import org.junit.Test;
34+
import org.junit.rules.Timeout;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
38+
@RunWith(JUnit4.class)
39+
public class JdbcPostgresCasIntegrationTests {
40+
41+
private static final String CONNECTION_NAME = System.getenv("POSTGRES_CAS_CONNECTION_NAME");
42+
private static final String DB_NAME = System.getenv("POSTGRES_DB");
43+
private static final String DB_USER = System.getenv("POSTGRES_USER");
44+
private static final String DB_PASSWORD = System.getenv("POSTGRES_CAS_PASS");
45+
private static final ImmutableList<String> requiredEnvVars =
46+
ImmutableList.of(
47+
"POSTGRES_USER", "POSTGRES_CAS_PASS", "POSTGRES_DB", "POSTGRES_CAS_CONNECTION_NAME");
48+
@Rule public Timeout globalTimeout = new Timeout(80, TimeUnit.SECONDS);
49+
50+
private HikariDataSource connectionPool;
51+
52+
@BeforeClass
53+
public static void checkEnvVars() {
54+
// Check that required env vars are set
55+
requiredEnvVars.forEach(
56+
(varName) ->
57+
assertWithMessage(
58+
String.format(
59+
"Environment variable '%s' must be set to perform these tests.", varName))
60+
.that(System.getenv(varName))
61+
.isNotEmpty());
62+
}
63+
64+
@Before
65+
public void setUpPool() throws SQLException {
66+
// Set up URL parameters
67+
String jdbcURL = String.format("jdbc:postgresql:///%s", DB_NAME);
68+
Properties connProps = new Properties();
69+
connProps.setProperty("user", DB_USER);
70+
connProps.setProperty("password", DB_PASSWORD);
71+
connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory");
72+
connProps.setProperty("cloudSqlInstance", CONNECTION_NAME);
73+
74+
// Initialize connection pool
75+
HikariConfig config = new HikariConfig();
76+
config.setJdbcUrl(jdbcURL);
77+
config.setDataSourceProperties(connProps);
78+
config.setConnectionTimeout(10000); // 10s
79+
80+
this.connectionPool = new HikariDataSource(config);
81+
}
82+
83+
@Test
84+
public void pooledConnectionTest() throws SQLException {
85+
86+
List<Timestamp> rows = new ArrayList<>();
87+
try (Connection conn = connectionPool.getConnection()) {
88+
try (PreparedStatement selectStmt = conn.prepareStatement("SELECT NOW() as TS")) {
89+
ResultSet rs = selectStmt.executeQuery();
90+
while (rs.next()) {
91+
rows.add(rs.getTimestamp("TS"));
92+
}
93+
}
94+
}
95+
assertThat(rows.size()).isEqualTo(1);
96+
}
97+
}

0 commit comments

Comments
 (0)