From 7fa9b21bc47b115f1449684ac8a852efc839378e Mon Sep 17 00:00:00 2001 From: Aihua Xu Date: Sun, 18 Aug 2024 18:06:12 -0700 Subject: [PATCH] Clean up test db before each test --- .../main/resources/META-INF/persistence.xml | 2 +- .../admin/PolarisOverlappingCatalogTest.java | 6 ++++- .../PolarisServiceImplIntegrationTest.java | 5 +++- .../PolarisRestCatalogIntegrationTest.java | 24 ++--------------- ...PolarisRestCatalogViewIntegrationTest.java | 24 ++--------------- .../catalog/PolarisSparkIntegrationTest.java | 7 ++++- .../test/PolarisConnectionExtension.java | 27 +++++++++++++++++++ 7 files changed, 47 insertions(+), 48 deletions(-) diff --git a/extension/persistence/eclipselink/src/main/resources/META-INF/persistence.xml b/extension/persistence/eclipselink/src/main/resources/META-INF/persistence.xml index 5ab5da98e..a48ed11af 100644 --- a/extension/persistence/eclipselink/src/main/resources/META-INF/persistence.xml +++ b/extension/persistence/eclipselink/src/main/resources/META-INF/persistence.xml @@ -35,7 +35,7 @@ NONE + value="jdbc:h2:file:./build/test_data/iceberg/{realm}/db"/> diff --git a/polaris-service/src/test/java/io/polaris/service/admin/PolarisOverlappingCatalogTest.java b/polaris-service/src/test/java/io/polaris/service/admin/PolarisOverlappingCatalogTest.java index 2955a25bb..eaaeeacfb 100644 --- a/polaris-service/src/test/java/io/polaris/service/admin/PolarisOverlappingCatalogTest.java +++ b/polaris-service/src/test/java/io/polaris/service/admin/PolarisOverlappingCatalogTest.java @@ -36,6 +36,7 @@ import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.client.Invocation; import jakarta.ws.rs.core.Response; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -64,9 +65,12 @@ public class PolarisOverlappingCatalogTest { private static String realm; @BeforeAll - public static void setup(PolarisConnectionExtension.PolarisToken adminToken) { + public static void setup(PolarisConnectionExtension.PolarisToken adminToken) throws IOException { userToken = adminToken.token(); realm = PolarisConnectionExtension.getTestRealm(PolarisOverlappingCatalogTest.class); + + // Set up the database location + PolarisConnectionExtension.createTestDir(realm); } private Response createCatalog(String prefix, String defaultBaseLocation, boolean isExternal) { diff --git a/polaris-service/src/test/java/io/polaris/service/admin/PolarisServiceImplIntegrationTest.java b/polaris-service/src/test/java/io/polaris/service/admin/PolarisServiceImplIntegrationTest.java index 527de7658..3bb52aca4 100644 --- a/polaris-service/src/test/java/io/polaris/service/admin/PolarisServiceImplIntegrationTest.java +++ b/polaris-service/src/test/java/io/polaris/service/admin/PolarisServiceImplIntegrationTest.java @@ -99,9 +99,12 @@ public class PolarisServiceImplIntegrationTest { private static String realm; @BeforeAll - public static void setup(PolarisConnectionExtension.PolarisToken adminToken) { + public static void setup(PolarisConnectionExtension.PolarisToken adminToken) throws IOException { userToken = adminToken.token(); realm = PolarisConnectionExtension.getTestRealm(PolarisServiceImplIntegrationTest.class); + + // Set up test location + PolarisConnectionExtension.createTestDir(realm); } @AfterEach diff --git a/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogIntegrationTest.java b/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogIntegrationTest.java index 39f78fe1e..0f67d5ec4 100644 --- a/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogIntegrationTest.java +++ b/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogIntegrationTest.java @@ -62,9 +62,6 @@ import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Response; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -134,25 +131,8 @@ public class PolarisRestCatalogIntegrationTest extends CatalogTests public static void setup() throws IOException { realm = PolarisConnectionExtension.getTestRealm(PolarisRestCatalogIntegrationTest.class); - Path testDir = Path.of("build/test_data/iceberg/" + realm); - if (Files.exists(testDir)) { - if (Files.isDirectory(testDir)) { - Files.walk(testDir) - .sorted(Comparator.reverseOrder()) - .forEach( - path -> { - try { - Files.delete(path); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - - } else { - Files.delete(testDir); - } - } - Files.createDirectories(testDir); + // Set up test location + PolarisConnectionExtension.createTestDir(realm); } @BeforeEach diff --git a/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogViewIntegrationTest.java b/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogViewIntegrationTest.java index faeea63d7..dcdec6d7c 100644 --- a/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogViewIntegrationTest.java +++ b/polaris-service/src/test/java/io/polaris/service/catalog/PolarisRestCatalogViewIntegrationTest.java @@ -47,9 +47,6 @@ import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Response; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Comparator; import java.util.List; import java.util.Optional; import org.apache.iceberg.CatalogProperties; @@ -96,25 +93,8 @@ public class PolarisRestCatalogViewIntegrationTest extends ViewCatalogTests { - try { - Files.delete(path); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - - } else { - Files.delete(testDir); - } - } - Files.createDirectories(testDir); + // Set up test location + PolarisConnectionExtension.createTestDir(realm); } @BeforeEach diff --git a/polaris-service/src/test/java/io/polaris/service/catalog/PolarisSparkIntegrationTest.java b/polaris-service/src/test/java/io/polaris/service/catalog/PolarisSparkIntegrationTest.java index 6c531ecb6..e3a0ba915 100644 --- a/polaris-service/src/test/java/io/polaris/service/catalog/PolarisSparkIntegrationTest.java +++ b/polaris-service/src/test/java/io/polaris/service/catalog/PolarisSparkIntegrationTest.java @@ -41,6 +41,7 @@ import io.polaris.service.types.TableUpdateNotification; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Response; +import java.io.IOException; import java.time.Instant; import java.util.List; import java.util.Map; @@ -79,10 +80,14 @@ public class PolarisSparkIntegrationTest { private static String realm; @BeforeAll - public static void setup(PolarisConnectionExtension.PolarisToken polarisToken) { + public static void setup(PolarisConnectionExtension.PolarisToken polarisToken) + throws IOException { s3Container.start(); PolarisSparkIntegrationTest.polarisToken = polarisToken; realm = PolarisConnectionExtension.getTestRealm(PolarisSparkIntegrationTest.class); + + // Set up test location + PolarisConnectionExtension.createTestDir(realm); } @AfterAll diff --git a/polaris-service/src/test/java/io/polaris/service/test/PolarisConnectionExtension.java b/polaris-service/src/test/java/io/polaris/service/test/PolarisConnectionExtension.java index d9b0d4d49..4686e4c13 100644 --- a/polaris-service/src/test/java/io/polaris/service/test/PolarisConnectionExtension.java +++ b/polaris-service/src/test/java/io/polaris/service/test/PolarisConnectionExtension.java @@ -42,9 +42,13 @@ import io.polaris.core.storage.PolarisStorageIntegrationProvider; import io.polaris.service.auth.TokenUtils; import io.polaris.service.config.PolarisApplicationConfig; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; +import java.util.Comparator; import java.util.EnumMap; import java.util.Map; import java.util.Optional; @@ -177,6 +181,29 @@ public static String getTestRealm(Class testClassName) { return testClassName.getName().replace('.', '_'); } + public static void createTestDir(String realm) throws IOException { + // Set up the database location + Path testDir = Path.of("build/test_data/iceberg/" + realm); + if (Files.exists(testDir)) { + if (Files.isDirectory(testDir)) { + Files.walk(testDir) + .sorted(Comparator.reverseOrder()) + .forEach( + path -> { + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + + } else { + Files.delete(testDir); + } + } + Files.createDirectories(testDir); + } + static PolarisPrincipalSecrets getAdminSecrets() { return adminSecrets; }