Skip to content

Commit

Permalink
Add a case class for FileSystemBind instead of Tuple 3
Browse files Browse the repository at this point in the history
  • Loading branch information
benkio committed Jan 27, 2022
1 parent b0a815f commit 72836ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.dimafeng.testcontainers

import com.dimafeng.testcontainers.GenericContainer.FileSystemBind
import org.testcontainers.containers.wait.strategy.WaitStrategy
import org.testcontainers.containers.{BindMode, FixedHostPortGenericContainer => JavaFixedHostPortGenericContainer}

class FixedHostPortGenericContainer(imageName: String,
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: Option[WaitStrategy] = None,
exposedHostPort: Int,
exposedContainerPort: Int,
fileSystemBind: Seq[(String, String, BindMode)] = Seq()
fileSystemBind: Seq[FileSystemBind] = Seq()
) extends SingleContainer[JavaFixedHostPortGenericContainer[_]] {

override implicit val container: JavaFixedHostPortGenericContainer[_] = new JavaFixedHostPortGenericContainer(imageName)
Expand All @@ -23,8 +24,14 @@ class FixedHostPortGenericContainer(imageName: String,
if (command.nonEmpty) {
container.withCommand(command: _*)
}
classpathResourceMapping.foreach{ case (r, c, m) => container.withClasspathResourceMapping(r, c, m) }
fileSystemBind.foreach{ case (r, c, m) => container.withFileSystemBind(r, c, m) }
classpathResourceMapping.foreach {
case FileSystemBind(hostFilePath, containerFilePath, bindMode) =>
container.withClasspathResourceMapping(hostFilePath, containerFilePath, bindMode)
}
fileSystemBind.foreach {
case FileSystemBind(hostFilePath, containerFilePath, bindMode) =>
container.withFileSystemBind(hostFilePath, containerFilePath, bindMode)
}
waitStrategy.foreach(container.waitingFor)
container.withFixedExposedPort(exposedHostPort, exposedContainerPort)
}
Expand All @@ -35,11 +42,11 @@ object FixedHostPortGenericContainer {
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
exposedHostPort: Int,
exposedContainerPort: Int,
fileSystemBind: Seq[(String, String, BindMode)] = Seq()
fileSystemBind: Seq[FileSystemBind] = Seq()
): FixedHostPortGenericContainer=
new FixedHostPortGenericContainer(
imageName = imageName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.dimafeng.testcontainers

import java.util.concurrent.Future

import com.dimafeng.testcontainers.GenericContainer.DockerImage
import com.dimafeng.testcontainers.GenericContainer.{FileSystemBind, DockerImage}
import org.testcontainers.containers.wait.strategy.WaitStrategy
import org.testcontainers.containers.{BindMode, GenericContainer => JavaGenericContainer}
import org.testcontainers.images.ImagePullPolicy
Expand All @@ -20,12 +20,12 @@ class GenericContainer(
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: Option[WaitStrategy] = None,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: Option[ImagePullPolicy] = None,
fileSystemBind: Seq[(String, String, BindMode)] = Seq()
fileSystemBind: Seq[FileSystemBind] = Seq()
) = this({
val underlying: JavaGenericContainer[_] = dockerImage match {
case DockerImage(Left(imageFromDockerfile)) => new JavaGenericContainer(imageFromDockerfile)
Expand All @@ -39,8 +39,14 @@ class GenericContainer(
if (command.nonEmpty) {
underlying.withCommand(command: _*)
}
classpathResourceMapping.foreach{ case (r, c, m) => underlying.withClasspathResourceMapping(r, c, m) }
fileSystemBind.foreach{ case (r, c, m) => underlying.withFileSystemBind(r, c, m) }
classpathResourceMapping.foreach {
case FileSystemBind(hostFilePath, containerFilePath, bindMode) =>
underlying.withClasspathResourceMapping(hostFilePath, containerFilePath, bindMode)
}
fileSystemBind.foreach {
case FileSystemBind(hostFilePath, containerFilePath, bindMode) =>
underlying.withFileSystemBind(hostFilePath, containerFilePath, bindMode)
}
waitStrategy.foreach(underlying.waitingFor)

if (labels.nonEmpty) {
Expand All @@ -61,6 +67,7 @@ class GenericContainer(

object GenericContainer {
case class DockerImage(image: Either[String, Future[String]])
case class FileSystemBind(hostFilePath: String, containerFilePath: String, bindMode: BindMode)

implicit def javaFutureToDockerImage(javaFuture: Future[String]): DockerImage = {
DockerImage(Right(javaFuture))
Expand All @@ -75,12 +82,12 @@ object GenericContainer {
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: ImagePullPolicy = null,
fileSystemBind: Seq[(String, String, BindMode)] = Seq()
fileSystemBind: Seq[FileSystemBind] = Seq()
): GenericContainer =
new GenericContainer(
dockerImage = dockerImage,
Expand All @@ -107,12 +114,12 @@ object GenericContainer {
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: ImagePullPolicy = null,
fileSystemBind: Seq[(String, String, BindMode)] = Seq()
fileSystemBind: Seq[FileSystemBind] = Seq()
) extends Def[GenericContainer](
GenericContainer(
dockerImage = dockerImage,
Expand All @@ -133,12 +140,12 @@ object GenericContainer {
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: ImagePullPolicy = null,
fileSystemBind: Seq[(String, String, BindMode)] = Seq()
fileSystemBind: Seq[FileSystemBind] = Seq()
): GenericContainer.Def[GenericContainer] =
Default(
dockerImage = dockerImage,
Expand Down

0 comments on commit 72836ea

Please sign in to comment.