From d7450c6cac856b543d7316b5260472733fd99b1f Mon Sep 17 00:00:00 2001 From: He Wang Date: Thu, 20 Jun 2024 19:10:01 +0800 Subject: [PATCH] add OceanBaseCETest --- .gitignore | 1 + test/pom.xml | 102 ++++++++++++++++++ .../com/oceanbase/test/OceanBaseCETest.java | 100 +++++++++++++++++ .../test/java/com/oceanbase/test/Utils.java | 98 +++++++++++++++++ 4 files changed, 301 insertions(+) create mode 100644 test/pom.xml create mode 100644 test/src/test/java/com/oceanbase/test/OceanBaseCETest.java create mode 100644 test/src/test/java/com/oceanbase/test/Utils.java diff --git a/.gitignore b/.gitignore index c3e4fa7..776d964 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ oceanbase-ce/init_store_for_fast_start.py .idea .vscode *.log +target diff --git a/test/pom.xml b/test/pom.xml new file mode 100644 index 0000000..84059fb --- /dev/null +++ b/test/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + + com.oceanbase + docker-images-test + 1.0-SNAPSHOT + + + UTF-8 + ${encoding} + ${encoding} + + 1.8 + ${java.version} + ${java.version} + + + + + mysql + mysql-connector-java + 5.1.47 + test + + + com.mysql + mysql-connector-j + 8.4.0 + test + + + com.google.protobuf + protobuf-java + + + + + org.junit.jupiter + junit-jupiter + 5.10.2 + test + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.23.1 + test + + + + + + + com.diffplug.spotless + spotless-maven-plugin + 2.27.1 + + + + 1.7 + + + + + + + UTF-8 + 4 + true + false + false + true + false + false + false + false + + + Leading blank line + project + project + + + + true + + + + + spotless-check + + check + + validate + + + + + + + diff --git a/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java b/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java new file mode 100644 index 0000000..4679393 --- /dev/null +++ b/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2024 OceanBase. + * + * Licensed 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 com.oceanbase.test; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.SQLException; +import java.util.Properties; +import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OceanBaseCETest { + + private static final Logger LOG = LoggerFactory.getLogger(OceanBaseCETest.class); + + static Stream testOceanBaseCEArgs() { + String host = Utils.getNonEmptyEnv("host"); + String port = Utils.getNonEmptyEnv("port"); + String sysUsername = Utils.getNonEmptyEnv("sys_username"); + 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)); + } + + @ParameterizedTest + @MethodSource("testOceanBaseCEArgs") + public void testOceanBaseCE( + boolean useLegacyDriver, + String host, + String port, + String tenantName, + String username, + String password) { + + String jdbcUrl = String.format("jdbc:mysql://%s:%s/test?useSSL=false", host, port); + + Properties props = new Properties(); + props.setProperty("user", username); + if (password != null) { + props.put("password", password); + } + + Driver driver = Utils.getDriver(useLegacyDriver); + try (Connection conn = driver.connect(jdbcUrl, props)) { + LOG.info("Connected to OceanBase successfully."); + LOG.info("Version comment: {}", Utils.getVersionComment(conn)); + + Assertions.assertEquals(tenantName, Utils.getTenantName(conn)); + checkClusterName(conn); + checkRSList(conn); + + if ("sys".equals(tenantName)) { + checkServerIP(conn); + } + } catch (SQLException e) { + Assertions.fail(e); + } + } + + private void checkClusterName(Connection conn) { + Assertions.assertEquals( + Utils.getEnvOrDefault("cluster_name", "obcluster"), Utils.getClusterName(conn)); + } + + private void checkServerIP(Connection conn) { + Assertions.assertEquals( + Utils.getEnvOrDefault("server_ip", "127.0.0.1"), Utils.getServerIP(conn)); + } + + private void checkRSList(Connection conn) { + Assertions.assertEquals( + Utils.getEnvOrDefault("rs_list", "127.0.0.1:2882:2881"), Utils.getRSList(conn)); + } +} diff --git a/test/src/test/java/com/oceanbase/test/Utils.java b/test/src/test/java/com/oceanbase/test/Utils.java new file mode 100644 index 0000000..24607d6 --- /dev/null +++ b/test/src/test/java/com/oceanbase/test/Utils.java @@ -0,0 +1,98 @@ +/* + * Copyright 2024 OceanBase. + * + * Licensed 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 com.oceanbase.test; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class Utils { + + public static Driver getDriver(boolean legacy) { + String className = legacy ? "com.mysql.jdbc.Driver" : "com.mysql.cj.jdbc.Driver"; + try { + return (Driver) Class.forName(className).getDeclaredConstructor().newInstance(); + } catch (Throwable throwable) { + throw new RuntimeException("Failed to load JDBC driver", throwable); + } + } + + public static String getNonEmptyEnv(String key) { + String value = System.getenv(key); + if (value == null || value.trim().isEmpty()) { + throw new IllegalArgumentException("Environment variable '" + key + "' is required"); + } + return value; + } + + public static String getEnvOrDefault(String name, String defaultValue) { + String env = System.getenv(name); + return env == null ? defaultValue : env; + } + + public static String getVersionComment(Connection connection) { + return (String) + query( + connection, + "SHOW VARIABLES LIKE 'version_comment'", + rs -> rs.next() ? rs.getString("VALUE") : null); + } + + public static String getClusterName(Connection connection) { + return (String) + query( + connection, + "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); + } + + public static String getServerIP(Connection connection) { + return (String) + query( + connection, + "SELECT svr_ip FROM oceanbase.__all_server", + rs -> rs.next() ? rs.getString(1) : null); + } + + public static String getRSList(Connection connection) { + return (String) + query( + connection, + "SHOW PARAMETERS LIKE 'rootservice_list'", + rs -> rs.next() ? rs.getString("VALUE") : null); + } + + @FunctionalInterface + interface ResultSetConsumer { + Object apply(ResultSet rs) throws SQLException; + } + + static Object query(Connection connection, String sql, ResultSetConsumer resultSetConsumer) { + try (Statement statement = connection.createStatement()) { + ResultSet rs = statement.executeQuery(sql); + return resultSetConsumer.apply(rs); + } catch (SQLException e) { + throw new RuntimeException("Failed to execute sql: " + sql, e); + } + } +}