Skip to content

Commit

Permalink
merged from master
Browse files Browse the repository at this point in the history
  • Loading branch information
Seetaramayya committed Apr 24, 2024
1 parent fb70abb commit 80ea4e5
Showing 1 changed file with 63 additions and 66 deletions.
129 changes: 63 additions & 66 deletions src/sbt-test/docker/file-permission/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ lazy val checkDockerfileWithStrategyRun = taskKey[Unit]("")
lazy val checkDockerfileWithStrategyCopyChown = taskKey[Unit]("")
lazy val checkDockerfileWithWriteExecute = taskKey[Unit]("")

lazy val root =
(project in file("."))
.enablePlugins(DockerPlugin, JavaAppPackaging)
.settings(
name := "file-permission-test",
version := "0.1.0",
checkDockerfileDefaults := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(List(lines.init.last), """LABEL snp-multi-stage="intermediate"""".stripMargin.linesIterator.toList)
assert(lines.last.substring(0, 25) == "LABEL snp-multi-stage-id=") // random generated id is hard to test
assertEquals(
lines.dropRight(2),
"""FROM fabric8/java-centos-openjdk8-jdk as stage0
|WORKDIR /opt/docker
lazy val root = (project in file("."))
.enablePlugins(DockerPlugin, JavaAppPackaging)
.settings(
name := "file-permission-test",
version := "0.1.0",
checkDockerfileDefaults := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines.take(2),
"""FROM fabric8/java-centos-openjdk8-jdk as stage0
|LABEL snp-multi-stage="intermediate"""".stripMargin.linesIterator.toList)
assert(lines(2).substring(0, 25) == "LABEL snp-multi-stage-id=") // random generated id is hard to test
assertEquals(lines.drop(3),
"""WORKDIR /opt/docker
|COPY 2/opt /2/opt
|COPY 4/opt /4/opt
|USER root
Expand All @@ -35,47 +34,44 @@ lazy val root =
|COPY --from=stage0 --chown=demiourgos728:root /4/opt/docker /opt/docker
|USER 1001:0
|ENTRYPOINT ["/opt/docker/bin/file-permission-test"]
|CMD []""".stripMargin.linesIterator.toList
)
},
checkDockerfileWithStrategyNone := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(
lines,
"""FROM fabric8/java-centos-openjdk8-jdk as mainstage
|CMD []""".stripMargin.linesIterator.toList)
},

checkDockerfileWithStrategyNone := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM fabric8/java-centos-openjdk8-jdk as mainstage
|USER root
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 0 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 0 root || addgroup -g 0 -S root )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 || adduser -S -u 1001 -G root demiourgos728 ))
|WORKDIR /opt/docker
|COPY 2/opt /opt
|COPY 4/opt /opt
|USER 1001:0
|ENTRYPOINT ["/opt/docker/bin/file-permission-test"]
|CMD []""".stripMargin.linesIterator.toList
)
},
checkDockerfileWithStrategyNoneGid := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(
lines,
"""FROM fabric8/java-centos-openjdk8-jdk as mainstage
|CMD []""".stripMargin.linesIterator.toList)
},

checkDockerfileWithStrategyNoneGid := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM fabric8/java-centos-openjdk8-jdk as mainstage
|USER root
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 5000 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 5000 sbt || addgroup -g 5000 -S sbt )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 5000 demiourgos728 || adduser -S -u 1001 -G sbt demiourgos728 ))
|WORKDIR /opt/docker
|COPY 2/opt /opt
|COPY 4/opt /opt
|USER 1001:5000
|ENTRYPOINT ["/opt/docker/bin/file-permission-test"]
|CMD []""".stripMargin.linesIterator.toList
)
},
checkDockerfileWithStrategyRun := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(
lines,
"""FROM openjdk:8 as mainstage
|CMD []""".stripMargin.linesIterator.toList)
},

checkDockerfileWithStrategyRun := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM openjdk:8 as mainstage
|USER root
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 0 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 0 root || addgroup -g 0 -S root )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 || adduser -S -u 1001 -G root demiourgos728 ))
|WORKDIR /opt/docker
Expand All @@ -85,29 +81,31 @@ lazy val root =
|RUN ["chmod", "u+x,g+x", "/opt/docker/bin/file-permission-test"]
|USER 1001:0
|ENTRYPOINT ["/opt/docker/bin/file-permission-test"]
|CMD []""".stripMargin.linesIterator.toList
)
},
checkDockerfileWithStrategyCopyChown := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines, """FROM fabric8/java-centos-openjdk8-jdk as mainstage
|CMD []""".stripMargin.linesIterator.toList)
},

checkDockerfileWithStrategyCopyChown := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM fabric8/java-centos-openjdk8-jdk as mainstage
|WORKDIR /opt/docker
|COPY --chown=daemon:root 2/opt /opt
|COPY --chown=daemon:root 4/opt /opt
|USER daemon
|ENTRYPOINT ["/opt/docker/bin/file-permission-test"]
|CMD []""".stripMargin.linesIterator.toList)
},
checkDockerfileWithWriteExecute := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(List(lines.init.last), """LABEL snp-multi-stage="intermediate"""".stripMargin.linesIterator.toList)
assert(lines.last.substring(0, 25) == "LABEL snp-multi-stage-id=") // random generated id is hard to test
assertEquals(
lines.dropRight(2),
"""FROM fabric8/java-centos-openjdk8-jdk as stage0
|WORKDIR /opt/docker
},

checkDockerfileWithWriteExecute := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines.take(2),
"""FROM fabric8/java-centos-openjdk8-jdk as stage0
|LABEL snp-multi-stage="intermediate"""".stripMargin.linesIterator.toList)
assert(lines(2).substring(0, 25) == "LABEL snp-multi-stage-id=") // random generated id is hard to test
assertEquals(lines.drop(3),
"""WORKDIR /opt/docker
|COPY 2/opt /2/opt
|COPY 4/opt /4/opt
|USER root
Expand All @@ -123,14 +121,13 @@ lazy val root =
|COPY --from=stage0 --chown=demiourgos728:root /4/opt/docker /opt/docker
|USER 1001:0
|ENTRYPOINT ["/opt/docker/bin/file-permission-test"]
|CMD []""".stripMargin.linesIterator.toList
)
}
)
|CMD []""".stripMargin.linesIterator.toList)
}
)

def assertEquals(left: List[String], right: List[String]) =
assert(left == right, "\n" + ((left zip right) flatMap {
case (a: String, b: String) =>
assert(left == right,
"\n" + ((left zip right) flatMap { case (a: String, b: String) =>
if (a == b) Nil
else List("- " + a, "+ " + b)
}).mkString("\n"))
}).mkString("\n"))

0 comments on commit 80ea4e5

Please sign in to comment.