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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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,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"))
}
}