You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/07/23 05:02:11 UTC

[shardingsphere] branch master updated: Refactor DockerStorageContainer (#19483)

This is an automated email from the ASF dual-hosted git repository.

zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 6534ed38abd Refactor DockerStorageContainer (#19483)
6534ed38abd is described below

commit 6534ed38abd40ed96a7cfd1a48c92d62186dc4fc
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Jul 23 13:02:06 2022 +0800

    Refactor DockerStorageContainer (#19483)
    
    * Refactor DockerStorageContainer
    
    * Fix checkstyle
---
 .../atomic/storage/DockerStorageContainer.java     | 35 +++++++++++++++++-----
 .../atomic/storage/impl/MySQLContainer.java        | 17 ++++-------
 .../atomic/storage/impl/PostgreSQLContainer.java   | 19 +++++-------
 3 files changed, 41 insertions(+), 30 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/DockerStorageContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/DockerStorageContainer.java
index 875e2bcebe4..bfed86191d8 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/DockerStorageContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/DockerStorageContainer.java
@@ -24,6 +24,7 @@ import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.test.integration.env.container.atomic.DockerITContainer;
 import org.apache.shardingsphere.test.integration.env.container.atomic.storage.StorageContainer;
+import org.apache.shardingsphere.test.integration.env.container.wait.JDBCConnectionWaitStrategy;
 import org.apache.shardingsphere.test.integration.env.runtime.DataSourceEnvironment;
 import org.apache.shardingsphere.test.integration.env.scenario.database.DatabaseEnvironmentManager;
 import org.apache.shardingsphere.test.integration.env.scenario.path.ScenarioDataPath;
@@ -33,21 +34,24 @@ import org.testcontainers.containers.BindMode;
 import javax.sql.DataSource;
 import javax.xml.bind.JAXBException;
 import java.io.IOException;
+import java.sql.DriverManager;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Optional;
 
 /**
  * Docker storage container.
  */
-@Getter
 public abstract class DockerStorageContainer extends DockerITContainer implements StorageContainer {
     
     private final DatabaseType databaseType;
     
     private final String scenario;
     
+    @Getter
     private final Map<String, DataSource> actualDataSourceMap;
     
+    @Getter
     private final Map<String, DataSource> expectedDataSourceMap;
     
     public DockerStorageContainer(final DatabaseType databaseType, final String dockerImageName, final String scenario) {
@@ -66,6 +70,12 @@ public abstract class DockerStorageContainer extends DockerITContainer implement
             withClasspathResourceMapping(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.ACTUAL, databaseType), "/docker-entrypoint-initdb.d/", BindMode.READ_ONLY);
             withClasspathResourceMapping(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.EXPECTED, databaseType), "/docker-entrypoint-initdb.d/", BindMode.READ_ONLY);
         }
+        withExposedPorts(getPort());
+        setWaitStrategy(new JDBCConnectionWaitStrategy(
+                () -> DriverManager.getConnection(getDefaultDatabaseName().isPresent() 
+                        ? DataSourceEnvironment.getURL(databaseType, "localhost", getFirstMappedPort(), getDefaultDatabaseName().get())
+                        : DataSourceEnvironment.getURL(databaseType, "localhost", getFirstMappedPort()),
+                        getUsername(), getPassword())));
     }
     
     @Override
@@ -79,20 +89,22 @@ public abstract class DockerStorageContainer extends DockerITContainer implement
         HikariDataSource result = new HikariDataSource();
         result.setDriverClassName(DataSourceEnvironment.getDriverClassName(databaseType));
         result.setJdbcUrl(DataSourceEnvironment.getURL(databaseType, getHost(), getMappedPort(getPort()), dataSourceName));
-        result.setUsername("root");
-        result.setPassword("root");
+        result.setUsername(getUsername());
+        result.setPassword(getPassword());
         result.setMaximumPoolSize(4);
         result.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
         return result;
     }
     
     /**
-     * Get jdbc url.
+     * Get JDBC URL.
      *
      * @param databaseName database name
-     * @return jdbc url
+     * @return JDBC URL
      */
-    public abstract String getJdbcUrl(String databaseName);
+    public final String getJdbcUrl(final String databaseName) {
+        return DataSourceEnvironment.getURL(databaseType, getHost(), getFirstMappedPort(), databaseName);
+    }
     
     /**
      * Get database username.
@@ -104,7 +116,7 @@ public abstract class DockerStorageContainer extends DockerITContainer implement
     /**
      * Get database password.
      *
-     * @return database username
+     * @return database password
      */
     public abstract String getPassword();
     
