Skip to content

Instantly share code, notes, and snippets.

@emaxerrno
Created April 18, 2021 19:32
Show Gist options
  • Save emaxerrno/20cebffb8267e2b8d4fa88ecee8bf41a to your computer and use it in GitHub Desktop.
Save emaxerrno/20cebffb8267e2b8d4fa88ecee8bf41a to your computer and use it in GitHub Desktop.
//DEPS org.testcontainers:testcontainers:1.15.3
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.Transferable;
import java.nio.charset.StandardCharsets;
public class RedpandaTest {
public static void main(String[] args) {
RedpandaContainer container = new RedpandaContainer();
container.start();
}
static class RedpandaContainer extends GenericContainer<RedpandaContainer> {
private static final String STARTER_SCRIPT = "/testcontainers_start.sh";
public RedpandaContainer() {
super("vectorized/redpanda:v21.4.12");
withExposedPorts(9092);
withCreateContainerCmdModifier(cmd -> {
cmd.withEntrypoint("sh");
});
withCommand("-c", "while [ ! -f " + STARTER_SCRIPT + " ]; do sleep 0.1; done; " + STARTER_SCRIPT);
waitingFor(Wait.forLogMessage(".*Started Kafka API server.*", 1));
}
@Override
protected void containerIsStarting(InspectContainerResponse containerInfo) {
super.containerIsStarting(containerInfo);
String command = "#!/bin/bash\n";
command += "/usr/bin/rpk redpanda start --overprovisioned --smp 1 --reserve-memory 0M --node-id 0 --check=false ";
command += "--kafka-addr PLAINTEXT://0.0.0.0:29092,OUTSIDE://0.0.0.0:9092 ";
command += "--advertise-kafka-addr OUTSIDE://" + getHost() + ":" + getMappedPort(9092);
copyFileToContainer(
Transferable.of(command.getBytes(StandardCharsets.UTF_8), 0777),
STARTER_SCRIPT
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment