From 8e3b7b4efb26467ab9323b2baaa8083def2a01cc Mon Sep 17 00:00:00 2001 From: Weimin Yu Date: Mon, 18 Nov 2019 11:33:26 -0500 Subject: [PATCH] Require explict tag when starting psql docker (#368) * Require explict tag when starting psql docker Defined a util class to return docker tag of desired PSQL version. Class is defined in ':db' and shared by ':db' and ':core'. Used an artifact declaration to exclude unnecesary compile dependencies. Added a presubmit check for instantiations without explicit tag. --- config/presubmits.py | 6 ++++ core/build.gradle | 2 ++ .../tools/GenerateSqlSchemaCommand.java | 3 +- .../JpaTransactionManagerRule.java | 5 +++- .../HibernateSchemaExporterTest.java | 5 +++- .../persistence/PersistenceModuleTest.java | 3 +- .../tools/GenerateSqlSchemaCommandTest.java | 6 ++-- db/build.gradle | 15 ++++++++++ .../persistence/NomulusPostgreSql.java | 28 +++++++++++++++++++ .../registry/sql/flyway/SchemaTest.java | 3 +- 10 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 db/src/main/java/google/registry/persistence/NomulusPostgreSql.java diff --git a/config/presubmits.py b/config/presubmits.py index 1efffc953..4559ea792 100644 --- a/config/presubmits.py +++ b/config/presubmits.py @@ -99,6 +99,12 @@ PRESUBMITS = { "System.(out|err).println is only allowed in tools/ packages. Please " "use a logger instead.", + # PostgreSQLContainer instantiation must specify docker tag + PresubmitCheck( + r"[\s\S]*new\s+PostgreSQLContainer(<[\s\S]*>)?\(\s*\)[\s\S]*", + "java", {}): + "PostgreSQLContainer instantiation must specify docker tag.", + # Various Soy linting checks PresubmitCheck( r".* (/\*)?\* {?@param ", diff --git a/core/build.gradle b/core/build.gradle index 302bd15d4..44fd5002f 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -243,6 +243,8 @@ dependencies { // Known issue: nebula-lint misses inherited dependency. compile project(':third_party') compile project(':util') + // Import NomulusPostreSql from ':db' for compile but exclude dependencies. + compile project(path: ':db', configuration: 'compileApi') testRuntime project(':db') // Include auto-value in compile until nebula-lint understands diff --git a/core/src/main/java/google/registry/tools/GenerateSqlSchemaCommand.java b/core/src/main/java/google/registry/tools/GenerateSqlSchemaCommand.java index 04a9beebc..e65ca2cc2 100644 --- a/core/src/main/java/google/registry/tools/GenerateSqlSchemaCommand.java +++ b/core/src/main/java/google/registry/tools/GenerateSqlSchemaCommand.java @@ -20,6 +20,7 @@ import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.common.annotations.VisibleForTesting; import google.registry.persistence.HibernateSchemaExporter; +import google.registry.persistence.NomulusPostgreSql; import google.registry.persistence.PersistenceXmlUtility; import java.io.File; import java.io.IOException; @@ -83,7 +84,7 @@ public class GenerateSqlSchemaCommand implements Command { // Start the container and store the address information. postgresContainer = - new PostgreSQLContainer() + new PostgreSQLContainer(NomulusPostgreSql.getDockerTag()) .withDatabaseName(DB_NAME) .withUsername(DB_USERNAME) .withPassword(DB_PASSWORD); diff --git a/core/src/test/java/google/registry/model/transaction/JpaTransactionManagerRule.java b/core/src/test/java/google/registry/model/transaction/JpaTransactionManagerRule.java index 72691ee04..fc72930d4 100644 --- a/core/src/test/java/google/registry/model/transaction/JpaTransactionManagerRule.java +++ b/core/src/test/java/google/registry/model/transaction/JpaTransactionManagerRule.java @@ -25,6 +25,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.google.common.io.Resources; import google.registry.persistence.HibernateSchemaExporter; +import google.registry.persistence.NomulusPostgreSql; import google.registry.persistence.PersistenceModule; import google.registry.persistence.PersistenceXmlUtility; import google.registry.testing.FakeClock; @@ -90,7 +91,9 @@ public class JpaTransactionManagerRule extends ExternalResource { } private static JdbcDatabaseContainer create() { - PostgreSQLContainer container = new PostgreSQLContainer().withDatabaseName(MANAGEMENT_DB_NAME); + PostgreSQLContainer container = + new PostgreSQLContainer(NomulusPostgreSql.getDockerTag()) + .withDatabaseName(MANAGEMENT_DB_NAME); container.start(); Runtime.getRuntime().addShutdownHook(new Thread(() -> container.close())); return container; diff --git a/core/src/test/java/google/registry/persistence/HibernateSchemaExporterTest.java b/core/src/test/java/google/registry/persistence/HibernateSchemaExporterTest.java index 93ce1c645..5893a14d4 100644 --- a/core/src/test/java/google/registry/persistence/HibernateSchemaExporterTest.java +++ b/core/src/test/java/google/registry/persistence/HibernateSchemaExporterTest.java @@ -36,7 +36,10 @@ import org.testcontainers.containers.PostgreSQLContainer; /** Unit tests for {@link HibernateSchemaExporter}. */ @RunWith(JUnit4.class) public class HibernateSchemaExporterTest { - @ClassRule public static final PostgreSQLContainer database = new PostgreSQLContainer(); + @ClassRule + public static final PostgreSQLContainer database = + new PostgreSQLContainer(NomulusPostgreSql.getDockerTag()); + private static HibernateSchemaExporter exporter; @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); diff --git a/core/src/test/java/google/registry/persistence/PersistenceModuleTest.java b/core/src/test/java/google/registry/persistence/PersistenceModuleTest.java index cf2f310ce..2ae98a5fb 100644 --- a/core/src/test/java/google/registry/persistence/PersistenceModuleTest.java +++ b/core/src/test/java/google/registry/persistence/PersistenceModuleTest.java @@ -29,7 +29,8 @@ import org.testcontainers.containers.PostgreSQLContainer; /** Unit tests for {@link PersistenceModule}. */ @RunWith(JUnit4.class) public class PersistenceModuleTest { - @Rule public PostgreSQLContainer database = new PostgreSQLContainer(); + @Rule + public PostgreSQLContainer database = new PostgreSQLContainer(NomulusPostgreSql.getDockerTag()); private EntityManagerFactory emf; diff --git a/core/src/test/java/google/registry/tools/GenerateSqlSchemaCommandTest.java b/core/src/test/java/google/registry/tools/GenerateSqlSchemaCommandTest.java index b9f63fc02..157a951eb 100644 --- a/core/src/test/java/google/registry/tools/GenerateSqlSchemaCommandTest.java +++ b/core/src/test/java/google/registry/tools/GenerateSqlSchemaCommandTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.io.Files; +import google.registry.persistence.NomulusPostgreSql; import java.io.File; import org.junit.Before; import org.junit.ClassRule; @@ -37,8 +38,9 @@ public class GenerateSqlSchemaCommandTest extends CommandTestCase("postgres:9.6.12") + new PostgreSQLContainer<>(NomulusPostgreSql.getDockerTag()) .withClasspathResourceMapping( MOUNTED_RESOURCE_PATH, CONTAINER_MOUNT_POINT, BindMode.READ_WRITE);