How to start a docker container before run the test

zero_coding

I would like to setup a test environment for Scala project. In addition, I need to start Kafka, that is running in a docker container. Before the test is going to start, it should start first Kafka container.

I am using Scalatest and thinking about to start the Kafka container in the TestFixture, once before tests run.

The question is, which is the recommended way to start a container before running tests. I considered the Docker API, but not sure, it is the right way or not.

Krzysztof Atłasik

You can use testcontainers-scala, which is just a wrapper around testcontainers.

In your build.sbt add:

libraryDependencies += "com.dimafeng" %% "testcontainers-scala" % "0.25.0" % "test"
libraryDependencies += "org.apache.kafka" % "kafka-clients" % "2.2.0"

And then you can create spec:

import com.dimafeng.testcontainers.{ForAllTestContainer, GenericContainer}
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.serialization.{StringDeserializer, StringSerializer}
import org.scalatest.FlatSpec
import org.testcontainers.containers.Network
import org.testcontainers.utility.Base58


class KafkaSpec extends FlatSpec with ForAllTestContainer {

  final val KafkaPort = 9093

  override val container = GenericContainer("confluentinc/cp-kafka").configure{ c =>
    c.withNetwork(Network.newNetwork())
    c.withNetworkAliases("kafka-" + Base58.randomString(6))
    c.withExposedPorts(KafkaPort)
    c.withEnv("KAFKA_LISTENERS", "PLAINTEXT://0.0.0.0:" + KafkaPort + ",BROKER://0.0.0.0:9092")
    c.withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT")
    c.withEnv("KAFKA_INTER_BROKER_LISTENER_NAME", "BROKER")
    c.withEnv("KAFKA_BROKER_ID", "1")
    c.withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "1")
    c.withEnv("KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS", "1")
    c.withEnv("KAFKA_LOG_FLUSH_INTERVAL_MESSAGES", Long.MaxValue.toString)
    c.withEnv("KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS", "0")
  }


  it should "do something" in {

    val properties = new Properties()
    properties.put("bootstrap.servers", s"${container.containerIpAddress}:$KafkaPort")
    properties.put("group.id", "test")
    properties.put("key.deserializer", classOf[StringDeserializer])
    properties.put("value.deserializer", classOf[StringDeserializer])
    properties.put("key.serializer", classOf[StringSerializer])
    properties.put("value.serializer", classOf[StringSerializer])

    val kafkaConsumer = new KafkaConsumer[String, String](properties)
    ....

  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to start kafka test container before @ContextConfiguration initializers?

how to call a method immediately before the start of a test run in minitest

Run cypress test in a docker container?

Run coverage test in a docker container

How to run websockets with docker container start in laravel application

How to have two JARs start automatically on "docker run container"

run multiple commands in docker container start

How to start docker container as server

Run (Docker) Test Container in gitlab with Maven

Not able to run JavaScript test inside Docker container

How to run a Docker Container in Heroku

How to run a Docker host inside a Docker container?

How to run tests before start ring server

Docker 'exitpoint' - run a command before a container is `down`ed or `stop`ped?

How to start docker container with dynamic url

Ansible - How to remove a docker container after start?

How to start an existing docker container with displaying the output?

Docker: How to start an existing container and forward the ports?

How to start a mongodb shell in docker container?

How to start a stopped Docker container with a different command?

How to start and stop docker container with systemd

How start filebeat inside docker container?

How to teach mariadb docker container not to start on the boot

How does Docker Swarm start a container

Cucumber 7 + TestNG - Dynamic tags manipulation before test run start

Why is there no logs using "docker run image command" to start container?

npm install and run dev for laravel / vue on Docker container start

Run execlineb when container start failed. Docker for windosw

How to run code before ApplicationRunner in a unit test?