Skip to content

Commit f9e482d

Browse files
committed
upgrade OceanBaseContainer with new docker image
1 parent cc870c9 commit f9e482d

File tree

4 files changed

+113
-22
lines changed

4 files changed

+113
-22
lines changed

modules/oceanbase-ce/build.gradle

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
description = "Testcontainers :: JDBC :: OceanBase"
1+
description = "Testcontainers :: JDBC :: OceanBase CE"
22

33
dependencies {
44
api project(':jdbc')
55

66
testImplementation project(':jdbc-test')
7-
testRuntimeOnly 'com.oceanbase:oceanbase-client:2.4.3'
8-
9-
compileOnly 'org.jetbrains:annotations:24.0.1'
7+
testRuntimeOnly 'mysql:mysql-connector-java:8.0.33'
108
}

modules/oceanbase-ce/src/main/java/org/testcontainers/containers/OceanBaseContainer.java

+109-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.testcontainers.containers;
22

3+
import org.apache.commons.lang3.StringUtils;
34
import org.testcontainers.containers.wait.strategy.Wait;
45
import org.testcontainers.utility.DockerImageName;
56

@@ -25,12 +26,53 @@ public class OceanBaseContainer extends JdbcDatabaseContainer<OceanBaseContainer
2526
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse(DOCKER_IMAGE_NAME);
2627

2728
private static final Integer SQL_PORT = 2881;
28-
2929
private static final Integer RPC_PORT = 2882;
3030

31-
private final String databaseName = "test";
32-
private final String username = "root@test";
33-
private final String password = "";
31+
private static final String SYSTEM_TENANT = "sys";
32+
private static final String DEFAULT_USERNAME = "root";
33+
private static final String DEFAULT_PASSWORD = "";
34+
private static final String DEFAULT_TENANT_NAME = "test";
35+
private static final String DEFAULT_DATABASE_NAME = "test";
36+
37+
/**
38+
* The deployment mode of OceanBase. See <a href="https://hub.docker.com/r/oceanbase/oceanbase-ce">Docker Hub</a> for more details.
39+
*/
40+
enum Mode {
41+
/**
42+
* Standard standalone deployment with a monitor service.
43+
*/
44+
NORMAL,
45+
46+
/**
47+
* Similar to 'normal' mode, but uses less hardware resources.
48+
*/
49+
MINI,
50+
51+
/**
52+
* Standalone deployment without the monitor service, which uses the least hardware resources.
53+
*/
54+
SLIM;
55+
56+
static Mode fromString(String mode) {
57+
if (StringUtils.isEmpty(mode)) {
58+
throw new IllegalArgumentException("Mode cannot be null or empty");
59+
}
60+
switch (mode.trim().toLowerCase()) {
61+
case "normal":
62+
return NORMAL;
63+
case "mini":
64+
return MINI;
65+
case "slim":
66+
return SLIM;
67+
default:
68+
throw new IllegalArgumentException("Unsupported mode: " + mode);
69+
}
70+
}
71+
}
72+
73+
private Mode mode = Mode.SLIM;
74+
private String sysRootPassword = DEFAULT_PASSWORD;
75+
private String tenantName = DEFAULT_TENANT_NAME;
3476

