diff --git a/README.md b/README.md index c190b722..06b9b863 100644 --- a/README.md +++ b/README.md @@ -455,6 +455,15 @@ object NginxContainer { } ``` +However, if you don't want to create a custom container, you can use `GenericContainer` directly while overriding `containerDef`: + +```scala +override val containerDef = GenericContainer.Def("nginx:latest", + exposedPorts = Seq(80), + waitStrategy = Wait.forHttp("/") +) +``` + ### Migration from the classic API 1. If you have custom containers created with the `GenericContainer`, add `ContainerDef` in the companion like this: diff --git a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala index 71f232a2..d9c5859e 100644 --- a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala +++ b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala @@ -83,4 +83,36 @@ object GenericContainer { override type Container = C protected def createContainer(): C = init } + + 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]( + GenericContainer( + dockerImage, exposedPorts, env, command, classpathResourceMapping, waitStrategy, + labels, tmpFsMapping, 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] = + Default( + dockerImage, exposedPorts, env, command, classpathResourceMapping, waitStrategy, + labels, tmpFsMapping, imagePullPolicy) + + } + } diff --git a/test-framework/scalatest/src/test/scala/com/dimafeng/testcontainers/GenericContainerSpec.scala b/test-framework/scalatest/src/test/scala/com/dimafeng/testcontainers/GenericContainerSpec.scala new file mode 100644 index 00000000..8fe7ecb9 --- /dev/null +++ b/test-framework/scalatest/src/test/scala/com/dimafeng/testcontainers/GenericContainerSpec.scala @@ -0,0 +1,21 @@ +package com.dimafeng.testcontainers + +import org.scalatest.flatspec.AnyFlatSpec +import scala.io.Source +import java.net.URL +import com.dimafeng.testcontainers.scalatest.TestContainerForAll +import org.testcontainers.containers.wait.strategy.Wait +import com.dimafeng.testcontainers.GenericContainer.Def + +class GenericContainerSpec extends AnyFlatSpec with TestContainerForAll { + override val containerDef: Def[GenericContainer] = GenericContainer.Def("nginx:latest", + exposedPorts = Seq(80), + waitStrategy = Wait.forHttp("/") + ) + + "GenericContainer" should "start nginx and expose 80 port" in withContainers { case container => + assert(Source.fromInputStream( + new URL(s"http://${container.containerIpAddress}:${container.mappedPort(80)}/").openConnection().getInputStream + ).mkString.contains("If you see this page, the nginx web server is successfully installed")) + } +}