diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 4fd1da6960..f3022c0925 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -152,6 +152,21 @@ object Dependencies { // was included transitively before, now explicit val commonsCodec: ModuleID = "commons-codec" % "commons-codec" % "1.15" + val embeddedPostgres = ("io.zonky.test" % "embedded-postgres" % "2.0.3") + .exclude("io.zonky.test", "postgres") + + val embeddedPgOsVersion = + System.getProperty("os.name").toLowerCase match { + case osName if osName.contains("mac") => + "embedded-postgres-binaries-darwin-amd64" + case osName if osName.contains("win") => + "embedded-postgres-binaries-windows-amd64" + case osName if osName.contains("linux") => + "embedded-postgres-binaries-linux-amd64" + case osName => throw new RuntimeException(s"Unknown operating system $osName") + } + val embeddedPgBinaries = "io.zonky.test.postgres" % embeddedPgOsVersion % "9.6.24" + val junit = "junit" % "junit" % "4.13.1" % "test" val rootDependencies = Seq( // proactively pull in latest versions of Jackson libs, instead of relying on the versions // specified as transitive dependencies, due to OWASP DependencyCheck warnings for earlier versions. @@ -201,6 +216,9 @@ object Dependencies { postgres, cloudResourceLib, nettyAll, - azureManagedApplications + azureManagedApplications, + embeddedPostgres, + embeddedPgBinaries, + junit ) ++ openCensusDependencies } diff --git a/src/test/resources/reference.conf b/src/test/resources/reference.conf index 9955e3131c..baa4cce766 100644 --- a/src/test/resources/reference.conf +++ b/src/test/resources/reference.conf @@ -128,9 +128,9 @@ db { poolMaxSize = 5 poolConnectionTimeoutMillis = 5000 driver = "org.postgresql.Driver" - url = "jdbc:postgresql://"${postgres.host}":"${postgres.port}"/testdb?stringtype=unspecified" - user = "sam-test" - password = "sam-test" + url = "jdbc:postgresql://localhost:5432/postgres?stringtype=unspecified" + user = "postgres" + password = "postgres" } sam_write { @@ -139,9 +139,9 @@ db { poolMaxSize = 5 poolConnectionTimeoutMillis = 5000 driver = "org.postgresql.Driver" - url = "jdbc:postgresql://"${postgres.host}":"${postgres.port}"/testdb?stringtype=unspecified" - user = "sam-test" - password = "sam-test" + url = "jdbc:postgresql://localhost:5432/postgres?stringtype=unspecified" + user = "postgres" + password = "postgres" } // this background pool is used to test the status of a pool that cannot connect @@ -152,8 +152,8 @@ db { poolConnectionTimeoutMillis = 5000 driver = "org.postgresql.Driver" url = "jdbc:postgresql://does_not_exist/testdb?stringtype=unspecified" - user = "sam-test" - password = "sam-test" + user = "postgres" + password = "postgres" } } diff --git a/src/test/scala/org/broadinstitute/dsde/workbench/sam/TestSupport.scala b/src/test/scala/org/broadinstitute/dsde/workbench/sam/TestSupport.scala index d61d6b079f..fa681a4c6f 100644 --- a/src/test/scala/org/broadinstitute/dsde/workbench/sam/TestSupport.scala +++ b/src/test/scala/org/broadinstitute/dsde/workbench/sam/TestSupport.scala @@ -71,7 +71,7 @@ object TestSupport extends TestSupport { val googleServicesConfig = appConfig.googleConfig.get.googleServicesConfig val configResourceTypes = config.as[Map[String, ResourceType]]("resourceTypes").values.map(rt => rt.name -> rt).toMap val adminConfig = config.as[AdminConfig]("admin") - val databaseEnabled = config.getBoolean("db.enabled") + val databaseEnabled = true val databaseEnabledClue = "-- skipping tests that talk to a real database" lazy val distributedLock = PostgresDistributedLockDAO[IO](dbRef, dbRef, appConfig.distributedLockConfig) diff --git a/src/test/scala/org/broadinstitute/dsde/workbench/sam/db/TestDbReference.scala b/src/test/scala/org/broadinstitute/dsde/workbench/sam/db/TestDbReference.scala index 8b33c5e6ea..6fc0f6ffa8 100644 --- a/src/test/scala/org/broadinstitute/dsde/workbench/sam/db/TestDbReference.scala +++ b/src/test/scala/org/broadinstitute/dsde/workbench/sam/db/TestDbReference.scala @@ -5,6 +5,7 @@ import com.google.common.base.Throwables import com.typesafe.config.ConfigFactory import com.typesafe.scalalogging.LazyLogging import io.opencensus.trace.AttributeValue +import io.zonky.test.db.postgres.embedded.{EmbeddedPostgres, LiquibasePreparer} import liquibase.database.jvm.JdbcConnection import liquibase.resource.{ClassLoaderResourceAccessor, ResourceAccessor} import liquibase.{Contexts, Liquibase} @@ -13,7 +14,7 @@ import org.broadinstitute.dsde.workbench.sam.db.TestDbReference.databaseEnabled import org.broadinstitute.dsde.workbench.sam.util.SamRequestContext import org.broadinstitute.dsde.workbench.util2.ExecutionContexts import scalikejdbc.config.DBs -import scalikejdbc.{ConnectionPool, DBSession, IsolationLevel} +import scalikejdbc.{DBSession, IsolationLevel} import java.security.cert.CertPathBuilderException import java.sql.SQLTimeoutException @@ -22,11 +23,18 @@ import scala.concurrent.ExecutionContext object TestDbReference extends LazyLogging { private val config = ConfigFactory.load() private val databaseEnabled = config.getBoolean("db.enabled") + val liquibaseChangelog = "liquibase/changelog.xml" + lazy val liquibasePreparer = LiquibasePreparer.forClasspathLocation(liquibaseChangelog) + + lazy val embeddedDb = EmbeddedPostgres + .builder() + .setPort(5432) + .start() // This code is copied over from DbReference. We didnt want to have to refactor DbReference to be able to use it in tests. private def initWithLiquibase(liquibaseConfig: LiquibaseConfig, dbName: Symbol, changelogParameters: Map[String, AnyRef] = Map.empty): Unit = if (databaseEnabled) { - val dbConnection = ConnectionPool.borrow(dbName) + val dbConnection = embeddedDb.getPostgresDatabase.getConnection try { val liquibaseConnection = new JdbcConnection(dbConnection) val resourceAccessor: ResourceAccessor = new ClassLoaderResourceAccessor() diff --git a/src/test/scala/org/broadinstitute/dsde/workbench/sam/service/UserServiceSpec.scala b/src/test/scala/org/broadinstitute/dsde/workbench/sam/service/UserServiceSpec.scala index 1526f70893..0c305e2ec2 100644 --- a/src/test/scala/org/broadinstitute/dsde/workbench/sam/service/UserServiceSpec.scala +++ b/src/test/scala/org/broadinstitute/dsde/workbench/sam/service/UserServiceSpec.scala @@ -283,8 +283,6 @@ class OldUserServiceSpec val allUsersGroup: BasicWorkbenchGroup = BasicWorkbenchGroup(WorkbenchGroupName("All_Users"), Set(), WorkbenchEmail("all_users@fake.com")) before { - clearDatabase() - googleExtensions = mock[GoogleExtensions](RETURNS_SMART_NULLS) if (databaseEnabled) { when(googleExtensions.getOrCreateAllUsersGroup(any[DirectoryDAO], any[SamRequestContext])(any[ExecutionContext])) diff --git a/src/test/scala/org/broadinstitute/dsde/workbench/sam/util/DatabaseSupportSpec.scala b/src/test/scala/org/broadinstitute/dsde/workbench/sam/util/DatabaseSupportSpec.scala index 1b01749e7d..f35a2c77a4 100644 --- a/src/test/scala/org/broadinstitute/dsde/workbench/sam/util/DatabaseSupportSpec.scala +++ b/src/test/scala/org/broadinstitute/dsde/workbench/sam/util/DatabaseSupportSpec.scala @@ -3,7 +3,7 @@ package org.broadinstitute.dsde.workbench.sam.util import cats.effect.IO import org.broadinstitute.dsde.workbench.model.WorkbenchUserId import org.broadinstitute.dsde.workbench.sam.TestSupport -import org.broadinstitute.dsde.workbench.sam.db.{PSQLStateExtensions, TestDbReference} +import org.broadinstitute.dsde.workbench.sam.db.PSQLStateExtensions import org.postgresql.util.PSQLException import org.scalatest.BeforeAndAfterEach import org.scalatest.freespec.AnyFreeSpec @@ -20,8 +20,8 @@ import org.broadinstitute.dsde.workbench.sam.TestSupport.{databaseEnabled, datab class DatabaseSupportSpec extends AnyFreeSpec with Matchers with BeforeAndAfterEach with TestSupport { implicit val ec = scala.concurrent.ExecutionContext.global object DatabaseSupport extends DatabaseSupport { - override protected lazy val writeDbRef: TestDbReference = TestSupport.dbRef - override protected lazy val readDbRef: TestDbReference = TestSupport.dbRef + override protected lazy val writeDbRef = TestSupport.dbRef + override protected lazy val readDbRef = TestSupport.dbRef override def serializableWriteTransaction[A](dbQueryName: String, samRequestContext: SamRequestContext, maxTries: Int = 3)( databaseFunction: DBSession => A