Moti Asayag has uploaded a new change for review. Change subject: engine: Introduce UpdateHostValidator ......................................................................
engine: Introduce UpdateHostValidator The UpdateHostValidator is used for validating host when it is being updated. Change-Id: Ia015aad730686df7954db919c4bac9bf7bd3aa21 Signed-off-by: Moti Asayag <[email protected]> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/HostValidator.java A backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/UpdateHostValidator.java A backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/UpdateHostValidatorTest.java 3 files changed, 403 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/69/37469/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/HostValidator.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/HostValidator.java index 4be9ed7..8ab13dc 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/HostValidator.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/HostValidator.java @@ -92,4 +92,8 @@ return ValidationResult.failWith(VdcBllMessages.VDS_CANNOT_INSTALL_EMPTY_PASSWORD) .when(!addPending && authMethod == AuthenticationMethod.Password && StringUtils.isEmpty(password)); } + + protected VDS getHost() { + return host; + } } diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/UpdateHostValidator.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/UpdateHostValidator.java new file mode 100644 index 0000000..26ef0bf --- /dev/null +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/UpdateHostValidator.java @@ -0,0 +1,96 @@ +package org.ovirt.engine.core.bll.validator; + +import org.apache.commons.lang.StringUtils; +import org.ovirt.engine.core.bll.ValidationResult; +import org.ovirt.engine.core.bll.VdsHandler; +import org.ovirt.engine.core.common.action.VdsOperationActionParameters.AuthenticationMethod; +import org.ovirt.engine.core.common.businessentities.VDS; +import org.ovirt.engine.core.common.businessentities.VDSStatus; +import org.ovirt.engine.core.common.businessentities.VDSType; +import org.ovirt.engine.core.common.errors.VdcBllMessages; +import org.ovirt.engine.core.dal.dbbroker.DbFacade; + +public class UpdateHostValidator extends HostValidator { + + private final VDS oldHost; + private final boolean installHost; + + public UpdateHostValidator(DbFacade dbFacade, VDS oldHost, VDS updatedHost, boolean installHost) { + super(dbFacade, updatedHost); + this.oldHost = oldHost; + this.installHost = installHost; + } + + public ValidationResult hostExists() { + return ValidationResult.failWith(VdcBllMessages.VDS_INVALID_SERVER_ID) + .when(oldHost == null || getHost() == null); + } + + public ValidationResult hostStatusValid() { + return ValidationResult.failWith(VdcBllMessages.VDS_STATUS_NOT_VALID_FOR_UPDATE).when(!isUpdateValid()); + } + + protected boolean isUpdateValid() { + return VdsHandler.isUpdateValid(getHost().getStaticData(), oldHost.getStaticData(), oldHost.getStatus()); + } + + public ValidationResult updateHostAddressAllowed() { + return ValidationResult.failWith(VdcBllMessages.ACTION_TYPE_FAILED_HOSTNAME_CANNOT_CHANGE) + .when(oldHost.getStatus() != VDSStatus.InstallFailed + && !oldHost.getHostName().equals(getHost().getHostName())); + } + + public ValidationResult nameNotUsed() { + if (StringUtils.equalsIgnoreCase(oldHost.getName(), getHost().getName())) { + return ValidationResult.VALID; + } + + return super.nameNotUsed(); + } + + public ValidationResult hostNameNotUsed() { + if (StringUtils.equalsIgnoreCase(oldHost.getHostName(), getHost().getHostName())) { + return ValidationResult.VALID; + } + + return super.hostNameNotUsed(); + } + + public ValidationResult statusSupportedForHostInstallation() { + return ValidationResult.failWith(VdcBllMessages.VDS_CANNOT_INSTALL_STATUS_ILLEGAL) + .when(installHost + && oldHost.getStatus() != VDSStatus.Maintenance + && oldHost.getStatus() != VDSStatus.NonOperational + && oldHost.getStatus() != VDSStatus.InstallFailed + && oldHost.getStatus() != VDSStatus.InstallingOS); + } + + public ValidationResult passwordProvidedForHostInstallation(AuthenticationMethod method, String password) { + return ValidationResult.failWith(VdcBllMessages.VDS_CANNOT_INSTALL_EMPTY_PASSWORD) + .when(installHost + && method == AuthenticationMethod.Password + && StringUtils.isEmpty(password) + && getHost().getVdsType() == VDSType.VDS); + } + + public ValidationResult updatePortAllowed() { + return ValidationResult.failWith(VdcBllMessages.VDS_PORT_CHANGE_REQUIRE_INSTALL) + .unless(installHost || oldHost.getPort() == getHost().getPort()); + } + + /** + * Forbids updating group id - this must be done through {@code ChangeVDSClusterCommand}. This is due to permission + * check that must be done both on the VDS and on the VDSGroup + */ + public ValidationResult clusterNotChanged() { + return ValidationResult.failWith(VdcBllMessages.VDS_CANNOT_UPDATE_CLUSTER) + .unless(oldHost.getVdsGroupId().equals(getHost().getVdsGroupId())); + } + + public ValidationResult changeProtocolAllowed() { + return ValidationResult.failWith(VdcBllMessages.VDS_STATUS_NOT_VALID_FOR_UPDATE) + .when(getHost().getProtocol() != oldHost.getProtocol() + && oldHost.getStatus() != VDSStatus.Maintenance + && oldHost.getStatus() != VDSStatus.InstallingOS); + } +} diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/UpdateHostValidatorTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/UpdateHostValidatorTest.java new file mode 100644 index 0000000..2614f26 --- /dev/null +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/UpdateHostValidatorTest.java @@ -0,0 +1,303 @@ +package org.ovirt.engine.core.bll.validator; + +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; +import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.failsWith; +import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.isValid; + +import java.util.Collections; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.ovirt.engine.core.common.action.VdsOperationActionParameters.AuthenticationMethod; +import org.ovirt.engine.core.common.businessentities.VDS; +import org.ovirt.engine.core.common.businessentities.VDSStatus; +import org.ovirt.engine.core.common.businessentities.VDSType; +import org.ovirt.engine.core.common.businessentities.VdsProtocol; +import org.ovirt.engine.core.common.config.ConfigValues; +import org.ovirt.engine.core.common.errors.VdcBllMessages; +import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.dbbroker.DbFacade; +import org.ovirt.engine.core.dao.VdsDAO; +import org.ovirt.engine.core.utils.MockConfigRule; +import org.ovirt.engine.core.utils.RandomUtils; + +@RunWith(MockitoJUnitRunner.class) +public class UpdateHostValidatorTest { + + private final static int HOST_NAME_SIZE = 20; + @Mock + private DbFacade dbFacade; + + @Mock + private VdsDAO hostDao; + + @Rule + public MockConfigRule mockConfigRule = new MockConfigRule(); + + @Mock + private VDS host; + + @Mock + private VDS oldHost; + + private UpdateHostValidator validator; + + @Before + public void setup() { + mockConfigRule.mockConfigValue(ConfigValues.MaxVdsNameLength, HOST_NAME_SIZE); + validator = new UpdateHostValidator(dbFacade, oldHost, host, false); + } + + @Test + public void hostExists() { + assertThat(validator.hostExists(), isValid()); + } + + @Test + public void oldHostDoesNotExist() { + validator = new UpdateHostValidator(dbFacade, oldHost, null, false); + + assertThat(validator.hostExists(), failsWith(VdcBllMessages.VDS_INVALID_SERVER_ID)); + } + + @Test + public void hostDoesNotExist() { + validator = new UpdateHostValidator(dbFacade, null, host, false); + assertThat(validator.hostExists(), failsWith(VdcBllMessages.VDS_INVALID_SERVER_ID)); + } + + @Test + public void hostStatusValid() { + validator = spy(new UpdateHostValidator(dbFacade, oldHost, host, false)); + doReturn(true).when(validator).isUpdateValid(); + + assertThat(validator.hostStatusValid(), isValid()); + } + + @Test + public void hostStatusInvalid() { + validator = spy(new UpdateHostValidator(dbFacade, oldHost, host, false)); + doReturn(false).when(validator).isUpdateValid(); + + assertThat(validator.hostStatusValid(), failsWith(VdcBllMessages.VDS_STATUS_NOT_VALID_FOR_UPDATE)); + } + + @Test + public void updateHostAddressAllowed() { + String hostName = RandomUtils.instance().nextString(HOST_NAME_SIZE); + when(oldHost.getHostName()).thenReturn(hostName); + when(host.getHostName()).thenReturn(hostName); + when(oldHost.getStatus()).thenReturn(VDSStatus.InstallFailed); + + assertThat(validator.updateHostAddressAllowed(), isValid()); + } + + @Test + public void updateHostAddressNotAllowedWhenStatusNotInstallFailed() { + when(oldHost.getHostName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + when(host.getHostName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + when(oldHost.getStatus()).thenReturn(VDSStatus.Maintenance); + + assertThat(validator.updateHostAddressAllowed(), + failsWith(VdcBllMessages.ACTION_TYPE_FAILED_HOSTNAME_CANNOT_CHANGE)); + } + + @Test + public void nameNotChanged() { + String name = RandomUtils.instance().nextString(HOST_NAME_SIZE); + when(oldHost.getName()).thenReturn(name); + when(host.getName()).thenReturn(name); + + assertThat(validator.nameNotUsed(), isValid()); + } + + @Test + public void nameNotUsed() { + when(oldHost.getName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + String newName = RandomUtils.instance().nextString(HOST_NAME_SIZE); + when(host.getName()).thenReturn(newName); + when(hostDao.getByName(any(String.class))).thenReturn(null); + when(dbFacade.getVdsDao()).thenReturn(hostDao); + validator = new UpdateHostValidator(dbFacade, oldHost, host, false); + + assertThat(validator.nameNotUsed(), isValid()); + } + + @Test + public void nameInUse() { + when(oldHost.getName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + when(host.getName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + when(hostDao.getByName(any(String.class))).thenReturn(mock(VDS.class)); + when(dbFacade.getVdsDao()).thenReturn(hostDao); + validator = new UpdateHostValidator(dbFacade, oldHost, host, false); + + assertThat(validator.nameNotUsed(), failsWith(VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED)); + } + + @Test + public void hostNameNotChanged() { + String name = RandomUtils.instance().nextString(HOST_NAME_SIZE); + when(oldHost.getHostName()).thenReturn(name); + when(host.getHostName()).thenReturn(name); + + assertThat(validator.hostNameNotUsed(), isValid()); + } + + @Test + public void hostNNotUsed() { + when(oldHost.getHostName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + String newName = RandomUtils.instance().nextString(HOST_NAME_SIZE); + when(host.getHostName()).thenReturn(newName); + when(hostDao.getAllForHostname(any(String.class))).thenReturn(Collections.<VDS> emptyList()); + when(dbFacade.getVdsDao()).thenReturn(hostDao); + validator = new UpdateHostValidator(dbFacade, oldHost, host, false); + + assertThat(validator.hostNameNotUsed(), isValid()); + } + + @Test + public void hostNInUse() { + when(oldHost.getHostName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + when(host.getHostName()).thenReturn(RandomUtils.instance().nextString(HOST_NAME_SIZE)); + when(hostDao.getAllForHostname(any(String.class))).thenReturn(Collections.singletonList(mock(VDS.class))); + when(dbFacade.getVdsDao()).thenReturn(hostDao); + validator = new UpdateHostValidator(dbFacade, oldHost, host, false); + + assertThat(validator.hostNameNotUsed(), failsWith(VdcBllMessages.ACTION_TYPE_FAILED_VDS_WITH_SAME_HOST_EXIST)); + } + + @Test + public void anyStatusValidWhenNoInstallationRequired() { + validator = new UpdateHostValidator(dbFacade, oldHost, host, false); + + assertThat(validator.statusSupportedForHostInstallation(), isValid()); + } + + @Test + public void statusSupportedForHostInstallation() { + when(oldHost.getStatus()).thenReturn(VDSStatus.Maintenance); + validator = new UpdateHostValidator(dbFacade, oldHost, host, true); + + assertThat(validator.statusSupportedForHostInstallation(), isValid()); + } + + @Test + public void statusNotSupportedForHostInstallation() { + when(oldHost.getStatus()).thenReturn(VDSStatus.Up); + validator = new UpdateHostValidator(dbFacade, oldHost, host, true); + + assertThat(validator.statusSupportedForHostInstallation(), + failsWith(VdcBllMessages.VDS_CANNOT_INSTALL_STATUS_ILLEGAL)); + } + + @Test + public void passwordNotNeededWhenNoInstallationRequired() { + assertThat(validator.passwordProvidedForHostInstallation(RandomUtils.instance() + .nextEnum(AuthenticationMethod.class), + null), isValid()); + } + + @Test + public void passwordNotNeededForNonPasswordMethod() { + validator = new UpdateHostValidator(dbFacade, oldHost, host, true); + + assertThat(validator.passwordProvidedForHostInstallation(AuthenticationMethod.PublicKey, null), isValid()); + } + + @Test + public void passwordProvidedForHostInstallation() { + when(host.getVdsType()).thenReturn(VDSType.VDS); + validator = new UpdateHostValidator(dbFacade, oldHost, host, true); + + assertThat(validator.passwordProvidedForHostInstallation(AuthenticationMethod.Password, RandomUtils.instance() + .nextString(10)), isValid()); + } + + @Test + public void passwordNotProvidedForHostInstallation() { + when(host.getVdsType()).thenReturn(VDSType.VDS); + validator = new UpdateHostValidator(dbFacade, oldHost, host, true); + + assertThat(validator.passwordProvidedForHostInstallation(AuthenticationMethod.Password, null), + failsWith(VdcBllMessages.VDS_CANNOT_INSTALL_EMPTY_PASSWORD)); + } + + @Test + public void updatePortAllowedWhenInstallationRequired() { + when(oldHost.getPort()).thenReturn(RandomUtils.instance().nextInt()); + when(host.getPort()).thenReturn(RandomUtils.instance().nextInt()); + validator = new UpdateHostValidator(dbFacade, oldHost, host, true); + + assertThat(validator.updatePortAllowed(), isValid()); + } + + @Test + public void portsNotChanged() { + int port = RandomUtils.instance().nextInt(); + when(oldHost.getPort()).thenReturn(port); + when(host.getPort()).thenReturn(port); + + assertThat(validator.updatePortAllowed(), isValid()); + } + + @Test + public void updatePortNotAllowed() { + when(oldHost.getPort()).thenReturn(RandomUtils.instance().nextInt()); + when(host.getPort()).thenReturn(RandomUtils.instance().nextInt()); + + assertThat(validator.updatePortAllowed(), failsWith(VdcBllMessages.VDS_PORT_CHANGE_REQUIRE_INSTALL)); + } + + @Test + public void clusterNotChanged() { + Guid clusterId = Guid.newGuid(); + when(oldHost.getVdsGroupId()).thenReturn(clusterId); + when(host.getVdsGroupId()).thenReturn(clusterId); + + assertThat(validator.clusterNotChanged(), isValid()); + } + + @Test + public void clusterChanged() { + when(oldHost.getVdsGroupId()).thenReturn(Guid.newGuid()); + when(host.getVdsGroupId()).thenReturn(Guid.newGuid()); + + assertThat(validator.clusterNotChanged(), failsWith(VdcBllMessages.VDS_CANNOT_UPDATE_CLUSTER)); + } + + @Test + public void protocolNotChanged() { + VdsProtocol protocol = RandomUtils.instance().nextEnum(VdsProtocol.class); + when(oldHost.getProtocol()).thenReturn(protocol); + when(host.getProtocol()).thenReturn(protocol); + + assertThat(validator.changeProtocolAllowed(), isValid()); + } + + @Test + public void changeProtocolAllowed() { + when(oldHost.getProtocol()).thenReturn(VdsProtocol.XML); + when(host.getProtocol()).thenReturn(VdsProtocol.STOMP); + when(oldHost.getStatus()).thenReturn(VDSStatus.Maintenance); + + assertThat(validator.changeProtocolAllowed(), isValid()); + } + + @Test + public void changeProtocolNotAllowed() { + when(oldHost.getProtocol()).thenReturn(VdsProtocol.XML); + when(host.getProtocol()).thenReturn(VdsProtocol.STOMP); + when(oldHost.getStatus()).thenReturn(VDSStatus.Up); + + assertThat(validator.changeProtocolAllowed(), failsWith(VdcBllMessages.VDS_STATUS_NOT_VALID_FOR_UPDATE)); + } +} -- To view, visit http://gerrit.ovirt.org/37469 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia015aad730686df7954db919c4bac9bf7bd3aa21 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Moti Asayag <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
