Search in sources :

Example 91 with StackGresCluster

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

the class ScriptsConfigValidatorTest method givenACreationUsingScriptsFromBinaryConfigMap_shouldValidateConfigMapReference.

@Test
void givenACreationUsingScriptsFromBinaryConfigMap_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().withBinaryData(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 92 with StackGresCluster

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

the class ClusterValidationQuarkusTest method given_invalidStackGresClusterName_shouldFail.

@Test
void given_invalidStackGresClusterName_shouldFail() {
    StackGresClusterReview clusterReview = getConstraintClusterReview();
    StackGresCluster cluster = clusterReview.getRequest().getObject();
    cluster.getMetadata().setName("postgres-13.0-to-13.1");
    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("Name must consist of lower case alphanumeric " + "characters or '-', start with an alphabetic character, " + "and end with an alphanumeric character")).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 93 with StackGresCluster

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

the class ScriptsConfigValidatorTest method givenACreationUsingScriptsFromNonexistentConfigMap_shouldFail.

@Test
void givenACreationUsingScriptsFromNonexistentConfigMap_shouldFail() 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.empty());
    ValidationUtils.assertValidationFailed(() -> validator.validate(review), ErrorType.INVALID_CR_REFERENCE, "Referenced ConfigMap " + randomConfigMapName + " does not exists in namespace " + namespace);
    verify(configMapFinder).findByNameAndNamespace(randomConfigMapName, namespace);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) Test(org.junit.jupiter.api.Test)

Example 94 with StackGresCluster

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

the class ScriptsConfigValidatorTest method givenACreationUsingScriptsFromNonexistentConfigMapKey_shouldFail.

@Test
void givenACreationUsingScriptsFromNonexistentConfigMapKey_shouldFail() 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().withBinaryData(ImmutableMap.of()).withData(ImmutableMap.of()).build()));
    ValidationUtils.assertValidationFailed(() -> validator.validate(review), ErrorType.INVALID_CR_REFERENCE, "Key " + randomConfigMapKey + " does not exists in ConfigMap " + randomConfigMapName);
    verify(configMapFinder).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) Test(org.junit.jupiter.api.Test)

Example 95 with StackGresCluster

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

the class AbstractRestartStateHandler method buildClusterRestartState.

protected ClusterRestartState buildClusterRestartState(StackGresDbOps dbOps, StackGresCluster cluster, Optional<StatefulSet> statefulSet, List<Pod> clusterPods) {
    DbOpsRestartStatus restartStatus = getDbOpRestartStatus(dbOps);
    Map<String, Pod> podsDict = clusterPods.stream().collect(Collectors.toMap(pod -> pod.getMetadata().getName(), Function.identity()));
    var initialInstances = Optional.ofNullable(restartStatus.getInitialInstances()).map(instances -> instances.stream().map(podsDict::get).collect(Collectors.toUnmodifiableList())).orElse(clusterPods);
    var restartedInstances = Optional.ofNullable(restartStatus.getRestartedInstances()).map(instances -> instances.stream().map(podsDict::get).collect(Collectors.toUnmodifiableList())).orElse(List.of());
    var podRestartReasonsMap = clusterPods.stream().collect(Collectors.toUnmodifiableMap(Function.identity(), pod -> getPodRestartReasons(cluster, statefulSet, pod)));
    final String method = getRestartMethod(dbOps).orElse(REDUCED_IMPACT_METHOD);
    final boolean onlyPendingRestart = Optional.of(dbOps.getSpec()).map(StackGresDbOpsSpec::getRestart).map(StackGresDbOpsRestart::getOnlyPendingRestart).orElse(false);
    return ImmutableClusterRestartState.builder().namespace(dbOps.getMetadata().getNamespace()).dbOpsName(dbOps.getMetadata().getName()).dbOpsOperation(dbOps.getSpec().getOp()).clusterName(cluster.getMetadata().getName()).restartMethod(method).isOnlyPendingRestart(onlyPendingRestart).primaryInstance(getPrimaryInstance(clusterPods)).isSwitchoverInitiated(restartStatus.getSwitchoverInitiated() != null).isSwitchoverFinalized(restartStatus.getSwitchoverFinalized() != null).initialInstances(initialInstances).restartedInstances(restartedInstances).totalInstances(clusterPods).podRestartReasonsMap(podRestartReasonsMap).build();
}
Also used : StackGresContext(io.stackgres.common.StackGresContext) StackGresDbOpsRestart(io.stackgres.common.crd.sgdbops.StackGresDbOpsRestart) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) DbOpsRestartStatus(io.stackgres.common.crd.sgdbops.DbOpsRestartStatus) InvalidCluster(io.stackgres.jobs.dbops.clusterrestart.InvalidCluster) Function(java.util.function.Function) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) RestartEvent(io.stackgres.jobs.dbops.clusterrestart.RestartEvent) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) Inject(javax.inject.Inject) ImmutableClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ImmutableClusterRestartState) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Map(java.util.Map) ClusterDbOpsRestartStatus(io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) REDUCED_IMPACT_METHOD(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartImpl.REDUCED_IMPACT_METHOD) RestartReasons(io.stackgres.common.ClusterPendingRestartUtil.RestartReasons) Logger(org.slf4j.Logger) EventEmitter(io.stackgres.common.event.EventEmitter) ClusterRestart(io.stackgres.jobs.dbops.clusterrestart.ClusterRestart) ClusterRestartStateHandlerImpl(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartStateHandlerImpl) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) Pod(io.fabric8.kubernetes.api.model.Pod) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) ClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState) List(java.util.List) CustomResourceScheduler(io.stackgres.common.resource.CustomResourceScheduler) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ResourceScanner(io.stackgres.common.resource.ResourceScanner) Optional(java.util.Optional) ResourceFinder(io.stackgres.common.resource.ResourceFinder) NotNull(org.jetbrains.annotations.NotNull) ClusterPendingRestartUtil(io.stackgres.common.ClusterPendingRestartUtil) Pod(io.fabric8.kubernetes.api.model.Pod) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) DbOpsRestartStatus(io.stackgres.common.crd.sgdbops.DbOpsRestartStatus) ClusterDbOpsRestartStatus(io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus)

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