cmccabe commented on a change in pull request #9986:
URL: https://github.com/apache/kafka/pull/9986#discussion_r572309452



##########
File path: core/src/test/java/kafka/test/junit/ZkClusterInvocationContext.java
##########
@@ -0,0 +1,250 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.test.junit;
+
+import integration.kafka.server.IntegrationTestHelper;
+import kafka.api.IntegrationTestHarness;
+import kafka.network.SocketServer;
+import kafka.server.KafkaServer;
+import kafka.utils.TestUtils;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import kafka.test.ClusterConfig;
+import kafka.test.ClusterInstance;
+import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
+import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import scala.Option;
+import scala.collection.JavaConverters;
+import scala.compat.java8.OptionConverters;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
+
+/**
+ * Wraps a {@link IntegrationTestHarness} inside lifecycle methods for a test 
invocation. Each instance of this
+ * class is provided with a configuration for the cluster.
+ *
+ * This context also provides parameter resolvers for:
+ *
+ * <ul>
+ *     <li>ClusterConfig (the same instance passed to the constructor)</li>
+ *     <li>ClusterInstance (includes methods to expose underlying 
SocketServer-s)</li>
+ *     <li>IntegrationTestHelper (helper methods)</li>
+ * </ul>
+ */
+public class ZkClusterInvocationContext implements 
TestTemplateInvocationContext {
+
+    private final ClusterConfig clusterConfig;
+    private final AtomicReference<IntegrationTestHarness> clusterReference;
+
+    public ZkClusterInvocationContext(ClusterConfig clusterConfig) {
+        this.clusterConfig = clusterConfig;
+        this.clusterReference = new AtomicReference<>();
+    }
+
+    @Override
+    public String getDisplayName(int invocationIndex) {
+        String clusterDesc = clusterConfig.nameTags().entrySet().stream()
+            .map(Object::toString)
+            .collect(Collectors.joining(", "));
+        return String.format("[Zk %d] %s", invocationIndex, clusterDesc);
+    }
+
+    @Override
+    public List<Extension> getAdditionalExtensions() {
+        ClusterInstance clusterShim = new ZkClusterInstance(clusterConfig, 
clusterReference);
+        return Arrays.asList(
+            (BeforeTestExecutionCallback) context -> {
+                // We have to wait to actually create the underlying cluster 
until after our @BeforeEach methods
+                // have run. This allows tests to set up external dependencies 
like ZK, MiniKDC, etc.
+                // However, since we cannot create this instance until we are 
inside the test invocation, we have
+                // to use a container class (AtomicReference) to provide this 
cluster object to the test itself
+
+                // This is what tests normally extend from to start a cluster, 
here we create it anonymously and
+                // configure the cluster using values from ClusterConfig
+                IntegrationTestHarness cluster = new IntegrationTestHarness() {
+                    @Override
+                    public Properties serverConfig() {
+                        return clusterConfig.serverProperties();
+                    }
+
+                    @Override
+                    public Properties adminClientConfig() {
+                        return clusterConfig.adminClientProperties();
+                    }
+
+                    @Override
+                    public Properties consumerConfig() {
+                        return clusterConfig.consumerProperties();
+                    }
+
+                    @Override
+                    public Properties producerConfig() {
+                        return clusterConfig.producerProperties();
+                    }
+
+                    @Override
+                    public SecurityProtocol securityProtocol() {
+                        return 
SecurityProtocol.forName(clusterConfig.securityProtocol());
+                    }
+
+                    @Override
+                    public ListenerName listenerName() {
+                        return 
clusterConfig.listenerName().map(ListenerName::normalised)
+                            .orElseGet(() -> 
ListenerName.forSecurityProtocol(securityProtocol()));
+                    }
+
+                    @Override
+                    public Option<Properties> serverSaslProperties() {
+                        if (clusterConfig.saslServerProperties().isEmpty()) {
+                            return Option.empty();
+                        } else {
+                            return 
Option.apply(clusterConfig.saslServerProperties());
+                        }
+                    }
+
+                    @Override
+                    public Option<Properties> clientSaslProperties() {
+                        if (clusterConfig.saslClientProperties().isEmpty()) {
+                            return Option.empty();
+                        } else {
+                            return 
Option.apply(clusterConfig.saslClientProperties());
+                        }
+                    }
+
+                    @Override
+                    public int brokerCount() {
+                        // Brokers and controllers are the same in zk mode, so 
just use the max
+                        return Math.max(clusterConfig.brokers(), 
clusterConfig.controllers());
+                    }
+
+                    @Override
+                    public Option<File> trustStoreFile() {
+                        return 
OptionConverters.toScala(clusterConfig.trustStoreFile());
+                    }
+                };
+
+                clusterReference.set(cluster);
+                if (clusterConfig.isAutoStart()) {
+                    clusterShim.start();
+                }
+            },
+            (AfterTestExecutionCallback) context -> clusterShim.stop(),
+            new ClusterInstanceParameterResolver(clusterShim),
+            new GenericParameterResolver<>(clusterConfig, ClusterConfig.class),
+            new GenericParameterResolver<>(new IntegrationTestHelper(), 
IntegrationTestHelper.class)
+        );
+    }
+
+    public static class ZkClusterInstance implements ClusterInstance {
+
+        final AtomicReference<IntegrationTestHarness> clusterReference;
+        final ClusterConfig config;
+        final AtomicBoolean started = new AtomicBoolean(false);
+        final AtomicBoolean stopped = new AtomicBoolean(false);
+
+        ZkClusterInstance(ClusterConfig config, 
AtomicReference<IntegrationTestHarness> clusterReference) {
+            this.config = config;
+            this.clusterReference = clusterReference;
+        }
+
+        @Override
+        public String brokerList() {
+            return 
TestUtils.bootstrapServers(clusterReference.get().servers(), 
clusterReference.get().listenerName());
+        }
+
+        @Override
+        public Collection<SocketServer> brokerSocketServers() {
+            return 
JavaConverters.asJavaCollection(clusterReference.get().servers()).stream()
+                    .map(KafkaServer::socketServer)
+                    .collect(Collectors.toList());
+        }
+
+        @Override
+        public ListenerName listener() {
+            return clusterReference.get().listenerName();
+        }
+
+        @Override
+        public Collection<SocketServer> controllerSocketServers() {
+            return 
JavaConverters.asJavaCollection(clusterReference.get().servers()).stream()
+                .filter(broker -> broker.kafkaController().isActive())
+                .map(KafkaServer::socketServer)
+                .collect(Collectors.toList());
+        }
+
+        @Override
+        public Optional<SocketServer> anyBrokerSocketServer() {
+            return 
JavaConverters.asJavaCollection(clusterReference.get().servers()).stream()
+                .map(KafkaServer::socketServer)
+                .findFirst();
+        }
+
+        @Override
+        public Optional<SocketServer> anyControllerSocketServer() {
+            return 
JavaConverters.asJavaCollection(clusterReference.get().servers()).stream()
+                .filter(broker -> broker.kafkaController().isActive())
+                .map(KafkaServer::socketServer)
+                .findFirst();
+        }
+
+        @Override
+        public ClusterType clusterType() {
+            return ClusterType.ZK;
+        }
+
+        @Override
+        public ClusterConfig config() {
+            return config;
+        }
+
+        @Override
+        public Object getUnderlying() {

Review comment:
       Hmm... why return `Object` rather than `IntegrationTestHarness`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to