Skip to content

Commit

Permalink
bump test containers version (#62)
Browse files Browse the repository at this point in the history
* bump test containers version

Signed-off-by: see-quick <maros.orsak159@gmail.com>

* Add Toxiproxy client + Proxy remove deprecated stuff

Signed-off-by: see-quick <maros.orsak159@gmail.com>

* checkstyle

Signed-off-by: see-quick <maros.orsak159@gmail.com>

* Update to latest tc version

Signed-off-by: see-quick <maros.orsak159@gmail.com>

* update to use random local port

Signed-off-by: see-quick <maros.orsak159@gmail.com>

---------

Signed-off-by: see-quick <maros.orsak159@gmail.com>
  • Loading branch information
see-quick authored Nov 30, 2023
1 parent 60b6e7d commit 2bf5ae6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@
<maven.compiler.source>11</maven.compiler.source>

<!-- DEPENDENCY CODE VERSIONS -->
<test-containers.version>1.17.4</test-containers.version>
<test-containers.version>1.19.3</test-containers.version>
<spotbugs.version>4.7.2</spotbugs.version>
<log4j.version>2.17.1</log4j.version>
<docker-java.version>3.2.13</docker-java.version>
<docker-java.version>3.3.3</docker-java.version>
<kafka.version>3.2.3</kafka.version>
<slf4j.version>1.7.32</slf4j.version>
<fasterxml.jackson-core.version>2.13.4</fasterxml.jackson-core.version>
<fasterxml.jackson-databind.version>2.13.4.2</fasterxml.jackson-databind.version>
<toxiproxy.java.version>2.1.7</toxiproxy.java.version>

<!-- DEPENDENCY TEST VERSIONS -->
<jupiter.version>5.9.1</jupiter.version>
Expand Down Expand Up @@ -172,6 +173,11 @@
<artifactId>jackson-databind</artifactId>
<version>${fasterxml.jackson-databind.version}</version>
</dependency>
<dependency>
<groupId>eu.rekawek.toxiproxy</groupId>
<artifactId>toxiproxy-java</artifactId>
<version>${toxiproxy.java.version}</version>
</dependency>

<!-- TEST DEPENDENCY -->
<dependency>
Expand Down
50 changes: 37 additions & 13 deletions src/main/java/io/strimzi/test/container/StrimziKafkaContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;
import eu.rekawek.toxiproxy.Proxy;
import eu.rekawek.toxiproxy.ToxiproxyClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
Expand All @@ -16,6 +18,7 @@
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.MountableFile;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -75,7 +78,8 @@ public class StrimziKafkaContainer extends GenericContainer<StrimziKafkaContaine

// proxy attributes
private ToxiproxyContainer proxyContainer;
private ToxiproxyContainer.ContainerProxy proxy;
private ToxiproxyClient toxiproxyClient;
private Proxy proxy;

/**
* Image name is specified lazily automatically in {@link #doStart()} method
Expand Down Expand Up @@ -109,9 +113,15 @@ private StrimziKafkaContainer(CompletableFuture<String> imageName) {

@Override
protected void doStart() {
if (proxyContainer != null && !proxyContainer.isRunning()) {
proxyContainer.start();
if (this.proxyContainer != null && !this.proxyContainer.isRunning()) {
this.proxyContainer.start();

// Instantiate a ToxiproxyClient if it has not been previously provided via configuration settings.
if (toxiproxyClient == null) {
toxiproxyClient = new ToxiproxyClient(this.proxyContainer.getHost(), this.proxyContainer.getControlPort());
}
}

if (!this.imageNameProvider.isDone()) {
this.imageNameProvider.complete(KafkaVersionService.strimziTestContainerImageName(this.kafkaVersion));
}
Expand Down Expand Up @@ -337,9 +347,7 @@ public String getInternalZooKeeperConnect() {
public String getBootstrapServers() {
if (proxyContainer != null) {
// returning the proxy host and port for indirect connection
return String.format("PLAINTEXT://%s:%d",
getProxy().getContainerIpAddress(),
getProxy().getProxyPort());
return String.format("PLAINTEXT://%s", getProxy().getListen());
}
return bootstrapServersProvider.apply(this);
}
Expand Down Expand Up @@ -470,17 +478,33 @@ public StrimziKafkaContainer withProxyContainer(final ToxiproxyContainer proxyCo
}

/**
* Returns the proxy for this Kafka broker if configured.
* Retrieves a synchronized Proxy instance for this Kafka broker.
*
* This method ensures that only one instance of Proxy is created per broker. If the proxy has not been
* initialized, it attempts to create one using the Toxiproxy client. If the Toxiproxy client is not initialized,
* it is created using the host and control port of the proxy container.
*
* @return ToxiproxyContainer.ContainerProxy instance
* @return Proxy instance for this Kafka broker.
* @throws IllegalStateException if the proxy container has not been configured.
* @throws RuntimeException if an IOException occurs during the creation of the Proxy.
*/
public synchronized ToxiproxyContainer.ContainerProxy getProxy() {
if (proxyContainer == null) {
public synchronized Proxy getProxy() {
if (this.proxyContainer == null) {
throw new IllegalStateException("The proxy container has not been configured");
}
if (proxy == null) {
this.proxy = proxyContainer.getProxy(this, KAFKA_PORT);

if (this.proxy == null) {
if (this.toxiproxyClient == null) {
this.toxiproxyClient = new ToxiproxyClient(proxyContainer.getHost(), proxyContainer.getControlPort());
}
try {
final int listenPort = 8666 + this.brokerId;
this.proxy = this.toxiproxyClient.createProxy("kafka" + this.brokerId, "0.0.0.0:" + listenPort, "toxiproxy:" + Utils.getFreePort());
} catch (IOException e) {
LOGGER.error("Error happened during creation of the Proxy: {}", e.getMessage());
throw new RuntimeException(e);
}
}
return proxy;
return this.proxy;
}
}
15 changes: 14 additions & 1 deletion src/main/java/io/strimzi/test/container/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
*/
package io.strimzi.test.container;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.util.function.BooleanSupplier;

import org.slf4j.Logger;
Expand Down Expand Up @@ -79,5 +81,16 @@ static long waitFor(String description, long pollIntervalMs, long timeoutMs, Boo
}
}


/**
* Finds a free server port which can be used by the web server
*
* @return A free TCP port
*/
public static int getFreePort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException("Failed to find free port", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import eu.rekawek.toxiproxy.Proxy;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
Expand Down Expand Up @@ -130,15 +132,15 @@ void testKafkaClusterFunctionality() throws InterruptedException, ExecutionExcep
@Test
void testStartClusterWithProxyContainer() {
ToxiproxyContainer proxyContainer = new ToxiproxyContainer(
DockerImageName.parse("ghcr.io/shopify/toxiproxy:2.4.0")
DockerImageName.parse("ghcr.io/shopify/toxiproxy:2.6.0")
.asCompatibleSubstituteFor("shopify/toxiproxy"));

StrimziKafkaCluster kafkaCluster = new StrimziKafkaCluster(3, proxyContainer);
kafkaCluster.start();

List<String> bootstrapUrls = new ArrayList<>();
for (KafkaContainer kafkaContainer : kafkaCluster.getBrokers()) {
ToxiproxyContainer.ContainerProxy proxy = ((StrimziKafkaContainer) kafkaContainer).getProxy();
Proxy proxy = ((StrimziKafkaContainer) kafkaContainer).getProxy();
assertThat(proxy, notNullValue());
bootstrapUrls.add(kafkaContainer.getBootstrapServers());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,16 @@ void testStartBrokerWithProxyContainer(final String imageName) {
assumeDocker();

ToxiproxyContainer proxyContainer = new ToxiproxyContainer(
DockerImageName.parse("ghcr.io/shopify/toxiproxy:2.4.0")
DockerImageName.parse("ghcr.io/shopify/toxiproxy:2.6.0")
.asCompatibleSubstituteFor("shopify/toxiproxy"));

systemUnderTest = new StrimziKafkaContainer(imageName)
.withProxyContainer(proxyContainer)
.waitForRunning();
systemUnderTest.start();

ToxiproxyContainer.ContainerProxy proxy = systemUnderTest.getProxy();
assertThat(systemUnderTest.getBootstrapServers(),
is(String.format("PLAINTEXT://%s:%d", proxy.getContainerIpAddress(), proxy.getProxyPort())));
is(String.format("PLAINTEXT://%s", systemUnderTest.getProxy().getListen())));

systemUnderTest.stop();
}
Expand Down

0 comments on commit 2bf5ae6

Please sign in to comment.