From a2adf7dd964d23f7a83a7b3e383e7f78cd7a23ad Mon Sep 17 00:00:00 2001 From: He Wang Date: Thu, 20 Jun 2024 19:10:01 +0800 Subject: [PATCH 1/7] test: add Java test cases for oceanbase-ce --- .github/workflows/java-test-oceanbase-ce.yml | 89 ++++++++++++++ .github/workflows/test-oceanbase-ce.yml | 45 +++---- .gitignore | 1 + test/pom.xml | 110 +++++++++++++++++ .../com/oceanbase/test/OceanBaseCETest.java | 115 ++++++++++++++++++ .../test/java/com/oceanbase/test/Utils.java | 98 +++++++++++++++ .../src/test/resources/log4j2-test.properties | 27 ++++ 7 files changed, 459 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/java-test-oceanbase-ce.yml 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 create mode 100644 test/src/test/resources/log4j2-test.properties diff --git a/.github/workflows/java-test-oceanbase-ce.yml b/.github/workflows/java-test-oceanbase-ce.yml new file mode 100644 index 0000000..0a5327f --- /dev/null +++ b/.github/workflows/java-test-oceanbase-ce.yml @@ -0,0 +1,89 @@ +name: java test oceanbase-ce + +on: + workflow_call: + inputs: + cache_key: + required: true + type: string + image_file: + required: true + type: string + mode: + required: true + type: string + port: + required: true + type: string + sys_password: + required: false + type: string + default: '' + test_tenant: + required: false + type: string + default: 'test' + test_password: + required: false + type: string + default: '' + server_ip: + required: false + type: string + default: '127.0.0.1' + rs_list: + required: false + type: string + default: '127.0.0.1:2882:2881' + init_sql: + required: false + type: string + default: '' + +jobs: + test-oceanbase-ce: + runs-on: ubuntu-latest + steps: + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: ${{ inputs.cache_key }} + path: /tmp + + - name: Load Docker image + run: docker load -i /tmp/${{ inputs.image_file }} + + - name: Start Docker container + uses: oceanbase/setup-oceanbase-ce@v1 + with: + image_name: oceanbase-ce + mode: ${{ inputs.mode }} + sql_port: ${{ inputs.port }} + sys_root_password: ${{ inputs.sys_password }} + tenant_name: ${{ inputs.test_tenant }} + init_sql: ${{ inputs.init_sql }} + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Java + uses: actions/setup-java@v4 + with: + java-version: '8' + distribution: 'zulu' + + - name: Test Docker container + env: + cluster_name: 'github-action' + 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: ${{ inputs.server_ip }} + rs_list: ${{ inputs.rs_list }} + run: | + cd test + mvn verify -Dtest=OceanBaseCETest -DfailIfNoTests=false diff --git a/.github/workflows/test-oceanbase-ce.yml b/.github/workflows/test-oceanbase-ce.yml index b6186f9..d826b2d 100644 --- a/.github/workflows/test-oceanbase-ce.yml +++ b/.github/workflows/test-oceanbase-ce.yml @@ -2,13 +2,16 @@ name: test oceanbase-ce on: push: - paths: - - '.github/workflows/test-oceanbase-ce.yml' - - 'oceanbase-ce/**' + branches: [ main ] pull_request: paths: - - '.github/workflows/test-oceanbase-ce.yml' + - '.github/workflows/**-oceanbase-ce.yml' - 'oceanbase-ce/**' + - 'test/**' + +concurrency: + group: test-oceanbase-ce-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true jobs: build: @@ -48,26 +51,16 @@ jobs: path: oceanbase-ce.tar test-slim: - runs-on: ubuntu-latest needs: build - steps: - - name: Download artifact - uses: actions/download-artifact@v4 - with: - name: oceanbase-ce - path: /tmp - - - name: Load Docker image - run: docker load -i /tmp/oceanbase-ce.tar - - - name: Start Docker container - uses: oceanbase/setup-oceanbase-ce@v1 - with: - image_name: oceanbase-ce - container_name: ob-slim - fastboot: false - - - name: Test Docker container - run: | - docker exec ob-slim obclient -h127.0.0.1 -P2881 -uroot -e 'select version()' - docker exec ob-slim obclient -h127.0.0.1 -P2881 -uroot@test -e 'show databases' + uses: ./.github/workflows/java-test-oceanbase-ce.yml + with: + cache_key: oceanbase-ce + image_file: oceanbase-ce.tar + mode: slim + port: 1234 + sys_password: '' + test_tenant: test + test_password: 123456 + server_ip: 127.0.0.1 + rs_list: 127.0.0.1:2882:2881 + init_sql: "ALTER USER root IDENTIFIED BY '123456'" diff --git a/.gitignore b/.gitignore index 7c6ddf0..72cc5b1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea .vscode *.log +target oceanbase-ce/Dockerfile.inner diff --git a/test/pom.xml b/test/pom.xml new file mode 100644 index 0000000..c82fb8b --- /dev/null +++ b/test/pom.xml @@ -0,0 +1,110 @@ + + + 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 + + + + + + + maven-surefire-plugin + 3.1.2 + + + maven-failsafe-plugin + 3.1.2 + + + 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..20e0422 --- /dev/null +++ b/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java @@ -0,0 +1,115 @@ +/* + * 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) { + + LOG.info( + "Testing with args: [useLegacyDriver: {}, host: {}, port: {}, username: {}, password: {}]", + useLegacyDriver, + host, + port, + username, + 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 CE successfully"); + + LOG.info("Version comment: {}", Utils.getVersionComment(conn)); + + String tenant = Utils.getTenantName(conn); + LOG.info("Tenant name: {}", tenant); + Assertions.assertEquals(tenantName, tenant); + + checkClusterName(conn); + checkRSList(conn); + + if ("sys".equals(tenantName)) { + checkServerIP(conn); + } + } 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); + } +} 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); + } + } +} diff --git a/test/src/test/resources/log4j2-test.properties b/test/src/test/resources/log4j2-test.properties new file mode 100644 index 0000000..3964819 --- /dev/null +++ b/test/src/test/resources/log4j2-test.properties @@ -0,0 +1,27 @@ +#################################################################### +# 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. +#################################################################### + +# Set root logger level to OFF to not flood build logs +# set manually to INFO for debugging purposes +rootLogger.level=INFO +rootLogger.appenderRef.test.ref = TestLogger + +appender.testlogger.name = TestLogger +appender.testlogger.type = CONSOLE +appender.testlogger.target = SYSTEM_ERR +appender.testlogger.layout.type = PatternLayout +appender.testlogger.layout.pattern = %-4r [%t] %-5p %c - %m%n From d240b43855444c20c1a72dba8d3e193917ae423f Mon Sep 17 00:00:00 2001 From: He Wang Date: Mon, 24 Jun 2024 16:55:32 +0800 Subject: [PATCH 2/7] update for new image --- .github/workflows/java-test-oceanbase-ce.yml | 23 +++++++++++++++---- .github/workflows/test-oceanbase-ce.yml | 21 ++++++++++++++--- .../com/oceanbase/test/OceanBaseCETest.java | 2 ++ .../test/java/com/oceanbase/test/Utils.java | 8 +++++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.github/workflows/java-test-oceanbase-ce.yml b/.github/workflows/java-test-oceanbase-ce.yml index 0a5327f..b314433 100644 --- a/.github/workflows/java-test-oceanbase-ce.yml +++ b/.github/workflows/java-test-oceanbase-ce.yml @@ -30,11 +30,11 @@ on: server_ip: required: false type: string - default: '127.0.0.1' + default: '' rs_list: required: false type: string - default: '127.0.0.1:2882:2881' + default: '' init_sql: required: false type: string @@ -57,12 +57,27 @@ jobs: uses: oceanbase/setup-oceanbase-ce@v1 with: image_name: oceanbase-ce + container_name: oceanbase-ce mode: ${{ inputs.mode }} sql_port: ${{ inputs.port }} sys_root_password: ${{ inputs.sys_password }} tenant_name: ${{ inputs.test_tenant }} + tenant_root_password: ${{ inputs.test_password }} init_sql: ${{ inputs.init_sql }} + - name: Set server IP + id: set_server_ip + run: | + if [ -z "${{ inputs.server_ip }}" ]; then + echo "server_ip is not set. Getting IP from container..." + container_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' oceanbase-ce) + echo "Container IP is $container_ip. Setting server_ip to container IP." + echo "server_ip=$container_ip" >> $GITHUB_OUTPUT + else + echo "server_ip is already set to ${{ inputs.server_ip }}." + echo "server_ip=${{ inputs.server_ip }}" >> $GITHUB_OUTPUT + fi + - name: Checkout repository uses: actions/checkout@v4 @@ -82,8 +97,8 @@ jobs: test_tenant: ${{ inputs.test_tenant }} test_username: 'root@${{ inputs.test_tenant }}' test_password: ${{ inputs.test_password }} - server_ip: ${{ inputs.server_ip }} - rs_list: ${{ inputs.rs_list }} + 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 diff --git a/.github/workflows/test-oceanbase-ce.yml b/.github/workflows/test-oceanbase-ce.yml index d826b2d..2754ec4 100644 --- a/.github/workflows/test-oceanbase-ce.yml +++ b/.github/workflows/test-oceanbase-ce.yml @@ -58,9 +58,24 @@ jobs: image_file: oceanbase-ce.tar mode: slim port: 1234 - sys_password: '' - test_tenant: test test_password: 123456 server_ip: 127.0.0.1 rs_list: 127.0.0.1:2882:2881 - init_sql: "ALTER USER root IDENTIFIED BY '123456'" + init_sql: "USE test; + CREATE TABLE user(id INT(10) PRIMARY KEY, name VARCHAR(20)); + INSERT INTO user VALUES (1, 'tom'), (2, 'jerry');" + + test-mini: + needs: build + uses: ./.github/workflows/java-test-oceanbase-ce.yml + with: + cache_key: oceanbase-ce + image_file: oceanbase-ce.tar + mode: mini + port: 1234 + sys_password: 1234567 + test_tenant: mini + test_password: 7654321 + init_sql: "USE test; + CREATE TABLE user(id INT(10) PRIMARY KEY, name VARCHAR(20)); + INSERT INTO user VALUES (1, 'tom'), (2, 'jerry');" diff --git a/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java b/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java index 20e0422..1ca1ba8 100644 --- a/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java +++ b/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java @@ -89,6 +89,8 @@ public void testOceanBaseCE( if ("sys".equals(tenantName)) { checkServerIP(conn); + } else { + Assertions.assertEquals(2, Utils.getTableRowsCount(conn, "user")); } } catch (SQLException e) { Assertions.fail(e); diff --git a/test/src/test/java/com/oceanbase/test/Utils.java b/test/src/test/java/com/oceanbase/test/Utils.java index 24607d6..5728b13 100644 --- a/test/src/test/java/com/oceanbase/test/Utils.java +++ b/test/src/test/java/com/oceanbase/test/Utils.java @@ -82,6 +82,14 @@ public static String getRSList(Connection connection) { rs -> rs.next() ? rs.getString("VALUE") : null); } + public static int getTableRowsCount(Connection connection, String tableName) { + return (int) + query( + connection, + "SELECT COUNT(1) FROM " + tableName, + rs -> rs.next() ? rs.getInt(1) : 0); + } + @FunctionalInterface interface ResultSetConsumer { Object apply(ResultSet rs) throws SQLException; From 191bb0f6db6c35e6d44c18eafddab5a3a75c6b35 Mon Sep 17 00:00:00 2001 From: He Wang Date: Mon, 24 Jun 2024 17:15:26 +0800 Subject: [PATCH 3/7] simplify input options of java test workflow --- .github/workflows/java-test-oceanbase-ce.yml | 18 +++++------------- .github/workflows/test-oceanbase-ce.yml | 2 -- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/.github/workflows/java-test-oceanbase-ce.yml b/.github/workflows/java-test-oceanbase-ce.yml index b314433..ea87b07 100644 --- a/.github/workflows/java-test-oceanbase-ce.yml +++ b/.github/workflows/java-test-oceanbase-ce.yml @@ -27,14 +27,6 @@ on: required: false type: string default: '' - server_ip: - required: false - type: string - default: '' - rs_list: - required: false - type: string - default: '' init_sql: required: false type: string @@ -68,14 +60,14 @@ jobs: - name: Set server IP id: set_server_ip run: | - if [ -z "${{ inputs.server_ip }}" ]; then - echo "server_ip is not set. Getting IP from container..." + if [ ${{ inputs.mode == 'slim' }} ]; then + echo "Use '127.0.0.1' as server_ip on slim mode." + echo "server_ip=127.0.0.1" >> $GITHUB_OUTPUT + else + echo "Getting IP from container..." container_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' oceanbase-ce) echo "Container IP is $container_ip. Setting server_ip to container IP." echo "server_ip=$container_ip" >> $GITHUB_OUTPUT - else - echo "server_ip is already set to ${{ inputs.server_ip }}." - echo "server_ip=${{ inputs.server_ip }}" >> $GITHUB_OUTPUT fi - name: Checkout repository diff --git a/.github/workflows/test-oceanbase-ce.yml b/.github/workflows/test-oceanbase-ce.yml index 2754ec4..0b2d1a6 100644 --- a/.github/workflows/test-oceanbase-ce.yml +++ b/.github/workflows/test-oceanbase-ce.yml @@ -59,8 +59,6 @@ jobs: mode: slim port: 1234 test_password: 123456 - server_ip: 127.0.0.1 - rs_list: 127.0.0.1:2882:2881 init_sql: "USE test; CREATE TABLE user(id INT(10) PRIMARY KEY, name VARCHAR(20)); INSERT INTO user VALUES (1, 'tom'), (2, 'jerry');" From 1d24cf56235c843328303c57cc9df1988b43f1c9 Mon Sep 17 00:00:00 2001 From: He Wang Date: Mon, 24 Jun 2024 17:37:00 +0800 Subject: [PATCH 4/7] fix cluster_name option --- .github/workflows/java-test-oceanbase-ce.yml | 7 ++++++- .github/workflows/test-oceanbase-ce.yml | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/java-test-oceanbase-ce.yml b/.github/workflows/java-test-oceanbase-ce.yml index ea87b07..289ddb7 100644 --- a/.github/workflows/java-test-oceanbase-ce.yml +++ b/.github/workflows/java-test-oceanbase-ce.yml @@ -12,6 +12,10 @@ on: mode: required: true type: string + cluster_name: + required: false + type: string + default: 'obcluster' port: required: true type: string @@ -51,6 +55,7 @@ jobs: image_name: oceanbase-ce container_name: oceanbase-ce mode: ${{ inputs.mode }} + cluster_name: ${{ inputs.cluster_name }} sql_port: ${{ inputs.port }} sys_root_password: ${{ inputs.sys_password }} tenant_name: ${{ inputs.test_tenant }} @@ -81,7 +86,7 @@ jobs: - name: Test Docker container env: - cluster_name: 'github-action' + cluster_name: ${{ inputs.cluster_name }} host: '127.0.0.1' port: ${{ inputs.port }} sys_username: 'root' diff --git a/.github/workflows/test-oceanbase-ce.yml b/.github/workflows/test-oceanbase-ce.yml index 0b2d1a6..178235c 100644 --- a/.github/workflows/test-oceanbase-ce.yml +++ b/.github/workflows/test-oceanbase-ce.yml @@ -69,6 +69,7 @@ jobs: with: cache_key: oceanbase-ce image_file: oceanbase-ce.tar + cluster_name: github-action mode: mini port: 1234 sys_password: 1234567 From 55b12bc36412bc1428072d6906ad7bfbc9bad9d3 Mon Sep 17 00:00:00 2001 From: He Wang Date: Mon, 24 Jun 2024 19:08:38 +0800 Subject: [PATCH 5/7] fix mode check --- .github/workflows/java-test-oceanbase-ce.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/java-test-oceanbase-ce.yml b/.github/workflows/java-test-oceanbase-ce.yml index 289ddb7..3d98541 100644 --- a/.github/workflows/java-test-oceanbase-ce.yml +++ b/.github/workflows/java-test-oceanbase-ce.yml @@ -65,7 +65,7 @@ jobs: - name: Set server IP id: set_server_ip run: | - if [ ${{ inputs.mode == 'slim' }} ]; then + if [ "${{ inputs.mode }}" == "slim" ]; then echo "Use '127.0.0.1' as server_ip on slim mode." echo "server_ip=127.0.0.1" >> $GITHUB_OUTPUT else From b6f93cf92d3aa66f1e445677a6ad132f2f0fa879 Mon Sep 17 00:00:00 2001 From: He Wang Date: Mon, 24 Jun 2024 19:42:07 +0800 Subject: [PATCH 6/7] simplify java test env vars --- .github/workflows/java-test-oceanbase-ce.yml | 6 +- .../com/oceanbase/test/OceanBaseCETest.java | 77 ++++++++++--------- .../test/java/com/oceanbase/test/Utils.java | 33 +++++++- 3 files changed, 71 insertions(+), 45 deletions(-) diff --git a/.github/workflows/java-test-oceanbase-ce.yml b/.github/workflows/java-test-oceanbase-ce.yml index 3d98541..fd17b86 100644 --- a/.github/workflows/java-test-oceanbase-ce.yml +++ b/.github/workflows/java-test-oceanbase-ce.yml @@ -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 diff --git a/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java b/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java index 1ca1ba8..1f5481a 100644 --- a/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java +++ b/test/src/test/java/com/oceanbase/test/OceanBaseCETest.java @@ -33,40 +33,60 @@ public class OceanBaseCETest { private static final Logger LOG = LoggerFactory.getLogger(OceanBaseCETest.class); static Stream 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); @@ -78,17 +98,18 @@ 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")); } @@ -96,22 +117,4 @@ public void testOceanBaseCE( 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); - } } diff --git a/test/src/test/java/com/oceanbase/test/Utils.java b/test/src/test/java/com/oceanbase/test/Utils.java index 5728b13..9b767d2 100644 --- a/test/src/test/java/com/oceanbase/test/Utils.java +++ b/test/src/test/java/com/oceanbase/test/Utils.java @@ -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 { @@ -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); } @@ -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); } @@ -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); } @@ -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); } From 6dd12b4af8e67a98141ddf5426a52a1615bf385e Mon Sep 17 00:00:00 2001 From: He Wang Date: Mon, 24 Jun 2024 19:47:42 +0800 Subject: [PATCH 7/7] add mvn install step --- .github/workflows/java-test-oceanbase-ce.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/java-test-oceanbase-ce.yml b/.github/workflows/java-test-oceanbase-ce.yml index fd17b86..ebcc67b 100644 --- a/.github/workflows/java-test-oceanbase-ce.yml +++ b/.github/workflows/java-test-oceanbase-ce.yml @@ -84,6 +84,11 @@ jobs: java-version: '8' distribution: 'zulu' + - name: Build test project + run: | + cd test + mvn install -DskipTests=true + - name: Test Docker container env: server_ip: ${{ steps.set_server_ip.outputs.server_ip }}