CURATOR-356 Allow SASL configuration for TestingServer

Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/dd20f1db
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/dd20f1db
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/dd20f1db

Branch: refs/heads/CURATOR-3.0
Commit: dd20f1dbed3879372483d7e8f7eb05d920707168
Parents: b51bb01
Author: eolivelli <eolive...@gmail.com>
Authored: Wed Oct 26 13:43:21 2016 +0200
Committer: eolivelli <eolive...@gmail.com>
Committed: Wed Oct 26 13:43:21 2016 +0200

----------------------------------------------------------------------
 .../org/apache/curator/test/InstanceSpec.java   | 28 ++++++++++-
 .../curator/test/QuorumConfigBuilder.java       |  7 +++
 .../curator/test/TestQuorumConfigBuilder.java   | 49 ++++++++++++++++++++
 3 files changed, 82 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/dd20f1db/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java
----------------------------------------------------------------------
diff --git 
a/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java 
b/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java
index b39a949..32b1738 100644
--- a/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java
+++ b/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java
@@ -25,6 +25,8 @@ import java.io.IOException;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.UnknownHostException;
+import java.util.Collections;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -69,6 +71,7 @@ public class InstanceSpec
     private final int serverId;
     private final int tickTime;
     private final int maxClientCnxns;
+    private final Map<String,Object> customProperties;
 
     public static InstanceSpec newInstanceSpec()
     {
@@ -113,7 +116,7 @@ public class InstanceSpec
      */
     public InstanceSpec(File dataDirectory, int port, int electionPort, int 
quorumPort, boolean deleteDataDirectoryOnClose, int serverId)
     {
-        this(dataDirectory, port, electionPort, quorumPort, 
deleteDataDirectoryOnClose, serverId, -1, -1);
+        this(dataDirectory, port, electionPort, quorumPort, 
deleteDataDirectoryOnClose, serverId, -1, -1, null);
     }
 
     /**
@@ -126,7 +129,22 @@ public class InstanceSpec
      * @param tickTime                   tickTime. Set -1 to used fault server 
configuration
      * @param maxClientCnxns             max number of client connections from 
the same IP. Set -1 to use default server configuration
      */
-    public InstanceSpec(File dataDirectory, int port, int electionPort, int 
quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int 
maxClientCnxns)
+    public InstanceSpec(File dataDirectory, int port, int electionPort, int 
quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int 
maxClientCnxns) {
+        this(dataDirectory, port, electionPort, quorumPort, 
deleteDataDirectoryOnClose, serverId, tickTime, maxClientCnxns, null);
+    }
+
+    /**
+     * @param dataDirectory              where to store data/logs/etc.
+     * @param port                       the port to listen on - each server 
in the ensemble must use a unique port
+     * @param electionPort               the electionPort to listen on - each 
server in the ensemble must use a unique electionPort
+     * @param quorumPort                 the quorumPort to listen on - each 
server in the ensemble must use a unique quorumPort
+     * @param deleteDataDirectoryOnClose if true, the data directory will be 
deleted when {@link TestingCluster#close()} is called
+     * @param serverId                   the server ID for the instance
+     * @param tickTime                   tickTime. Set -1 to used fault server 
configuration
+     * @param maxClientCnxns             max number of client connections from 
the same IP. Set -1 to use default server configuration
+     * @param customProperties           other properties to be passed to the 
server
+     */
+    public InstanceSpec(File dataDirectory, int port, int electionPort, int 
quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int 
maxClientCnxns, Map<String,Object> customProperties)
     {
         this.dataDirectory = (dataDirectory != null) ? dataDirectory : 
Files.createTempDir();
         this.port = (port >= 0) ? port : getRandomPort();
@@ -136,6 +154,7 @@ public class InstanceSpec
         this.serverId = (serverId >= 0) ? serverId : 
nextServerId.getAndIncrement();
         this.tickTime = (tickTime > 0 ? tickTime : -1); // -1 to set default 
value
         this.maxClientCnxns = (maxClientCnxns >= 0 ? maxClientCnxns : -1); // 
-1 to set default value
+        this.customProperties = customProperties != null ? 
Collections.<String,Object>unmodifiableMap(customProperties) : 
Collections.<String,Object>emptyMap();
     }
 
     public int getServerId()
@@ -183,6 +202,10 @@ public class InstanceSpec
         return deleteDataDirectoryOnClose;
     }
 
