-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecated remaining older extensions
- Loading branch information
Showing
11 changed files
with
300 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/io/kotest/extensions/testcontainers/ContainerLifecycleMode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.kotest.extensions.testcontainers | ||
|
||
/** | ||
* Determines the lifetime of a test container installed in a Kotest extension. | ||
*/ | ||
enum class ContainerLifecycleMode { | ||
|
||
/** | ||
* The TestContainer is started only when first installed and stopped after the spec where it was | ||
* installed completes. | ||
* | ||
* Use this when you need the test container to shut down as soon as the spec does - usually | ||
* because you are using a separate test container per spec and waiting until the test suite | ||
* completes to shut them all down will take too much memory. | ||
*/ | ||
Spec, | ||
|
||
/** | ||
* The TestContainer is started only when first installed and stopped after the entire test suite. | ||
* This mode is the default choice for test containers. This mode can be used with | ||
* multiple test containers by using separate instances of the container extensions. | ||
*/ | ||
Project, | ||
} |
114 changes: 114 additions & 0 deletions
114
src/main/kotlin/io/kotest/extensions/testcontainers/DockerComposeContainerExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package io.kotest.extensions.testcontainers | ||
|
||
import io.kotest.core.extensions.MountableExtension | ||
import io.kotest.core.listeners.AfterProjectListener | ||
import io.kotest.core.listeners.AfterSpecListener | ||
import io.kotest.core.listeners.AfterTestListener | ||
import io.kotest.core.listeners.BeforeSpecListener | ||
import io.kotest.core.listeners.BeforeTestListener | ||
import io.kotest.core.spec.Spec | ||
import io.kotest.core.test.TestCase | ||
import io.kotest.core.test.TestResult | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.testcontainers.containers.DockerComposeContainer | ||
import org.testcontainers.containers.GenericContainer | ||
|
||
/** | ||
* A Kotest [MountableExtension] for [GenericContainer]s that are started the first time they are | ||
* installed in a spec. | ||
* | ||
* If no spec is executed that installs a particular container, | ||
* then that container is never started. | ||
* | ||
* Containers can be shared between specs using the default [mode]. | ||
* | ||
* @param container the test container instance | ||
* | ||
* @param mode determines if the container is shutdown after the test suite (project) or after the installed spec | ||
* The default is after the test suite. | ||
* | ||
* @param beforeSpec a beforeSpec callback | ||
* @param afterSpec an afterSpec callback | ||
* @param beforeTest a beforeTest callback | ||
* @param afterTest a afterTest callback | ||
* | ||
* @param beforeStart a callback that is invoked only once, just before the container is started. | ||
* If the container is never started, this callback will not be invoked. | ||
* This callback can be useful instead of the installation callback as it will only | ||
* be executed once, regardless of how many times this container is installed. | ||
* | ||
* @param afterStart a callback that is invoked only once, just after the container is started. | ||
* If the container is never started, this callback will not be invoked. | ||
* | ||
* @param beforeShutdown a callback that is invoked only once, just before the container is stopped. | ||
* If the container is never started, this callback will not be invoked. | ||
* | ||
* @param afterShutdown a callback that is invoked only once, just after the container is stopped. | ||
* If the container is never started, this callback will not be invoked. | ||
*/ | ||
class DockerComposeContainerExtension<T : DockerComposeContainer<T>>( | ||
private val container: T, | ||
private val mode: ContainerLifecycleMode = ContainerLifecycleMode.Project, | ||
private val beforeStart: (T) -> Unit = {}, | ||
private val afterStart: (T) -> Unit = {}, | ||
private val beforeTest: suspend (TestCase, T) -> Unit = { _, _ -> }, | ||
private val afterTest: suspend (TestCase, T) -> Unit = { _, _ -> }, | ||
private val beforeSpec: suspend (Spec, T) -> Unit = { _, _ -> }, | ||
private val afterSpec: suspend (Spec, T) -> Unit = { _, _ -> }, | ||
private val beforeShutdown: (T) -> Unit = {}, | ||
private val afterShutdown: (T) -> Unit = {}, | ||
) : MountableExtension<T, T>, | ||
AfterProjectListener, | ||
BeforeTestListener, | ||
BeforeSpecListener, | ||
AfterTestListener, | ||
AfterSpecListener { | ||
|
||
private var started: Boolean = false | ||
|
||
/** | ||
* Mounts the container, starting it if necessary. The [configure] block will be invoked | ||
* every time the container is mounted, and after the container has started. | ||
*/ | ||
override fun mount(configure: T.() -> Unit): T { | ||
if (!started) { | ||
beforeStart(container) | ||
container.start() | ||
started = true | ||
afterStart(container) | ||
} | ||
container.configure() | ||
return container | ||
} | ||
|
||
override suspend fun beforeTest(testCase: TestCase) { | ||
beforeTest(testCase, container) | ||
} | ||
|
||
override suspend fun afterTest(testCase: TestCase, result: TestResult) { | ||
afterTest(testCase, container) | ||
} | ||
|
||
override suspend fun beforeSpec(spec: Spec) { | ||
beforeSpec(spec, container) | ||
} | ||
|
||
override suspend fun afterSpec(spec: Spec) { | ||
afterSpec(spec, container) | ||
if (mode == ContainerLifecycleMode.Spec && started) close() | ||
} | ||
|
||
override suspend fun afterProject() { | ||
if (started) close() | ||
} | ||
|
||
private suspend fun close() { | ||
withContext(Dispatchers.IO) { | ||
beforeShutdown(container) | ||
container.stop() | ||
started = false | ||
afterShutdown(container) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/kotlin/io/kotest/extensions/testcontainers/JdbcDatabaseContainerExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.kotest.extensions.testcontainers | ||
|
||
import com.zaxxer.hikari.HikariDataSource | ||
import io.kotest.core.extensions.MountableExtension | ||
import io.kotest.core.listeners.AfterProjectListener | ||
import io.kotest.core.listeners.AfterSpecListener | ||
import io.kotest.core.listeners.AfterTestListener | ||
import io.kotest.core.listeners.BeforeSpecListener | ||
import io.kotest.core.listeners.BeforeTestListener | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.testcontainers.containers.JdbcDatabaseContainer | ||
|
||
/** | ||
* A Kotest [MountableExtension] for [JdbcDatabaseContainer]s that are started the first time they are | ||
* installed in a spec. | ||
* | ||
* If no spec is executed that installs a particular container, | ||
* then that container is never started. | ||
* | ||
* Once mounted in a spec, the return value from the installation point is a configured HikariDataSource. | ||
* | ||
* @param container the specific database test container type | ||
* @param beforeSpec a beforeSpec callback | ||
* @param afterSpec an afterSpec callback | ||
* @param beforeTest a beforeTest callback | ||
* @param afterTest a afterTest callback | ||
* | ||
* @param beforeStart a callback that is invoked only once, just before the container is started. | ||
* If the container is never started, this callback will not be invoked. | ||
* This callback can be useful instead of the installation callback as it will only | ||
* be executed once, regardless of how many times this container is installed. | ||
* | ||
* @param afterStart a callback that is invoked only once, just after the container is started. | ||
* If the container is never started, this callback will not be invoked. | ||
* | ||
* @param beforeShutdown a callback that is invoked only once, just before the container is stopped. | ||
* If the container is never started, this callback will not be invoked. | ||
* | ||
* @param afterShutdown a callback that is invoked only once, just after the container is stopped. | ||
* If the container is never started, this callback will not be invoked. | ||
*/ | ||
class JdbcDatabaseContainerExtension( | ||
private val container: JdbcDatabaseContainer<*>, | ||
private val beforeStart: (JdbcDatabaseContainer<*>) -> Unit = {}, | ||
private val afterStart: (JdbcDatabaseContainer<*>) -> Unit = {}, | ||
private val beforeShutdown: (JdbcDatabaseContainer<*>) -> Unit = {}, | ||
private val beforeTest: suspend (HikariDataSource) -> Unit = {}, | ||
private val afterTest: suspend (HikariDataSource) -> Unit = {}, | ||
private val beforeSpec: suspend (HikariDataSource) -> Unit = {}, | ||
private val afterSpec: suspend (HikariDataSource) -> Unit = {}, | ||
private val afterShutdown: (HikariDataSource) -> Unit = {}, | ||
) : MountableExtension<TestContainerHikariConfig, HikariDataSource>, | ||
AfterProjectListener, | ||
BeforeTestListener, | ||
BeforeSpecListener, | ||
AfterTestListener, | ||
AfterSpecListener { | ||
|
||
/** | ||
* Mounts the container, starting it if necessary. The [configure] block will be invoked | ||
* every time the container is mounted, and after the container has started. | ||
*/ | ||
override fun mount(configure: TestContainerHikariConfig.() -> Unit): HikariDataSource { | ||
if (!container.isRunning) { | ||
beforeStart(container) | ||
container.start() | ||
afterStart(container) | ||
} | ||
return createDataSource(configure) | ||
} | ||
|
||
override suspend fun afterProject() { | ||
if (container.isRunning) withContext(Dispatchers.IO) { | ||
beforeShutdown(container) | ||
container.stop() | ||
} | ||
} | ||
|
||
private fun createDataSource(configure: TestContainerHikariConfig.() -> Unit): HikariDataSource { | ||
val config = TestContainerHikariConfig() | ||
config.jdbcUrl = container.jdbcUrl | ||
config.username = container.username | ||
config.password = container.password | ||
config.configure() | ||
// runInitScripts(ds.connection, config.dbInitScripts) | ||
return HikariDataSource(config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.