|
| 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 | + |
| 12 | +class DockerRunUserTest : GradleProjectTest() { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun `dockerRun user is set to guest`() { |
| 16 | + buildGradleKtsFile.appendLine( |
| 17 | + """ |
| 18 | + import dev.codebandits.container.gradle.tasks.ContainerRunTask |
| 19 | + |
| 20 | + plugins { |
| 21 | + id("dev.codebandits.container") |
| 22 | + } |
| 23 | + |
| 24 | + tasks { |
| 25 | + register<ContainerRunTask>("whoami") { |
| 26 | + dockerRun { |
| 27 | + image = "alpine:latest" |
| 28 | + user = "guest" |
| 29 | + entrypoint = "whoami" |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + """.trimIndent() |
| 34 | + ) |
| 35 | + |
| 36 | + val result = GradleRunner.create() |
| 37 | + .withPluginClasspath() |
| 38 | + .withProjectDir(projectDirectory.toFile()) |
| 39 | + .withArguments("whoami") |
| 40 | + .build() |
| 41 | + |
| 42 | + expectThat(result).and { |
| 43 | + get { task(":whoami") }.isNotNull().get { outcome }.isEqualTo(TaskOutcome.SUCCESS) |
| 44 | + get { output }.contains("guest") |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `dockerRun user is set to root`() { |
| 50 | + buildGradleKtsFile.appendLine( |
| 51 | + """ |
| 52 | + import dev.codebandits.container.gradle.tasks.ContainerRunTask |
| 53 | + |
| 54 | + plugins { |
| 55 | + id("dev.codebandits.container") |
| 56 | + } |
| 57 | + |
| 58 | + tasks { |
| 59 | + register<ContainerRunTask>("whoami") { |
| 60 | + dockerRun { |
| 61 | + image = "alpine:latest" |
| 62 | + user = "root" |
| 63 | + entrypoint = "whoami" |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + """.trimIndent() |
| 68 | + ) |
| 69 | + |
| 70 | + val result = GradleRunner.create() |
| 71 | + .withPluginClasspath() |
| 72 | + .withProjectDir(projectDirectory.toFile()) |
| 73 | + .withArguments("whoami") |
| 74 | + .build() |
| 75 | + |
| 76 | + expectThat(result).and { |
| 77 | + get { task(":whoami") }.isNotNull().get { outcome }.isEqualTo(TaskOutcome.SUCCESS) |
| 78 | + get { output }.contains("root") |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments