|
| 1 | +/* |
| 2 | + * Copyright 2022 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.mysql; |
| 18 | + |
| 19 | + |
| 20 | +import static com.google.common.truth.Truth.assertThat; |
| 21 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 22 | + |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | +import com.zaxxer.hikari.HikariConfig; |
| 25 | +import com.zaxxer.hikari.HikariDataSource; |
| 26 | +import java.sql.Connection; |
| 27 | +import java.sql.PreparedStatement; |
| 28 | +import java.sql.ResultSet; |
| 29 | +import java.sql.SQLException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Properties; |
| 33 | +import java.util.UUID; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.Rule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.rules.Timeout; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.junit.runners.JUnit4; |
| 43 | + |
| 44 | + |
| 45 | +@RunWith(JUnit4.class) |
| 46 | +public class JdbcMysqlJ8IamAuthIntegrationTests { |
| 47 | + |
| 48 | + private static final String CONNECTION_NAME = System.getenv("MYSQL_IAM_CONNECTION_NAME"); |
| 49 | + private static final String DB_NAME = System.getenv("MYSQL_DB"); |
| 50 | + private static final String DB_USER = System.getenv("MYSQL_IAM_USER"); |
| 51 | + |
| 52 | + private static ImmutableList<String> requiredEnvVars = ImmutableList |
| 53 | + .of("MYSQL_IAM_USER", "MYSQL_DB", "MYSQL_IAM_CONNECTION_NAME"); |
| 54 | + @Rule |
| 55 | + public Timeout globalTimeout = new Timeout(30, TimeUnit.SECONDS); |
| 56 | + private String tableName; |
| 57 | + private HikariDataSource connectionPool; |
| 58 | + |
| 59 | + @BeforeClass |
| 60 | + public static void checkEnvVars() { |
| 61 | + // Check that required env vars are set |
| 62 | + requiredEnvVars.stream().forEach((varName) -> { |
| 63 | + assertWithMessage( |
| 64 | + String.format("Environment variable '%s' must be set to perform these tests.", varName)) |
| 65 | + .that(System.getenv(varName)).isNotEmpty(); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @Before |
| 70 | + public void setUpPool() throws SQLException { |
| 71 | + // Set up URL parameters |
| 72 | + String jdbcURL = String.format("jdbc:mysql:///%s", DB_NAME); |
| 73 | + Properties connProps = new Properties(); |
| 74 | + connProps.setProperty("user", DB_USER); |
| 75 | + connProps.setProperty("sslmode", "disable"); |
| 76 | + connProps.setProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory"); |
| 77 | + connProps.setProperty("cloudSqlInstance", CONNECTION_NAME); |
| 78 | + connProps.setProperty("enableIamAuth", "true"); |
| 79 | + |
| 80 | + // Initialize connection pool |
| 81 | + HikariConfig config = new HikariConfig(); |
| 82 | + config.setJdbcUrl(jdbcURL); |
| 83 | + config.setDataSourceProperties(connProps); |
| 84 | + config.setConnectionTimeout(10000); // 10s |
| 85 | + |
| 86 | + this.connectionPool = new HikariDataSource(config); |
| 87 | + this.tableName = String.format("books_%s", UUID.randomUUID().toString().replace("-", "")); |
| 88 | + |
| 89 | + // Create table |
| 90 | + try (Connection conn = connectionPool.getConnection()) { |
| 91 | + String stmt = String.format("CREATE TABLE %s (", this.tableName) |
| 92 | + + " ID CHAR(20) NOT NULL," |
| 93 | + + " TITLE TEXT NOT NULL" |
| 94 | + + ");"; |
| 95 | + try (PreparedStatement createTableStatement = conn.prepareStatement(stmt)) { |
| 96 | + createTableStatement.execute(); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + @After |
| 103 | + public void dropTableIfPresent() throws SQLException { |
| 104 | + try (Connection conn = connectionPool.getConnection()) { |
| 105 | + String stmt = String.format("DROP TABLE %s;", this.tableName); |
| 106 | + try (PreparedStatement dropTableStatement = conn.prepareStatement(stmt)) { |
| 107 | + dropTableStatement.execute(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void pooledConnectionTest() throws SQLException { |
| 114 | + try (Connection conn = connectionPool.getConnection()) { |
| 115 | + String stmt = String.format("INSERT INTO %s (ID, TITLE) VALUES (?, ?)", this.tableName); |
| 116 | + try (PreparedStatement insertStmt = conn.prepareStatement(stmt)) { |
| 117 | + insertStmt.setQueryTimeout(10); |
| 118 | + insertStmt.setString(1, "book1"); |
| 119 | + insertStmt.setString(2, "Book One"); |
| 120 | + insertStmt.execute(); |
| 121 | + insertStmt.setString(1, "book2"); |
| 122 | + insertStmt.setString(2, "Book Two"); |
| 123 | + insertStmt.execute(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + List<String> bookList = new ArrayList<>(); |
| 128 | + try (Connection conn = connectionPool.getConnection()) { |
| 129 | + String stmt = String.format("SELECT TITLE FROM %s ORDER BY ID", this.tableName); |
| 130 | + try (PreparedStatement selectStmt = conn.prepareStatement(stmt)) { |
| 131 | + selectStmt.setQueryTimeout(10); // 10s |
| 132 | + ResultSet rs = selectStmt.executeQuery(); |
| 133 | + while (rs.next()) { |
| 134 | + bookList.add(rs.getString("TITLE")); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + assertThat(bookList).containsExactly("Book One", "Book Two"); |
| 139 | + |
| 140 | + } |
| 141 | +} |
0 commit comments