Skip to content

Commit d7450c6

Browse files
committed
add OceanBaseCETest
1 parent 40d8cee commit d7450c6

File tree

4 files changed

+301
-0
lines changed

4 files changed

+301
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ oceanbase-ce/init_store_for_fast_start.py
44
.idea
55
.vscode
66
*.log
7+
target

test/pom.xml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.oceanbase</groupId>
6+
<artifactId>docker-images-test</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<properties>
10+
<encoding>UTF-8</encoding>
11+
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
12+
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
13+
14+
<java.version>1.8</java.version>
15+
<maven.compiler.source>${java.version}</maven.compiler.source>
16+
<maven.compiler.target>${java.version}</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>mysql</groupId>
22+
<artifactId>mysql-connector-java</artifactId>
23+
<version>5.1.47</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.mysql</groupId>
28+
<artifactId>mysql-connector-j</artifactId>
29+
<version>8.4.0</version>
30+
<scope>test</scope>
31+
<exclusions>
32+
<exclusion>
33+
<groupId>com.google.protobuf</groupId>
34+
<artifactId>protobuf-java</artifactId>
35+
</exclusion>
36+
</exclusions>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter</artifactId>
41+
<version>5.10.2</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.apache.logging.log4j</groupId>
46+
<artifactId>log4j-slf4j-impl</artifactId>
47+
<version>2.23.1</version>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>com.diffplug.spotless</groupId>
56+
<artifactId>spotless-maven-plugin</artifactId>
57+
<version>2.27.1</version>
58+
<configuration>
59+
<java>
60+
<googleJavaFormat>
61+
<version>1.7</version>
62+
<style>AOSP</style>
63+
</googleJavaFormat>
64+
<removeUnusedImports />
65+
</java>
66+
<pom>
67+
<sortPom>
68+
<encoding>UTF-8</encoding>
69+
<nrOfIndentSpace>4</nrOfIndentSpace>
70+
<keepBlankLines>true</keepBlankLines>
71+
<indentBlankLines>false</indentBlankLines>
72+
<indentSchemaLocation>false</indentSchemaLocation>
73+
<spaceBeforeCloseEmptyElement>true</spaceBeforeCloseEmptyElement>
74+
<sortModules>false</sortModules>
75+
<sortExecutions>false</sortExecutions>
76+
<expandEmptyElements>false</expandEmptyElements>
77+
<sortProperties>false</sortProperties>
78+
</sortPom>
79+
<replace>
80+
<name>Leading blank line</name>
81+
<search>project</search>
82+
<replacement>project</replacement>
83+
</replace>
84+
</pom>
85+
<upToDateChecking>
86+
<enabled>true</enabled>
87+
</upToDateChecking>
88+
</configuration>
89+
<executions>
90+
<execution>
91+
<id>spotless-check</id>
92+
<goals>
93+
<goal>check</goal>
94+
</goals>
95+
<phase>validate</phase>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
102+
</project>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2024 OceanBase.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.oceanbase.test;
18+
19+
import java.sql.Connection;
20+
import java.sql.Driver;
21+
import java.sql.SQLException;
22+
import java.util.Properties;
23+
import java.util.stream.Stream;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.Arguments;
27+
import org.junit.jupiter.params.provider.MethodSource;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
public class OceanBaseCETest {
32+
33+
private static final Logger LOG = LoggerFactory.getLogger(OceanBaseCETest.class);
34+
35+
static Stream<Arguments> testOceanBaseCEArgs() {
36+
String host = Utils.getNonEmptyEnv("host");
37+
String port = Utils.getNonEmptyEnv("port");
38+
String sysUsername = Utils.getNonEmptyEnv("sys_username");
39+
String sysPassword = System.getenv("sys_password");
40+
String testTenant = Utils.getEnvOrDefault("test_tenant", "test");
41+
String testUsername = Utils.getNonEmptyEnv("test_username");
42+
String testPassword = System.getenv("test_password");
43+
44+
return Stream.of(
45+
Arguments.of(true, host, port, "sys", sysUsername, sysPassword),
46+
Arguments.of(false, host, port, "sys", sysUsername, sysPassword),
47+
Arguments.of(true, host, port, testTenant, testUsername, testPassword),
48+
Arguments.of(false, host, port, testTenant, testUsername, testPassword));
49+
}
50+
51+
@ParameterizedTest
52+
@MethodSource("testOceanBaseCEArgs")
53+
public void testOceanBaseCE(
54+
boolean useLegacyDriver,
55+
String host,
56+
String port,
57+
String tenantName,
58+
String username,
59+
String password) {
60+
61+
String jdbcUrl = String.format("jdbc:mysql://%s:%s/test?useSSL=false", host, port);
62+
63+
Properties props = new Properties();
64+
props.setProperty("user", username);
65+
if (password != null) {
66+
props.put("password", password);
67+
}
68+
69+
Driver driver = Utils.getDriver(useLegacyDriver);
70+
try (Connection conn = driver.connect(jdbcUrl, props)) {
71+
LOG.info("Connected to OceanBase successfully.");
72+
LOG.info("Version comment: {}", Utils.getVersionComment(conn));
73+
74+
Assertions.assertEquals(tenantName, Utils.getTenantName(conn));
75+
checkClusterName(conn);
76+
checkRSList(conn);
77+
78+
if ("sys".equals(tenantName)) {
79+
checkServerIP(conn);
80+
}
81+
} catch (SQLException e) {
82+
Assertions.fail(e);
83+
}
84+
}
85+
86+
private void checkClusterName(Connection conn) {
87+
Assertions.assertEquals(
88+
Utils.getEnvOrDefault("cluster_name", "obcluster"), Utils.getClusterName(conn));
89+
}
90+
91+
private void checkServerIP(Connection conn) {
92+
Assertions.assertEquals(
93+
Utils.getEnvOrDefault("server_ip", "127.0.0.1"), Utils.getServerIP(conn));
94+
}
95+
96+
private void checkRSList(Connection conn) {
97+
Assertions.assertEquals(
98+
Utils.getEnvOrDefault("rs_list", "127.0.0.1:2882:2881"), Utils.getRSList(conn));
99+
}
100+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2024 OceanBase.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.oceanbase.test;
18+
19+
import java.sql.Connection;
20+
import java.sql.Driver;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
import java.sql.Statement;
24+
25+
public class Utils {
26+
27+
public static Driver getDriver(boolean legacy) {
28+
String className = legacy ? "com.mysql.jdbc.Driver" : "com.mysql.cj.jdbc.Driver";
29+
try {
30+
return (Driver) Class.forName(className).getDeclaredConstructor().newInstance();
31+
} catch (Throwable throwable) {
32+
throw new RuntimeException("Failed to load JDBC driver", throwable);
33+
}
34+
}
35+
36+
public static String getNonEmptyEnv(String key) {
37+
String value = System.getenv(key);
38+
if (value == null || value.trim().isEmpty()) {
39+
throw new IllegalArgumentException("Environment variable '" + key + "' is required");
40+
}
41+
return value;
42+
}
43+
44+
public static String getEnvOrDefault(String name, String defaultValue) {
45+
String env = System.getenv(name);
46+
return env == null ? defaultValue : env;
47+
}
48+
49+
public static String getVersionComment(Connection connection) {
50+
return (String)
51+
query(
52+
connection,
53+
"SHOW VARIABLES LIKE 'version_comment'",
54+
rs -> rs.next() ? rs.getString("VALUE") : null);
55+
}
56+
57+
public static String getClusterName(Connection connection) {
58+
return (String)
59+
query(
60+
connection,
61+
"SHOW PARAMETERS LIKE 'cluster'",
62+
rs -> rs.next() ? rs.getString("VALUE") : null);
63+
}
64+
65+
public static String getTenantName(Connection connection) {
66+
return (String) query(connection, "SHOW TENANT", rs -> rs.next() ? rs.getString(1) : null);
67+
}
68+
69+
public static String getServerIP(Connection connection) {
70+
return (String)
71+
query(
72+
connection,
73+
"SELECT svr_ip FROM oceanbase.__all_server",
74+
rs -> rs.next() ? rs.getString(1) : null);
75+
}
76+
77+
public static String getRSList(Connection connection) {
78+
return (String)
79+
query(
80+
connection,
81+
"SHOW PARAMETERS LIKE 'rootservice_list'",
82+
rs -> rs.next() ? rs.getString("VALUE") : null);
83+
}
84+
85+
@FunctionalInterface
86+
interface ResultSetConsumer {
87+
Object apply(ResultSet rs) throws SQLException;
88+
}
89+
90+
static Object query(Connection connection, String sql, ResultSetConsumer resultSetConsumer) {
91+
try (Statement statement = connection.createStatement()) {
92+
ResultSet rs = statement.executeQuery(sql);
93+
return resultSetConsumer.apply(rs);
94+
} catch (SQLException e) {
95+
throw new RuntimeException("Failed to execute sql: " + sql, e);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)