forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKafkaContainerTest.java
More file actions
342 lines (305 loc) · 14 KB
/
KafkaContainerTest.java
File metadata and controls
342 lines (305 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package org.testcontainers.containers;
import com.google.common.collect.ImmutableMap;
import lombok.SneakyThrows;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.errors.SaslAuthenticationException;
import org.apache.kafka.common.errors.TopicAuthorizationException;
import org.awaitility.Awaitility;
import org.junit.Test;
import org.testcontainers.AbstractKafka;
import org.testcontainers.KCatContainer;
import org.testcontainers.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;
import java.util.Collection;
import java.util.Collections;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class KafkaContainerTest extends AbstractKafka {
private static final DockerImageName KAFKA_TEST_IMAGE = DockerImageName.parse("confluentinc/cp-kafka:6.2.1");
private static final DockerImageName KAFKA_KRAFT_TEST_IMAGE = DockerImageName.parse("confluentinc/cp-kafka:7.0.1");
private static final DockerImageName ZOOKEEPER_TEST_IMAGE = DockerImageName.parse(
"confluentinc/cp-zookeeper:4.0.0"
);
@Test
public void testUsage() throws Exception {
try (KafkaContainer kafka = new KafkaContainer(KAFKA_TEST_IMAGE)) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testUsageWithSpecificImage() throws Exception {
try (
// constructorWithVersion {
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))
// }
) {
kafka.start();
testKafkaFunctionality(
// getBootstrapServers {
kafka.getBootstrapServers()
// }
);
}
}
@Test
public void testUsageWithVersion() throws Exception {
try (KafkaContainer kafka = new KafkaContainer("6.2.1")) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testExternalZookeeperWithExternalNetwork() throws Exception {
try (
Network network = Network.newNetwork();
// withExternalZookeeper {
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))
.withNetwork(network)
.withExternalZookeeper("zookeeper:2181");
// }
GenericContainer<?> zookeeper = new GenericContainer<>(ZOOKEEPER_TEST_IMAGE)
.withNetwork(network)
.withNetworkAliases("zookeeper")
.withEnv("ZOOKEEPER_CLIENT_PORT", "2181");
) {
zookeeper.start();
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testConfluentPlatformVersion7() throws Exception {
try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.2.2"))) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testConfluentPlatformVersion5() throws Exception {
try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.4.3"))) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testWithHostExposedPort() throws Exception {
Testcontainers.exposeHostPorts(12345);
try (KafkaContainer kafka = new KafkaContainer(KAFKA_TEST_IMAGE)) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testWithHostExposedPortAndExternalNetwork() throws Exception {
Testcontainers.exposeHostPorts(12345);
try (KafkaContainer kafka = new KafkaContainer(KAFKA_TEST_IMAGE).withNetwork(Network.newNetwork())) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testUsageKraftBeforeConfluentPlatformVersion74() throws Exception {
try (
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.0.1")).withKraft()
) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testUsageKraftAfterConfluentPlatformVersion74() throws Exception {
try (
// withKraftMode {
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")).withKraft()
// }
) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testNotSupportedKraftVersion() {
try (
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1")).withKraft()
) {} catch (IllegalArgumentException e) {
assertThat(e.getMessage())
.isEqualTo(
"Provided Confluent Platform's version 6.2.1 is not supported in Kraft mode (must be 7.0.0 or above)"
);
}
}
@Test
public void testKraftZookeeperMutualExclusion() {
try (
KafkaContainer kafka = new KafkaContainer(KAFKA_KRAFT_TEST_IMAGE).withKraft().withExternalZookeeper("")
) {} catch (IllegalStateException e) {
assertThat(e.getMessage()).isEqualTo("Cannot configure Zookeeper when using Kraft mode");
}
try (
KafkaContainer kafka = new KafkaContainer(KAFKA_KRAFT_TEST_IMAGE).withExternalZookeeper("").withKraft()
) {} catch (IllegalStateException e) {
assertThat(e.getMessage()).isEqualTo("Cannot configure Kraft mode when Zookeeper configured");
}
try (
KafkaContainer kafka = new KafkaContainer(KAFKA_KRAFT_TEST_IMAGE).withKraft().withEmbeddedZookeeper()
) {} catch (IllegalStateException e) {
assertThat(e.getMessage()).isEqualTo("Cannot configure Zookeeper when using Kraft mode");
}
}
@Test
public void testKraftPrecedenceOverEmbeddedZookeeper() throws Exception {
try (KafkaContainer kafka = new KafkaContainer(KAFKA_KRAFT_TEST_IMAGE).withEmbeddedZookeeper().withKraft()) {
kafka.start();
testKafkaFunctionality(kafka.getBootstrapServers());
}
}
@Test
public void testUsageWithListener() throws Exception {
try (
Network network = Network.newNetwork();
// registerListener {
KafkaContainer kafka = new KafkaContainer(KAFKA_KRAFT_TEST_IMAGE)
.withListener(() -> "kafka:19092")
.withNetwork(network);
// }
// createKCatContainer {
KCatContainer kcat = new KCatContainer().withNetwork(network)
// }
) {
kafka.start();
kcat.start();
// produceConsumeMessage {
kcat.execInContainer("kcat", "-b", "kafka:19092", "-t", "msgs", "-P", "-l", "/data/msgs.txt");
String stdout = kcat
.execInContainer("kcat", "-b", "kafka:19092", "-C", "-t", "msgs", "-c", "1")
.getStdout();
// }
assertThat(stdout).contains("Message produced by kcat");
}
}
@SneakyThrows
@Test
public void shouldConfigureAuthenticationWithSaslUsingJaas() {
try (
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))
.withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "PLAINTEXT:SASL_PLAINTEXT,BROKER:SASL_PLAINTEXT")
.withEnv("KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_PLAINTEXT_SASL_ENABLED_MECHANISMS", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_BROKER_SASL_ENABLED_MECHANISMS", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_BROKER_PLAIN_SASL_JAAS_CONFIG", getJaasConfig())
.withEnv("KAFKA_LISTENER_NAME_PLAINTEXT_PLAIN_SASL_JAAS_CONFIG", getJaasConfig())
) {
kafka.start();
testSecurePlainKafkaFunctionality(kafka.getBootstrapServers());
}
}
@SneakyThrows
@Test
public void shouldConfigureAuthenticationWithSaslScramUsingJaas() {
try (
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.7.0")) {
protected String commandKraft() {
String command = "sed -i '/KAFKA_ZOOKEEPER_CONNECT/d' /etc/confluent/docker/configure\n";
command +=
"echo 'kafka-storage format --ignore-formatted -t \"" +
"$CLUSTER_ID" +
"\" --add-scram SCRAM-SHA-256=[name=admin,password=admin] -c /etc/kafka/kafka.properties' >> /etc/confluent/docker/configure\n";
return command;
}
}
.withKraft()
.withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "PLAINTEXT:SASL_PLAINTEXT,BROKER:SASL_PLAINTEXT")
.withEnv("KAFKA_LISTENER_NAME_PLAINTEXT_SASL_ENABLED_MECHANISMS", "SCRAM-SHA-256")
.withEnv("KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL", "SCRAM-SHA-256")
.withEnv("KAFKA_SASL_ENABLED_MECHANISMS", "SCRAM-SHA-256")
.withEnv("KAFKA_OPTS", "-Djava.security.auth.login.config=/etc/kafka/secrets/kafka_server_jaas.conf")
.withCopyFileToContainer(
MountableFile.forClasspathResource("kafka_server_jaas.conf"),
"/etc/kafka/secrets/kafka_server_jaas.conf"
)
) {
kafka.start();
testSecureScramKafkaFunctionality(kafka.getBootstrapServers());
}
}
@SneakyThrows
@Test
public void enableSaslWithUnsuccessfulTopicCreation() {
try (
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))
.withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "PLAINTEXT:SASL_PLAINTEXT,BROKER:SASL_PLAINTEXT")
.withEnv("KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_PLAINTEXT_SASL_ENABLED_MECHANISMS", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_BROKER_SASL_ENABLED_MECHANISMS", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_BROKER_PLAIN_SASL_JAAS_CONFIG", getJaasConfig())
.withEnv("KAFKA_LISTENER_NAME_PLAINTEXT_PLAIN_SASL_JAAS_CONFIG", getJaasConfig())
.withEnv("KAFKA_AUTHORIZER_CLASS_NAME", "kafka.security.authorizer.AclAuthorizer")
.withEnv("KAFKA_SUPER_USERS", "User:admin")
) {
kafka.start();
AdminClient adminClient = AdminClient.create(
ImmutableMap.of(
AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,
kafka.getBootstrapServers(),
AdminClientConfig.SECURITY_PROTOCOL_CONFIG,
"SASL_PLAINTEXT",
SaslConfigs.SASL_MECHANISM,
"PLAIN",
SaslConfigs.SASL_JAAS_CONFIG,
"org.apache.kafka.common.security.plain.PlainLoginModule required username=\"test\" password=\"secret\";"
)
);
String topicName = "messages-" + UUID.randomUUID();
Collection<NewTopic> topics = Collections.singletonList(new NewTopic(topicName, 1, (short) 1));
Awaitility
.await()
.untilAsserted(() -> {
assertThatThrownBy(() -> adminClient.createTopics(topics).all().get(30, TimeUnit.SECONDS))
.hasCauseInstanceOf(TopicAuthorizationException.class);
});
}
}
@SneakyThrows
@Test
public void enableSaslAndWithAuthenticationError() {
String jaasConfig = getJaasConfig();
try (
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))
.withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "PLAINTEXT:SASL_PLAINTEXT,BROKER:SASL_PLAINTEXT")
.withEnv("KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_PLAINTEXT_SASL_ENABLED_MECHANISMS", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_BROKER_SASL_ENABLED_MECHANISMS", "PLAIN")
.withEnv("KAFKA_LISTENER_NAME_BROKER_PLAIN_SASL_JAAS_CONFIG", jaasConfig)
.withEnv("KAFKA_LISTENER_NAME_PLAINTEXT_PLAIN_SASL_JAAS_CONFIG", jaasConfig)
) {
kafka.start();
AdminClient adminClient = AdminClient.create(
ImmutableMap.of(
AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,
kafka.getBootstrapServers(),
AdminClientConfig.SECURITY_PROTOCOL_CONFIG,
"SASL_PLAINTEXT",
SaslConfigs.SASL_MECHANISM,
"PLAIN",
SaslConfigs.SASL_JAAS_CONFIG,
"org.apache.kafka.common.security.plain.PlainLoginModule required username=\"test\" password=\"secretx\";"
)
);
String topicName = "messages-" + UUID.randomUUID();
Collection<NewTopic> topics = Collections.singletonList(new NewTopic(topicName, 1, (short) 1));
Awaitility
.await()
.untilAsserted(() -> {
assertThatThrownBy(() -> adminClient.createTopics(topics).all().get(30, TimeUnit.SECONDS))
.hasCauseInstanceOf(SaslAuthenticationException.class);
});
}
}
}