Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test out embedded (in mem) pg for tests #1005

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -201,6 +216,9 @@ object Dependencies {
postgres,
cloudResourceLib,
nettyAll,
azureManagedApplications
azureManagedApplications,
embeddedPostgres,
embeddedPgBinaries,
junit
) ++ openCensusDependencies
}
16 changes: 8 additions & 8 deletions src/test/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down