diff --git a/core/src/main/scala/com/dimafeng/testcontainers/FixedHostPortGenericContainer.scala b/core/src/main/scala/com/dimafeng/testcontainers/FixedHostPortGenericContainer.scala index aee563fa..5fbda2a3 100644 --- a/core/src/main/scala/com/dimafeng/testcontainers/FixedHostPortGenericContainer.scala +++ b/core/src/main/scala/com/dimafeng/testcontainers/FixedHostPortGenericContainer.scala @@ -1,5 +1,6 @@ package com.dimafeng.testcontainers +import com.dimafeng.testcontainers.GenericContainer.FileSystemBind import org.testcontainers.containers.wait.strategy.WaitStrategy import org.testcontainers.containers.{BindMode, FixedHostPortGenericContainer => JavaFixedHostPortGenericContainer} @@ -7,10 +8,11 @@ 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 + exposedContainerPort: Int, + fileSystemBind: Seq[FileSystemBind] = Seq() ) extends SingleContainer[JavaFixedHostPortGenericContainer[_]] { override implicit val container: JavaFixedHostPortGenericContainer[_] = new JavaFixedHostPortGenericContainer(imageName) @@ -22,27 +24,39 @@ class FixedHostPortGenericContainer(imageName: String, if (command.nonEmpty) { container.withCommand(command: _*) } - classpathResourceMapping.foreach{ case (r, c, m) => container.withClasspathResourceMapping(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) } object FixedHostPortGenericContainer { - def apply(imageName: String, - exposedPorts: Seq[Int] = Seq(), - env: Map[String, String] = Map(), - command: Seq[String] = Seq(), - classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(), - waitStrategy: WaitStrategy = null, - exposedHostPort: Int, - exposedContainerPort: Int): FixedHostPortGenericContainer= - new FixedHostPortGenericContainer(imageName, - exposedPorts, - env, - command, - classpathResourceMapping, - Option(waitStrategy), - exposedHostPort, - exposedContainerPort + def apply( + imageName: String, + exposedPorts: Seq[Int] = Seq(), + env: Map[String, String] = Map(), + command: Seq[String] = Seq(), + classpathResourceMapping: Seq[FileSystemBind] = Seq(), + waitStrategy: WaitStrategy = null, + exposedHostPort: Int, + exposedContainerPort: Int, + fileSystemBind: Seq[FileSystemBind] = Seq() + ): FixedHostPortGenericContainer= + new FixedHostPortGenericContainer( + imageName = imageName, + exposedPorts = exposedPorts, + env = env, + command = command, + classpathResourceMapping = classpathResourceMapping, + fileSystemBind = fileSystemBind, + waitStrategy = Option(waitStrategy), + exposedHostPort = exposedHostPort, + exposedContainerPort = exposedContainerPort ) } diff --git a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala index 4c202395..3d57ee3d 100644 --- a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala +++ b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala @@ -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 @@ -20,11 +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 + imagePullPolicy: Option[ImagePullPolicy] = None, + fileSystemBind: Seq[FileSystemBind] = Seq() ) = this({ val underlying: JavaGenericContainer[_] = dockerImage match { case DockerImage(Left(imageFromDockerfile)) => new JavaGenericContainer(imageFromDockerfile) @@ -38,7 +39,14 @@ class GenericContainer( if (command.nonEmpty) { underlying.withCommand(command: _*) } - classpathResourceMapping.foreach{ case (r, c, m) => underlying.withClasspathResourceMapping(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) { @@ -59,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)) @@ -68,17 +77,30 @@ object GenericContainer { DockerImage(Left(imageName)) } - def apply(dockerImage: DockerImage, - exposedPorts: Seq[Int] = Seq(), - env: Map[String, String] = Map(), - command: Seq[String] = Seq(), - classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(), - waitStrategy: WaitStrategy = null, - labels: Map[String, String] = Map.empty, - tmpFsMapping: Map[String, String] = Map.empty, - imagePullPolicy: ImagePullPolicy = null): GenericContainer = - new GenericContainer(dockerImage, exposedPorts, env, command, classpathResourceMapping, Option(waitStrategy), labels, tmpFsMapping, - Option(imagePullPolicy)) + def apply( + dockerImage: DockerImage, + exposedPorts: Seq[Int] = Seq(), + env: Map[String, String] = Map(), + command: Seq[String] = 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[FileSystemBind] = Seq() + ): GenericContainer = + new GenericContainer( + dockerImage = dockerImage, + exposedPorts = exposedPorts, + env = env, + command = command, + classpathResourceMapping = classpathResourceMapping, + fileSystemBind = fileSystemBind, + waitStrategy = Option(waitStrategy), + labels = labels, + tmpFsMapping = tmpFsMapping, + imagePullPolicy = Option(imagePullPolicy) + ) abstract class Def[C <: GenericContainer](init: => C) extends ContainerDef { override type Container = C @@ -87,32 +109,56 @@ object GenericContainer { object Def { - private final case class Default(dockerImage: DockerImage, - exposedPorts: Seq[Int] = Seq(), - env: Map[String, String] = Map(), - command: Seq[String] = Seq(), - classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(), - waitStrategy: WaitStrategy = null, - labels: Map[String, String] = Map.empty, - tmpFsMapping: Map[String, String] = Map.empty, - imagePullPolicy: ImagePullPolicy = null) extends Def[GenericContainer]( + private final case class Default( + dockerImage: DockerImage, + exposedPorts: Seq[Int] = Seq(), + env: Map[String, String] = Map(), + command: Seq[String] = 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[FileSystemBind] = Seq() + ) extends Def[GenericContainer]( GenericContainer( - dockerImage, exposedPorts, env, command, classpathResourceMapping, waitStrategy, - labels, tmpFsMapping, imagePullPolicy) + dockerImage = dockerImage, + exposedPorts = exposedPorts, + env = env, + command = command, + classpathResourceMapping = classpathResourceMapping, + fileSystemBind = fileSystemBind, + waitStrategy = waitStrategy, + labels = labels, + tmpFsMapping = tmpFsMapping, + imagePullPolicy = imagePullPolicy + ) ) - def apply(dockerImage: DockerImage, - exposedPorts: Seq[Int] = Seq(), - env: Map[String, String] = Map(), - command: Seq[String] = Seq(), - classpathResourceMapping: Seq[(String, String, BindMode)] = Seq(), - waitStrategy: WaitStrategy = null, - labels: Map[String, String] = Map.empty, - tmpFsMapping: Map[String, String] = Map.empty, - imagePullPolicy: ImagePullPolicy = null): GenericContainer.Def[GenericContainer] = + def apply( + dockerImage: DockerImage, + exposedPorts: Seq[Int] = Seq(), + env: Map[String, String] = Map(), + command: Seq[String] = 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[FileSystemBind] = Seq() + ): GenericContainer.Def[GenericContainer] = Default( - dockerImage, exposedPorts, env, command, classpathResourceMapping, waitStrategy, - labels, tmpFsMapping, imagePullPolicy) + dockerImage = dockerImage, + exposedPorts = exposedPorts, + env = env, + command = command, + classpathResourceMapping = classpathResourceMapping, + fileSystemBind = fileSystemBind, + waitStrategy = waitStrategy, + labels = labels, + tmpFsMapping = tmpFsMapping, + imagePullPolicy = imagePullPolicy + ) }