Search in sources :

Example 11 with TestResource

use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.

the class ControllerCoordinatorProcessingCriteriaTest method testUpgrade.

@Test
@Description("A resource should be processed again when the ControllerCoordinator starts up if it was processed with a version of the" + " Operator that is now being replaced")
void testUpgrade() {
    step("Given the Coordinator observes this namespace", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), OBSERVED_NAMESPACE));
    step("And the current version of the operator is 6.3.1", () -> System.setProperty(ControllerCoordinatorProperty.ENTANDO_K8S_OPERATOR_VERSION.getJvmSystemProperty(), "6.3.1"));
    step("And the version of the operator to replace was 6.3.0", () -> System.setProperty(ControllerCoordinatorProperty.ENTANDO_K8S_OPERATOR_VERSION_TO_REPLACE.getJvmSystemProperty(), "6.3.0"));
    final ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
    step("And I have created an TestResource resource that was processed by version 6.3.0", () -> testResource.set(createTestResource(10L, Collections.singletonMap(AnnotationNames.PROCESSED_BY_OPERATOR_VERSION.getName(), "6.3.0"))));
    step("And it has been successfully deployed previously", () -> {
        testResource.set(clientDouble.updatePhase(testResource.get(), EntandoDeploymentPhase.SUCCESSFUL));
        attachment("Processed Resource", objectMapper.writeValueAsString(testResource.get()));
    });
    step("When I start the ControllerCoordinator", () -> coordinator.onStartup(new StartupEvent()));
    step("Then a new controller pod is created", () -> {
        await().ignoreExceptions().atMost(3, TimeUnit.SECONDS).until(() -> clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(testResource.get())) != null);
        attachment("Controller Pod", objectMapper.writeValueAsString(clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(testResource.get()))));
    });
    step("And the resource is marked as having been processed by version 6.3.1 when its deployment succeeds", () -> {
        clientDouble.updatePhase(testResource.get(), EntandoDeploymentPhase.SUCCESSFUL);
        SerializedEntandoResource latestKeycloakServer = clientDouble.load(TestResource.class, testResource.get().getMetadata().getNamespace(), testResource.get().getMetadata().getName());
        assertThat(CoordinatorUtils.resolveAnnotation(latestKeycloakServer, AnnotationNames.PROCESSED_BY_OPERATOR_VERSION)).contains("6.3.1");
    });
    step("And the upgrade event was logged", () -> {
        final Optional<String> logEntry = LogInterceptor.getLogEntries().stream().filter(s -> s.contains("needs to be processed as part of the upgrade to the version")).findFirst();
        assertThat(logEntry).isPresent();
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Allure.attachment(io.qameta.allure.Allure.attachment) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) DbmsVendor(org.entando.kubernetes.model.common.DbmsVendor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) Action(io.fabric8.kubernetes.client.Watcher.Action) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) EntandoOperatorConfigProperty(org.entando.kubernetes.controller.support.common.EntandoOperatorConfigProperty) Feature(io.qameta.allure.Feature) LabelNames(org.entando.kubernetes.controller.spi.common.LabelNames) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) LogInterceptor(org.entando.kubernetes.test.common.LogInterceptor) Map(java.util.Map) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ResourceUtils(org.entando.kubernetes.controller.spi.common.ResourceUtils) Tag(org.junit.jupiter.api.Tag) Issue(io.qameta.allure.Issue) CoordinatorTestUtils(org.entando.kubernetes.controller.coordinator.common.CoordinatorTestUtils) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) SimpleKubernetesClientDouble(org.entando.kubernetes.controller.coordinator.common.SimpleKubernetesClientDouble) CommonLabels(org.entando.kubernetes.test.common.CommonLabels) FluentIntegrationTesting(org.entando.kubernetes.controller.support.client.impl.integrationtesthelpers.FluentIntegrationTesting) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pod(io.fabric8.kubernetes.api.model.Pod) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) StandardCapability(org.entando.kubernetes.model.capability.StandardCapability) TestResource(org.entando.kubernetes.fluentspi.TestResource) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) EntandoResourceClientDouble(org.entando.kubernetes.controller.support.client.doubles.EntandoResourceClientDouble) AfterEach(org.junit.jupiter.api.AfterEach) Allure.step(io.qameta.allure.Allure.step) ProvidedCapabilityBuilder(org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder) Optional(java.util.Optional) Tags(org.junit.jupiter.api.Tags) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) StartupEvent(io.quarkus.runtime.StartupEvent) Description(io.qameta.allure.Description) Collections(java.util.Collections) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) EntandoDeploymentPhase(org.entando.kubernetes.model.common.EntandoDeploymentPhase) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) StartupEvent(io.quarkus.runtime.StartupEvent) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 12 with TestResource

