Skip to content

Commit 80bcc3d

Browse files
whhefokion
authored andcommitted
Support tenant name, password and mode in OceanBase module (testcontainers#8737)
1 parent fcc2e6d commit 80bcc3d

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

docs/modules/databases/jdbc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Insert `tc:` after `jdbc:` as follows. Note that the hostname, port and database
5757

5858
#### Using OceanBase
5959

60-
`jdbc:tc:oceanbasece:4.2.2:///databasename`
60+
`jdbc:tc:oceanbasece:4.2.1-lts:///databasename`
6161

6262
#### Using Oracle
6363

modules/oceanbase/src/main/java/org/testcontainers/oceanbase/OceanBaseCEContainer.java

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testcontainers.oceanbase;
22

33
import org.testcontainers.containers.JdbcDatabaseContainer;
4+
import org.testcontainers.containers.wait.strategy.Wait;
45
import org.testcontainers.utility.DockerImageName;
56

67
/**
@@ -26,14 +27,20 @@ public class OceanBaseCEContainer extends JdbcDatabaseContainer<OceanBaseCEConta
2627

2728
private static final Integer RPC_PORT = 2882;
2829

29-
private static final String DEFAULT_TEST_TENANT_NAME = "test";
30+
private static final String DEFAULT_TENANT_NAME = "test";
3031

31-
private static final String DEFAULT_USERNAME = "root";
32+
private static final String DEFAULT_USER = "root";
3233

3334
private static final String DEFAULT_PASSWORD = "";
3435

3536
private static final String DEFAULT_DATABASE_NAME = "test";
3637

38+
private Mode mode = Mode.SLIM;
39+
40+
private String tenantName = DEFAULT_TENANT_NAME;
41+
42+
private String password = DEFAULT_PASSWORD;
43+
3744
public OceanBaseCEContainer(String dockerImageName) {
3845
this(DockerImageName.parse(dockerImageName));
3946
}
@@ -43,6 +50,29 @@ public OceanBaseCEContainer(DockerImageName dockerImageName) {
4350
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
4451

4552
addExposedPorts(SQL_PORT, RPC_PORT);
53+
setWaitStrategy(Wait.forLogMessage(".*boot success!.*", 1));
54+
}
55+
56+
@Override
57+
protected void configure() {
58+
addEnv("MODE", mode.name().toLowerCase());
59+
60+
if (!DEFAULT_TENANT_NAME.equals(tenantName)) {
61+
if (mode == Mode.SLIM) {
62+
logger().warn("The tenant name is not configurable on slim mode, so this option will be ignored.");
63+
// reset the tenant name to ensure the constructed username is correct
64+
tenantName = DEFAULT_TENANT_NAME;
65+
} else {
66+
addEnv("OB_TENANT_NAME", tenantName);
67+
}
68+
}
69+
70+
addEnv("OB_TENANT_PASSWORD", password);
71+
}
72+
73+
@Override
74+
protected void waitUntilContainerStarted() {
75+
getWaitStrategy().waitUntilReady(this);
4676
}
4777

4878
@Override
@@ -64,19 +94,49 @@ public String getDatabaseName() {
6494

6595
@Override
6696
public String getUsername() {
67-
// In OceanBase, the jdbc username is related to the name of user, tenant and cluster,
68-
// if a tenant name other than the default value 'test' is used, you should manually
69-
// construct the jdbc username by yourself.
70-
return DEFAULT_USERNAME + "@" + DEFAULT_TEST_TENANT_NAME;
97+
return DEFAULT_USER + "@" + tenantName;
7198
}
7299

73100
@Override
74101
public String getPassword() {
75-
return DEFAULT_PASSWORD;
102+
return password;
76103
}
77104

78105
@Override
79106
protected String getTestQueryString() {
80107
return "SELECT 1";
81108
}
109+
110+
public OceanBaseCEContainer withMode(Mode mode) {
111+
this.mode = mode;
112+
return this;
113+
}
114+
115+
public OceanBaseCEContainer withTenantName(String tenantName) {
116+
this.tenantName = tenantName;
117+
return this;
118+
}
119+
120+
public OceanBaseCEContainer withPassword(String password) {
121+
this.password = password;
122+
return this;
123+
}
124+
125+
public enum Mode {
126+
/**
127+
* Use as much hardware resources as possible for deployment by default,
128+
* and all environment variables are available.
129+
*/
130+
NORMAL,
131+
/**
132+
* Use the minimum hardware resources for deployment by default,
133+
* and all environment variables are available.
134+
*/
135+
MINI,
136+
/**
137+
* Use minimal hardware resources and pre-built deployment files for quick startup,
138+
* and password of user tenant is the only available environment variable.
139+
*/
140+
SLIM,
141+
}
82142
}

modules/oceanbase/src/main/java/org/testcontainers/oceanbase/OceanBaseCEContainerProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class OceanBaseCEContainerProvider extends JdbcDatabaseContainerProvider {
1111

12-
private static final String DEFAULT_TAG = "4.2.2";
12+
private static final String DEFAULT_TAG = "4.2.1-lts";
1313

1414
@Override
1515
public boolean supports(String databaseType) {

modules/oceanbase/src/test/java/org/testcontainers/oceanbase/SimpleOceanBaseCETest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ public class SimpleOceanBaseCETest extends AbstractContainerDatabaseTest {
1919

2020
@SuppressWarnings("resource")
2121
private OceanBaseCEContainer testContainer() {
22-
return ((OceanBaseCEContainer) containerProvider.newInstance()).withEnv("MODE", "slim")
23-
.withEnv("FASTBOOT", "true")
24-
.withLogConsumer(new Slf4jLogConsumer(logger));
22+
return ((OceanBaseCEContainer) containerProvider.newInstance()).withLogConsumer(new Slf4jLogConsumer(logger));
2523
}
2624

2725
@Test

0 commit comments

Comments
 (0)