'Testcontainers fix bootstrapServers port for Kafka
I want to specify a custom port from the config for the TestContainers Kafka image To be able to reuse bootstrapservers param later for the black box application testing using https://github.com/testcontainers/testcontainers-scala Kafka module Did not find an API for fixing the port while running the container, all I found is the port is dynamically assigned to the container
class KafkaSetUpSpec extends AnyFlatSpec with TestContainerForAll with Matchers {
override val containerDef: KafkaContainer.Def = KafkaContainer.Def()
import org.testcontainers.Testcontainers
//Testcontainers.exposeHostPorts(9092)
it should "return Kafka connection options for kafka container" in withContainers { kafkaContainer =>
kafkaContainer.bootstrapServers.nonEmpty shouldBe true
kafkaContainer.bootstrapServers.split(":")(2).toInt shouldBe kafkaContainer.container.getMappedPort(9093)
}
All I need is to take the connection URL from the config and fix it in the Kafka container like a port, do you have any idea how to do it? How do assign the same port from the outside world? Addition info that client not in the same network and located localy
Solution 1:[1]
Testcontainers is providingg dynamic port mapping for all modules by design. You have to use the provided kafkaContainer.getBootstrapServer() after the container has been started to get the dynamically mapped port. This needs to be injected into your system under test afterwards.
You can make use of the experimental reusable mode, to reuse a Testcontainers instrumented container across JVMs.
Add testcontainers.reuse.enable=true to the ~/.testcontainers.properties file on your local machine and set withReuse(true) on the KafkaContainer. Note that reusable mode currently does not support Docker networks.
See further examples in the corresponding PR: https://github.com/testcontainers/testcontainers-java/pull/1781
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kevin Wittek |
