Skip to content

Commit

Permalink
Improved ForAllTestContainer trait to also support docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
KarelCemus committed Apr 11, 2024
1 parent dd8c5e9 commit f7ccba2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/test/resources/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 14 additions & 4 deletions src/test/scala/play/api/cache/redis/test/ForAllTestContainer.scala
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 4 additions & 2 deletions src/test/scala/play/api/cache/redis/test/RedisContainer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}

}
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
}

0 comments on commit f7ccba2

Please sign in to comment.