Skip to content

Commit

Permalink
update to latest docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
whhe committed Jan 5, 2024
1 parent b1cd817 commit fed062f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.testcontainers.containers;

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

import java.time.Duration;

/**
* Testcontainers implementation for OceanBase.
* <p>
Expand All @@ -31,48 +28,12 @@ public class OceanBaseContainer extends JdbcDatabaseContainer<OceanBaseContainer
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_TEST_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;
private boolean enableFastboot;
private String mode;
private String tenantName = DEFAULT_TEST_TENANT_NAME;

public OceanBaseContainer(String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
Expand All @@ -82,22 +43,12 @@ public OceanBaseContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

this.waitStrategy = Wait.forLogMessage(".*boot success!.*", 1).withStartupTimeout(Duration.ofMinutes(3));

addExposedPorts(SQL_PORT, RPC_PORT);
}

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

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

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

@Override
Expand All @@ -112,7 +63,7 @@ public String getJdbcUrl() {

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

@Override
Expand All @@ -136,37 +87,33 @@ protected String getTestQueryString() {
}

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

/**
* Set the root password of sys tenant.
* Set the deployment mode, see <a href="https://hub.docker.com/r/oceanbase/oceanbase-ce">Docker Hub</a> for more details.
*
* @param sysRootPassword the root password of sys tenant
* @param mode the deployment mode
* @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;
public OceanBaseContainer withMode(String mode) {
this.mode = mode;
return self();
}

/**
* Set the non-system tenant name to be created for testing.
* Set the non-system tenant to be created for testing.
*
* @param tenantName the tenant name to be created
* @param tenantName the name of tenant to be created
* @return this
*/
public OceanBaseContainer withTenantName(String tenantName) {
public OceanBaseContainer withTenant(String tenantName) {
if (StringUtils.isEmpty(tenantName)) {
throw new IllegalArgumentException("Tenant name cannot be null or empty");
}
Expand All @@ -179,8 +126,14 @@ public OceanBaseContainer withTenantName(String tenantName) {

@Override
protected void configure() {
withEnv("MODE", mode.toString().toLowerCase());
withEnv("OB_ROOT_PASSWORD", sysRootPassword);
withEnv("OB_TENANT_NAME", tenantName);
if (StringUtils.isNotBlank(mode)) {
withEnv("MODE", mode);
}
if (enableFastboot) {
withEnv("FASTBOOT", "true");
}
if (!DEFAULT_TEST_TENANT_NAME.equals(tenantName)) {
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.2.0.0";
private static final String DEFAULT_TAG = "4.2.1_bp3";

@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.2.0.0");
public static final DockerImageName OCEANBASE_CE_IMAGE = DockerImageName.parse("oceanbase/oceanbase-ce:4.2.1_bp3");
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,32 @@ public class SimpleOceanBaseTest extends AbstractContainerDatabaseTest {
public void testSimple() throws SQLException {
try (
OceanBaseContainer container = new OceanBaseContainer(OceanBaseTestImages.OCEANBASE_CE_IMAGE)
.withMode("slim")
.enableFastboot()
.withLogConsumer(new Slf4jLogConsumer(logger))
) {
container.start();

ResultSet resultSet = performQuery(container, "SELECT 1");
int resultSetInt = resultSet.getInt(1);

assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
assertHasCorrectExposedAndLivenessCheckPorts(container);
}
}

@Test
public void testExplicitInitScript() throws SQLException {
try (
OceanBaseContainer container = new OceanBaseContainer(OceanBaseTestImages.OCEANBASE_CE_IMAGE)
.withMode("slim")
.enableFastboot()
.withInitScript("init.sql")
.withLogConsumer(new Slf4jLogConsumer(logger))
) {
container.start();

ResultSet resultSet = performQuery(container, "SELECT foo FROM bar");
String firstColumnValue = resultSet.getString(1);

assertThat(firstColumnValue).as("Value from init script should equal real value").isEqualTo("hello world");
}
}
Expand All @@ -52,15 +55,25 @@ public void testExplicitInitScript() throws SQLException {
public void testWithAdditionalUrlParamInJdbcUrl() {
try (
OceanBaseContainer container = new OceanBaseContainer(OceanBaseTestImages.OCEANBASE_CE_IMAGE)
.withMode("slim")
.enableFastboot()
.withUrlParam("useSSL", "false")
.withLogConsumer(new Slf4jLogConsumer(logger))
) {
container.start();

container.start();
String jdbcUrl = container.getJdbcUrl();
assertThat(jdbcUrl).contains("?");
assertThat(jdbcUrl).contains("useSSL=false");
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(OceanBaseContainer container) {
int sqlPort = 2881;
int rpcPort = 2882;

assertThat(container.getExposedPorts()).containsExactlyInAnyOrder(sqlPort, rpcPort);
assertThat(container.getLivenessCheckPortNumbers())
.containsExactlyInAnyOrder(container.getMappedPort(sqlPort), container.getMappedPort(rpcPort));
}
}

0 comments on commit fed062f

Please sign in to comment.