generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kafka Kraft Mode support via confluent-local image (#623)
* Kafka Kraft Mode support via confluent-local image Fixes: #622 * Allow custom image specification with kraft mode
- Loading branch information
Showing
7 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...urces-kafka/src/main/java/io/micronaut/testresources/kafka/KafkaConfigurationSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.testresources.kafka; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Internal class to deal with the test resources | ||
* kafka configuration block. | ||
*/ | ||
abstract class KafkaConfigurationSupport { | ||
public static final String CONFIG_KAFKA_KRAFT_MODE = "containers.kafka.kraft"; | ||
|
||
private KafkaConfigurationSupport() { | ||
|
||
} | ||
|
||
/** | ||
* Start Kafka container in Kraft mode with the confluent-local image. | ||
* See: <a href="https://docs.confluent.io/platform/current/installation/docker/image-reference.html#ak-images">Confluent Kafka Images</a> | ||
* See: <a href="https://java.testcontainers.org/modules/kafka/#using-kraft-mode">TestContainers Kafka Kraft Mode</a> | ||
*/ | ||
static boolean isKraftMode(Map<String, Object> testResourcesConfig) { | ||
Boolean clusterMode = (Boolean) testResourcesConfig.getOrDefault(CONFIG_KAFKA_KRAFT_MODE, false); | ||
return Boolean.TRUE.equals(clusterMode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...s-kafka/src/test/groovy/io/micronaut/testresources/kafka/CustomKafkaImageKraftTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.micronaut.testresources.kafka | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import io.micronaut.testresources.core.Scope | ||
import io.micronaut.testresources.testcontainers.TestContainers | ||
import jakarta.inject.Inject | ||
|
||
@MicronautTest(environments = "kraft") | ||
class CustomKafkaImageKraftTest extends AbstractKafkaSpec { | ||
|
||
@Override | ||
Map<String, String> getProperties() { | ||
super.properties + [ | ||
"test-resources.containers.kafka.image-name": "confluentinc/confluent-local:7.7.1" | ||
] | ||
} | ||
|
||
@Override | ||
String getImageName() { | ||
'confluent-local' | ||
} | ||
|
||
@Inject | ||
ApplicationContext applicationContext | ||
|
||
/** | ||
* Note this test will fail if not launched in kraft mode (i.e. environments = kraft) as no zookeeper was configured. | ||
* So there are no assertions in the test to check if kraft mode was enabled. | ||
* | ||
* Example Error: | ||
* | ||
* 10:22:21.672 [main] ERROR t.confluentinc/confluent-local:7.7.1 - Could not start container java.lang.IllegalStateException: Wait strategy failed. Container exited with code 1 | ||
* | ||
* Docker Logs: | ||
* 2024-10-11 10:21:22 [2024-10-11 14:21:22,411] ERROR Exiting Kafka due to fatal exception (kafka.Kafka$) | ||
* 2024-10-11 10:21:22 java.lang.IllegalArgumentException: requirement failed: controller.listener.names must contain at least one value appearing in the 'listeners' configuration when running the KRaft controller role | ||
*/ | ||
def "starts Kafka using a custom image with Kraft"() { | ||
when: | ||
def client = applicationContext.getBean(AnalyticsClient) | ||
def result = client.updateAnalytics("oh yeah!") | ||
|
||
then: | ||
listContainers().size() == 1 | ||
result.block() == "oh yeah!" | ||
with(TestContainers.listByScope("kafka").get(Scope.of("kafka"))) { | ||
size() == 1 | ||
get(0).dockerImageName == "confluentinc/confluent-local:7.7.1" | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...urces-kafka/src/test/groovy/io/micronaut/testresources/kafka/KafkaKraftStartedTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.micronaut.testresources.kafka | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import io.micronaut.testresources.core.Scope | ||
import io.micronaut.testresources.testcontainers.TestContainers | ||
import jakarta.inject.Inject | ||
|
||
@MicronautTest(environments = "kraft") | ||
class KafkaKraftStartedTest extends AbstractKafkaSpec { | ||
|
||
@Inject | ||
ApplicationContext applicationContext | ||
|
||
@Override | ||
String getImageName() { | ||
'confluent-local' | ||
} | ||
|
||
def "starts Kafka using a kraft mode and confluent-local image"() { | ||
when: | ||
def client = applicationContext.getBean(AnalyticsClient) | ||
def result = client.updateAnalytics("oh yeah!") | ||
|
||
then: | ||
listContainers().size() == 1 | ||
result.block() == "oh yeah!" | ||
with(TestContainers.listByScope("kafka").get(Scope.of("kafka"))) { | ||
size() == 1 | ||
get(0).dockerImageName == KafkaTestResourceProvider.DEFAULT_KRAFT_IMAGE | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
test-resources-kafka/src/test/resources/application-kraft.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test-resources: | ||
containers: | ||
kafka: | ||
kraft: true |