-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
579318f
commit 1b00e0f
Showing
7 changed files
with
63 additions
and
43 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
5 changes: 5 additions & 0 deletions
5
src/test/kotlin/kr/galaxyhub/sc/config/EnableTestcontianers.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,5 @@ | ||
package kr.galaxyhub.sc.config | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class EnableTestcontianers |
9 changes: 0 additions & 9 deletions
9
src/test/kotlin/kr/galaxyhub/sc/config/KotestProjectConfig.kt
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
src/test/kotlin/kr/galaxyhub/sc/config/TestContainerExtension.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,43 @@ | ||
package kr.galaxyhub.sc.config | ||
|
||
import io.kotest.core.extensions.ProjectExtension | ||
import io.kotest.core.project.ProjectContext | ||
import io.kotest.core.spec.AutoScan | ||
import io.kotest.mpp.hasAnnotation | ||
import java.io.File | ||
import kotlin.reflect.full.superclasses | ||
import org.slf4j.LoggerFactory | ||
import org.testcontainers.containers.DockerComposeContainer | ||
import org.testcontainers.containers.output.Slf4jLogConsumer | ||
import org.testcontainers.containers.wait.strategy.ShellStrategy | ||
|
||
@AutoScan | ||
class TestContainerExtension : ProjectExtension { | ||
|
||
private val containers = mutableListOf<DockerComposeContainer<*>>() | ||
|
||
override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) { | ||
if (isTestcontianersEnable(context)) { | ||
DockerComposeContainer(File("src/test/resources/docker-compose.yml")).apply { | ||
waitingFor( | ||
"test_mysql", | ||
ShellStrategy().withCommand("mysql -u'test' -p'1234' -e'select 1' && sleep 2") | ||
) | ||
withLogConsumer("test_mysql", Slf4jLogConsumer(LoggerFactory.getLogger(javaClass))) | ||
start() | ||
}.also { | ||
containers.add(it) | ||
} | ||
} | ||
callback(context) | ||
containers.forEach { it.stop() } | ||
} | ||
|
||
private fun isTestcontianersEnable(context: ProjectContext): Boolean { | ||
val hasAnnotationItself = context.suite.specs.stream() | ||
.anyMatch { it.kclass.hasAnnotation<EnableTestcontianers>() } | ||
return if (hasAnnotationItself) true else context.suite.specs.stream() | ||
.flatMap { it.kclass.superclasses.stream() } | ||
.anyMatch { it.hasAnnotation<EnableTestcontianers>() } | ||
} | ||
} |
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
22 changes: 4 additions & 18 deletions
22
src/test/kotlin/kr/galaxyhub/sc/config/spec/IntegrationSpec.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 |
---|---|---|
@@ -1,27 +1,13 @@ | ||
package kr.galaxyhub.sc.config.spec | ||
|
||
import io.kotest.core.extensions.install | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.extensions.testcontainers.ContainerLifecycleMode | ||
import io.kotest.extensions.testcontainers.DockerComposeContainerExtension | ||
import java.io.File | ||
import org.slf4j.LoggerFactory | ||
import io.kotest.extensions.spring.SpringExtension | ||
import kr.galaxyhub.sc.config.EnableTestcontianers | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.testcontainers.containers.output.Slf4jLogConsumer | ||
import org.testcontainers.containers.wait.strategy.ShellStrategy | ||
|
||
@SpringBootTest | ||
@EnableTestcontianers | ||
abstract class IntegrationSpec(body: DescribeSpec.() -> Unit = {}) : DescribeSpec(body) { | ||
|
||
private val log = LoggerFactory.getLogger(javaClass) | ||
|
||
init { | ||
install(DockerComposeContainerExtension(File("src/test/resources/docker-compose.yml"), ContainerLifecycleMode.Project)) | ||
.apply { | ||
waitingFor("test_mysql", ShellStrategy().withCommand("mysql -u'test' -p'1234' -e'select 1' && sleep 2")) | ||
withLogConsumer("test_mysql", Slf4jLogConsumer(log)) | ||
}.apply { | ||
start() | ||
} | ||
} | ||
override fun extensions() = listOf(SpringExtension) | ||
} |
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 |
---|---|---|
@@ -1,21 +1,15 @@ | ||
version: "3.3" | ||
volumes: | ||
mysql_data: { } | ||
version: "3" | ||
services: | ||
test_mysql: | ||
image: mysql:8.0.33 | ||
ports: | ||
- "13306:3306" | ||
environment: | ||
TZ: Asia/Seoul | ||
MYSQL_DATABASE: test | ||
MYSQL_ROOT_PASSWORD: 1234 | ||
MYSQL_USER: test | ||
MYSQL_PASSWORD: 1234 | ||
MYSQL_ROOT_PASSWORD: 1234 | ||
MYSQL_DATABASE: test | ||
volumes: | ||
- mysql_data:/var/lib/mysql/ | ||
- ./sql:/docker-entrypoint-initdb.d | ||
command: | ||
- "--character-set-server=utf8mb4" | ||
- "--collation-server=utf8mb4_unicode_ci" | ||
- "--skip-character-set-client-handshake" | ||
- --character-set-server=utf8mb4 | ||
- --collation-server=utf8mb4_unicode_ci |