Skip to content

Commit

Permalink
Merge pull request #279 from mikhailchuryakov/fixedhostport-container…
Browse files Browse the repository at this point in the history
…-def-pattern

Add Def pattern for FixedHostPortGenericContainer and seq port bindings
  • Loading branch information
dimafeng authored May 13, 2024
2 parents 43d5537 + 5b52aa2 commit 51bafdd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ package com.dimafeng.testcontainers
import com.dimafeng.testcontainers.GenericContainer.FileSystemBind
import org.testcontainers.containers.startupcheck.StartupCheckStrategy
import org.testcontainers.containers.wait.strategy.WaitStrategy
import org.testcontainers.containers.{BindMode, FixedHostPortGenericContainer => JavaFixedHostPortGenericContainer}
import org.testcontainers.containers.{FixedHostPortGenericContainer => JavaFixedHostPortGenericContainer}

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

override implicit val container: JavaFixedHostPortGenericContainer[_] = new JavaFixedHostPortGenericContainer(imageName)

if (exposedPorts.nonEmpty) {
container.withExposedPorts(exposedPorts.map(int2Integer): _*)
}
env.foreach{ case (k, v) => container.withEnv(k, v) }
env.foreach { case (k, v) => container.withEnv(k, v) }
if (command.nonEmpty) {
container.withCommand(command: _*)
}
Expand All @@ -36,21 +35,47 @@ class FixedHostPortGenericContainer(imageName: String,
}
waitStrategy.foreach(container.waitingFor)
startupCheckStrategy.foreach(container.withStartupCheckStrategy)
container.withFixedExposedPort(exposedHostPort, exposedContainerPort)
portBindings.foreach { case (hostPort, containerPort) => container.withFixedExposedPort(hostPort, containerPort) }
}

object FixedHostPortGenericContainer {

case class Def(imageName: String,
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: Option[WaitStrategy] = None,
fileSystemBind: Seq[FileSystemBind] = Seq(),
startupCheckStrategy: Option[StartupCheckStrategy] = None,
portBindings: Seq[(Int, Int)]
) extends ContainerDef {
override type Container = FixedHostPortGenericContainer

override protected def createContainer(): FixedHostPortGenericContainer =
new FixedHostPortGenericContainer(
imageName = imageName,
exposedPorts = exposedPorts,
env = env,
command = command,
classpathResourceMapping = classpathResourceMapping,
waitStrategy = waitStrategy,
fileSystemBind = fileSystemBind,
startupCheckStrategy = startupCheckStrategy,
portBindings = portBindings
)
}

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=
imageName: String,
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
fileSystemBind: Seq[FileSystemBind] = Seq(),
portBindings: Seq[(Int, Int)] = Seq()
): FixedHostPortGenericContainer =
new FixedHostPortGenericContainer(
imageName = imageName,
exposedPorts = exposedPorts,
Expand All @@ -59,7 +84,6 @@ object FixedHostPortGenericContainer {
classpathResourceMapping = classpathResourceMapping,
fileSystemBind = fileSystemBind,
waitStrategy = Option(waitStrategy),
exposedHostPort = exposedHostPort,
exposedContainerPort = exposedContainerPort
portBindings = portBindings
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import scala.io.Source
class FixedHostPortContainerSpec extends AnyFlatSpec with ForAllTestContainer {
override val container: FixedHostPortGenericContainer = FixedHostPortGenericContainer("nginx:latest",
waitStrategy = new HttpWaitStrategy().forPath("/"),
exposedHostPort = 8090,
exposedContainerPort = 80
portBindings = Seq((8090, 80))
)

"FixedHostPortGenericContainer" should "start nginx and expose 8090 port on host" in {
Expand Down

0 comments on commit 51bafdd

Please sign in to comment.