Skip to content

Commit

Permalink
Allow to start Kafka and Schema Registry containers in the same Netwo…
Browse files Browse the repository at this point in the history
…rk using public Interface
  • Loading branch information
slawomirkania committed Dec 11, 2023
1 parent 69e5c65 commit 050540d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package com.dimafeng.testcontainers

import org.testcontainers.containers.{KafkaContainer => JavaKafkaContainer}
import org.testcontainers.containers.{Network, KafkaContainer => JavaKafkaContainer}
import org.testcontainers.utility.DockerImageName

case class KafkaContainer(dockerImageName: DockerImageName = DockerImageName.parse(KafkaContainer.defaultDockerImageName)
) extends SingleContainer[JavaKafkaContainer] {
case class KafkaContainer(
override val network: Network = KafkaContainer.defaultNetwork,
dockerImageName: DockerImageName = KafkaContainer.defaultDockerImage
) extends SingleContainer[JavaKafkaContainer] {

override val container: JavaKafkaContainer = new JavaKafkaContainer(dockerImageName)
override val container: JavaKafkaContainer = new JavaKafkaContainer(dockerImageName).withNetwork(network)

def bootstrapServers: String = container.getBootstrapServers
}

object KafkaContainer {

val defaultImage = "confluentinc/cp-kafka"
val defaultTag = "7.2.0"
val defaultDockerImageName = s"$defaultImage:$defaultTag"

case class Def(dockerImageName: DockerImageName = DockerImageName.parse(KafkaContainer.defaultDockerImageName)
) extends ContainerDef {
private val defaultDockerImage = DockerImageName.parse(s"confluentinc/cp-kafka:$defaultTag")
private def defaultNetwork: Network = Network.newNetwork()

case class Def(
network: Network = defaultNetwork,
dockerImageName: DockerImageName = defaultDockerImage
) extends ContainerDef {

override type Container = KafkaContainer

override def createContainer(): KafkaContainer = {
new KafkaContainer(dockerImageName)
new KafkaContainer(network, dockerImageName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SchemaRegistrySpec extends AnyFlatSpec with ForAllTestContainer with Match

//this should be the same version that your lib is using under the hood
val kafkaVersion = "6.1.1"
val kafkaDockerImage = DockerImageName.parse(s"confluentinc/cp-kafka:$kafkaVersion")

//these are the default kafka host name but because that may change
//we need to ensure that these are the values for container network, kafka and the schema registry
Expand All @@ -27,11 +28,10 @@ class SchemaRegistrySpec extends AnyFlatSpec with ForAllTestContainer with Match
//a way to communicate containers
val network: Network = Network.newNetwork()

val kafkaContainer: KafkaContainer = KafkaContainer.Def(DockerImageName.parse(s"confluentinc/cp-kafka:$kafkaVersion")).createContainer()
val kafkaContainer: KafkaContainer = KafkaContainer.Def(network, kafkaDockerImage).createContainer()
val schemaRegistryContainer: GenericContainer = SchemaRegistryContainer.Def(network, hostName, kafkaVersion).createContainer()

kafkaContainer.container
.withNetwork(network)
.withNetworkAliases(hostName)
.withEnv(
Map[String, String](
Expand Down

0 comments on commit 050540d

Please sign in to comment.