diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/MockNameNodeResourceChecker.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/MockNameNodeResourceChecker.java new file mode 100644 index 00000000000..745ef8c1138 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/MockNameNodeResourceChecker.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.hadoop.hdfs.server.namenode; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; + +/** + * Mock NameNodeResourceChecker with resource availability flag which will be + * used to simulate the Namenode resource status. + */ +public class MockNameNodeResourceChecker extends NameNodeResourceChecker { + private volatile boolean hasResourcesAvailable = true; + + public MockNameNodeResourceChecker(Configuration conf) throws IOException { + super(conf); + } + + @Override + public boolean hasAvailableDiskSpace() { + return hasResourcesAvailable; + } + + /** + * Sets resource availability flag. + * + * @param resourceAvailable + * sets true if the resource is available otherwise sets to false + */ + public void setResourcesAvailable(boolean resourceAvailable) { + hasResourcesAvailable = resourceAvailable; + } +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestNameNodeResourceChecker.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestNameNodeResourceChecker.java index 2012b6aabe1..f86ce5fc067 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestNameNodeResourceChecker.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestNameNodeResourceChecker.java @@ -97,9 +97,10 @@ public class TestNameNodeResourceChecker { cluster = new MiniDFSCluster.Builder(conf) .numDataNodes(1).build(); - NameNodeResourceChecker mockResourceChecker = Mockito.mock(NameNodeResourceChecker.class); - Mockito.when(mockResourceChecker.hasAvailableDiskSpace()).thenReturn(true); - cluster.getNameNode().getNamesystem().nnResourceChecker = mockResourceChecker; + MockNameNodeResourceChecker mockResourceChecker = + new MockNameNodeResourceChecker(conf); + cluster.getNameNode() + .getNamesystem().nnResourceChecker = mockResourceChecker; cluster.waitActive(); @@ -117,8 +118,8 @@ public class TestNameNodeResourceChecker { isNameNodeMonitorRunning); assertFalse("NN should not presently be in safe mode", cluster.getNameNode().isInSafeMode()); - - Mockito.when(mockResourceChecker.hasAvailableDiskSpace()).thenReturn(false); + + mockResourceChecker.setResourcesAvailable(false); // Make sure the NNRM thread has a chance to run. long startMillis = Time.now(); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestNNHealthCheck.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestNNHealthCheck.java index 4fca63b9fe8..e0f794f285d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestNNHealthCheck.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestNNHealthCheck.java @@ -20,7 +20,8 @@ package org.apache.hadoop.hdfs.server.namenode.ha; import static org.apache.hadoop.fs.CommonConfigurationKeys.HA_HM_RPC_TIMEOUT_DEFAULT; import static org.apache.hadoop.fs.CommonConfigurationKeys.HA_HM_RPC_TIMEOUT_KEY; import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_LIFELINE_RPC_ADDRESS_KEY; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; @@ -30,14 +31,13 @@ import org.apache.hadoop.ha.HealthCheckFailedException; import org.apache.hadoop.hdfs.DFSUtil; import org.apache.hadoop.hdfs.MiniDFSCluster; import org.apache.hadoop.hdfs.MiniDFSNNTopology; -import org.apache.hadoop.hdfs.server.namenode.NameNodeResourceChecker; +import org.apache.hadoop.hdfs.server.namenode.MockNameNodeResourceChecker; import org.apache.hadoop.hdfs.tools.NNHAServiceTarget; import org.apache.hadoop.ipc.RemoteException; import org.apache.hadoop.test.GenericTestUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; public class TestNNHealthCheck { @@ -77,9 +77,8 @@ public class TestNNHealthCheck { } private void doNNHealthCheckTest() throws IOException { - NameNodeResourceChecker mockResourceChecker = Mockito.mock( - NameNodeResourceChecker.class); - Mockito.doReturn(true).when(mockResourceChecker).hasAvailableDiskSpace(); + MockNameNodeResourceChecker mockResourceChecker = + new MockNameNodeResourceChecker(conf); cluster.getNameNode(0).getNamesystem() .setNNResourceChecker(mockResourceChecker); @@ -101,7 +100,7 @@ public class TestNNHealthCheck { // Should not throw error, which indicates healthy. rpc.monitorHealth(); - Mockito.doReturn(false).when(mockResourceChecker).hasAvailableDiskSpace(); + mockResourceChecker.setResourcesAvailable(false); try { // Should throw error - NN is unhealthy. diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java index 94cccedc02f..dfdcf3483c3 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java @@ -36,9 +36,9 @@ import org.apache.hadoop.hdfs.DFSConfigKeys; import org.apache.hadoop.hdfs.MiniDFSCluster; import org.apache.hadoop.hdfs.MiniDFSNNTopology; import org.apache.hadoop.hdfs.server.namenode.EditLogFileOutputStream; -import org.apache.hadoop.hdfs.server.namenode.ha.HATestUtil; +import org.apache.hadoop.hdfs.server.namenode.MockNameNodeResourceChecker; import org.apache.hadoop.hdfs.server.namenode.NameNode; -import org.apache.hadoop.hdfs.server.namenode.NameNodeResourceChecker; +import org.apache.hadoop.hdfs.server.namenode.ha.HATestUtil; import org.apache.hadoop.test.GenericTestUtils; import org.apache.hadoop.test.MultithreadedTestUtil.TestContext; import org.apache.hadoop.test.MultithreadedTestUtil.TestingThread; @@ -47,7 +47,6 @@ import org.junit.Before; import org.junit.Test; import com.google.common.base.Supplier; -import org.mockito.Mockito; public class TestDFSZKFailoverController extends ClientBaseWithFixes { private Configuration conf; @@ -135,9 +134,9 @@ public class TestDFSZKFailoverController extends ClientBaseWithFixes { */ @Test(timeout=60000) public void testThreadDumpCaptureAfterNNStateChange() throws Exception { - NameNodeResourceChecker mockResourceChecker = Mockito.mock( - NameNodeResourceChecker.class); - Mockito.doReturn(false).when(mockResourceChecker).hasAvailableDiskSpace(); + MockNameNodeResourceChecker mockResourceChecker = + new MockNameNodeResourceChecker(conf); + mockResourceChecker.setResourcesAvailable(false); cluster.getNameNode(0).getNamesystem() .setNNResourceChecker(mockResourceChecker); waitForHAState(0, HAServiceState.STANDBY);