|
| 1 | +package dev.codebandits.container.gradle |
| 2 | + |
| 3 | +import dev.codebandits.container.gradle.helpers.appendLine |
| 4 | +import org.gradle.testkit.runner.GradleRunner |
| 5 | +import org.gradle.testkit.runner.TaskOutcome |
| 6 | +import org.junit.jupiter.api.Test |
| 7 | +import strikt.api.expectThat |
| 8 | +import strikt.assertions.contains |
| 9 | +import strikt.assertions.isEqualTo |
| 10 | +import strikt.assertions.isNotNull |
| 11 | +import strikt.assertions.isTrue |
| 12 | +import java.util.UUID |
| 13 | + |
| 14 | +class DockerPullTest : GradleProjectTest() { |
| 15 | + |
| 16 | + @Test |
| 17 | + fun `dockerPull pulls the specified image`() { |
| 18 | + removeImage("alpine:3.18.9") |
| 19 | + |
| 20 | + buildGradleKtsFile.appendLine( |
| 21 | + """ |
| 22 | + import dev.codebandits.container.gradle.tasks.ContainerRunTask |
| 23 | + |
| 24 | + plugins { |
| 25 | + id("dev.codebandits.container") |
| 26 | + } |
| 27 | + |
| 28 | + tasks { |
| 29 | + register<ContainerRunTask>("pullAlpineImage") { |
| 30 | + dockerPull { |
| 31 | + image = "alpine:3.18.9" |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + """.trimIndent() |
| 36 | + ) |
| 37 | + |
| 38 | + val result = GradleRunner.create() |
| 39 | + .withPluginClasspath() |
| 40 | + .withProjectDir(projectDirectory.toFile()) |
| 41 | + .withArguments("pullAlpineImage") |
| 42 | + .build() |
| 43 | + |
| 44 | + expectThat(result).and { |
| 45 | + get { task(":pullAlpineImage") }.isNotNull().get { outcome }.isEqualTo(TaskOutcome.SUCCESS) |
| 46 | + } |
| 47 | + |
| 48 | + expectThat(imageExists("alpine:3.18.9")).isTrue() |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `dockerPull fails when pulling an image that does not exist`() { |
| 53 | + buildGradleKtsFile.appendLine( |
| 54 | + """ |
| 55 | + import dev.codebandits.container.gradle.tasks.ContainerRunTask |
| 56 | + |
| 57 | + plugins { |
| 58 | + id("dev.codebandits.container") |
| 59 | + } |
| 60 | + |
| 61 | + tasks { |
| 62 | + register<ContainerRunTask>("pullImageNotExist") { |
| 63 | + dockerPull { |
| 64 | + image = "alpine:${UUID.randomUUID()}" |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + """.trimIndent() |
| 69 | + ) |
| 70 | + |
| 71 | + val result = GradleRunner.create() |
| 72 | + .withPluginClasspath() |
| 73 | + .withProjectDir(projectDirectory.toFile()) |
| 74 | + .withArguments("pullImageNotExist") |
| 75 | + .buildAndFail() |
| 76 | + |
| 77 | + expectThat(result).and { |
| 78 | + get { task(":pullImageNotExist") }.isNotNull().get { outcome }.isEqualTo(TaskOutcome.FAILED) |
| 79 | + get { output }.contains("manifest unknown") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private fun removeImage(imageReference: String) { |
| 84 | + ProcessBuilder("docker", "rmi", "-f", imageReference) |
| 85 | + .inheritIO() |
| 86 | + .start() |
| 87 | + .waitFor() |
| 88 | + } |
| 89 | + |
| 90 | + private fun imageExists(imageReference: String): Boolean { |
| 91 | + val process = ProcessBuilder("docker", "image", "inspect", imageReference) |
| 92 | + .redirectErrorStream(true) |
| 93 | + .start() |
| 94 | + val exitCode = process.waitFor() |
| 95 | + return exitCode == 0 |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments