unit test for KVMFencer A few cases covered with unit tests.
Signed-off-by: Laszlo Hornyak <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/c9f41d40 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/c9f41d40 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/c9f41d40 Branch: refs/heads/ui-restyle Commit: c9f41d40465e3cc8bd775dcfdeb3fa1907fd1e7f Parents: 826c69f Author: Laszlo Hornyak <[email protected]> Authored: Sun Sep 22 23:03:19 2013 +0200 Committer: Darren Shepherd <[email protected]> Committed: Fri Oct 4 11:38:33 2013 -0700 ---------------------------------------------------------------------- server/test/com/cloud/ha/KVMFencerTest.java | 175 +++++++++++++++++++++++ 1 file changed, 175 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c9f41d40/server/test/com/cloud/ha/KVMFencerTest.java ---------------------------------------------------------------------- diff --git a/server/test/com/cloud/ha/KVMFencerTest.java b/server/test/com/cloud/ha/KVMFencerTest.java new file mode 100644 index 0000000..4fa0b70 --- /dev/null +++ b/server/test/com/cloud/ha/KVMFencerTest.java @@ -0,0 +1,175 @@ +package com.cloud.ha; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import com.cloud.agent.AgentManager; +import com.cloud.agent.api.FenceAnswer; +import com.cloud.agent.api.FenceCommand; +import com.cloud.exception.AgentUnavailableException; +import com.cloud.exception.OperationTimedoutException; +import com.cloud.host.HostVO; +import com.cloud.host.Status; +import com.cloud.host.dao.HostDao; +import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.resource.ResourceManager; +import com.cloud.vm.VirtualMachine; + +@RunWith(MockitoJUnitRunner.class) +public class KVMFencerTest { + + @Mock + HostDao hostDao; + @Mock + AgentManager agentManager; + @Mock + ResourceManager resourceManager; + + KVMFencer fencer; + + @Before + public void setup() { + fencer = new KVMFencer(); + fencer._agentMgr = agentManager; + fencer._hostDao = hostDao; + fencer._resourceMgr = resourceManager; + } + + @Test + public void testWithSingleHost() { + HostVO host = Mockito.mock(HostVO.class); + Mockito.when(host.getClusterId()).thenReturn(1l); + Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); + Mockito.when(host.getStatus()).thenReturn(Status.Up); + Mockito.when(host.getId()).thenReturn(1l); + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); + + Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn( + Collections.singletonList(host)); + Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); + } + + @Test + public void testWithSingleHostDown() { + HostVO host = Mockito.mock(HostVO.class); + Mockito.when(host.getClusterId()).thenReturn(1l); + Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); + Mockito.when(host.getStatus()).thenReturn(Status.Down); + Mockito.when(host.getId()).thenReturn(1l); + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); + + Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn( + Collections.singletonList(host)); + Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); + } + + @Test + public void testWithHosts() throws AgentUnavailableException, + OperationTimedoutException { + HostVO host = Mockito.mock(HostVO.class); + Mockito.when(host.getClusterId()).thenReturn(1l); + Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); + Mockito.when(host.getStatus()).thenReturn(Status.Up); + Mockito.when(host.getId()).thenReturn(1l); + + HostVO secondHost = Mockito.mock(HostVO.class); + Mockito.when(secondHost.getClusterId()).thenReturn(1l); + Mockito.when(secondHost.getHypervisorType()).thenReturn( + HypervisorType.KVM); + Mockito.when(secondHost.getStatus()).thenReturn(Status.Up); + Mockito.when(host.getId()).thenReturn(2l); + + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); + + Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn( + Arrays.asList(host, secondHost)); + + FenceAnswer answer = new FenceAnswer(null, true, "ok"); + Mockito.when( + agentManager.send(Mockito.anyLong(), + Mockito.any(FenceCommand.class))).thenReturn(answer); + + Assert.assertTrue(fencer.fenceOff(virtualMachine, host)); + } + + @Test + public void testWithFailingFence() throws AgentUnavailableException, + OperationTimedoutException { + HostVO host = Mockito.mock(HostVO.class); + Mockito.when(host.getClusterId()).thenReturn(1l); + Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); + Mockito.when(host.getStatus()).thenReturn(Status.Up); + Mockito.when(host.getId()).thenReturn(1l); + + HostVO secondHost = Mockito.mock(HostVO.class); + Mockito.when(secondHost.getClusterId()).thenReturn(1l); + Mockito.when(secondHost.getHypervisorType()).thenReturn( + HypervisorType.KVM); + Mockito.when(secondHost.getStatus()).thenReturn(Status.Up); + Mockito.when(host.getId()).thenReturn(2l); + + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); + + Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn( + Arrays.asList(host, secondHost)); + + Mockito.when( + agentManager.send(Mockito.anyLong(), + Mockito.any(FenceCommand.class))).thenThrow( + new AgentUnavailableException(2l)); + + Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); + } + + @Test + public void testWithTimeoutingFence() throws AgentUnavailableException, + OperationTimedoutException { + HostVO host = Mockito.mock(HostVO.class); + Mockito.when(host.getClusterId()).thenReturn(1l); + Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); + Mockito.when(host.getStatus()).thenReturn(Status.Up); + Mockito.when(host.getId()).thenReturn(1l); + + HostVO secondHost = Mockito.mock(HostVO.class); + Mockito.when(secondHost.getClusterId()).thenReturn(1l); + Mockito.when(secondHost.getHypervisorType()).thenReturn( + HypervisorType.KVM); + Mockito.when(secondHost.getStatus()).thenReturn(Status.Up); + Mockito.when(host.getId()).thenReturn(2l); + + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); + + Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn( + Arrays.asList(host, secondHost)); + + Mockito.when( + agentManager.send(Mockito.anyLong(), + Mockito.any(FenceCommand.class))).thenThrow( + new OperationTimedoutException(null, 2l, 0l, 0, false)); + + Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); + } + + @Test + public void testWithSingleNotKVM() { + HostVO host = Mockito.mock(HostVO.class); + Mockito.when(host.getClusterId()).thenReturn(1l); + Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.Any); + Mockito.when(host.getStatus()).thenReturn(Status.Down); + Mockito.when(host.getId()).thenReturn(1l); + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); + + Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn( + Collections.singletonList(host)); + Assert.assertNull(fencer.fenceOff(virtualMachine, host)); + } + +}