use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.

the class ControllerCoordinatorProcessingCriteriaTest method createTestResource.

protected SerializedEntandoResource createTestResource(Long generation, Map<String, String> annotations) throws JsonProcessingException {
    TestResource keycloakServer = new TestResource().withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.MYSQL).build());
    keycloakServer.setMetadata(new ObjectMetaBuilder().withGeneration(generation).withUid(RandomStringUtils.randomAlphanumeric(12)).withName("test-keycloak").withNamespace(OBSERVED_NAMESPACE).withAnnotations(annotations).build());
    final SerializedEntandoResource serializedResource = CoordinatorTestUtils.toSerializedResource(keycloakServer);
    serializedResource.setDefinition(CustomResourceDefinitionContext.fromCustomResourceType(TestResource.class));
    final SerializedEntandoResource createdResource = clientDouble.createOrPatchEntandoResource(serializedResource);
    attachment("TestResource", objectMapper.writeValueAsString(createdResource));
    return createdResource;
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) TestResource(org.entando.kubernetes.fluentspi.TestResource) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder)

Example 13 with TestResource

use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleEntandoOperationsTest method shouldRemoveSuccessfullyCompletedPods.

@Test
@Description("Should delete pods and wait until they have been successfully deleted ")
void shouldRemoveSuccessfullyCompletedPods() {
    awaitDefaultToken(MY_APP_NAMESPACE_1);
    final TestResource testResource = new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource");
    step("Given I have a TestResource", () -> attachment("TestResource", objectMapper.writeValueAsString(testResource)));
    step("And I have started a service pod with the label associated with this resource that will complete after 1 second", () -> {
        final Pod startedPod = getFabric8Client().pods().inNamespace(MY_APP_NAMESPACE_1).create(new PodBuilder().withNewMetadata().withName(MY_POD).withNamespace(MY_APP_NAMESPACE_1).addToLabels(CoordinatorUtils.podLabelsFor(testResource)).endMetadata().withNewSpec().addNewContainer().withImage("busybox").withName("busybox").withArgs("/bin/sh", "-c", "sleep 3").endContainer().withRestartPolicy("Never").endSpec().build());
        attachment("Started Pod", objectMapper.writeValueAsString(startedPod));
    });
    step("And I have waited for the pod to be ready", () -> {
        await().ignoreExceptions().atMost(30, TimeUnit.SECONDS).until(() -> PodResult.of(getFabric8Client().pods().inNamespace(MY_APP_NAMESPACE_1).withName(MY_POD).fromServer().get()).getState() != State.CREATING);
        attachment("Started Pod", objectMapper.writeValueAsString(getFabric8Client().pods().inNamespace(MY_APP_NAMESPACE_1).withName(MY_POD).fromServer().get()));
    });
    step("When I remove all the successfully completed pods", () -> {
        getMyOperations().removeSuccessfullyCompletedPods(CoordinatorTestUtils.toSerializedResource(testResource));
    });
    step("Then that pod will be absent immediately after the call finished", () -> {
        assertThat(getFabric8Client().pods().inNamespace(MY_APP_NAMESPACE_1).withName(MY_POD).fromServer().get()).isNull();
    });
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) TestResource(org.entando.kubernetes.fluentspi.TestResource) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 14 with TestResource

use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleEntandoOperationsTest method shouldAndAndRemoveAnnotations.