+    public Map<String, Object> getCustomProperties() {
+        return customProperties;
+    }
+
     @Override
     public String toString()
     {
@@ -195,6 +218,7 @@ public class InstanceSpec
             ", serverId=" + serverId +
             ", tickTime=" + tickTime +
             ", maxClientCnxns=" + maxClientCnxns +
+            ", customProperties=" + customProperties +
             "} " + super.toString();
     }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/dd20f1db/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java
----------------------------------------------------------------------
diff --git 
a/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java 
b/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java
index 8add08e..4e20163 100644
--- 
a/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java
+++ 
b/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java
@@ -25,6 +25,7 @@ import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
 import java.io.File;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
 @SuppressWarnings("UnusedDeclaration")
@@ -102,6 +103,12 @@ public class QuorumConfigBuilder
                 properties.setProperty("server." + thisSpec.getServerId(), 
String.format("localhost:%d:%d", thisSpec.getQuorumPort(), 
thisSpec.getElectionPort()));
             }
         }
+        Map<String,Object> customProperties = spec.getCustomProperties();
+        if (customProperties != null) {
+            for (Map.Entry<String,Object> property : 
customProperties.entrySet()) {
+                properties.put(property.getKey(), property.getValue());
+            }
+        }
 
         QuorumPeerConfig config = new QuorumPeerConfig();
         config.parseProperties(properties);

http://git-wip-us.apache.org/repos/asf/curator/blob/dd20f1db/curator-test/src/test/java/org/apache/curator/test/TestQuorumConfigBuilder.java
----------------------------------------------------------------------
diff --git 
a/curator-test/src/test/java/org/apache/curator/test/TestQuorumConfigBuilder.java
 
b/curator-test/src/test/java/org/apache/curator/test/TestQuorumConfigBuilder.java
new file mode 100644
index 0000000..cfb89c5
--- /dev/null
+++ 
b/curator-test/src/test/java/org/apache/curator/test/TestQuorumConfigBuilder.java
@@ -0,0 +1,49 @@
+/**
+ * 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 org.apache.curator.test;
+
+import java.util.HashMap;
+import java.util.Map;
+import static org.testng.AssertJUnit.assertEquals;
+import org.testng.annotations.Test;
+
+/**
+ * Test QuorumConfigBuilder
+ */
+public class TestQuorumConfigBuilder {
+
+    @Test
+    public void testCustomProperties() throws Exception {
+        Map<String,Object> customProperties = new HashMap<String,Object>();
+        customProperties.put("authProvider.1", 
"org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
+        customProperties.put("kerberos.removeHostFromPrincipal", "true");
+        customProperties.put("kerberos.removeRealmFromPrincipal", "true");
+        InstanceSpec spec = new InstanceSpec(null, -1, -1, -1, true, 1,-1, 
-1,customProperties);
+        TestingServer server = new TestingServer(spec, true);
+        try {
+            
assertEquals("org.apache.zookeeper.server.auth.SASLAuthenticationProvider", 
System.getProperty("zookeeper.authProvider.1"));
+            assertEquals("true", 
System.getProperty("zookeeper.kerberos.removeHostFromPrincipal"));
+            assertEquals("true", 
System.getProperty("zookeeper.kerberos.removeRealmFromPrincipal"));
+        } finally {
+            server.close();
+        }
+    }
+}
\ No newline at end of file

Reply via email to