Skip to content

Commit f101731

Browse files
authored
Refactor DockerStorageContainer (#36503)
1 parent 4ca0f61 commit f101731

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/storage/DockerStorageContainer.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import javax.sql.DataSource;
3131
import java.sql.DriverManager;
32+
import java.util.Arrays;
3233
import java.util.Collection;
3334
import java.util.HashMap;
3435
import java.util.LinkedHashMap;
@@ -47,6 +48,8 @@ public abstract class DockerStorageContainer extends DockerITContainer implement
4748

4849
private static final String READY_USER_PASSWORD = "Ready@123";
4950

51+
private static final Collection<String> TO_BE_MOUNTED_SQL_FILES = Arrays.asList("00-init-authority.sql", "99-be-ready.sql");
52+
5053
private final DatabaseType databaseType;
5154

5255
private final Map<String, DataSource> actualDataSourceMap = new LinkedHashMap<>();
@@ -60,20 +63,24 @@ protected DockerStorageContainer(final DatabaseType databaseType, final String c
6063

6164
@Override
6265
protected void configure() {
63-
withClasspathResourceMapping("/container/" + databaseType.getType().toLowerCase() + "/init-sql/00-init-authority.sql", "/docker-entrypoint-initdb.d/00-init-authority.sql", BindMode.READ_ONLY);
64-
withClasspathResourceMapping("/container/" + databaseType.getType().toLowerCase() + "/init-sql/99-be-ready.sql", "/docker-entrypoint-initdb.d/99-be-ready.sql", BindMode.READ_ONLY);
66+
for (String each : TO_BE_MOUNTED_SQL_FILES) {
67+
findToBeMountedSQLFile(each).ifPresent(optional -> withClasspathResourceMapping(optional, "/docker-entrypoint-initdb.d/" + each, BindMode.READ_ONLY));
68+
}
6569
withExposedPorts(getExposedPort());
66-
setWaitStrategy(new JdbcConnectionWaitStrategy(
67-
() -> DriverManager.getConnection(getDefaultDatabaseName().isPresent()
68-
? DataSourceEnvironment.getURL(databaseType, "localhost", getFirstMappedPort(), getDefaultDatabaseName().get())
69-
: DataSourceEnvironment.getURL(databaseType, "localhost", getFirstMappedPort()), READY_USER, READY_USER_PASSWORD)));
70+
setWaitStrategy(new JdbcConnectionWaitStrategy(() -> DriverManager.getConnection(getDefaultDatabaseName().isPresent()
71+
? DataSourceEnvironment.getURL(databaseType, "localhost", getFirstMappedPort(), getDefaultDatabaseName().get())
72+
: DataSourceEnvironment.getURL(databaseType, "localhost", getFirstMappedPort()), READY_USER, READY_USER_PASSWORD)));
73+
}
74+
75+
private Optional<String> findToBeMountedSQLFile(final String toBeMountedSQLFile) {
76+
String toBeMountedSQLFilePath = String.format("container/%s/init-sql/%s", databaseType.getType().toLowerCase(), toBeMountedSQLFile);
77+
return null == Thread.currentThread().getContextClassLoader().getResource(toBeMountedSQLFilePath) ? Optional.empty() : Optional.of("/" + toBeMountedSQLFilePath);
7078
}
7179

7280
protected final void setCommands(final String command) {
73-
if (Strings.isNullOrEmpty(command)) {
74-
return;
81+
if (!Strings.isNullOrEmpty(command)) {
82+
setCommand(command);
7583
}
76-
setCommand(command);
7784
}
7885

7986
protected final void addEnvs(final Map<String, String> envs) {
@@ -165,12 +172,12 @@ public final String getAbbreviation() {
165172

166173
@Override
167174
public Map<String, String> getLinkReplacements() {
168-
Map<String, String> replacements = new HashMap<>();
175+
Map<String, String> result = new HashMap<>();
169176
for (String each : getNetworkAliases()) {
170177
for (Integer exposedPort : getExposedPorts()) {
171-
replacements.put(each + ":" + exposedPort, getHost() + ":" + getMappedPort(exposedPort));
178+
result.put(each + ":" + exposedPort, getHost() + ":" + getMappedPort(exposedPort));
172179
}
173180
}
174-
return replacements;
181+
return result;
175182
}
176183
}

test/e2e/env/src/test/resources/container/mysql/init-sql/99-be-ready.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@
1616
--
1717

1818
CREATE USER IF NOT EXISTS 'ready_user'@'%' IDENTIFIED BY 'Ready@123';
19-

0 commit comments

Comments
 (0)