Skip to content

Commit

Permalink
upgrade OceanBaseContainer with new docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
whhe committed Sep 20, 2023
1 parent cc870c9 commit f9e482d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 22 deletions.
6 changes: 2 additions & 4 deletions modules/oceanbase-ce/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
description = "Testcontainers :: JDBC :: OceanBase"
description = "Testcontainers :: JDBC :: OceanBase CE"

dependencies {
api project(':jdbc')

testImplementation project(':jdbc-test')
testRuntimeOnly 'com.oceanbase:oceanbase-client:2.4.3'

compileOnly 'org.jetbrains:annotations:24.0.1'
testRuntimeOnly 'mysql:mysql-connector-java:8.0.33'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.testcontainers.containers;

import org.apache.commons.lang3.StringUtils;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

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

private static final Integer SQL_PORT = 2881;

private static final Integer RPC_PORT = 2882;

private final String databaseName = "test";
private final String username = "root@test";
private final String password = "";
private static final String SYSTEM_TENANT = "sys";
private static final String DEFAULT_USERNAME = "root";
private static final String DEFAULT_PASSWORD = "";
private static final String DEFAULT_TENANT_NAME = "test";
private static final String DEFAULT_DATABASE_NAME = "test";

/**
* The deployment mode of OceanBase. See <a href="https://hub.docker.com/r/oceanbase/oceanbase-ce">Docker Hub</a> for more details.
*/
enum Mode {
/**
* Standard standalone deployment with a monitor service.
*/
NORMAL,

/**
* Similar to 'normal' mode, but uses less hardware resources.
*/
MINI,

/**
* Standalone deployment without the monitor service, which uses the least hardware resources.
*/
SLIM;

static Mode fromString(String mode) {
if (StringUtils.isEmpty(mode)) {
throw new IllegalArgumentException("Mode cannot be null or empty");
}
switch (mode.trim().toLowerCase()) {
case "normal":
return NORMAL;
case "mini":
return MINI;
case "slim":
return SLIM;
default:
throw new IllegalArgumentException("Unsupported mode: " + mode);
}
}
}

private Mode mode = Mode.SLIM;
private String sysRootPassword = DEFAULT_PASSWORD;
private String tenantName = DEFAULT_TENANT_NAME;

public OceanBaseContainer(String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
Expand All @@ -45,49 +87,100 @@ public OceanBaseContainer(DockerImageName dockerImageName) {
addExposedPorts(SQL_PORT, RPC_PORT);
}

@Override
protected void waitUntilContainerStarted() {
getWaitStrategy().waitUntilReady(this);
}

public Integer getSqlPort() {
return getMappedPort(SQL_PORT);
return getActualPort(SQL_PORT);
}

public Integer getRpcPort() {
return getMappedPort(RPC_PORT);
public Integer getActualPort(int port) {
return "host".equals(getNetworkMode()) ? port : getMappedPort(port);
}

@Override
public String getDriverClassName() {
return "com.oceanbase.jdbc.Driver";
return "com.mysql.cj.jdbc.Driver";
}

@Override
public String getJdbcUrl() {
return getJdbcUrl(DEFAULT_DATABASE_NAME);
}

public String getJdbcUrl(String databaseName) {
String additionalUrlParams = constructUrlParameters("?", "&");
return (
"jdbc:oceanbase://" + getHost() + ":" + getMappedPort(SQL_PORT) + "/" + databaseName + additionalUrlParams
);
return "jdbc:mysql://" + getHost() + ":" + getSqlPort() + "/" + databaseName + additionalUrlParams;
}

@Override
public String getDatabaseName() {
return databaseName;
return DEFAULT_DATABASE_NAME;
}

@Override
public String getUsername() {
return username;
return DEFAULT_USERNAME + "@" + tenantName;
}

@Override
public String getPassword() {
return password;
return DEFAULT_PASSWORD;
}

@Override
protected String getTestQueryString() {
return "SELECT 1";
}

/**
* Set the deployment mode.
*
* @param mode the deployment mode, can be 'slim', 'mini' or 'normal'
* @return this
*/
public OceanBaseContainer withMode(String mode) {
this.mode = Mode.fromString(mode);
return self();
}

/**
* Set the root password of sys tenant.
*
* @param sysRootPassword the root password of sys tenant
* @return this
*/
public OceanBaseContainer withSysRootPassword(String sysRootPassword) {
if (sysRootPassword == null) {
throw new IllegalArgumentException("The root password of sys tenant cannot be null");
}
this.sysRootPassword = sysRootPassword;
return self();
}

/**
* Set the non-system tenant name to be created for testing.
*
* @param tenantName the tenant name to be created
* @return this
*/
public OceanBaseContainer withTenantName(String tenantName) {
if (StringUtils.isEmpty(tenantName)) {
throw new IllegalArgumentException("Tenant name cannot be null or empty");
}
if (SYSTEM_TENANT.equals(tenantName)) {
throw new IllegalArgumentException("Tenant name cannot be " + SYSTEM_TENANT);
}
this.tenantName = tenantName;
return self();
}

@Override
protected void waitUntilContainerStarted() {
getWaitStrategy().waitUntilReady(this);
protected void configure() {
withEnv("MODE", mode.toString().toLowerCase());
withEnv("OB_ROOT_PASSWORD", sysRootPassword);
withEnv("OB_TENANT_NAME", tenantName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class OceanBaseContainerProvider extends JdbcDatabaseContainerProvider {

private static final String DEFAULT_TAG = "4.1.0.0";
private static final String DEFAULT_TAG = "4.2.0.0";

@Override
public boolean supports(String databaseType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

public class OceanBaseTestImages {

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

0 comments on commit f9e482d

Please sign in to comment.