sijie closed pull request #1097: Use loopback network interface for testcases.
URL: https://github.com/apache/bookkeeper/pull/1097
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
index dbfb9abf1..36406ce74 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
@@ -155,7 +155,7 @@ public void testBookieRegistrationWithSameZooKeeperClient() 
throws Exception {
         final ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setJournalDirName(tmpDir.getPath())
             .setLedgerDirNames(new String[] { tmpDir.getPath() })
-            .setZkServers(null);
+            .setZkServers(null).setListeningInterface(null);
 
         final String bkRegPath = conf.getZkAvailableBookiesPath() + "/"
                 + InetAddress.getLocalHost().getHostAddress() + ":"
@@ -188,7 +188,7 @@ public void testBookieRegistration() throws Exception {
         final ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setJournalDirName(tmpDir.getPath())
             .setLedgerDirNames(new String[] { tmpDir.getPath() })
-            .setZkServers(null);
+            .setZkServers(null).setListeningInterface(null);
 
         final String bkRegPath = conf.getZkAvailableBookiesPath() + "/"
                 + InetAddress.getLocalHost().getHostAddress() + ":"
@@ -250,7 +250,7 @@ public void 
testBookieRegistrationWithFQDNHostNameAsBookieID() throws Exception
 
         final ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration().setZkServers(null)
                 .setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new 
String[] { tmpDir.getPath() })
-                .setUseHostNameAsBookieID(true);
+                .setUseHostNameAsBookieID(true).setListeningInterface(null);
 
         final String bkRegPath = conf.getZkAvailableBookiesPath() + "/"
                 + InetAddress.getLocalHost().getCanonicalHostName() + ":" + 
conf.getBookiePort();
@@ -271,7 +271,7 @@ public void 
testBookieRegistrationWithShortHostNameAsBookieID() throws Exception
 
         final ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration().setZkServers(null)
                 .setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new 
String[] { tmpDir.getPath() })
-                .setUseHostNameAsBookieID(true).setUseShortHostName(true);
+                
.setUseHostNameAsBookieID(true).setUseShortHostName(true).setListeningInterface(null);
 
         final String bkRegPath = conf.getZkAvailableBookiesPath() + "/"
                 + 
