Skip to content

Commit

Permalink
docker: switches to testcontainers jupiter (#1407)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <adrian@tetrate.io>
  • Loading branch information
codefromthecrypt authored Jan 15, 2024
1 parent 0ec2f9b commit b62f266
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 199 deletions.
2 changes: 1 addition & 1 deletion instrumentation/kafka-clients/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
4 changes: 2 additions & 2 deletions instrumentation/kafka-clients/src/it/kafka1/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013-2023 The OpenZipkin Authors
Copyright 2013-2024 The OpenZipkin 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
Expand Down Expand Up @@ -86,7 +86,7 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<artifactId>junit-jupiter</artifactId>
<version>@testcontainers.version@</version>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.TopicPartition;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static brave.kafka.clients.KafkaTags.KAFKA_TOPIC_TAG;
import static brave.kafka.clients.KafkaTest.TEST_KEY;
Expand All @@ -48,9 +51,11 @@
import static brave.messaging.MessagingRequestMatchers.operationEquals;
import static org.assertj.core.api.Assertions.assertThat;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("docker")
@Testcontainers(disabledWithoutDocker = true)
@Timeout(60)
public class ITKafkaTracing extends ITKafka { // public for src/it
@RegisterExtension KafkaExtension kafka = new KafkaExtension();
@Container KafkaContainer kafka = new KafkaContainer();
@RegisterExtension IntegrationTestSpanHandler producerSpanHandler =
new IntegrationTestSpanHandler();
@RegisterExtension IntegrationTestSpanHandler consumerSpanHandler =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.opentest4j.TestAbortedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
Expand All @@ -31,32 +27,20 @@

import static org.testcontainers.utility.DockerImageName.parse;

class KafkaExtension implements BeforeAllCallback, AfterAllCallback {
static final Logger LOGGER = LoggerFactory.getLogger(KafkaExtension.class);
final class KafkaContainer extends GenericContainer<KafkaContainer> {
static final Logger LOGGER = LoggerFactory.getLogger(KafkaContainer.class);
static final int KAFKA_PORT = 19092;

final KafkaContainer container = new KafkaContainer();

@Override public void beforeAll(ExtensionContext context) {
if (context.getRequiredTestClass().getEnclosingClass() != null) {
// Only run once in outermost scope.
return;
}

container.start();
LOGGER.info("Using bootstrapServer " + bootstrapServer());
KafkaContainer() {
super(parse("ghcr.io/openzipkin/zipkin-kafka:3.0.2"));
waitStrategy = Wait.forHealthcheck();
// Kafka broker listener port (19092) needs to be exposed for test cases to access it.
addFixedExposedPort(KAFKA_PORT, KAFKA_PORT, InternetProtocol.TCP);
withLogConsumer(new Slf4jLogConsumer(LOGGER));
}

String bootstrapServer() {
return container.getHost() + ":" + container.getMappedPort(KAFKA_PORT);
}

@Override public void afterAll(ExtensionContext context) {
if (context.getRequiredTestClass().getEnclosingClass() != null) {
// Only run once in outermost scope.
return;
}
container.stop();
return getHost() + ":" + getMappedPort(KAFKA_PORT);
}

KafkaProducer<String, String> createStringProducer() {
Expand Down Expand Up @@ -91,18 +75,4 @@ Properties consumerConfig() {
props.put("metadata.max.age.ms", "100");
return props;
}

// mostly waiting for https://github.com/testcontainers/testcontainers-java/issues/3537
static final class KafkaContainer extends GenericContainer<KafkaContainer> {
KafkaContainer() {
super(parse("ghcr.io/openzipkin/zipkin-kafka:2.27.0"));
if ("true".equals(System.getProperty("docker.skip"))) {
throw new TestAbortedException("${docker.skip} == true");
}
waitStrategy = Wait.forHealthcheck();
// Kafka broker listener port (19092) needs to be exposed for test cases to access it.
addFixedExposedPort(KAFKA_PORT, KAFKA_PORT, InternetProtocol.TCP);
withLogConsumer(new Slf4jLogConsumer(LOGGER));
}
}
}
2 changes: 1 addition & 1 deletion instrumentation/kafka-streams/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,25 @@
import org.apache.kafka.streams.kstream.Consumed;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static brave.Span.Kind.CONSUMER;
import static brave.Span.Kind.PRODUCER;
import static brave.kafka.streams.KafkaStreamsTracingTest.TEST_KEY;
import static brave.kafka.streams.KafkaStreamsTracingTest.TEST_VALUE;
import static org.assertj.core.api.Assertions.assertThat;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("docker")
@Testcontainers(disabledWithoutDocker = true)
@Timeout(60)
class ITKafkaStreamsTracing extends ITKafkaStreams {
@RegisterExtension KafkaExtension kafka = new KafkaExtension();
@Container KafkaContainer kafka = new KafkaContainer();

Producer<String, String> producer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.opentest4j.TestAbortedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
Expand All @@ -31,32 +27,20 @@

import static org.testcontainers.utility.DockerImageName.parse;

class KafkaExtension implements BeforeAllCallback, AfterAllCallback {
static final Logger LOGGER = LoggerFactory.getLogger(KafkaExtension.class);
final class KafkaContainer extends GenericContainer<KafkaContainer> {
static final Logger LOGGER = LoggerFactory.getLogger(KafkaContainer.class);
static final int KAFKA_PORT = 19092;

final KafkaContainer container = new KafkaContainer();

@Override public void beforeAll(ExtensionContext context) {
if (context.getRequiredTestClass().getEnclosingClass() != null) {
// Only run once in outermost scope.
return;
}

container.start();
LOGGER.info("Using bootstrapServer " + bootstrapServer());
KafkaContainer() {
super(parse("ghcr.io/openzipkin/zipkin-kafka:3.0.2"));
waitStrategy = Wait.forHealthcheck();
// Kafka broker listener port (19092) needs to be exposed for test cases to access it.
addFixedExposedPort(KAFKA_PORT, KAFKA_PORT, InternetProtocol.TCP);
withLogConsumer(new Slf4jLogConsumer(LOGGER));
}

String bootstrapServer() {
return container.getHost() + ":" + container.getMappedPort(KAFKA_PORT);
}

@Override public void afterAll(ExtensionContext context) {
if (context.getRequiredTestClass().getEnclosingClass() != null) {
// Only run once in outermost scope.
return;
}
container.stop();
return getHost() + ":" + getMappedPort(KAFKA_PORT);
}

KafkaProducer<String, String> createStringProducer() {
Expand Down Expand Up @@ -91,18 +75,4 @@ Properties consumerConfig() {
props.put("metadata.max.age.ms", "100");
return props;
}

// mostly waiting for https://github.com/testcontainers/testcontainers-java/issues/3537
static final class KafkaContainer extends GenericContainer<KafkaContainer> {
KafkaContainer() {
super(parse("ghcr.io/openzipkin/zipkin-kafka:2.27.0"));
if ("true".equals(System.getProperty("docker.skip"))) {
throw new TestAbortedException("${docker.skip} == true");
}
waitStrategy = Wait.forHealthcheck();
// Kafka broker listener port (19092) needs to be exposed for test cases to access it.
addFixedExposedPort(KAFKA_PORT, KAFKA_PORT, InternetProtocol.TCP);
withLogConsumer(new Slf4jLogConsumer(LOGGER));
}
}
}
2 changes: 1 addition & 1 deletion instrumentation/mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 The OpenZipkin Authors
* Copyright 2013-2024 The OpenZipkin 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
Expand Down Expand Up @@ -32,38 +32,42 @@
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.Document;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.Timeout;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static brave.Span.Kind.CLIENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;

@TestInstance(TestInstance.Lifecycle.PER_CLASS) class ITMongoDBTracing extends ITRemote {
@Tag("docker")
@Testcontainers(disabledWithoutDocker = true)
@Timeout(60)
class ITMongoDBTracing extends ITRemote {
static final String DATABASE_NAME = "myDatabase";
static final String COLLECTION_NAME = "myCollection";
static final String INVALID_COLLECTION_NAME = "?.$";

@RegisterExtension MongoDBExtension mongo = new MongoDBExtension();
@Container MongoDBContainer mongo = new MongoDBContainer();
CommandListener listener = MongoDBTracing.newBuilder(tracing).build().commandListener();
MongoClientSettings settings;
MongoClient mongoClient;
MongoDatabase database;
String clusterId;

@BeforeAll void initClient() {
@BeforeEach void initClient() {
settings = mongo.mongoClientSettingsBuilder().addCommandListener(listener).build();
mongoClient = MongoClients.create(settings);
database = mongoClient.getDatabase(DATABASE_NAME);
}

@AfterAll void closeClient() {
@AfterEach void closeClient() {
mongoClient.close();
}

Expand Down
Loading

0 comments on commit b62f266

Please sign in to comment.