-
-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add More Config Options to Docker Contrib Module #1456
Merged
lefou
merged 4 commits into
com-lihaoyi:main
from
LaurenceWarne:add-more-docker-config-options2
Sep 18, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
434e84c
Add more dockerfile options to the docker contrib module
LaurenceWarne a5fc21f
Add tests for docker module
LaurenceWarne b7e35f7
Update docker contrib module documentation
LaurenceWarne 744c68d
Enable use of alternate docker executables in docker plugin
LaurenceWarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
object Main extends App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package mill | ||
package contrib.docker | ||
|
||
import mill.api.PathRef | ||
import mill.scalalib.JavaModule | ||
import mill.util.{TestEvaluator, TestUtil} | ||
import os.Path | ||
import utest._ | ||
import utest.framework.TestPath | ||
|
||
object DockerModuleTest extends TestSuite { | ||
|
||
private def testExecutable = | ||
if (isInstalled("podman")) "podman" | ||
else "docker" | ||
|
||
object Docker extends TestUtil.BaseModule with JavaModule with DockerModule { | ||
|
||
override def millSourcePath = TestUtil.getSrcPathStatic() | ||
override def artifactName = testArtifactName | ||
|
||
object dockerDefault extends DockerConfig { | ||
override def executable = testExecutable | ||
} | ||
|
||
object dockerAll extends DockerConfig { | ||
override def baseImage = "docker.io/openjdk:11" | ||
override def labels = Map("version" -> "1.0") | ||
override def exposedPorts = Seq(8080, 443) | ||
override def exposedUdpPorts = Seq(80) | ||
override def volumes = Seq("/v1", "/v2") | ||
override def envVars = Map("foo" -> "bar", "foobar" -> "barfoo") | ||
override def run = Seq( | ||
"/bin/bash -c 'echo Hello World!'", | ||
"useradd -ms /bin/bash user1" | ||
) | ||
override def user = "user1" | ||
override def executable = testExecutable | ||
} | ||
} | ||
|
||
val testArtifactName = "mill-docker-contrib-test" | ||
|
||
val testModuleSourcesPath: Path = | ||
os.pwd / "contrib" / "docker" / "test" / "resources" / "docker" | ||
|
||
val multineRegex = "\\R+".r | ||
|
||
private def isInstalled(executable: String): Boolean = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
val getPathCmd = if (scala.util.Properties.isWin) "where" else "which" | ||
os.proc(getPathCmd, executable).call(check = false).exitCode == 0 | ||
} | ||
|
||
private def workspaceTest(m: TestUtil.BaseModule)(t: TestEvaluator => Unit)( | ||
implicit tp: TestPath | ||
): Unit = { | ||
if (isInstalled(testExecutable) && !scala.util.Properties.isWin) { | ||
val eval = new TestEvaluator(m) | ||
os.remove.all(m.millSourcePath) | ||
os.remove.all(eval.outPath) | ||
os.makeDir.all(m.millSourcePath / os.up) | ||
os.copy(testModuleSourcesPath, m.millSourcePath) | ||
t(eval) | ||
} else { | ||
val identifier = tp.value.mkString("/") | ||
println(s"Skipping '$identifier' since no docker installation was found") | ||
assert(true) | ||
} | ||
} | ||
|
||
override def utestAfterAll(): Unit = { | ||
if (isInstalled(testExecutable) && !scala.util.Properties.isWin) | ||
os | ||
.proc(testExecutable, "rmi", testArtifactName) | ||
.call(stdout = os.Inherit, stderr = os.Inherit) | ||
else () | ||
} | ||
|
||
def tests = Tests { | ||
|
||
test("docker build") { | ||
"default options" - workspaceTest(Docker) { eval => | ||
val Right((imageName :: Nil, _)) = eval(Docker.dockerDefault.build) | ||
assert(imageName == testArtifactName) | ||
} | ||
|
||
"all options" - workspaceTest(Docker) { eval => | ||
val Right((imageName :: Nil, _)) = eval(Docker.dockerAll.build) | ||
assert(imageName == testArtifactName) | ||
} | ||
} | ||
|
||
test("dockerfile contents") { | ||
"default options" - { | ||
val eval = new TestEvaluator(Docker) | ||
val Right((dockerfileString, _)) = eval(Docker.dockerDefault.dockerfile) | ||
val expected = multineRegex.replaceAllIn( | ||
""" | ||
|FROM gcr.io/distroless/java:latest | ||
|COPY out.jar /out.jar | ||
|ENTRYPOINT ["java", "-jar", "/out.jar"]""".stripMargin, | ||
sys.props("line.separator") | ||
) | ||
val dockerfileStringRefined = multineRegex.replaceAllIn( | ||
dockerfileString, | ||
sys.props("line.separator") | ||
) | ||
assert(dockerfileStringRefined == expected) | ||
} | ||
|
||
"all options" - { | ||
val eval = new TestEvaluator(Docker) | ||
val Right((dockerfileString, _)) = eval(Docker.dockerAll.dockerfile) | ||
val expected = multineRegex.replaceAllIn( | ||
""" | ||
|FROM docker.io/openjdk:11 | ||
|LABEL "version"="1.0" | ||
|EXPOSE 8080/tcp 443/tcp | ||
|EXPOSE 80/udp | ||
|ENV foo=bar | ||
|ENV foobar=barfoo | ||
|VOLUME ["/v1", "/v2"] | ||
|RUN /bin/bash -c 'echo Hello World!' | ||
|RUN useradd -ms /bin/bash user1 | ||
|USER user1 | ||
|COPY out.jar /out.jar | ||
|ENTRYPOINT ["java", "-jar", "/out.jar"]""".stripMargin, | ||
sys.props("line.separator") | ||
) | ||
val dockerfileStringRefined = multineRegex.replaceAllIn( | ||
dockerfileString, | ||
sys.props("line.separator") | ||
) | ||
assert(dockerfileStringRefined == expected) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've written these based on the other contrib module test suites, let me know if there's anything untoward here.