Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer only contents of Kafka and ZooKeeper properties files #76

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/main/java/io/strimzi/test/container/StrimziKafkaContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class StrimziKafkaContainer extends GenericContainer<StrimziKafkaContaine
private boolean useKraft;
private Function<StrimziKafkaContainer, String> bootstrapServersProvider = c -> String.format("PLAINTEXT://%s:%s", getHost(), this.kafkaExposedPort);
private String clusterId;
private MountableFile serverPropertiesFile;

// proxy attributes
private ToxiproxyContainer proxyContainer;
Expand Down Expand Up @@ -282,6 +283,11 @@ protected void containerIsStarting(final InspectContainerResponse containerInfo,
command += "bin/kafka-server-start.sh config/kraft/server.properties" + kafkaConfigurationOverride;
}

Utils.asTransferableBytes(serverPropertiesFile).ifPresent(properties -> copyFileToContainer(
properties,
this.useKraft ? "/opt/kafka/config/kraft/server.properties" : "/opt/kafka/config/server.properties"
));

LOGGER.info("Copying command to 'STARTER_SCRIPT' script.");

copyFileToContainer(
Expand Down Expand Up @@ -354,7 +360,7 @@ public String getBootstrapServers() {

/**
* Get the cluster id. This is only supported for KRaft containers.
* @return The cluster id.
* @return The cluster id.
*/
public String getClusterId() {
return clusterId;
Expand Down Expand Up @@ -442,8 +448,12 @@ public StrimziKafkaContainer withPort(final int fixedPort) {
* @return StrimziKafkaContainer instance
*/
public StrimziKafkaContainer withServerProperties(final MountableFile serverPropertiesFile) {
withCopyFileToContainer(serverPropertiesFile,
this.useKraft ? "/opt/kafka/config/kraft/server.properties" : "/opt/kafka/config/server.properties");
/*
* Save a reference to the file and delay copying to the container until the container
* is starting. This allows for `useKraft` to be set either before or after this method
* is called.
*/
this.serverPropertiesFile = serverPropertiesFile;
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ protected void containerIsStarting(InspectContainerResponse containerInfo, boole
* @return StrimziZookeeperContainer instance
*/
public StrimziZookeeperContainer withZooKeeperPropertiesFile(final MountableFile zooKeeperPropertiesFile) {
withCopyFileToContainer(zooKeeperPropertiesFile, "/opt/kafka/config/zookeeper.properties");
Utils.asTransferableBytes(zooKeeperPropertiesFile)
.ifPresent(properties -> withCopyToContainer(properties, "/opt/kafka/config/zookeeper.properties"));
return this;
}

Expand Down
34 changes: 33 additions & 1 deletion src/main/java/io/strimzi/test/container/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.BooleanSupplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.MountableFile;

/**
* Utils contains auxiliary static methods, which are used in whole project.
Expand Down Expand Up @@ -81,6 +87,32 @@ static long waitFor(String description, long pollIntervalMs, long timeoutMs, Boo
}
}

/**
* Converts the contents of {@code file} to a new Transferable. If the file is
* null, an empty Optional is returned. This method reads the contents of the
* file to avoid preservation of the file's owner and group attributes when
* copying into the container.
*
* @param file the source file
* @return an Optional containing the Transferable contents of file, or an empty
* Optional when the file is null.
*/
static Optional<Transferable> asTransferableBytes(MountableFile file) {
if (file != null) {
final byte[] data;

try {
data = Files.readAllBytes(Path.of(file.getFilesystemPath()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}

return Optional.of(Transferable.of(data));
}

return Optional.empty();
}

/**
* Finds a free server port which can be used by the web server
*
Expand All @@ -90,7 +122,7 @@ 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);
throw new UncheckedIOException("Failed to find free port", e);
}
}
}
Loading