spin redis from docker-it-scala



我正在尝试从docker-it-scala旋转一个docker容器。目标是能够针对容器中的 redis 运行单元测试,而不是作为托管服务 (AWS ElastiCache( 的 prod

我构建了一个服务,非常密切地遵循

import com.spotify.docker.client.{DefaultDockerClient, DockerClient}
import com.whisk.docker.impl.spotify.SpotifyDockerFactory
import com.whisk.docker.{DockerContainer, DockerFactory, DockerKit, DockerReadyChecker}
// Look at https://github.com/whisklabs/docker-it-scala
// .. and let yourself be inspired
trait DockerRedisService extends DockerKit {
  val redisDefaultPort = 6379
  val redisContainer = DockerContainer("redis")
    .withPorts(redisDefaultPort -> None)
    .withReadyChecker(DockerReadyChecker.LogLineContains("Ready to accept connections"))
  abstract override def dockerContainers: List[DockerContainer] =
    redisContainer :: super.dockerContainers
}

从那里,我应该能够运行一些单元测试

class ElastiCache extends FlatSpec with Matchers with BeforeAndAfter with ScalaFutures with DockerRedisService {
  private val client: DockerClient = DefaultDockerClient.fromEnv().build()
  override implicit val dockerFactory: DockerFactory = new SpotifyDockerFactory(client)
  implicit val pc = PatienceConfig(Span(40, Seconds), Span(1, Second))
  "the redis container" should "be ready with log line checker" in {
    isContainerReady(redisContainer).futureValue shouldBe true
    redisContainer.getPorts().futureValue.get(6379 ) should not be empty
    redisContainer.getIpAddresses().futureValue should not be Seq.empty
  }

单元测试只是超时,没有太多信息。

看看我如何从该 redis 中设置和获取一些值会很有用。

您缺少指定 java-docker 后端的执行特征。

您的class ElastiCache应扩展其中之一:

使用docker-testkit-impl-docker-java

with DockerTestKit with DockerKitDockerJava

使用docker-testkit-impl-spotify

with DockerTestKit with DockerKitDockerSpotify

您不需要创建 docker 客户端或设置 docker 工厂。您只需要添加所有适合您的特征即可。

 class ElastiCache extends FlatSpec with Matchers 
    with DockerTestKit with DockerRedisService with DockerKitDockerJava 

此外,如果您的测试在主机上运行,并且它们需要访问这些容器,则可能需要公开端口:

    .withPorts(6379 -> Some(9999)) 

这会将容器中的默认 Redis 端口6379映射到主机端口9999

最新更新