org.testcontainers.containers.KafkaContainer Java Examples

The following examples show how to use org.testcontainers.containers.KafkaContainer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaTestContainerManager.java    From apicurio-registry with Apache License 2.0 6 votes vote down vote up
public Map<String, String> start() {
    String bootstrapServers = "localhost:9092";
    if (!skipKafkaContainer) {
        kafka = new KafkaContainer();
        kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1");
        kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1");
        kafka.start();

        bootstrapServers = kafka.getBootstrapServers();

        createTopics(bootstrapServers);
    }
    return Collections.singletonMap(
            CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG,
            bootstrapServers
    );
}
 
Example #2
Source File: CamelKafkaTestResource.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, String> start() {
    LOGGER.info(TestcontainersConfiguration.getInstance().toString());

    try {
        container = new KafkaContainer(CONFLUENT_PLATFORM_VERSION)
                .withEmbeddedZookeeper()
                .waitingFor(Wait.forListeningPort());

        container.start();

        return Collections.singletonMap("camel.component.kafka.brokers", container.getBootstrapServers());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: KafkaLegacyClientIT.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() {
    // confluent versions 5.3.x correspond Kafka versions 2.3.x -
    // https://docs.confluent.io/current/installation/versions-interoperability.html#cp-and-apache-ak-compatibility
    kafka = new KafkaContainer("5.3.0");
    kafka.start();
    kafkaPort = kafka.getMappedPort(KafkaContainer.KAFKA_PORT);
    bootstrapServers = kafka.getBootstrapServers();
    consumerThread = new Consumer();
    consumerThread.start();
    replyConsumer = createKafkaConsumer();
    replyConsumer.subscribe(Collections.singletonList(REPLY_TOPIC));
    producer = new KafkaProducer<>(
        ImmutableMap.of(
            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers,
            ProducerConfig.CLIENT_ID_CONFIG, UUID.randomUUID().toString(),
            // This should guarantee that records are batched, as long as they are sent within the configured duration
            ProducerConfig.LINGER_MS_CONFIG, 50
        ),
        new StringSerializer(),
        new StringSerializer()
    );
}
 
Example #4
Source File: KafkaLegacyBrokerIT.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() {
    reporter.disableDestinationAddressCheck();

    // confluent versions 3.2.x correspond Kafka versions 0.10.2.2 -
    // https://docs.confluent.io/current/installation/versions-interoperability.html#cp-and-apache-ak-compatibility
    kafka = new KafkaContainer("3.2.2");
    kafka.start();
    bootstrapServers = kafka.getBootstrapServers();
    consumerThread = new Consumer();
    consumerThread.start();
    replyConsumer = createKafkaConsumer();
    replyConsumer.subscribe(Collections.singletonList(REPLY_TOPIC));
    producer = new KafkaProducer<>(
        ImmutableMap.of(
            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers,
            ProducerConfig.CLIENT_ID_CONFIG, UUID.randomUUID().toString(),
            // This should guarantee that records are batched, as long as they are sent within the configured duration
            ProducerConfig.LINGER_MS_CONFIG, 50
        ),
        new StringSerializer(),
        new StringSerializer()
    );
}
 
Example #5
Source File: KafkaIT.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() {
    // confluent versions 5.3.0 correspond Kafka versions 2.3.0 -
    // https://docs.confluent.io/current/installation/versions-interoperability.html#cp-and-apache-ak-compatibility
    kafka = new KafkaContainer("5.3.0");
    kafka.start();
    kafkaPort = kafka.getMappedPort(KafkaContainer.KAFKA_PORT);
    bootstrapServers = kafka.getBootstrapServers();
    consumerThread = new Consumer();
    consumerThread.start();
    replyConsumer = createKafkaConsumer();
    replyConsumer.subscribe(Collections.singletonList(REPLY_TOPIC));
    producer = new KafkaProducer<>(
        ImmutableMap.of(
            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers,
            ProducerConfig.CLIENT_ID_CONFIG, UUID.randomUUID().toString(),
            // This should guarantee that records are batched, as long as they are sent within the configured duration
            ProducerConfig.LINGER_MS_CONFIG, 50
        ),
        new StringSerializer(),
        new StringSerializer()
    );
}
 
Example #6
Source File: KafkaSinkTester.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected KafkaContainer createSinkService(PulsarCluster cluster) {
    return new KafkaContainer()
            .withEmbeddedZookeeper()
            .withNetworkAliases(containerName)
            .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd
                .withName(containerName)
                .withHostName(cluster.getClusterName() + "-" + containerName));
}
 
Example #7
Source File: EventRegistryKafkaConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "stop")
public KafkaContainer kafkaContainer() {
    KafkaContainer container = new KafkaContainer();
    container.withEnv("KAFKA_DELETE_TOPIC_ENABLE", "true");
    container.withEnv("KAFKA_GROUP_MIN_SESSION_TIMEOUT_MS ", "500");
    container.start();
    return container;
}
 
Example #8
Source File: EventRegistryKafkaConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public AdminClient kafkaAdminClient(KafkaContainer kafkaContainer) {
    Map<String, Object> adminProperties = new HashMap<>();
    adminProperties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());

    return AdminClient.create(adminProperties);
}
 
Example #9
Source File: EventRegistryKafkaConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<?, ?> kafkaProducerFactory(KafkaContainer kafkaContainer) {
    Map<String, Object> producerProperties = new HashMap<>();
    producerProperties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());
    producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

    return new DefaultKafkaProducerFactory<>(producerProperties);
}
 
