Skip to content

Commit 2265733

Browse files
authored
Add StorageContainerConfigurationOption (#36473)
* Add StorageContainerConfigurationOption * Add StorageContainerConfigurationOption
1 parent 4a8bbd2 commit 2265733

File tree

16 files changed

+366
-263
lines changed

16 files changed

+366
-263
lines changed

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

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
import lombok.AccessLevel;
2121
import lombok.NoArgsConstructor;
2222
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
23-
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.dialect.H2ContainerConfigurationFactory;
24-
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.dialect.MariaDBContainerConfigurationFactory;
25-
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.dialect.MySQLContainerConfigurationFactory;
26-
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.dialect.OpenGaussContainerConfigurationFactory;
27-
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.dialect.PostgreSQLContainerConfigurationFactory;
23+
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.StorageContainerConfigurationOption;
24+
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.database.DatabaseEnvironmentManager;
25+
26+
import java.util.Collections;
27+
import java.util.Map;
2828

2929
/**
3030
* Storage container configuration factory.
@@ -35,48 +35,29 @@ public final class StorageContainerConfigurationFactory {
3535
/**
3636
* Create new instance of storage container configuration.
3737
*
38-
* @param databaseType database type
39-
* @param scenario scenario
40-
* @return created instance
41-
* @throws RuntimeException runtime exception
38+
* @param option storage container configuration option
39+
* @param majorVersion majorVersion
40+
* @return created storage container configuration
4241
*/
43-
public static StorageContainerConfiguration newInstance(final DatabaseType databaseType, final String scenario) {
44-
switch (databaseType.getType()) {
45-
case "MySQL":
46-
return MySQLContainerConfigurationFactory.newInstance(scenario);
47-
case "PostgreSQL":
48-
return PostgreSQLContainerConfigurationFactory.newInstance(scenario);
49-
case "openGauss":
50-
return OpenGaussContainerConfigurationFactory.newInstance(scenario);
51-
case "H2":
52-
return H2ContainerConfigurationFactory.newInstance(scenario);
53-
default:
54-
throw new RuntimeException(String.format("Database `%s` is unknown.", databaseType.getType()));
55-
}
42+
public static StorageContainerConfiguration newInstance(final StorageContainerConfigurationOption option, final int majorVersion) {
43+
return option.isRecognizeMajorVersion()
44+
? new StorageContainerConfiguration(option.getCommand(), option.getContainerEnvironments(), option.getMountedResources(majorVersion), Collections.emptyMap(), Collections.emptyMap())
45+
: new StorageContainerConfiguration(option.getCommand(), option.getContainerEnvironments(), option.getMountedResources(), Collections.emptyMap(), Collections.emptyMap());
5646
}
5747

5848
/**
5949
* Create new instance of storage container configuration.
6050
*
51+
* @param option storage container configuration option
6152
* @param databaseType database type
62-
* @param majorVersion majorVersion
63-
* @return created instance
64-
* @throws RuntimeException runtime exception
53+
* @param scenario scenario
54+
* @return created storage container configuration
6555
*/
66-
public static StorageContainerConfiguration newInstance(final DatabaseType databaseType, final int majorVersion) {
67-
switch (databaseType.getType()) {
68-
case "MySQL":
69-
return MySQLContainerConfigurationFactory.newInstance(majorVersion);
70-
case "PostgreSQL":
71-
return PostgreSQLContainerConfigurationFactory.newInstance();
72-
case "openGauss":
73-
return OpenGaussContainerConfigurationFactory.newInstance();
74-
case "H2":
75-
return H2ContainerConfigurationFactory.newInstance();
76-
case "MariaDB":
77-
return MariaDBContainerConfigurationFactory.newInstance();
78-
default:
79-
throw new RuntimeException(String.format("Database `%s` is unknown.", databaseType.getType()));
80-
}
56+
public static StorageContainerConfiguration newInstance(final StorageContainerConfigurationOption option, final DatabaseType databaseType, final String scenario) {
57+
Map<String, DatabaseType> databaseTypes = DatabaseEnvironmentManager.getDatabaseTypes(scenario, databaseType);
58+
Map<String, DatabaseType> expectedDatabaseTypes = DatabaseEnvironmentManager.getExpectedDatabaseTypes(scenario, databaseType);
59+
return option.isEmbeddedStorageContainer()
60+
? new StorageContainerConfiguration(scenario, option.getCommand(), option.getContainerEnvironments(), option.getMountedResources(scenario), databaseTypes, expectedDatabaseTypes)
61+
: new StorageContainerConfiguration(option.getCommand(), option.getContainerEnvironments(), option.getMountedResources(scenario), databaseTypes, expectedDatabaseTypes);
8162
}
8263
}

test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/storage/config/dialect/H2ContainerConfigurationFactory.java

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* Storage container configuration option.
24+
*/
25+
public interface StorageContainerConfigurationOption {
26+
27+
/**
28+
* Get command.
29+
*
30+
* @return command
31+
*/
32+
String getCommand();
33+
34+
/**
35+
* Get container environments.
36+
*
37+
* @return container environments
38+
*/
39+
Map<String, String> getContainerEnvironments();
40+
41+
/**
42+
* Get mounted resources.
43+
*
44+
* @return mounted resources
45+
*/
46+
Map<String, String> getMountedResources();
47+
48+
/**
49+
* Get mounted resources.
50+
*
51+
* @param scenario scenario
52+
* @return mounted resources
53+
*/
54+
Map<String, String> getMountedResources(String scenario);
55+
56+
/**
57+
* Get mounted resources.
58+
*
59+
* @param majorVersion major version
60+
* @return mounted resources
61+
*/
62+
Map<String, String> getMountedResources(int majorVersion);
63+
64+
/**
65+
* Whether embedded storage container.
66+
*
67+
* @return is embedded storage container or not
68+
*/
69+
boolean isEmbeddedStorageContainer();
70+
71+
/**
72+
* Whether recognize major version.
73+
*
74+
* @return recognized major version or not
75+
*/
76+
boolean isRecognizeMajorVersion();
77+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option;
19+
20+
import lombok.AccessLevel;
21+
import lombok.NoArgsConstructor;
22+
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
23+
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.dialect.H2StorageContainerConfigurationOption;
24+
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.dialect.MariaDBStorageContainerConfigurationOption;
25+
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.dialect.MySQLStorageContainerConfigurationOption;
26+
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.dialect.OpenGaussStorageContainerConfigurationOption;
27+
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.dialect.PostgreSQLStorageContainerConfigurationOption;
28+
29+
/**
30+
* Storage container configuration option factory.
31+
*/
32+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
33+
public final class StorageContainerConfigurationOptionFactory {
34+
35+
/**
36+
* Create new instance of storage container configuration option.
37+
*
38+
* @param databaseType database type
39+
* @return created storage container configuration option
40+
* @throws RuntimeException runtime exception
41+
*/
42+
public static StorageContainerConfigurationOption newInstance(final DatabaseType databaseType) {
43+
switch (databaseType.getType()) {
44+
case "MySQL":
45+
return new MySQLStorageContainerConfigurationOption();
46+
case "MariaDB":
47+
return new MariaDBStorageContainerConfigurationOption();
48+
case "PostgreSQL":
49+
return new PostgreSQLStorageContainerConfigurationOption();
50+
case "openGauss":
51+
return new OpenGaussStorageContainerConfigurationOption();
52+
case "H2":
53+
return new H2StorageContainerConfigurationOption();
54+
default:
55+
throw new RuntimeException(String.format("Database `%s` is unknown.", databaseType.getType()));
56+
}
57+
}
58+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.dialect;
19+
20+
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
21+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
22+
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.option.StorageContainerConfigurationOption;
23+
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath;
24+
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath.Type;
25+
26+
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
/**
31+
* Storage container configuration option for H2.
32+
*/
33+
public final class H2StorageContainerConfigurationOption implements StorageContainerConfigurationOption {
34+
35+
private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "H2");
36+
37+
@Override
38+
public String getCommand() {
39+
return "";
40+
}
41+
42+
@Override
43+
public Map<String, String> getContainerEnvironments() {
44+
return Collections.emptyMap();
45+
}
46+
47+
@Override
48+
public Map<String, String> getMountedResources() {
49+
return Collections.singletonMap("/env/mysql/01-initdb.sql", "/docker-entrypoint-initdb.d/01-initdb.sql");
50+
}
51+
52+
@Override
53+
public Map<String, String> getMountedResources(final String scenario) {
54+
Map<String, String> result = new HashMap<>(2, 1F);
55+
result.put(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.ACTUAL, databaseType) + "/01-actual-init.sql", "/docker-entrypoint-initdb.d/01-actual-init.sql");
56+
result.put(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.EXPECTED, databaseType) + "/01-expected-init.sql", "/docker-entrypoint-initdb.d/01-expected-init.sql");
57+
return result;
58+
}
59+
60+
@Override
61+
public Map<String, String> getMountedResources(final int majorVersion) {
62+
return getMountedResources();
63+
}
64+
65+
@Override
66+
public boolean isEmbeddedStorageContainer() {
67+
return true;
68+
}
69+
70+
@Override
71+
public boolean isRecognizeMajorVersion() {
72+
return false;
73+
}
74+
}

0 commit comments

Comments
 (0)