Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[MINOR] fix(spark): Limit integration test cleanup to suite-owned databases (#15159)

Qi Yub295166f6140
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.java
index c9e25e4a2c..535ea93895 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.java
@@ -180,11 +180,11 @@ public abstract class SparkCommonIT extends SparkEnvIT {
@AfterAll
void cleanUp() {
- getDatabases().stream()
- .filter(database -> !database.equals("default"))
+ getDatabases(getCatalogName()).stream()
+ .filter(this::ownsTestDatabase)
.forEach(
database -> {
- sql("USE " + database);
+ sql("USE " + getCatalogName() + "." + database);
listTableNames().forEach(table -> dropTableIfExists(table));
dropDatabaseIfExists(database);
});
@@ -237,7 +237,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
@Test
@EnabledIf("supportsSchemaAndTableProperties")
protected void testCreateAndLoadSchema() {
- String testDatabaseName = "t_create1";
+ String testDatabaseName = suiteDatabaseName("t_create1");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
@@ -247,7 +247,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
String properties = databaseMeta.get("Properties");
Assertions.assertTrue(properties.contains("(ID,001)"));
- testDatabaseName = "t_create2";
+ testDatabaseName = suiteDatabaseName("t_create2");
dropDatabaseIfExists(testDatabaseName);
String testDatabaseLocation = "/tmp/" + testDatabaseName;
sql(
@@ -267,7 +267,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
@Test
@EnabledIf("supportsSchemaAndTableProperties")
protected void testAlterSchema() {
- String testDatabaseName = "t_alter";
+ String testDatabaseName = suiteDatabaseName("t_alter");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Assertions.assertTrue(
@@ -289,7 +289,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
@Test
protected void testDropSchema() {
- String testDatabaseName = "t_drop";
+ String testDatabaseName = suiteDatabaseName("t_drop");
dropDatabaseIfExists(testDatabaseName);
Set<String> databases = getDatabases();
Assertions.assertFalse(databases.contains(testDatabaseName));
@@ -326,7 +326,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
@Test
void testCreateTableWithDatabase() {
// test db.table as table identifier
- String databaseName = "db1";
+ String databaseName = suiteDatabaseName("db1");
String tableName = "table1";
createDatabaseIfNotExists(databaseName, getProvider());
String tableIdentifier = String.join(".", databaseName, tableName);
@@ -340,7 +340,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
checkTableReadWrite(tableInfo);
// use db then create table with table name
- databaseName = "db2";
+ databaseName = suiteDatabaseName("db2");
tableName = "table2";
createDatabaseIfNotExists(databaseName, getProvider());
@@ -427,7 +427,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
Assertions.assertTrue(tables.contains(table2));
// show tables from not current db
- String database = "db_list";
+ String database = suiteDatabaseName("db_list");
String table3 = "list3";
String table4 = "list4";
createDatabaseIfNotExists(database, getProvider());
@@ -1038,7 +1038,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {
}
protected String getDefaultDatabase() {
- return "default_db";
+ return suiteDatabaseName("default_db");
}
// Helper method to create a simple table, and could use corresponding
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkEnvIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkEnvIT.java
index e5191a7d2b..16e7c3cbe5 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkEnvIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkEnvIT.java
@@ -25,7 +25,10 @@ import static org.apache.gravitino.spark.connector.iceberg.IcebergPropertiesCons
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants;
@@ -66,7 +69,9 @@ public abstract class SparkEnvIT extends SparkUtilIT {
protected String warehouse;
protected FileSystem hdfs;
protected String icebergRestServiceUri;
- protected final String functionSchemaName = "test_function_schema";
+ private final String databaseSuffix = UUID.randomUUID().toString().replace("-", "");
+ private final Set<String> testDatabases = new HashSet<>();
+ protected final String functionSchemaName = suiteDatabaseName("test_function_schema");
protected final String functionName = "my_string_length";
protected final String nonSparkFunctionName = "trino_upper";
@@ -75,6 +80,22 @@ public abstract class SparkEnvIT extends SparkUtilIT {
private String gravitinoUri = "http://127.0.0.1:8090";
private final String lakeHouseIcebergProvider = "lakehouse-iceberg";
+ /**
+ * Names a database for this suite instance and registers it for teardown.
+ *
+ * @param name the fixture's local database name
+ * @return a database name unique to this suite run
+ */
+ protected final String suiteDatabaseName(String name) {
+ String database = name + "_" + databaseSuffix;
+ testDatabases.add(database);
+ return database;
+ }
+
+ boolean ownsTestDatabase(String database) {
+ return testDatabases.contains(database);
+ }
+
protected abstract String getCatalogName();
protected abstract String getProvider();
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/TestSparkCommonITCleanup.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/TestSparkCommonITCleanup.java
new file mode 100644
index 0000000000..9a2a985d12
--- /dev/null
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/TestSparkCommonITCleanup.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.gravitino.spark.connector.integration.test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.withSettings;
+
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+/** Checks the SQL issued by Spark suite teardown against a shared catalog. */
+class TestSparkCommonITCleanup {
+
+ // Expose the SQL boundary so the test needs neither a Spark session nor a database server.
+ abstract static class Fixture extends SparkCommonIT {
+ @Override
+ protected abstract List<Object[]> sql(String query);
+ }
+
+ @Test
+ void dropsOnlyTheCurrentSuitesDatabase() {
+ Fixture suite =
+ mock(Fixture.class, withSettings().useConstructor().defaultAnswer(CALLS_REAL_METHODS));
+ Fixture other =
+ mock(Fixture.class, withSettings().useConstructor().defaultAnswer(CALLS_REAL_METHODS));
+ String owned = suite.getDefaultDatabase();
+ doReturn("jdbc_mysql").when(suite).getCatalogName();
+ doReturn(
+ List.of(
+ new Object[] {"MYSQL_JDBC_BACKEND"},
+ new Object[] {"default"},
+ new Object[] {"default_db"},
+ new Object[] {other.getDefaultDatabase()},
+ new Object[] {owned}))
+ .when(suite)
+ .sql("SHOW DATABASES IN jdbc_mysql");
+ doReturn(List.<Object[]>of(new Object[] {owned, "fixture_table"}))
+ .when(suite)
+ .sql("SHOW TABLES");
+
+ suite.cleanUp();
+
+ ArgumentCaptor<String> statements = ArgumentCaptor.forClass(String.class);
+ verify(suite, times(5)).sql(statements.capture());
+ assertEquals(
+ List.of(
+ "SHOW DATABASES IN jdbc_mysql",
+ "USE jdbc_mysql." + owned,
+ "SHOW TABLES",
+ "DROP TABLE IF EXISTS fixture_table",
+ "DROP DATABASE IF EXISTS " + owned),
+ statements.getAllValues());
+ }
+}
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/glue/SparkGlueCatalogIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/glue/SparkGlueCatalogIT.java
index c981ab646b..aebdfd16a3 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/glue/SparkGlueCatalogIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/glue/SparkGlueCatalogIT.java
@@ -512,7 +512,7 @@ public abstract class SparkGlueCatalogIT extends SparkGlueEnvIT {
@Test
@Override
protected void testCreateAndLoadSchema() {
- String testDatabaseName = "t_create1";
+ String testDatabaseName = suiteDatabaseName("t_create1");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
@@ -520,7 +520,7 @@ public abstract class SparkGlueCatalogIT extends SparkGlueEnvIT {
String properties = databaseMeta.get("Properties");
Assertions.assertTrue(properties.contains("(ID,001)"));
- testDatabaseName = "t_create2";
+ testDatabaseName = suiteDatabaseName("t_create2");
dropDatabaseIfExists(testDatabaseName);
String testDatabaseLocation = warehouse + "/" + testDatabaseName;
sql(
@@ -558,7 +558,7 @@ public abstract class SparkGlueCatalogIT extends SparkGlueEnvIT {
@Test
@Override
protected void testAlterSchema() {
- String testDatabaseName = "t_alter";
+ String testDatabaseName = suiteDatabaseName("t_alter");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Assertions.assertTrue(
@@ -582,7 +582,7 @@ public abstract class SparkGlueCatalogIT extends SparkGlueEnvIT {
@Test
@Override
protected void testDropSchema() {
- String testDatabaseName = "t_drop";
+ String testDatabaseName = suiteDatabaseName("t_drop");
dropDatabaseIfExists(testDatabaseName);
Set<String> databases = getDatabases();
Assertions.assertFalse(databases.contains(testDatabaseName));
@@ -614,7 +614,7 @@ public abstract class SparkGlueCatalogIT extends SparkGlueEnvIT {
Assertions.assertTrue(tables.contains(table1));
Assertions.assertTrue(tables.contains(table2));
- String database = "db_list";
+ String database = suiteDatabaseName("db_list");
String table3 = "list3";
String table4 = "list4";
createDatabaseIfNotExists(database, getProvider());
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java
index b2e58eeca7..635f9d7413 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java
@@ -551,7 +551,7 @@ public abstract class SparkHiveCatalogIT extends SparkCommonIT {
@Test
void testCreateTableWithTimestamp() {
- String databaseName = "test_db_with_timestamp";
+ String databaseName = suiteDatabaseName("test_db_with_timestamp");
String tableName = "test_table_with_timestamp";
createDatabaseIfNotExists(databaseName, getProvider());
sql(String.format("USE %s", databaseName));
@@ -654,7 +654,7 @@ public abstract class SparkHiveCatalogIT extends SparkCommonIT {
void testSelectViewWithUnqualifiedSqlResolvesInViewNamespace() {
String tableName = "view_base_table_unq";
String schemaName = getDefaultDatabase();
- String otherDb = "db_view_unq_other";
+ String otherDb = suiteDatabaseName("db_view_unq_other");
String ownView = "test_hive_view_unq_own";
String recordedView = "test_hive_view_unq_recorded";
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogFilesystemBackendIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogFilesystemBackendIT.java
index 3d4a3257a9..1d6c4b3596 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogFilesystemBackendIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogFilesystemBackendIT.java
@@ -42,7 +42,7 @@ public abstract class SparkPaimonCatalogFilesystemBackendIT extends SparkPaimonC
@Test
@Override
protected void testCreateAndLoadSchema() {
- String testDatabaseName = "t_create1";
+ String testDatabaseName = suiteDatabaseName("t_create1");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
@@ -53,7 +53,7 @@ public abstract class SparkPaimonCatalogFilesystemBackendIT extends SparkPaimonC
@Test
@Override
protected void testAlterSchema() {
- String testDatabaseName = "t_alter";
+ String testDatabaseName = suiteDatabaseName("t_alter");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT.java
index 50a6bfa029..613ee856c4 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT.java
@@ -64,14 +64,14 @@ public abstract class SparkPaimonCatalogHiveBackendIT extends SparkPaimonCatalog
protected void testCreateAndLoadSchema() {
// Same as the common test, except that the schema comment is not checked: Gravitino's Paimon
// catalog does not store the comment of a new schema yet (#5719).
- String testDatabaseName = "t_create1";
+ String testDatabaseName = suiteDatabaseName("t_create1");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
Assertions.assertTrue(databaseMeta.containsKey("Location"));
Assertions.assertTrue(databaseMeta.get("Properties").contains("(ID,001)"));
- testDatabaseName = "t_create2";
+ testDatabaseName = suiteDatabaseName("t_create2");
dropDatabaseIfExists(testDatabaseName);
String testDatabaseLocation = "/tmp/" + testDatabaseName;
sql(
@@ -86,7 +86,7 @@ public abstract class SparkPaimonCatalogHiveBackendIT extends SparkPaimonCatalog
@Test
@Override
protected void testAlterSchema() {
- String testDatabaseName = "t_alter";
+ String testDatabaseName = suiteDatabaseName("t_alter");
dropDatabaseIfExists(testDatabaseName);
sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES (ID=001);");
Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/util/SparkUtilIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/util/SparkUtilIT.java
index 809d900fee..00af2207f7 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/util/SparkUtilIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/util/SparkUtilIT.java
@@ -60,6 +60,16 @@ public abstract class SparkUtilIT extends BaseIT {
return convertToStringSet(sql("SHOW DATABASES"), 0);
}
+ /**
+ * Lists databases in the specified catalog regardless of the current Spark namespace.
+ *
+ * @param catalog the Spark catalog name
+ * @return the database names visible in the catalog
+ */
+ protected Set<String> getDatabases(String catalog) {
+ return convertToStringSet(sql("SHOW DATABASES IN " + catalog), 0);
+ }
+
protected Set<String> listTableNames() {
// the first column is namespace, the second column is table name
return convertToStringSet(sql("SHOW TABLES"), 1);