Search in sources :

Example 26 with ValidationFailed

use of io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed in project stackgres by ongres.

the class PgConfigUpdateValidator method validate.

@Override
public void validate(PgConfigReview review) throws ValidationFailed {
    if (review.getRequest().getOperation() == Operation.UPDATE) {
        String oldPgVersion = review.getRequest().getOldObject().getSpec().getPostgresVersion();
        String newPgVersion = review.getRequest().getObject().getSpec().getPostgresVersion();
        if (!oldPgVersion.equals(newPgVersion)) {
            String detail = "postgresVersion is not updatable";
            Status failedStatus = new StatusBuilder().withCode(400).withKind(HasMetadata.getKind(StackGresPostgresConfig.class)).withReason(ErrorType.getErrorTypeUri(ErrorType.FORBIDDEN_CR_UPDATE)).withDetails(new StatusDetailsBuilder().addNewCause(pgVersionPath, detail, "FieldNotUpdatable").withKind(HasMetadata.getKind(StackGresPostgresConfig.class)).withGroup(CommonDefinition.GROUP).withName(pgVersionPath).build()).build();
            throw new ValidationFailed(failedStatus);
        }
    }
}
Also used : Status(io.fabric8.kubernetes.api.model.Status) StatusDetailsBuilder(io.fabric8.kubernetes.api.model.StatusDetailsBuilder) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) StatusBuilder(io.fabric8.kubernetes.api.model.StatusBuilder)

Example 27 with ValidationFailed

use of io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed in project stackgres by ongres.

the class StorageValidatorTest method givenCreationWithoutVolumeProperties_shouldFail.

@Test
void givenCreationWithoutVolumeProperties_shouldFail() {
    BackupConfigReview review = JsonUtil.readFromJson("backupconfig_allow_request/invalid_creation_no_s3.json", BackupConfigReview.class);
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String errorMessage = ex.getResult().getMessage();
    assertEquals("Invalid backup configuration," + " source s3 must be set when source type is s3", errorMessage);
}
Also used : ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) BackupConfigReview(io.stackgres.operator.common.BackupConfigReview) Test(org.junit.jupiter.api.Test)

Example 28 with ValidationFailed

use of io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed in project stackgres by ongres.

the class BackupConfigValidatorTest method giveInvalidStackGresBackupOnCreation_shouldFail.

@Test
void giveInvalidStackGresBackupOnCreation_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
    String backupConfig = review.getRequest().getObject().getSpec().getConfiguration().getBackupConfig();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(configFinder.findByNameAndNamespace(backupConfig, namespace)).thenReturn(Optional.empty());
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = ex.getMessage();
    assertEquals("Backup config " + backupConfig + " not found", resultMessage);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) Test(org.junit.jupiter.api.Test)

Example 29 with ValidationFailed

use of io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed in project stackgres by ongres.

the class BackupConfigValidatorTest method giveAnAttemptToUpdateToAnUnknownBackupConfig_shouldFail.

@Test
void giveAnAttemptToUpdateToAnUnknownBackupConfig_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/backup_config_update.json", StackGresClusterReview.class);
    String backupConfig = review.getRequest().getObject().getSpec().getConfiguration().getBackupConfig();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(configFinder.findByNameAndNamespace(backupConfig, namespace)).thenReturn(Optional.empty());
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = ex.getMessage();
    assertEquals("Cannot update to backup config " + backupConfig + " because it doesn't exists", resultMessage);
    verify(configFinder).findByNameAndNamespace(eq(backupConfig), eq(namespace));
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) Test(org.junit.jupiter.api.Test)

Example 30 with ValidationFailed

use of io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed in project stackgres by ongres.

the class BackupSourceValidatorTest method giveInvalidStackGresBackupOnCreation_shouldFail.

@Test
void giveInvalidStackGresBackupOnCreation_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
    String backupConfig = review.getRequest().getObject().getSpec().getConfiguration().getBackupConfig();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(configFinder.findByNameAndNamespace(backupConfig, namespace)).thenReturn(Optional.empty());
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = ex.getMessage();
    assertEquals("Backup config " + backupConfig + " not found", resultMessage);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationFailed (io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed)61 Test (org.junit.jupiter.api.Test)46 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)24 BackupConfigReview (io.stackgres.operator.common.BackupConfigReview)17 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)15 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)10 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)5 Status (io.fabric8.kubernetes.api.model.Status)4 StackGresPostgresConfig (io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig)4 StatusBuilder (io.fabric8.kubernetes.api.model.StatusBuilder)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 StatusDetailsBuilder (io.fabric8.kubernetes.api.model.StatusDetailsBuilder)2 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)2 PgConfigReview (io.stackgres.operator.common.PgConfigReview)2 StackGresDbOpsReview (io.stackgres.operator.common.StackGresDbOpsReview)2 Operation (io.stackgres.operatorframework.admissionwebhook.Operation)2 List (java.util.List)2 ImmutableList (com.google.common.collect.ImmutableList)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1