0

Im using elastic4s and also interested in using a docker container based testing environment for my elastic search.

There are few libraries like: testcontainers-scala and docker-it-scala, but can't find how I integrate elastic4s into those libraries, did someone ever used a docker container based testing env?

currently my spec is very simple:

class ElasticSearchApiServiceSpec extends FreeSpec {

  implicit val defaultPatience = PatienceConfig(timeout = Span(100, Seconds), interval = Span(50, Millis))

  val configuration: Configuration = app.injector.instanceOf[Configuration]

  val elasticSearchApiService = new ElasticSearchApiService(configuration)

  override protected def beforeAll(): Unit = {
    elasticSearchApiService.elasticClient.execute {
      index into s"peopleIndex/person" doc StringDocumentSource(PeopleFactory.rawStringGoodPerson)
    }
    // since ES is eventually
    Thread.sleep(3000)
  }

  override protected def afterAll(): Unit = {
    elasticSearchApiService.elasticClient.execute {
      deleteIndex("peopleIndex")
    }
  }

  "ElasticSearchApiService Tests" - {

    "elastic search service should retrieve person info properly - case existing person" in {
      val personInfo = elasticSearchApiService.getPersonInfo("2324").futureValue

      personInfo.get.name shouldBe "john"
    }
  }

}

and when I run it, I run elastic search in the background from my terminal, but I want to use containers now so it will be less dependent.

JohnBigs
  • 2,691
  • 3
  • 31
  • 61

1 Answers1

1

I guess you don't want to depend on ES server running on your local machine for the tests. Then the simplest approach would be using testcontainers-scala's GenericContainer to run official ES docker image this way:

class GenericContainerSpec extends FlatSpec with ForAllTestContainer {
    override val container = GenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.5.1",
        exposedPorts = Seq(9200),
        waitStrategy = Wait.forHttp("/")
    )

    "GenericContainer" should "start ES and expose 9200 port" in {
       assert(Source.fromInputStream(
          new URL(
            s"http://${container.containerIpAddress}:${container.mappedPort(9200)}/_status")
            .openConnection()
            .getInputStream)
           .mkString
           .contains("ES server is successfully installed"))
    }
}
shutty
  • 3,298
  • 16
  • 27