Skip to content

Commit

Permalink
simplify java test env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
whhe committed Jun 24, 2024
1 parent 55b12bc commit b6f93cf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/java-test-oceanbase-ce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,12 @@ jobs:

- name: Test Docker container
env:
server_ip: ${{ steps.set_server_ip.outputs.server_ip }}
cluster_name: ${{ inputs.cluster_name }}
host: '127.0.0.1'
port: ${{ inputs.port }}
sys_username: 'root'
sys_password: ${{ inputs.sys_password }}
test_tenant: ${{ inputs.test_tenant }}
test_username: 'root@${{ inputs.test_tenant }}'
test_password: ${{ inputs.test_password }}
server_ip: ${{ steps.set_server_ip.outputs.server_ip }}
rs_list: ${{ steps.set_server_ip.outputs.server_ip }}:2882:2881
run: |
cd test
mvn verify -Dtest=OceanBaseCETest -DfailIfNoTests=false
77 changes: 40 additions & 37 deletions test/src/test/java/com/oceanbase/test/OceanBaseCETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,60 @@ public class OceanBaseCETest {
private static final Logger LOG = LoggerFactory.getLogger(OceanBaseCETest.class);

static Stream<Arguments> testOceanBaseCEArgs() {
String host = Utils.getNonEmptyEnv("host");
// non-null env vars
String serverIP = Utils.getNonEmptyEnv("server_ip");
String clusterName = Utils.getNonEmptyEnv("cluster_name");
String port = Utils.getNonEmptyEnv("port");
String sysUsername = Utils.getNonEmptyEnv("sys_username");
String testTenant = Utils.getNonEmptyEnv("test_tenant");

// nullable env vars
String sysPassword = System.getenv("sys_password");
String testTenant = Utils.getEnvOrDefault("test_tenant", "test");
String testUsername = Utils.getNonEmptyEnv("test_username");
String testPassword = System.getenv("test_password");

return Stream.of(
Arguments.of(true, host, port, "sys", sysUsername, sysPassword),
Arguments.of(false, host, port, "sys", sysUsername, sysPassword),
Arguments.of(true, host, port, testTenant, testUsername, testPassword),
Arguments.of(false, host, port, testTenant, testUsername, testPassword));
Arguments.of(true, serverIP, clusterName, port, "sys", "root", sysPassword),
Arguments.of(false, serverIP, clusterName, port, "sys", "root", sysPassword),
Arguments.of(
true,
serverIP,
clusterName,
port,
testTenant,
"root@" + testTenant,
testPassword),
Arguments.of(
false,
serverIP,
clusterName,
port,
testTenant,
"root@" + testTenant,
testPassword));
}

@ParameterizedTest
@MethodSource("testOceanBaseCEArgs")
public void testOceanBaseCE(
boolean useLegacyDriver,
String host,
String serverIP,
String clusterName,
String port,
String tenantName,
String username,
String password) {

boolean slimMode = "127.0.0.1".equals(serverIP);

LOG.info(
"Testing with args: [useLegacyDriver: {}, host: {}, port: {}, username: {}, password: {}]",
"Testing with args: [useLegacyDriver: {}, server_ip: {}, cluster_name: {}, port: {}, username: {}, password: {}]",
useLegacyDriver,
host,
serverIP,
clusterName,
port,
username,
password);

String jdbcUrl = String.format("jdbc:mysql://%s:%s/test?useSSL=false", host, port);
String jdbcUrl = String.format("jdbc:mysql://127.0.0.1:%s/test?useSSL=false", port);

Properties props = new Properties();
props.setProperty("user", username);
Expand All @@ -78,40 +98,23 @@ public void testOceanBaseCE(
try (Connection conn = driver.connect(jdbcUrl, props)) {
LOG.info("Connected to OceanBase CE successfully");

LOG.info("Version comment: {}", Utils.getVersionComment(conn));
Assertions.assertNotNull(Utils.getVersionComment(conn));

String tenant = Utils.getTenantName(conn);
LOG.info("Tenant name: {}", tenant);
Assertions.assertEquals(tenantName, tenant);
Assertions.assertEquals(serverIP + ":2882:2881", Utils.getRSList(conn));
Assertions.assertEquals(clusterName, Utils.getClusterName(conn));
Assertions.assertEquals(tenantName, Utils.getTenantName(conn));

checkClusterName(conn);
checkRSList(conn);
if (!slimMode) {
Assertions.assertNotNull(Utils.getConfigUrl(conn));
}

if ("sys".equals(tenantName)) {
checkServerIP(conn);
Assertions.assertEquals(serverIP, Utils.getServerIP(conn));
} else {
Assertions.assertEquals(2, Utils.getTableRowsCount(conn, "user"));
}
} catch (SQLException e) {
Assertions.fail(e);
}
}

private void checkClusterName(Connection conn) {
String clusterName = Utils.getClusterName(conn);
LOG.info("Cluster name: {}", clusterName);
Assertions.assertEquals(Utils.getEnvOrDefault("cluster_name", "obcluster"), clusterName);
}

private void checkServerIP(Connection conn) {
String serverIP = Utils.getServerIP(conn);
LOG.info("Server IP: {}", serverIP);
Assertions.assertEquals(Utils.getEnvOrDefault("server_ip", "127.0.0.1"), serverIP);
}

private void checkRSList(Connection conn) {
String rsList = Utils.getRSList(conn);
LOG.info("RS List: {}", rsList);
Assertions.assertEquals(Utils.getEnvOrDefault("rs_list", "127.0.0.1:2882:2881"), rsList);
}
}
33 changes: 30 additions & 3 deletions test/src/test/java/com/oceanbase/test/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

