Skip to content

Commit 429b7b3

Browse files
authored
Add unit tests for information_schema database with custom tables (#23)
* Add unit tests for `information_schema` database with custom tables * Remove use of HMS to speed up unit test execution
1 parent 280d72e commit 429b7b3

File tree

4 files changed

+130
-38
lines changed

4 files changed

+130
-38
lines changed

hive-server2-jdbc-driver-thin/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/thin/InformationSchemaTest.java

+46-19
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package io.github.linghengqian.hive.server2.jdbc.driver.thin;
1818

19+
import org.junit.jupiter.api.AfterAll;
1920
import org.junit.jupiter.api.Test;
2021
import org.testcontainers.containers.Container.ExecResult;
2122
import org.testcontainers.containers.GenericContainer;
23+
import org.testcontainers.containers.Network;
24+
import org.testcontainers.images.builder.ImageFromDockerfile;
2225
import org.testcontainers.junit.jupiter.Container;
2326
import org.testcontainers.junit.jupiter.Testcontainers;
2427

2528
import java.io.IOException;
29+
import java.nio.file.Paths;
2630
import java.sql.*;
2731
import java.time.Duration;
2832
import java.time.temporal.ChronoUnit;
@@ -32,21 +36,43 @@
3236
import static org.hamcrest.Matchers.is;
3337
import static org.junit.jupiter.api.Assertions.assertThrows;
3438

35-
/**
36-
* TODO This unit test is affected by <a href="https://github.com/apache/hive/pull/5629">apache/hive#5629</a>,
37-
* the `information_schema` database does not exist by default.
38-
*/
3939
@SuppressWarnings({"SqlNoDataSourceInspection", "resource"})
4040
@Testcontainers
4141
public class InformationSchemaTest {
42+
43+
private static final Network NETWORK = Network.newNetwork();
44+
4245
@Container
43-
public static final GenericContainer<?> CONTAINER = new GenericContainer<>("apache/hive:4.0.1")
46+
public static final GenericContainer<?> POSTGRES = new GenericContainer<>("postgres:17.2-bookworm")
47+
.withEnv("POSTGRES_PASSWORD", "example")
48+
.withNetwork(NETWORK)
49+
.withNetworkAliases("some-postgres");
50+
51+
@Container
52+
public static final GenericContainer<?> HS2 = new GenericContainer<>(
53+
new ImageFromDockerfile().withFileFromPath(
54+
"Dockerfile",
55+
Paths.get("src/test/resources/information-schema/Dockerfile").toAbsolutePath()
56+
))
4457
.withEnv("SERVICE_NAME", "hiveserver2")
58+
.withEnv("DB_DRIVER", "postgres")
59+
.withEnv("SERVICE_OPTS", "-Djavax.jdo.option.ConnectionDriverName=org.postgresql.Driver" + " " +
60+
"-Djavax.jdo.option.ConnectionURL=jdbc:postgresql://some-postgres:5432/postgres" + " " +
61+
"-Djavax.jdo.option.ConnectionUserName=postgres" + " " +
62+
"-Djavax.jdo.option.ConnectionPassword=example")
63+
.withNetwork(NETWORK)
64+
.dependsOn(POSTGRES)
4565
.withExposedPorts(10000);
4666

67+
68+
@AfterAll
69+
static void afterAll() {
70+
NETWORK.close();
71+
}
72+
4773
@Test
4874
void test() throws SQLException, IOException, InterruptedException {
49-
String jdbcUrlPrefix = "jdbc:hive2://" + CONTAINER.getHost() + ":" + CONTAINER.getMappedPort(10000);
75+
String jdbcUrlPrefix = "jdbc:hive2://" + HS2.getHost() + ":" + HS2.getMappedPort(10000);
5076
await().atMost(Duration.of(30L, ChronoUnit.SECONDS)).ignoreExceptions().until(() -> {
5177
DriverManager.getConnection(jdbcUrlPrefix).close();
5278
return true;
@@ -69,23 +95,14 @@ void test() throws SQLException, IOException, InterruptedException {
6995
statement.executeUpdate("INSERT INTO t_order (order_id, user_id, order_type, address_id, status) VALUES (1, 1, 1, 1, 'INSERT_TEST')");
7096
ResultSet resultSet = statement.executeQuery("select * from t_order");
7197
assertThat(resultSet.next(), is(true));
98+
assertThat(resultSet.next(), is(false));
7299
}
73100
assertThrows(SQLException.class, () -> DriverManager.getConnection(jdbcUrlPrefix + "/information_schema").close());
74-
ExecResult infoResult = CONTAINER.execInContainer(
75-
"/opt/hive/bin/schematool",
76-
"-info",
77-
"-dbType", "hive",
78-
"-metaDbType", "derby",
79-
"-url", "jdbc:hive2://localhost:10000/default"
80-
);
81-
assertThat(infoResult.getStdout(), is("Metastore connection URL:\t jdbc:hive2://localhost:10000/default\n" +
82-
"Metastore connection Driver :\t org.apache.hive.jdbc.HiveDriver\n" +
83-
"Metastore connection User:\t APP\n"));
84-
ExecResult initResult = CONTAINER.execInContainer(
101+
ExecResult initResult = HS2.execInContainer(
85102
"/opt/hive/bin/schematool",
86103
"-initSchema",
87104
"-dbType", "hive",
88-
"-metaDbType", "derby",
105+
"-metaDbType", "postgres",
89106
"-url", "jdbc:hive2://localhost:10000/default"
90107
);
91108
assertThat(initResult.getStdout(), is("Initializing the schema to: 4.0.0\n" +
@@ -97,7 +114,17 @@ void test() throws SQLException, IOException, InterruptedException {
97114
"Initialization script completed\n"));
98115
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/information_schema");
99116
Statement statement = connection.createStatement()) {
100-
ResultSet resultSet = statement.executeQuery("select * from information_schema.COLUMNS limit 100");
117+
ResultSet resultSet = statement.executeQuery("select TABLE_CATALOG,\n" +
118+
" TABLE_NAME,\n" +
119+
" COLUMN_NAME,\n" +
120+
" DATA_TYPE,\n" +
121+
" ORDINAL_POSITION,\n" +
122+
" IS_NULLABLE\n" +
123+
"FROM INFORMATION_SCHEMA.COLUMNS\n" +
124+
"WHERE TABLE_CATALOG = 'default'\n" +
125+
" AND TABLE_SCHEMA = 'demo_ds_0'\n" +
126+
" AND UPPER(TABLE_NAME) IN ('T_ORDER')\n" +
127+
"ORDER BY ORDINAL_POSITION limit 100");
101128
assertThat(resultSet.next(), is(true));
102129
}
103130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2025 Qiheng He
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM alpine:3.21.2 AS prepare
16+
RUN apk add --no-cache wget
17+
RUN wget https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.5/postgresql-42.7.5.jar --directory-prefix=/opt/hive/lib
18+
FROM apache/hive:4.0.1
19+
COPY --from=prepare /opt/hive/lib /opt/hive/lib

hive-server2-jdbc-driver-uber/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/uber/InformationSchemaTest.java

+46-19
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package io.github.linghengqian.hive.server2.jdbc.driver.uber;
1818

19+
import org.junit.jupiter.api.AfterAll;
1920
import org.junit.jupiter.api.Test;
2021
import org.testcontainers.containers.Container.ExecResult;
2122
import org.testcontainers.containers.GenericContainer;
23+
import org.testcontainers.containers.Network;
24+
import org.testcontainers.images.builder.ImageFromDockerfile;
2225
import org.testcontainers.junit.jupiter.Container;
2326
import org.testcontainers.junit.jupiter.Testcontainers;
2427

2528
import java.io.IOException;
29+
import java.nio.file.Paths;
2630
import java.sql.*;
2731
import java.time.Duration;
2832
import java.time.temporal.ChronoUnit;
@@ -32,21 +36,43 @@
3236
import static org.hamcrest.Matchers.is;
3337
import static org.junit.jupiter.api.Assertions.assertThrows;
3438

35-
/**
36-
* TODO This unit test is affected by <a href="https://github.com/apache/hive/pull/5629">apache/hive#5629</a>,
37-
* the `information_schema` database does not exist by default.
38-
*/
3939
@SuppressWarnings({"SqlNoDataSourceInspection", "resource"})
4040
@Testcontainers
4141
public class InformationSchemaTest {
42+
43+
private static final Network NETWORK = Network.newNetwork();
44+
4245
@Container
43-
public static final GenericContainer<?> CONTAINER = new GenericContainer<>("apache/hive:4.0.1")
46+
public static final GenericContainer<?> POSTGRES = new GenericContainer<>("postgres:17.2-bookworm")
47+
.withEnv("POSTGRES_PASSWORD", "example")
48+
.withNetwork(NETWORK)
49+
.withNetworkAliases("some-postgres");
50+
51+
@Container
52+
public static final GenericContainer<?> HS2 = new GenericContainer<>(
53+
new ImageFromDockerfile().withFileFromPath(
54+
"Dockerfile",
55+
Paths.get("src/test/resources/information-schema/Dockerfile").toAbsolutePath()
56+
))
4457
.withEnv("SERVICE_NAME", "hiveserver2")
58+
.withEnv("DB_DRIVER", "postgres")
59+
.withEnv("SERVICE_OPTS", "-Djavax.jdo.option.ConnectionDriverName=org.postgresql.Driver" + " " +
60+
"-Djavax.jdo.option.ConnectionURL=jdbc:postgresql://some-postgres:5432/postgres" + " " +
61+
"-Djavax.jdo.option.ConnectionUserName=postgres" + " " +
62+
"-Djavax.jdo.option.ConnectionPassword=example")
63+
.withNetwork(NETWORK)
64+
.dependsOn(POSTGRES)
4565
.withExposedPorts(10000);
4666

67+
68+
@AfterAll
69+
static void afterAll() {
70+
NETWORK.close();
71+
}
72+
4773
@Test
4874
void test() throws SQLException, IOException, InterruptedException {
49-
String jdbcUrlPrefix = "jdbc:hive2://" + CONTAINER.getHost() + ":" + CONTAINER.getMappedPort(10000);
75+
String jdbcUrlPrefix = "jdbc:hive2://" + HS2.getHost() + ":" + HS2.getMappedPort(10000);
5076
await().atMost(Duration.of(30L, ChronoUnit.SECONDS)).ignoreExceptions().until(() -> {
5177
DriverManager.getConnection(jdbcUrlPrefix).close();
5278
return true;
@@ -69,23 +95,14 @@ void test() throws SQLException, IOException, InterruptedException {
6995
statement.executeUpdate("INSERT INTO t_order (order_id, user_id, order_type, address_id, status) VALUES (1, 1, 1, 1, 'INSERT_TEST')");
7096
ResultSet resultSet = statement.executeQuery("select * from t_order");
7197
assertThat(resultSet.next(), is(true));
98+
assertThat(resultSet.next(), is(false));
7299
}
73100
assertThrows(SQLException.class, () -> DriverManager.getConnection(jdbcUrlPrefix + "/information_schema").close());
74-
ExecResult infoResult = CONTAINER.execInContainer(
75-
"/opt/hive/bin/schematool",
76-
"-info",
77-
"-dbType", "hive",
78-
"-metaDbType", "derby",
79-
"-url", "jdbc:hive2://localhost:10000/default"
80-
);
81-
assertThat(infoResult.getStdout(), is("Metastore connection URL:\t jdbc:hive2://localhost:10000/default\n" +
82-
"Metastore connection Driver :\t org.apache.hive.jdbc.HiveDriver\n" +
83-
"Metastore connection User:\t APP\n"));
84-
ExecResult initResult = CONTAINER.execInContainer(
101+
ExecResult initResult = HS2.execInContainer(
85102
"/opt/hive/bin/schematool",
86103
"-initSchema",
87104
"-dbType", "hive",
88-
"-metaDbType", "derby",
105+
"-metaDbType", "postgres",
89106
"-url", "jdbc:hive2://localhost:10000/default"
90107
);
91108
assertThat(initResult.getStdout(), is("Initializing the schema to: 4.0.0\n" +
@@ -97,7 +114,17 @@ void test() throws SQLException, IOException, InterruptedException {
97114
"Initialization script completed\n"));
98115
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/information_schema");
99116
Statement statement = connection.createStatement()) {
100-
ResultSet resultSet = statement.executeQuery("select * from information_schema.COLUMNS limit 100");
117+
ResultSet resultSet = statement.executeQuery("select TABLE_CATALOG,\n" +
118+
" TABLE_NAME,\n" +
119+
" COLUMN_NAME,\n" +
120+
" DATA_TYPE,\n" +
121+
" ORDINAL_POSITION,\n" +
122+
" IS_NULLABLE\n" +
123+
"FROM INFORMATION_SCHEMA.COLUMNS\n" +
124+
"WHERE TABLE_CATALOG = 'default'\n" +
125+
" AND TABLE_SCHEMA = 'demo_ds_0'\n" +
126+
" AND UPPER(TABLE_NAME) IN ('T_ORDER')\n" +
127+
"ORDER BY ORDINAL_POSITION limit 100");
101128
assertThat(resultSet.next(), is(true));
102129
}
103130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2025 Qiheng He
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM alpine:3.21.2 AS prepare
16+
RUN apk add --no-cache wget
17+
RUN wget https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.5/postgresql-42.7.5.jar --directory-prefix=/opt/hive/lib
18+
FROM apache/hive:4.0.1
19+
COPY --from=prepare /opt/hive/lib /opt/hive/lib

0 commit comments

Comments
 (0)