Skip to content

Commit

Permalink
Merge pull request #206 from benkio/205_AddWithFileSystemBind
Browse files Browse the repository at this point in the history
Add `withFileSystemBind` + name parameters
  • Loading branch information
dimafeng authored Jan 28, 2022
2 parents d00f8f3 + 72836ea commit e327921
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +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
exposedContainerPort: Int,
fileSystemBind: Seq[FileSystemBind] = Seq()
) extends SingleContainer[JavaFixedHostPortGenericContainer[_]] {

override implicit val container: JavaFixedHostPortGenericContainer[_] = new JavaFixedHostPortGenericContainer(imageName)
Expand All @@ -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
)
}
120 changes: 83 additions & 37 deletions core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala
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,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)
Expand All @@ -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) {
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
)

}

Expand Down

0 comments on commit e327921

Please sign in to comment.