Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Testkit: start configurable Testcontainer via mixin #936

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2014 - 2016 Softwaremill <http://softwaremill.com>
* Copyright (C) 2016 - 2019 Lightbend Inc. <http://www.lightbend.com>
*/

package akka.kafka.testkit.scaladsl

import akka.kafka.testkit.internal.TestcontainersKafkaHelper
import org.testcontainers.containers.KafkaContainer


/**
* Uses [[https://www.testcontainers.org/ Testcontainers]] to start a Kafka broker in a Docker container.
* The Testcontainers dependency has to be added explicitly.
*/
trait TestcontainersKafkaPerClassLike extends KafkaSpec {

private var kafkaContainer: KafkaContainer = _
private var kafkaBootstrapServersInternal: String = _
private var kafkaPortInternal: Int = -1

private def requireStarted(): Unit =
require(kafkaPortInternal != -1, "Testcontainers Kafka hasn't been started via `setUp`")

override def kafkaPort: Int = {
requireStarted()
kafkaPortInternal
}

def bootstrapServers: String = {
requireStarted()
kafkaBootstrapServersInternal
}

def startKafka(confluentPlatformVersion: String): String = {
if (kafkaPortInternal == -1) {
val kafkaContainer = new KafkaContainer(confluentPlatformVersion)
configureKafka(kafkaContainer)
kafkaContainer.start()
kafkaBootstrapServersInternal = kafkaContainer.getBootstrapServers
kafkaPortInternal =
kafkaBootstrapServersInternal.substring(kafkaBootstrapServersInternal.lastIndexOf(":") + 1).toInt
}
kafkaBootstrapServersInternal
}

def stopKafka(): Unit =
if (kafkaPortInternal != -1) {
kafkaContainer.stop()
kafkaPortInternal = -1
kafkaContainer = null
}

/**
* Override this to select a different Kafka version be choosing the desired version of Confluent Platform:
* [[https://hub.docker.com/r/confluentinc/cp-kafka/tags Available Docker images]],
* [[https://docs.confluent.io/current/installation/versions-interoperability.html Kafka versions in Confluent Platform]]
*/
def confluentPlatformVersion: String = TestcontainersKafkaHelper.ConfluentPlatformVersionDefault

/**
* Override this to configure the Kafka container before it is started.
*/
def configureKafka(kafkaContainer: KafkaContainer): Unit = ()

override def setUp(): Unit = {
startKafka(confluentPlatformVersion)
super.setUp()
}

override def cleanUp(): Unit = {
super.cleanUp()
stopKafka()
}

}