This is an automated email from the ASF dual-hosted git repository.

mmiller pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new 6a0a5ee  Create MockServerContext for use in unit tests (#2083)
6a0a5ee is described below

commit 6a0a5ee30788f9f606166b8391b22c23d65071dc
Author: Mike Miller <mmil...@apache.org>
AuthorDate: Thu May 6 15:13:54 2021 -0400

    Create MockServerContext for use in unit tests (#2083)
---
 .../apache/accumulo/server/MockServerContext.java  | 55 ++++++++++++++++++++++
 .../accumulo/server/client/BulkImporterTest.java   | 11 +----
 .../server/conf/NamespaceConfigurationTest.java    | 10 +---
 .../conf/ServerConfigurationFactoryTest.java       | 16 +------
 .../server/conf/TableConfigurationTest.java        | 10 +---
 .../server/conf/ZooConfigurationFactoryTest.java   |  7 +--
 .../master/balancer/TableLoadBalancerTest.java     | 10 +---
 .../server/problems/ProblemReportTest.java         |  4 +-
 .../security/handler/ZKAuthenticatorTest.java      |  7 ++-
 9 files changed, 72 insertions(+), 58 deletions(-)

diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/MockServerContext.java 
b/server/base/src/test/java/org/apache/accumulo/server/MockServerContext.java
new file mode 100644
index 0000000..64694e8
--- /dev/null
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/MockServerContext.java
@@ -0,0 +1,55 @@
+/*
+ * 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.accumulo.server;
+
+import static org.easymock.EasyMock.expect;
+
+import java.util.Properties;
+
+import org.apache.accumulo.core.conf.ConfigurationCopy;
+import org.apache.accumulo.core.conf.DefaultConfiguration;
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.crypto.CryptoServiceFactory;
+import org.easymock.EasyMock;
+
+/**
+ * Get a generic mocked ServerContext with junk for testing.
+ */
+public class MockServerContext {
+
+  public static ServerContext get() {
+    ServerContext sc = EasyMock.createMock(ServerContext.class);
+    ConfigurationCopy conf = new 
ConfigurationCopy(DefaultConfiguration.getInstance());
+    conf.set(Property.INSTANCE_VOLUMES, "file:///");
+    expect(sc.getConfiguration()).andReturn(conf).anyTimes();
+    
expect(sc.getCryptoService()).andReturn(CryptoServiceFactory.newDefaultInstance()).anyTimes();
+    expect(sc.getProperties()).andReturn(new Properties()).anyTimes();
+    return sc;
+  }
+
+  public static ServerContext getWithZK(String instanceID, String zk, int 
zkTimeout) {
+    var sc = get();
+    expect(sc.getZooKeeperRoot()).andReturn("/accumulo/" + 
instanceID).anyTimes();
+    expect(sc.getInstanceID()).andReturn(instanceID).anyTimes();
+    expect(sc.getZooKeepers()).andReturn(zk).anyTimes();
+    expect(sc.getZooKeepersSessionTimeOut()).andReturn(zkTimeout).anyTimes();
+    return sc;
+  }
+
+}
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/client/BulkImporterTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/client/BulkImporterTest.java
index 566596d..b0d17be 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/client/BulkImporterTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/client/BulkImporterTest.java
@@ -31,9 +31,6 @@ import java.util.TreeSet;
 import org.apache.accumulo.core.clientImpl.ClientContext;
 import org.apache.accumulo.core.clientImpl.TabletLocator;
 import org.apache.accumulo.core.clientImpl.TabletLocator.TabletLocation;
-import org.apache.accumulo.core.conf.ConfigurationCopy;
-import org.apache.accumulo.core.conf.DefaultConfiguration;
-import org.apache.accumulo.core.conf.Property;
 import org.apache.accumulo.core.crypto.CryptoServiceFactory;
 import org.apache.accumulo.core.data.Key;
 import org.apache.accumulo.core.data.Mutation;
@@ -43,6 +40,7 @@ import org.apache.accumulo.core.data.Value;
 import org.apache.accumulo.core.dataImpl.KeyExtent;
 import org.apache.accumulo.core.file.FileOperations;
 import org.apache.accumulo.core.file.FileSKVWriter;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.apache.accumulo.server.fs.VolumeManagerImpl;
 import org.apache.hadoop.conf.Configuration;
@@ -113,12 +111,7 @@ public class BulkImporterTest {
   public void testFindOverlappingTablets() throws Exception {
     MockTabletLocator locator = new MockTabletLocator();
     FileSystem fs = FileSystem.getLocal(new Configuration());
-    ServerContext context = EasyMock.createMock(ServerContext.class);
-    ConfigurationCopy conf = new 
ConfigurationCopy(DefaultConfiguration.getInstance());
-    conf.set(Property.INSTANCE_VOLUMES, "file:///");
-    EasyMock.expect(context.getConfiguration()).andReturn(conf).anyTimes();
-    
EasyMock.expect(context.getCryptoService()).andReturn(CryptoServiceFactory.newDefaultInstance())
-        .anyTimes();
+    ServerContext context = MockServerContext.get();
     EasyMock.replay(context);
     String file = "target/testFile.rf";
     fs.delete(new Path(file), true);
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java
index fcf6d20..2b51c95 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java
@@ -29,7 +29,6 @@ import static org.junit.Assert.assertNull;
 
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.UUID;
 import java.util.function.Predicate;
 
@@ -41,6 +40,7 @@ import org.apache.accumulo.core.data.NamespaceId;
 import org.apache.accumulo.fate.zookeeper.ZooCache;
 import org.apache.accumulo.fate.zookeeper.ZooCacheFactory;
 import org.apache.accumulo.fate.zookeeper.ZooUtil;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.junit.Before;
 import org.junit.Test;
@@ -61,14 +61,8 @@ public class NamespaceConfigurationTest {
   public void setUp() {
     iid = UUID.randomUUID().toString();
 
-    context = createMock(ServerContext.class);
+    context = MockServerContext.getWithZK(iid, ZOOKEEPERS, ZK_SESSION_TIMEOUT);
     parent = createMock(AccumuloConfiguration.class);
-
-    expect(context.getProperties()).andReturn(new Properties());
-    expect(context.getZooKeeperRoot()).andReturn("/accumulo/" + 
iid).anyTimes();
-    expect(context.getInstanceID()).andReturn(iid).anyTimes();
-    expect(context.getZooKeepers()).andReturn(ZOOKEEPERS).anyTimes();
-    
expect(context.getZooKeepersSessionTimeOut()).andReturn(ZK_SESSION_TIMEOUT).anyTimes();
     replay(context);
 
     c = new NamespaceConfiguration(NSID, context, parent);
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java
index d753270..67c22ed 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java
@@ -30,14 +30,13 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 
-import java.util.Properties;
-
 import org.apache.accumulo.core.conf.AccumuloConfiguration;
 import org.apache.accumulo.core.conf.DefaultConfiguration;
 import org.apache.accumulo.core.conf.SiteConfiguration;
 import org.apache.accumulo.core.data.NamespaceId;
 import org.apache.accumulo.fate.zookeeper.ZooCache;
 import org.apache.accumulo.fate.zookeeper.ZooCacheFactory;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.easymock.EasyMock;
 import org.junit.After;
@@ -77,11 +76,7 @@ public class ServerConfigurationFactoryTest {
 
   @Before
   public void setUp() {
-    context = createMock(ServerContext.class);
-    expect(context.getInstanceID()).andReturn(IID).anyTimes();
-    expect(context.getProperties()).andReturn(new Properties()).anyTimes();
-    expect(context.getZooKeepers()).andReturn(ZK_HOST).anyTimes();
-    
expect(context.getZooKeepersSessionTimeOut()).andReturn(ZK_TIMEOUT).anyTimes();
+    context = MockServerContext.getWithZK(IID, ZK_HOST, ZK_TIMEOUT);
   }
 
   @After
@@ -89,10 +84,6 @@ public class ServerConfigurationFactoryTest {
     ServerConfigurationFactory.clearCachedConfigurations();
   }
 
-  private void mockInstanceForConfig() {
-    expect(context.getZooKeeperRoot()).andReturn("/accumulo/" + 
IID).anyTimes();
-  }
-
   private void ready() {
     replay(context);
     scf = new ServerConfigurationFactory(context, siteConfig);
@@ -115,7 +106,6 @@ public class ServerConfigurationFactoryTest {
 
   @Test
   public void testGetConfiguration() {
-    mockInstanceForConfig();
     ready();
     AccumuloConfiguration c = scf.getSystemConfiguration();
     assertNotNull(c);
@@ -125,11 +115,9 @@ public class ServerConfigurationFactoryTest {
 
   @Test
   public void testGetNamespaceConfiguration() {
-    mockInstanceForConfig();
     ready();
     NamespaceConfiguration c = scf.getNamespaceConfiguration(NSID);
     assertEquals(NSID, c.getNamespaceId());
-
     assertSame(c, scf.getNamespaceConfiguration(NSID));
   }
 
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java
index 6b19a18..6b20d87 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java
@@ -28,7 +28,6 @@ import static org.junit.Assert.assertEquals;
 
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.UUID;
 import java.util.function.Predicate;
 
@@ -38,6 +37,7 @@ import org.apache.accumulo.core.data.TableId;
 import org.apache.accumulo.fate.zookeeper.ZooCache;
 import org.apache.accumulo.fate.zookeeper.ZooCacheFactory;
 import org.apache.accumulo.fate.zookeeper.ZooUtil;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.junit.Before;
 import org.junit.Test;
@@ -57,13 +57,7 @@ public class TableConfigurationTest {
   @Before
   public void setUp() {
     iid = UUID.randomUUID().toString();
-    context = createMock(ServerContext.class);
-
-    expect(context.getProperties()).andReturn(new Properties()).anyTimes();
-    expect(context.getInstanceID()).andReturn(iid).anyTimes();
-    expect(context.getZooKeeperRoot()).andReturn("/accumulo/" + 
iid).anyTimes();
-    expect(context.getZooKeepers()).andReturn(ZOOKEEPERS).anyTimes();
-    
expect(context.getZooKeepersSessionTimeOut()).andReturn(ZK_SESSION_TIMEOUT).anyTimes();
+    context = MockServerContext.getWithZK(iid, ZOOKEEPERS, ZK_SESSION_TIMEOUT);
     replay(context);
 
     parent = createMock(NamespaceConfiguration.class);
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/conf/ZooConfigurationFactoryTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/conf/ZooConfigurationFactoryTest.java
index c651b24..4b3cac6 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/conf/ZooConfigurationFactoryTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/conf/ZooConfigurationFactoryTest.java
@@ -30,6 +30,7 @@ import static org.junit.Assert.assertSame;
 import org.apache.accumulo.core.conf.AccumuloConfiguration;
 import org.apache.accumulo.fate.zookeeper.ZooCache;
 import org.apache.accumulo.fate.zookeeper.ZooCacheFactory;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.apache.zookeeper.Watcher;
 import org.junit.Before;
@@ -45,7 +46,7 @@ public class ZooConfigurationFactoryTest {
 
   @Before
   public void setUp() {
-    context = createMock(ServerContext.class);
+    context = MockServerContext.getWithZK("iid", "localhost", 120000);
     zcf = createMock(ZooCacheFactory.class);
     zc = createMock(ZooCache.class);
     zconff = new ZooConfigurationFactory();
@@ -54,10 +55,6 @@ public class ZooConfigurationFactoryTest {
 
   @Test
   public void testGetInstance() {
-    expect(context.getZooKeeperRoot()).andReturn("zkroot").anyTimes();
-    expect(context.getInstanceID()).andReturn("iid").anyTimes();
-    expect(context.getZooKeepers()).andReturn("localhost").anyTimes();
-    expect(context.getZooKeepersSessionTimeOut()).andReturn(120000).anyTimes();
     replay(context);
     expect(zcf.getZooCache(eq("localhost"), eq(120000), 
isA(Watcher.class))).andReturn(zc)
         .anyTimes();
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/master/balancer/TableLoadBalancerTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/master/balancer/TableLoadBalancerTest.java
index e9c19fb..c0a9011 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/master/balancer/TableLoadBalancerTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/master/balancer/TableLoadBalancerTest.java
@@ -28,7 +28,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
@@ -43,6 +42,7 @@ import 
org.apache.accumulo.core.master.thrift.TabletServerStatus;
 import org.apache.accumulo.core.metadata.TServerInstance;
 import org.apache.accumulo.core.tabletserver.thrift.TabletStats;
 import org.apache.accumulo.core.util.HostAndPort;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.apache.accumulo.server.conf.TableConfiguration;
 import org.apache.accumulo.server.master.state.TabletMigration;
@@ -134,15 +134,9 @@ public class TableLoadBalancerTest {
   }
 
   private ServerContext createMockContext() {
-    ServerContext context = createMock(ServerContext.class);
     final String instanceId =
         UUID.nameUUIDFromBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 
0}).toString();
-    expect(context.getProperties()).andReturn(new Properties()).anyTimes();
-    expect(context.getInstanceID()).andReturn(instanceId).anyTimes();
-    expect(context.getZooKeepers()).andReturn("10.0.0.1:1234").anyTimes();
-    expect(context.getZooKeepersSessionTimeOut()).andReturn(30_000).anyTimes();
-    expect(context.getZooKeeperRoot()).andReturn("/root/").anyTimes();
-    return context;
+    return MockServerContext.getWithZK(instanceId, "10.0.0.1:1234", 30_000);
   }
 
   @Test
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/problems/ProblemReportTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/problems/ProblemReportTest.java
index 43a4ad3..1fe239c 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/problems/ProblemReportTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/problems/ProblemReportTest.java
@@ -41,6 +41,7 @@ import org.apache.accumulo.fate.zookeeper.ZooReaderWriter;
 import org.apache.accumulo.fate.zookeeper.ZooUtil;
 import org.apache.accumulo.fate.zookeeper.ZooUtil.NodeExistsPolicy;
 import org.apache.accumulo.fate.zookeeper.ZooUtil.NodeMissingPolicy;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.apache.hadoop.io.Text;
 import org.junit.Before;
@@ -57,9 +58,8 @@ public class ProblemReportTest {
 
   @Before
   public void setUp() {
-    context = createMock(ServerContext.class);
+    context = MockServerContext.getWithZK("instance", "", 30_000);
     zoorw = createMock(ZooReaderWriter.class);
-    expect(context.getZooKeeperRoot()).andReturn("/accumulo/instance");
     expect(context.getZooReaderWriter()).andReturn(zoorw).anyTimes();
     replay(context);
   }
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/security/handler/ZKAuthenticatorTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/security/handler/ZKAuthenticatorTest.java
index cf38070..be96256 100644
--- 
a/server/base/src/test/java/org/apache/accumulo/server/security/handler/ZKAuthenticatorTest.java
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/security/handler/ZKAuthenticatorTest.java
@@ -39,6 +39,7 @@ import org.apache.accumulo.core.security.SystemPermission;
 import org.apache.accumulo.core.security.TablePermission;
 import org.apache.accumulo.core.util.ByteArraySet;
 import org.apache.accumulo.fate.zookeeper.ZooReaderWriter;
+import org.apache.accumulo.server.MockServerContext;
 import org.apache.accumulo.server.ServerContext;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
@@ -132,10 +133,9 @@ public class ZKAuthenticatorTest {
     byte[] newHash = ZKSecurityTool.createPass(rawPass.clone());
 
     // mocking zk interaction
-    ServerContext context = createMock(ServerContext.class);
+    ServerContext context = MockServerContext.getWithZK("example", "", 30_000);
     ZooReaderWriter zr = createMock(ZooReaderWriter.class);
     expect(context.getZooReaderWriter()).andReturn(zr).anyTimes();
-    expect(context.getInstanceID()).andReturn("example").once();
     ZooKeeper zk = createMock(ZooKeeper.class);
     expect(zk.getChildren(EasyMock.anyObject(), EasyMock.anyObject()))
         .andReturn(Arrays.asList(principal)).anyTimes();
@@ -166,10 +166,9 @@ public class ZKAuthenticatorTest {
     byte[] outdatedHash = ZKSecurityTool.createOutdatedPass(rawPass);
 
     // mocking zk interaction
-    ServerContext context = createMock(ServerContext.class);
+    ServerContext context = MockServerContext.getWithZK("example", "", 30_000);
     ZooReaderWriter zr = createMock(ZooReaderWriter.class);
     expect(context.getZooReaderWriter()).andReturn(zr).anyTimes();
-    expect(context.getInstanceID()).andReturn("example").once();
     ZooKeeper zk = createMock(ZooKeeper.class);
     expect(zk.getChildren(EasyMock.anyObject(), EasyMock.anyObject()))
         .andReturn(Arrays.asList(principal)).anyTimes();

Reply via email to