Laszlo Hornyak has uploaded a new change for review.

Change subject: engine: check if vds exists with same name
......................................................................

engine: check if vds exists with same name

Adds some checking to AddVdsCommand an updates UpdateVdsCommand to check
if there is such a VDS in the database already.

Change-Id: I59bea31c78da3f718497bd1b58bada21c744fdc7
Bug-Url: https://bugzilla.redhat.com/893089
Signed-off-by: Laszlo Hornyak <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVdsCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVdsCommand.java
A 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVdsCommandTest.java
3 files changed, 98 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/92/12192/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVdsCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVdsCommand.java
index 5288e81..2f0c01c 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVdsCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVdsCommand.java
@@ -303,6 +303,12 @@
             } else if (!ValidationUtils.validHostname(hostName)) {
                 
addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_INVALID_VDS_HOSTNAME);
                 returnValue = false;
+            } else if (getVdsDAO().getAllWithName(vdsName).size() != 0) {
+                
addCanDoActionMessage(VdcBllMessages.VDS_TRY_CREATE_WITH_EXISTING_PARAMS);
+                returnValue = false;
+            } else if (getVdsDAO().getAllForHostname(hostName).size() != 0) {
+                
addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_VDS_WITH_SAME_HOST_EXIST);
+                returnValue = false;
             } else {
                 returnValue = returnValue && 
validateSingleHostAttachedToLocalStorage();
 
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVdsCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVdsCommand.java
index 000de03..270825b 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVdsCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVdsCommand.java
@@ -43,7 +43,7 @@
     @Override
     protected boolean canDoAction() {
         boolean returnValue = false;
-        _oldVds = DbFacade.getInstance().getVdsDao().get(getVdsId());
+        _oldVds = getVdsDAO().get(getVdsId());
 
         if (_oldVds != null && getParameters().getVdsStaticData() != null) {
             String compatibilityVersion = 
_oldVds.getVdsGroupCompatibilityVersion().toString();
@@ -71,11 +71,11 @@
                 // check if a name is updated to an existing vds name
                 else if (!StringUtils.equals(_oldVds.getName().toLowerCase(), 
getParameters().getVdsStaticData()
                         .getName().toLowerCase())
-                        && 
VdsHandler.isVdsWithSameNameExistStatic(getParameters().getVdsStaticData().getName()))
 {
+                        && getVdsDAO().getAllWithName(vdsName).size() != 0) {
                     
addCanDoActionMessage(VdcBllMessages.VDS_TRY_CREATE_WITH_EXISTING_PARAMS);
                 } else if 
(!StringUtils.equals(_oldVds.getHostName().toLowerCase(), 
getParameters().getVdsStaticData()
                         .getHostName().toLowerCase())
-                        && 
VdsHandler.isVdsWithSameHostExistStatic(getParameters().getVdsStaticData().getHostName()))
 {
+                        && getVdsDAO().getAllForHostname(hostName).size() != 
0) {
                     
addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_VDS_WITH_SAME_HOST_EXIST);
                 } else if (getParameters().getInstallVds() && 
_oldVds.getStatus() != VDSStatus.Maintenance
                         && _oldVds.getStatus() != VDSStatus.NonOperational
@@ -94,6 +94,8 @@
                     // This is due to permission check that must be done both 
on
                     // the VDS and on the VDSGroup
                     
addCanDoActionMessage(VdcBllMessages.VDS_CANNOT_UPDATE_CLUSTER);
+                } else if (getVdsDAO().getAllForHostname(hostName).size() != 
0) {
+                    
addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_VDS_WITH_SAME_HOST_EXIST);
                 } else {
                     returnValue = true;
                 }
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVdsCommandTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVdsCommandTest.java
new file mode 100644
index 0000000..2cd7672
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVdsCommandTest.java
@@ -0,0 +1,87 @@
+package org.ovirt.engine.core.bll;
+
+import java.util.Arrays;
+
+import org.junit.Assert;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.ovirt.engine.core.common.action.UpdateVdsActionParameters;
+import org.ovirt.engine.core.common.businessentities.VDS;
+import org.ovirt.engine.core.common.businessentities.VdsStatic;
+import org.ovirt.engine.core.common.config.ConfigValues;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.compat.Version;
+import org.ovirt.engine.core.dao.VdsDAO;
+import org.ovirt.engine.core.utils.MockConfigRule;
+
+public class UpdateVdsCommandTest {
+
+    @ClassRule
+    public static MockConfigRule configRule =
+            new 
MockConfigRule(MockConfigRule.mockConfig(ConfigValues.MaxVdsNameLength, 4));
+
+    @Test
+    public void canDoAction() {
+        UpdateVdsActionParameters parameters = new UpdateVdsActionParameters();
+        Guid vdsId = new Guid();
+        VDS newVdsData = makeTestVds(vdsId);
+        VDS oldVdsData = newVdsData.clone();
+        oldVdsData.setVdsName("FOO");
+        oldVdsData.setVdsGroupCompatibilityVersion(new Version("1.2.3"));
+        parameters.setvds(newVdsData);
+
+        @SuppressWarnings("unchecked")
+        UpdateVdsCommand<UpdateVdsActionParameters> commandMock = 
Mockito.mock(UpdateVdsCommand.class);
+        Mockito.when(commandMock.getVdsId()).thenReturn(vdsId);
+        Mockito.when(commandMock.canDoAction()).thenCallRealMethod();
+        Mockito.when(commandMock.getParameters()).thenReturn(parameters);
+        
Mockito.when(commandMock.IsPowerManagementLegal(Mockito.any(VdsStatic.class), 
Mockito.any(String.class)))
+                .thenReturn(true);
+        VdsDAO vdsDaoMock = Mockito.mock(VdsDAO.class);
+        Mockito.when(vdsDaoMock.get(vdsId)).thenReturn(oldVdsData);
+        Mockito.when(commandMock.getVdsDAO()).thenReturn(vdsDaoMock);
+        VdsHandler.Init();
+
+        Assert.assertTrue(commandMock.canDoAction());
+    }
+
+    private VDS makeTestVds(Guid vdsId) {
+        VDS newVdsData = new VDS();
+        newVdsData.setHostName("BUZZ");
+        newVdsData.setVdsName("BAR");
+        newVdsData.setVdsGroupCompatibilityVersion(new Version("1.2.3"));
+        newVdsData.setVdsGroupId(new Guid());
+        newVdsData.setId(vdsId);
+        return newVdsData;
+    }
+
+    @Test
+    public void canDoActionSameName() {
+        UpdateVdsActionParameters parameters = new UpdateVdsActionParameters();
+        Guid vdsId = new Guid();
+        VDS newVdsData = makeTestVds(vdsId);
+        VDS oldVdsData = newVdsData.clone();
+        oldVdsData.setVdsName("FOO");
+        oldVdsData.setVdsGroupCompatibilityVersion(new Version("1.2.3"));
+        parameters.setvds(newVdsData);
+
+        @SuppressWarnings("unchecked")
+        UpdateVdsCommand<UpdateVdsActionParameters> commandMock = 
Mockito.mock(UpdateVdsCommand.class);
+        Mockito.when(commandMock.getVdsId()).thenReturn(vdsId);
+        Mockito.when(commandMock.canDoAction()).thenCallRealMethod();
+        Mockito.when(commandMock.getParameters()).thenReturn(parameters);
+        
Mockito.when(commandMock.IsPowerManagementLegal(Mockito.any(VdsStatic.class), 
Mockito.any(String.class)))
+                .thenReturn(true);
+        VdsDAO vdsDaoMock = Mockito.mock(VdsDAO.class);
+        Mockito.when(vdsDaoMock.get(vdsId)).thenReturn(oldVdsData);
+        //now return the old vds data
+        
Mockito.when(vdsDaoMock.getAllWithName("BAR")).thenReturn(Arrays.asList(oldVdsData));
+
+        Mockito.when(commandMock.getVdsDAO()).thenReturn(vdsDaoMock);
+        VdsHandler.Init();
+
+        Assert.assertFalse(commandMock.canDoAction());
+    }
+
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I59bea31c78da3f718497bd1b58bada21c744fdc7
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Laszlo Hornyak <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to