Skip to content

Commit f22c1ba

Browse files
Add smoke test for Kafka with SSL
Closes gh-38260
1 parent 47cac96 commit f22c1ba

File tree

9 files changed

+136
-2
lines changed

9 files changed

+136
-2
lines changed

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-kafka/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ dependencies {
1414
testImplementation("org.springframework.kafka:spring-kafka-test") {
1515
exclude group: "commons-logging", module: "commons-logging"
1616
}
17+
testImplementation("org.testcontainers:junit-jupiter")
1718
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-kafka/src/main/java/smoketest/kafka/Consumer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.stereotype.Component;
2424

2525
@Component
26-
class Consumer {
26+
public class Consumer {
2727

2828
private final List<SampleMessage> messages = new CopyOnWriteArrayList<>();
2929

@@ -33,7 +33,7 @@ void processMessage(SampleMessage message) {
3333
System.out.println("Received sample message [" + message + "]");
3434
}
3535

36-
List<SampleMessage> getMessages() {
36+
public List<SampleMessage> getMessages() {
3737
return this.messages;
3838
}
3939

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.kafka.ssl;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class SampleKafkaSslApplication {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(SampleKafkaSslApplication.class, args);
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.kafka.ssl;
18+
19+
import java.io.File;
20+
import java.time.Duration;
21+
22+
import org.awaitility.Awaitility;
23+
import org.junit.jupiter.api.Test;
24+
import org.testcontainers.containers.DockerComposeContainer;
25+
import org.testcontainers.containers.wait.strategy.Wait;
26+
import org.testcontainers.junit.jupiter.Container;
27+
import org.testcontainers.junit.jupiter.Testcontainers;
28+
import smoketest.kafka.Consumer;
29+
import smoketest.kafka.Producer;
30+
import smoketest.kafka.SampleMessage;
31+
32+
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.boot.test.context.SpringBootTest;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.hamcrest.Matchers.empty;
37+
import static org.hamcrest.Matchers.not;
38+
39+
@Testcontainers
40+
@SpringBootTest(classes = { SampleKafkaSslApplication.class, Producer.class, Consumer.class },
41+
properties = { "spring.kafka.security.protocol=SSL", "spring.kafka.bootstrap-servers=localhost:9093",
42+
"spring.kafka.ssl.bundle=client",
43+
"spring.ssl.bundle.jks.client.keystore.location=classpath:ssl/test-client.p12",
44+
"spring.ssl.bundle.jks.client.keystore.password=password",
45+
"spring.ssl.bundle.jks.client.truststore.location=classpath:ssl/test-ca.p12",
46+
"spring.ssl.bundle.jks.client.truststore.password=password" })
47+
class SampleKafkaSslApplicationTests {
48+
49+
private static final File KAFKA_COMPOSE_FILE = new File("src/test/resources/docker-compose.yml");
50+
51+
private static final String KAFKA_COMPOSE_SERVICE = "kafka";
52+
53+
private static final int KAFKA_SSL_PORT = 9093;
54+
55+
@Container
56+
public DockerComposeContainer<?> container = new DockerComposeContainer<>(KAFKA_COMPOSE_FILE)
57+
.withExposedService(KAFKA_COMPOSE_SERVICE, KAFKA_SSL_PORT, Wait.forListeningPorts(KAFKA_SSL_PORT));
58+
59+
@Autowired
60+
private Producer producer;
61+
62+
@Autowired
63+
private Consumer consumer;
64+
65+
@Test
66+
void testVanillaExchange() {
67+
this.producer.send(new SampleMessage(1, "A simple test message"));
68+
69+
Awaitility.waitAtMost(Duration.ofSeconds(30)).until(this.consumer::getMessages, not(empty()));
70+
assertThat(this.consumer.getMessages()).extracting("message").containsOnly("A simple test message");
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
version: '2'
3+
services:
4+
zookeeper:
5+
image: confluentinc/cp-zookeeper:7.4.0
6+
environment:
7+
ZOOKEEPER_CLIENT_PORT: 2181
8+
ZOOKEEPER_TICK_TIME: 2000
9+
10+
kafka:
11+
image: confluentinc/cp-kafka:7.4.0
12+
depends_on:
13+
- zookeeper
14+
ports:
15+
- "9092:9092"
16+
- "9093:9093"
17+
environment:
18+
KAFKA_BROKER_ID: 1
19+
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
20+
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,SSL://localhost:9093
21+
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
22+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
23+
KAFKA_SSL_CLIENT_AUTH: "required"
24+
KAFKA_SSL_KEYSTORE_FILENAME: '/certs/test-server.p12'
25+
KAFKA_SSL_KEYSTORE_CREDENTIALS: '/certs/credentials'
26+
KAFKA_SSL_KEY_CREDENTIALS: '/certs/credentials'
27+
KAFKA_SSL_TRUSTSTORE_FILENAME: '/certs/test-ca.p12'
28+
KAFKA_SSL_TRUSTSTORE_CREDENTIALS: '/certs/credentials'
29+
volumes:
30+
- ./ssl/:/etc/kafka/secrets/certs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
password

0 commit comments

Comments
 (0)