Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Run Gradle verification
run: |
chmod +x gradlew
./gradlew check --no-daemon --build-cache --parallel
./gradlew check --no-daemon --build-cache --parallel -PrequireContainerTests=true

runtime-containers:
runs-on: ubuntu-latest
Expand Down
9 changes: 9 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Market provider
- Command 기본은 JPA, 복합 조회는 jOOQ
- DBML·Flyway는 루트 저장소가 통합 관리합니다.

## 테스트와 Docker

- PostgreSQL·Redis가 필요한 테스트는 Testcontainers로 실행합니다. 로컬에서 Docker가 없으면
`disabledWithoutDocker = true`에 따라 건너뛰고 `./gradlew check`는 통과합니다.
- Docker 사용 가능 여부는 모든 `Test` 작업의 입력입니다. Docker 없이 실행한 결과는 빌드 캐시에
저장하지 않고, Docker를 켠 뒤의 실행에서 UP-TO-DATE로 재사용하지도 않습니다.
- CI는 `./gradlew check -PrequireContainerTests=true`로 실행합니다. 이때 테스트를 하나라도
건너뛴 `Test` 작업은 실패합니다. 로컬에서 같은 옵션으로 CI 조건을 재현할 수 있습니다.

## Git Flow

- 기본 브랜치: `develop`
Expand Down
81 changes: 81 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,62 @@ allprojects {
}
}

/**
* Whether a Docker engine accepts connections on an endpoint Testcontainers would try.
*
* Container test classes use `disabledWithoutDocker = true`, so a run without Docker passes with
* them skipped. Such a skip-only result is never stored in the build cache, and as a test input
* this keeps a later run that has Docker from treating it as up-to-date.
*/
abstract class DockerAvailability : ValueSource<Boolean, ValueSourceParameters.None> {
override fun obtain(): Boolean = candidateEndpoints().any(::accepts)

private fun candidateEndpoints(): List<String> {
val home = System.getProperty("user.home")
val userProperties = java.util.Properties()
val userPropertiesFile = java.io.File(home, ".testcontainers.properties")
if (userPropertiesFile.isFile) {
userPropertiesFile.inputStream().use(userProperties::load)
}
return listOfNotNull(
System.getenv("DOCKER_HOST") ?: userProperties.getProperty("docker.host"),
userProperties.getProperty("tc.host"),
"unix:///var/run/docker.sock",
"unix://$home/.docker/run/docker.sock",
"unix://$home/.docker/desktop/docker.sock",
System.getenv("XDG_RUNTIME_DIR")?.let { "unix://$it/docker.sock" },
"npipe:////./pipe/docker_engine",
).filter(String::isNotBlank)
}

private fun accepts(endpoint: String): Boolean = try {
val uri = java.net.URI(endpoint)
when (uri.scheme) {
"unix" -> java.nio.channels.SocketChannel.open(java.net.StandardProtocolFamily.UNIX).use {
it.connect(java.net.UnixDomainSocketAddress.of(uri.path))
}
"tcp", "http", "https" -> java.net.Socket().use {
it.connect(java.net.InetSocketAddress(uri.host, uri.port.takeIf { port -> port > 0 } ?: 2375), 1_000)
true
}
"npipe" -> java.io.File(endpoint.removePrefix("npipe://").replace('/', '\\')).exists()
else -> false
}
} catch (unreachable: Exception) {
false
}
}

val dockerAvailable = providers.of(DockerAvailability::class.java) {}

/**
* CI passes `-PrequireContainerTests=true`: a test task that skipped any test then fails, so a run
* without Docker cannot pass with its container tests skipped. Local runs leave it unset.
*/
val requireContainerTests = providers.gradleProperty("requireContainerTests")
.map(String::toBoolean)
.getOrElse(false)

subprojects {
plugins.withId("java") {
extensions.configure<JavaPluginExtension> {
Expand All @@ -22,6 +78,31 @@ subprojects {

tasks.withType<Test>().configureEach {
useJUnitPlatform()
val docker = dockerAvailable
inputs.property("dockerAvailable", docker)
outputs.cacheIf("Docker is available, so container tests ran") { docker.get() }
if (requireContainerTests) {
val junitXml = reports.junitXml.outputLocation
doLast("rejectSkippedTests") {
val skipped = junitXml.get().asFile
.listFiles { file -> file.name.startsWith("TEST-") && file.name.endsWith(".xml") }
.orEmpty()
.map { report ->
javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(report).documentElement
}
.filter { suite -> (suite.getAttribute("skipped").toIntOrNull() ?: 0) > 0 }
.map { suite -> "${suite.getAttribute("name")} (${suite.getAttribute("skipped")})" }
.sorted()
if (skipped.isNotEmpty()) {
throw GradleException(
"$path skipped tests with -PrequireContainerTests=true. Container tests " +
"need a Docker engine that Testcontainers can reach. Skipped: " +
skipped.joinToString(", "),
)
}
}
}
}
}
}
Expand Down
Loading