Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alias for creating a generic container definition #194

Merged
merged 8 commits into from
Nov 1, 2021
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ The most flexible but less convenient container type is `GenericContainer`. This
with custom configuration.

```scala
class GenericContainerSpec extends AnyFlatSpec with ForAllTestContainer {
override val container: GenericContainer = GenericContainer("nginx:latest",
class GenericContainerSpec extends AnyFlatSpec with TestContainerForAll {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update this section instead - https://github.com/testcontainers/testcontainers-scala#genericcontainer-usage

I'd like to keep it consistent until we finally migrate to one way of doing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here: 4e8e553

override val containerDef = GenericContainer.Def("nginx:latest",
exposedPorts = Seq(80),
waitStrategy = Wait.forHttp("/")
)

"GenericContainer" should "start nginx and expose 80 port" in {
"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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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

class GenericContainerSpec extends AnyFlatSpec with TestContainerForAll {
override val containerDef = 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"))
}
}