public class Utils {

private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(Utils.class);

public static Driver getDriver(boolean legacy) {
String className = legacy ? "com.mysql.jdbc.Driver" : "com.mysql.cj.jdbc.Driver";
try {
Expand All @@ -50,6 +52,7 @@ public static String getVersionComment(Connection connection) {
return (String)
query(
connection,
"version_comment",
"SHOW VARIABLES LIKE 'version_comment'",
rs -> rs.next() ? rs.getString("VALUE") : null);
}
Expand All @@ -58,18 +61,25 @@ public static String getClusterName(Connection connection) {
return (String)
query(
connection,
"cluster",
"SHOW PARAMETERS LIKE 'cluster'",
rs -> rs.next() ? rs.getString("VALUE") : null);
}

public static String getTenantName(Connection connection) {
return (String) query(connection, "SHOW TENANT", rs -> rs.next() ? rs.getString(1) : null);
return (String)
query(
connection,
"tenant",
"SHOW TENANT",
rs -> rs.next() ? rs.getString(1) : null);
}

public static String getServerIP(Connection connection) {
return (String)
query(
connection,
"svr_ip",
"SELECT svr_ip FROM oceanbase.__all_server",
rs -> rs.next() ? rs.getString(1) : null);
}
Expand All @@ -78,14 +88,25 @@ public static String getRSList(Connection connection) {
return (String)
query(
connection,
"rootservice_list",
"SHOW PARAMETERS LIKE 'rootservice_list'",
rs -> rs.next() ? rs.getString("VALUE") : null);
}

public static String getConfigUrl(Connection connection) {
return (String)
query(
connection,
"obconfig_url",
"SHOW PARAMETERS LIKE 'obconfig_url'",
rs -> rs.next() ? rs.getString("VALUE") : null);
}

public static int getTableRowsCount(Connection connection, String tableName) {
return (int)
query(
connection,
"table '" + tableName + "' rows count",
"SELECT COUNT(1) FROM " + tableName,
rs -> rs.next() ? rs.getInt(1) : 0);
}
Expand All @@ -95,10 +116,16 @@ interface ResultSetConsumer {
Object apply(ResultSet rs) throws SQLException;
}

static Object query(Connection connection, String sql, ResultSetConsumer resultSetConsumer) {
static Object query(
Connection connection,
String queryName,
String sql,
ResultSetConsumer resultSetConsumer) {
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(sql);
return resultSetConsumer.apply(rs);
Object result = resultSetConsumer.apply(rs);
LOG.info("Query: {}, got result: {}", queryName, result);
return result;
} catch (SQLException e) {
throw new RuntimeException("Failed to execute sql: " + sql, e);
}
Expand Down

0 comments on commit b6f93cf

Please sign in to comment.