Skip to content

Commit

Permalink
[FLINK-35638] Refactor OceanBase test cases and remove dependency on …
Browse files Browse the repository at this point in the history
…host network
  • Loading branch information
whhe committed Jun 28, 2024
1 parent 0723009 commit 7e1255d
Show file tree
Hide file tree
Showing 15 changed files with 661 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,21 @@ limitations under the License.

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@

package org.apache.flink.cdc.connectors.oceanbase;

import org.apache.flink.runtime.minicluster.RpcServiceSharing;
import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
import org.apache.flink.cdc.connectors.oceanbase.testutils.OceanBaseCdcMetadata;
import org.apache.flink.table.planner.factories.TestValuesTableFactory;
import org.apache.flink.table.utils.LegacyRowResource;
import org.apache.flink.test.util.MiniClusterWithClientResource;
import org.apache.flink.util.TestLogger;
import org.apache.flink.test.util.AbstractTestBase;

import org.junit.ClassRule;
import org.junit.Rule;

import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
Expand All @@ -43,51 +41,13 @@
import static org.junit.Assert.assertTrue;

/** Basic class for testing OceanBase source. */
public abstract class OceanBaseTestBase extends TestLogger {
public abstract class OceanBaseTestBase extends AbstractTestBase {

private static final Pattern COMMENT_PATTERN = Pattern.compile("^(.*)--.*$");

protected static final int DEFAULT_PARALLELISM = 4;

@Rule
public final MiniClusterWithClientResource miniClusterResource =
new MiniClusterWithClientResource(
new MiniClusterResourceConfiguration.Builder()
.setNumberTaskManagers(1)
.setNumberSlotsPerTaskManager(DEFAULT_PARALLELISM)
.setRpcServiceSharing(RpcServiceSharing.DEDICATED)
.withHaLeadershipControl()
.build());

@ClassRule public static LegacyRowResource usesLegacyRows = LegacyRowResource.INSTANCE;

protected final String compatibleMode;
protected final String username;
protected final String password;
protected final String hostname;
protected final int port;
protected final String logProxyHost;
protected final int logProxyPort;
protected final String tenant;

public OceanBaseTestBase(
String compatibleMode,
String username,
String password,
String hostname,
int port,
String logProxyHost,
int logProxyPort,
String tenant) {
this.compatibleMode = compatibleMode;
this.username = username;
this.password = password;
this.hostname = hostname;
this.port = port;
this.logProxyHost = logProxyHost;
this.logProxyPort = logProxyPort;
this.tenant = tenant;
}
protected abstract OceanBaseCdcMetadata metadata();

protected String commonOptionsString() {
return String.format(
Expand All @@ -96,8 +56,14 @@ protected String commonOptionsString() {
+ " 'password' = '%s', "
+ " 'hostname' = '%s', "
+ " 'port' = '%s', "
+ " 'compatible-mode' = '%s'",
username, password, hostname, port, compatibleMode);
+ " 'compatible-mode' = '%s', "
+ " 'jdbc.driver' = '%s'",
metadata().getUsername(),
metadata().getPassword(),
metadata().getHostname(),
metadata().getPort(),
metadata().getCompatibleMode(),
metadata().getDriverClass());
}

protected String logProxyOptionsString() {
Expand All @@ -106,7 +72,9 @@ protected String logProxyOptionsString() {
+ " 'tenant-name' = '%s',"
+ " 'logproxy.host' = '%s',"
+ " 'logproxy.port' = '%s'",
tenant, logProxyHost, logProxyPort);
metadata().getTenantName(),
metadata().getLogProxyHost(),
metadata().getLogProxyPort());
}

protected String initialOptionsString() {
Expand All @@ -120,7 +88,10 @@ protected String snapshotOptionsString() {
return " 'scan.startup.mode' = 'snapshot', " + commonOptionsString();
}

protected abstract Connection getJdbcConnection() throws SQLException;
protected Connection getJdbcConnection() throws SQLException {
return DriverManager.getConnection(
metadata().getJdbcUrl(), metadata().getUsername(), metadata().getPassword());
}

protected void setGlobalTimeZone(String serverTimeZone) throws SQLException {
try (Connection connection = getJdbcConnection();
Expand All @@ -130,7 +101,8 @@ protected void setGlobalTimeZone(String serverTimeZone) throws SQLException {
}

protected void initializeTable(String sqlFile) {
final String ddlFile = String.format("ddl/%s/%s.sql", compatibleMode, sqlFile);
final String ddlFile =
String.format("ddl/%s/%s.sql", metadata().getCompatibleMode(), sqlFile);
final URL ddlTestFile = getClass().getClassLoader().getResource(ddlFile);
assertNotNull("Cannot locate " + ddlFile, ddlTestFile);
try (Connection connection = getJdbcConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.cdc.connectors.oceanbase;

import org.apache.flink.cdc.connectors.oceanbase.testutils.LogProxyContainer;
import org.apache.flink.cdc.connectors.oceanbase.testutils.OceanBaseCdcMetadata;
import org.apache.flink.cdc.connectors.oceanbase.testutils.OceanBaseContainer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;

/** Utils to help test. */
@SuppressWarnings("resource")
public class OceanBaseTestUtils {

private static final Logger LOG = LoggerFactory.getLogger(OceanBaseTestUtils.class);

public static final Network NETWORK = Network.newNetwork();

private static final String LATEST_VERSION = "latest";
private static final String CDC_TEST_OB_VERSION = "4.2.1.6-106000012024042515";

private static final String SYS_PASSWORD = "123456";
private static final String TEST_PASSWORD = "654321";

public static OceanBaseContainer createOceanBaseContainer() {
return createOceanBaseContainer(CDC_TEST_OB_VERSION, "mini");
}

public static OceanBaseContainer createOceanBaseContainer(String mode) {
return createOceanBaseContainer(LATEST_VERSION, mode);
}

public static OceanBaseContainer createOceanBaseContainer(String version, String mode) {
return new OceanBaseContainer(version)
.withNetwork(NETWORK)
.withMode(mode)
.withSysPassword(SYS_PASSWORD)
.withTenantPassword(TEST_PASSWORD)
.withStartupTimeout(Duration.ofMinutes(4))
.withLogConsumer(new Slf4jLogConsumer(LOG));
}

public static LogProxyContainer createLogProxyContainer() {
return createLogProxyContainer(LATEST_VERSION);
}

public static LogProxyContainer createLogProxyContainer(String version) {
return new LogProxyContainer(version)
.withNetwork(NETWORK)
.withSysPassword(SYS_PASSWORD)
.withStartupTimeout(Duration.ofMinutes(1))
.withLogConsumer(new Slf4jLogConsumer(LOG));
}

public static Connection getConnection(OceanBaseCdcMetadata metadata) throws SQLException {
return DriverManager.getConnection(
metadata.getJdbcUrl(), metadata.getUsername(), metadata.getPassword());
}

public static String queryRootServiceList(OceanBaseCdcMetadata metadata) throws SQLException {
try (Connection connection = getConnection(metadata);
Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery("SHOW PARAMETERS LIKE 'rootservice_list'");
return rs.next() ? rs.getString("VALUE") : null;
}
}

public static String queryConfigUrl(OceanBaseCdcMetadata metadata) throws SQLException {
try (Connection connection = getConnection(metadata);
Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery("SHOW PARAMETERS LIKE 'obconfig_url'");
return rs.next() ? rs.getString("VALUE") : null;
}
}
}
Loading

0 comments on commit 7e1255d

Please sign in to comment.