Search in sources :

Example 11 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ScriptsConfigValidatorTest method givenACreationUsingScriptFromNonexistentSecretKey_shouldFail.

@Test
void givenACreationUsingScriptFromNonexistentSecretKey_shouldFail() {
    final StackGresClusterReview review = getCreationReview();
    String randomSecretKey = StringUtil.generateRandom();
    String randomSecretName = StringUtil.generateRandom();
    final StackGresCluster cluster = review.getRequest().getObject();
    prepareForSecret(review, randomSecretName, randomSecretKey);
    final String namespace = cluster.getMetadata().getNamespace();
    when(secretFinder.findByNameAndNamespace(randomSecretName, namespace)).thenReturn(Optional.of(new SecretBuilder().withNewMetadata().withName(randomSecretName).withNamespace(namespace).endMetadata().withData(ImmutableMap.of()).build()));
    ValidationUtils.assertValidationFailed(() -> validator.validate(review), ErrorType.INVALID_CR_REFERENCE, "Key " + randomSecretKey + " does not exists in Secret " + randomSecretName);
    verify(secretFinder).findByNameAndNamespace(randomSecretName, namespace);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) Test(org.junit.jupiter.api.Test)

Example 12 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ScriptsConfigValidatorTest method givenACreationUsingScriptFromNonexistentSecret_shouldFail.

@Test
void givenACreationUsingScriptFromNonexistentSecret_shouldFail() {
    final StackGresClusterReview review = getCreationReview();
    String randomSecretKey = StringUtil.generateRandom();
    String randomSecretName = StringUtil.generateRandom();
    final StackGresCluster cluster = review.getRequest().getObject();
    prepareForSecret(review, randomSecretName, randomSecretKey);
    final String namespace = cluster.getMetadata().getNamespace();
    when(secretFinder.findByNameAndNamespace(randomSecretName, namespace)).thenReturn(Optional.empty());
    ValidationUtils.assertValidationFailed(() -> validator.validate(review), ErrorType.INVALID_CR_REFERENCE, "Referenced Secret " + randomSecretName + " does not exists in namespace " + namespace);
    verify(secretFinder).findByNameAndNamespace(randomSecretName, namespace);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) Test(org.junit.jupiter.api.Test)

Example 13 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ScriptsConfigValidatorTest method givenACreationUsingScriptsFromPlainConfigMap_shouldValidateConfigMapReference.

@Test
void givenACreationUsingScriptsFromPlainConfigMap_shouldValidateConfigMapReference() throws ValidationFailed {
    final StackGresClusterReview review = getCreationReview();
    String randomConfigMapName = StringUtil.generateRandom();
    String randomConfigMapKey = StringUtil.generateRandom();
    prepareForConfigMap(review, randomConfigMapName, randomConfigMapKey);
    final StackGresCluster cluster = review.getRequest().getObject();
    final String namespace = cluster.getMetadata().getNamespace();
    when(configMapFinder.findByNameAndNamespace(randomConfigMapName, namespace)).thenReturn(Optional.of(new ConfigMapBuilder().withNewMetadata().withName(randomConfigMapName).withNamespace(namespace).endMetadata().withData(ImmutableMap.of(randomConfigMapKey, "CREATE DATABASE test;")).build()));
    validator.validate(review);
    final List<StackGresClusterScriptEntry> scripts = cluster.getSpec().getInitData().getScripts();
    verify(configMapFinder, times(scripts.size())).findByNameAndNamespace(randomConfigMapName, namespace);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) Test(org.junit.jupiter.api.Test)

Example 14 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ClusterValidationQuarkusTest method given_invalidStackGresClusterLongName_shouldFail.

@Test
void given_invalidStackGresClusterLongName_shouldFail() {
    StackGresClusterReview clusterReview = getConstraintClusterReview();
    StackGresCluster cluster = clusterReview.getRequest().getObject();
    cluster.getMetadata().setName("husked-condition-calculus-ridden-pancreas-heave-extented");
    RestAssured.given().body(clusterReview).contentType(ContentType.JSON).accept(ContentType.JSON).post(ValidationUtil.CLUSTER_VALIDATION_PATH).then().body("response.allowed", is(false), "kind", is("AdmissionReview"), "response.status.code", is(422), "response.status.message", is("Valid name must be 53 characters or less")).statusCode(200);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 15 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ClusterControllerReconciliator method reconcile.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "False positives")
@Override
protected ReconciliationResult<?> reconcile(KubernetesClient client, StackGresClusterContext context) throws Exception {
    ReconciliationResult<Boolean> extensionReconciliationResult = extensionReconciliator.reconcile(client, context);
    if (extensionReconciliationResult.result().orElse(false)) {
        final String podName = propertyContext.getString(ClusterControllerProperty.CLUSTER_CONTROLLER_POD_NAME);
        final StackGresCluster cluster = context.getCluster();
        final StackGresClusterStatus status = cluster.getStatus();
        String clusterName = cluster.getMetadata().getName();
        String namespace = cluster.getMetadata().getNamespace();
        clusterFinder.findByNameAndNamespace(clusterName, namespace).ifPresent(savedCluster -> {
            var newPodStatus = findPodStatus(status.getPodStatuses(), podName).orElseThrow();
            Optional.ofNullable(savedCluster.getStatus()).ifPresentOrElse(savedStatus -> Optional.ofNullable(savedStatus.getPodStatuses()).ifPresentOrElse(savedPodStatuses -> findPodStatus(savedPodStatuses, podName).ifPresentOrElse(savedPodStatus -> applyPodStatusChanges(newPodStatus, savedPodStatus), () -> savedPodStatuses.add(newPodStatus)), () -> savedStatus.setPodStatuses(status.getPodStatuses())), () -> savedCluster.setStatus(status));
            clusterScheduler.updateStatus(savedCluster);
        });
    }
    return extensionReconciliationResult;
}
Also used : StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) ClusterControllerProperty(io.stackgres.common.ClusterControllerProperty) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ReconciliationResult(io.stackgres.operatorframework.reconciliation.ReconciliationResult) Reconciliator(io.stackgres.operatorframework.reconciliation.Reconciliator) ClusterControllerPropertyContext(io.stackgres.cluster.configuration.ClusterControllerPropertyContext) Inject(javax.inject.Inject) CdiUtil(io.stackgres.common.CdiUtil) List(java.util.List) Dependent(javax.enterprise.context.Dependent) CustomResourceScheduler(io.stackgres.common.resource.CustomResourceScheduler) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Optional(java.util.Optional) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StackGresClusterContext(io.stackgres.cluster.common.StackGresClusterContext) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) StackGresClusterPodStatus(io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)127 Test (org.junit.jupiter.api.Test)48 Optional (java.util.Optional)32 List (java.util.List)31 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)27 StackGresClusterInstalledExtension (io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)25 ImmutableList (com.google.common.collect.ImmutableList)24 Inject (javax.inject.Inject)22 ArrayList (java.util.ArrayList)19 StackGresClusterStatus (io.stackgres.common.crd.sgcluster.StackGresClusterStatus)18 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)15 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 StackGresComponent (io.stackgres.common.StackGresComponent)15 StackGresClusterPodStatus (io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus)14 ExtensionInstaller (io.stackgres.common.extension.ExtensionManager.ExtensionInstaller)14 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)13 Collectors (java.util.stream.Collectors)13 BeforeEach (org.junit.jupiter.api.BeforeEach)13 ExtensionUninstaller (io.stackgres.common.extension.ExtensionManager.ExtensionUninstaller)12 JsonUtil (io.stackgres.testutil.JsonUtil)12