Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the update validation error while stopping an idling workspace. #15689

Merged
merged 4 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public void validateUpdate(Map<String, String> existing, Map<String, String> upd
WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, existingNamespace));
}

if (isNullOrEmpty(existingNamespace)) {
// this would mean that the user made an update to the workspace without it having the
// namespace attribute stored. This is very, very unlikely, because the setting of attributes
// happens during the creation process. But let's just cover this case anyway, just to be
// sure.
validate(update);

// everything is fine. We allow to change infra namespace in such case.
return;
}

if (!updateNamespace.equals(existingNamespace)) {
throw new ValidationException(
format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

import static java.util.Collections.emptyMap;
import static org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;

import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -112,6 +116,24 @@ public void shouldDoNotAllowToRemoveNamespaceAttribute() throws ValidationExcept
emptyMap());
}

@Test
public void shouldValidateFullyIfExistingIsEmpty() throws ValidationException {
validator.validateUpdate(
emptyMap(),
ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-workspaces"));

verify(namespaceFactory).checkIfNamespaceIsAllowed(eq("che-workspaces"));
}

@Test
public void shouldNotValidateFullyIfExistingIsNotEmpty() throws ValidationException {
validator.validateUpdate(
ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-workspaces"),
ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-workspaces"));

verify(namespaceFactory, never()).checkIfNamespaceIsAllowed(any());
}

@DataProvider
public Object[][] invalidNamespaces() {
return new String[][] {{"name!space"}, {"name@space"}, {"-namespace"}, {"namespace-"}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public interface WorkspaceAttributeValidator {
/**
* Validates if the specified workspace attributes can be updated with new values.
*
* <p>Note that this method must not allow updates that would not validate using the {@link
* #validate(Map)} method.
*
* <p>This check includes:
*
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,14 @@ public void validateAttributes(Map<String, String> attributes) throws Validation
}

/**
* Checks whether workspace attributes are valid on updating. The attribute is valid if it's key
* is not null & not empty & is not prefixed with 'codenvy'.
* Checks whether workspace attributes are valid on updating.
*
* @param existing actual attributes
* @param update new attributes that are going to be stored instead of existing
* @throws ValidationException when attributes are not valid
*/
public void validateUpdateAttributes(Map<String, String> existing, Map<String, String> update)
throws ValidationException {
validateAttributes(update);

for (WorkspaceAttributeValidator attributeValidator : attributeValidators) {
attributeValidator.validateUpdate(existing, update);
}
Expand Down