(InetAddress.getLocalHost().getCanonicalHostName().split("\\.", 2)[0]) + ":" + 
conf.getBookiePort();
@@ -298,7 +298,8 @@ public void testRegNodeExistsAfterSessionTimeOut() throws 
Exception {
         final ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setJournalDirName(tmpDir.getPath())
             .setLedgerDirNames(new String[] { tmpDir.getPath() })
-            .setZkServers(zkUtil.getZooKeeperConnectString());
+            .setZkServers(zkUtil.getZooKeeperConnectString())
+            .setListeningInterface(null);
 
         String bkRegPath = conf.getZkAvailableBookiesPath() + "/"
                 + InetAddress.getLocalHost().getHostAddress() + ":"
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java
index 75e061a27..e3cec7c3c 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java
@@ -40,6 +40,7 @@
 import org.apache.bookkeeper.bookie.InterleavedLedgerStorage;
 import org.apache.bookkeeper.bookie.LedgerDirsManager;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.stats.NullStatsLogger;
 import org.apache.bookkeeper.util.DiskChecker;
 import org.apache.commons.io.FileUtils;
@@ -81,9 +82,8 @@ public void convertFromDbStorageToInterleaved() throws 
Exception {
 
         log.info("Using temp directory: {}", tmpDir);
 
-        ServerConfiguration conf = new ServerConfiguration();
+        ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setLedgerDirNames(new String[] { tmpDir.toString() });
-        conf.setAllowLoopback(true);
         LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, 
conf.getLedgerDirs(),
                 new DiskChecker(conf.getDiskUsageThreshold(), 
conf.getDiskUsageWarnThreshold()));
 
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java
index 1816945d7..dbc3c9702 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java
@@ -39,6 +39,7 @@
 import org.apache.bookkeeper.bookie.InterleavedLedgerStorage;
 import org.apache.bookkeeper.bookie.LedgerDirsManager;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.stats.NullStatsLogger;
 import org.apache.bookkeeper.util.DiskChecker;
 import org.apache.commons.io.FileUtils;
@@ -78,9 +79,8 @@ public void test() throws Exception {
 
         System.out.println(tmpDir);
 
-        ServerConfiguration conf = new ServerConfiguration();
+        ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setLedgerDirNames(new String[] { tmpDir.toString() });
-        conf.setAllowLoopback(true);
         LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, 
conf.getLedgerDirs(),
                 new DiskChecker(conf.getDiskUsageThreshold(), 
conf.getDiskUsageWarnThreshold()));
 
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java
index 1e91c2ce8..4b61a657f 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java
@@ -40,6 +40,7 @@
 import org.apache.bookkeeper.bookie.EntryLocation;
 import org.apache.bookkeeper.bookie.EntryLogger;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.proto.BookieProtocol;
 import org.junit.After;
 import org.junit.Before;
@@ -62,9 +63,8 @@ public void setup() throws Exception {
         Bookie.checkDirectoryStructure(curDir);
 
         int gcWaitTime = 1000;
-        ServerConfiguration conf = new ServerConfiguration();
+        ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setGcWaitTime(gcWaitTime);
-        conf.setAllowLoopback(true);
         conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
         conf.setLedgerDirNames(new String[] { tmpDir.toString() });
         Bookie bookie = new Bookie(conf);
@@ -229,9 +229,8 @@ public void testBookieCompaction() throws Exception {
     @Test
     public void doubleDirectoryError() throws Exception {
         int gcWaitTime = 1000;
-        ServerConfiguration conf = new ServerConfiguration();
+        ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setGcWaitTime(gcWaitTime);
-        conf.setAllowLoopback(true);
         conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
         conf.setLedgerDirNames(new String[] { "dir1", "dir2" });
 
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java
index 0d3f5bbc8..c2281a7dc 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java
@@ -31,6 +31,7 @@
 
 import org.apache.bookkeeper.bookie.Bookie;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -79,9 +80,8 @@ public void setup() throws Exception {
         Bookie.checkDirectoryStructure(curDir);
 
         int gcWaitTime = 1000;
-        ServerConfiguration conf = new ServerConfiguration();
+        ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setGcWaitTime(gcWaitTime);
-        conf.setAllowLoopback(true);
         conf.setLedgerStorageClass(MockedDbLedgerStorage.class.getName());
         conf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, 1);
         conf.setLedgerDirNames(new String[] { tmpDir.toString() });
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java
index c1f7e3971..9919dce8c 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java
@@ -39,6 +39,7 @@
 import org.apache.bookkeeper.bookie.Checkpointer;
 import org.apache.bookkeeper.bookie.LedgerDirsManager;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.stats.NullStatsLogger;
 import org.apache.bookkeeper.util.DiskChecker;
 import org.apache.commons.io.FileUtils;
@@ -78,10 +79,9 @@ public void test() throws Exception {
 
         System.out.println(tmpDir);
 
-        ServerConfiguration conf = new ServerConfiguration();
+        ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
         conf.setLedgerDirNames(new String[] { tmpDir.toString() });
         conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
-        conf.setAllowLoopback(true);
         LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, 
conf.getLedgerDirs(),
                 new DiskChecker(conf.getDiskUsageThreshold(), 
conf.getDiskUsageWarnThreshold()));
 
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java
index 14c4b6113..d773bda6e 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java
@@ -53,7 +53,6 @@
 
     public UpdateLedgerCmdTest() {
         super(3);
-        baseConf.setAllowLoopback(true);
         baseConf.setGcWaitTime(100000);
     }
 
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java
index 961cecc66..aba12a748 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java
@@ -21,23 +21,61 @@
 
 package org.apache.bookkeeper.conf;
 
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Collections;
+import java.util.Enumeration;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * Test the BK configuration object.
  */
 public class TestBKConfiguration {
 
+    static final Logger LOG = 
LoggerFactory.getLogger(TestBKConfiguration.class);
+
+    /**
+     * Loopback interface is set as the listening interface and allowloopback 
is
+     * set to true in this server config.
+     *
+     * <p>If the caller doesn't want loopback address, then listeningInterface
+     * should be set back to null.
+     */
     public static ServerConfiguration newServerConfiguration() {
         ServerConfiguration confReturn = new ServerConfiguration();
         confReturn.setJournalFlushWhenQueueEmpty(true);
         // enable journal format version
         confReturn.setJournalFormatVersionToWrite(5);
-        confReturn.setAllowLoopback(true);
         confReturn.setAllowEphemeralPorts(true);
         confReturn.setBookiePort(0);
         confReturn.setGcWaitTime(1000);
         confReturn.setDiskUsageThreshold(0.999f);
         confReturn.setDiskUsageWarnThreshold(0.99f);
+        setLoopbackInterfaceAndAllowLoopback(confReturn);
         return confReturn;
     }
 
+    private static String getLoopbackInterfaceName() {
+        try {
+            Enumeration<NetworkInterface> nifs = 
NetworkInterface.getNetworkInterfaces();
+            for (NetworkInterface nif : Collections.list(nifs)) {
+                if (nif.isLoopback()) {
+                    return nif.getName();
+                }
+            }
+        } catch (SocketException se) {
+            LOG.warn("Exception while figuring out loopback interface. Will 
use null.", se);
+            return null;
+        }
+        LOG.warn("Unable to deduce loopback interface. Will use null");
+        return null;
+    }
+
+    public static ServerConfiguration 
setLoopbackInterfaceAndAllowLoopback(ServerConfiguration serverConf) {
+        serverConf.setListeningInterface(getLoopbackInterfaceName());
+        serverConf.setAllowLoopback(true);
+        return serverConf;
+    }
 }
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
index 526a4c7b1..6365c157e 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
@@ -85,6 +85,11 @@
     protected int numBookies;
     protected BookKeeperTestClient bkc;
 
+    /*
+     * Loopback interface is set as the listening interface and allowloopback 
is
+     * set to true in this server config. So bookies in this test process would
+     * bind to loopback address.
+     */
     protected final ServerConfiguration baseConf = 
TestBKConfiguration.newServerConfiguration();
     protected final ClientConfiguration baseClientConf = new 
ClientConfiguration();
 
@@ -99,7 +104,6 @@ public BookKeeperClusterTestCase(int numBookies) {
     public BookKeeperClusterTestCase(int numBookies, int testTimeoutSecs) {
         this.numBookies = numBookies;
         this.globalTimeout = Timeout.seconds(testTimeoutSecs);
-        baseConf.setAllowLoopback(true);
     }
 
     @Before


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services

Reply via email to