Example #10
Source File: EventRegistryKafkaConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaContainer kafkaContainer) {
    Map<String, Object> consumerProperties = new HashMap<>();
    consumerProperties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());
    consumerProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    consumerProperties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 500);
    consumerProperties.put(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG, 400);

    return new DefaultKafkaConsumerFactory<>(consumerProperties);
}
 
Example #11
Source File: KafkaResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    kafka = new KafkaContainer();
    kafka.start();
    System.setProperty("kafka.bootstrap.servers", kafka.getBootstrapServers());
    return Collections.emptyMap();
}
 
Example #12
Source File: KafkaResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    KafkaContainer KAFKA = new KafkaContainer();
    KAFKA.start();
    System.setProperty("kafka.bootstrap.servers", KAFKA.getBootstrapServers());
    return Collections.emptyMap();
}
 
Example #13
Source File: KafkaConnectConverterIT.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
private KafkaConsumer<byte[], byte[]> getConsumerBytes(KafkaContainer kafkaContainer) {
    return new KafkaConsumer<>(
        ImmutableMap.of(
            ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092",
            ConsumerConfig.GROUP_ID_CONFIG, "tc-" + UUID.randomUUID(),
            ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"),
        new ByteArrayDeserializer(),
        new ByteArrayDeserializer());
}
 
Example #14
Source File: KafkaFacade.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
public void start() {
    LOGGER.info("Starting kafka container");
    kafkaContainer = new KafkaContainer();
    kafkaContainer.addEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1");
    kafkaContainer.addEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1");
    kafkaContainer.start();
}
 
Example #15
Source File: SchemaRegistryContainer.java    From kafka-tutorials with Apache License 2.0 4 votes vote down vote up
SchemaRegistryContainer withKafka(KafkaContainer kafka) {
    return withKafka(kafka.getNetwork(), kafka.getNetworkAliases().get(0) + ":9092");
}
 
Example #16
Source File: LiiklusContainer.java    From liiklus with MIT License 4 votes vote down vote up
public LiiklusContainer withKafka(KafkaContainer kafkaContainer) {
    return withKafka(kafkaContainer.getNetwork(), kafkaContainer.getNetworkAliases().get(0) + ":9092");
}
 
Example #17
Source File: TestingKafka.java    From presto with Apache License 2.0 4 votes vote down vote up
public TestingKafka()
{
    container = new KafkaContainer("5.4.1");
}
 
Example #18
Source File: TopicManagerIT.java    From kafka-topology-builder with MIT License 4 votes vote down vote up
@BeforeClass
public static void setup() {
  container = new KafkaContainer("5.3.1");
  container.start();
}
 
Example #19
Source File: KafkaConnectConverterIT.java    From apicurio-registry with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void startContainers() {
    String apicurioVersion = System.getProperty("project.version");
    assertNotNull(apicurioVersion);

    Path converterDistro = Paths.get(System.getProperty("user.dir"), "..", "distro", "connect-converter",
            "target", "apicurio-kafka-connect-converter-" + apicurioVersion + "-converter.tar.gz");

    if (Files.notExists(converterDistro)) {
        LOGGER.info("Connecter distribution {}", converterDistro.toString());
        throw new IllegalStateException("Kafka connect converter distribution is not present");
    }

    ImageFromDockerfile apicurioDebeziumImage = new ImageFromDockerfile()
            .withFileFromPath("converter-distro.tar.gz", converterDistro)
            .withDockerfileFromBuilder(builder -> builder
                    .from("debezium/connect:1.1.1.Final")
                    .env("KAFKA_CONNECT_DEBEZIUM_DIR", "$KAFKA_CONNECT_PLUGINS_DIR/debezium-connector-postgres")
                    .copy("converter-distro.tar.gz", "$KAFKA_CONNECT_DEBEZIUM_DIR/apicurio-kafka-connect-converter.tar.gz")
                    .run("cd $KAFKA_CONNECT_DEBEZIUM_DIR && tar -xvf apicurio-kafka-connect-converter.tar.gz")
                    .build());

    if (!TestUtils.isExternalRegistry()) {
        Testcontainers.exposeHostPorts(8081);
    }

    Testcontainers.exposeHostPorts(9092);
    kafka = new KafkaContainer();
    kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1");
    kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1");
    kafka.addExposedPorts(9092);
    kafka.withCreateContainerCmdModifier(cmd -> {
        cmd
                .withHostName("localhost")
                .withPortBindings(new PortBinding(Ports.Binding.bindPort(9092), new ExposedPort(9092)));
    });
    kafka.start();

    Network network = Network.newNetwork();

    postgres = new PostgreSQLContainer<>("debezium/postgres:11")
          .withNetwork(network)
          .withNetworkAliases("postgres");
    postgres.start();

    debeziumContainer = new DebeziumContainer("dummy-version");
    debeziumContainer.setImage(apicurioDebeziumImage);
    debeziumContainer.withNetwork(network)
          .withEnv("BOOTSTRAP_SERVERS", "host.testcontainers.internal:9092")
          .withLogConsumer(new Slf4jLogConsumer(LOGGER));
    debeziumContainer.setWaitStrategy(
            Wait.forHttp("/connectors")
            .forPort(8083)
            .forStatusCode(200)
            .withReadTimeout(Duration.ofSeconds(3))
            .withStartupTimeout(Duration.ofSeconds(300)));
    debeziumContainer.start();

}
 
Example #20
Source File: KafkaSourceTester.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public void setServiceContainer(KafkaContainer container) {
    this.kafkaContainer = container;
}