Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add redpandadata/redpanda as a compatible image (#7898)
Test has been added for all compatible images.
- Loading branch information
1 parent
7a1bcd2
commit 892b06b
Showing
4 changed files
with
146 additions
and
80 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
92 changes: 92 additions & 0 deletions
92
modules/redpanda/src/test/java/org/testcontainers/redpanda/AbstractRedpanda.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,92 @@ | ||
package org.testcontainers.redpanda; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
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.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.rnorth.ducttape.unreliables.Unreliables; | ||
|
||
import java.time.Duration; | ||
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.tuple; | ||
|
||
public class AbstractRedpanda { | ||
|
||
protected void testKafkaFunctionality(String bootstrapServers) throws Exception { | ||
testKafkaFunctionality(bootstrapServers, 1, 1); | ||
} | ||
|
||
protected void testKafkaFunctionality(String bootstrapServers, int partitions, int rf) throws Exception { | ||
try ( | ||
AdminClient adminClient = AdminClient.create( | ||
ImmutableMap.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers) | ||
); | ||
KafkaProducer<String, String> producer = new KafkaProducer<>( | ||
ImmutableMap.of( | ||
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, | ||
bootstrapServers, | ||
ProducerConfig.CLIENT_ID_CONFIG, | ||
UUID.randomUUID().toString() | ||
), | ||
new StringSerializer(), | ||
new StringSerializer() | ||
); | ||
KafkaConsumer<String, String> consumer = new KafkaConsumer<>( | ||
ImmutableMap.of( | ||
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, | ||
bootstrapServers, | ||
ConsumerConfig.GROUP_ID_CONFIG, | ||
"tc-" + UUID.randomUUID(), | ||
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, | ||
"earliest" | ||
), | ||
new StringDeserializer(), | ||
new StringDeserializer() | ||
); | ||
) { | ||
String topicName = "messages-" + UUID.randomUUID(); | ||
|
||
Collection<NewTopic> topics = Collections.singletonList(new NewTopic(topicName, partitions, (short) rf)); | ||
adminClient.createTopics(topics).all().get(30, TimeUnit.SECONDS); | ||
|
||
consumer.subscribe(Collections.singletonList(topicName)); | ||
|
||
producer.send(new ProducerRecord<>(topicName, "testcontainers", "rulezzz")).get(); | ||
|
||
Unreliables.retryUntilTrue( | ||
10, | ||
TimeUnit.SECONDS, | ||
() -> { | ||
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); | ||
|
||
if (records.isEmpty()) { | ||
return false; | ||
} | ||
|
||
assertThat(records) | ||
.hasSize(1) | ||
.extracting(ConsumerRecord::topic, ConsumerRecord::key, ConsumerRecord::value) | ||
.containsExactly(tuple(topicName, "testcontainers", "rulezzz")); | ||
|
||
return true; | ||
} | ||
); | ||
|
||
consumer.unsubscribe(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
modules/redpanda/src/test/java/org/testcontainers/redpanda/CompatibleImageTest.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,28 @@ | ||
package org.testcontainers.redpanda; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class CompatibleImageTest extends AbstractRedpanda { | ||
|
||
private final String image; | ||
|
||
public CompatibleImageTest(String image) { | ||
this.image = image; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{0}") | ||
public static String[] image() { | ||
return new String[] { "docker.redpanda.com/vectorized/redpanda:v22.2.1", "redpandadata/redpanda:v22.2.1" }; | ||
} | ||
|
||
@Test | ||
public void shouldProduceAndConsumeMessage() throws Exception { | ||
try (RedpandaContainer container = new RedpandaContainer(this.image)) { | ||
container.start(); | ||
testKafkaFunctionality(container.getBootstrapServers()); | ||
} | ||
} | ||
} |
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