Ori Liel has uploaded a new change for review.

Change subject: Engine: Add unit-tests for fence execution
......................................................................

Engine: Add unit-tests for fence execution

Change-Id: I311933f0e3d23489c44faa6dced4373f7254b56e
Signed-off-by: Ori Liel <ol...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceExecutor.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsArchitectureHelper.java
A 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/FenceExecutorTest.java
3 files changed, 294 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/94/36494/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceExecutor.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceExecutor.java
index ab289a0..b42f9d1 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceExecutor.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceExecutor.java
@@ -1,5 +1,6 @@
 package org.ovirt.engine.core.bll;
 
+import org.ovirt.engine.core.bll.interfaces.BackendInternal;
 import org.ovirt.engine.core.common.AuditLogType;
 import org.ovirt.engine.core.common.businessentities.ArchitectureType;
 import org.ovirt.engine.core.common.businessentities.FenceActionType;
@@ -27,6 +28,8 @@
 
     private final VDS _vds;
     private FencingPolicy fencingPolicy;
+    private FenceProxyLocator proxyLocator;
+    private VdsArchitectureHelper architectureHelper;
 
     public FenceExecutor(VDS vds) {
         // TODO remove if block after UI patch that should set also cluster & 
proxy preferences in GetNewVdsFenceStatusParameters
@@ -40,6 +43,8 @@
             }
         }
         this._vds = vds;
