Search in sources :

Example 41 with ValidationFailed

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

the class BackupConfigSourceValidatorTest method givenNonExistentAccountKeyForAzureBlobStorageCredentialsOnCreation_shouldFail.

@Test
void givenNonExistentAccountKeyForAzureBlobStorageCredentialsOnCreation_shouldFail() throws ValidationFailed {
    final BackupConfigReview review = getEmptyReview();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    String accountName = "secret1";
    String accountKey = "key1";
    String accessKeyName = "secret2";
    String accessKeyKey = "key2";
    setAzureBlobCredentials(review, accountName, accountKey, accessKeyName, accessKeyKey);
    when(secretFinder.findByNameAndNamespace(accountName, namespace)).thenReturn(Optional.of(new SecretBuilder(secret).withData(ImmutableMap.of(accountKey + "-wrong", ResourceUtil.encodeSecret("account"))).build()));
    ValidationFailed ex = ValidationUtils.assertErrorType(ErrorType.INVALID_SECRET, () -> validator.validate(review));
    assertEquals("Invalid backup configuration, key " + accountKey + " of secret " + accountName + " for account of azureblob credentials not found", ex.getResult().getMessage());
    verify(secretFinder).findByNameAndNamespace(eq(accountName), eq(namespace));
}
Also used : SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) BackupConfigReview(io.stackgres.operator.common.BackupConfigReview) Test(org.junit.jupiter.api.Test)

Example 42 with ValidationFailed

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

the class StorageClassValidatorTest method giveAnAttemptToUpdateToAUnknownStorageClass_shouldFail.

@Test
void giveAnAttemptToUpdateToAUnknownStorageClass_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/storage_class_config_update.json", StackGresClusterReview.class);
    String storageClass = review.getRequest().getObject().getSpec().getPod().getPersistentVolume().getStorageClass();
    when(storageClassFinder.findByName(storageClass)).thenReturn(Optional.empty());
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = ex.getMessage();
    assertEquals("Cannot update to storage class " + storageClass + " because it doesn't exists", resultMessage);
    verify(storageClassFinder).findByName(eq(storageClass));
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 43 with ValidationFailed

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

the class DistributedLogsReferenceValidatorTest method giveInvalidStackGresReferenceOnCreation_shouldFail.

@Test
void giveInvalidStackGresReferenceOnCreation_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
    String distributedLogsName = review.getRequest().getObject().getSpec().getDistributedLogs().getDistributedLogs();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(distributedLogsFinder.findByNameAndNamespace(distributedLogsName, namespace)).thenReturn(Optional.empty());
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = ex.getMessage();
    assertEquals("Distributed logs " + distributedLogsName + " not found", resultMessage);
    verify(distributedLogsFinder).findByNameAndNamespace(anyString(), anyString());
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 44 with ValidationFailed

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

the class DistributedLogsReferenceValidatorTest method giveAnAttemptToUpdateToAnUnknownProfile_shouldFail.

@Test
void giveAnAttemptToUpdateToAnUnknownProfile_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/distributed_logs_update.json", StackGresClusterReview.class);
    String distributedLogsName = review.getRequest().getObject().getSpec().getDistributedLogs().getDistributedLogs();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(distributedLogsFinder.findByNameAndNamespace(distributedLogsName, namespace)).thenReturn(Optional.empty());
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = ex.getMessage();
    assertEquals("Distributed logs " + distributedLogsName + " not found", resultMessage);
    verify(distributedLogsFinder).findByNameAndNamespace(anyString(), anyString());
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 45 with ValidationFailed

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

the class PostgresVersionValidatorTest method givenInvalidPostgresVersion_shouldFail.

@Test
void givenInvalidPostgresVersion_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/invalid_creation_no_pg_version.json", StackGresClusterReview.class);
    String postgresVersion = getRandomInvalidPostgresVersion();
    review.getRequest().getObject().getSpec().getPostgres().setVersion(postgresVersion);
    ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = exception.getResult().getMessage();
    assertTrue(resultMessage.contains("Unsupported postgres version " + postgresVersion));
    verify(configFinder, never()).findByNameAndNamespace(anyString(), anyString());
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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