Skip to content

Commit

Permalink
Refactor platform test container configuration
Browse files Browse the repository at this point in the history
Authored-by: Leonhardt Koepsell <[email protected]>
  • Loading branch information
lnhrdt committed Nov 3, 2024
1 parent d26dfaa commit a825b2f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package dev.codebandits

import org.testcontainers.containers.GenericContainer
import org.testcontainers.images.builder.ImageFromDockerfile
import org.testcontainers.images.builder.dockerfile.DockerfileBuilder

object ImageFixtures {
private val imageCache = mutableMapOf<Any, ImageFromDockerfile>()

enum class JavaVersion {
TEMURIN_22,
TEMURIN_21,
TEMURIN_20,
OPENJDK_17,
OPENJDK_11,
object ContainerProvider {
fun dockerJavaGradle(
dockerVersion: String,
javaVersion: JavaVersion,
gradleVersion: String,
): GenericContainer<*> {
val image = ImageFixtures.dockerJavaGradle(
dockerVersion = dockerVersion,
javaVersion = javaVersion,
gradleVersion = gradleVersion,
)
val container: GenericContainer<*> = GenericContainer(image)
if (System.getenv("CI") == "true") {
container.withStartupAttempts(3)
}
return container
}
}

private object ImageFixtures {
private val imageCache = mutableMapOf<List<Any>, ImageFromDockerfile>()

fun dockerTemurinGradle(
fun dockerJavaGradle(
dockerVersion: String,
javaVersion: JavaVersion,
gradleVersion: String,
): ImageFromDockerfile {
val imageIdentifier = Pair("docker-temurin-gradle", Triple(dockerVersion, javaVersion, gradleVersion))
val imageIdentifier = listOf("docker-java-gradle", dockerVersion, javaVersion, gradleVersion)
return imageCache.getOrPut(imageIdentifier) {
ImageFromDockerfile()
.withDockerfileFromBuilder { builder ->
Expand Down
13 changes: 4 additions & 9 deletions src/testPlatforms/kotlin/dev/codebandits/DockerHostPathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import dev.codebandits.helpers.configureBuildGradleKtsPluginFromLibsDir
import dev.codebandits.helpers.setupPluginLibsDir
import org.junit.jupiter.api.Test
import org.testcontainers.containers.BindMode
import org.testcontainers.containers.GenericContainer
import org.testcontainers.utility.MountableFile
import strikt.api.expectThat
import strikt.assertions.contains
Expand All @@ -32,13 +31,11 @@ class DockerHostPathTest : GradleProjectTest() {
}
""".trimIndent()
)
val image = ImageFixtures.dockerTemurinGradle(
val container = ContainerProvider.dockerJavaGradle(
dockerVersion = "27",
javaVersion = ImageFixtures.JavaVersion.TEMURIN_21,
javaVersion = JavaVersion.TEMURIN_21,
gradleVersion = "8.10.2",
)
val container = GenericContainer(image)
.withStartupAttempts(3)
.withFileSystemBind("/var/run/docker.sock", "/var/run/custom.sock", BindMode.READ_ONLY)
.withCopyFileToContainer(MountableFile.forHostPath(projectDirectory), "/project")
.withWorkingDirectory("/project")
Expand Down Expand Up @@ -74,13 +71,11 @@ class DockerHostPathTest : GradleProjectTest() {
""".trimIndent()
)

val image = ImageFixtures.dockerTemurinGradle(
val container = ContainerProvider.dockerJavaGradle(
dockerVersion = "27",
javaVersion = ImageFixtures.JavaVersion.TEMURIN_21,
javaVersion = JavaVersion.TEMURIN_21,
gradleVersion = "8.10.2",
)
val container = GenericContainer(image)
.withStartupAttempts(3)
.withFileSystemBind("/var/run/docker.sock", "/var/run/docker.sock", BindMode.READ_ONLY)
.withCopyFileToContainer(MountableFile.forHostPath(projectDirectory), "/project")
.withWorkingDirectory("/project")
Expand Down
9 changes: 9 additions & 0 deletions src/testPlatforms/kotlin/dev/codebandits/JavaVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.codebandits

enum class JavaVersion {
TEMURIN_22,
TEMURIN_21,
TEMURIN_20,
OPENJDK_17,
OPENJDK_11,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.codebandits.helpers.setupPluginLibsDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.testcontainers.containers.BindMode
import org.testcontainers.containers.GenericContainer
import org.testcontainers.utility.MountableFile
import strikt.api.expectThat
import strikt.assertions.contains
Expand All @@ -19,7 +18,7 @@ class VersionCompatabilityTest : GradleProjectTest() {
"27, TEMURIN_21, 8.10.2",
"26, OPENJDK_17, 7.6.4",
)
fun `run dockerRun`(dockerVersion: String, javaVersion: ImageFixtures.JavaVersion, gradleVersion: String) {
fun `run dockerRun`(dockerVersion: String, javaVersion: JavaVersion, gradleVersion: String) {
setupPluginLibsDir()
buildGradleFile.configureBuildGradlePluginFromLibsDir()
buildGradleFile.appendLine(
Expand All @@ -33,13 +32,11 @@ class VersionCompatabilityTest : GradleProjectTest() {
}
""".trimIndent()
)
val image = ImageFixtures.dockerTemurinGradle(
val container = ContainerProvider.dockerJavaGradle(
dockerVersion = dockerVersion,
javaVersion = javaVersion,
gradleVersion = gradleVersion,
)
val container = GenericContainer(image)
.withStartupAttempts(3)
.withFileSystemBind("/var/run/docker.sock", "/var/run/docker.sock", BindMode.READ_ONLY)
.withCopyFileToContainer(MountableFile.forHostPath(projectDirectory), "/project")
.withWorkingDirectory("/project")
Expand Down

0 comments on commit a825b2f

Please sign in to comment.