Search in sources :

Example 46 with ValidationFailed

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

the class PostgresVersionValidatorTest method givenInconsistentPostgresVersion_shouldFail.

@Test
void givenInconsistentPostgresVersion_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/invalid_creation_pg_version.json", StackGresClusterReview.class);
    review.getRequest().getObject().getSpec().getPostgres().setVersion(secondPgMajorVersion);
    postgresConfig.getSpec().setPostgresVersion(firstPgMajorVersion);
    StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
    String postgresProfile = spec.getConfiguration().getPostgresConfig();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(configFinder.findByNameAndNamespace(eq(postgresProfile), eq(namespace))).thenReturn(Optional.of(postgresConfig));
    ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = exception.getResult().getMessage();
    assertEquals("Invalid postgres version, must be " + firstPgMajorVersion + " to use sgPostgresConfig postgresconf", resultMessage);
    verify(configFinder).findByNameAndNamespace(eq(postgresProfile), eq(namespace));
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 47 with ValidationFailed

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

the class PostgresVersionValidatorTest method givenNoPostgresVersion_shouldFail.

@Test
void givenNoPostgresVersion_shouldFail() throws ValidationFailed {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/invalid_creation_no_pg_version.json", StackGresClusterReview.class);
    ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = exception.getResult().getMessage();
    assertEquals("postgres version must be provided", resultMessage);
    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)

Example 48 with ValidationFailed

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

the class PostgresVersionValidatorTest method givenInvalidPostgresConfigReference_shouldFail.

@Test
void givenInvalidPostgresConfigReference_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
    review.getRequest().getObject().getSpec().getPostgres().setVersion(firstPgMajorVersion);
    StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
    String postgresProfile = spec.getConfiguration().getPostgresConfig();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(configFinder.findByNameAndNamespace(eq(postgresProfile), eq(namespace))).thenReturn(Optional.empty());
    ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = exception.getResult().getMessage();
    assertEquals("Invalid sgPostgresConfig value " + postgresProfile, resultMessage);
    verify(configFinder).findByNameAndNamespace(eq(postgresProfile), eq(namespace));
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 49 with ValidationFailed

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

the class ProfileReferenceValidatorTest method giveInvalidStackGresReferenceOnCreation_shouldFail.

@Test
void giveInvalidStackGresReferenceOnCreation_shouldFail() {
    final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
    String resourceProfile = review.getRequest().getObject().getSpec().getResourceProfile();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    when(profileFinder.findByNameAndNamespace(resourceProfile, namespace)).thenReturn(Optional.empty());
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String resultMessage = ex.getMessage();
    assertEquals("Invalid profile " + resourceProfile, resultMessage);
    verify(profileFinder).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 50 with ValidationFailed

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

the class BlocklistValidatorTest method givenCreationWithBlocklistedProperties_shouldFail.

@Test
void givenCreationWithBlocklistedProperties_shouldFail() {
    PgConfigReview review = JsonUtil.readFromJson("pgconfig_allow_request/valid_pgconfig.json", PgConfigReview.class);
    StackGresPostgresConfig pgConfig = review.getRequest().getObject();
    String[] blocklistedProperties = addBlocklistProperties(pgConfig);
    ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
        validator.validate(review);
    });
    String errorMessage = ex.getResult().getMessage();
    assertEquals("Invalid postgres configuration, properties: " + String.join(", ", blocklistedProperties) + " cannot be settled", errorMessage);
}
Also used : PgConfigReview(io.stackgres.operator.common.PgConfigReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) 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