diff --git a/kafka-test/src/main/java/io/specmesh/kafka/DockerKafkaEnvironment.java b/kafka-test/src/main/java/io/specmesh/kafka/DockerKafkaEnvironment.java index d56b0966..2785bb85 100644 --- a/kafka-test/src/main/java/io/specmesh/kafka/DockerKafkaEnvironment.java +++ b/kafka-test/src/main/java/io/specmesh/kafka/DockerKafkaEnvironment.java @@ -85,6 +85,7 @@ public final class DockerKafkaEnvironment private final Map srEnv; private final Set aclBindings; private final Optional adminUser; + private final Optional explicitNetwork; private Network network; private KafkaContainer kafkaBroker; @@ -108,7 +109,8 @@ private DockerKafkaEnvironment( final Optional srDockerImage, final Map srEnv, final Set aclBindings, - final Optional adminUser) { + final Optional adminUser, + final Optional explicitNetwork) { this.startUpTimeout = requireNonNull(startUpTimeout, "startUpTimeout"); this.startUpAttempts = startUpAttempts; this.kafkaDockerImage = requireNonNull(kafkaDockerImage, "kafkaDockerImage"); @@ -117,6 +119,7 @@ private DockerKafkaEnvironment( this.srEnv = Map.copyOf(requireNonNull(srEnv, "srEnv")); this.aclBindings = Set.copyOf(requireNonNull(aclBindings, "aclBindings")); this.adminUser = requireNonNull(adminUser, "credentials"); + this.explicitNetwork = requireNonNull(explicitNetwork, "explicitNetwork"); tearDown(); } @@ -187,7 +190,7 @@ private void setUp() { return; } - network = Network.newNetwork(); + network = explicitNetwork.orElseGet(Network::newNetwork); kafkaBroker = new KafkaContainer(kafkaDockerImage) @@ -232,7 +235,9 @@ private void tearDown() { } if (network != null) { - network.close(); + if (explicitNetwork.isEmpty()) { + network.close(); + } network = null; } @@ -247,6 +252,17 @@ private void installAcls() { } } + public String testNetworkKafkaBootstrapServers() { + return "PLAINTEXT://" + kafkaBroker.getNetworkAliases().get(0) + ":9092"; + } + + public String testNetworkSchemeRegistryServer() { + return "http://" + + schemaRegistry.getNetworkAliases().get(0) + + ":" + + SchemaRegistryContainer.SCHEMA_REGISTRY_PORT; + } + /** Builder of {@link DockerKafkaEnvironment}. */ public static final class Builder { @@ -271,6 +287,12 @@ public static final class Builder { private final Map userPasswords = new LinkedHashMap<>(); private boolean enableAcls = false; private final Set aclBindings = new HashSet<>(); + private Optional explicitNetwork = Optional.empty(); + + public Builder withNetwork(final Network network) { + this.explicitNetwork = Optional.of(network); + return this; + } /** * Customise the startup count. @@ -436,7 +458,8 @@ public DockerKafkaEnvironment build() { srImage, srEnv, aclBindings, - adminUser()); + adminUser(), + explicitNetwork); } private Optional adminUser() {