16
16
17
17
package io .github .linghengqian .hive .server2 .jdbc .driver .thin ;
18
18
19
+ import org .junit .jupiter .api .AfterAll ;
19
20
import org .junit .jupiter .api .Test ;
20
21
import org .testcontainers .containers .Container .ExecResult ;
21
22
import org .testcontainers .containers .GenericContainer ;
23
+ import org .testcontainers .containers .Network ;
24
+ import org .testcontainers .images .builder .ImageFromDockerfile ;
22
25
import org .testcontainers .junit .jupiter .Container ;
23
26
import org .testcontainers .junit .jupiter .Testcontainers ;
24
27
25
28
import java .io .IOException ;
29
+ import java .nio .file .Paths ;
26
30
import java .sql .*;
27
31
import java .time .Duration ;
28
32
import java .time .temporal .ChronoUnit ;
32
36
import static org .hamcrest .Matchers .is ;
33
37
import static org .junit .jupiter .api .Assertions .assertThrows ;
34
38
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
- */
39
39
@ SuppressWarnings ({"SqlNoDataSourceInspection" , "resource" })
40
40
@ Testcontainers
41
41
public class InformationSchemaTest {
42
+
43
+ private static final Network NETWORK = Network .newNetwork ();
44
+
42
45
@ 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
+ ))
44
57
.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 )
45
65
.withExposedPorts (10000 );
46
66
67
+
68
+ @ AfterAll
69
+ static void afterAll () {
70
+ NETWORK .close ();
71
+ }
72
+
47
73
@ Test
48
74
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 );
50
76
await ().atMost (Duration .of (30L , ChronoUnit .SECONDS )).ignoreExceptions ().until (() -> {
51
77
DriverManager .getConnection (jdbcUrlPrefix ).close ();
52
78
return true ;
@@ -69,23 +95,14 @@ void test() throws SQLException, IOException, InterruptedException {
69
95
statement .executeUpdate ("INSERT INTO t_order (order_id, user_id, order_type, address_id, status) VALUES (1, 1, 1, 1, 'INSERT_TEST')" );
70
96
ResultSet resultSet = statement .executeQuery ("select * from t_order" );
71
97
assertThat (resultSet .next (), is (true ));
98
+ assertThat (resultSet .next (), is (false ));
72
99
}
73
100
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 (
85
102
"/opt/hive/bin/schematool" ,
86
103
"-initSchema" ,
87
104
"-dbType" , "hive" ,
88
- "-metaDbType" , "derby " ,
105
+ "-metaDbType" , "postgres " ,
89
106
"-url" , "jdbc:hive2://localhost:10000/default"
90
107
);
91
108
assertThat (initResult .getStdout (), is ("Initializing the schema to: 4.0.0\n " +
@@ -97,7 +114,17 @@ void test() throws SQLException, IOException, InterruptedException {
97
114
"Initialization script completed\n " ));
98
115
try (Connection connection = DriverManager .getConnection (jdbcUrlPrefix + "/information_schema" );
99
116
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" );
101
128
assertThat (resultSet .next (), is (true ));
102
129
}
103
130
}
0 commit comments