3577
public OceanBaseContainer(String dockerImageName) {
3678
this(DockerImageName.parse(dockerImageName));
@@ -45,49 +87,100 @@ public OceanBaseContainer(DockerImageName dockerImageName) {
4587
addExposedPorts(SQL_PORT, RPC_PORT);
4688
}
4789

90+
@Override
91+
protected void waitUntilContainerStarted() {
92+
getWaitStrategy().waitUntilReady(this);
93+
}
94+
4895
public Integer getSqlPort() {
49-
return getMappedPort(SQL_PORT);
96+
return getActualPort(SQL_PORT);
5097
}
5198

52-
public Integer getRpcPort() {
53-
return getMappedPort(RPC_PORT);
99+
public Integer getActualPort(int port) {
100+
return "host".equals(getNetworkMode()) ? port : getMappedPort(port);
54101
}
55102

56103
@Override
57104
public String getDriverClassName() {
58-
return "com.oceanbase.jdbc.Driver";
105+
return "com.mysql.cj.jdbc.Driver";
59106
}
60107

61108
@Override
62109
public String getJdbcUrl() {
110+
return getJdbcUrl(DEFAULT_DATABASE_NAME);
111+
}
112+
113+
public String getJdbcUrl(String databaseName) {
63114
String additionalUrlParams = constructUrlParameters("?", "&");
64-
return (
65-
"jdbc:oceanbase://" + getHost() + ":" + getMappedPort(SQL_PORT) + "/" + databaseName + additionalUrlParams
66-
);
115+
return "jdbc:mysql://" + getHost() + ":" + getSqlPort() + "/" + databaseName + additionalUrlParams;
67116
}
68117

69118
@Override
70119
public String getDatabaseName() {
71-
return databaseName;
120+
return DEFAULT_DATABASE_NAME;
72121
}
73122

74123
@Override
75124
public String getUsername() {
76-
return username;
125+
return DEFAULT_USERNAME + "@" + tenantName;
77126
}
78127

79128
@Override
80129
public String getPassword() {
81-
return password;
130+
return DEFAULT_PASSWORD;
82131
}
83132

84133
@Override
85134
protected String getTestQueryString() {
86135
return "SELECT 1";
87136
}
88137

138+
/**
139+
* Set the deployment mode.
140+
*
141+
* @param mode the deployment mode, can be 'slim', 'mini' or 'normal'
142+
* @return this
143+
*/
144+
public OceanBaseContainer withMode(String mode) {
145+
this.mode = Mode.fromString(mode);
146+
return self();
147+
}
148+
149+
/**
150+
* Set the root password of sys tenant.
151+
*
152+
* @param sysRootPassword the root password of sys tenant
153+
* @return this
154+
*/
155+
public OceanBaseContainer withSysRootPassword(String sysRootPassword) {
156+
if (sysRootPassword == null) {
157+
throw new IllegalArgumentException("The root password of sys tenant cannot be null");
158+
}
159+
this.sysRootPassword = sysRootPassword;
160+
return self();
161+
}
162+
163+
/**
164+
* Set the non-system tenant name to be created for testing.
165+
*
166+
* @param tenantName the tenant name to be created
167+
* @return this
168+
*/
169+
public OceanBaseContainer withTenantName(String tenantName) {
170+
if (StringUtils.isEmpty(tenantName)) {
171+
throw new IllegalArgumentException("Tenant name cannot be null or empty");
172+
}
173+
if (SYSTEM_TENANT.equals(tenantName)) {
174+
throw new IllegalArgumentException("Tenant name cannot be " + SYSTEM_TENANT);
175+
}
176+
this.tenantName = tenantName;
177+
return self();
178+
}
179+
89180
@Override
90-
protected void waitUntilContainerStarted() {
91-
getWaitStrategy().waitUntilReady(this);
181+
protected void configure() {
182+
withEnv("MODE", mode.toString().toLowerCase());
183+
withEnv("OB_ROOT_PASSWORD", sysRootPassword);
184+
withEnv("OB_TENANT_NAME", tenantName);
92185
}
93186
}

modules/oceanbase-ce/src/main/java/org/testcontainers/containers/OceanBaseContainerProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class OceanBaseContainerProvider extends JdbcDatabaseContainerProvider {
99

10-
private static final String DEFAULT_TAG = "4.1.0.0";
10+
private static final String DEFAULT_TAG = "4.2.0.0";
1111

1212
@Override
1313
public boolean supports(String databaseType) {

modules/oceanbase-ce/src/test/java/org/testcontainers/OceanBaseTestImages.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
public class OceanBaseTestImages {
66

7-
public static final DockerImageName OCEANBASE_CE_IMAGE = DockerImageName.parse("oceanbase/oceanbase-ce:4.1.0.0");
7+
public static final DockerImageName OCEANBASE_CE_IMAGE = DockerImageName.parse("oceanbase/oceanbase-ce:4.2.0.0");
88
}

0 commit comments

Comments
 (0)