You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/01/04 04:15:07 UTC

[shardingsphere] branch master updated: Add scenarios supported for docker IT (#8870)

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

panjuan 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 d845d2b  Add scenarios supported for docker IT (#8870)
d845d2b is described below

commit d845d2b896e8aa671a26a4d3ef94d9b867c229e4
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Jan 4 12:14:28 2021 +0800

    Add scenarios supported for docker IT (#8870)
---
 .../shardingsphere-proxy-docker-build/pom.xml      |  2 +-
 .../shardingsphere-test-suite/pom.xml              | 12 ++---
 .../test/integration/engine/it/BaseIT.java         | 34 ++-----------
 .../integration/env/IntegrateTestEnvironment.java  | 59 +++++++++++++++++++---
 .../datasource/builder/ProxyDataSourceBuilder.java | 26 +++-------
 .../resources/docker/{ => db}/docker-compose.yml   | 17 ++++---
 .../test/resources/docker/{ => db}/mysql/init.sql  |  0
 .../docker/{ => db}/proxy/conf/config-db.yaml      |  2 +-
 .../docker/{ => db}/proxy/conf/logback.xml         |  0
 .../docker/{ => db}/proxy/conf/server.yaml         |  0
 .../test/resources/integrate/env-native.properties |  5 ++
 .../test/resources/integrate/env-proxy.properties  |  5 ++
 12 files changed, 91 insertions(+), 71 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-proxy-docker-build/pom.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-proxy-docker-build/pom.xml
index f650561..6049a08 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-proxy-docker-build/pom.xml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-proxy-docker-build/pom.xml
@@ -68,7 +68,7 @@
     
     <profiles>
         <profile>
-            <id>it-proxy</id>
+            <id>it.docker</id>
             <build>
                 <finalName>shardingsphere-proxy-${project.version}</finalName>
                 <plugins>
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/pom.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/pom.xml
index 7e3a1f0..4e0479b 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/pom.xml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/pom.xml
@@ -81,7 +81,7 @@
             </properties>
         </profile>
         <profile>
-            <id>it-proxy</id>
+            <id>it.docker</id>
             <properties>
                 <it.mode>proxy</it.mode>
             </properties>
@@ -92,26 +92,26 @@
                         <artifactId>docker-compose-maven-plugin</artifactId>
                         <executions>
                             <execution>
-                                <id>up</id>
+                                <id>db-up</id>
                                 <phase>pre-integration-test</phase>
                                 <goals>
                                     <goal>up</goal>
                                 </goals>
                                 <configuration>
-                                    <composeFile>${project.basedir}/src/test/resources/docker/docker-compose.yml</composeFile>
+                                    <composeFile>${project.basedir}/src/test/resources/docker/db/docker-compose.yml</composeFile>
                                     <detachedMode>true</detachedMode>
                                 </configuration>
                             </execution>
                             <execution>
-                                <id>down</id>
+                                <id>db-down</id>
                                 <phase>post-integration-test</phase>
                                 <goals>
                                     <goal>down</goal>
                                 </goals>
                                 <configuration>
-                                    <composeFile>${project.basedir}/src/test/resources/docker/docker-compose.yml</composeFile>
+                                    <composeFile>${project.basedir}/src/test/resources/docker/db/docker-compose.yml</composeFile>
                                     <removeVolumes>true</removeVolumes>
-                                    <removeImages>true</removeImages>
+<!--                                    <removeImages>true</removeImages>-->
                                 </configuration>
                             </execution>
                         </executions>
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java
index 77d4b37..32f7b87 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFac
 import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.test.integration.env.EnvironmentPath;
+import org.apache.shardingsphere.test.integration.env.IntegrateTestEnvironment;
 import org.apache.shardingsphere.test.integration.env.datasource.builder.ActualDataSourceBuilder;
 import org.apache.shardingsphere.test.integration.env.datasource.builder.ProxyDataSourceBuilder;
 import org.junit.After;
@@ -33,10 +34,7 @@ import javax.sql.DataSource;
 import javax.xml.bind.JAXBException;
 import java.io.File;
 import java.io.IOException;
-import java.sql.Connection;
-import java.sql.DriverManager;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.util.Map;
 import java.util.TimeZone;
 
@@ -66,41 +64,19 @@ public abstract class BaseIT {
         this.databaseType = databaseType;
         actualDataSources = ActualDataSourceBuilder.createActualDataSources(scenario, databaseType);
         targetDataSource = createTargetDataSource();
-        if ("proxy".equalsIgnoreCase(adapter)) {
-            waitForProxyReady();
-        }
     }
     
     private DataSource createTargetDataSource() throws SQLException, IOException {
-        return "proxy".equalsIgnoreCase(adapter) ? ProxyDataSourceBuilder.build(String.format("proxy_%s", scenario), databaseType) 
-                : YamlShardingSphereDataSourceFactory.createDataSource(actualDataSources, new File(EnvironmentPath.getRulesConfigurationFile(scenario)));
+        if ("proxy".equalsIgnoreCase(adapter)) {
+            return ProxyDataSourceBuilder.build(scenario, databaseType, IntegrateTestEnvironment.getInstance().getProxyEnvironments().get(scenario));
+        }
+        return YamlShardingSphereDataSourceFactory.createDataSource(actualDataSources, new File(EnvironmentPath.getRulesConfigurationFile(scenario)));
     }
     
     protected final void resetTargetDataSource() throws IOException, SQLException {
         targetDataSource = createTargetDataSource();
     }
     
-    private void waitForProxyReady() {
-        int retryCount = 0;
-        while (!isProxyReady() && retryCount < 30) {
-            try {
-                Thread.sleep(1000L);
-            } catch (final InterruptedException ignore) {
-            }
-            retryCount++;
-        }
-    }
-    
-    private boolean isProxyReady() {
-        try (Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:33070/proxy_db/?serverTimezone=UTC&useSSL=false&useLocalSessionState=true");
-             Statement statement = connection.createStatement()) {
-            statement.execute("SELECT 1");
-        } catch (final SQLException ignore) {
-            return false;
-        }
-        return true;
-    }
-    
     @After
     public final void tearDown() {
         if (targetDataSource instanceof ShardingSphereDataSource) {
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java
index fd90a58..93b523e 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java
@@ -21,11 +21,17 @@ import com.google.common.base.Splitter;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
 import org.apache.shardingsphere.test.integration.env.datasource.DatabaseEnvironment;
 
 import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -48,6 +54,8 @@ public final class IntegrateTestEnvironment {
     private final Collection<String> scenarios;
     
     private final Map<DatabaseType, DatabaseEnvironment> databaseEnvironments;
+
+    private final Map<String, DatabaseEnvironment> proxyEnvironments;
     
     private IntegrateTestEnvironment() {
         activeProfile = loadProperties("integrate/profile.properties").getProperty("mode");
@@ -56,6 +64,7 @@ public final class IntegrateTestEnvironment {
         runAdditionalTestCases = Boolean.parseBoolean(envProps.getProperty("it.run.additional.cases"));
         scenarios = Splitter.on(",").trimResults().splitToList(envProps.getProperty("it.scenarios"));
         databaseEnvironments = createDatabaseEnvironments(envProps);
+        proxyEnvironments = createProxyEnvironments(envProps);
     }
     
     private Properties loadProperties(final String fileName) {
@@ -99,13 +108,21 @@ public final class IntegrateTestEnvironment {
         }
     }
     
-    /**
-     * Judge whether proxy environment.
-     *
-     * @return is proxy environment or not 
-     */
-    public boolean isProxyEnvironment() {
-        return "proxy".equals(activeProfile);
+    private Map<String, DatabaseEnvironment> createProxyEnvironments(final Properties envProps) {
+        Map<String, DatabaseEnvironment> result = new HashMap<>(scenarios.size(), 1);
+        for (String each : scenarios) {
+            // TODO hard code for MySQL, should configurable
+            result.put(each, createProxyEnvironment(envProps, each));
+        }
+        return result;
+    }
+    
+    private DatabaseEnvironment createProxyEnvironment(final Properties envProps, final String scenario) {
+        String host = envProps.getProperty(String.format("it.proxy.%s.host", scenario), "127.0.0.1");
+        int port = Integer.parseInt(envProps.getProperty(String.format("it.proxy.%s.port", scenario), "3307"));
+        String username = envProps.getProperty(String.format("it.proxy.%s.username", scenario), "root");
+        String password = envProps.getProperty(String.format("it.proxy.%s.password", scenario), "root");
+        return new DatabaseEnvironment(new MySQLDatabaseType(), host, port, username, password);
     }
     
     /**
@@ -114,6 +131,34 @@ public final class IntegrateTestEnvironment {
      * @return singleton instance
      */
     public static IntegrateTestEnvironment getInstance() {
+        if (INSTANCE.adapters.contains("proxy")) {
+            for (String each : INSTANCE.scenarios) {
+                waitForProxyReady(each);
+            }
+        }
         return INSTANCE;
     }
+    
+    private static void waitForProxyReady(final String scenario) {
+        int retryCount = 0;
+        while (!isProxyReady(scenario) && retryCount < 30) {
+            try {
+                Thread.sleep(1000L);
+            } catch (final InterruptedException ignore) {
+            }
+            retryCount++;
+        }
+    }
+    
+    @SuppressWarnings("CallToDriverManagerGetConnection")
+    private static boolean isProxyReady(final String scenario) {
+        DatabaseEnvironment dbEnv = INSTANCE.proxyEnvironments.get(scenario);
+        try (Connection connection = DriverManager.getConnection(dbEnv.getURL(scenario), dbEnv.getUsername(), dbEnv.getPassword());
+             Statement statement = connection.createStatement()) {
+            statement.execute("SELECT 1");
+        } catch (final SQLException ignore) {
+            return false;
+        }
+        return true;
+    }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/datasource/builder/ProxyDataSourceBuilder.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/datasource/builder/ProxyDataSourceBuilder.java
index 0c79789..22d7af2 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/datasource/builder/ProxyDataSourceBuilder.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/datasource/builder/ProxyDataSourceBuilder.java
@@ -22,7 +22,6 @@ import com.zaxxer.hikari.HikariDataSource;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.test.integration.env.IntegrateTestEnvironment;
 import org.apache.shardingsphere.test.integration.env.datasource.DatabaseEnvironment;
 
 import javax.sql.DataSource;
@@ -42,38 +41,27 @@ public final class ProxyDataSourceBuilder {
      *
      * @param name data source name
      * @param databaseType database type
+     * @param databaseEnvironment database environment
      * @return proxy data source
      */
-    public static DataSource build(final String name, final DatabaseType databaseType) {
+    public static DataSource build(final String name, final DatabaseType databaseType, final DatabaseEnvironment databaseEnvironment) {
         DataSourceCacheKey cacheKey = new DataSourceCacheKey(name, databaseType);
         if (CACHE.containsKey(cacheKey)) {
             return CACHE.get(cacheKey);
         }
-        DataSource result = createHikariCP(databaseType, name);
+        DataSource result = createHikariCP(name, databaseEnvironment);
         CACHE.put(cacheKey, result);
         return result;
     }
     
-    private static DataSource createHikariCP(final DatabaseType databaseType, final String dataSourceName) {
+    private static DataSource createHikariCP(final String dataSourceName, final DatabaseEnvironment databaseEnvironment) {
         HikariConfig result = new HikariConfig();
-        DatabaseEnvironment databaseEnvironment = IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().get(databaseType);
         result.setDriverClassName(databaseEnvironment.getDriverClassName());
-        result.setJdbcUrl(getURL(dataSourceName, databaseType));
-        result.setUsername("root");
-        result.setPassword("root");
+        result.setJdbcUrl(databaseEnvironment.getURL(dataSourceName));
+        result.setUsername(databaseEnvironment.getUsername());
+        result.setPassword(databaseEnvironment.getPassword());
         result.setMaximumPoolSize(2);
         result.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
         return new HikariDataSource(result);
     }
-    
-    private static String getURL(final String dataSourceName, final DatabaseType databaseType) {
-        switch (databaseType.getName()) {
-            case "MySQL":
-                return String.format("jdbc:mysql://127.0.0.1:33070/%s?serverTimezone=UTC&useSSL=false&useLocalSessionState=true", dataSourceName);
-            case "PostgreSQL":
-                return String.format("jdbc:postgresql://127.0.0.1:33070/%s", dataSourceName);
-            default:
-                throw new UnsupportedOperationException(databaseType.getName());
-        }
-    }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/docker-compose.yml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/docker-compose.yml
similarity index 86%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/docker-compose.yml
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/docker-compose.yml
index 2fd84ed..b242a9b 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/docker-compose.yml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/docker-compose.yml
@@ -16,30 +16,31 @@
 #
 
 version: "2.1"
+
 services:
   mysql:
     image: "mysql/mysql-server:5.7"
-    container_name: mysql-5.7
+    container_name: db-mysql
     command: --default-authentication-plugin=mysql_native_password
     volumes:
       - ./mysql:/docker-entrypoint-initdb.d/
     ports:
       - "33060:3306"
   
-  zookeeper:
-    image: "zookeeper:3.6.2"
-    container_name: register_center
-    ports:
-      - "2181:21810"
+#  zookeeper:
+#    image: "zookeeper:3.6.2"
+#    container_name: db-register-center
+#    ports:
+#      - "2181:21810"
   
   shardingsphere-proxy:
     image: apache/shardingsphere-proxy-test
-    container_name: shardingsphere-proxy
+    container_name: db-shardingsphere-proxy
     ports:
       - "33070:3307"
     links:
       - "mysql:db.mysql"
-      - "zookeeper:zk"
+#      - "zookeeper:zk"
     volumes:
       - ./proxy/conf:/opt/shardingsphere-proxy/conf
     depends_on:
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/mysql/init.sql b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/mysql/init.sql
similarity index 100%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/mysql/init.sql
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/mysql/init.sql
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/proxy/conf/config-db.yaml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/proxy/conf/config-db.yaml
similarity index 99%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/proxy/conf/config-db.yaml
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/proxy/conf/config-db.yaml
index 94960f2..ff0e573 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/proxy/conf/config-db.yaml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/proxy/conf/config-db.yaml
@@ -15,7 +15,7 @@
 # limitations under the License.
 #
 
-schemaName: proxy_db
+schemaName: db
 
 dataSources:
   ds_0:
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/proxy/conf/logback.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/proxy/conf/logback.xml
similarity index 100%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/proxy/conf/logback.xml
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/proxy/conf/logback.xml
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/proxy/conf/server.yaml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/proxy/conf/server.yaml
similarity index 100%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/proxy/conf/server.yaml
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/docker/db/proxy/conf/server.yaml
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-native.properties b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-native.properties
index 3d22d9f..e8157a1 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-native.properties
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-native.properties
@@ -43,3 +43,8 @@ it.oracle.host=db.oracle
 it.oracle.port=1521
 it.oracle.username=jdbc
 it.oracle.password=jdbc
+
+it.proxy.db.host=127.0.0.1
+it.proxy.db.port=33070
+it.proxy.db.username=root
+it.proxy.db.password=root
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-proxy.properties b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-proxy.properties
index bd2f3d7..cfad73c 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-proxy.properties
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/resources/integrate/env-proxy.properties
@@ -34,3 +34,8 @@ it.postgresql.host=127.0.0.1
 it.postgresql.port=57320
 it.postgresql.username=postgres
 it.postgresql.password=postgres
+
+it.proxy.db.host=127.0.0.1
+it.proxy.db.port=33070
+it.proxy.db.username=root
+it.proxy.db.password=root