diff --git a/src/test/resources/docker-compose.yml b/src/test/resources/docker-compose.yml index 8840e8a..ddd3454 100644 --- a/src/test/resources/docker-compose.yml +++ b/src/test/resources/docker-compose.yml @@ -4,11 +4,11 @@ services: image: redis:latest hostname: redis-master ports: - - '6379:6379' + - '${REDIS_MASTER_PORT}:6379' redis-slave: image: redis:latest hostname: redis-slave ports: - - '6479:6379' + - '${REDIS_SLAVE_PORT}:6379' command: redis-server --slaveof redis-master 6379 diff --git a/src/test/scala/play/api/cache/redis/test/ForAllTestContainer.scala b/src/test/scala/play/api/cache/redis/test/ForAllTestContainer.scala index 1a92f9c..34b42f7 100644 --- a/src/test/scala/play/api/cache/redis/test/ForAllTestContainer.scala +++ b/src/test/scala/play/api/cache/redis/test/ForAllTestContainer.scala @@ -1,16 +1,26 @@ package play.api.cache.redis.test -import com.dimafeng.testcontainers.SingleContainer +import com.dimafeng.testcontainers.ContainerDef +import com.dimafeng.testcontainers.lifecycle.Stoppable import org.scalatest.{BeforeAndAfterAll, Suite} +import org.testcontainers.lifecycle.Startable trait ForAllTestContainer extends BeforeAndAfterAll { this: Suite => - protected def newContainer: SingleContainer[?] + protected type TestContainer <: Startable with Stoppable - final protected lazy val container = newContainer + protected type TestContainerDef[C <: Startable with Stoppable] = ContainerDef { type Container = C } + + protected def newContainer: TestContainerDef[TestContainer] + + final protected def container: TestContainer = containerHandle.getOrElse { + throw new IllegalStateException("Container not yet started") + } + + final private var containerHandle: Option[TestContainer] = None override def beforeAll(): Unit = - container.start() + containerHandle = Some(newContainer.start()) override def afterAll(): Unit = container.stop() diff --git a/src/test/scala/play/api/cache/redis/test/RedisContainer.scala b/src/test/scala/play/api/cache/redis/test/RedisContainer.scala index 2ef26b9..e91b2b7 100644 --- a/src/test/scala/play/api/cache/redis/test/RedisContainer.scala +++ b/src/test/scala/play/api/cache/redis/test/RedisContainer.scala @@ -9,19 +9,21 @@ import scala.annotation.nowarn trait RedisContainer extends ForAllTestContainer { this: Suite => + final override protected type TestContainer = GenericContainer + protected def redisConfig: RedisContainerConfig private lazy val config = redisConfig @nowarn("cat=deprecation") @SuppressWarnings(Array("org.wartremover.warts.ForeachEntry")) - final override protected val newContainer: GenericContainer = { + final override protected val newContainer: TestContainerDef[TestContainer] = { val container: FixedHostPortGenericContainer[?] = new FixedHostPortGenericContainer(config.redisDockerImage) container.withExposedPorts(config.redisMappedPorts.map(int2Integer): _*) config.redisEnvironment.foreach { case (k, v) => container.withEnv(k, v) } container.waitingFor(Wait.forListeningPorts(config.redisMappedPorts ++ config.redisFixedPorts: _*)) config.redisFixedPorts.foreach(port => container.withFixedExposedPort(port, port)) - new GenericContainer(container) + new GenericContainer.Def(new GenericContainer(container): TestContainer) {} } } diff --git a/src/test/scala/play/api/cache/redis/test/RedisMasterSlaveContainer.scala b/src/test/scala/play/api/cache/redis/test/RedisMasterSlaveContainer.scala index e63aa5d..cd7108b 100644 --- a/src/test/scala/play/api/cache/redis/test/RedisMasterSlaveContainer.scala +++ b/src/test/scala/play/api/cache/redis/test/RedisMasterSlaveContainer.scala @@ -1,6 +1,6 @@ package play.api.cache.redis.test -import com.dimafeng.testcontainers.{DockerComposeContainer, ExposedService, SingleContainer} +import com.dimafeng.testcontainers.{DockerComposeContainer, ExposedService} import org.scalatest.Suite import org.testcontainers.containers.wait.strategy.Wait @@ -9,25 +9,24 @@ import java.io.File trait RedisMasterSlaveContainer extends ForAllTestContainer { this: Suite => - protected val host = "localhost" - protected val masterPort = 6379 - protected val slavePort = 6479 + override protected type TestContainer = DockerComposeContainer + + final protected val host = "localhost" + final protected val masterPort = 6379 + final protected val slavePort = 6479 + + protected def newContainer: TestContainerDef[TestContainer] = + DockerComposeContainer.Def( + new File("src/test/resources/docker-compose.yml"), + tailChildContainers = true, + env = Map( + "REDIS_MASTER_PORT" -> s"$masterPort", + "REDIS_SLAVE_PORT" -> s"$slavePort", + ), + exposedServices = Seq( + ExposedService("redis-master", masterPort, Wait.forLogMessage(".*Ready to accept connections tcp.*\\n", 1)), + ExposedService("redis-slave", slavePort, Wait.forLogMessage(".*MASTER <-> REPLICA sync: Finished with success.*\\n", 1)), + ), + ) - private val composeContainerDef = DockerComposeContainer.Def( - new File("src/test/resources/docker-compose.yml"), - tailChildContainers = true, - exposedServices = Seq( - ExposedService("redis-master", masterPort, Wait.forLogMessage(".*Ready to accept connections tcp.*\\n", 1)), - ExposedService("redis-slave", slavePort, Wait.forLogMessage(".*MASTER <-> REPLICA sync: Finished with success.*\\n", 1)), - ), - ) - - private lazy val dockerComposeContainer: DockerComposeContainer = composeContainerDef.createContainer() - - protected def newContainer: SingleContainer[?] = - throw new UnsupportedOperationException("Creating a single container is not supported.") - - override def beforeAll(): Unit = dockerComposeContainer.start() - - override def afterAll(): Unit = dockerComposeContainer.stop() }