@Test
@Description("Should add and remove annotations")
void shouldAndAndRemoveAnnotations() {
    ValueHolder<SerializedEntandoResource> resource = new ValueHolder<>();
    step("Given I have a TestResource", () -> {
        final TestResource testResource = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource"));
        resource.set(CoordinatorTestUtils.toSerializedResource(testResource));
        attachment("TestResource", objectMapper.writeValueAsString(resource.get()));
    });
    step("And I have added the annotation 'entando.org/processed-by-version=6.3.0' to it", () -> {
        resource.set(getMyOperations().putAnnotation(resource.get(), AnnotationNames.PROCESSED_BY_OPERATOR_VERSION.getName(), "6.3.0"));
        assertThat(CoordinatorUtils.resolveAnnotation(resource.get(), AnnotationNames.PROCESSED_BY_OPERATOR_VERSION)).contains("6.3.0");
        attachment("TestResource", objectMapper.writeValueAsString(resource.get()));
    });
    step("When I remove the annotation 'entando.org/processed-by-version=6.3.0' from it it", () -> {
        resource.set(getMyOperations().removeAnnotation(resource.get(), AnnotationNames.PROCESSED_BY_OPERATOR_VERSION.getName()));
        attachment("TestResource", objectMapper.writeValueAsString(resource.get()));
    });
    step("The annotation is no longer present on the resource", () -> {
        assertThat(resource.get().getMetadata().getAnnotations()).doesNotContainKey(AnnotationNames.PROCESSED_BY_OPERATOR_VERSION.getName());
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 15 with TestResource

use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleEntandoOperationsTest method shouldWatchInSingleNamespace.

@Test
@Description("Should watch in a single namespace")
void shouldWatchInSingleNamespace() {
    final Map<String, SerializedEntandoResource> resourcesObserved = new ConcurrentHashMap<>();
    step(format("Given I have registered a Watcher for TestResource in namespace %s", MY_APP_NAMESPACE_2), () -> {
        getMyOperations().inNamespace(MY_APP_NAMESPACE_2).watch((action, serializedEntandoResource) -> resourcesObserved.put(serializedEntandoResource.getMetadata().getName(), serializedEntandoResource));
    });
    step(format("When  I create two TestResource in namespaces %s and %s", MY_APP_NAMESPACE_1, MY_APP_NAMESPACE_1 + "1"), () -> {
        final TestResource testResource1 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource1"));
        attachment("TestResource 1", objectMapper.writeValueAsString(testResource1));
        final TestResource testResource2 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_2, "my-test-resource2"));
        attachment("TestResource 2", objectMapper.writeValueAsString(testResource2));
    });
    step("Then both resources were observed", () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> resourcesObserved.containsKey("my-test-resource2"));
        assertThat(resourcesObserved).containsKey("my-test-resource2");
        assertThat(resourcesObserved).doesNotContainKey("my-test-resource1");
        attachment("TestResources", objectMapper.writeValueAsString(resourcesObserved));
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) TestResource(org.entando.kubernetes.fluentspi.TestResource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Aggregations

TestResource (org.entando.kubernetes.fluentspi.TestResource)17 Description (io.qameta.allure.Description)15 SerializedEntandoResource (org.entando.kubernetes.controller.spi.client.SerializedEntandoResource)15 Test (org.junit.jupiter.api.Test)15 BasicDeploymentSpecBuilder (org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder)10 ValueHolder (org.entando.kubernetes.test.common.ValueHolder)10 StartupEvent (io.quarkus.runtime.StartupEvent)8 Pod (io.fabric8.kubernetes.api.model.Pod)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)5 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)5 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)5 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)5 Allure.attachment (io.qameta.allure.Allure.attachment)5 Allure.step (io.qameta.allure.Allure.step)5 Feature (io.qameta.allure.Feature)5 Issue (io.qameta.allure.Issue)5 IOException (java.io.IOException)5 Map (java.util.Map)5 TimeUnit (java.util.concurrent.TimeUnit)5