From 4d27eb47f75eac9e14b20a77e3c3d5648baaaef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Wed, 6 Oct 2021 21:41:32 +0200 Subject: [PATCH 1/7] Add alias for creating a generic container definition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alejandro Hernández --- .../testcontainers/GenericContainer.scala | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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) + + } + } From e609deb3b65c5ee9e5c65b0cbf25d366aec29687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Fri, 8 Oct 2021 19:34:21 +0200 Subject: [PATCH 2/7] Rename method to `anonymous` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alejandro Hernández --- .../scala/com/dimafeng/testcontainers/GenericContainer.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala index d9c5859e..e5b24e84 100644 --- a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala +++ b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala @@ -100,7 +100,7 @@ object GenericContainer { labels, tmpFsMapping, imagePullPolicy) ) - def apply(dockerImage: DockerImage, + def anonymous(dockerImage: DockerImage, exposedPorts: Seq[Int] = Seq(), env: Map[String, String] = Map(), command: Seq[String] = Seq(), From 40172b9f21898e86eac701b969fe4ba2396c2dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Fri, 22 Oct 2021 06:49:54 +0200 Subject: [PATCH 3/7] Rename `anonymous` to `apply` --- .../scala/com/dimafeng/testcontainers/GenericContainer.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala index e5b24e84..d9c5859e 100644 --- a/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala +++ b/core/src/main/scala/com/dimafeng/testcontainers/GenericContainer.scala @@ -100,7 +100,7 @@ object GenericContainer { labels, tmpFsMapping, imagePullPolicy) ) - def anonymous(dockerImage: DockerImage, + def apply(dockerImage: DockerImage, exposedPorts: Seq[Int] = Seq(), env: Map[String, String] = Map(), command: Seq[String] = Seq(), From 02561ef2d008992f3419ed5883a75961b5a4eecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Fri, 22 Oct 2021 06:50:12 +0200 Subject: [PATCH 4/7] Add test for `GenericContainer.Def.apply()` --- .../testcontainers/GenericContainerSpec.scala | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test-framework/scalatest/src/test/scala/com/dimafeng/testcontainers/GenericContainerSpec.scala 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..b0417126 --- /dev/null +++ b/test-framework/scalatest/src/test/scala/com/dimafeng/testcontainers/GenericContainerSpec.scala @@ -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")) + } +} From 56235f364a2890d7a59b0485b103b7cd026d8f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Fri, 22 Oct 2021 06:50:28 +0200 Subject: [PATCH 5/7] Update README example with new method --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c190b722..bdbbc087 100644 --- a/README.md +++ b/README.md @@ -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 { + 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")) From 4e8e553a6e6dff2eb49d4ebab70ff4dfb1cee2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Sun, 24 Oct 2021 07:10:27 +0200 Subject: [PATCH 6/7] Update `GenericContainer Usage` section instead of the one previously updated --- README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bdbbc087..06b9b863 100644 --- a/README.md +++ b/README.md @@ -181,13 +181,13 @@ The most flexible but less convenient container type is `GenericContainer`. This with custom configuration. ```scala -class GenericContainerSpec extends AnyFlatSpec with TestContainerForAll { - override val containerDef = GenericContainer.Def("nginx:latest", +class GenericContainerSpec extends AnyFlatSpec with ForAllTestContainer { + override val container: GenericContainer = GenericContainer("nginx:latest", exposedPorts = Seq(80), waitStrategy = Wait.forHttp("/") ) - "GenericContainer" should "start nginx and expose 80 port" in withContainers { case container => + "GenericContainer" should "start nginx and expose 80 port" in { 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")) @@ -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: From 96f340487457890aa8e4061c134c5abe2f32d6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Sun, 31 Oct 2021 10:50:49 +0100 Subject: [PATCH 7/7] Add missing type --- .../com/dimafeng/testcontainers/GenericContainerSpec.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index b0417126..8fe7ecb9 100644 --- a/test-framework/scalatest/src/test/scala/com/dimafeng/testcontainers/GenericContainerSpec.scala +++ b/test-framework/scalatest/src/test/scala/com/dimafeng/testcontainers/GenericContainerSpec.scala @@ -5,9 +5,10 @@ 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 = GenericContainer.Def("nginx:latest", + override val containerDef: Def[GenericContainer] = GenericContainer.Def("nginx:latest", exposedPorts = Seq(80), waitStrategy = Wait.forHttp("/") )