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);
}
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);
}
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);
}
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);
}
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();
}
Aggregations