+        this.proxyLocator = new FenceProxyLocator(_vds, fencingPolicy);
+        this.architectureHelper = new VdsArchitectureHelper();
     }
 
     public FenceExecutor(VDS vds, FencingPolicy fencingPolicy) {
@@ -53,7 +58,7 @@
      */
     public VDSFenceReturnValue checkStatus() {
         VDSFenceReturnValue returnValue = null;
-        VDS proxyHost = new FenceProxyLocator(_vds, 
fencingPolicy).findProxyHost(false);
+        VDS proxyHost = proxyLocator.findProxyHost();
         if (proxyHost == null) {
             returnValue = proxyNotFound();
         } else {
@@ -72,7 +77,7 @@
 
     public VDSFenceReturnValue fence(FenceActionType action, FenceAgent agent) 
{
         boolean withRetries = action != FenceActionType.Status; // for status 
check, no retries on proxy-host selection.
-        VDS proxyHost = new FenceProxyLocator(_vds, 
fencingPolicy).findProxyHost(withRetries);
+        VDS proxyHost = proxyLocator.findProxyHost(withRetries);
         if (proxyHost == null) {
             return proxyNotFound();
         } else {
@@ -101,7 +106,7 @@
                         proxyHost.getId());
                 boolean withRetries = action != FenceActionType.Status;
                 VDS alternativeProxy =
-                        new FenceProxyLocator(_vds, 
fencingPolicy).findProxyHost(withRetries, proxyHost.getId());
+                        proxyLocator.findProxyHost(withRetries, 
proxyHost.getId());
                 if (alternativeProxy != null) {
                     result = runFenceAction(action, agent, alternativeProxy);
                 } else {
@@ -130,8 +135,7 @@
             VdsSpmStatus spmStatus = 
DbFacade.getInstance().getVdsDao().get(_vds.getId()).getSpmStatus();
             // try to stop SPM if action is Restart or Stop and the vds is SPM
             if (spmStatus != VdsSpmStatus.None) {
-                Backend.getInstance()
-                        .getResourceManager()
+                getBackend().getResourceManager()
                         .RunVdsCommand(VDSCommandType.SpmStop,
                                 new SpmStopVDSCommandParameters(_vds.getId(), 
_vds.getStoragePoolId()));
             }
@@ -145,9 +149,7 @@
      */
     private VDSReturnValue runFenceAction(FenceActionType action, FenceAgent 
agent, VDS proxyHost) {
         auditFenceAction(action, agent, proxyHost);
-        return Backend
-                    .getInstance()
-                    .getResourceManager()
+        return getBackend().getResourceManager()
                     .RunVdsCommand(
                             VDSCommandType.FenceVds,
                         new FenceVdsVDSCommandParameters(proxyHost.getId(), 
_vds.getId(), agent.getIp(),
@@ -200,9 +202,11 @@
     }
 
     private String getOptions(FenceAgent agent) {
-        ArchitectureType architectureType = 
VdsArchitectureHelper.getArchitecture(_vds.getStaticData());
+        ArchitectureType architectureType = 
architectureHelper.getArchitecture(_vds.getStaticData());
         String managementOptions =
-                VdsFenceOptions.getDefaultAgentOptions(agent.getType(), 
agent.getOptions(), architectureType);
+                VdsFenceOptions.getDefaultAgentOptions(agent.getType(),
+                        agent.getOptions() == null ? "" : agent.getOptions(),
+                        architectureType);
         return managementOptions;
     }
 
@@ -217,4 +221,24 @@
             return host.getId().toString();
         }
     }
+
+    BackendInternal getBackend() {
+        return Backend.getInstance();
+    }
+
+    public VdsArchitectureHelper getArchitectureHelper() {
+        return architectureHelper;
+    }
+
+    public void setArchitectureHelper(VdsArchitectureHelper 
architectureHelper) {
+        this.architectureHelper = architectureHelper;
+    }
+
+    public FenceProxyLocator getProxyLocator() {
+        return proxyLocator;
+    }
+
+    public void setProxyLocator(FenceProxyLocator proxyLocator) {
+        this.proxyLocator = proxyLocator;
+    }
 }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsArchitectureHelper.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsArchitectureHelper.java
index 7eeb72e..3b853e9 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsArchitectureHelper.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsArchitectureHelper.java
@@ -20,7 +20,7 @@
      * @return
      *            The host architecture type
      */
-    public static ArchitectureType getArchitecture(VdsStatic host) {
+    public ArchitectureType getArchitecture(VdsStatic host) {
         VDSGroup cluster = 
DbFacade.getInstance().getVdsGroupDao().get(host.getVdsGroupId());
         VdsDynamic vdsDynamic = 
DbFacade.getInstance().getVdsDynamicDao().get(host.getId());
         ServerCpu cpu = 
CpuFlagsManagerHandler.findMaxServerCpuByFlags(vdsDynamic.getcpu_flags(),
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/FenceExecutorTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/FenceExecutorTest.java
new file mode 100644
index 0000000..a28b434
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/FenceExecutorTest.java
@@ -0,0 +1,259 @@
+package org.ovirt.engine.core.bll;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.ovirt.engine.core.bll.interfaces.BackendInternal;
+import org.ovirt.engine.core.common.businessentities.ArchitectureType;
+import org.ovirt.engine.core.common.businessentities.AuditLog;
+import org.ovirt.engine.core.common.businessentities.FenceActionType;
+import org.ovirt.engine.core.common.businessentities.FenceAgent;
+import org.ovirt.engine.core.common.businessentities.VDS;
+import org.ovirt.engine.core.common.businessentities.VDSGroup;
+import org.ovirt.engine.core.common.businessentities.VdsSpmStatus;
+import org.ovirt.engine.core.common.businessentities.VdsStatic;
+import org.ovirt.engine.core.common.interfaces.VDSBrokerFrontend;
+import 
org.ovirt.engine.core.common.vdscommands.GetDeviceListVDSCommandParameters;
+import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
+import org.ovirt.engine.core.common.vdscommands.VDSFenceReturnValue;
+import org.ovirt.engine.core.common.vdscommands.VDSParametersBase;
+import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.compat.Version;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dal.dbbroker.DbFacadeLocator;
+import org.ovirt.engine.core.dao.AuditLogDAO;
+import org.ovirt.engine.core.dao.VdsDAO;
+import org.ovirt.engine.core.dao.VdsGroupDAO;
+import org.ovirt.engine.core.utils.MockConfigRule;
+import org.ovirt.engine.core.common.config.ConfigValues;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.stub;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.verify;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+
+@RunWith(MockitoJUnitRunner.class)
+public class FenceExecutorTest {
+
+    private static final Version CLUSTER_VERSION = Version.v3_0;
+    private static final ArchitectureType CLUSTER_ARCHITECTURE_TYPE = 
ArchitectureType.ppc64;
+    private static Guid FENCECD_HOST_ID = new 
Guid("11111111-1111-1111-1111-111111111111");
+    private static Guid PROXY_HOST_ID = new 
Guid("44444444-4444-4444-4444-444444444444");
+    private static Guid SECOND_PROXY_HOST_ID = new 
Guid("77777777-7777-7777-7777-777777777777");
+    private static Guid FENCECD_HOST_CLUSTER_ID = new 
Guid("22222222-2222-2222-2222-222222222222");
+    private static Guid FENCED_HOST_DATACENTER_ID = new 
Guid("33333333-3333-3333-3333-333333333333");
+    private static Guid FENCE_AGENT_ID = new 
Guid("55555555-5555-5555-5555-555555555555");
+
+    @ClassRule
+    public static MockConfigRule configRule =
+            new 
MockConfigRule(MockConfigRule.mockConfig(ConfigValues.FenceAgentMapping, ""));
+
+    @Mock
+    private DbFacade dbFacade;
+
+    @Mock
+    private VDS vds;
+
+    @Mock
+    private VdsDAO vdsDao;
+
+    @Mock
+    private VdsGroupDAO vdsGroupDao;
+
+    @Mock
+    private AuditLogDAO auditLogDao;
+
+    private VdsStatic vdsStatic = new VdsStatic();
+
+    @Mock
+    private VdsArchitectureHelper architectureHelper;
+
+    @Mock
+    private FenceProxyLocator proxyLocator;
+
+    private FenceExecutor executor;
+
+    @Mock
+    private VDSBrokerFrontend vdsBrokerFrontend;
+
+    @Mock
+    private BackendInternal backend;
+
+    @Before
+    public void setup() {
+        mockDbFacades();
+        mockVds();
+        executor = new FenceExecutor(vds);
+        executor.setArchitectureHelper(architectureHelper);
+        executor.setProxyLocator(proxyLocator);
+        executor = spy(executor);
+        stub(executor.getBackend()).toReturn(backend);
+        VDSReturnValue retValue = new VDSReturnValue();
+        retValue.setSucceeded(true);
+        when(backend.getResourceManager()).thenReturn(vdsBrokerFrontend);
+        when(vdsBrokerFrontend.RunVdsCommand(eq(VDSCommandType.FenceVds), 
any(VDSParametersBase.class))).thenReturn(retValue);
+        
when(architectureHelper.getArchitecture(vdsStatic)).thenReturn(CLUSTER_ARCHITECTURE_TYPE);
+    }
+
+    private void mockDbFacades() {
+        when(vdsDao.get(FENCECD_HOST_ID)).thenReturn(vds);
+        VDSGroup cluster = mockCluster();
+        when(vdsGroupDao.get(FENCECD_HOST_CLUSTER_ID)).thenReturn(cluster);
+        doNothing().when(auditLogDao).save(any(AuditLog.class));
+        when(dbFacade.getVdsDao()).thenReturn(vdsDao);
+        when(dbFacade.getVdsGroupDao()).thenReturn(vdsGroupDao);
+        when(dbFacade.getAuditLogDao()).thenReturn(auditLogDao);
+        DbFacadeLocator.setDbFacade(dbFacade);
+    }
+
+    private VDSGroup mockCluster() {
+        VDSGroup cluster = new VDSGroup();
+        cluster.setId(FENCECD_HOST_CLUSTER_ID);
+        cluster.setcompatibility_version(CLUSTER_VERSION);
+        cluster.setArchitecture(CLUSTER_ARCHITECTURE_TYPE);
+        return cluster;
+    }
+
+    private void mockVds() {
+        when(vds.getId()).thenReturn(FENCECD_HOST_ID);
+        when(vds.getVdsGroupId()).thenReturn(FENCECD_HOST_CLUSTER_ID);
+        when(vds.getStoragePoolId()).thenReturn(FENCED_HOST_DATACENTER_ID);
+        when(vds.getStaticData()).thenReturn(vdsStatic);
+        when(vds.getSpmStatus()).thenReturn(VdsSpmStatus.Contending);
+        List<FenceAgent> agents = new LinkedList<>();
+        agents.add(createAgent());
+        when(vds.getFenceAgents()).thenReturn(agents);
+    }
+
+    private void mockProxyHost() {
+        mockProxyHost(false);
+    }
+
+    private void mockProxyHost(boolean anotherProxyAvailable) {
+        VDS proxyHost = new VDS();
+        proxyHost.setId(PROXY_HOST_ID);
+        when(proxyLocator.findProxyHost()).thenReturn(proxyHost);
+        when(proxyLocator.findProxyHost(true)).thenReturn(proxyHost);
+        VDS secondProxyHost = new VDS();
+        if (anotherProxyAvailable) {
+            secondProxyHost.setId(SECOND_PROXY_HOST_ID);
+            when(proxyLocator.findProxyHost(true, 
PROXY_HOST_ID)).thenReturn(secondProxyHost);
+        } else {
+            when(proxyLocator.findProxyHost(true, 
PROXY_HOST_ID)).thenReturn(null);
+        }
+    }
+
+    private void mockFenceSuccess() {
+        VDSReturnValue returnValue = new VDSReturnValue();
+        returnValue.setSucceeded(true);
+        when(vdsBrokerFrontend.RunVdsCommand(eq(VDSCommandType.FenceVds), 
any(GetDeviceListVDSCommandParameters.class))).thenReturn(returnValue);
+    }
+
+    private void mockFenceFailure(boolean succeedOnSecondAttempt) {
+        VDSReturnValue firstReturnValue = new VDSReturnValue();
+        firstReturnValue.setSucceeded(false);
+        VDSReturnValue secondReturnValue = new VDSReturnValue();
+        secondReturnValue.setSucceeded(succeedOnSecondAttempt ? true : false);
+        when(vdsBrokerFrontend.RunVdsCommand(eq(VDSCommandType.FenceVds), 
any(GetDeviceListVDSCommandParameters.class))).thenReturn(firstReturnValue)
+                .thenReturn(secondReturnValue);
+    }
+
+    @Test
+    public void checkStatus_handleProxyNotFound() {
+        when(proxyLocator.findProxyHost()).thenReturn(null);
+        VDSFenceReturnValue result = executor.checkStatus();
+        assertFalse(result.getSucceeded());
+        assertTrue(result.getExceptionString().contains("no running proxy Host 
was found"));
+    }
+
+    @Test
+    public void fence_handleSuccess() {
+        mockProxyHost();
+        mockFenceSuccess();
+        FenceAgent agent = createAgent();
+        VDSFenceReturnValue result = executor.fence(FenceActionType.Start, 
agent);
+        assertTrue(result.getSucceeded());
+        assertEquals(result.getFenceAgentUsed(), agent);
+    }
+
+    @Test
+    public void fence_stopSpmCalled() {
+        mockProxyHost();
+        mockFenceSuccess();
+        FenceAgent agent = createAgent();
+        VDSFenceReturnValue result = executor.fence(FenceActionType.Stop, 
agent);
+        verify(vdsBrokerFrontend).RunVdsCommand(eq(VDSCommandType.SpmStop), 
any(VDSParametersBase.class));
+        assertTrue(result.getSucceeded());
+        assertEquals(result.getFenceAgentUsed(), agent);
+    }
+
+
+    @Test
+    public void fence_retryWithDifferentProxyAndSucceed() {
+        mockProxyHost(true);
+        mockFenceFailure(true);
+        FenceAgent agent = createAgent();
+        VDSFenceReturnValue result = executor.fence(FenceActionType.Start, 
agent);
+        assertTrue(result.getSucceeded());
+        verify(proxyLocator).findProxyHost(true, PROXY_HOST_ID);
+    }
+
+    @Test
+    public void fence_retryWithSameProxyAndSucceed() {
+        mockProxyHost(false);
+        mockFenceFailure(true);
+        FenceAgent agent = createAgent();
+        VDSFenceReturnValue result = executor.fence(FenceActionType.Start, 
agent);
+        assertTrue(result.getSucceeded());
+        verify(proxyLocator).findProxyHost(true, PROXY_HOST_ID);
+    }
+
+    @Test
+    public void fence_retryWithDifferentProxyAndFail() {
+        mockProxyHost(true);
+        mockFenceFailure(false);
+        FenceAgent agent = createAgent();
+        VDSFenceReturnValue result = executor.fence(FenceActionType.Start, 
agent);
+        assertFalse(result.getSucceeded());
+        verify(proxyLocator).findProxyHost(true, PROXY_HOST_ID);
+    }
+
+    @Test
+    public void fence_retryWithSameProxyAndFail() {
+        mockProxyHost(false);
+        mockFenceFailure(false);
+        FenceAgent agent = createAgent();
+        VDSFenceReturnValue result = executor.fence(FenceActionType.Start, 
agent);
+        assertFalse(result.getSucceeded());
+        verify(proxyLocator).findProxyHost(true, PROXY_HOST_ID);
+    }
+
+
+    @Test
+    public void checkStatus_success() {
+        mockProxyHost();
+        mockFenceSuccess();
+        VDSFenceReturnValue result = executor.checkStatus();
+        assertTrue(result.getSucceeded());
+    }
+
+    private FenceAgent createAgent() {
+        FenceAgent agent = new FenceAgent();
+        agent.setId(FENCE_AGENT_ID);
+        return agent;
+    }
+
+}


-- 
To view, visit http://gerrit.ovirt.org/36494
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I311933f0e3d23489c44faa6dced4373f7254b56e
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Ori Liel <ol...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to