dave08
02/15/2021, 3:06 PMStartable.perProject(...)
takes a name param, but doesn't work for a customized container...dave08
02/15/2021, 3:16 PMdave08
02/15/2021, 3:50 PMclass Postgresql: PostgreSQLContainer<Postgresql>("postgres:9.6") {
companion object {
private lateinit var instance: Postgresql
fun start() {
if (!Companion::instance.isInitialized) {
// Reuse container between tests instead of starting a new one per execution
instance = Postgresql()
instance
.withReuse(true)
.withUsername("...")
.withDatabaseName("...")
.withInitScript("db-structure.sql")
.start()
System.setProperty("datasources.default.url", instance.jdbcUrl)
System.setProperty("datasources.default.username", instance.username)
System.setProperty("datasources.default.password", instance.password)
}
}
fun stop() {
if (Companion::instance.isInitialized) {
instance.stop()
}
}
}
}
dave08
02/15/2021, 3:51 PMsam
02/15/2021, 3:51 PMdave08
02/15/2021, 3:51 PMGenericContainer<Nothing>("redis:5.0.3-alpine")
... but not using configuration like I did in the previous code.dave08
02/15/2021, 3:52 PMsam
02/15/2021, 3:53 PMlistener(mycontainer.perTest())
dave08
02/15/2021, 3:56 PMsam
02/15/2021, 3:57 PMdave08
02/15/2021, 3:58 PMfun <T : Startable> T.perProject(containerName: String): StartablePerProjectListener<T> = StartablePerProjectListener<T>(this, containerName)
In the kotest extenstion libsam
02/15/2021, 3:58 PMsam
02/15/2021, 3:59 PMsam
02/15/2021, 3:59 PMdave08
02/15/2021, 4:02 PMinstance
in the companion object, it would be ok... but that's a pretty messy way to configure an extension like this 🤒sam
02/15/2021, 4:03 PMsam
02/15/2021, 4:04 PMsam
02/15/2021, 4:04 PMsam
02/15/2021, 4:04 PMclass PostgresTest : FunSpec() {
init {
class MyPostgreSQLContainer : PostgreSQLContainer<MyPostgreSQLContainer>("somedocker")
val postgres = MyPostgreSQLContainer()
.withReuse(true)
.withUsername("...")
.withDatabaseName("...")
listener(postgres.perProject("listener-name"))
test("with postgres container") {
}
}
}
sam
02/15/2021, 4:04 PMdave08
02/15/2021, 4:07 PMGenericContainer<Nothing>(...)
, that can't be used here too? PostgreSQLContainer<Nothing>("somedocker")
?sam
02/15/2021, 4:08 PMsam
02/15/2021, 4:09 PMdave08
02/15/2021, 4:31 PMsam
02/15/2021, 4:31 PMdave08
02/15/2021, 4:33 PMsam
02/15/2021, 4:33 PMsam
02/15/2021, 4:33 PMdave08
02/15/2021, 4:34 PMsam
02/15/2021, 4:34 PMsam
02/15/2021, 4:34 PMdave08
02/15/2021, 4:34 PMsam
02/15/2021, 4:35 PMsam
02/15/2021, 4:35 PMdave08
02/15/2021, 4:35 PMthanksforallthefish
02/15/2021, 5:08 PMclass ProjectConfig : AbstractProjectConfig() {
override fun listeners() = listOf(
StartablePerProjectListener(
SpringPostgreSQLContainer("10").withNetwork(Network.SHARED)
.withNetworkAliases("db"), "postgres"
),
StartablePerProjectListener(
SpringLocalstackContainer("0.11.2").withServices(LocalStackContainer.Service.S3)
.withNetwork(Network.SHARED)
.withNetworkAliases("s3"), "s3"
),
SpringListener
)
}
class SpringLocalstackContainer(tag: String) :
LocalStackContainer(DockerImageName.parse("localstack/localstack").withTag(tag)) {
override fun start() {
super.start()
System.setProperty("S3_ENDPOINT", getEndpointConfiguration(Service.S3).serviceEndpoint)
System.setProperty("S3_REGION", region)
System.setProperty("AWS_ACCESS_KEY", accessKey)
System.setProperty("AWS_SECRET_KEY", secretKey)
}
}
class SpringPostgreSQLContainer(tag: String) :
PostgreSQLContainer<SpringPostgreSQLContainer>(DockerImageName.parse(IMAGE).withTag(tag)) {
override fun start() {
super.start()
System.setProperty("DB_URL", "jdbc:postgresql://${connectionString()}")
System.setProperty("DB_USERNAME", username)
System.setProperty("DB_PASSWORD", password)
System.setProperty("spring.flyway.schemas", databaseName)
}
private fun connectionString() = "$host:$firstMappedPort/$databaseName"
}
but we have a very simple project with just this 2 containers as dependencies for now
got hit by the weird type variance as well: https://github.com/testcontainers/testcontainers-java/issues/318dave08
02/16/2021, 12:47 PMclass StartablePerProjectListener<T : Startable>(
val startable: T,
val containerName: String,
val afterStart: (T.() -> Unit)?
) : TestListener,
ProjectListener {
override val name = containerName
private val testLifecycleAwareListener = TestLifecycleAwareListener(startable)
override suspend fun beforeProject() {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
startable.start()
afterStart?.invoke(startable)
}
}
...
dave08
02/16/2021, 12:48 PMsam
02/16/2021, 12:56 PMdave08
02/16/2021, 12:57 PM