@@ -115,8 +127,15 @@ public abstract class DockerStorageContainer extends DockerITContainer implement
      */
     public abstract int getPort();
     
+    /**
+     * Get default database name.
+     * 
+     * @return default database name
+     */
+    protected abstract Optional<String> getDefaultDatabaseName();
+    
     @Override
     public final String getAbbreviation() {
-        return getDatabaseType().getType().toLowerCase();
+        return databaseType.getType().toLowerCase();
     }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/MySQLContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/MySQLContainer.java
index ee58b43db7e..dcad95bc7d2 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/MySQLContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/MySQLContainer.java
@@ -19,11 +19,9 @@ package org.apache.shardingsphere.test.integration.container.atomic.storage.impl
 
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeFactory;
 import org.apache.shardingsphere.test.integration.container.atomic.storage.DockerStorageContainer;
-import org.apache.shardingsphere.test.integration.env.container.wait.JDBCConnectionWaitStrategy;
-import org.apache.shardingsphere.test.integration.env.runtime.DataSourceEnvironment;
 import org.testcontainers.containers.BindMode;
 
-import java.sql.DriverManager;
+import java.util.Optional;
 
 /**
  * MySQL container.
@@ -41,15 +39,7 @@ public final class MySQLContainer extends DockerStorageContainer {
         addEnv("MYSQL_ROOT_PASSWORD", getPassword());
         addEnv("MYSQL_ROOT_HOST", "%");
         withClasspathResourceMapping("/env/mysql/my.cnf", "/etc/mysql/my.cnf", BindMode.READ_ONLY);
-        withExposedPorts(getPort());
         super.configure();
-        setWaitStrategy(new JDBCConnectionWaitStrategy(
-                () -> DriverManager.getConnection(DataSourceEnvironment.getURL(getDatabaseType(), "localhost", getFirstMappedPort()), getUsername(), getPassword())));
-    }
-    
-    @Override
-    public String getJdbcUrl(final String databaseName) {
-        return DataSourceEnvironment.getURL(getDatabaseType(), getHost(), getFirstMappedPort(), databaseName);
     }
     
     @Override
@@ -66,4 +56,9 @@ public final class MySQLContainer extends DockerStorageContainer {
     public int getPort() {
         return 3306;
     }
+    
+    @Override
+    protected Optional<String> getDefaultDatabaseName() {
+        return Optional.empty();
+    }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/PostgreSQLContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/PostgreSQLContainer.java
index 54a58d37918..aad92c8d9c7 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/PostgreSQLContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/container/atomic/storage/impl/PostgreSQLContainer.java
@@ -19,10 +19,9 @@ package org.apache.shardingsphere.test.integration.container.atomic.storage.impl
 
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeFactory;
 import org.apache.shardingsphere.test.integration.container.atomic.storage.DockerStorageContainer;
-import org.apache.shardingsphere.test.integration.env.container.wait.JDBCConnectionWaitStrategy;
-import org.apache.shardingsphere.test.integration.env.runtime.DataSourceEnvironment;
+import org.testcontainers.containers.BindMode;
 
-import java.sql.DriverManager;
+import java.util.Optional;
 
 /**
  * PostgreSQL container.
@@ -38,15 +37,8 @@ public final class PostgreSQLContainer extends DockerStorageContainer {
         withCommand("--max_connections=600", "--wal_level=logical");
         addEnv("POSTGRES_USER", getUsername());
         addEnv("POSTGRES_PASSWORD", getPassword());
-        withExposedPorts(getPort());
+        withClasspathResourceMapping("/env/postgresql/postgresql.conf", "/etc/postgresql/postgresql.conf", BindMode.READ_ONLY);
         super.configure();
-        setWaitStrategy(new JDBCConnectionWaitStrategy(
-                () -> DriverManager.getConnection(DataSourceEnvironment.getURL(getDatabaseType(), "localhost", getFirstMappedPort(), "postgres"), getUsername(), getPassword())));
-    }
-    
-    @Override
-    public String getJdbcUrl(final String databaseName) {
-        return DataSourceEnvironment.getURL(getDatabaseType(), getHost(), getFirstMappedPort(), databaseName);
     }
     
     @Override
@@ -63,4 +55,9 @@ public final class PostgreSQLContainer extends DockerStorageContainer {
     public int getPort() {
         return 5432;
     }
+    
+    @Override
+    protected Optional<String> getDefaultDatabaseName() {
+        return Optional.of("postgres");
+    }
 }