Light, fluffy, and always free - The AWS Local Emulator alternative
test(rds): connect to Aurora PostgreSQL through the AWS Advanced JDBC Wrapper (#6213)
Tony Nguyen58ce60b06ea6
diff --git a/compatibility-tests/sdk-test-java/pom.xml b/compatibility-tests/sdk-test-java/pom.xmlindex ff02accc98..28924d1ba8 100644--- a/compatibility-tests/sdk-test-java/pom.xml+++ b/compatibility-tests/sdk-test-java/pom.xml@@ -645,6 +645,11 @@<artifactId>postgresql</artifactId><version>42.7.11</version></dependency>+ <dependency>+ <groupId>software.amazon.jdbc</groupId>+ <artifactId>aws-advanced-jdbc-wrapper</artifactId>+ <version>4.5.0</version>+ </dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId>diff --git a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.javaindex 4feb3d2d01..759bfec008 100644--- a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java+++ b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java@@ -186,10 +186,14 @@ import java.security.SecureRandom;import java.security.cert.Certificate;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;+import java.sql.Connection;+import java.sql.DriverManager;+import java.sql.SQLException;import java.time.Duration;import java.util.ArrayList;import java.util.List;import java.util.Map;+import java.util.Properties;import java.util.TreeMap;import java.util.UUID;import java.util.function.BooleanSupplier;@@ -276,6 +280,22 @@ public final class TestFixtures {throw new AssertionError(resourceName + " timed out waiting for " + expected + timeoutDetails.get());}+ /** Opens a JDBC connection, retrying for up to 60 seconds while the database endpoint starts. */+ public static Connection awaitJdbcConnection(String url, Properties properties)+ throws SQLException, InterruptedException {+ long deadline = System.nanoTime() + Duration.ofSeconds(60).toNanos();+ SQLException last = null;+ while (System.nanoTime() < deadline) {+ try {+ return DriverManager.getConnection(url, properties);+ } catch (SQLException e) {+ last = e;+ Thread.sleep(1000);+ }+ }+ throw last != null ? last : new SQLException("Timed out waiting for " + url);+ }+/** The region every fixture client signs for, for tests that build or compare region-scoped values. */public static Region region() {return REGION;diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsAwsAdvancedJdbcWrapperTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsAwsAdvancedJdbcWrapperTest.javanew file mode 100644index 0000000000..5f4a0d5550--- /dev/null+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsAwsAdvancedJdbcWrapperTest.java@@ -0,0 +1,144 @@+package com.floci.test;++import org.jboss.logging.Logger;+import org.junit.jupiter.api.AfterAll;+import org.junit.jupiter.api.Assumptions;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.DisplayName;+import org.junit.jupiter.api.Test;+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;+import software.amazon.awssdk.services.rds.RdsClient;+import software.amazon.awssdk.services.rds.model.DBCluster;+import software.amazon.awssdk.services.rds.model.RdsException;+import software.amazon.jdbc.authentication.AwsCredentialsManager;+import software.amazon.jdbc.wrapper.ConnectionWrapper;++import java.sql.Connection;+import java.sql.PreparedStatement;+import java.sql.ResultSet;+import java.sql.Statement;+import java.util.Properties;++import static org.assertj.core.api.Assertions.assertThat;++/**+ * Connects to an Aurora PostgreSQL cluster through the AWS Advanced JDBC Wrapper.+ * Floci runs plain PostgreSQL behind the cluster endpoint, so the aurora_* topology+ * functions are absent and topology-based plugins (failover, failover2, efm, gdbFailover)+ * are out of scope. The test uses the plain PostgreSQL dialect and the IAM plugin.+ */+@DisplayName("RDS AWS Advanced JDBC Wrapper")+class RdsAwsAdvancedJdbcWrapperTest {+ private static final Logger LOG = Logger.getLogger(RdsAwsAdvancedJdbcWrapperTest.class);+ private static final String USERNAME = "clusteradmin";+ private static final String PASSWORD = "ClusterPassword1";+ private static final String IAM_USERNAME = "iam_user";+ private static final String DATABASE = "app";++ private static RdsClient rds;+ private static String clusterId;+ private static String memberId;+ private static int port;++ @BeforeAll+ static void createCluster() {+ Assumptions.assumeFalse(TestFixtures.isRealAws(), "Connects through the Floci proxy host with test credentials");+ rds = TestFixtures.rdsClient();+ clusterId = TestFixtures.uniqueName("jdbc-wrapper");+ memberId = clusterId + "-1";+ DBCluster cluster = rds.createDBCluster(b -> b.dbClusterIdentifier(clusterId)+ .engine("aurora-postgresql").masterUsername(USERNAME).masterUserPassword(PASSWORD)+ .databaseName(DATABASE).enableIAMDatabaseAuthentication(true)).dbCluster();+ rds.createDBInstance(b -> b.dbInstanceIdentifier(memberId).dbClusterIdentifier(clusterId)+ .engine("aurora-postgresql").dbInstanceClass("db.serverless"));+ port = cluster.port();+ }++ @AfterAll+ static void cleanup() {+ if (rds == null) {+ return;+ }+ try {+ rds.deleteDBInstance(b -> b.dbInstanceIdentifier(memberId).skipFinalSnapshot(true));+ } catch (RdsException e) {+ LOG.warnv(e, "Could not clean up member {0}", memberId);+ }+ try {+ rds.deleteDBCluster(b -> b.dbClusterIdentifier(clusterId).skipFinalSnapshot(true));+ } catch (RdsException e) {+ LOG.warnv(e, "Could not clean up cluster {0}", clusterId);+ }+ rds.close();+ }++ @Test+ @DisplayName("Writes and reads through the wrapper with password authentication")+ void writeAndReadWithPassword() throws Exception {+ Properties properties = wrapperProperties("");+ properties.setProperty("user", USERNAME);+ properties.setProperty("password", PASSWORD);++ try (Connection connection = awaitConnection(properties)) {+ assertThat(connection).isInstanceOf(ConnectionWrapper.class);+ try (Statement statement = connection.createStatement()) {+ statement.execute("CREATE TABLE IF NOT EXISTS wrapper_items (id INT PRIMARY KEY, name TEXT)");+ }+ try (PreparedStatement insert = connection.prepareStatement(+ "INSERT INTO wrapper_items (id, name) VALUES (?, ?)")) {+ insert.setInt(1, 1);+ insert.setString(2, "floci");+ assertThat(insert.executeUpdate()).isEqualTo(1);+ }+ try (Statement statement = connection.createStatement();+ ResultSet resultSet = statement.executeQuery("SELECT name FROM wrapper_items WHERE id = 1")) {+ assertThat(resultSet.next()).isTrue();+ assertThat(resultSet.getString(1)).isEqualTo("floci");+ }+ }+ }++ @Test+ @DisplayName("Connects through the wrapper IAM plugin")+ void connectWithIamPlugin() throws Exception {+ Properties admin = wrapperProperties("");+ admin.setProperty("user", USERNAME);+ admin.setProperty("password", PASSWORD);+ try (Connection connection = awaitConnection(admin);+ Statement statement = connection.createStatement()) {+ statement.execute("DO $$ BEGIN CREATE ROLE " + IAM_USERNAME + " LOGIN; "+ + "EXCEPTION WHEN duplicate_object THEN NULL; END $$");+ statement.execute("GRANT rds_iam TO " + IAM_USERNAME);+ }++ AwsCredentialsManager.setCustomHandler((hostSpec, props) ->+ StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")));+ Properties properties = wrapperProperties("iam");+ properties.setProperty("user", IAM_USERNAME);+ properties.setProperty("iamRegion", TestFixtures.region().id());++ try (Connection connection = awaitConnection(properties);+ Statement statement = connection.createStatement();+ ResultSet resultSet = statement.executeQuery("SELECT current_user")) {+ assertThat(resultSet.next()).isTrue();+ assertThat(resultSet.getString(1)).isEqualTo(IAM_USERNAME);+ } finally {+ AwsCredentialsManager.resetCustomHandler();+ }+ }++ private static Properties wrapperProperties(String plugins) {+ Properties properties = new Properties();+ properties.setProperty("wrapperPlugins", plugins);+ properties.setProperty("wrapperDialect", "pg");+ properties.setProperty("sslmode", "disable");+ properties.setProperty("connectTimeout", "5");+ return properties;+ }++ private static Connection awaitConnection(Properties properties) throws Exception {+ return TestFixtures.awaitJdbcConnection(+ "jdbc:aws-wrapper:postgresql://" + TestFixtures.proxyHost() + ":" + port + "/" + DATABASE, properties);+ }+}diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsIamTokenCompatTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsIamTokenCompatTest.javaindex 701ae2b023..2ed204a3dc 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsIamTokenCompatTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsIamTokenCompatTest.java@@ -22,8 +22,6 @@ import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;-import java.time.Duration;-import java.time.Instant;import java.util.Properties;import static org.assertj.core.api.Assertions.assertThat;@@ -195,28 +193,24 @@ class RdsIamTokenCompatTest {}private static Connection awaitPostgresConnection(String username, String password) throws Exception {- Instant deadline = Instant.now().plus(Duration.ofSeconds(60));- SQLException last = null;- while (Instant.now().isBefore(deadline)) {- try {- return openPostgresConnection(username, password);- } catch (SQLException e) {- last = e;- Thread.sleep(1000);- }- }- throw last != null ? last : new SQLException("Timed out waiting for RDS proxy connection");+ return TestFixtures.awaitJdbcConnection(postgresUrl(), postgresProperties(username, password));}private static Connection openPostgresConnection(String username, String password) throws SQLException {+ return DriverManager.getConnection(postgresUrl(), postgresProperties(username, password));+ }++ private static String postgresUrl() {+ return "jdbc:postgresql://" + TestFixtures.proxyHost() + ":" + proxyPort + "/" + DATABASE;+ }++ private static Properties postgresProperties(String username, String password) {Properties props = new Properties();props.setProperty("user", username);props.setProperty("password", password);props.setProperty("sslmode", "disable");props.setProperty("connectTimeout", "5");- return DriverManager.getConnection(- "jdbc:postgresql://" + TestFixtures.proxyHost() + ":" + proxyPort + "/" + DATABASE,- props);+ return props;}private static int selectOne(Connection conn) throws SQLException {diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.javaindex b6bf89972b..1c204bb5d5 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.java@@ -25,8 +25,6 @@ import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;-import java.time.Duration;-import java.time.Instant;import java.util.Properties;import static org.assertj.core.api.Assertions.assertThat;@@ -414,17 +412,7 @@ class RdsJdbcCompatTest {}private static Connection awaitPostgresConnection(String username, String password, int port) throws Exception {- Instant deadline = Instant.now().plus(Duration.ofSeconds(60));- SQLException last = null;- while (Instant.now().isBefore(deadline)) {- try {- return openPostgresConnection(username, password, port);- } catch (SQLException e) {- last = e;- Thread.sleep(1000);- }- }- throw last != null ? last : new SQLException("Timed out waiting for RDS proxy connection");+ return TestFixtures.awaitJdbcConnection(postgresUrl(port), postgresProperties(username, password));}private static Connection openPostgresConnection(String username, String password) throws SQLException {@@ -432,14 +420,20 @@ class RdsJdbcCompatTest {}private static Connection openPostgresConnection(String username, String password, int port) throws SQLException {+ return DriverManager.getConnection(postgresUrl(port), postgresProperties(username, password));+ }++ private static String postgresUrl(int port) {+ return "jdbc:postgresql://" + TestFixtures.proxyHost() + ":" + port + "/" + DATABASE;+ }++ private static Properties postgresProperties(String username, String password) {Properties properties = new Properties();properties.setProperty("user", username);properties.setProperty("password", password);properties.setProperty("sslmode", "disable");properties.setProperty("connectTimeout", "5");- return DriverManager.getConnection(- "jdbc:postgresql://" + TestFixtures.proxyHost() + ":" + port + "/" + DATABASE,- properties);+ return properties;}private static int selectOne(Connection connection) throws SQLException {