mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
Run database tests in a Docker container
This commit is contained in:
@@ -66,8 +66,7 @@ libraryDependencies ++= Seq(
|
|||||||
"junit" % "junit" % "4.12" % "test",
|
"junit" % "junit" % "4.12" % "test",
|
||||||
"org.scalatra" %% "scalatra-scalatest" % ScalatraVersion % "test",
|
"org.scalatra" %% "scalatra-scalatest" % ScalatraVersion % "test",
|
||||||
"org.mockito" % "mockito-core" % "2.23.4" % "test",
|
"org.mockito" % "mockito-core" % "2.23.4" % "test",
|
||||||
"com.wix" % "wix-embedded-mysql" % "4.2.0" % "test",
|
"com.dimafeng" %% "testcontainers-scala" % "0.22.0" % "test",
|
||||||
"ru.yandex.qatools.embed" % "postgresql-embedded" % "2.10" % "test",
|
|
||||||
"net.i2p.crypto" % "eddsa" % "0.3.0",
|
"net.i2p.crypto" % "eddsa" % "0.3.0",
|
||||||
"is.tagomor.woothee" % "woothee-java" % "1.8.0",
|
"is.tagomor.woothee" % "woothee-java" % "1.8.0",
|
||||||
"org.ec4j.core" % "ec4j-core" % "0.0.3"
|
"org.ec4j.core" % "ec4j-core" % "0.0.3"
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
package gitbucket.core
|
package gitbucket.core
|
||||||
|
|
||||||
import java.sql.DriverManager
|
import java.sql.DriverManager
|
||||||
|
import java.time.Duration
|
||||||
|
import java.time.temporal.ChronoUnit.SECONDS
|
||||||
|
|
||||||
|
import com.dimafeng.testcontainers.GenericContainer
|
||||||
import io.github.gitbucket.solidbase.Solidbase
|
import io.github.gitbucket.solidbase.Solidbase
|
||||||
import io.github.gitbucket.solidbase.model.Module
|
import io.github.gitbucket.solidbase.model.Module
|
||||||
import liquibase.database.core.{H2Database, MySQLDatabase, PostgresDatabase}
|
import liquibase.database.core.{H2Database, MySQLDatabase, PostgresDatabase}
|
||||||
|
import org.junit.runner.Description
|
||||||
import org.scalatest.{FunSuite, Tag}
|
import org.scalatest.{FunSuite, Tag}
|
||||||
import com.wix.mysql.EmbeddedMysql._
|
import org.testcontainers.containers.ContainerLaunchException
|
||||||
import com.wix.mysql.config.Charset
|
import org.testcontainers.containers.wait.strategy.{HostPortWaitStrategy, Wait}
|
||||||
import com.wix.mysql.config.MysqldConfig._
|
|
||||||
import com.wix.mysql.distribution.Version._
|
|
||||||
import ru.yandex.qatools.embed.postgresql.PostgresStarter
|
|
||||||
import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.{Credentials, Net, Storage, Timeout}
|
|
||||||
import ru.yandex.qatools.embed.postgresql.config.PostgresConfig
|
|
||||||
import ru.yandex.qatools.embed.postgresql.distribution.Version.Main.PRODUCTION
|
|
||||||
|
|
||||||
object ExternalDBTest extends Tag("ExternalDBTest")
|
object ExternalDBTest extends Tag("ExternalDBTest")
|
||||||
|
|
||||||
@@ -28,52 +26,106 @@ class GitBucketCoreModuleSpec extends FunSuite {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
test("Migration MySQL", ExternalDBTest) {
|
implicit private val suiteDescription = Description.createSuiteDescription(getClass)
|
||||||
val config = aMysqldConfig(v5_7_latest)
|
|
||||||
.withPort(3306)
|
|
||||||
.withUser("sa", "sa")
|
|
||||||
.withCharset(Charset.UTF8)
|
|
||||||
.withServerVariable("bind-address", "127.0.0.1")
|
|
||||||
.build()
|
|
||||||
|
|
||||||
val mysqld = anEmbeddedMysql(config)
|
test("Migration MySQL 5.7", ExternalDBTest) {
|
||||||
.addSchema("gitbucket")
|
val container = GenericContainer(
|
||||||
.start()
|
"mysql:5.7",
|
||||||
|
env = Map("MYSQL_ROOT_PASSWORD" -> "my-secret-pw", "MYSQL_DATABASE" -> "gitbucket"),
|
||||||
|
waitStrategy = new HostPortWaitStrategy {
|
||||||
|
override def waitUntilReady(): Unit = {
|
||||||
|
super.waitUntilReady()
|
||||||
|
|
||||||
|
def readyForConnections(retry: Int = 0): Boolean = {
|
||||||
|
var con: java.sql.Connection = null
|
||||||
|
try {
|
||||||
|
con = DriverManager.getConnection(
|
||||||
|
s"jdbc:mysql://${waitStrategyTarget.getContainerIpAddress}:${waitStrategyTarget.getMappedPort(3306)}/gitbucket?useSSL=false",
|
||||||
|
"root",
|
||||||
|
"my-secret-pw"
|
||||||
|
)
|
||||||
|
con.createStatement().execute("SELECT 1")
|
||||||
|
} catch {
|
||||||
|
case _: Exception if retry < 3 =>
|
||||||
|
Thread.sleep(10000)
|
||||||
|
readyForConnections(retry + 1)
|
||||||
|
case _: Exception => false
|
||||||
|
} finally {
|
||||||
|
Option(con).foreach(_.close())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!readyForConnections()) throw new ContainerLaunchException("Timed out")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
container.starting()
|
||||||
try {
|
try {
|
||||||
new Solidbase().migrate(
|
new Solidbase().migrate(
|
||||||
DriverManager.getConnection("jdbc:mysql://localhost:3306/gitbucket?useSSL=false", "sa", "sa"),
|
DriverManager.getConnection(
|
||||||
|
s"jdbc:mysql://${container.containerIpAddress}:${container.mappedPort(3306)}/gitbucket?useSSL=false",
|
||||||
|
"root",
|
||||||
|
"my-secret-pw"
|
||||||
|
),
|
||||||
Thread.currentThread().getContextClassLoader(),
|
Thread.currentThread().getContextClassLoader(),
|
||||||
new MySQLDatabase(),
|
new MySQLDatabase(),
|
||||||
new Module(GitBucketCoreModule.getModuleId, GitBucketCoreModule.getVersions)
|
new Module(GitBucketCoreModule.getModuleId, GitBucketCoreModule.getVersions)
|
||||||
)
|
)
|
||||||
} finally {
|
} finally {
|
||||||
mysqld.stop()
|
container.finished()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test("Migration PostgreSQL", ExternalDBTest) {
|
test("Migration PostgreSQL 11", ExternalDBTest) {
|
||||||
val runtime = PostgresStarter.getDefaultInstance()
|
val container = GenericContainer(
|
||||||
val config = new PostgresConfig(
|
"postgres:11",
|
||||||
PRODUCTION,
|
env = Map("POSTGRES_PASSWORD" -> "mysecretpassword", "POSTGRES_DB" -> "gitbucket"),
|
||||||
new Net("localhost", 5432),
|
waitStrategy = Wait
|
||||||
new Storage("gitbucket"),
|
.forLogMessage(".*database system is ready to accept connections.*\\s", 2)
|
||||||
new Timeout(),
|
.withStartupTimeout(Duration.of(60, SECONDS))
|
||||||
new Credentials("sa", "sa")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
val exec = runtime.prepare(config)
|
container.starting()
|
||||||
val process = exec.start()
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new Solidbase().migrate(
|
new Solidbase().migrate(
|
||||||
DriverManager.getConnection("jdbc:postgresql://localhost:5432/gitbucket", "sa", "sa"),
|
DriverManager.getConnection(
|
||||||
|
s"jdbc:postgresql://${container.containerIpAddress}:${container.mappedPort(5432)}/gitbucket",
|
||||||
|
"postgres",
|
||||||
|
"mysecretpassword"
|
||||||
|
),
|
||||||
Thread.currentThread().getContextClassLoader(),
|
Thread.currentThread().getContextClassLoader(),
|
||||||
new PostgresDatabase(),
|
new PostgresDatabase(),
|
||||||
new Module(GitBucketCoreModule.getModuleId, GitBucketCoreModule.getVersions)
|
new Module(GitBucketCoreModule.getModuleId, GitBucketCoreModule.getVersions)
|
||||||
)
|
)
|
||||||
} finally {
|
} finally {
|
||||||
process.stop()
|
container.finished()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("Migration PostgreSQL 10", ExternalDBTest) {
|
||||||
|
val container = GenericContainer(
|
||||||
|
"postgres:10",
|
||||||
|
env = Map("POSTGRES_PASSWORD" -> "mysecretpassword", "POSTGRES_DB" -> "gitbucket"),
|
||||||
|
waitStrategy = Wait
|
||||||
|
.forLogMessage(".*database system is ready to accept connections.*\\s", 2)
|
||||||
|
.withStartupTimeout(Duration.of(60, SECONDS))
|
||||||
|
)
|
||||||
|
|
||||||
|
container.starting()
|
||||||
|
try {
|
||||||
|
new Solidbase().migrate(
|
||||||
|
DriverManager.getConnection(
|
||||||
|
s"jdbc:postgresql://${container.containerIpAddress}:${container.mappedPort(5432)}/gitbucket",
|
||||||
|
"postgres",
|
||||||
|
"mysecretpassword"
|
||||||
|
),
|
||||||
|
Thread.currentThread().getContextClassLoader(),
|
||||||
|
new PostgresDatabase(),
|
||||||
|
new Module(GitBucketCoreModule.getModuleId, GitBucketCoreModule.getVersions)
|
||||||
|
)
|
||||||
|
} finally {
|
||||||
|
container.finished()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user